32
|
1 import std/strutils
|
|
2 import std/unicode
|
|
3 import std/strformat
|
|
4 import std/typetraits
|
|
5
|
30
|
6 import ./vulkan
|
|
7 import ./vulkan_helpers
|
|
8 import ./math/vector
|
|
9 import ./math/matrix
|
|
10 import ./buffer
|
32
|
11 import ./glsl_helpers
|
30
|
12
|
32
|
13 # TODO: check for alignment in uniform blocks
|
|
14 #
|
30
|
15 type
|
|
16 DescriptorType = SomeNumber|Vec|Mat
|
|
17 Descriptor*[T:DescriptorType] = object
|
|
18 value*: T
|
|
19
|
|
20 proc createUniformDescriptorLayout*(device: VkDevice, shaderStage: VkShaderStageFlags, binding: uint32): VkDescriptorSetLayout =
|
|
21 var
|
|
22 layoutbinding = VkDescriptorSetLayoutBinding(
|
|
23 binding: binding,
|
|
24 descriptorType: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
25 descriptorCount: 1,
|
|
26 stageFlags: shaderStage,
|
|
27 pImmutableSamplers: nil,
|
|
28 )
|
|
29 layoutInfo = VkDescriptorSetLayoutCreateInfo(
|
|
30 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
|
31 bindingCount: 1,
|
|
32 pBindings: addr(layoutbinding)
|
|
33 )
|
|
34 checkVkResult device.vkCreateDescriptorSetLayout(addr(layoutInfo), nil, addr(result))
|
|
35
|
|
36 proc createUniformBuffers*[nBuffers: static int, T](device: VkDevice, physicalDevice: VkPhysicalDevice): array[nBuffers, Buffer] =
|
|
37 let size = sizeof(T)
|
|
38 for i in 0 ..< nBuffers:
|
|
39 var buffer = InitBuffer(
|
|
40 device,
|
|
41 physicalDevice,
|
|
42 uint64(size),
|
|
43 {UniformBuffer},
|
|
44 {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT},
|
|
45 persistentMapping=true,
|
|
46 )
|
|
47 result[i] = buffer
|
32
|
48
|
|
49 template getDescriptorType*(v: Descriptor): auto = get(genericParams(typeof(v)), 0)
|
|
50
|
|
51 func generateGLSLUniformDeclarations*[Uniforms](binding: int = 0): string {.compileTime.} =
|
|
52 var stmtList: seq[string]
|
|
53
|
|
54 let uniformTypeName = name(Uniforms).toUpper()
|
|
55 let uniformInstanceName = name(Uniforms).toLower()
|
|
56 stmtList.add(&"layout(binding = {binding}) uniform {uniformTypeName} {{")
|
|
57 for fieldname, value in Uniforms().fieldPairs:
|
|
58 when typeof(value) is Descriptor:
|
|
59 let glsltype = getGLSLType[getDescriptorType(value)]()
|
|
60 let n = fieldname
|
|
61 stmtList.add(&" {glsltype} {n};")
|
|
62 stmtList.add(&"}} {uniformInstanceName};")
|
|
63
|
|
64 return stmtList.join("\n")
|