'Optimize Uniform Transmission
I'm working on an OpenGL renderer in C++, but I ran into a massive performance problem. The way I am setting Uniforms is I push the uniform value to a material class that stores all of the pushed uniforms in an std::map. I then have to loop over all of the uniforms I pushed to check what type of uniform it is and then send the uniform to the shader program. It's incredibly slow and CPU usage is greatly increased from ~9% to over 20%. The obvious way to fix this is to not store the uniforms and just send them in the renderer, but material specific uniforms can't be sent like that because every object has a different material. Here's the code that I run to set the uniforms:
void Material::SetUniforms(){
for(auto it = pm_textures.begin(); it != pm_textures.end(); it++){
switch(it->second.type){
case UTYPE_SAMPLERCUBE:
std::get<Cubemap*>(it->second.texture)->Bind(it->second.slot);
break;
default:
std::get<Texture*>(it->second.texture)->Bind(it->second.slot);
break;
}
}
for(auto it = pm_uniforms.begin(); it != pm_uniforms.end(); it++){
switch(it->second.type){
case UTYPE_INT:
pm_shader->SetUniform1i(it->first, std::get<int>(it->second.value));
break;
case UTYPE_FLOAT:
pm_shader->SetUniform1f(it->first, std::get<float>(it->second.value));
break;
case UTYPE_VEC2:
pm_shader->SetUniformVec2(it->first, std::get<glm::vec2>(it->second.value));
break;
case UTYPE_VEC3:
pm_shader->SetUniformVec3(it->first, std::get<glm::vec3>(it->second.value));
break;
case UTYPE_VEC4:
pm_shader->SetUniformVec4(it->first, std::get<glm::vec4>(it->second.value));
break;
case UTYPE_MAT2:
pm_shader->SetUniformMat2(it->first, std::get<glm::mat2>(it->second.value));
break;
case UTYPE_MAT3:
pm_shader->SetUniformMat3(it->first, std::get<glm::mat3>(it->second.value));
break;
case UTYPE_MAT4:
pm_shader->SetUniformMat4(it->first, std::get<glm::mat4>(it->second.value));
break;
default:
std::cout << "Material : unknown uniform type" << std::endl;
break;
}
}
}
I need a better way to set all of the uniforms because this way is very hard on my system.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
