Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glTexImage2d and Null data

This is a weird question but I was wondering why glTexImage2d cares about the pixel data type and format of Null data. The signature for glTexImage2d is...

void glTexImage2D( GLenum target, GLint level, GLint internalFormat, 
           GLsizei width, GLsizei height, GLint border, GLenum format, 
           GLenum type, const GLvoid * data);

The internalFormat acts to tell the graphics driver what you want to store the data as on the gpu and the format and type tell the graphics driver what to expect from GLvoid * data. So, if I don't pass any data, by passing null for instance, why does the graphics driver care what the format and type is? So this is a weird question because sometimes it doesn't. The times when it does, and I haven't checked every single iteration but, is specifically when making a depth texture, or something I've come across recently is the Integer types, like GL_RED_INTEGER, GL_RG_INTEGER, etc. and/or their corresponding internalFormats like GL_R8I, GL_RGBA32UI. Whereas, any of the "simple" types don't have to correspond in anyway to the internalFormat, like GL_RGBA8, GL_RGBA32F, etc. For some reason, the former particular data types and formats have to be exact even if you're not passing any data. Why is that?

like image 725
Ryan Bartley Avatar asked Nov 20 '13 20:11

Ryan Bartley


People also ask

What is glTexImage2D?

The glTexImage2D function specifies a two-dimensional texture image. Texturing maps a portion of a specified texture image onto each graphical primitive for which texturing is enabled. Two-dimensional texturing is enabled and disabled using glEnable and glDisable with argument GL_TEXTURE_2D.

What is Gl_depth_component?

GL_DEPTH_COMPONENT is used for PixelFormat, and GL_DEPTH_COMPONENT16 is used for Framebuffer/Renderbuffer Object.


1 Answers

There are two formats in a call to glTexImage2D (...), the internal format and the format used for pixel transfer. The internal format is necessary to allocate storage for the texture, the pixel transfer format is used by GL to interpret the (optional) array of pixels you pass.

When you pass NULL for the pixel data, no pixel transfer occurs so you would think that format and type would be unnecessary. Technically, they are unnecessary in this case, but GL still validates the pair of format and type. You can use any valid pair of values you want there, since no pixel transfer operation will actually occur. All you are doing is allocating texture storage.

By the way, glTexStorage2D (...) is the modern way of allocating texture storage without pixel transfer. You can create immutable textures this way, where you cannot accidentally re-define a texture LOD by an incorrect glTexImage2D (...) call. After allocating texture storage with glTexStorage2D (...) you can feed it data by calling glTexSubImage2D (...).

like image 101
Andon M. Coleman Avatar answered Sep 18 '22 13:09

Andon M. Coleman