[SOLVED] Shaders won't update

I have that code in my init method

auto p = CreateGLProgram("Shaders/Object.vsh", "Shaders/Object.fsh");
sprite->setGLProgram(p);

CreateGLProgram:

static cocos2d::GLProgram* CreateGLProgram(const char* ver, const char* frag) {
  auto p = cocos2d::GLProgram::createWithFilenames(ver, frag);
  p->link();
  CHECK_GL_ERROR_DEBUG();
  p->updateUniforms();
  CHECK_GL_ERROR_DEBUG();
  p->retain();
  p->bindAttribLocation(cocos2d::GLProgram::ATTRIBUTE_NAME_POSITION, cocos2d::GLProgram::VERTEX_ATTRIB_POSITION);
  p->bindAttribLocation(cocos2d::GLProgram::ATTRIBUTE_NAME_COLOR, cocos2d::GLProgram::VERTEX_ATTRIB_COLOR);
  p->bindAttribLocation(cocos2d::GLProgram::ATTRIBUTE_NAME_TEX_COORD, cocos2d::GLProgram::VERTEX_ATTRIB_TEX_COORD);

  return p;
};

This works. But if i change color in my shader color doesn’t change after rerun. If I change something in this code (Add comment for example). This code works again. Then if I replace color in shader with another it’s not working again and so on. I need to recompile that part of code when changing shader code. Why?

Looks like cocos2dx caches glsl code

1 Like

How to disable caching?

Shouldn’t you pass your defined uniforms to the shader program via those setUniformXXX methods?

    /** @{
     Setting user defined uniforms by uniform string name in the shader.
     */
    void setUniformInt(const std::string& uniformName, int value);
    void setUniformFloat(const std::string& uniformName, float value);
    void setUniformFloatv(const std::string& uniformName, ssize_t size, const float* pointer);
    void setUniformVec2(const std::string& uniformName, const Vec2& value);
    void setUniformVec2v(const std::string& uniformName, ssize_t size, const Vec2* pointer);
    void setUniformVec3(const std::string& uniformName, const Vec3& value);
    void setUniformVec3v(const std::string& uniformName, ssize_t size, const Vec3* pointer);
    void setUniformVec4(const std::string& uniformName, const Vec4& value);
    void setUniformVec4v(const std::string& uniformName, ssize_t size, const Vec4* pointer);
    void setUniformMat4(const std::string& uniformName, const Mat4& value);
    void setUniformCallback(const std::string& uniformName, const std::function<void(GLProgram*, Uniform*)> &callback);
    void setUniformTexture(const std::string& uniformName, Texture2D *texture);
    /**
     * @deprecated, please use setUniformTexture(const std::string& uniformName, Texture2D *texture) instead,
     * Passing a `textureId` may trigger texture lost issue (https://github.com/cocos2d/cocos2d-x/issues/16871).
     */
    CC_DEPRECATED_ATTRIBUTE void setUniformTexture(const std::string& uniformName, GLuint textureId);
    /**@}*/
    
    /** @{
     Setting user defined uniforms by uniform location in the shader.
     */
    void setUniformInt(GLint uniformLocation, int value);
    void setUniformFloat(GLint uniformLocation, float value);
    void setUniformFloatv(GLint uniformLocation, ssize_t size, const float* pointer);
    void setUniformVec2(GLint uniformLocation, const Vec2& value);
    void setUniformVec2v(GLint uniformLocation, ssize_t size, const Vec2* pointer);
    void setUniformVec3(GLint uniformLocation, const Vec3& value);
    void setUniformVec3v(GLint uniformLocation, ssize_t size, const Vec3* pointer);
    void setUniformVec4(GLint uniformLocation, const Vec4& value);
    void setUniformVec4v(GLint uniformLocation, ssize_t size, const Vec4* pointer);
    void setUniformMat4(GLint uniformLocation, const Mat4& value);
    void setUniformCallback(GLint uniformLocation, const std::function<void(GLProgram*, Uniform*)> &callback);
    void setUniformTexture(GLint uniformLocation, Texture2D *texture);

@ichinfungi Not yet. I just have hardcoded values right in .vsh and .fsh

Are you using XCode? It’s probably caching data file resources. If so, probably need to re-include the shader folder as “as reference” instead of “as group”, and should have blue icon instead of standard yellow.

I’m using CLion

No one else had the same issue?

[SOLVED] It was cmake issue

that block

      add_custom_command(TARGET ${APP_NAME} POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources $<TARGET_FILE_DIR:${APP_NAME}>${RES_PREFIX}
                   )

copies Resources directory only POST_BUILD so i need to recompile a part of code and my new resources will be copied into cmake-build-directory.

I wrote a quick fix for clion (bash script)

#!/bin/bash
cp -r ./Resources/. ./cmake-build-debug/bin/Resources/

and I added hook in run configuration that executes this script each run. So each run i have actual resources in my cmake path without need to recompile