1
$\begingroup$

I want to send a series of integer to the geometry shader, while the data will be modified every frame.

But when I try to retrive the sampleBuffer in geometry shader, it seems to keep output zero.

Am I using Texture Buffer wrong, or the TBO itself just can't be used as GL_DYNAMIC_DRAW?

Init Texture Buffer

void InitialTBO() {
    int index_tbo_reserved_size = 5000;
    glGenBuffers(1, &index_tbo);
    glBindBuffer(GL_TEXTURE_BUFFER, index_tbo);
    glBufferData(GL_TEXTURE_BUFFER, index_tbo_reserved_size * sizeof(GLint), NULL, GL_DYNAMIC_DRAW);
    glGenTextures(1, &index_tbo_tex);
}

Render

void RenderChunkDataCone(Primitive* primitive) {
    Program* shaders = primitive->shaders;

    shaders->use();

    glActiveTexture(GL_TEXTURE0);   
    glBindBuffer(GL_TEXTURE_BUFFER, this->index_tbo);
    glBufferSubData(GL_TEXTURE_BUFFER, 0, sizeof(GLint)*index_tbo_data.size(), index_tbo_data.data());
    glBindTexture(GL_TEXTURE_BUFFER, this->index_tbo_tex);
    glTexBuffer(GL_TEXTURE_BUFFER, GL_R32I, this->index_tbo);
    shaders->setUniform("index_tex", 0);

    glBindVertexArray(primitive->vao);
    glDrawElements(primitive->drawType, primitive->drawCount * primitive->drawCount_Inner, GL_UNSIGNED_INT, 0);

    glBindVertexArray(0);
    shaders->stopUsing();
}

Geometry Shader

#version 460 core
uniform samplerBuffer index_tex;
vec3 render_primitive(vec3 position){
    int status = int(texelFetch(index_tex, 0).r);
    if(status==0)
        return vec3(0,10,10);
    else if(status==1)
        return vec3(10,0,0);
    else if(status==2)
        return vec3(0,0,10);
}

void main() {
    vec4 position =gl_in[0].gl_Position;
    vec3 returnedValue = render_primitive(position.xyz);
}

I've confirmed the index_tbo[0] is 1, But the returnedValue outputed by shader will always be (0,10,10) as the value index_tex[0] received seems to be 0.

$\endgroup$
1
$\begingroup$

These:

glTexBuffer(GL_TEXTURE_BUFFER, GL_R32I, this->index_tbo);
uniform samplerBuffer index_tex;

Do not match. If the texture is a signed integer texture, then the sampler type needs to match. It should be isamplerBuffer, and you won't need to do the cast.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.