OpenGL:计算着色器 - gl_GlobalInvocationID 提供静态输出

OpenGL: Compute Shader - gl_GlobalInvocationID giving static output

所以我有一个计算着色器,它应该获取一个纹理并将其复制到另一个纹理并稍作修改。我已经确认纹理已绑定,并且可以使用图形调试工具 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 之间的值。由于 gl_GlobalInvocationID 在大多数情况下都大于 1,因此它被限制为最大值 1,这使纹理变黄。

如果要在两个方向上创建渐变,则必须确保存储的值从 0 开始到 1 结束。一种可能是除以最大值:

result = vec4(vec2(gl_GlobalInvocationID.xy) / vec2(640, 480), 0, 1);