在 GLSL 中将 uint 转换为 float 的常量表达式
Constant expression with casting uint to float in GLSL
我这样定义了一个全局常量
const uint Y = 4;
const float X = float(Y);
它工作得很好。现在我想通过使用专业化常量
让它更灵活一点
layout (constant_id = 1) const uint Y = 4;
const float X = float(Y);
不幸的是现在我得到了
error: '=' : global const initializers must be constant ' const highp float'
为什么 GLSL 不能在存在特化常量的情况下执行如此简单的转换?专业化常量不应该与任何其他常量表达式同等对待吗?真的是这个问题唯一可能的解决方案是使用
提供两个常量吗?
layout (constant_id = 1) const uint Y = 4;
layout (constant_id = 2) const float X = 4.;
根据 Vulkan GLSL specification,只有特定的运算符可以用于特化常量,以使其保持特化常量。将一个转换为 float
不在该列表中。
至于为什么不能这样做,是因为Vulkan编译器应该越简单越好。当您有一个特化常量并对其执行一些操作并尝试将其用作常量时,编译器必须在特化时不可见地执行该操作。这对实施来说是一个很大的痛苦,所以 Vulkan GLSL 限制了需要多少。
Vulkan GLSL 规范包含一些错误。这是非规范的文本说这是可能的:
layout(constant_id = 18) const int scX = 1;
layout(constant_id = 19) const int scZ = 1;
const vec3 scVec = vec3(scX, 1, scZ); // partially specialized vector
但是由于从 int
到 float
.
的隐式转换,“专业化常量操作”部分特别指出它不是
我这样定义了一个全局常量
const uint Y = 4;
const float X = float(Y);
它工作得很好。现在我想通过使用专业化常量
让它更灵活一点layout (constant_id = 1) const uint Y = 4;
const float X = float(Y);
不幸的是现在我得到了
error: '=' : global const initializers must be constant ' const highp float'
为什么 GLSL 不能在存在特化常量的情况下执行如此简单的转换?专业化常量不应该与任何其他常量表达式同等对待吗?真的是这个问题唯一可能的解决方案是使用
提供两个常量吗?layout (constant_id = 1) const uint Y = 4;
layout (constant_id = 2) const float X = 4.;
根据 Vulkan GLSL specification,只有特定的运算符可以用于特化常量,以使其保持特化常量。将一个转换为 float
不在该列表中。
至于为什么不能这样做,是因为Vulkan编译器应该越简单越好。当您有一个特化常量并对其执行一些操作并尝试将其用作常量时,编译器必须在特化时不可见地执行该操作。这对实施来说是一个很大的痛苦,所以 Vulkan GLSL 限制了需要多少。
Vulkan GLSL 规范包含一些错误。这是非规范的文本说这是可能的:
layout(constant_id = 18) const int scX = 1; layout(constant_id = 19) const int scZ = 1; const vec3 scVec = vec3(scX, 1, scZ); // partially specialized vector
但是由于从 int
到 float
.