for循环中的着色器code硬codeD一些工作,但没有统一的变量 [英] for-loop in shader code working with hardcoded number but not with uniform variable

查看:276
本文介绍了for循环中的着色器code硬codeD一些工作,但没有统一的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问有关在<一的OpenGL ES 2.0的问题求助href="http://stackoverflow.com/questions/18735667/libgdx-shader-working-on-desktop-but-not-on-android">this问题。 什么似乎答案已经很奇怪了吧。 因此,我决定问这个问题在能够明白是怎么回事了希望。

I asked for help about an OpenGL ES 2.0 Problem in this question. What seems to be the answer is very odd to me. Therefore I decided to ask this question in hope of being able to understand what is going on.

下面是一段错误的顶点着色器code:

Here is the piece of faulty 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...
}

我知道这看起来很奇怪,但这是真的是需要解释的问题/错误行为的所有code。

I know this looks odd but this is really all code that is needed to explain the problem / faulty behavior.

我的问题是:为什么环没有得到当我通过一些数值越大0 u_lights_active执行? 当我硬code一些整数例如而是采用了统一的u_lights_active 4,它工作得很好。

My question is: Why is the loop not getting executed when I pass in some value greater 0 for u_lights_active? When I hardcode some integer e.g. 4, instead of using the uniform u_lights_active, it is working just fine.

还有一件事,这只是出现在Android上而不是在桌面上。我用LibGDX运行在这两个平台在同一code。

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

如果需要更多的信息,你可以看看<一href="http://stackoverflow.com/questions/18735667/libgdx-shader-working-on-desktop-but-not-on-android">original问题但我不想复制和粘贴在这里所有的东西。 我希望保持它短的这种做法是AP preciated,否则我将复制所有的东西了。

If more information is needed you can look at the original question but I didn't want to copy and paste all the stuff here. I hope that this approach of keeping it short is appreciated, otherwise I will copy all the stuff over.

我很期待一个解释:)

谢谢!

推荐答案

基本上GLSL指定的实现可能会限制循环,以对他们的常量范围。这是为了使其更简单,以优化code键并行运行(不同循环计数不同像素将是复杂的)。相信在某些实现中,常数甚至要小。请注意,该规范只规定了最低的行为,所以一些设备可能支持更复杂的回路控制比规范要求。

Basically GLSL specifies that implementations may restrict loops to have "constant" bounds on them. This is to make it simpler to optimize the code to run in parallel (different loop counts for different pixels would be complex). I believe on some implementations the constants even have to be small. Note that the spec just specifies the "minimum" behavior, so some devices might support more complex loop controls than the spec requires.

下面是制约一个很好的总结: <一href="http://www.khronos.org/webgl/public-mailing-list/archives/1012/msg00063.html">http://www.khronos.org/webgl/public-mailing-list/archives/1012/msg00063.html

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

下面是GLSL规范(看附录A的第4条): <一href="http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf">http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf

Here's the GLSL spec (look at section 4 of Appendix A): http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf

这篇关于for循环中的着色器code硬codeD一些工作,但没有统一的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆