0
votes

I want to pass a big array of unsigned short tuples (rect geometries) to my Fragment Shader and be able to sample them as-is using integer texture coordinates, to do this I'm trying with a 1D texture as follows, but get just blank (0) values.

Texture creation and initialization:

GLushort data[128][2];
// omitted array initialization

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_1D, tex);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage1D(
    GL_TEXTURE_1D,
    0,
    GL_RG16UI,
    128,
    0,
    GL_RG_INTEGER,
    GL_UNSIGNED_SHORT,
    data
);

Texture passing to shader:

int tex_unit = 0;
glActiveTexture(GL_TEXTURE0 + tex_unit);
glBindTexture(GL_TEXTURE_1D, tex);
glUniform1iv(loc, 1, &tex_unit);

Debug fragment shader:

#version 330 core
out vec4 out_color;

uniform usampler1D tex;

void main()
{
    uvec4 rect = texelFetch(tex, 70, 0);
    uint w = rect.r;
    uint h = rect.g;
    out_color = w > 0u ? vec4(1) : vec4(0);
}

Things that work for sure: non-zero data in data array, texture image unit setup and sampler uniform initialization.

OpenGL 4.1, OSX 10.11.6 "El Capitan"

1
As an arguable "proof" that the texture is created and sampled, the debug shader renders white colors if the texture image data is populated like this, with everything else untouched: glTexImage1D(GL_TEXTURE_1D, 0, GL_RG, 128, 0, GL_RG, GL_UNSIGNED_SHORT, data); (although this makes little sense to me, because if I understood the documentation well, the internalFormat must match the sampler type, and in this case, the first is float and second is unsigned int)Ivan Nikolaev

1 Answers

1
votes

After some googling and by trial and error, I discovered this post, which states that with integer texture formats (for example, GL_RG16UI internal format) GL_LINEAR filtering cannot be specified and only GL_NEAREST applies, thus, everything worked out with the following texture creation code:

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_1D, tex);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage1D(
    GL_TEXTURE_1D,
    0,
    GL_RG16UI,
    128,
    0,
    GL_RG_INTEGER,
    GL_UNSIGNED_BYTE,
    data
);