I have a subclassed sprite with a custom shader in a cocos2d 2.1 / kobold2d project (ARC enabled). i'm getting a crash in CCGLProgram when changing scenes. dealloc is being called and i crash on an NSAssert that: 'Vertex Shaders should have been already deleted'
the shader is defined first as CCGLProgam object "theShaderProgram" and is declared as an instance variable in the header file and defined in the init method.
the shader is called at some point with:
[self shaderOn:self];
via the method:
-(void) shaderOn:(CCNode*)noder {
noder.shaderProgram = theShaderProgram;
[noder.shaderProgram addAttribute:kCCAttributeNamePosition index:kCCVertexAttrib_Position ];
[noder.shaderProgram addAttribute:kCCAttributeNameTexCoord index:kCCVertexAttrib_TexCoords ];
[noder.shaderProgram link ];
[noder.shaderProgram updateUniforms ];
}
i tried adding:
-(void) onExit {
self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTextureColor];
}
for reference, my shader is from birkemose over at cocos2d:
theShaderProgram = [[CCGLProgram alloc]
initWithVertexShaderByteArray:
" \n\
attribute vec4 a_position; \n\
attribute vec2 a_texCoord; \n\
\n\
varying vec2 v_texCoord; \n\
\n\
void main( ) { \n\
gl_Position = CC_MVPMatrix * a_position; \n\
v_texCoord = a_texCoord; \n\
} \n\
"
fragmentShaderByteArray:
" \n\
uniform sampler2D CC_Texture0; \n\
\n\
varying vec2 v_texCoord; \n\
\n\
void main( ) { \n\
// old default shader \n\
// gl_FragColor = texture2D( CC_Texture0, v_texCoord ); \n\
// new greyscale shader \n\
vec4 color = texture2D( CC_Texture0, v_texCoord ); \n\
float grey = color.r * 0.212 + color.g * 0.715 + color.b * 0.072; \n\
gl_FragColor = vec4( grey, grey, grey, color.a ); \n\
} \n\
"
];
figured it out; i was creating a new instance of CCGLProgram but never running [link] which deletes the shader.