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