annotate src/vertex.nim @ 478:871ee602bf95

add: vertex basics, some refactoring
author Sam <sam@basx.dev>
date Sun, 01 Jan 2023 01:00:50 +0700
parents d4750f2e0760
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
478
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
1 import std/macros
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
2 import std/strutils
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
3 import std/strformat
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
4 import std/typetraits
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
5
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
6 import ./math/vector
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
7 import ./vulkan
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
8
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
9 type
478
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
10 VertexAttributeType = SomeNumber|Vec
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
11 VertexAttribute*[T:VertexAttributeType] = object
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
12
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
13
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
14 # from https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap15.html
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
15 func nLocationSlots[T: VertexAttributeType](): int =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
16 when (T is Vec3[float64] or T is Vec3[uint64] or T is Vec4[float64] or T is Vec4[float64]):
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
17 2
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
18 else:
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
19 1
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
20
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
21 # numbers
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
22 func getVkFormat[T: VertexAttributeType](): VkFormat =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
23 when T is uint8: VK_FORMAT_R8_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
24 elif T is int8: VK_FORMAT_R8_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
25 elif T is uint16: VK_FORMAT_R16_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
26 elif T is int16: VK_FORMAT_R16_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
27 elif T is uint32: VK_FORMAT_R32_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
28 elif T is int32: VK_FORMAT_R32_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
29 elif T is uint64: VK_FORMAT_R64_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
30 elif T is int64: VK_FORMAT_R64_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
31 elif T is float32: VK_FORMAT_R32_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
32 elif T is float64: VK_FORMAT_R64_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
33 elif T is Vec2[uint8]: VK_FORMAT_R8G8_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
34 elif T is Vec2[int8]: VK_FORMAT_R8G8_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
35 elif T is Vec2[uint16]: VK_FORMAT_R16G16_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
36 elif T is Vec2[int16]: VK_FORMAT_R16G16_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
37 elif T is Vec2[uint32]: VK_FORMAT_R32G32_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
38 elif T is Vec2[int32]: VK_FORMAT_R32G32_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
39 elif T is Vec2[uint64]: VK_FORMAT_R64G64_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
40 elif T is Vec2[int64]: VK_FORMAT_R64G64_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
41 elif T is Vec2[float32]: VK_FORMAT_R32G32_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
42 elif T is Vec2[float64]: VK_FORMAT_R64G64_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
43 elif T is Vec3[uint8]: VK_FORMAT_R8G8B8_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
44 elif T is Vec3[int8]: VK_FORMAT_R8G8B8_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
45 elif T is Vec3[uint16]: VK_FORMAT_R16G16B16_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
46 elif T is Vec3[int16]: VK_FORMAT_R16G16B16_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
47 elif T is Vec3[uint32]: VK_FORMAT_R32G32B32_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
48 elif T is Vec3[int32]: VK_FORMAT_R32G32B32_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
49 elif T is Vec3[uint64]: VK_FORMAT_R64G64B64_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
50 elif T is Vec3[int64]: VK_FORMAT_R64G64B64_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
51 elif T is Vec3[float32]: VK_FORMAT_R32G32B32_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
52 elif T is Vec3[float64]: VK_FORMAT_R64G64B64_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
53 elif T is Vec4[uint8]: VK_FORMAT_R8G8B8A8_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
54 elif T is Vec4[int8]: VK_FORMAT_R8G8B8A8_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
55 elif T is Vec4[uint16]: VK_FORMAT_R16G16B16A16_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
56 elif T is Vec4[int16]: VK_FORMAT_R16G16B16A16_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
57 elif T is Vec4[uint32]: VK_FORMAT_R32G32B32A32_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
58 elif T is Vec4[int32]: VK_FORMAT_R32G32B32A32_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
59 elif T is Vec4[uint64]: VK_FORMAT_R64G64B64A64_UINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
60 elif T is Vec4[int64]: VK_FORMAT_R64G64B64A64_SINT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
61 elif T is Vec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
62 elif T is Vec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
63
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
64 func getGLSLType[T: VertexAttributeType](): string =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
65 # todo: likely not correct as we would need to enable some
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
66 # extensions somewhere (Vulkan/GLSL compiler?) to have
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
67 # everything work as intended. Or maybe the GPU driver does
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
68 # some automagic conversion stuf..
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
69 when T is uint8: "uint"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
70 elif T is int8: "int"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
71 elif T is uint16: "uint"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
72 elif T is int16: "int"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
73 elif T is uint32: "uint"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
74 elif T is int32: "int"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
75 elif T is uint64: "uint"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
76 elif T is int64: "int"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
77 elif T is float32: "float"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
78 elif T is float64: "double"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
79
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
80 elif T is Vec2[uint8]: "uvec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
81 elif T is Vec2[int8]: "ivec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
82 elif T is Vec2[uint16]: "uvec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
83 elif T is Vec2[int16]: "ivec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
84 elif T is Vec2[uint32]: "uvec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
85 elif T is Vec2[int32]: "ivec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
86 elif T is Vec2[uint64]: "uvec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
87 elif T is Vec2[int64]: "ivec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
88 elif T is Vec2[float32]: "vec2"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
89 elif T is Vec2[float64]: "dvec2"
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
90
478
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
91 elif T is Vec3[uint8]: "uvec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
92 elif T is Vec3[int8]: "ivec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
93 elif T is Vec3[uint16]: "uvec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
94 elif T is Vec3[int16]: "ivec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
95 elif T is Vec3[uint32]: "uvec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
96 elif T is Vec3[int32]: "ivec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
97 elif T is Vec3[uint64]: "uvec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
98 elif T is Vec3[int64]: "ivec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
99 elif T is Vec3[float32]: "vec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
100 elif T is Vec3[float64]: "dvec3"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
101
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
102 elif T is Vec4[uint8]: "uvec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
103 elif T is Vec4[int8]: "ivec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
104 elif T is Vec4[uint16]: "uvec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
105 elif T is Vec4[int16]: "ivec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
106 elif T is Vec4[uint32]: "uvec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
107 elif T is Vec4[int32]: "ivec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
108 elif T is Vec4[uint64]: "uvec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
109 elif T is Vec4[int64]: "ivec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
110 elif T is Vec4[float32]: "vec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
111 elif T is Vec4[float64]: "dvec4"
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
112
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
113 template rawAttributeType(v: VertexAttribute): auto = get(genericParams(typeof(v)), 0)
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
114
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
115 func generateGLSL[T](): string =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
116 var stmtList: seq[string]
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
117 var i = 0
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
118 for name, value in T().fieldPairs:
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
119 when typeof(value) is VertexAttribute:
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
120 let glsltype = getGLSLType[rawAttributeType(value)]()
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
121 let n = name
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
122 stmtList.add(&"layout(location = {i}) in {glsltype} {n};")
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
123 i += nLocationSlots[rawAttributeType(value)]()
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
124
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
125 return stmtList.join("\n")
474
d4750f2e0760 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
126
478
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
127 func generateInputVertexBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputBindingDescription] =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
128 # packed attribute data, not interleaved (aks "struct of arrays")
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
129 var binding = bindingoffset
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
130 for name, value in T().fieldPairs:
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
131 when typeof(value) is VertexAttribute:
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
132 result.add(
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
133 VkVertexInputBindingDescription(
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
134 binding: uint32(binding),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
135 stride: uint32(sizeof(rawAttributeType(value))),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
136 inputRate: VK_VERTEX_INPUT_RATE_VERTEX, # VK_VERTEX_INPUT_RATE_INSTANCE for instances
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
137 )
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
138 )
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
139 binding += 1
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
140
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
141 func generateInputAttributeBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputAttributeDescription] =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
142 # packed attribute data, not interleaved (aks "struct of arrays")
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
143 var location = 0
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
144 var binding = bindingoffset
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
145 for name, value in T().fieldPairs:
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
146 when typeof(value) is VertexAttribute:
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
147 result.add(
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
148 VkVertexInputAttributeDescription(
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
149 binding: uint32(binding),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
150 location: uint32(location),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
151 format: getVkFormat[rawAttributeType(value)](),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
152 offset: 0,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
153 )
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
154 )
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
155 location += nLocationSlots[rawAttributeType(value)]()
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
156 binding += 1
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
157
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
158 func getBindingDescription(binding: int): auto =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
159 VkVertexInputBindingDescription(
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
160 binding: uint32(binding),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
161 stride: 0, # either sizeof of vertex (array of structs) or of attribute (struct of arrays)
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
162 inputRate: VK_VERTEX_INPUT_RATE_VERTEX, # VK_VERTEX_INPUT_RATE_INSTANCE for instances
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
163 )
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
164
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
165 func getAttributeDescriptions(binding: int): auto =
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
166 [
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
167 VkVertexInputAttributeDescription(
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
168 binding: 0'u32,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
169 location: 0,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
170 format: VK_FORMAT_R32G32_SFLOAT,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
171 offset: 0,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
172 ),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
173 VkVertexInputAttributeDescription(
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
174 binding: 0'u32,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
175 location: 1,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
176 format: VK_FORMAT_R32G32B32_SFLOAT,
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
177 offset: uint32(sizeof(Vec2)), # use offsetOf?
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
178 ),
871ee602bf95 add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 474
diff changeset
179 ]