glTexImage2D Segfault related to width/height

2019-02-14 06:33发布

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)

2条回答
爷的心禁止访问
2楼-- · 2019-02-14 06:57

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);
查看更多
老娘就宠你
3楼-- · 2019-02-14 07:12

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

查看更多
登录 后发表回答