annotate src/shader.nim @ 478:871ee602bf95

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