无法在OpenGL碎片着色器中正确检索16位整数
Cannot correctly retrieve 16-bit integers inside openGL fragment shader
我想做的:在片段着色器中使用符号的16位整数进行GPGPU计算,使用iOS上的OpenGL ES 2.0。
我已经验证了我已经正确设置了我的(输入)纹理缓冲区和(输出)帧缓冲区,并且可以将16位整数传递(通过使用未签名的8位指针将其放在缓冲区上),渲染单个字节到框架缓冲区,并在CPU侧重建字节并重建正确的16位值。
如果我致电GlclearColor,我可以传递
之类的值glClearColor(0.5, 0.5, 0.5, 0.5); // endian-agnostic way to initialize buffer with "no data" values (-32640)
glClear(GL_COLOR_BUFFER_BIT);
我使用的是(0.5、0.5、0.5、0.5)的测试值。这些应该等于传递-32640(签名)或32896(未签名)。
i可以正确检索片段着色器内部的值(作为无符号的等效物)
#extension GL_EXT_shader_framebuffer_fetch : require
varying highp vec2 vTexCoord;
uniform sampler2D myTexture;
void main()
{
lowp vec4 myIntArray = texture2D( myTexture, vTexCoord);
// Retrieve value from frame buffer
lowp vec4 lastFragColor = gl_LastFragData[0];
// Recover the value (stored in alpha (high byte) and red (low byte)
highp float signed_value = lastFragColor.a;
signed_value = signed_value*255.0*256.0 + lastFragColor.r*255.0;
// ... Do stuff with the recovered value.
// Here, I simply verify value is recovered correctly (as an unsigned int equivalent):
// This draws the pixel green, indicating success
if (signed_value >= 32896.0) {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
else {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
// But changing the threshold value very slightly (replacing the lines
// above with those below) draws the pixel red, correctly
// indicating the condition is not met, because the value is
// 32896
if (signed_value >= 32896.01) {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
else {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
}
但是,我想传递一系列不同的值,因此我不使用glclearcolor,而是设置了一个缓冲区,并将其传递给我签名的16位整数的数组(基本上是在我签名的INT16_T上,字节好像它们只是未签名字节。)
然后,我可以将它们渲染到框架缓冲区,并使用GlreadPixels并将正确的值归还正确的值,并将INT16_T指针与CPU上的该数组混为一谈。但是,使用与上述相同的逻辑,但访问我的纹理而不是框架缓冲区:highp float signed_value = myIntArray.a;
signed_value = value*255.0*256.0 + myIntArray.r*255.0;
使用我的纹理值,我只能在片段着色器内正确重建从0到32767的值,但不正确的值> 32767。未签名。有什么想法,为什么我可以从框架缓冲区重建大于32767的值,但不能从我的纹理缓冲区中重建值?
(编辑为添加:校正 - 看来我无法传递,渲染和检索 all 通过我的纹理进行...使用glclear()。我通过纹理呈现为黑色传递的负值并在CPU上重建为零。正确处理0和32767之间的值。或未签名的INT16值> 32767)并正确地重建片段着色器?)
结果,片段着色器代码很好;我看着错误的地方。对我来说,"客户"不为人知,将投入限制为非负。神秘的人解决了,尽管额头上有大耳光。
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 使用VerQueryValue检索应用程序的文件描述
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 在一定长度后从数组中打印时缺少整数
- 无法在OpenGL碎片着色器中正确检索16位整数
- 使用 sscanf 检索整数
- 从 C++ 中的字节数组中检索整数的最佳方法是什么