annotate src/semicongine/gpu_data.nim @ 120:2780d9aad142

add: better mesh support, indexed mesh
author Sam <sam@basx.dev>
date Thu, 06 Apr 2023 00:30:56 +0700
parents 056e08dfad10
children dfaddaf96f09
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
114
056e08dfad10 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 113
diff changeset
1 import std/sequtils
113
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
2 import std/typetraits
111
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/strformat
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/tables
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
5
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./vulkan/api
113
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
7 import ./math
111
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
8
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
9 type
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
10 CountType = 1'u32 .. 4'u32
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
11 DataType* = enum
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
12 Float32
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
13 Float64
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
14 Int8
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
15 Int16
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
16 Int32
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
17 Int64
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
18 UInt8
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
19 UInt16
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
20 UInt32
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
21 UInt64
120
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
22 MemoryLocation* = enum
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
23 VRAM, VRAMVisible, RAM # VRAM is fastest, VRAMVisible allows updating memory directly, may be slower
111
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
24 Attribute* = object
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
25 name*: string
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
26 thetype*: DataType
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
27 components*: CountType # how many components the vectors has (1 means scalar)
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
28 rows*: CountType # used to split matrices into rows of vectors
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
29 perInstance*: bool
120
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
30 memoryLocation*: MemoryLocation
111
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
31 AttributeGroup* = object
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
32 attributes*: seq[Attribute]
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
33
114
056e08dfad10 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 113
diff changeset
34 func initAttributeGroup*(attrs: varargs[Attribute]): auto =
056e08dfad10 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 113
diff changeset
35 AttributeGroup(attributes: attrs.toSeq)
056e08dfad10 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 113
diff changeset
36
113
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
37 func vertexInputs*(group: AttributeGroup): seq[Attribute] =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
38 for attr in group.attributes:
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
39 if attr.perInstance == false:
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
40 result.add attr
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
41
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
42 func instanceInputs*(group: AttributeGroup): seq[Attribute] =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
43 for attr in group.attributes:
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
44 if attr.perInstance == false:
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
45 result.add attr
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
46
120
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
47 func attr*(
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
48 name: string,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
49 thetype: DataType,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
50 components=CountType(1),
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
51 rows=CountType(1),
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
52 perInstance=false,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
53 memoryLocation=VRAMVisible,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
54 ): auto =
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
55 Attribute(
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
56 name: name,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
57 thetype: thetype,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
58 components: components,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
59 rows: rows,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
60 perInstance: perInstance,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
61 memoryLocation: memoryLocation,
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
62 )
111
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
63
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
64 func size*(thetype: DataType): uint32 =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
65 case thetype:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
66 of Float32: 4
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
67 of Float64: 8
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
68 of Int8: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
69 of Int16: 2
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
70 of Int32: 4
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
71 of Int64: 8
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
72 of UInt8: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
73 of UInt16: 2
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
74 of UInt32: 4
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
75 of UInt64: 8
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
76
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
77 func size*(attribute: Attribute, perRow=false): uint32 =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
78 if perRow:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
79 attribute.thetype.size * attribute.components
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
80 else:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
81 attribute.thetype.size * attribute.components * attribute.rows
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
82
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
83 func size*(thetype: AttributeGroup): uint32 =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
84 for attribute in thetype.attributes:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
85 result += attribute.size
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
86
113
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
87 func getDataType*[T: SomeNumber](value: T): DataType =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
88 when T is float32: Float32
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
89 elif T is float64: Float64
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
90 elif T is int8: Int8
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
91 elif T is int16: Int16
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
92 elif T is int32: Int32
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
93 elif T is int64: Int64
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
94 elif T is uint8: UInt8
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
95 elif T is uint16: UInt16
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
96 elif T is uint32: UInt32
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
97 elif T is uint64: UInt64
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
98 elif T is int and sizeof(int) == sizeof(int64): Int64
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
99 elif T is int and sizeof(int) == sizeof(int32): Int32
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
100 elif T is uint and sizeof(uint) == sizeof(uint64): UInt64
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
101 elif T is uint and sizeof(uint) == sizeof(uint32): UInt32
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
102 elif T is float and sizeof(float) == sizeof(float32): Float32
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
103 elif T is float and sizeof(float) == sizeof(float64): Float64
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
104 else:
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
105 static:
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
106 raise newException(Exception, &"Unsupported data type for GPU data: {name(T)}" )
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
107
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
108 func asAttribute*[T: SomeNumber|TMat|TVec](value: T, name: string): Attribute =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
109 when not (T is SomeNumber):
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
110 let nonScalarDatatype = getDataType(default(get(genericParams(typeof(value)), 0)))
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
111
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
112 when T is SomeNumber: attr(name, getDataType(default(T)))
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
113 elif T is TMat22: attr(name, nonScalarDatatype, 2, 2)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
114 elif T is TMat23: attr(name, nonScalarDatatype, 3, 2)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
115 elif T is TMat32: attr(name, nonScalarDatatype, 3, 2)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
116 elif T is TMat33: attr(name, nonScalarDatatype, 3, 3)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
117 elif T is TMat34: attr(name, nonScalarDatatype, 4, 3)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
118 elif T is TMat43: attr(name, nonScalarDatatype, 3, 4)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
119 elif T is TMat44: attr(name, nonScalarDatatype, 4, 4)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
120 elif T is TVec2: attr(name, nonScalarDatatype, 2)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
121 elif T is TVec3: attr(name, nonScalarDatatype, 3)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
122 elif T is TVec4: attr(name, nonScalarDatatype, 4)
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
123 else: {.error "Unsupported attribute type for GPU data".}
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
124
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
125 func asAttribute*[T: SomeNumber|TMat|TVec](): Attribute =
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
126 asAttribute(default(T), name(T))
7b695fb335ed did: first final implementation of scene-graph <-> pipeline connection, not working yet
Sam <sam@basx.dev>
parents: 112
diff changeset
127
111
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
128 const TYPEMAP = {
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
129 CountType(1): {
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
130 UInt8: VK_FORMAT_R8_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
131 Int8: VK_FORMAT_R8_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
132 UInt16: VK_FORMAT_R16_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
133 Int16: VK_FORMAT_R16_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
134 UInt32: VK_FORMAT_R32_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
135 Int32: VK_FORMAT_R32_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
136 UInt64: VK_FORMAT_R64_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
137 Int64: VK_FORMAT_R64_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
138 Float32: VK_FORMAT_R32_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
139 Float64: VK_FORMAT_R64_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
140 }.toTable,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
141 CountType(2): {
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
142 UInt8: VK_FORMAT_R8G8_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
143 Int8: VK_FORMAT_R8G8_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
144 UInt16: VK_FORMAT_R16G16_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
145 Int16: VK_FORMAT_R16G16_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
146 UInt32: VK_FORMAT_R32G32_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
147 Int32: VK_FORMAT_R32G32_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
148 UInt64: VK_FORMAT_R64G64_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
149 Int64: VK_FORMAT_R64G64_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
150 Float32: VK_FORMAT_R32G32_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
151 Float64: VK_FORMAT_R64G64_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
152 }.toTable,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
153 CountType(3): {
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
154 UInt8: VK_FORMAT_R8G8B8_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
155 Int8: VK_FORMAT_R8G8B8_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
156 UInt16: VK_FORMAT_R16G16B16_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
157 Int16: VK_FORMAT_R16G16B16_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
158 UInt32: VK_FORMAT_R32G32B32_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
159 Int32: VK_FORMAT_R32G32B32_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
160 UInt64: VK_FORMAT_R64G64B64_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
161 Int64: VK_FORMAT_R64G64B64_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
162 Float32: VK_FORMAT_R32G32B32_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
163 Float64: VK_FORMAT_R64G64B64_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
164 }.toTable,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
165 CountType(4): {
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
166 UInt8: VK_FORMAT_R8G8B8A8_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
167 Int8: VK_FORMAT_R8G8B8A8_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
168 UInt16: VK_FORMAT_R16G16B16A16_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
169 Int16: VK_FORMAT_R16G16B16A16_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
170 UInt32: VK_FORMAT_R32G32B32A32_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
171 Int32: VK_FORMAT_R32G32B32A32_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
172 UInt64: VK_FORMAT_R64G64B64A64_UINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
173 Int64: VK_FORMAT_R64G64B64A64_SINT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
174 Float32: VK_FORMAT_R32G32B32A32_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
175 Float64: VK_FORMAT_R64G64B64A64_SFLOAT,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
176 }.toTable,
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
177 }.toTable
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
178
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
179 func getVkFormat*(value: Attribute): VkFormat =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
180 TYPEMAP[value.components][value.thetype]
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
181
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
182 # from https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap15.html
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
183 func nLocationSlots*(attribute: Attribute): uint32 =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
184 #[
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
185 single location:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
186 16-bit scalar and vector types, and
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
187 32-bit scalar and vector types, and
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
188 64-bit scalar and 2-component vector types.
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
189 two locations
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
190 64-bit three- and four-component vectors
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
191 ]#
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
192 case attribute.thetype:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
193 of Float32: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
194 of Float64: (if attribute.components < 3: 1 else: 2)
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
195 of Int8: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
196 of Int16: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
197 of Int32: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
198 of Int64: (if attribute.components < 3: 1 else: 2)
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
199 of UInt8: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
200 of UInt16: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
201 of UInt32: 1
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
202 of UInt64: (if attribute.components < 3: 1 else: 2)
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
203
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
204 func glslType*(attribute: Attribute): string =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
205 # todo: likely not correct as we would need to enable some
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
206 # extensions somewhere (Vulkan/GLSL compiler?) to have
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
207 # everything work as intended. Or maybe the GPU driver does
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
208 # some automagic conversion stuf..
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
209
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
210 # used to ensure square matrix get only one number for side instead of two, e.g. mat2 instead of mat22
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
211 let matrixColumns = if attribute.components == attribute.rows: "" else: $attribute.components
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
212 case attribute.rows:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
213 of 1:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
214 case attribute.components:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
215 of 1: # scalars
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
216 case attribute.thetype:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
217 of Float32: "float"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
218 of Float64: "double"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
219 of Int8, Int16, Int32, Int64: "int"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
220 of UInt8, UInt16, UInt32, UInt64: "uint"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
221 else: # vectors
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
222 case attribute.thetype:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
223 of Float32: &"vec{attribute.components}"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
224 of Float64: &"dvec{attribute.components}"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
225 of Int8, Int16, Int32, Int64: &"ivec{attribute.components}"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
226 of UInt8, UInt16, UInt32, UInt64: &"uvec{attribute.components}"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
227 else:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
228 case attribute.components:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
229 of 1: raise newException(Exception, &"Unsupported matrix-column-count: {attribute.components}")
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
230 else:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
231 case attribute.thetype:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
232 of Float32: &"mat{attribute.rows}{matrixColumns}"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
233 of Float64: &"dmat{attribute.rows}{matrixColumns}"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
234 else: raise newException(Exception, &"Unsupported matrix-component type: {attribute.thetype}")
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
235
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
236 func glslInput*(group: AttributeGroup): seq[string] =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
237 if group.attributes.len == 0:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
238 return @[]
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
239 var i = 0'u32
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
240 for attribute in group.attributes:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
241 result.add &"layout(location = {i}) in {attribute.glslType} {attribute.name};"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
242 for j in 0 ..< attribute.rows:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
243 i += attribute.nLocationSlots
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
244
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
245 func glslUniforms*(group: AttributeGroup, blockName="Uniforms", binding=0): seq[string] =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
246 if group.attributes.len == 0:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
247 return @[]
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
248 # currently only a single uniform block supported, therefore binding = 0
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
249 result.add(&"layout(binding = {binding}) uniform T{blockName} {{")
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
250 for attribute in group.attributes:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
251 result.add(&" {attribute.glslType} {attribute.name};")
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
252 result.add(&"}} {blockName};")
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
253
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
254 func glslOutput*(group: AttributeGroup): seq[string] =
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
255 if group.attributes.len == 0:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
256 return @[]
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
257 var i = 0'u32
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
258 for attribute in group.attributes:
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
259 result.add &"layout(location = {i}) out {attribute.glslType} {attribute.name};"
6fd10b7e2d6a did: allow runtime shader-input definitions
Sam <sam@basx.dev>
parents:
diff changeset
260 i += 1
120
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
261
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
262 func groupByMemoryLocation*(attributes: openArray[Attribute]): Table[MemoryLocation, seq[Attribute]] =
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
263 for attr in attributes:
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
264 if not (attr.memoryLocation in result):
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
265 result[attr.memoryLocation] = @[]
2780d9aad142 add: better mesh support, indexed mesh
Sam <sam@basx.dev>
parents: 114
diff changeset
266 result[attr.memoryLocation].add attr