annotate static_utils.nim @ 1176:511c9f7cd1da compiletime-tests

sync to notebook in bedroom
author sam <sam@basx.dev>
date Sat, 29 Jun 2024 21:04:04 +0700
parents fafc2f14da0b
children 4ef959278451
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1 import std/os
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
2 import std/enumerate
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
3 import std/hashes
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
4 import std/macros
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
5 import std/strformat
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
6 import std/strutils
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
7 import std/sequtils
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
8 import std/typetraits as tt
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
9
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
10 import semicongine/core/utils
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
11 import semicongine/core/imagetypes
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
12 import semicongine/core/vector
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
13 import semicongine/core/matrix
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
14 import semicongine/core/vulkanapi
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
15 import semicongine/vulkan/buffer
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
16
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
17 template VertexAttribute* {.pragma.}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
18 template InstanceAttribute* {.pragma.}
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
19 template Pass* {.pragma.}
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
20 template PassFlat* {.pragma.}
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
21 template ShaderOutput* {.pragma.}
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
22
1165
58694b30b9cb add: some stuff
sam <sam@basx.dev>
parents: 1164
diff changeset
23 const INFLIGHTFRAMES = 2'u32
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
24
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
25 type
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
26 SupportedGPUType* = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64]
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
27 ShaderObject*[TShader] = object
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
28 vertexShader: VkShaderModule
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
29 fragmentShader: VkShaderModule
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
30
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
31 func VkType[T: SupportedGPUType](value: T): VkFormat =
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
32 when T is float32: VK_FORMAT_R32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
33 elif T is float64: VK_FORMAT_R64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
34 elif T is int8: VK_FORMAT_R8_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
35 elif T is int16: VK_FORMAT_R16_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
36 elif T is int32: VK_FORMAT_R32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
37 elif T is int64: VK_FORMAT_R64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
38 elif T is uint8: VK_FORMAT_R8_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
39 elif T is uint16: VK_FORMAT_R16_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
40 elif T is uint32: VK_FORMAT_R32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
41 elif T is uint64: VK_FORMAT_R64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
42 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
43 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
44 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
45 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
46 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
47 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
48 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
49 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
50 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
51 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
52 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
53 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
54 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
55 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
56 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
57 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
58 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
59 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
60 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
61 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
62 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
63 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
64 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
65 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
66 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
67 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
68 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
69 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
70 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
71 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
72 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
73 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
74 else: {.error: "Unsupported data type on GPU".}
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
75
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
76 func GlslType[T: SupportedGPUType|Texture](value: T): string =
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
77 when T is float32: "float"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
78 elif T is float64: "double"
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
79 elif T is int8 or T is int16 or T is int32 or T is int64: "int"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
80 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
81 elif T is TVec2[int32]: "ivec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
82 elif T is TVec2[int64]: "ivec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
83 elif T is TVec3[int32]: "ivec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
84 elif T is TVec3[int64]: "ivec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
85 elif T is TVec4[int32]: "ivec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
86 elif T is TVec4[int64]: "ivec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
87 elif T is TVec2[uint32]: "uvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
88 elif T is TVec2[uint64]: "uvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
89 elif T is TVec3[uint32]: "uvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
90 elif T is TVec3[uint64]: "uvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
91 elif T is TVec4[uint32]: "uvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
92 elif T is TVec4[uint64]: "uvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
93 elif T is TVec2[float32]: "vec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
94 elif T is TVec2[float64]: "dvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
95 elif T is TVec3[float32]: "vec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
96 elif T is TVec3[float64]: "dvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
97 elif T is TVec4[float32]: "vec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
98 elif T is TVec4[float64]: "dvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
99 elif T is TMat2[float32]: "mat2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
100 elif T is TMat2[float64]: "dmat2"
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
101 elif T is TMat23[float32]: "mat23"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
102 elif T is TMat23[float64]: "dmat23"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
103 elif T is TMat32[float32]: "mat32"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
104 elif T is TMat32[float64]: "dmat32"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
105 elif T is TMat3[float32]: "mat3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
106 elif T is TMat3[float64]: "dmat3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
107 elif T is TMat34[float32]: "mat34"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
108 elif T is TMat34[float64]: "dmat34"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
109 elif T is TMat43[float32]: "mat43"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
110 elif T is TMat43[float64]: "dmat43"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
111 elif T is TMat4[float32]: "mat4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
112 elif T is TMat4[float64]: "dmat4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
113 elif T is Texture: "sampler2D"
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
114 else: {.error: "Unsupported data type on GPU".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
115
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
116 template ForVertexDataFields*(inputData: typed, fieldname, valuename, isinstancename, body: untyped): untyped =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
117 for theFieldname, value in fieldPairs(inputData):
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
118 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
119 when not typeof(value) is seq:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
120 {.error: "field '" & theFieldname & "' needs to be a seq".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
121 when not typeof(value) is SupportedGPUType:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
122 {.error: "field '" & theFieldname & "' is not a supported GPU type".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
123 block:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
124 let `fieldname` {.inject.} = theFieldname
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
125 let `valuename` {.inject.} = value
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
126 let `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
127 body
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
128
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
129 template ForDescriptorFields*(inputData: typed, fieldname, typename, countname, bindingNumber, body: untyped): untyped =
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
130 var `bindingNumber` {.inject.} = 1'u32
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
131 for theFieldname, value in fieldPairs(inputData):
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
132 let `fieldname` {.inject.} = theFieldname
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
133 when typeof(value) is Texture:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
134 block:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
135 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
136 let `countname` {.inject.} = 1'u32
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
137 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
138 `bindingNumber`.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
139 elif typeof(value) is object:
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
140 block:
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
141 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
142 let `countname` {.inject.} = 1'u32
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
143 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
144 `bindingNumber`.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
145 elif typeof(value) is array:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
146 when elementType(value) is Texture:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
147 block:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
148 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
149 let `countname` {.inject.} = uint32(typeof(value).len)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
150 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
151 `bindingNumber`.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
152 elif elementType(value) is object:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
153 block:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
154 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
155 let `countname` {.inject.} = uint32(typeof(value).len)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
156 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
157 `bindingNumber`.inc
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
158
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
159 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
160 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
161 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
162 elif T is TMat32[float32] or T is TMat32[float64] or T is TMat3[float32] or T is TMat3[float64] or T is TMat34[float32] or T is TMat34[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
163 3
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
164 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
165 4
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
166 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
167 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
168
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
169 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
170 #[
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
171 single location:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
172 - any scalar
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
173 - any 16-bit vector
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
174 - any 32-bit vector
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
175 - any 64-bit vector that has max. 2 components
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
176 16-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
177 32-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
178 64-bit scalar and 2-component vector types.
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
179 two locations
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
180 64-bit three- and four-component vectors
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
181 ]#
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
182 when T is TVec3[int64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
183 T is TVec4[int64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
184 T is TVec3[uint64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
185 T is TVec4[uint64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
186 T is TVec3[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
187 T is TVec4[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
188 T is TMat23[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
189 T is TMat3[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
190 T is TMat34[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
191 T is TMat43[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
192 T is TMat4[float64]:
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
193 return 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
194 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
195 return 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
196
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
197 type
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
198 IndexType = enum
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
199 None, UInt8, UInt16, UInt32
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
200
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
201 IndirectGPUMemory = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
202 vk: VkDeviceMemory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
203 size: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
204 DirectGPUMemory = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
205 vk: VkDeviceMemory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
206 size: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
207 data: pointer
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
208 GPUMemory = IndirectGPUMemory | DirectGPUMemory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
209
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
210 Buffer[TMemory: GPUMemory] = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
211 vk: VkBuffer
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
212 memory*: TMemory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
213 offset: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
214 size: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
215
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
216 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
217 data: seq[T]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
218 buffer: Buffer[TMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
219 offset: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
220 GPUValue[T: object|array, TMemory: GPUMemory] = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
221 data: T
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
222 buffer: Buffer[TMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
223 offset: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
224
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
225 Renderable[TMesh, TInstance] = object
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
226 vertexBuffers: seq[VkBuffer]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
227 bufferOffsets: seq[VkDeviceSize]
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
228 instanceCount: uint32
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
229 case indexType: IndexType
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
230 of None:
1160
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
231 vertexCount: uint32
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
232 else:
1160
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
233 indexBuffer: VkBuffer
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
234 indexCount: uint32
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
235 indexBufferOffset: VkDeviceSize
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
236
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
237 Pipeline[TShader] = object
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
238 pipeline: VkPipeline
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
239 layout: VkPipelineLayout
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
240 descriptorSetLayout: VkDescriptorSetLayout
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
241 RenderData = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
242 descriptorPool: VkDescriptorPool
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
243 indirectMemory: seq[IndirectGPUMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
244 nextFreeIndirectMemoryOffset: seq[uint64]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
245 indirectBuffers: seq[Buffer[IndirectGPUMemory]]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
246 directMemory: seq[DirectGPUMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
247 nextFreeDirectMemoryOffset: seq[uint64]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
248 directBuffers: seq[Buffer[DirectGPUMemory]]
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
249
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
250 converter toVkIndexType(indexType: IndexType): VkIndexType =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
251 case indexType:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
252 of None: VK_INDEX_TYPE_NONE_KHR
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
253 of UInt8: VK_INDEX_TYPE_UINT8_EXT
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
254 of UInt16: VK_INDEX_TYPE_UINT16
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
255 of UInt32: VK_INDEX_TYPE_UINT32
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
256
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
257 proc CreateRenderPass*(
sam <sam@basx.dev>
parents: 1165
diff changeset
258 device: VkDevice,
sam <sam@basx.dev>
parents: 1165
diff changeset
259 format: VkFormat,
sam <sam@basx.dev>
parents: 1165
diff changeset
260 ): VkRenderPass =
sam <sam@basx.dev>
parents: 1165
diff changeset
261
sam <sam@basx.dev>
parents: 1165
diff changeset
262 var
sam <sam@basx.dev>
parents: 1165
diff changeset
263 attachments = @[VkAttachmentDescription(
sam <sam@basx.dev>
parents: 1165
diff changeset
264 format: format,
sam <sam@basx.dev>
parents: 1165
diff changeset
265 samples: VK_SAMPLE_COUNT_1_BIT,
sam <sam@basx.dev>
parents: 1165
diff changeset
266 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
sam <sam@basx.dev>
parents: 1165
diff changeset
267 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
sam <sam@basx.dev>
parents: 1165
diff changeset
268 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
sam <sam@basx.dev>
parents: 1165
diff changeset
269 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
sam <sam@basx.dev>
parents: 1165
diff changeset
270 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
sam <sam@basx.dev>
parents: 1165
diff changeset
271 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
sam <sam@basx.dev>
parents: 1165
diff changeset
272 )]
sam <sam@basx.dev>
parents: 1165
diff changeset
273 dependencies = @[VkSubpassDependency(
sam <sam@basx.dev>
parents: 1165
diff changeset
274 srcSubpass: VK_SUBPASS_EXTERNAL,
sam <sam@basx.dev>
parents: 1165
diff changeset
275 dstSubpass: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
276 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
277 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
278 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
279 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
280 )]
sam <sam@basx.dev>
parents: 1165
diff changeset
281 outputs = @[
sam <sam@basx.dev>
parents: 1165
diff changeset
282 VkAttachmentReference(
sam <sam@basx.dev>
parents: 1165
diff changeset
283 attachment: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
284 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
sam <sam@basx.dev>
parents: 1165
diff changeset
285 )
sam <sam@basx.dev>
parents: 1165
diff changeset
286 ]
sam <sam@basx.dev>
parents: 1165
diff changeset
287
sam <sam@basx.dev>
parents: 1165
diff changeset
288 var subpassesList = [
sam <sam@basx.dev>
parents: 1165
diff changeset
289 VkSubpassDescription(
sam <sam@basx.dev>
parents: 1165
diff changeset
290 flags: VkSubpassDescriptionFlags(0),
sam <sam@basx.dev>
parents: 1165
diff changeset
291 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS,
sam <sam@basx.dev>
parents: 1165
diff changeset
292 inputAttachmentCount: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
293 pInputAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
294 colorAttachmentCount: uint32(outputs.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
295 pColorAttachments: outputs.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
296 pResolveAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
297 pDepthStencilAttachment: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
298 preserveAttachmentCount: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
299 pPreserveAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
300 )
sam <sam@basx.dev>
parents: 1165
diff changeset
301 ]
sam <sam@basx.dev>
parents: 1165
diff changeset
302
sam <sam@basx.dev>
parents: 1165
diff changeset
303 var createInfo = VkRenderPassCreateInfo(
sam <sam@basx.dev>
parents: 1165
diff changeset
304 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
sam <sam@basx.dev>
parents: 1165
diff changeset
305 attachmentCount: uint32(attachments.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
306 pAttachments: attachments.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
307 subpassCount: uint32(subpassesList.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
308 pSubpasses: subpassesList.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
309 dependencyCount: uint32(dependencies.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
310 pDependencies: dependencies.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
311 )
sam <sam@basx.dev>
parents: 1165
diff changeset
312 checkVkResult device.vkCreateRenderPass(addr(createInfo), nil, addr(result))
sam <sam@basx.dev>
parents: 1165
diff changeset
313
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
314 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
315 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
316 case stage
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
317 of VK_SHADER_STAGE_VERTEX_BIT: "vert"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
318 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
319 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
320 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
321 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
322 of VK_SHADER_STAGE_COMPUTE_BIT: "comp"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
323 else: ""
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
324
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
325 when defined(nimcheck): # will not run if nimcheck is running
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
326 return result
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
327
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
328 let
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
329 stagename = stage2string(stage)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
330 shaderHash = hash(shaderSource)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
331 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
332
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
333 if not shaderfile.fileExists:
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
334 echo "shader of type ", stage
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
335 for i, line in enumerate(shaderSource.splitlines()):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
336 echo " ", i + 1, " ", line
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
337 # var glslExe = currentSourcePath.parentDir.parentDir.parentDir / "tools" / "glslangValidator"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
338 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
339 when defined(windows):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
340 glslExe = glslExe & "." & ExeExt
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
341 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
342 echo "run: ", command
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
343 discard StaticExecChecked(
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
344 command = command,
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
345 input = shaderSource
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
346 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
347 else:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
348 echo &"shaderfile {shaderfile} is up-to-date"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
349
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
350 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
351 let shaderbinary = staticRead shaderfile.replace("\\", "/")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
352 else:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
353 let shaderbinary = staticRead shaderfile
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
354
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
355 var i = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
356 while i < shaderbinary.len:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
357 result.add(
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
358 (uint32(shaderbinary[i + 0]) shl 0) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
359 (uint32(shaderbinary[i + 1]) shl 8) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
360 (uint32(shaderbinary[i + 2]) shl 16) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
361 (uint32(shaderbinary[i + 3]) shl 24)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
362 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
363 i += 4
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
364
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
365 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
366 const GLSL_VERSION = "450"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
367 var vsInput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
368 var vsOutput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
369 var fsInput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
370 var fsOutput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
371 var uniforms: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
372 var samplers: seq[string]
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
373 var vsInputLocation = 0'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
374 var passLocation = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
375 var fsOutputLocation = 0
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
376 var descriptorBinding = 0
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
377
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
378 for fieldname, value in fieldPairs(shader):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
379 # vertex shader inputs
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
380 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute):
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
381 assert typeof(value) is SupportedGPUType
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
382 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
383 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
384 vsInputLocation += NLocationSlots(value)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
385 # intermediate values, passed between shaders
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
386 elif hasCustomPragma(value, Pass) or hasCustomPragma(value, PassFlat):
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
387 let flat = if hasCustomPragma(value, PassFlat): "flat " else: ""
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
388 vsOutput.add "layout(location = " & $passLocation & ") " & flat & "out " & GlslType(value) & " " & fieldname & ";"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
389 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
390 passLocation.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
391 elif hasCustomPragma(value, ShaderOutput):
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
392 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
393 fsOutputLocation.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
394 elif typeof(value) is Texture:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
395 samplers.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(value) & " " & fieldname & ";"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
396 descriptorBinding.inc
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
397 elif typeof(value) is object:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
398 # TODO
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
399 uniforms.add ""
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
400 descriptorBinding.inc
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
401 elif typeof(value) is array:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
402 when elementType(value) is Texture:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
403 let arrayDecl = "[" & $typeof(value).len & "]"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
404 samplers.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(value))) & " " & fieldname & "" & arrayDecl & ";"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
405 descriptorBinding.inc
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
406 elif elementType(value) is object:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
407 # TODO
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
408 let arrayDecl = "[" & $typeof(value).len & "]"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
409 # uniforms.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(elementType(value)) & " " & fieldname & "" & arrayDecl & ";"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
410 descriptorBinding.inc
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
411 else:
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
412 {.error: "Unsupported shader field " & fieldname.}
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
413 elif fieldname in ["vertexCode", "fragmentCode"]:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
414 discard
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
415 else:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
416 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).}
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
417
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
418 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
419 vsInput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
420 uniforms &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
421 samplers &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
422 vsOutput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
423 @[shader.vertexCode]).join("\n")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
424
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
425 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
426 fsInput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
427 uniforms &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
428 samplers &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
429 fsOutput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
430 @[shader.fragmentCode]).join("\n")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
431
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
432 proc CompileShader[TShader](device: VkDevice, shader: static TShader): ShaderObject[TShader] =
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
433 const (vertexShaderSource, fragmentShaderSource) = generateShaderSource(shader)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
434
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
435 let vertexBinary = compileGlslToSPIRV(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderSource)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
436 let fragmentBinary = compileGlslToSPIRV(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderSource)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
437
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
438 var createInfoVertex = VkShaderModuleCreateInfo(
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
439 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
440 codeSize: csize_t(vertexBinary.len * sizeof(uint32)),
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
441 pCode: vertexBinary.ToCPointer,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
442 )
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
443 checkVkResult device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader))
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
444 var createInfoFragment = VkShaderModuleCreateInfo(
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
445 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
446 codeSize: csize_t(fragmentBinary.len * sizeof(uint32)),
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
447 pCode: fragmentBinary.ToCPointer,
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
448 )
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
449 checkVkResult device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader))
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
450
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
451
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
452 proc CreatePipeline*[TShader](
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
453 device: VkDevice,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
454 renderPass: VkRenderPass,
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
455 shader: ShaderObject[TShader],
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
456 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
457 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
458 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
459 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
460 descriptorPoolLimit = 1024
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
461 ): Pipeline[TShader] =
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
462 # create pipeline
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
463 var layoutbindings: seq[VkDescriptorSetLayoutBinding]
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
464 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber):
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
465 layoutbindings.add VkDescriptorSetLayoutBinding(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
466 binding: descriptorBindingNumber,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
467 descriptorType: descriptorType,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
468 descriptorCount: descriptorCount,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
469 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS),
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
470 pImmutableSamplers: nil,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
471 )
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
472 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
473 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
474 bindingCount: uint32(layoutbindings.len),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
475 pBindings: layoutbindings.ToCPointer
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
476 )
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
477 result.descriptorSetLayout: VkDescriptorSetLayout
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
478 checkVkResult vkCreateDescriptorSetLayout(device, addr(layoutCreateInfo), nil, addr(result.descriptorSetLayout))
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
479 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
480 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
481 setLayoutCount: 1,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
482 pSetLayouts: addr(result.descriptorSetLayout),
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
483 # pushConstantRangeCount: uint32(pushConstants.len),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
484 # pPushConstantRanges: pushConstants.ToCPointer,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
485 )
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
486 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, addr(result.layout))
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
487
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
488 let stages = [
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
489 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
490 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
491 stage: VK_SHADER_STAGE_VERTEX_BIT,
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
492 module: shader.vertexShader,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
493 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
494 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
495 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
496 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
497 stage: VK_SHADER_STAGE_FRAGMENT_BIT,
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
498 module: shader.fragmentShader,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
499 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
500 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
501 ]
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
502 var
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
503 bindings: seq[VkVertexInputBindingDescription]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
504 attributes: seq[VkVertexInputAttributeDescription]
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
505 var inputBindingNumber = 0'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
506 var location = 0'u32
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
507 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
508 bindings.add VkVertexInputBindingDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
509 binding: inputBindingNumber,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
510 stride: sizeof(value).uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
511 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
512 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
513 # allows to submit larger data structures like Mat44, for most other types will be 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
514 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
515 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value):
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
516 attributes.add VkVertexInputAttributeDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
517 binding: inputBindingNumber,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
518 location: location,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
519 format: VkType(value),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
520 offset: i * perDescriptorSize,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
521 )
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
522 location += NLocationSlots(value)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
523 inc inputBindingNumber
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
524
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
525 let
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
526 vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
527 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
528 vertexBindingDescriptionCount: uint32(bindings.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
529 pVertexBindingDescriptions: bindings.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
530 vertexAttributeDescriptionCount: uint32(attributes.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
531 pVertexAttributeDescriptions: attributes.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
532 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
533 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
534 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
535 topology: topology,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
536 primitiveRestartEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
537 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
538 viewportState = VkPipelineViewportStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
539 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
540 viewportCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
541 scissorCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
542 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
543 rasterizer = VkPipelineRasterizationStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
544 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
545 depthClampEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
546 rasterizerDiscardEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
547 polygonMode: polygonMode,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
548 lineWidth: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
549 cullMode: toBits [cullMode],
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
550 frontFace: frontFace,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
551 depthBiasEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
552 depthBiasConstantFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
553 depthBiasClamp: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
554 depthBiasSlopeFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
555 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
556 multisampling = VkPipelineMultisampleStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
557 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
558 sampleShadingEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
559 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
560 minSampleShading: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
561 pSampleMask: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
562 alphaToCoverageEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
563 alphaToOneEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
564 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
565 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
566 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT],
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
567 blendEnable: VK_TRUE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
568 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
569 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
570 colorBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
571 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
572 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
573 alphaBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
574 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
575 colorBlending = VkPipelineColorBlendStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
576 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
577 logicOpEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
578 attachmentCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
579 pAttachments: addr(colorBlendAttachment),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
580 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
581 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
582 dynamicState = VkPipelineDynamicStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
583 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
584 dynamicStateCount: dynamicStates.len.uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
585 pDynamicStates: dynamicStates.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
586 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
587 let createInfo = VkGraphicsPipelineCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
588 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
589 stageCount: 2,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
590 pStages: stages.ToCPointer,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
591 pVertexInputState: addr(vertexInputInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
592 pInputAssemblyState: addr(inputAssembly),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
593 pViewportState: addr(viewportState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
594 pRasterizationState: addr(rasterizer),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
595 pMultisampleState: addr(multisampling),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
596 pDepthStencilState: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
597 pColorBlendState: addr(colorBlending),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
598 pDynamicState: addr(dynamicState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
599 layout: result.layout,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
600 renderPass: renderPass,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
601 subpass: 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
602 basePipelineHandle: VkPipeline(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
603 basePipelineIndex: -1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
604 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
605 checkVkResult vkCreateGraphicsPipelines(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
606 device,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
607 VkPipelineCache(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
608 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
609 addr(createInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
610 nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
611 addr(result.pipeline)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
612 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
613
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
614 proc AllocateIndirectMemory(device: VkDevice, pDevice: VkPhysicalDevice, allocationSize: uint64): IndirectGPUMemory =
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
615 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
616 result.size = allocationSize
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
617
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
618 # find a good memory type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
619 var physicalProperties: VkPhysicalDeviceMemoryProperties
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
620 vkGetPhysicalDeviceMemoryProperties(pDevice, addr physicalProperties)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
621
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
622 var biggestHeap: uint64 = -1
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
623 var memoryTypeIndex = -1
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
624 # try to find non-host-visible type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
625 for i in 0 ..< physicalProperties.memoryTypeCount:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
626 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT not in toEnums(physicalProperties.memoryTypes[i].propertyFlags)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
627 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
628 if size > biggestHeap:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
629 biggest = size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
630 memoryTypeIndex = i
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
631
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
632 # If we did not found a device-only memory type, let's just take the biggest overall
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
633 if memoryTypeIndex < 0:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
634 for i in 0 ..< physicalProperties.memoryTypeCount:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
635 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
636 if size > biggestHeap:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
637 biggest = size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
638 memoryTypeIndex = i
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
639
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
640 assert memoryTypeIndex >= 0, "Unable to find indirect memory type"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
641 var allocationInfo = VkMemoryAllocateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
642 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
643 allocationSize: initialAllocationSize,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
644 memoryTypeIndex: memoryTypeIndex,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
645 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
646 checkVkResult vkAllocateMemory(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
647 device,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
648 addr allocationInfo,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
649 nil,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
650 addr result.vk
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
651 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
652
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
653 proc AllocateDirectMemory(device: VkDevice, allocationSize: uint64): DirectGPUMemory =
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
654 result.size = allocationSize
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
655
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
656 # find a good memory type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
657 var physicalProperties: VkPhysicalDeviceMemoryProperties
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
658 vkGetPhysicalDeviceMemoryProperties(pDevice, addr physicalProperties)
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
659
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
660 var biggestHeap: uint64 = -1
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
661 var memoryTypeIndex = -1
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
662 # try to find host-visible type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
663 for i in 0 ..< physicalProperties.memoryTypeCount:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
664 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
665 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
666 if size > biggestHeap:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
667 biggest = size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
668 memoryTypeIndex = i
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
669
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
670 assert memoryTypeIndex >= 0, "Unable to find direct (aka host-visible) memory type"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
671 var allocationInfo = VkMemoryAllocateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
672 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
673 allocationSize: initialAllocationSize,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
674 memoryTypeIndex: memoryTypeIndex,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
675 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
676 checkVkResult vkAllocateMemory(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
677 device,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
678 addr allocationInfo,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
679 nil,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
680 addr result.vk
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
681 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
682 checkVkResult result.device.vk.vkMapMemory(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
683 memory = result.vk,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
684 offset = 0'u64,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
685 size = result.size,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
686 flags = VkMemoryMapFlags(0),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
687 ppData = addr(result.data)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
688 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
689
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
690 proc InitRenderData(device: VkDevice, descriptorPoolLimit = 1024): RenderData =
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
691 # allocate descriptor pools
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
692 var poolSizes = [
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
693 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
694 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
695 ]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
696 var poolInfo = VkDescriptorPoolCreateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
697 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
698 poolSizeCount: poolSizes.len.uint32,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
699 pPoolSizes: poolSizes.ToCPointer,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
700 maxSets: descriptorPoolLimit,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
701 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
702 checkVkResult vkCreateDescriptorPool(device, addr(poolInfo), nil, addr(result.descriptorPool))
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
703
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
704 # allocate some memory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
705 var initialAllocationSize: 1_000_000_000 # TODO: make this more dynamic or something
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
706 result.indirectMemory = @[AllocateIndirectMemory(device, size=initialAllocationSize)]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
707 result.nextFreeIndirectMemoryOffset = @[0'u64]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
708 result.directMemory = @[AllocateDirectMemory(device, size=initialAllocationSize)]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
709 result.nextFreeDirectMemoryOffset = @[0'u64]
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
710
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
711 proc WriteDescriptors[TShader](device: VkDevice, pipeline: Pipeline[TShader]) =
1165
58694b30b9cb add: some stuff
sam <sam@basx.dev>
parents: 1164
diff changeset
712 var descriptorSetWrites: seq[VkWriteDescriptorSet]
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
713 # map (buffer + offset + range) to descriptor
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
714 # map (texture) to descriptor
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
715 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber):
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
716 for frameInFlight in 0 ..< pipeline.descriptorSets.len:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
717 if descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
718 # TODO
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
719 let bufferInfo = VkDescriptorBufferInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
720 buffer: VkBuffer(0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
721 offset: 0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
722 range: 1,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
723 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
724 descriptorSetWrites.add VkWriteDescriptorSet(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
725 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
726 dstSet: pipeline.descriptorSets[frameInFlight],
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
727 dstBinding: descriptorBindingNumber,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
728 dstArrayElement: uint32(0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
729 descriptorType: descriptorType,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
730 descriptorCount: descriptorCount,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
731 pImageInfo: nil,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
732 pBufferInfo: addr(bufferInfo),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
733 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
734 elif descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
735 # TODO
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
736 let imageInfo = VkDescriptorImageInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
737 sampler: VkSampler(0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
738 imageView: VkImageView(0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
739 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
740 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
741 descriptorSetWrites.add VkWriteDescriptorSet(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
742 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
743 dstSet: pipeline.descriptorSets[frameInFlight],
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
744 dstBinding: descriptorBindingNumber,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
745 dstArrayElement: uint32(0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
746 descriptorType: descriptorType,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
747 descriptorCount: descriptorCount,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
748 pImageInfo: addr(imageInfo),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
749 pBufferInfo: nil,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
750 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
751 vkUpdateDescriptorSets(device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil)
1165
58694b30b9cb add: some stuff
sam <sam@basx.dev>
parents: 1164
diff changeset
752
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
753 proc CreateRenderable[TMesh, TInstance](
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
754 mesh: TMesh,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
755 instance: TInstance,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
756 ): Renderable[TMesh, TInstance] =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
757 result.indexType = None
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
758
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
759 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) =
sam <sam@basx.dev>
parents: 1165
diff changeset
760 let a = pipeline.descriptorSets
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
761 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline)
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
762 if a[currentFrameInFlight] != VkDescriptorSet(0):
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
763 commandBuffer.vkCmdBindDescriptorSets(
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
764 VK_PIPELINE_BIND_POINT_GRAPHICS,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
765 pipeline.layout,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
766 0,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
767 1,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
768 addr pipeline.descriptorSets[currentFrameInFlight],
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
769 0,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
770 nil,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
771 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
772
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
773 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals: typedesc) =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
774 # assert seq-fields of TMesh|TInstance == seq-fields of TShader
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
775 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
776 for inputName, inputValue in default(TShader).fieldPairs:
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
777 var foundField = false
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
778
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
779 # Vertex input data
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
780 when hasCustomPragma(inputValue, VertexAttribute):
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
781 assert typeof(inputValue) is SupportedGPUType
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
782 for meshName, meshValue in default(TMesh).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
783 when meshName == inputName:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
784 assert meshValue is GPUArray, "Mesh attribute '" & meshName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(meshValue))
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
785 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
786 assert elementType(meshValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue.data)) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
787 foundField = true
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
788 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
789
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
790 # Instance input data
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
791 elif hasCustomPragma(inputValue, InstanceAttribute):
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
792 assert typeof(inputValue) is SupportedGPUType
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
793 for instanceName, instanceValue in default(TInstance).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
794 when instanceName == inputName:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
795 assert instanceValue is GPUArray, "Instance attribute '" & instanceName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(instanceName))
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
796 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
797 assert elementType(instanceValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but instance attribute is of type '" & tt.name(elementType(instanceValue.data)) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
798 foundField = true
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
799 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TInstance) & "'"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
800
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
801 # Texture
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
802 elif typeof(inputValue) is Texture:
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
803 for meshName, meshValue in default(TMesh).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
804 when meshName == inputName:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
805 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
806 assert typeof(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
807 foundField = true
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
808 for globalName, globalValue in default(TGlobals).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
809 when globalName == inputName:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
810 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
811 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
812 foundField = true
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
813 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
814
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
815 # Uniform block
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
816 elif typeof(inputValue) is object:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
817 for meshName, meshValue in default(TMesh).fieldPairs:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
818 when meshName == inputName:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
819 assert meshValue is GPUValue, "Mesh attribute '" & meshName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(meshValue))
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
820 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
821 assert typeof(meshValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue.data)) & "'"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
822 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
823 for globalName, globalValue in default(TGlobals).fieldPairs:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
824 when globalName == inputName:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
825 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue))
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
826 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
827 assert typeof(globalValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue.data)) & "'"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
828 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
829 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
830
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
831 # array
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
832 elif typeof(inputValue) is array:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
833
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
834 # texture-array
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
835 when elementType(inputValue) is Texture:
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
836 for meshName, meshValue in default(TMesh).fieldPairs:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
837 when meshName == inputName:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
838 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
839 assert typeof(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
840 foundField = true
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
841 for globalName, globalValue in default(TGlobals).fieldPairs:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
842 when globalName == inputName:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
843 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
844 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
845 foundField = true
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
846 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
847
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
848 # uniform-block array
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
849 elif elementType(inputValue) is object:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
850 for meshName, meshValue in default(TMesh).fieldPairs:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
851 when meshName == inputName:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
852 assert meshValue is GPUValue, "Mesh attribute '" & meshName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(meshValue))
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
853 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
854 assert typeof(meshValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue.data)) & "'"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
855 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
856 for globalName, globalValue in default(TGlobals).fieldPairs:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
857 when globalName == inputName:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
858 assert globalValue is GPUValue, "global attribute '" & globalName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(globalValue))
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
859 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
860 assert typeof(globalValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue.data)) & "'"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
861 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
862 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'"
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
863
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
864
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
865 proc Render[TShader, TMesh, TInstance, TGlobals](
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
866 pipeline: Pipeline[TShader],
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
867 renderable: Renderable[TMesh, TInstance],
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
868 globals: TGlobals,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
869 commandBuffer: VkCommandBuffer,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
870 ) =
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
871 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals)
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
872 if renderable.vertexBuffers.len > 0:
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
873 commandBuffer.vkCmdBindVertexBuffers(
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
874 firstBinding = 0'u32,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
875 bindingCount = uint32(renderable.vertexBuffers.len),
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
876 pBuffers = renderable.vertexBuffers.ToCPointer(),
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
877 pOffsets = renderable.bufferOffsets.ToCPointer()
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
878 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
879 if renderable.indexType != None:
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
880 commandBuffer.vkCmdBindIndexBuffer(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
881 renderable.indexBuffer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
882 renderable.indexBufferOffset,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
883 renderable.indexType,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
884 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
885 commandBuffer.vkCmdDrawIndexed(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
886 indexCount = renderable.indexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
887 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
888 firstIndex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
889 vertexOffset = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
890 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
891 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
892 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
893 commandBuffer.vkCmdDraw(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
894 vertexCount = renderable.vertexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
895 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
896 firstVertex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
897 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
898 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
899
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
900 when isMainModule:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
901 import semicongine/platform/window
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
902 import semicongine/vulkan/instance
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
903 import semicongine/vulkan/device
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
904 import semicongine/vulkan/physicaldevice
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
905 import std/options
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
906
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
907 type
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
908 MaterialA = object
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
909 reflection: float32
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
910 baseColor: Vec3f
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
911 ShaderSettings = object
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
912 brightness: float32
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
913 MeshA = object
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
914 position: GPUArray[Vec3f, IndirectGPUMemory]
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
915 InstanceA = object
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
916 rotation: GPUArray[Vec4f, IndirectGPUMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
917 objPosition: GPUArray[Vec3f, IndirectGPUMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
918 UniformsA = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
919 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
920 materialTextures: array[3, Texture]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
921 GlobalsA = object
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
922 fontAtlas: Texture
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
923 settings: GPUValue[ShaderSettings, IndirectGPUMemory]
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
924
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
925 ShaderA = object
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
926 # vertex input
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
927 position {.VertexAttribute.}: Vec3f
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
928 objPosition {.InstanceAttribute.}: Vec3f
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
929 rotation {.InstanceAttribute.}: Vec4f
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
930 # intermediate
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
931 test {.Pass.}: float32
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
932 test1 {.PassFlat.}: Vec3f
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
933 # output
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
934 color {.ShaderOutput.}: Vec4f
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
935 # uniforms
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
936 materials: array[3, MaterialA]
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
937 settings: ShaderSettings
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
938 # textures
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
939 fontAtlas: Texture
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
940 materialTextures: array[3, Texture]
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
941 # code
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
942 vertexCode: string = "void main() {}"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
943 fragmentCode: string = "void main() {}"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
944
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
945 let w = CreateWindow("test2")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
946 putEnv("VK_LAYER_ENABLES", "VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_AMD,VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_NVIDIA,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXTVK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
947 let i = w.CreateInstance(
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
948 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0),
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
949 instanceExtensions = @[],
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
950 layers = @["VK_LAYER_KHRONOS_validation"],
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
951 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
952
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
953 let selectedPhysicalDevice = i.GetPhysicalDevices().FilterBestGraphics()
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
954 let dev = i.CreateDevice(
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
955 selectedPhysicalDevice,
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
956 enabledExtensions = @[],
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
957 selectedPhysicalDevice.FilterForGraphicsPresentationQueues()
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
958 )
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
959 let frameWidth = 100'u32
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
960 let frameHeight = 100'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
961
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
962 var myMesh1 = MeshA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
963 position: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
964 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
965 var uniforms1 = UniformsA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
966 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
967 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
968 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
969 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
970 ]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
971 materialTextures: [
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
972 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[255'u8, 0'u8, 0'u8, 255'u8]])),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
973 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 255'u8, 0'u8, 255'u8]])),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
974 Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 1, height: 1, imagedata: @[[0'u8, 0'u8, 255'u8, 255'u8]])),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
975 ]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
976 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
977 var instances1 = InstanceA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
978 rotation: GPUArray[Vec4f, IndirectGPUMemory](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
979 objPosition: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
980 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
981 var myGlobals: GlobalsA
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
982
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
983 # setup for rendering (TODO: swapchain & framebuffers)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
984
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
985 # renderpass
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
986 let renderpass = dev.vk.CreateRenderPass(dev.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
987
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
988 # shaders
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
989 const shader = ShaderA()
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
990 let shaderObject = dev.vk.CompileShader(shader)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
991 var pipeline1 = CreatePipeline(dev.vk, renderPass = renderpass, shaderObject)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
992
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
993 var renderdata = InitRenderData(dev.vk)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
994
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
995 # create descriptor sets
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
996 var descriptorSets: array[INFLIGHTFRAMES.int, VkDescriptorSet]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
997 var layouts = newSeqWith(descriptorSets.len, pipeline.descriptorSetLayout)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
998 var allocInfo = VkDescriptorSetAllocateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
999 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1000 descriptorPool: pool,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1001 descriptorSetCount: uint32(layouts.len),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1002 pSetLayouts: layouts.ToCPointer,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1003 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1004 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), descriptorSets.ToCPointer)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1005
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1006
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1007 #[
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1008 # TODO: probably here: allocate renderables, uniform buffers & textures
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1009 let meshBuffers: seq[(bool, uint64)] = GetBufferSizes[MeshA](item = myMesh1)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1010 let instanceBuffers: seq[(bool, uint64)] = GetBufferSizes[InstanceA](item = instances1)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1011 let globalBuffers: seq[(bool, uint64)] = GetBufferSizes[Globals](item = myGlobals)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1012 var myRenderable = CreateRenderable()
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1013 UploadTextures[MeshA]()
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1014 UploadTextures[Globals]()
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1015 ]#
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1016 var myRenderable: Renderable[MeshA, InstanceA]
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1017
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1018 # descriptors
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1019 WriteDescriptors(dev.vk, pipeline1)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1020
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1021 # command buffer
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1022 var
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1023 commandBufferPool: VkCommandPool
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1024 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer]
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1025 createInfo = VkCommandPoolCreateInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1026 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1027 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1028 queueFamilyIndex: dev.FirstGraphicsQueue().get().family.index,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1029 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1030 checkVkResult vkCreateCommandPool(dev.vk, addr createInfo, nil, addr commandBufferPool)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1031 var allocInfo = VkCommandBufferAllocateInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1032 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1033 commandPool: commandBufferPool,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1034 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1035 commandBufferCount: INFLIGHTFRAMES,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1036 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1037 checkVkResult vkAllocateCommandBuffers(dev.vk, addr allocInfo, cmdBuffers.ToCPointer)
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1038
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1039 # start command buffer
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1040 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1041 let
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1042 currentFramebuffer = VkFramebuffer(0) # TODO
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1043 currentFrameInFlight = 1
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1044 cmd = cmdBuffers[currentFrameInFlight]
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1045 beginInfo = VkCommandBufferBeginInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1046 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1047 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1048 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1049 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1050 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo))
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1051
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1052 # start renderpass
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1053 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1054 var
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1055 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))]
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1056 renderPassInfo = VkRenderPassBeginInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1057 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1058 renderPass: renderpass,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1059 framebuffer: currentFramebuffer,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1060 renderArea: VkRect2D(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1061 offset: VkOffset2D(x: 0, y: 0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1062 extent: VkExtent2D(width: frameWidth, height: frameHeight),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1063 ),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1064 clearValueCount: uint32(clearColors.len),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1065 pClearValues: clearColors.ToCPointer(),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1066 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1067 viewport = VkViewport(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1068 x: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1069 y: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1070 width: frameWidth.float32,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1071 height: frameHeight.float32,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1072 minDepth: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1073 maxDepth: 1.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1074 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1075 scissor = VkRect2D(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1076 offset: VkOffset2D(x: 0, y: 0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1077 extent: VkExtent2D(width: frameWidth, height: frameHeight)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1078 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1079 vkCmdBeginRenderPass(cmd, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE)
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1080
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1081 # setup viewport
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1082 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1083 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1084
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1085 # bind pipeline, will be loop
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1086 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1087 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1088
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1089 # render object, will be loop
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1090 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1091 Render(pipeline1, myRenderable, myGlobals, cmd)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1092
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1093 vkCmdEndRenderPass(cmd)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1094 checkVkResult cmd.vkEndCommandBuffer()