问题
I don't know how uniforms are represented in memory.
Uniforms seem like they could take up valuable register space, but they're ultimately passed in/through/out into global memory, right?
Does the situation change when the uniforms are unused? Can the compiler optimize them away?--I have gotten invalid (-1) as a binding location when this is the case, so I assume yes.
回答1:
First of all the GLSL specification doesn't say anything about the actual implementation of it's concepts, so the following elaborations are of course to be read as "could be any way, but nowadays it's usually that way".
As to my (maybe limited) knowledge about graphics hardware, uniforms usually live in the so-called constant memory, which is part of the global device memory (and on newer hardware should even be cached), since they cannot be changed by the shader program anyway and are global to all invocations of the program (that could and should run on different multiprocessors in parallel). So they don't take up any per-multiprocessor register space themselves.
You are also right in that the GLSL compiler can (and usually will) optimize away any unused uniforms (and also attributes), but only if those are not used in any possible branch of execution, of course. So what you experienced with getting a uniform location of -1 is perfectly valid (and usually desired) behaviour.
回答2:
Uniforms are represented in whatever manor the GLSL compiler and OpenGL implementation deem fit. Some implementations make certain uniforms actual constants within the assembly, such that changing a uniform is actually patching the assembly in-situ. Some have special memory for uniforms.
It's all hardware dependent.
Compilers are allowed to optimize uniforms out; this is where the term "active uniform" comes from. The OpenGL API for querying uniform information only works for active uniforms: those that are actually detected to be in-use by the compiler.
来源:https://stackoverflow.com/questions/12148992/do-unused-glsl-uniforms-in-out-contribute-to-register-pressure