Search code examples
openglglsl

Why are shader-stage input variables not writable


The GLSLangSpec.3.30 says:

Values from the previous pipeline stage are copied into input variables at the beginning of shader execution. Variables declared as in or centroid in may not be written to during shader execution.

If they are copied in and the original values are left untouched, why are input variables not writable?


Solution

  • The standard describes behavior, not implementation. Thus, the statement about being "copied in" merely describes the apparent effect, not what the actual hardware does.

    Indeed, the whole point of these two specific requirements is to allow implementations to not copy inputs into specific variables. Or rather, to allow VS implementations to avoid having to allocate storage for a variable if it doesn't need to. If an implementation wants to make a use of a shader-stage input variable read from the buffer directly (or a cache), it can do so.

    Now yes, you can still have that implementation with the ability to modify in variables. But the compiler would have to check to see if the shader does modify them. So it'd be a lot easier to implement such optimizations (where relevant) if the compiler didn't have to check to see if you're doing something you shouldn't be doing.