【发布时间】:2018-06-18 14:02:06
【问题描述】:
所以我有一个计算着色器,它应该采用纹理并将其复制到另一个纹理并稍作修改。我已经确认纹理是绑定的,并且可以使用 RenderDoc 写入数据,这是一个图形调试工具。我遇到的问题是,在着色器中,由 OpenGL 创建的变量 gl_GlobalInvocationID 似乎无法正常工作。
这是我对计算着色器的调用:(纹理高度为 480)
glDispatchCompute(1, this->m_texture_height, 1); //Call upon shader
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
然后我们在这里有我的计算着色器:
#version 440
#extension GL_ARB_compute_shader : enable
#extension GL_ARB_shader_image_load_store : enable
layout (rgba8, binding=0) uniform image2D texture_source0;
layout (rgba8, binding=1) uniform image2D texture_target0;
layout (local_size_x=640 , local_size_y=1 , local_size_z=1) in; //Local work-group size
void main() {
ivec2 txlPos; //A variable keeping track of where on the texture current texel is from
vec4 result; //A variable to store color
txlPos = ivec2(gl_GlobalInvocationID.xy);
//txlPos = ivec2( (gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID).xy );
result = imageLoad(texture_source0, txlPos); //Get color value
barrier();
result = vec4(txlPos, 0.0, 1.0);
imageStore(texture_target0, txlPos, result); //Save color in target texture
}
当我运行这个时,目标纹理变成完全黄色,除了沿着左边框的 1pxl 粗的绿线和沿着底部边框的 1pxl 粗的红线。我的期望是看到某种渐变,因为将 txlPos 保存为颜色值。
我是否以某种方式错误地定义了我的工作组?我尝试将 gl_GlobalInvokationID 拆分为其组件,但没有设法更明智地摆弄它们。
【问题讨论】:
-
8位浮点纹理通常只能存储0到1之间的值。txlPos通常大于1。如果改为输出
float(txlPos) / vec2(640,480)会怎样? -
我得到了一个渐变,从左到右从黑到黄。
-
这几乎就是你想要的。我对 float(..) 的事情犯了一个错误,但我会写一个答案并纠正这个问题。
标签: c++ opengl compute-shader