in cocos2d-2.1beta3-x-2.1.0 using any of my own shaders leads to invisible sprite

Hi guys!

In cocos2d-2.0-rc0a-x-2.0 i successfully use my own shaders. But when i migrated my project to cocos2d-2.1beta3-x-2.1.0 all sprites with my any of my shaders has disappeared.

below is snippet of code that work properly with cocos2d-2.0-rc0a-x-2.0 but don’t with cocos2d-2.1beta3-x-2.1.0

CCGLProgram *pBWShaderProgram = new CCGLProgram();

pBWShaderProgram->autorelease();

pBWShaderProgram->initWithVertexShaderFilename("BlackAndWhite.vsh", "BlackAndWhite.fsh");

pBWShaderProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);

pBWShaderProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);

pBWShaderProgram->link();

pBWShaderProgram->updateUniforms();

CCShaderCache::sharedShaderCache()->addProgram(pBWShaderProgram, kBWShaderProgram);

CCSprite *pSprite = CCSprite::spriteWithFile("mySprite.png");

addChild(pSprite, 10);

pDisabledSprite->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kBWShaderProgram));

pDisabledSprite->getShaderProgram()->use();

and here is shaders code:

BlackAndWhite.vsh

attribute vec4 a_position;
attribute vec2 a_texCoord;

uniform mat4 u_MVPMatrix;

#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif

void main()
{
    gl_Position = u_MVPMatrix * a_position;
    v_texCoord = a_texCoord;
}

and BlackAndWhite.fsh

#ifdef GL_ES
precision mediump float;
#endif

varying vec2 v_texCoord;
uniform sampler2D u_texture;

void main()
{
    vec4 normalColor = texture2D(u_texture, v_texCoord);//.rgba;
    float gray = 0.299*normalColor.r + 0.587*normalColor.g + 0.114*normalColor.b;
    gl_FragColor = vec4(gray, gray, gray, normalColor.a);
}

Maybe someone got the same issue and figured out hot to fix it?

Unfortunately I 2nd this.
I’m experiencing exactly the same issue using a shader with a custom CCLayerColor node.
Any suggestion/improvements on this?

Hi, Sherry Haibara. I have created almost the same post after this and Minggo Zhang told me that they changed uniform name from u_MVPMatrix to CC_MVPMatrix. And you should not to declare this variable because they add code responsible for declaration CC_MVPMatrix to you shader before it has been compiled. So i just comment out declaration line: //uniform mat4 u_MVPMatrix; and in the rest of my code replaced u_MVPMatrix to CC_MVPMatrix.

here is Minggo Zhang response to my question: http://www.cocos2d-x.org/boards/6/topics/19548?r=19598#message-19598

Mikhail andMinggo Thanks a lot I was starting to think I was crazy (:

I am just starting to play around with shaders and am trying to use your post and the other one above as a jumping off point. I have made the changes mentioned regarding the CC_MVPMatrix and such. The issue is I am getting the following error:

Cocos2d: cocos2d: ERROR: 0:23: Use of undeclared identifier 'gl_FragColor'

Code:

    CCGLProgram *pBWShaderProgram = new CCGLProgram();
    pBWShaderProgram->autorelease();
    pBWShaderProgram->initWithVertexShaderFilename("shaders/BlackAndWhite.vsh", "shaders/BlackAndWhite.fsh");
    pBWShaderProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    pBWShaderProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
    pBWShaderProgram->link();
    pBWShaderProgram->updateUniforms();

    CCShaderCache::sharedShaderCache()->addProgram(pBWShaderProgram, "kBWShaderProgram");

    CCSprite *tempSprite = CCSprite::createWithSpriteFrameName("landingStart.png");
    tempSprite->setPosition(ccp(winSize.width/2, winSize.height/2));
    this->addChild(tempSprite);

    tempSprite->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey("kBWShaderProgram"));
    tempSprite->getShaderProgram()->use();

Any ideas?

EDIT:
Had the wrong code in the wrong files (vertex vs fragment)