"GL_HALF_FLOAT"; OpenGL渲染和GLSL [英] "GL_HALF_FLOAT" with OpenGL Rendering and GLSL
问题描述
我正在用C ++编写OpenGL渲染器.我希望它尽可能高效,并且每个顶点/法线/UV tex坐标/切线/等都占用尽可能少的内存.我正在使用索引,线带和风扇.我以为32位浮点数不是必需的,而16位浮点数应该没问题,至少对于其中一些像法线和UV而言.我似乎在任何地方都找不到任何示例.我可以找到"GL_HALF_FLOAT"的话题,但没有真实的例子.我在正确的轨道上吗?还是不值得一看?如果有人知道这样的示例,他们可以发送源代码链接吗?
I am programming an OpenGL renderer in C++. I want it to be as efficient as possible and each vertex/normal/UV tex coord/tangents/etc to take up as little memory as possible. I am using indexes, line strips, and fans. I was thinking that 32bit floating points are not necessary and 16 bit Floating points should be fine, at least for some of these like normals and UVs. I can't seem to find any examples of this anywhere. I can find talk of "GL_HALF_FLOAT", but no real examples. Am I on the right track? Or is this not worth looking in to? If anyone knows of an example of this could they send a link of source code?
推荐答案
在完整的OpenGL中(与OpenGL ES不同),着色器代码始终以32位浮点数运行.从OpenGL 3.0开始,支持将顶点数据指定为半浮点数.如果精度足以满足您的需求,则可以减少顶点数据的内存使用量,并减少顶点获取所需的带宽.
In full OpenGL (unlike in OpenGL ES), shader code always operates with 32-bit floats. Starting with OpenGL 3.0, specifying vertex data as half-floats is supported, though. If the precision is sufficient for your needs, this can reduce memory usage for your vertex data, and reduce the bandwidth needed for vertex fetching.
请记住,半浮点数的精度仅为大约3-4个十进制数字.因此准确度确实受到严重限制.
Keep in mind that the precision of a half-float is only about 3-4 decimal digits. So the accuracy really is seriously limited.
关于如何使用它们,这非常简单.如果您有一个指向半浮点型值的指针,则可以使用glBufferData()
或glBufferSubData()
将它们存储在VBO中,就像处理任何其他类型一样.然后glVertexAttribPointer()
调用将看起来像这样,以包含3个组件的属性为例:
As for how to use them, it's quite straightforward. If you have a pointer to half-float values, you store them in a VBO using glBufferData()
or glBufferSubData()
, just like you would for any other type. The glVertexAttribPointer()
call will then look like this, using an attribute with 3 components as an example:
glVertexAttribPointer(loc, 3, GL_HALF_FLOAT, GL_FALSE, 0);
数据本身的格式在 ARB_texture_float 扩展中定义.虽然未正式命名,但它看起来至少与 IEEE 754-2008 IEEE 754-2008 格式.我之前根据维基百科格式描述编写了转换代码,并且对于OpenGL的使用来说效果很好.
The format of the data itself is defined in the ARB_texture_float extension. While it's not officially named, it looks at least very similar to the IEEE 754-2008 format. I wrote conversion code based on that Wikipedia format description before, and it worked fine for OpenGL usage.
大多数语言没有用于半浮点数的内置类型.因此,您要么必须编写几行代码来完成从float到half-float的转换,要么使用别人编写的代码.
Most languages don't have built-in types for half-floats. So you either will have to write a few lines of code to do the conversion from float to half-float, or use code that somebody else wrote.
以下有关半浮点转换的资源来自快速搜索.我对其中任何一个都没有亲身经历,因此您应该进行自己的搜索以找到最适合您的需求:
The following resources about half-float conversion are from a quick search. I have no personal experience with any of them, and you should do your own search to find the one most suitable for your needs:
- 有趣的英特尔文章,解释了可能的性能优势: http://half.sourceforge.net/
- gcc文档说它支持ARM目标的半浮点类型(
__fp16
).
- Interesting article from Intel, explaining possible performance benefits: https://software.intel.com/en-us/articles/performance-benefits-of-half-precision-floats. This also mentions that Intel processors have instructions for the conversion (e.g. there's a
_mm256_cvtps_ph
intrinsic to convert from float to half-float). - Open source library for half-float operations and conversions: http://half.sourceforge.net/.
- gcc documentation saying that it supports a half-float type (
__fp16
) for ARM targets.
这篇关于"GL_HALF_FLOAT"; OpenGL渲染和GLSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!