annotate src/zamikongine/vertex.nim @ 35:7f99b21a8777

add: support for instance data
author Sam <sam@basx.dev>
date Mon, 16 Jan 2023 00:35:41 +0700
parents 94c38e4b5782
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
1 import std/macros
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
2 import std/strutils
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
3 import std/strformat
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
4 import std/typetraits
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
5
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
6 import ./math/vector
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
7 import ./math/matrix
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
8 import ./vulkan
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
9 import ./glsl_helpers
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
10
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
11 type
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
12 VertexAttributeType = SomeNumber|Vec
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
13 AttributePurpose* = enum
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
14 Unknown, Position Color
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
15 GenericAttribute*[T:VertexAttributeType] = object
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
16 data*: seq[T]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
17 PositionAttribute*[T:Vec] = object
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
18 data*: seq[T]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
19 ColorAttribute*[T:Vec] = object
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
20 data*: seq[T]
35
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
21 InstanceAttribute*[T:Vec] = object
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
22 data*: seq[T]
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
23 VertexAttribute* = GenericAttribute|PositionAttribute|ColorAttribute|InstanceAttribute
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
24
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
25 template getAttributeType*(v: VertexAttribute): auto = get(genericParams(typeof(v)), 0)
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
26
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
27 func datasize*(attribute: VertexAttribute): uint64 =
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
28 uint64(sizeof(getAttributeType(attribute))) * uint64(attribute.data.len)
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
29
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
30 # from https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap15.html
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
31 func nLocationSlots[T: VertexAttributeType](): int =
35
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
32 when (T is Mat44[float64]):
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
33 8
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
34 elif (T is Mat44[float32]):
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
35 4
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
36 elif (T is Vec3[float64] or T is Vec3[uint64] or T is Vec4[float64] or T is Vec4[float64]):
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
37 2
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
38 else:
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
39 1
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
40
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
41 # numbers
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
42 func getVkFormat[T: VertexAttributeType](): VkFormat =
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
43 when T is uint8: VK_FORMAT_R8_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
44 elif T is int8: VK_FORMAT_R8_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
45 elif T is uint16: VK_FORMAT_R16_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
46 elif T is int16: VK_FORMAT_R16_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
47 elif T is uint32: VK_FORMAT_R32_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
48 elif T is int32: VK_FORMAT_R32_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
49 elif T is uint64: VK_FORMAT_R64_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
50 elif T is int64: VK_FORMAT_R64_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
51 elif T is float32: VK_FORMAT_R32_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
52 elif T is float64: VK_FORMAT_R64_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
53 elif T is Vec2[uint8]: VK_FORMAT_R8G8_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
54 elif T is Vec2[int8]: VK_FORMAT_R8G8_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
55 elif T is Vec2[uint16]: VK_FORMAT_R16G16_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
56 elif T is Vec2[int16]: VK_FORMAT_R16G16_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
57 elif T is Vec2[uint32]: VK_FORMAT_R32G32_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
58 elif T is Vec2[int32]: VK_FORMAT_R32G32_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
59 elif T is Vec2[uint64]: VK_FORMAT_R64G64_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
60 elif T is Vec2[int64]: VK_FORMAT_R64G64_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
61 elif T is Vec2[float32]: VK_FORMAT_R32G32_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
62 elif T is Vec2[float64]: VK_FORMAT_R64G64_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
63 elif T is Vec3[uint8]: VK_FORMAT_R8G8B8_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
64 elif T is Vec3[int8]: VK_FORMAT_R8G8B8_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
65 elif T is Vec3[uint16]: VK_FORMAT_R16G16B16_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
66 elif T is Vec3[int16]: VK_FORMAT_R16G16B16_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
67 elif T is Vec3[uint32]: VK_FORMAT_R32G32B32_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
68 elif T is Vec3[int32]: VK_FORMAT_R32G32B32_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
69 elif T is Vec3[uint64]: VK_FORMAT_R64G64B64_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
70 elif T is Vec3[int64]: VK_FORMAT_R64G64B64_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
71 elif T is Vec3[float32]: VK_FORMAT_R32G32B32_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
72 elif T is Vec3[float64]: VK_FORMAT_R64G64B64_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
73 elif T is Vec4[uint8]: VK_FORMAT_R8G8B8A8_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
74 elif T is Vec4[int8]: VK_FORMAT_R8G8B8A8_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
75 elif T is Vec4[uint16]: VK_FORMAT_R16G16B16A16_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
76 elif T is Vec4[int16]: VK_FORMAT_R16G16B16A16_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
77 elif T is Vec4[uint32]: VK_FORMAT_R32G32B32A32_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
78 elif T is Vec4[int32]: VK_FORMAT_R32G32B32A32_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
79 elif T is Vec4[uint64]: VK_FORMAT_R64G64B64A64_UINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
80 elif T is Vec4[int64]: VK_FORMAT_R64G64B64A64_SINT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
81 elif T is Vec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
82 elif T is Vec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
83
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
84
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
85
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
86 func VertexCount*[T](t: T): uint32 =
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
87 for name, value in t.fieldPairs:
35
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
88 when typeof(value) is VertexAttribute and not (typeof(value) is InstanceAttribute):
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
89 if result == 0:
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
90 result = uint32(value.data.len)
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
91 else:
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
92 assert result == uint32(value.data.len)
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 17
diff changeset
93
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
94 func generateGLSLVertexDeclarations*[T](): string =
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
95 var stmtList: seq[string]
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
96 var i = 0
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
97 for name, value in T().fieldPairs:
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
98 when typeof(value) is VertexAttribute:
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
99 let glsltype = getGLSLType[getAttributeType(value)]()
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
100 let n = name
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
101 stmtList.add(&"layout(location = {i}) in {glsltype} {n};")
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
102 i += nLocationSlots[getAttributeType(value)]()
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
103
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
104 return stmtList.join("\n")
13
a571db114152 add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff changeset
105
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
106 func generateInputVertexBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputBindingDescription] =
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
107 # packed attribute data, not interleaved (aks "struct of arrays")
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
108 var binding = bindingoffset
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
109 for name, value in T().fieldPairs:
35
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
110 when typeof(value) is InstanceAttribute:
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
111 result.add(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
112 VkVertexInputBindingDescription(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
113 binding: uint32(binding),
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
114 stride: uint32(sizeof(getAttributeType(value))),
35
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
115 inputRate: VK_VERTEX_INPUT_RATE_INSTANCE,
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
116 )
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
117 )
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
118 binding += 1
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
119 elif typeof(value) is VertexAttribute:
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
120 result.add(
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
121 VkVertexInputBindingDescription(
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
122 binding: uint32(binding),
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
123 stride: uint32(sizeof(getAttributeType(value))),
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
124 inputRate: VK_VERTEX_INPUT_RATE_VERTEX,
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
125 )
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
126 )
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
127 binding += 1
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
128
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
129 func generateInputAttributeBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputAttributeDescription] =
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
130 # packed attribute data, not interleaved (aks "struct of arrays")
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
131 var location = 0
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
132 var binding = bindingoffset
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
133 for name, value in T().fieldPairs:
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
134 when typeof(value) is VertexAttribute:
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
135 result.add(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
136 VkVertexInputAttributeDescription(
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
137 binding: uint32(binding),
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
138 location: uint32(location),
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
139 format: getVkFormat[getAttributeType(value)](),
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
140 offset: 0,
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
141 )
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
142 )
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 19
diff changeset
143 location += nLocationSlots[getAttributeType(value)]()
17
b40466fa446a add: vertex basics, some refactoring
Sam <sam@basx.dev>
parents: 13
diff changeset
144 binding += 1