我学习WebGL。下一个着色器可以正常工作:
// vertex.shader
// precision mediump float;
attribute vec4 a_Position;
attribute float a_PointSize;
void main(){
gl_Position = a_Position;
gl_PointSize = a_PointSize;
}
和
// fragment.shader
precision mediump float;
uniform vec4 u_FragColor;
void main(){
gl_FragColor = u_FragColor;
}
为什么需要为片段着色器设置精度?没有此功能,顶点着色器就可以工作,但是没有此代码行,片段着色器就无法工作(如我所见)。为什么存在不同的行为?
我以前读过this,但是对我没有帮助。
最佳答案
OpenGL ES 2.0中的片段着色器中的fp类型不存在默认精度。
在顶点着色器中,如果未显式设置浮点类型的默认精度,则默认为highp
。但是,如果片段着色器也默认设置为highp
,则将导致问题,因为OpenGL ES 2.0不需要在片段着色器阶段支持高精度浮点类型。
OpenGL ES Shading Language - 4. Variables and Types - pp. 35-36
The fragment language has no default precision qualifier for floating point types. Hence for float, floating point vector and matrix variable declarations, either the declaration must include a precision qualifier or the default float precision must have been previously declared.
4.5.4 Available Precision Qualifiers
The built-in macro
GL_FRAGMENT_PRECISION_HIGH
is defined to one on systems supportinghighp
precision in the fragment language
#define GL_FRAGMENT_PRECISION_HIGH 1
and is not defined on systems not supporting
highp
precision in the fragment language. When defined, this macro is available in both the vertex and fragment languages. Thehighp
qualifier is an optional feature in the fragment language and is not enabled by#extension
.
关于opengl-es - 为什么必须为片段着色器设置精度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28540290/