且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

索引到第二个数组元素总是返回零

更新时间:2023-02-05 12:58:00

std140 布局中,所有数组都有一个元素到元素的数组步长,四舍五入到最接近的 sizeof(vec4),即16个字节.因此,除非 u32 实际上是一个16字节结构,否则 u32 selection [128]; 与您的定义不匹配.

In std140 layout, all arrays have an element-to-element array stride rounded up to the nearest sizeof(vec4), which is 16 bytes. So unless u32 is actually a 16-byte structure, u32 selection[128]; doesn't match with your definition.

您的GLSL需要做的是获取一个 uvec4 数组,其大小是其实际大小的4倍,并像这样对数组进行索引:

What your GLSL needs to do is to take a uvec4 array 4 times smaller than its actual size, and index the array like this:

const int num_selection_elements = 128;
const int num_selection_words = num_selection_elements / 4
layout(std140, align = 16, binding = 0) uniform UBO
{
    ...
    uvec4 selection[num_selection_words];
} ubo;

void main() {
    uint id = gl_InstanceIndex;
    uint bit_index = id % 32;
    uint word_index = (id / 32) % 4;
    uint vec_index = id / (num_selection_words * 32 * 4);
    uint n = ubo.selection[vec_index][word_index];
    is_selected = n & (1u << bit_index);
...
}