问题描述
我正在尝试制作一个多级哈希表,以对内存更友好的方式表示一些体积数据。为此,我做了这个:
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 (将#修改为@)