Create shader modules

In order to use a shader in Vulkan, the shader file will first need to be compiled into a shader module. In the shader module, the shader bytecode is loaded and validated, with all entry points mapped. We can then later refer to these shader modules when constructing graphics pipeline state.

VkShaderModule vs, fs;
result = vkCreateShaderModule (
    renderer->device,
    &(VkShaderModuleCreateInfo){
        .sType    = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
        .pNext    = NULL,
        .flags    = 0,
        .codeSize = vsAsset->size,
        .pCode    = (const uint32_t*)vsAsset->data,
    },
    NULL,
    &vs
);
if ( result != VK_SUCCESS )
    RETURN_ERROR(-1, "vkCreateShaderModule failed (0x%08X)", (uint32_t)result);

result = vkCreateShaderModule (
    renderer->device,
    &(VkShaderModuleCreateInfo){
        .sType    = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
        .pNext    = NULL,
        .flags    = 0,
        .codeSize = fsAsset->size,
        .pCode    = (const uint32_t*)fsAsset->data,
    },
    NULL,
    &fs
);
if ( result != VK_SUCCESS )
    RETURN_ERROR(-1, "vkCreateShaderModule failed (0x%08X)", (uint32_t)result);

What follows is the shaders being employed in this example.

Vertex:

#version 400

#extension GL_ARB_separate_shader_objects  : enable
#extension GL_ARB_shading_language_420pack : enable

layout(push_constant, binding = 0)
uniform CB
{
    mat4 MVP;
} cb;

layout(location = 0)
in vec3 inPosition;
layout(location = 1)
in vec2 inTexcoord;

layout(location = 0)
out vec2 outTexcoord;

out gl_PerVertex
{
    vec4 gl_Position;
};

void main()
{
    vec4 transformedPosition = cb.MVP * vec4 ( inPosition, 1.0 );
    gl_Position = transformedPosition * vec4 ( 1.0, -1.0, 1.0, 1.0 );
    outTexcoord = inTexcoord;
}

Fragment:

#version 400

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout(binding = 0)
uniform sampler2D tex[64];

layout(location = 0)
in vec2 texcoord;

layout(location = 0)
out vec4 fragColor;

layout(push_constant, binding = 64)
uniform CB
{
    mat4 wat;
    uint textureIndex;
} cb;

void main()
{
    fragColor = texture ( tex[cb.textureIndex], texcoord );
}