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

时间:2019-06-10 06:44:35

标签: opengl opengl-4 shader-storage-buffer

我有如下代码:

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之前通过,从而崩溃了吗?

我的应用程序正在运行,但是我不确定它是否仅在fluke上运行。

1 个答案:

答案 0 :(得分:3)

都不需要同时调用glBufferData。几乎所有的OpenGL函数(包括glBufferSubData//Init SSBO glBufferData(...); glBufferSubData(...); //No sync needed here glDispatchCompute(...); //Shader which writes to the SSBO //Sync here to make writes visible glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); //Read back SSBO memory glMapBuffer(...); ... )都遵循传统的OpenGL内存模型,该模型可以自动保证同步。

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

let name = "John"
  "My name is \(name)"