OpenGL 能否将整数像素数据转换为浮点数或 UNORM?

Can OpenGL convert integer pixel data to floating point or UNORM?

glTexImage2D 采用 internalFormat(指定位数和数据 type/encoding)、format(没有位数和编码)和 type .

例如,是否可以让 OpenGL 将传递的包含 32 位整数的像素数据从格式 GL_RGB_INTEGER 和类型 GL_INT 转换为内部格式 GL_RGB32F

维基文章 https://www.khronos.org/opengl/wiki/Pixel_Transfer#Format_conversion 向我建议可以通过以下说明:

Pixels specified by the user must be converted between the user-specified format (with format​ and type​) and the internal representation controlled by the image format of the image.

但我无法从着色器中的浮点采样器读取数据。

_INTEGER 像素传输格式 用于将数据传输为整数图像格式。您正在填充浮点纹理,因此不符合条件。您应该得到一个 OpenGL 错误。

确实,您链接到的那篇文章说明了这一点:

Also, if "_INTEGER" is specified but the image format is not integral, then the transfer fails.

GL_RGBA32F 不是完整的图像格式。

如果删除 _INTEGER 部分,则像素传输将 "work"。 OpenGL 将假定整数数据是 标准化的 值,因此您将获得 [-1, 1] 范围内的浮点值。也就是说,如果您传递一个 32 位整数值 1,则相应的浮点值将为 1/(2^31-1),这是一个非常小的数字(因此,几乎可以肯定只是 0.0)。

如果您希望 OpenGL 强制转换 整数,就像通过 C 强制转换 (float)1 一样...好吧,实际上没有办法做到这一点。您只需要自己转换数据即可。