glPixelStorei(GL_UNPACK_ALIGNMENT, 1) 缺点?
总是使用 1 的 alginment 有什么缺点?
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)glPixelStorei(GL_PACK_ALIGNMENT,1)
它会影响现代 gpu 的性能吗?
解决方案数据怎么可能不是1字节对齐的?
这强烈表明对像素传输操作中的行对齐意味着什么缺乏了解.
您传递给 OpenGL 的图像数据应按行分组.每行包含 width
个像素,每个像素的大小由格式和类型参数定义.因此,具有 GL_UNSIGNED_BYTE
类型的 GL_RGB
格式将导致像素大小为 24 位.否则预计像素会被打包,因此这些像素中的一行 16 将占用 48 个字节.
每一行都应根据GL_PACK/UNPACK_ALIGNMENT
定义的特定值对齐.这意味着您添加到指针以到达下一行的值是:align(pixel_size * width, GL_*_ALIGNMENT)
.如果像素大小为3字节,宽度为2,对齐为1,则行字节大小为6.如果对齐为4,则行字节大小为8.
看到问题了吗?
图像数据可能来自某些图像加载器加载的某种图像文件格式,具有行对齐方式.有时这是 1 字节对齐的,有时它不是.DDS 图像具有指定为格式一部分的对齐方式.在许多情况下,图像具有 4 字节的行对齐方式;因此,小于 32 位的像素大小将在具有特定宽度的行的末尾填充.如果你给 OpenGL 的对齐方式不匹配,那么你会得到一个畸形的纹理.
您设置对齐方式以匹配图像格式的对齐方式.如果您知道或以其他方式可以确保您的行对齐始终为 1(除非您编写了自己的图像格式或 DDS 编写器,否则这不太可能),您需要将行对齐设置为您的图像格式所使用的.>
What are the disadvantages of always using alginment of 1?
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glPixelStorei(GL_PACK_ALIGNMENT, 1)
Will it impact performance on modern gpus?
解决方案How can data not be 1-byte aligned?
This strongly suggests a lack of understanding of what the row alignment in pixel transfer operations means.
Image data that you pass to OpenGL is expected to be grouped into rows. Each row contains width
number of pixels, with each pixel being the size as defined by the format and type parameters. So a format of GL_RGB
with a type of GL_UNSIGNED_BYTE
will result in a pixel that is 24-bits in size. Pixels are otherwise expected to be packed, so a row of 16 of these pixels will take up 48 bytes.
Each row is expected to be aligned on a specific value, as defined by the GL_PACK/UNPACK_ALIGNMENT
. This means that the value you add to the pointer to get to the next row is: align(pixel_size * width, GL_*_ALIGNMENT)
. If the pixel size is 3-bytes, the width is 2, and the alignment is 1, the row byte size is 6. If the alignment is 4, the row byte size is eight.
See the problem?
Image data, which may come from some image file format as loaded with some image loader, has a row alignment. Sometimes this is 1-byte aligned, and sometimes it isn't. DDS images have an alignment specified as part of the format. In many cases, images have 4-byte row alignments; pixel sizes less than 32-bits will therefore have padding at the end of rows with certain widths. If the alignment you give OpenGL doesn't match that, then you get a malformed texture.
You set the alignment to match the image format's alignment. If you know or otherwise can ensure that your row alignment is always 1 (and that's unlikely unless you've written your own image format or DDS writer), you need to set the row alignment to be exactly what your image format uses.
相关文章