inImage
复制到outImage
。它是从更复杂的后处理通道派生而来的。在
main()
的前几行中,单个线程将64个像素的数据加载到共享数组中。然后,在同步之后,每个64个线程中的一个线程向输出图像写入一个像素。根据我的同步方式,我会得到不同的结果。我最初认为
memoryBarrierShared()
会是正确的调用,但它产生了以下结果:
![Unsynchronized result](https://istack.dev59.com/7bu0y.webp)
memoryBarrier()
相同。如果我使用
barrier()
,我会得到以下(期望的)结果:
![enter image description here](https://istack.dev59.com/rqybJ.webp)
这里发生了什么?我误解了
memoryBarrierShared()
的目的吗?为什么barrier()
有效?#version 430
#define SIZE 64
layout (local_size_x = SIZE, local_size_y = 1, local_size_z = 1) in;
layout(rgba32f) uniform readonly image2D inImage;
uniform writeonly image2D outImage;
shared vec4 shared_data[SIZE];
void main() {
ivec2 base = ivec2(gl_WorkGroupID.xy * gl_WorkGroupSize.xy);
ivec2 my_index = base + ivec2(gl_LocalInvocationID.x,0);
if (gl_LocalInvocationID.x == 0) {
for (int i = 0; i < SIZE; i++) {
shared_data[i] = imageLoad(inImage, base + ivec2(i,0));
}
}
// with no synchronization: stripes
// memoryBarrier(); // stripes
// memoryBarrierShared(); // stripes
// barrier(); // works
imageStore(outImage, my_index, shared_data[gl_LocalInvocationID.x]);
}
glMemoryBarrier
(不确定)。 - Christian RauglMemoryBarrier
仍然是设备-设备同步,而不是主机-设备同步。 - Christian Rau