在OpenGL ES 2.0/GLSL中,您在哪里需要精度说明符? [英] In OpenGL ES 2.0 / GLSL, where do you need precision specifiers?

查看:322
本文介绍了在OpenGL ES 2.0/GLSL中,您在哪里需要精度说明符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您要填充值的变量是否在等号的右边指示您正在使用的精度?

Does the variable that you're stuffing values into dictate what precision you're working with, to the right of the equals sign?

例如,这里的精度说明符在含义上有什么区别:

For example, is there any difference, of meaning, to the precision specifier here:

gl_FragColor = lowp vec4(1);

这是另一个例子:

lowp float floaty = 1. * 2.;
floaty = lowp 1. * lowp 2.;

如果您使用一些浮点数,并从它们中创建一个向量或矩阵,那么该向量或矩阵将采用您填充其的值的精度,还是将这些值转换为另一个精度级别?

And if you take some floats, and create a vector or matrix from them, will that vector or matrix take on the precision of the values you stuff it with, or will those values transform into another precision level?

我认为优化此方法将最好地回答以下问题:

I think optimizing this would best answer the question:

dot(gl_LightSource[0].position.xyz, gl_NormalMatrix * gl_Normal)

我的意思是,如果您想尽可能快地将它走得更远,还是有些没用?

I mean, does it need to go this far, if you want it as fast as possible, or is some of it useless?

lowp dot(lowp gl_LightSource[0].position.xyz, lowp gl_NormalMatrix * lowp gl_Normal)

我知道您可以为float定义默认精度,并且此后应该将其用于矢量和矩阵.假设出于教育目的,我们之前已经对此进行了定义:

I know you can define the default precision for float, and that this supposedly is used for vectors and matrices afterwards. Assume for the purpose of education, that we had defined this previously:

precision highp float;

推荐答案

  1. 您不需要常量/字面量的精确说明符,因为它们可以根据分配给它们的值进行编译时评估.

  1. You don't need precision specifiers on constants/literals since those get compile time evaluated to whatever they are being assigned to.

在顶点着色器中,默认情况下声明以下精度:(4.5.3 Default Precision Qualifiers)

In vertex shaders, the following precisions are declared by default: (4.5.3 Default Precision Qualifiers)

precision highp float;
precision highp int;
precision lowp sampler2D;
precision lowp samplerCube;

在片段着色器中,您将得到:

And in fragment shaders you get:

precision mediump int;
precision lowp sampler2D;
precision lowp samplerCube;

这意味着,如果您在片段着色器中声明浮点数,则必须说出它是lowp还是mediump.默认的float/int精度也扩展到矩阵/向量.

This means that if you declare a float in a fragment shader, you have to say whether it is a lowp or a mediump. The default float/int precisions also extend to matrices/vectors.

highp;剩下的你会得到一个编译器错误. (4.5.4 Available Precision Qualifiers)

highp is only supported on systems that have the GL_FRAGMENT_PRECISION_HIGH macro defined to 1; on the rest you'll get a compiler error. (4.5.4 Available Precision Qualifiers)

表达式中的精度规则是它们会自动转换为绑定到的赋值/参数的类型.因此,对于您的点,默认情况下它将使用输入类型的精度,并且不需要附加的lowp(在语法上不正确).如果要将类型下转换为较低的精度,唯一的方法是将其显式分配为较低的精度.

The rule for precision in an expression is that they get cast automatically to the type of the assignment / parameter they are bound to. So for your dot, it would use the precision of the input types by default and the additional lowp's are unnecessary (and syntactically incorrect). If you want to down-cast a type to a lower precision, the only way to do it is to explicitly assign it to a lower precision.

这些答案全部来自Khronos GLSL规范,您可以在此处找到(相关部分为4.5.2和4.5.3):

These answers are all from the Khronos GLSL spec, which you can find here (relevant sections are 4.5.2 and 4.5.3): http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf

这篇关于在OpenGL ES 2.0/GLSL中,您在哪里需要精度说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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