0
votes

recently i found an Opengl example that applies a single texture without specifying texture image unit and without sending the corresponding unit integer uniform into the shader, is it possible to apply textures without using texture units?? or it simply uses a default value for both active texture unit and its shader sampler value.

my code block (texture related):

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

GLuint TextureID = 0;


SDL_Surface* Surface = IMG_Load("Abrams_Tank.jpg");

glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);

int Mode = GL_RGB;

if(Surface->format->BytesPerPixel == 4) {
    Mode = GL_RGBA;
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Surface->w, Surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, Surface->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);






GLuint vbo;
GLuint vbo_Text;
GLuint vao;



glGenBuffers(1, &vbo);
glGenBuffers(1, &vbo_Text);
glGenVertexArrays(1, &vao);






glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData) * 30,vertexData, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(sizeof(float) * 18));
glDrawArrays(GL_TRIANGLES, 0,6);


glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(0, vbo);

my fragment shader:

#version 410

in vec2 texCoordout;

uniform sampler2D mysampler;

out vec4 Fcolor;

void main()
{
Fcolor = texture (mysampler, texCoordout);
}
1
It is possible, but it requires an extension. The concept is known as bindless textures. Texture image units are an artificial construct in GL, the hardware has actually always worked by passing around references to texture memory itself and has no actual binding mechanism. TIUs are essentially just state containers; they make texture residency and validation simpler, but they are not necessary.Andon M. Coleman

1 Answers

2
votes

This will work, and is within specs:

  • The default texture unit is GL_TEXTURE0, so there is no need to ever call glActiveTexture() if only texture unit 0 is used.
  • Uniforms have a default value of 0, which is the correct value for a sampler that references texture unit 0.

I would still always set the uniform values just to make it explicit. If you don't set the uniform in this case, it is also much more likely that you will miss to set it if you ever use a texture unit other than 0.