GLSL Half (floating point) attribute type

匿名 (未验证) 提交于 2019-12-03 02:43:01

问题:

I have been trying to get a 16bit float (half-floating point) as an attribute into my GLSL vertex shader. It won't let me compile saying:

error C7506: OpenGL does not define the global type half 

but my #version is 410 and so it should support half? Am I missing something obvious?

回答1:

In the absence of a MCVE demonstrating otherwise I assume you tried something like:

half float aHalfFloat; 

However, "half" is a reserved keyword in #version 410:

OpenGL Shading Language 4.10 Specification, page 15 (emphasis mine):

The following are the keywords reserved for future use. Using them will result in an error:

common partition active asm class union enum typedef template this packed goto inline noinline volatile public static extern external interface long short half fixed unsigned superp input output hvec2 hvec3 hvec4 fvec2 fvec3 fvec4 sampler3DRect filter image1D image2D image3D imageCube iimage1D iimage2D iimage3D iimageCube uimage1D uimage2D uimage3D uimageCube image1DArray image2DArray iimage1DArray iimage2DArray uimage1DArray uimage2DArray image1DShadow image2DShadow image1DArrayShadow image2DArrayShadow imageBuffer iimageBuffer uimageBuffer sizeof cast namespace using row_major In addition, all identifiers



回答2:

OpenGL and OpenGL ES define two concurrent types of precision.

  • Storage precision in buffers
  • Minimum computational precision used in shaders.

Storage precision is defined by your vertex attribute upload, such as GL_FLOAT or GL_HALF_FLOAT. This will be the precision used to store the data in memory.

Usage precision is defined in the shader as highp (at least 32-bit), mediump (at least 16-bit), and lowp (at least 9-bit). These are minimum precisions; it is perfectly legal for a shader to specify a variable as mediump and for the shader compiler to generate fp32 data types. Desktop GPUs tend to only support fp32 computation, so the use of highp, mediump and lowp all map to fp32 data types (the precision qualifiers are only included to keep compatibility with OpenGL ES shaders, and can legally be ignored by the compiler). Mobile GPUs implementing OpenGL ES tend to map highp to fp32, and mediump and lowp to fp16. Detailed information can be found in the GLSL ES 3.0 Specification, Section 4.5.1

When binding a vertex attribute in memory to an input shader variable the storage and usage precisions are not required to match; the API will include transparent attribute precision conversion. It is perfectly legal for a user to upload e.g. a GL_FLOAT and then use it in a shader as a mediump fp16 variable, although it would be a waste of memory bandwidth to do so.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!