Change shaders on the fly


Hi guys!

I have a question, is it a good idea to change shaders for the node on the fly?

I have a RenderTexture that most of the time is plain, yet when times come for the explosion to occur it runs quite costly displacement shader to render a shockwave. And I want to turn it of when its not needed (it has already enough If statements in it, I don’t want to add more).

I wonder if change of the shader programm operation is costly and if swaping shaders on the fly is a good idea or bad?


Yep, change shader program is costly. You can have a try with your app.


Uhmm… according to the following code:

void CCNode::setShaderProgram(CCGLProgram *pShaderProgram)
    m_pShaderProgram = pShaderProgram;

changing shader program is quite cheap. Cocos2dx internally calls CCGLProgram::use to change shader program for different types of nodes. So, I don’t think that calling that function one more time (in several thousands of frames) won’t affect performance at all.


I belive zhangxm told about change or render-state because of new shader in render-que.


isn’t it the purpose of shadercache?
that we can cache all shaders so we can switch on the fly instead of compiling and linking on the fly?