2
votes

When trying to render a 2D label using a bitmap texture, my texture sampling returns black. I tried debugging using RenderDoc but I can't find the problem. It seems the texture loads fine and is stored in the correct register, but it still renders black.

I even tried using a full red texture to check the texture coordinates, but the texture still showed up as black.

Pipeline texture Texture output output Here is the code I use for loading/rendering the texture. It tried to render a label. (genMipMaps is false).

void Texture::CreateGLTextureWithData(GLubyte* data, bool genMipMaps) {
    if (bitmap)
        glDeleteTextures(1, &bitmap);

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

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    if (genMipMaps)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    else
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

    if (genMipMaps)
        glGenerateMipmap(GL_TEXTURE_2D);
}

Custom sampler:

glGenSamplers(1, &linearSampler);
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(linearSampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glSamplerParameteri(linearSampler, GL_TEXTURE_COMPARE_FUNC, GL_NEVER);

glSamplerParameterf(linearSampler, GL_TEXTURE_MIN_LOD, 0);
glSamplerParameterf(linearSampler, GL_TEXTURE_MAX_LOD, GL_TEXTURE_MAX_LOD);
glBindSampler(0, linearSampler);

Rendering:

glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST); 
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture->getBitmap());
// Set index and vertex buffers
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);


if (glIsBuffer(vertBuffer) && vertBuffer != lastVertBuffer)
    glBindBuffer(GL_ARRAY_BUFFER, vertBuffer);

if (glIsBuffer(indexBuffer) && indexBuffer != lastIndexBuffer)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

shader->updateLayout();

// Draw
const void* firstIndex = reinterpret_cast<const void*>(0);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, firstIndex);

glBindVertexArray(0);

Shaders:

VS:

#version 430 core

// Vertex atributes
in vec3 a_position;
in vec2 a_texture;
in vec4 a_color;


// Constant buffers
layout(std140) uniform VertexBlock
{
    mat4 u_wvp;
    mat4 u_world;
};

// Vertex shader outputs
out vec2 v_texture;
out vec4 v_color;

void main()
{
    v_texture = a_texture;
    v_color = a_color;
    gl_Position = u_wvp * vec4(a_position, 1.0);
}

FS: (I tried setting the color to red without using the texture and it renders in red correctly.)

#version 430 core

in vec4 v_color;
in vec2 v_texture;

layout(binding = 0) uniform sampler2D u_texture;

out vec4 fragColor;

void main()
{
    fragColor = v_color * texture(u_texture, v_texture);
}
1

1 Answers

4
votes

Found out what the problem was. I was not generating mipmaps for the 2D textures but the sampler was still using them.

Texture loading:

...
if (genMipMaps)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
else
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

if (genMipMaps)
    glGenerateMipmap(GL_TEXTURE_2D);
...

Sampler:

...
glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
...

What I did to fix it is just always generate mipmaps for the textures