For-loop in shader code working with hard code but not with a homogeneous variable

I asked for help on the OpenGL ES 2.0 issue in this matter . It seems that the answer is very strange for me. So I decided to ask this question in the hope of understanding what was going on.

Here is a fragment of an erroneous vertex shader code:

// a bunch of uniforms and stuff... uniform int u_lights_active; void main() { // some code... for ( int i = 0; i < u_lights_active; ++i ) { // do some stuff using u_lights_active } // some other code... } 

I know this looks weird, but this is really all the code that is needed to explain the problem / incorrect behavior.

My question is: why does the loop not execute when I pass some value greater than 0 for u_lights_active? When I hardcode an integer, for example. 4, instead of using uniform u_lights_active, it works fine.

One more thing, this only appears on Android, but not on the desktop. I use LibGDX to run the same code on both platforms.

If you need more information, you can look at the original question , but I do not want to copy and paste everything here. I hope that such an approach to its reduction will be appreciated, otherwise I will copy all this.

I look forward to explaining :)

Thanks!

+4
source share
2 answers

Basically, GLSL indicates that implementations can limit loops to have β€œconstant” boundaries on them. This makes it easier to optimize the code for parallel operation (different loop counts for different pixels will be difficult). I believe that in some implementations the constants should even be small. Note that the specification simply defines the minimum behavior, so some devices may support more sophisticated circuit controls than the specification requires.

Here's a good summary of the restrictions: http://www.khronos.org/webgl/public-mailing-list/archives/1012/msg00063.html

Here's the GLSL specification (see Section 4 of Appendix A): http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf

+10
source

http://www.opengl.org/discussion_boards/showthread.php/171437-can-for-loops-terminate-with-a-uniform

http://www.opengl.org/discussion_boards/showthread.php/177051-GLSL-loop-problem-on-Radeon-HD-cards

http://www.opengl.org/discussion_boards/showthread.php/162722-Problem-when-using-uniform-variable-as-a-loop-count-in-fragment-shader

https://www.opengl.org/discussion_boards/showthread.php/162535-variable-controlled-for-loops

If you have a static loop, it can be deployed and converted to static constant lookup. If you absolutely need to make it dynamic, you will need to store the indexed data in a 1D texture and sample, which are instead.

I guess the hardware on the desktop is more advanced than on the tablet. Hope this helps!

+2
source

Source: https://habr.com/ru/post/1501609/


All Articles