diff 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
line wrap: on
line diff
--- a/src/zamikongine/descriptor.nim	Sat Jan 14 14:15:50 2023 +0700
+++ b/src/zamikongine/descriptor.nim	Sat Jan 14 23:34:50 2023 +0700
@@ -1,9 +1,17 @@
+import std/strutils
+import std/unicode
+import std/strformat
+import std/typetraits
+
 import ./vulkan
 import ./vulkan_helpers
 import ./math/vector
 import ./math/matrix
 import ./buffer
+import ./glsl_helpers
 
+# TODO: check for alignment in uniform blocks
+#
 type
   DescriptorType = SomeNumber|Vec|Mat
   Descriptor*[T:DescriptorType] = object
@@ -37,3 +45,20 @@
       persistentMapping=true,
     )
     result[i] = buffer
+
+template getDescriptorType*(v: Descriptor): auto = get(genericParams(typeof(v)), 0)
+
+func generateGLSLUniformDeclarations*[Uniforms](binding: int = 0): string {.compileTime.} =
+  var stmtList: seq[string]
+
+  let uniformTypeName = name(Uniforms).toUpper()
+  let uniformInstanceName = name(Uniforms).toLower()
+  stmtList.add(&"layout(binding = {binding}) uniform {uniformTypeName} {{")
+  for fieldname, value in Uniforms().fieldPairs:
+    when typeof(value) is Descriptor:
+      let glsltype = getGLSLType[getDescriptorType(value)]()
+      let n = fieldname
+      stmtList.add(&"    {glsltype} {n};")
+  stmtList.add(&"}} {uniformInstanceName};")
+
+  return stmtList.join("\n")