Mercurial > games > semicongine
comparison 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 |
comparison
equal
deleted
inserted
replaced
34:a3d46f434616 | 35:7f99b21a8777 |
---|---|
16 data*: seq[T] | 16 data*: seq[T] |
17 PositionAttribute*[T:Vec] = object | 17 PositionAttribute*[T:Vec] = object |
18 data*: seq[T] | 18 data*: seq[T] |
19 ColorAttribute*[T:Vec] = object | 19 ColorAttribute*[T:Vec] = object |
20 data*: seq[T] | 20 data*: seq[T] |
21 VertexAttribute* = GenericAttribute|PositionAttribute|ColorAttribute | 21 InstanceAttribute*[T:Vec] = object |
22 data*: seq[T] | |
23 VertexAttribute* = GenericAttribute|PositionAttribute|ColorAttribute|InstanceAttribute | |
22 | 24 |
23 template getAttributeType*(v: VertexAttribute): auto = get(genericParams(typeof(v)), 0) | 25 template getAttributeType*(v: VertexAttribute): auto = get(genericParams(typeof(v)), 0) |
24 | 26 |
25 func datasize*(attribute: VertexAttribute): uint64 = | 27 func datasize*(attribute: VertexAttribute): uint64 = |
26 uint64(sizeof(getAttributeType(attribute))) * uint64(attribute.data.len) | 28 uint64(sizeof(getAttributeType(attribute))) * uint64(attribute.data.len) |
27 | 29 |
28 # from https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap15.html | 30 # from https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap15.html |
29 func nLocationSlots[T: VertexAttributeType](): int = | 31 func nLocationSlots[T: VertexAttributeType](): int = |
30 when (T is Vec3[float64] or T is Vec3[uint64] or T is Vec4[float64] or T is Vec4[float64]): | 32 when (T is Mat44[float64]): |
33 8 | |
34 elif (T is Mat44[float32]): | |
35 4 | |
36 elif (T is Vec3[float64] or T is Vec3[uint64] or T is Vec4[float64] or T is Vec4[float64]): | |
31 2 | 37 2 |
32 else: | 38 else: |
33 1 | 39 1 |
34 | 40 |
35 # numbers | 41 # numbers |
77 | 83 |
78 | 84 |
79 | 85 |
80 func VertexCount*[T](t: T): uint32 = | 86 func VertexCount*[T](t: T): uint32 = |
81 for name, value in t.fieldPairs: | 87 for name, value in t.fieldPairs: |
82 when typeof(value) is VertexAttribute: | 88 when typeof(value) is VertexAttribute and not (typeof(value) is InstanceAttribute): |
83 if result == 0: | 89 if result == 0: |
84 result = uint32(value.data.len) | 90 result = uint32(value.data.len) |
85 else: | 91 else: |
86 assert result == uint32(value.data.len) | 92 assert result == uint32(value.data.len) |
87 | |
88 func VertexAttributesCount*[T](): uint32 = | |
89 for name, value in T().fieldPairs: | |
90 when typeof(value) is VertexAttribute: | |
91 result += 1 | |
92 | 93 |
93 func generateGLSLVertexDeclarations*[T](): string = | 94 func generateGLSLVertexDeclarations*[T](): string = |
94 var stmtList: seq[string] | 95 var stmtList: seq[string] |
95 var i = 0 | 96 var i = 0 |
96 for name, value in T().fieldPairs: | 97 for name, value in T().fieldPairs: |
104 | 105 |
105 func generateInputVertexBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputBindingDescription] = | 106 func generateInputVertexBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputBindingDescription] = |
106 # packed attribute data, not interleaved (aks "struct of arrays") | 107 # packed attribute data, not interleaved (aks "struct of arrays") |
107 var binding = bindingoffset | 108 var binding = bindingoffset |
108 for name, value in T().fieldPairs: | 109 for name, value in T().fieldPairs: |
109 when typeof(value) is VertexAttribute: | 110 when typeof(value) is InstanceAttribute: |
110 result.add( | 111 result.add( |
111 VkVertexInputBindingDescription( | 112 VkVertexInputBindingDescription( |
112 binding: uint32(binding), | 113 binding: uint32(binding), |
113 stride: uint32(sizeof(getAttributeType(value))), | 114 stride: uint32(sizeof(getAttributeType(value))), |
114 inputRate: VK_VERTEX_INPUT_RATE_VERTEX, # VK_VERTEX_INPUT_RATE_INSTANCE for instances | 115 inputRate: VK_VERTEX_INPUT_RATE_INSTANCE, |
116 ) | |
117 ) | |
118 binding += 1 | |
119 elif typeof(value) is VertexAttribute: | |
120 result.add( | |
121 VkVertexInputBindingDescription( | |
122 binding: uint32(binding), | |
123 stride: uint32(sizeof(getAttributeType(value))), | |
124 inputRate: VK_VERTEX_INPUT_RATE_VERTEX, | |
115 ) | 125 ) |
116 ) | 126 ) |
117 binding += 1 | 127 binding += 1 |
118 | 128 |
119 func generateInputAttributeBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputAttributeDescription] = | 129 func generateInputAttributeBinding*[T](bindingoffset: int = 0, locationoffset: int = 0): seq[VkVertexInputAttributeDescription] = |