着色器代码中的 for 循环使用硬编码数字但不使用

for-loop in shader code working with hardcoded number but not with uniform variable(着色器代码中的 for 循环使用硬编码数字但不使用统一变量)
本文介绍了着色器代码中的 for 循环使用硬编码数字但不使用统一变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 中寻求有关 OpenGL ES 2.0 问题的帮助这个问题.似乎答案对我来说很奇怪.因此,我决定提出这个问题,希望能够了解发生了什么.

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.

这是一段错误的顶点着色器代码:

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...
}

我知道这看起来很奇怪,但这实际上是解释问题/错误行为所需的所有代码.

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

我的问题是:为什么当我为 u_lights_active 传入一些大于 0 的值时,循环没有被执行?当我硬编码一些整数时,例如4、不用统一的u_lights_active,它工作得很好.

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 在两个平台上运行相同的代码.

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

如果需要更多信息,您可以查看 original问题,但我不想在这里复制和粘贴所有内容.我希望这种保持简短的方法受到赞赏,否则我将复制所有内容.

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 指定实现可以限制循环在它们上具有恒定"边界.这是为了更简单地优化代码以并行运行(不同像素的不同循环计数会很复杂).我相信在某些实现中,常数甚至必须很小.请注意,规范仅指定最小"行为,因此某些设备可能支持比规范要求更复杂的循环控制.

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.

这是一个很好的约束总结: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 节):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 循环使用硬编码数字但不使用统一变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)