[英]How to properly send 16 bit integers to GLSL Shaders via Buffer Texture
我有3072个类型为std::int16_t
值(16位整数),它对应于GLshort
。 我想将它们发送给我的GLSL着色器。 我阅读了“ 缓冲区纹理”并尝试实现该方法。 但是,一旦到达Shader,数据似乎不再完整。 我不确定,但看起来值全为0
或已用完。 我究竟做错了什么?
我的初始化代码如下所示(排除了一些不相关的内容):
// 1: Get the data - array of GLshort:
GLshort tboData[3072];
for (size_t i = 0; i < 3072; ++i)
{
// cdb.getSprite() returns std::int16_t
tboData[i] = (GLshort) cbd.getSprite(i);
}
// 2: Make sure the Shader Program is being used:
sp->use(); // sp is a wrapper class for GL Shader Programs
// 3: Generate the GL_TEXTURE_BUFFER, bind it and send the data:
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBufferData(GL_TEXTURE_BUFFER, sizeof(tboData), tboData, GL_STATIC_DRAW);
// 4: Generate the Buffer Texture, activate and bind it:
GLuint tboTex;
glGenTextures(1, &tboTex);
glActiveTexture(GL_TEXTURE1); // GL_TEXTURE0 is a spritesheet
glBindTexture(GL_TEXTURE_BUFFER, tboTex);
// 5: Associate them using `GL_R16` to match the 16 bit integer array:
glTexBuffer(GL_TEXTURE_BUFFER, GL_R16, tbo);
// 6: Make the connection within the Shader:
glUniform1i(sp->getUniformLocation("tbo"), 1);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
在渲染循环的开始,我确保所有内容都已绑定:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, spriteSheet);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_BUFFER, tboTex);
在渲染循环中,我设置了一个将用于索引TBO的制服:
glUniform1i(sp->getUniformLocation("tboIndex"), tboIndex);
现在, 顶点着色器的重要部分:
#version 140
// other ins, outs, uniforms ...
uniform int tboIndex;
uniform isamplerBuffer tbo;
out float pass_Sprite; // Frag shader crashes when this is int, why?
void main()
{
// gl_Position, matrices, etc ...
pass_Sprite = texelFetch(tbo, tboIndex).r;
}
欢迎任何建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.