comparison src/zamikongine/descriptor.nim @ 493:680c4b8ca28a

add: working implementation of uniforms
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 23:34:50 +0700
parents f18331a8a314
children 94c38e4b5782
comparison
equal deleted inserted replaced
492:d63d980fa3bb 493:680c4b8ca28a
1 import std/strutils
2 import std/unicode
3 import std/strformat
4 import std/typetraits
5
1 import ./vulkan 6 import ./vulkan
2 import ./vulkan_helpers 7 import ./vulkan_helpers
3 import ./math/vector 8 import ./math/vector
4 import ./math/matrix 9 import ./math/matrix
5 import ./buffer 10 import ./buffer
11 import ./glsl_helpers
6 12
13 # TODO: check for alignment in uniform blocks
14 #
7 type 15 type
8 DescriptorType = SomeNumber|Vec|Mat 16 DescriptorType = SomeNumber|Vec|Mat
9 Descriptor*[T:DescriptorType] = object 17 Descriptor*[T:DescriptorType] = object
10 value*: T 18 value*: T
11 19
35 {UniformBuffer}, 43 {UniformBuffer},
36 {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT}, 44 {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT},
37 persistentMapping=true, 45 persistentMapping=true,
38 ) 46 )
39 result[i] = buffer 47 result[i] = buffer
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")