在顶点着色器中声明常量而不是文字.标准做法,还是不必要的严厉? [英] Declaring constants instead of literals in vertex shader. Standard practice, or needless rigor?

查看:110
本文介绍了在顶点着色器中声明常量而不是文字.标准做法,还是不必要的严厉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

顶点着色器中,当然只允许有限的统一存储,而且据我了解,不同的系统可能会以略有不同的方式实现GLSL编译代码的条款.我听说过建议使用常量而不是在顶点着色器代码中写出文字.

In a vertex shader, there is of course a limited amount of uniform storage allowed, and it is my understanding that different systems may implement GLSL in slightly different ways in terms of compiling code. I've heard the recommendation to use constants instead of writing out literals in the vertex shader code.

例如,以下代码可能应该会导致可用统一存储空间的减少. (我不太了解.)

For instance, the following code could supposedly result in a reduction in available uniform storage. (I don't quite understand how.)

示例1:带有文字

vec4 myVector = vec4(1.0, 0.0, 0.0, 1.0);

据我了解,每次使用1.00.0都有可能占用一定数量的统一存储空间.因此,建议将先前的代码转换为如下代码:

To my understanding, there is the possibility that each use of 1.0or 0.0 takes up some amount of the uniform storage space. The recommendation is therefore to turn that previous code into something like the following:

示例2:使用常量而不是文字

const float zero = 0.0;
const float one = 1.0;

vec4 myVector = vec4(one, zero, zero, one);

有人知道发生了什么事吗?我的代码没有任何问题,我只是想正确地理解这些内容,以便将来不会出现任何问题.

Does anyone understand the argument behind what's going on? I'm not having any problems with code, I'm just trying to understand the stuff properly so that I don't have problems in the future.

我的正式问题如下:专门针对使用 OpenGL ES 2.0 iOS 平台,写出东西的最佳实践带有文字(示例1)或常量(示例2).我应该花时间每次都用常量来写东西,还是应该写出文字并仅在顶点着色器无法正确编译的情况下才使用常量?

My formal question is the following: specifically for the iOS platform using OpenGL ES 2.0, is the best practice to write out the thing with the literals (example 1), or with constants (example 2). Should I spend my time writing things out with constants each and every time, or should I write out literals and only use constants if the vertex shader fails to compile properly?

谢谢!

推荐答案

关于Kimi关于未在规范中找到任何内容的提法,请参见

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.

这可能是Kimi引用的OpenGL®ES 2.0编程指南中的推荐内容.

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

但是,该规范没有强制要求该限制,并且大概可以对任何实现进行自由改进,但是关于iOS GL驱动程序,我找不到任何一种方式.

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?

(对不起...我打算将此答案发布为对Kimi的回答的评论,但还没有要求的50 Rep分).

这篇关于在顶点着色器中声明常量而不是文字.标准做法,还是不必要的严厉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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