annotate src/zamikongine/descriptor.nim @ 32:9edca5dc4e93

add: working implementation of uniforms
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 23:34:50 +0700
parents 917d99d5eb89
children 94c38e4b5782
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
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
36 proc createUniformBuffers*[nBuffers: static int, T](device: VkDevice, physicalDevice: VkPhysicalDevice): array[nBuffers, Buffer] =
917d99d5eb89 add: missing file
Sam <sam@basx.dev>
parents:
diff changeset
37 let size = sizeof(T)
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
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
54 let uniformTypeName = name(Uniforms).toUpper()
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
55 let uniformInstanceName = name(Uniforms).toLower()
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
56 stmtList.add(&"layout(binding = {binding}) uniform {uniformTypeName} {{")
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
57 for fieldname, value in Uniforms().fieldPairs:
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
58 when typeof(value) is Descriptor:
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
59 let glsltype = getGLSLType[getDescriptorType(value)]()
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
60 let n = fieldname
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
61 stmtList.add(&" {glsltype} {n};")
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
62 stmtList.add(&"}} {uniformInstanceName};")
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
63
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 30
diff changeset
64 return stmtList.join("\n")