Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Shader prevent optimizer from removing attributes/uniforms

Tags:

opengl

Is there any way to prevent the shader compiler from removing a uniform/attribute which it detects as not being used? I occassionally comment out parts of my shader for testing, but this causes problems in the rest of my program because suddenly certain names no longer exist (thus causing lookup errors, and errors when trying to set the value).

like image 712
edA-qa mort-ora-y Avatar asked Aug 21 '12 08:08

edA-qa mort-ora-y


1 Answers

No, but it's not strictly necessary, depending on how you wrote your code.

The glUniform* functions will happily take a uniform location of -1. And if you're using program_pack420 and explicit_attrib_location, you can put your attribute indices, fragment shader outputs, UBO bindings, and texture unit bindings all in the shader. So you don't have to query for active attributes, outputs, uniform blocks, or samplers.

Note that we also have ARB_explicit_uniform_location in GL 4.3. So you can specify them in the shader, and they won't be optimized out.

The rest of the program needs that attribute to work, otherwise it'd be littered with a bunch of if statements -- which I'm trying to avoid.

The only reason you would encounter this is if you were not giving OpenGL attribute indices yourself, either with explicit_attrib_location or with calls glBindAttribLocation pre-linking. That's terrible coding, and you shouldn't have been doing it that way.

Always tell OpenGL what your attribute locations are. You should never be querying them unless you're writing a shader introspection tool.

like image 159
Nicol Bolas Avatar answered Oct 21 '22 07:10

Nicol Bolas