当调用 gl.texImage2D 时,内存分配会发生什么?

What happens in terms of memory allocation when gl.texImage2D is called?

我特别想知道如果我使用 gl.R32Fgl.RGB32F 实际分配了多少纹理内存? GPU 会继续为每个像素分配 4 个通道吗?特别是 gl_FragColor 总是 vec4

纹理占用多少内存未定义。这取决于 driver。 driver 可能将 R32F 存储为 RGBA32F,它可能将 RGB8 存储为 RGBA8。它可能有各种限制要求它填充行。例如,假设 16 字节或 128 字节对齐要求,以便 1x10 像素 R8 扩展到 128x10 字节。其他 drivers 可能需要将图像分成正方形,每个正方形具有最小或固定大小,例如每个正方形需要 16x16 像素,因此 12x10 纹理实际上需要 16x16 space 而 25x10 纹理需要32x16 space.

确实没有办法确切地知道 space 纹理将使用多少。我知道 driver 可以做所有这些事情。

来自the spec

internalformat is a symbolic constant indicating with what format and minimum precision the values should be stored by the GL

注意短语 "minimum precision"。

在规范的其他地方

the memory allocation per texture component is assigned by the GL to match or exceed the allocations listed in tables 3.13 - 3.14.

表 3.13 和 3.14 列出了每个通道的最小大小(以位为单位)。

由 driver 选择存储内容的实际方式。

举个例子,我希望大多数桌面 GPU 将 RGB5_A1 存储为 RGBA8,而移动 GPU 可能实际上在硬件本身中支持 RGB5_A1,因此会保留它作为 RGB5_A1。我怀疑对于每通道少于 8 位的所有格式也是如此。