annotate src/zamikongine/descriptor.nim @ 33:94c38e4b5782

did: refactoring, move more from make to nimscript
author Sam <sam@basx.dev>
date Sun, 15 Jan 2023 23:23:54 +0700
parents 9edca5dc4e93
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
1 import std/strutils
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
2 import std/unicode
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
3 import std/strformat
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
4 import std/typetraits
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
5
30
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./vulkan
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
7 import ./vulkan_helpers
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
8 import ./math/vector
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
9 import ./math/matrix
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./buffer
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
11 import ./glsl_helpers
30
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
12
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
13 # TODO: check for alignment in uniform blocks
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
14 #
30
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
15 type
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
16 DescriptorType = SomeNumber|Vec|Mat
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
17 Descriptor*[T:DescriptorType] = object
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
18 value*: T
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
19
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
20 proc createUniformDescriptorLayout*(device: VkDevice, shaderStage: VkShaderStageFlags, binding: uint32): VkDescriptorSetLayout =
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
21 var
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
22 layoutbinding = VkDescriptorSetLayoutBinding(
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
23 binding: binding,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
24 descriptorType: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
25 descriptorCount: 1,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
26 stageFlags: shaderStage,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
27 pImmutableSamplers: nil,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
28 )
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
29 layoutInfo = VkDescriptorSetLayoutCreateInfo(
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
30 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
31 bindingCount: 1,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
32 pBindings: addr(layoutbinding)
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
33 )
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
34 checkVkResult device.vkCreateDescriptorSetLayout(addr(layoutInfo), nil, addr(result))
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
35
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
36 proc createUniformBuffers*[nBuffers: static int, Uniforms](device: VkDevice, physicalDevice: VkPhysicalDevice): array[nBuffers, Buffer] =
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
37 let size = sizeof(Uniforms)
30
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
38 for i in 0 ..< nBuffers:
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
39 var buffer = InitBuffer(
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
40 device,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
41 physicalDevice,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
42 uint64(size),
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
43 {UniformBuffer},
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
44 {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT},
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
45 persistentMapping=true,
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
46 )
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
47 result[i] = buffer
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
48
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
49 template getDescriptorType*(v: Descriptor): auto = get(genericParams(typeof(v)), 0)
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
50
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
51 func generateGLSLUniformDeclarations*[Uniforms](binding: int = 0): string {.compileTime.} =
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
52 var stmtList: seq[string]
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
53
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
54 when not (Uniforms is void):
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
55 let uniformTypeName = name(Uniforms).toUpper()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
56 let uniformInstanceName = name(Uniforms).toLower()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
57 stmtList.add(&"layout(binding = {binding}) uniform {uniformTypeName} {{")
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
58 for fieldname, value in Uniforms().fieldPairs:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
59 when typeof(value) is Descriptor:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
60 let glsltype = getGLSLType[getDescriptorType(value)]()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
61 let n = fieldname
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
62 stmtList.add(&" {glsltype} {n};")
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
63 stmtList.add(&"}} {uniformInstanceName};")
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
64
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
65 return stmtList.join("\n")