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

GLSLvulkan 方言模运算符导致奇怪的行为

如何解决GLSLvulkan 方言模运算符导致奇怪的行为

我正在尝试制作一个多级哈希表,以对内存更友好的方式表示一些体积数据。为此,我做了这个:

struct Node
{
    ivec4 key;
    vec4 value;
};

layout(scalar,binding = 4) buffer hash_tree_ssbo
{
    int depth;
    int depth_size;
    int size;
    int pad2;
    Node hash_tree[];
};

int HashFunction(ivec3 coord,int m)
{
    const int p1 = 5003;
    const int p2 = 7129;
    const int p3 = 7877;

    return (coord.x * p3 + coord.y * p2 + coord.z * p1) % m;
}

vec4 ReadValue(ivec3 coord,int lod)
{
    const float pn = pow(0.5,lod);
    int offset = 0;

    const int local_size = int(size >> lod);
    int hash = HashFunction(coord,local_size);
    vec4 read_hash = hash_tree[hash + offset].value;

    if(hash_tree[hash + offset].key.xyz == coord)
        return hash_tree[hash + offset].value;

    return vec4(0);
}

void StoreValue(vec4 val,ivec3 coord,int lod)
{
    int offset = 0;

    const int local_size = int(size >> lod);
    int hash = HashFunction(coord,local_size);
    float read_hash = hash_tree[hash + offset].value.a;

    hash_tree[hash + offset].value = val;
    hash_tree[hash + offset].key = ivec4(coord,0);
}

注意变量 offset 如果它是 0,这个版本的代码工作正常(我可以光线跟踪体积结构并看到预期的图像)。相反,如果我编写不应更改逻辑的 int offset = 1;,则会出现黑屏。使用 renderdoc 分析着色器执行,当 offset=1 等所有值写入 ssbo 索引 0 时,所有哈希值似乎都变为 0。

但是,如果我随后将哈希函数更改为 return (coord.x * p3 + coord.y * p2 + coord.z * p1);,事情会再次起作用。

这不是 m 是 0 或 1,因为如果这是问题 A) offset=0 将不起作用,B) 所有值都应该导致存储桶 1 而不是存储桶 0。

我对这种行为感到非常困惑,为什么模运算符会如此糟糕地破坏程序逻辑?

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