0

SPIR-V 允许非常冗长的数据格式

GLSL 只有基本数据类型(第 4 章),没有指定位长。

据我所知,为 Vulkan 编写着色器最方便的方法是在 GLSL 中对其进行编程,然后使用 Vulkan SDK 提供的编译器 (glslc.exe) 将文件转换为 SPIR-V 二进制文件。

VK_FORMAT_R4G4_UNORM_PACK8我的问题是如何在使用 glslc.exe 编译我们的着色器代码时在 GLSL 中使用这些详细的数据格式,例如(在上面的 SPIR-V 链接中找到)。有编译器允许的特殊数据类型吗?如果没有,是否可以使用另一种更高级别的语言,然后编译成二进制文件?

例如,如果这是图形管道中使用的属性描述:

struct Attributes {
   vec2 pos;
   char flags;
};

static inline std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() {
   std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions{};
   attributeDescriptions[0].binding = 0;
   attributeDescriptions[0].location = 0;
   attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
   attributeDescriptions[0].offset = offsetof(Attributes, pos);

   attributeDescriptions[1].binding = 0;
   attributeDescriptions[1].location = 1;
   attributeDescriptions[1].format = VK_FORMAT_R4G4_UNORM_PACK8;
   attributeDescriptions[1].offset = offsetof(Attributes, flags);

return attributeDescriptions;

正在进行的 GLSL 着色器代码如下所示:

#version 450
#extension GL_ARB_separate_shader_objects : enable

//Instance Attributes
layout(location = 0) in vec2 pos;
layout(location = 1) in 4BitVec2DataType flags;         
//4BitVec2DataType is a placeholder for whatever GLSL's equivalent of SPIR-V's VK_FORMAT_R4G4_UNORM_PACK8 would be

void main() {
    ...
}
4

1 回答 1

3

正在进行的 GLSL 着色器代码如下所示:

不,它不会。您会vec2在着色器中收到 a,因为这就是顶点属性的工作方式。顶点格式并不意味着完全匹配数据格式;数据将从该格式转换为着色器预期的位深度。无符号归一化值是浮点数据,因此 2 向量 UNORM 映射到 GLSL vec2

顺便说一句,SPIR-V并没有改变这一点。着色器的输入大小不需要完全匹配给定的数据大小;任何转换都只是烘焙到着色器中(这也是为什么顶点格式是管道的一部分的部分原因)。

GL_EXT_shader_16bit_storage扩展在 GLSL提供了更大的灵活性,可以在缓冲区支持的接口块中创建异常大小的数据类型。但这些专门用于 UBO/SSBO 中的数据,而不是顶点格式。但是,此扩展需要 SPV_KHR_16bit_storage 和 SPV_KHR_8bit_storage SPIR-V 扩展。

于 2021-01-23T19:30:03.100 回答