Skip to content

Commit dc3ad27

Browse files
committed
Fixed a minor issue with detecting default uniforms in CCNode.
1 parent 3993fbc commit dc3ad27

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

cocos2d/CCNode.m

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,13 +1736,32 @@ -(BOOL) doesOpacityModifyRGB{
17361736

17371737
#pragma mark - RenderState Methods
17381738

1739+
// The default dictionary is either nil or only contains the main texture.
1740+
static inline BOOL
1741+
CheckDefaultUniforms(NSDictionary *uniforms, CCTexture *texture)
1742+
{
1743+
if(uniforms == nil){
1744+
return YES;
1745+
} else {
1746+
// Check that the uniforms has only one key for the main texture.
1747+
return (uniforms.count == 1 && uniforms[CCShaderUniformMainTexture] == texture);
1748+
}
1749+
}
1750+
17391751
-(CCRenderState *)renderState
17401752
{
17411753
if(_renderState == nil){
1742-
if(_shaderUniforms.count > 1){
1743-
_renderState = [[CCRenderState alloc] initWithBlendMode:_blendMode shader:_shader shaderUniforms:_shaderUniforms];
1754+
CCTexture *texture = (_texture ?: [CCTexture none]);
1755+
1756+
if(CheckDefaultUniforms(_shaderUniforms, texture)){
1757+
// Create a cached render state so we can use the fast path.
1758+
_renderState = [CCRenderState renderStateWithBlendMode:_blendMode shader:_shader mainTexture:texture];
1759+
1760+
// If the uniform dictionary was set, it was the default. Throw it away.
1761+
_shaderUniforms = nil;
17441762
} else {
1745-
_renderState = [CCRenderState renderStateWithBlendMode:_blendMode shader:_shader mainTexture:(_texture ?: [CCTexture none])];
1763+
// Since the node has unique uniforms, it cannot be batched or use the fast path.
1764+
_renderState = [[CCRenderState alloc] initWithBlendMode:_blendMode shader:_shader shaderUniforms:_shaderUniforms];
17461765
}
17471766
}
17481767

@@ -1768,10 +1787,7 @@ -(CCBlendMode *)blendMode
17681787
-(NSMutableDictionary *)shaderUniforms
17691788
{
17701789
if(_shaderUniforms == nil){
1771-
_shaderUniforms = [NSMutableDictionary dictionaryWithObjectsAndKeys:
1772-
(_texture ?: [CCTexture none]), CCShaderUniformMainTexture,
1773-
nil
1774-
];
1790+
_shaderUniforms = [NSMutableDictionary dictionaryWithObject:(_texture ?: [CCTexture none]) forKey:CCShaderUniformMainTexture];
17751791

17761792
_renderState = nil;
17771793
}

0 commit comments

Comments
 (0)