Declaring constants instead of literals in vertex shader. Standard practice, or needless rigor?

前端 未结 2 582
谎友^
谎友^ 2021-01-13 06:45

In a vertex shader, there is of course a limited amount of uniform storage allowed, and it is my understanding that different systems may i

相关标签:
2条回答
  • 2021-01-13 07:11

    From the OpenGL® ES 2.0 Programming Guide

    As far as literal values are concerned, the OpenGL ES 2.0 shading language spec states that no constant propagation is assumed. This means that multiple instances of the same literal value(s) will be counted multiple times. Instead of using literal values, appropriate const variables should be declared. This avoids having the same literal value count multiple times, which might cause the vertex shader to fail to compile if vertex uniform storage requirements exceed what the implementation supports.

    I could not find anything related to this in the actual spec. Also there is no information specific to the iOS.

    Also you can check a GLSL Optimizer tool written to tackle this issue (and lots of others).

    0 讨论(0)
  • 2021-01-13 07:16

    Regarding Kimi's mention about not finding anything in the spec, Appendix A-7 of The OpenGL® ES Shading Language spec does include the following:

    When calculating the number of uniform variables used, any literal constants present in the shader source after preprocessing are included when calculating the storage requirements. Multiple instances of identical constants should count multiple times.

    This is probably the source of the recommendation in OpenGL® ES 2.0 Programming Guide that Kimi quotes.

    However, the spec does not mandate this restriction, and presumably any implementation is free to improve on it, but I cannot find anything either way regarding the iOS GL drivers.

    I'm curious, did anyone actually follow up on the ideas of overloading a sample shader with literals, in an attempt to reach any potential maximum uniform limit?

    (Sorry...I had intended to post this answer as a comment to Kimi's answer, but don't have the required 50 Rep points yet).

    0 讨论(0)
提交回复
热议问题