17
|
1 import std/tables
|
|
2
|
|
3 import ./vulkan_helpers
|
|
4 import ./vulkan
|
|
5 import ./glslang/glslang
|
|
6
|
|
7 type
|
|
8 ShaderProgram* = object
|
|
9 entryPoint*: string
|
|
10 programType*: VkShaderStageFlagBits
|
|
11 shader*: VkPipelineShaderStageCreateInfo
|
|
12
|
|
13 proc initShaderProgram*(device: VkDevice, programType: VkShaderStageFlagBits, shader: string, entryPoint: string="main"): ShaderProgram =
|
|
14 result.entryPoint = entryPoint
|
|
15 result.programType = programType
|
|
16
|
|
17 const VK_GLSL_MAP = {
|
|
18 VK_SHADER_STAGE_VERTEX_BIT: GLSLANG_STAGE_VERTEX,
|
|
19 VK_SHADER_STAGE_FRAGMENT_BIT: GLSLANG_STAGE_FRAGMENT,
|
|
20 }.toTable()
|
|
21 var code = compileGLSLToSPIRV(VK_GLSL_MAP[result.programType], shader, "<memory-shader>")
|
|
22 var createInfo = VkShaderModuleCreateInfo(
|
|
23 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
|
|
24 codeSize: uint(code.len * sizeof(uint32)),
|
|
25 pCode: if code.len > 0: addr(code[0]) else: nil,
|
|
26 )
|
|
27 var shaderModule: VkShaderModule
|
|
28 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule))
|
|
29
|
|
30 result.shader = VkPipelineShaderStageCreateInfo(
|
|
31 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
32 stage: programType,
|
|
33 module: shaderModule,
|
|
34 pName: cstring(result.entryPoint), # entry point for shader
|
|
35 )
|