glTexImage2D null data pointer and memory initialization

Let's assume that 2D texture is defined using glTexImage2D with the null data pointer. Does passing the null data pointer zeroes memory or I may get some garbage data when a fragment shader samples that texture ?

vec3 color = texture(texture0, texCoord).xyz;

Solution 1:

The documentation clearly states:

data may be a null pointer. In this case, texture memory is allocated to accommodate a texture of width width and height height. ... The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive.

So yes -- you will get garbage. Keep in mind that zeroes are also valid garbage, so don't use that as a proof that it "works".

The easiest way to clear a texture to all-zeros is with glClearTexImage:

glClearTexImage(texture, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

For this function data == 0 actually means "all zeroes".