在初始化 SSBO 存储和填充它之间是否需要内存屏障?

Do I need a memory barrier between initializing an SSBO storage and filling it?

我的代码看起来像这样:

uint ssboId;
glGenBuffers(1, &ssboId);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssboId);

//initialize
glBufferData(GL_SHADER_STORAGE_BUFFER, size, 0, GL_STATIC_DRAW);


// memory barrier here?
// glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);


//start writes
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset1, sourceSize1, data1);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset2, sourceSize2, data2);
...

// Ensure changes are applied before shader grabs it.
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);


我是否需要取消对 glBufferData 和 glBufferSubData 之间的 glMemoryBarrier 调用的注释?如果 SSBO 的东西不连贯,glBufferSubData 会不会在 glBufferData 之前通过,从而崩溃?

我的应用程序正在运行,但我不确定它是否只是偶然运行。

两次调用 glMemoryBarrier 都没有必要。几乎所有 OpenGL 函数(包括 glBufferDataglBufferSubData)都遵循自动保证同步的传统 OpenGL 内存模型。

只有在少数情况下才需要手动同步。例如,当着色器写入 SSBO 以保证所有进一步的操作都将看到此写入时:

//Init SSBO
glBufferData(...);
glBufferSubData(...);

//No sync needed here

glDispatchCompute(...); //Shader which writes to the SSBO

//Sync here to make writes visible
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);

//Read back SSBO memory
glMapBuffer(...);
...