Lag or jerk as soon i implement shader [Code Inside][Solved]{Add Glprog to cache}

I have 10 - 15 sprites and i’m trying to shade them instead of using rendertexture cause i thought 10-15 for shading is a small number here is my code showing how i Implement and deimplement shader.

I dont thik deimplementation has to do anything with this problem cause it (lag or jerk) occurs as soon as shader is implemented on
10-15 sprites in a loop

like this

for(j=0; j<=TOTAL_SPRITES; j++){
            shadeSprite(sprite_array[j]);
}

Implementation

void TouchScene::shadeSprite(cocos2d::Sprite* sprite)
{
    if(sprite)
    {
        GLProgram * p = new GLProgram();
        p->initWithFilenames("gray.vsh", "gray.fsh");
        p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
        p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
        p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
        p->link();
        p->updateUniforms();
        sprite->setGLProgram(p);
    }
}

deimplementation (or removing shader)

void TouchScene::removeShader(cocos2d::Sprite* sprite){

    sprite->setGLProgramState(
       GLProgramState::getOrCreateWithGLProgramName(
      GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP
       )
    );
}

gray.fsh


varying vec4 v_fragmentColor;     
varying vec2 v_texCoord;     
         
void main()             
{ 
    vec4 v_orColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord); 
    float gray = dot(v_orColor.rgb, vec3(0.299, 0.587, 0.114)); 
    gl_FragColor = vec4(gray, gray, gray, v_orColor.a); 
}                

gray.vsh


attribute vec4 a_position; 
attribute vec2 a_texCoord; 
attribute vec4 a_color; 
                     
 
varying vec4 v_fragmentColor; 
varying vec2 v_texCoord; 
                                 
void main()     
{                             
    //gl_Position = CC_MVPMatrix * a_position; 
    gl_Position = CC_PMatrix * a_position; 
    v_fragmentColor = a_color; 
    v_texCoord = a_texCoord; 
}
1 Like

Okay i thought that lag might be due to creating new GLprogramme every time , so i thought to add it in cache but looks like i’m doing it wrong here is my code


void TouchScene::shadeSprite(cocos2d::Sprite* sprite)
{
    if(sprite)
    {
        
        auto gl_cache = GLProgramCache::getInstance();
        auto p = gl_cache->getGLProgram("hey");
        if(p == nullptr)
        {
            //prog = GLProgram::createWithByteArrays(gray.vsh, frag);
            auto p = new GLProgram();
            p->initWithFilenames("gray.vsh", "gray.fsh");
            p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
            p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
            p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
            p->link();
            p->updateUniforms();
            gl_cache->addGLProgram(p, "hey");
        }
        auto programState = GLProgramState::create(p);
        sprite->setGLProgramState(programState);
    }
}

but it says
Assert failed: invalid shader
MyGame: --/renderer/CCGLProgramState.cpp:423: bool cocos2d::GLProgramState::init(cocos2d::GLProgram*): Assertion `glprogram’ failed.
Aborted (core dumped)

Thank You

perhaps it failed because glprogram is nil.

please, post the traceback, or try to inspect the stackframe and see why it crashed (the assert).

But yeah… you should create the GLProgram once and then reuse it. Don’t create it multiple times. Consumes time, and memory!

From StackTrace i found that in this code


void TouchScene::shadeSprite(cocos2d::Sprite* sprite)
{
    if(sprite)
    {
        
        auto gl_cache = GLProgramCache::getInstance();
        auto p = gl_cache->getGLProgram("hey");
        if(p == nullptr)
        {
            //prog = GLProgram::createWithByteArrays(gray.vsh, frag);
            auto p = new GLProgram();
            p->initWithFilenames("gray.vsh", "gray.fsh");
            p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
            p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
            p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
            p->link();
            p->updateUniforms();
            gl_cache->addGLProgram(p, "hey");
        }
        auto programState = GLProgramState::create(p);
        sprite->setGLProgramState(programState);
    }
}

on line


auto programState = GLProgramState::create(p);

it is giving away null , GLProgramState::create§ is giving nil

Solved , adding GLprogramme to cache fixed it error was


if(p == nullptr)
        {
            auto p = new GLProgram();

i did it local :stuck_out_tongue:
but problem solved no more lag issue thanks @ricardo @namkazt