gpt4 book ai didi

opengl - 用于数组大小的专门化常量

转载 作者:行者123 更新时间:2023-12-02 20:02:01 24 4
gpt4 key购买 nike

我正在尝试使用 SPIR-V 专门化常量来定义统一 block 中数组的大小。

#version 460 core

layout(constant_id = 0) const uint count = 0;

layout(binding = 0) uniform Uniform
{
vec4 foo[count];
uint bar[count];
};

void main() {}

在着色器中声明 count = 0 时,编译失败并显示:

array size must be a positive integer

使用 count = 1 且特化为 5,代码可以编译,但在运行时链接会失败,并会出现别名问题:

error: different uniforms (named Uniform.foo[4] and Uniform.bar[3]) sharing the same offset within a uniform block (named Uniform) between shaders
error: different uniforms (named Uniform.foo[3] and Uniform.bar[2]) sharing the same offset within a uniform block (named Uniform) between shaders
error: different uniforms (named Uniform.foo[2] and Uniform.bar[1]) sharing the same offset within a uniform block (named Uniform) between shaders
error: different uniforms (named Uniform.foo[1] and Uniform.bar[0]) sharing the same offset within a uniform block (named Uniform) between shaders

似乎uniform block 的布局(每个成员的偏移量)在专门化期间不受影响,因此foobar重叠。

显式偏移也不起作用并导致相同的链接错误:

layout(binding = 0, std140) uniform Uniform
{
layout(offset = 0) vec4 foo[count];
layout(offset = count) uint bar[count];
};

这是预期的行为吗?俯瞰?可以使用专门化常量来定义数组的大小吗?

最佳答案

这是 ARB_spir_v 的一个奇怪的现象。来自 the extension specification :

Arrays inside a block may be sized with a specialization constant, but the block will have a static layout. Changing the specialized size will not re-layout the block. In the absence of explicit offsets, the layout will be based on the default size of the array.

由于默认大小为 0,因此 block 中的结构将按照数组大小为零的方式进行布局。

基本上,您可以使用专门化常量使数组比默认值短,但不能更长。即使您将它们缩短,它们仍然占用与默认空间相同的空间。

实际上,在 block 数组长度中使用专门化常量只是用默认值作为长度声明数组的一种简写方式,然后将使用 name.length() 的位置替换为专门化常量/表达式。它纯粹是语法糖。

关于opengl - 用于数组大小的专门化常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com