glTexImage2D 与宽度/高度相关的段错误

发布于 2024-12-03 22:16:07 字数 538 浏览 2 评论 0原文

当我尝试加载 771x768 图像时出现段错误。

尝试使用 24x24 和 768x768 图像,它们工作正常,没有问题。

这是预期的吗?为什么它不会因 GL 错误而优雅地失败呢?

分段错误发生在 glTexImage2D 调用中。我正在加载 PPM 二进制文件,因此它每像素打包 24 位。这个奇数与奇数维度相结合可能会产生一个非 4 字节(甚至 2 字节)对齐的结构(并且引用我完全分配的缓冲区之外可能是错误的原因,但 gdb 没有向我显示内存地址(我可以用它来查明这是否是导致它的原因))。

glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, dataptr);
// in this specific case of failure, width = 771, height = 768,
// dataptr contains 1776384 bytes of binary RGB image data (771*768*3 = 1776384)

I got a segfault when I tried to load a 771x768 image.

Tried with a 24x24 and 768x768 image and they worked, no problem.

Is this expected? Why wouldn't it just fail gracefully with a GL Error?

The segmentation fault occurs in the glTexImage2D call. I am loading a PPM binary file so it is packed 24 bits per pixel. This odd number combined with an odd dimension probably produces a not-4-byte (or even 2-byte) aligned structure (and referencing outside of my exactly enough allocated buffer may be the cause of the error but gdb does not show me a memory address (which I could use to find out if this is what causes it)).

glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, dataptr);
// in this specific case of failure, width = 771, height = 768,
// dataptr contains 1776384 bytes of binary RGB image data (771*768*3 = 1776384)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-12-10 22:16:07

这个奇数与奇数维度相结合可能会产生一个非 4 字节(甚至 2 字节)对齐的结构(并且引用我完全分配的缓冲区之外可能是错误的原因

这可能是原因。幸运的是,你可以在调用 glTexImage…(…) 之前设置 OpenGL 使用的对齐方式。

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);

This odd number combined with an odd dimension probably produces a not-4-byte (or even 2-byte) aligned structure (and referencing outside of my exactly enough allocated buffer may be the cause of the error

This is likely the cause. Luckily you can set the alignment OpenGL uses reading pixel data. Right before calling glTexImage…(…) do

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
执手闯天涯 2024-12-10 22:16:07

我在 opengl 论坛中读过此内容:

width must be 2^m + 2(border) for some integer m.
height must be 2^n + 2(border) for some integer n. 

(来源)

我发现了这个,我相信它澄清了正在发生的事情:

1. What should this extension be called?

  STATUS: RESOLVED

  RESOLUTION:  ARB_texture_non_power_of_two.  Conventional OpenGL
  textures are restricted to size dimensions that are powers of two.

来自 GL_ARB_texture_non_power_of_two

I've read this in the opengl forums:

width must be 2^m + 2(border) for some integer m.
height must be 2^n + 2(border) for some integer n. 

(source)

I found this which I believe it clarifies what's happening:

1. What should this extension be called?

  STATUS: RESOLVED

  RESOLUTION:  ARB_texture_non_power_of_two.  Conventional OpenGL
  textures are restricted to size dimensions that are powers of two.

from GL_ARB_texture_non_power_of_two

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文