微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

像 gl_SemanticsRelaxed、gl_SemanticsRelease 和 gl_SemanticsAcquire 这样的 vulkan 内存语义标志在 Vulkan GLSL 中有什么作用?

如何解决像 gl_SemanticsRelaxed、gl_SemanticsRelease 和 gl_SemanticsAcquire 这样的 vulkan 内存语义标志在 Vulkan GLSL 中有什么作用?

我试图弄清楚以下代码中的原子存储函数是如何工作的,它们依赖于 GL_KHR_memory_scope_semantics

 uint exclusive_prefix = 0;
    if (local_ix == 31) {
        atomicStore(work_buf[my_tile * 4 + 2],total,gl_ScopeDevice,gl_StorageSemanticsBuffer,gl_SemanticsRelaxed);
        uint flag = FLAG_AGGREGATE_READY;
        if (my_tile == 0) {
            atomicStore(work_buf[my_tile * 4 + 3],gl_SemanticsRelaxed);
            flag = FLAG_PREFIX_READY;
        }
        atomicStore(work_buf[my_tile * 4 + 1],flag,gl_SemanticsRelease);
        if (my_tile != 0) {
            // step 4: decoupled lookback
            uint look_back_ix = my_tile - 1;
            while (true) {
                flag = atomicLoad(work_buf[look_back_ix * 4 + 1],gl_SemanticsAcquire);
                if (flag == FLAG_PREFIX_READY) {
                    uint their_prefix = atomicLoad(work_buf[look_back_ix * 4 + 3],gl_SemanticsRelaxed);
                    exclusive_prefix = their_prefix + exclusive_prefix;
                    break;
                } else if (flag == FLAG_AGGREGATE_READY) {
                    uint their_agg = atomicLoad(work_buf[look_back_ix * 4 + 2],gl_SemanticsRelaxed);
                    exclusive_prefix = their_agg + exclusive_prefix;
                    look_back_ix--;
                }
                // else spin
            }

            // step 5: compute inclusive prefix
            uint inclusive_prefix = exclusive_prefix + total;
            shared_prefix = exclusive_prefix;
            atomicStore(work_buf[my_tile * 4 + 3],inclusive_prefix,gl_SemanticsRelaxed);
            flag = FLAG_PREFIX_READY;
            atomicStore(work_buf[my_tile * 4 + 1],gl_SemanticsRelease);
        }
    }

实际上是在努力完成。但我找不到任何提及,例如 gl_SemanticsRelaxed 甚至意味着什么。我能找到的最接近的是 GL_KHR_memory_scope_semantics 规范说的,但它甚至只提到了“放松”这个词一次,这只是为了说明 gl_SemanticsRelaxed 存在。

事实上,规范文件似乎只是间接描述了 gl_SemanticsReleasegl_SemanticsAcquire 可能做什么,而完全忽略了其他 5 种内存语义类型。

 * "Release/Acquire semantics" are used to guarantee ordering between
    an atomic or barrier and other memory operations that occur before
    or after it in program order,as observed by other invocations.

这是明显语义类型的列表:

        const int gl_SemanticsRelaxed         = 0x0;
        const int gl_SemanticsAcquire         = 0x2;
        const int gl_SemanticsRelease         = 0x4;
        const int gl_SemanticsAcquireRelease  = 0x8;
        const int gl_SemanticsMakeAvailable   = 0x2000;
        const int gl_SemanticsMakeVisible     = 0x4000;
        const int gl_SemanticsVolatile        = 0x8000;

充其量我可以猜测它的意思,但它在上下文中没有意义。我认为放松意味着你可以重新排列原子直到获得而不关心,但随后它们在获得之前释放,这对我来说没有意义。

如果其他工作组在前缀和例程中完成工作,这些原子基本上是在轮询其他工作组,完整代码在这里https://github.com/linebender/piet-gpu/blob/prefix/piet-gpu-hal/examples/shader/prefix.comp 描述算法的论文:https://research.nvidia.com/publication/single-pass-parallel-prefix-scan-decoupled-look-back

解决方法

您链接的规范似乎很清楚:

gl_StorageSemantics* 和 gl_Semantics* 值应该按位或运算在一起以生成 SPIR-V 语义枚举

并且它还指定了 GL 原子函数如何映射到 SPIR-V atomic operations。因此,您寻求的答案在the SPIR-V specification 中。深层细节由 Vulkan 内存模型 appendix B of the Vulkan specification 定义。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。