c++ - 与计算着色器和 imageStore 的内存一致性

标签 c++ opengl memory shader compute-shader

我想在多个计算着色器 中使用imageStoreimageLoad

这与屏幕或帧缓冲区的“正常”渲染命令 (glDraw) 混合,但这些不使用 imageStoreimageLoad(仅 texturetexelFetch)。

我只有一个 OpenGL 上下文和线程。

我的假设如下:

  • 在执行 imageStore 时,我需要在稍后的计算着色器或 texture 中执行 imageLoad 之前执行 glMemoryBarrier code> 或 texelFetch 在后面的片段着色器中。
  • 我根本不需要使用coherent
  • 我根本不需要使用 glSync
  • 在使用帧缓冲区写入纹理然后使用 imageLoad 读取它之后,我不需要使用 glMemoryBarrier
  • OpenGL 将按照请求的顺序运行计算着色器和“正常”绘制操作。假设我在调用之间使用 glMemoryBarrier,则没有“线程问题”。

它们正确吗?

最佳答案

除了一个“可能”的异常(exception):

I don't need to use coherent at all.

您可能需要 coherent,这取决于您的计算着色器正在做什么。如果您的计算着色器写入图像,然后从另一个写入的数据中读取 invocation within the same dispatch ,那么你需要 coherent。因此,如果您执行 imageStore,发出计算着色器 barrier() 调用,然后执行 imageLoad 以读取其他 invocation的值,那么您需要 coherent 限定词。

coherent 是为了确保 visibility within a rendering command (CS 分派(dispatch)在 OpenGL 中被视为“渲染命令”)。如果您不需要内部可见性,那么您就不需要 coherent


我想详细说明一下,因为这是一个常见的混淆来源:

I don't need to use glMemoryBarrier after writing to a texture using a framebuffer and then reading it with imageLoad

这是绝对正确的。内存屏障是关于确保不连贯写入内存的可见性(和同步)。渲染到帧缓冲区不是不连贯的写入。因此,您可以对此类数据使用 imageLoad,而无需显式同步。

当然假设你不是rendering to that framebuffer in the same operation you're imageLoading from it .该规则仍然适用。

关于c++ - 与计算着色器和 imageStore 的内存一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34069961/

相关文章:

c++ - "xvalue has identity",为什么我不能计算它的地址

c++ - Bazel,带有 mingw64 编译器的 Windows 10 工具链配置

c++ - 输出换行符的最有效方法

python - OpenGL:相机 theta 和缩放

r - 为什么 as.integer 在 R 中返回 NA?

c++ - 在线程中等待时我该怎么办

opengl - 3D游戏特效,火、闪电、水、冰

c++ - OpenGL FBO 不渲染到纹理 (C++)

c++ - 使用常量是否节省内存

haskell - 了解此 Haskell 程序的内存使用情况