annotate src/shader.nim @ 17:b40466fa446a

add: vertex basics, some refactoring
author Sam <sam@basx.dev>
date Sun, 01 Jan 2023 01:00:50 +0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/tables
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
2
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
3 import ./vulkan_helpers
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
4 import ./vulkan
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
5 import ./glslang/glslang
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
6
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
7 type
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
8 ShaderProgram* = object
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
9 entryPoint*: string
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
10 programType*: VkShaderStageFlagBits
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
11 shader*: VkPipelineShaderStageCreateInfo
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
12
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
13 proc initShaderProgram*(device: VkDevice, programType: VkShaderStageFlagBits, shader: string, entryPoint: string="main"): ShaderProgram =
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
14 result.entryPoint = entryPoint
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
15 result.programType = programType
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
16
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
17 const VK_GLSL_MAP = {
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
18 VK_SHADER_STAGE_VERTEX_BIT: GLSLANG_STAGE_VERTEX,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
19 VK_SHADER_STAGE_FRAGMENT_BIT: GLSLANG_STAGE_FRAGMENT,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
20 }.toTable()
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
21 var code = compileGLSLToSPIRV(VK_GLSL_MAP[result.programType], shader, "<memory-shader>")
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
22 var createInfo = VkShaderModuleCreateInfo(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
23 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
24 codeSize: uint(code.len * sizeof(uint32)),
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
25 pCode: if code.len > 0: addr(code[0]) else: nil,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
26 )
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
27 var shaderModule: VkShaderModule
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
28 checkVkResult vkCreateShaderModule(device, addr(createInfo), nil, addr(shaderModule))
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
29
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
30 result.shader = VkPipelineShaderStageCreateInfo(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
31 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
32 stage: programType,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
33 module: shaderModule,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
34 pName: cstring(result.entryPoint), # entry point for shader
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents:
diff changeset
35 )