SPIR允许非常详细数据格式.
GLSL只有不指定位长的基本数据类型(第四章)。
据我所知,为Vulkan编写着色器最方便的方法是在GLSL中编程,然后使用Vulkan提供的编译器(glslc.exe)将文件转换为SPIR二进制文件。
我的问题是如何使用这些冗长的数据格式,如VK_FORMAT_R4G4_UNORM_PACK8 (在上面的SPIR链接中找到)在GLSL中,同时使用glslc.exe编译我们的着色代码。编译器是否允许特殊的数据类型?如果没有,是否有其他高级语言可供使用,然后编译成二进制语言?
例如,如果这是图形管道中使用的属性描述:
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() {
...
}发布于 2021-01-23 19:30:03
正在进行的GLSL着色器代码如下所示:
不,它不会。你会收到一个vec2在着色器,因为这是顶点属性的工作方式。顶点格式并不意味着与数据格式完全匹配;数据将从该格式转换为着色器期望的位深度。无符号归一化值是浮点数据,因此2向量vec2映射到GLSL .
而BTW,SPIR-V并不改变这一点。着色器的输入大小不需要与给定的数据大小完全匹配;任何转换都只需放入着色器(这也是顶点格式是管道一部分的原因之一)。
存储扩展在GLSL中提供了更多的灵活性,可以在缓冲区支持的接口块中创建异常大小的数据类型。但这些是专门用于UBO/SSBO格式的数据,而不是顶点格式。然而,这个扩展需要SPV_KHR_16bit_storage和SPV_KHR_8bit_storage SPIR扩展.
https://stackoverflow.com/questions/65862652
复制相似问题