-
Notifications
You must be signed in to change notification settings - Fork 0
SPIRV parsing
Parsing the SPIRV binary code simplifies the creation of the pipeline layout and vertex attributes bindings which is often an slow and unpleasant experience. Almost all the information we needs to create the pipeline layout, descriptors and vertex attributes are already in the shader code. Parsing the glsl code directly could be very difficult but parsing the spirv is surprisingly simple. It's organized in a way that a computer can easily parse it.
It is possible to see an output of the spirv in command line if you are interested. It's useful when debugging the SpirvParse. Vulkan providers the spirv-dis.exe tool juste for that. You will find it in the bin folder for your Vulkan SDK installation.
spirv-dis.exe [sourcespv]Exemple:
spirv-dis.exe shader-simple.vert.spv
The Renderer has a CreateShader method that creates a ShaderWrapper. The ShaderWrapper calls SpirvParser.ParseBytes and update the definition of the ShaderWrapper. Note that the CreateShader will also call ShaderCompiler.Compile if provided only with text code.
Exemple:
var shader = Context.Renderer.CreateShader(new()
{
VertexCode = @"#version 450
//push constants block
layout( push_constant ) uniform constants
{
mat4 _matrix_mvp;
};
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
void main() {
gl_Position = _matrix_mvp * vec4(inPosition, 1.0);
fragColor = inColor;
fragTexCoord = inTexCoord;
}",
FragmentCode = @"#version 450
#extension GL_EXT_nonuniform_qualifier : enable
layout(push_constant) uniform constants {
layout(offset = 64) int _mat_diffuse_index;
};
layout(binding = 1) uniform sampler2D _sampler_diffuse[];
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 0) out vec4 outColor;
void main() {
outColor = texture(_sampler_diffuse[_mat_diffuse_index], fragTexCoord);
}"
,
VariableDefinitions = new()
{
{ "_sampler_diffuse", new() { Count = 10, Bindless = true } }
}
});