GLSL - 为什么不能用`uint64_t`索引数组?

问题描述

我有这个编译良好的 GLSL 代码

#version 450
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable

layout (local_size_x = 256) in;

layout(binding = 1) buffer OutBuffer {
    uint64_t outBuf[];
};

void main()
{
    uint myId = gl_GlobalInvocationID.x;
    outBuf[myId] = 0;
}

如果我将 myId 的类型从 uint 更改为 uint64_t,它不会编译:

ERROR: calc.comp.glsl:13: '[]' : scalar integer expression required 

我只能使用 uint,但我很好奇你为什么不能使用 uint64_t

解决方法

当用于索引数组时,除 uintint 之外的任何内容都需要显式转换为以下类型之一:

uint64_t myId = gl_GlobalInvocationID.x;
outBuf[uint(myId)] = 0;

spec GL_EXT_shader_explicit_arithmetic_types_*** 似乎没有说明使用它引入索引数组的类型。

它定义了隐含的提升规则,例如uint16_t -> uint32_t(定义为等价于uint)。
这些当然适用于函数参数,
但奇怪的是,你甚至不能使用 uint16_t 作为数组索引,并期望它被隐式提升为 'uint32_t';您需要将其显式转换为 uint(或 uint32_t)。

所以我们在索引数组时受原始 GLSL 规范的支配;使用 uintsint,这是它知道的唯一标量整数类型。