繁体   English   中英

如何通过缓冲区纹理将16位整数正确发送到GLSL着色器

[英]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;
}

欢迎任何建议。

GL_R16

那是一个无符号的标准化整数格式。 您的缓冲区采样器需要带符号的非标准化整数。

正确的图像格式GL_R16I

当它为int时,frag shader崩溃,为什么

可能是因为您没有使用flat插值 插值不适用于非浮点值。

而且它不会崩溃; 它只会编译失败。 您应该始终检查着色器中的编译错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM