GLSL 自动优化

GLSL auto optimization

我有三个关于 GLSL 自动优化(?)过程的问题。

  1. 未使用的变量 -> 是否 glsl 删除了所有不影响最终片段着色器像素的变量(输出变量)。

  2. 未使用的函数 -> 是否 glsl 删除了 void main 之前所有未定义的函数...?

  3. 还有来龙去脉的变量呢。一个例子:我有 100 个着色器,它们将纹理坐标从顶点着色器发送到片段着色器。在片段着色器中,这些坐标不会影响最终颜色。 glsl 会删除这个变量吗?

没有明确说明。 OpenGL 规范说:

OpenGL 4.6 Core Profile Specification - 7.6 Uniform Variables - p. 130:

7.6 Uniform Variables

Shaders can declare named uniform variables, as described in the OpenGL Shading Language Specification. A uniform is considered an active uniform if the compiler and linker determine that the uniform will actually be accessed when the executable code is executed. In cases where the compiler and linker cannot make a conclusive determination, the uniform will be considered active.

参见OpenGL 4.6 Core Profile Specification - 7.3.1 Program Interfaces - p. 101

7.3.1 Program Interfaces

When a program object is made part of the current rendering state, its executable code may communicate with other GL pipeline stages or application code through a variety of interfaces. When a program is linked, the GL builds a list of active resources for each interface. Examples of active resources include variables, interface blocks, and subroutines used by shader code. Resources referenced in shader code are considered active unless the compiler and linker can conclusively determine that they have no observable effect on the results produced by the executable code of the program. For example, variables might be considered inactive if they are declared but not used in executable code, used only in a clause of an if statement that would never be executed, used only in functions that are never called, or used only in computations of temporary variables having no effect on any shader output. In cases where the compiler or linker cannot make a conclusive determination, any resource referenced by shader code will be considered active. The set of active resources for any interface is implementation-dependent because it depends on various analysis and optimizations performed by the compiler and linker.

If a program is linked successfully, the GL will generate lists of active resources based on the executable code produced by the link.