vendredi 9 août 2019

decoupling custom user data

I have a custom shader generation.

class My_UniformInt { public:
    std::string idName;
    int glsl_id;  //<-- userData-like field
};
class My_Shader{ public:
    My_UniformInt textureCoordinate;
    public: My_Shader(){
        textureCoordinate.idName="textureCoordinate";
    }
};
void registerUniform( My_Shader& shader, My_UniformInt & uniInt){
    uniInt.glsl_id=5; 
    //vec3.glsl_id=glGetUniformLocation(uniInt.idName);
}
void passUniformInt( My_Shader& shader, My_UniformInt & uniInt,int k){
    std::cout<<"successfully pass name="<<uniInt.idName<<" va="<<k<<std::endl;
    //glUniform1i(uniInt.glsl_id,k);
}

I can generate shader and use it like :-

int main(){
    My_Shader shader;
    registerUniform( shader , shader.textureCoordinate ) ;
    //.. several frame later
    passUniformInt( shader , shader.textureCoordinate ,  5 ) ;
    return 0;
}

It works and fast (MCVE), but there is undesirable coupling.
1. My_UniformInt caches glsl-specific variable (glsl_id).
2. My_UniformInt is also emotionally attached to the glGetUniformLocation.

How to make My_UniformInt more independent without sacrificing speed?

I have several cases that has similar issues. Unfortunately, the most obvious choice usually seems to be adding more userData-like fields. It increases coupling.

My poor solutions :-

  1. Make glsl_id as void* userData, then assign userData=new int()
    It is uncool + has a little cost + need to be deleted later.

  2. Create a map std::map<My_UniformInt*,int glsl_id>. I can't afford the cost though.

Aucun commentaire:

Enregistrer un commentaire