annotate static_utils.nim @ 1180:d554f8185815 compiletime-tests

add: tons of work on descriptors
author sam <sam@basx.dev>
date Tue, 02 Jul 2024 06:42:42 +0700
parents e1830f9b8af4
children 6b66e6c837bc
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
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
15
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
16 template VertexAttribute {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
17 template InstanceAttribute {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
18 template Pass {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
19 template PassFlat {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
20 template ShaderOutput {.pragma.}
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
21 template VertexIndices {.pragma.}
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
22 template DescriptorSet {.pragma.}
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
23
1165
58694b30b9cb add: some stuff
sam <sam@basx.dev>
parents: 1164
diff changeset
24 const INFLIGHTFRAMES = 2'u32
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
25 const MAX_DESCRIPTORSETS = 2
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
26 const MEMORY_ALIGNMENT = 65536'u64 # Align buffers inside memory along this alignment
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
27 const BUFFER_ALIGNMENT = 64'u64 # align offsets inside buffers along this alignment
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
28
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
29 # some globals that will (likely?) never change during the life time of the engine
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
30 type
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
31 VulkanGlobals = object
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
32 instance: VkInstance
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
33 device: VkDevice
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
34 physicalDevice: VkPhysicalDevice
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
35 queueFamilyIndex: uint32
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
36 queue: VkQueue
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
37 var vulkan: VulkanGlobals
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
38
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
39 type
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
40 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]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
41 ShaderObject[TShader] = 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
42 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
43 fragmentShader: VkShaderModule
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
44
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
45 func alignedTo[T: SomeInteger](value: T, alignment: T): T =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
46 let remainder = value mod alignment
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
47 if remainder == 0:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
48 return value
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
49 else:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
50 return value + alignment - remainder
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
51
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
52 func VkType[T: SupportedGPUType](value: T): VkFormat =
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
53 when T is float32: VK_FORMAT_R32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
54 elif T is float64: VK_FORMAT_R64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
55 elif T is int8: VK_FORMAT_R8_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
56 elif T is int16: VK_FORMAT_R16_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
57 elif T is int32: VK_FORMAT_R32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
58 elif T is int64: VK_FORMAT_R64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
59 elif T is uint8: VK_FORMAT_R8_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
60 elif T is uint16: VK_FORMAT_R16_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
61 elif T is uint32: VK_FORMAT_R32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
62 elif T is uint64: VK_FORMAT_R64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
63 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
64 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
65 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
66 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
67 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
68 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
69 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
70 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
71 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
72 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
73 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
74 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
75 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
76 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
77 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
78 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
79 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
80 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
81 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
82 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
83 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
84 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
85 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
86 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
87 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
88 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
89 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
90 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
91 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
92 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
93 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
94 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
95 else: {.error: "Unsupported data type on GPU".}
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
96
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
97 func GlslType[T: SupportedGPUType|Texture](value: T): string =
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
98 when T is float32: "float"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
99 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
100 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
101 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
102 elif T is TVec2[int32]: "ivec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
103 elif T is TVec2[int64]: "ivec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
104 elif T is TVec3[int32]: "ivec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
105 elif T is TVec3[int64]: "ivec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
106 elif T is TVec4[int32]: "ivec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
107 elif T is TVec4[int64]: "ivec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
108 elif T is TVec2[uint32]: "uvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
109 elif T is TVec2[uint64]: "uvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
110 elif T is TVec3[uint32]: "uvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
111 elif T is TVec3[uint64]: "uvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
112 elif T is TVec4[uint32]: "uvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
113 elif T is TVec4[uint64]: "uvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
114 elif T is TVec2[float32]: "vec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
115 elif T is TVec2[float64]: "dvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
116 elif T is TVec3[float32]: "vec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
117 elif T is TVec3[float64]: "dvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
118 elif T is TVec4[float32]: "vec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
119 elif T is TVec4[float64]: "dvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
120 elif T is TMat2[float32]: "mat2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
121 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
122 elif T is TMat23[float32]: "mat23"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
123 elif T is TMat23[float64]: "dmat23"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
124 elif T is TMat32[float32]: "mat32"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
125 elif T is TMat32[float64]: "dmat32"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
126 elif T is TMat3[float32]: "mat3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
127 elif T is TMat3[float64]: "dmat3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
128 elif T is TMat34[float32]: "mat34"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
129 elif T is TMat34[float64]: "dmat34"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
130 elif T is TMat43[float32]: "mat43"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
131 elif T is TMat43[float64]: "dmat43"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
132 elif T is TMat4[float32]: "mat4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
133 elif T is TMat4[float64]: "dmat4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
134 elif T is Texture: "sampler2D"
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
135 else: {.error: "Unsupported data type on GPU".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
136
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
137 template ForVertexDataFields(shader: typed, fieldname, valuename, isinstancename, body: untyped): untyped =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
138 for theFieldname, value in fieldPairs(shader):
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
139 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
140 when not typeof(value) is seq:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
141 {.error: "field '" & theFieldname & "' needs to be a seq".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
142 when not typeof(value) is SupportedGPUType:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
143 {.error: "field '" & theFieldname & "' is not a supported GPU type".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
144 block:
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
145 const `fieldname` {.inject.} = theFieldname
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
146 let `valuename` {.inject.} = value
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
147 const `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
148 body
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
149
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
150 template ForDescriptorSets(shader: typed, setNumber, descriptorSet, body: untyped): untyped =
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
151 var n = 0
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
152 for theFieldname, value in fieldPairs(shader):
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
153 when value.hasCustomPragma(DescriptorSet):
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
154 block:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
155 let `setNumber` {.inject.} = n
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
156 let `descriptorSet` {.inject.} = value
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
157 body
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
158 n.inc
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
159
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
160 template ForDescriptorFields(shader: typed, fieldname, typename, countname, bindingNumber, body: untyped): untyped =
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
161 var `bindingNumber` {.inject.} = 1'u32
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
162 for theFieldname, value in fieldPairs(shader):
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
163 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
164 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
165 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
166 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
167 const `countname` {.inject.} = 1'u32
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
168 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
169 `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
170 elif typeof(value) is object:
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
171 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
172 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
173 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
174 const `countname` {.inject.} = 1'u32
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
175 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
176 `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
177 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
178 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
179 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
180 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
181 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
182 const `countname` {.inject.} = uint32(typeof(value).len)
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
183 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
184 `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
185 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
186 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
187 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
188 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
189 const `countname` {.inject.} = uint32(typeof(value).len)
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
190 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
191 `bindingNumber`.inc
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
192
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
193 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
194 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
195 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
196 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
197 3
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
198 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
199 4
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
200 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
201 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
202
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
203 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
204 #[
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
205 single location:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
206 - any scalar
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
207 - any 16-bit vector
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
208 - any 32-bit vector
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
209 - any 64-bit vector that has max. 2 components
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
210 16-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
211 32-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
212 64-bit scalar and 2-component vector types.
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
213 two locations
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
214 64-bit three- and four-component vectors
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
215 ]#
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
216 when T is TVec3[int64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
217 T is TVec4[int64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
218 T is TVec3[uint64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
219 T is TVec4[uint64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
220 T is TVec3[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
221 T is TVec4[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
222 T is TMat23[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
223 T is TMat3[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
224 T is TMat34[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
225 T is TMat43[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
226 T is TMat4[float64]:
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
227 return 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
228 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
229 return 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
230
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
231 type
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
232 IndexType = enum
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
233 None, UInt8, UInt16, UInt32
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
234
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
235 IndirectGPUMemory = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
236 vk: VkDeviceMemory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
237 size: uint64
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
238 needsTransfer: bool # usually true
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
239 DirectGPUMemory = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
240 vk: VkDeviceMemory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
241 size: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
242 data: pointer
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
243 needsFlush: bool # usually true
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
244 GPUMemory = IndirectGPUMemory | DirectGPUMemory
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
245
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
246 Buffer[TMemory: GPUMemory] = object
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
247 memory: TMemory
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
248 vk: VkBuffer
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
249 offset: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
250 size: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
251
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
252 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
253 data: seq[T]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
254 buffer: Buffer[TMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
255 offset: uint64
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
256 GPUValue[T: object|array, TMemory: GPUMemory] = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
257 data: T
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
258 buffer: Buffer[TMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
259 offset: uint64
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
260 GPUData = GPUArray | GPUValue
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
261
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
262 Pipeline[TShader] = object
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
263 pipeline: VkPipeline
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
264 layout: VkPipelineLayout
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
265 descriptorSetLayouts: array[MAX_DESCRIPTORSETS, VkDescriptorSetLayout]
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
266 BufferType = enum
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
267 VertexBuffer, IndexBuffer, UniformBuffer
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
268 RenderData = object
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
269 descriptorPool: VkDescriptorPool
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
270 # tuple is memory and offset to next free allocation in that memory
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
271 indirectMemory: seq[tuple[memory: IndirectGPUMemory, usedOffset: uint64]]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
272 directMemory: seq[tuple[memory: DirectGPUMemory, usedOffset: uint64]]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
273 indirectBuffers: seq[tuple[buffer: Buffer[IndirectGPUMemory], btype: BufferType, usedOffset: uint64]]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
274 directBuffers: seq[tuple[buffer: Buffer[DirectGPUMemory], btype: BufferType, usedOffset: uint64]]
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
275
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
276 template UsesIndirectMemory(gpuData: GPUData): untyped =
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
277 get(genericParams(typeof(gpuData)), 1) is IndirectGPUMemory
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
278 template UsesDirectMemory(gpuData: GPUData): untyped =
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
279 get(genericParams(typeof(gpuData)), 1) is DirectGPUMemory
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
280
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
281 template size(gpuArray: GPUArray): uint64 =
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
282 (gpuArray.data.len * sizeof(elementType(gpuArray.data))).uint64
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
283 template size(gpuValue: GPUValue): uint64 =
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
284 sizeof(gpuValue.data).uint64
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
285
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
286 template datapointer(gpuArray: GPUArray): pointer =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
287 addr(gpuArray.data[0])
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
288 template datapointer(gpuValue: GPUValue): pointer =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
289 addr(gpuValue.data)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
290
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
291 proc AllocationSize(buffer: Buffer): uint64 =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
292 var req: VkMemoryRequirements
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
293 vkGetBufferMemoryRequirements(vulkan.device, buffer.vk, addr(req))
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
294 return req.size
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
295
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
296 proc GetPhysicalDevice(instance: VkInstance): VkPhysicalDevice =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
297 var nDevices: uint32
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
298 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), nil)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
299 var devices = newSeq[VkPhysicalDevice](nDevices)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
300 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), devices.ToCPointer)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
301
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
302 var score = 0'u32
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
303 for pDevice in devices:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
304 var props: VkPhysicalDeviceProperties
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
305 vkGetPhysicalDeviceProperties(pDevice, addr(props))
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
306 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU and props.limits.maxImageDimension2D > score:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
307 score = props.limits.maxImageDimension2D
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
308 result = pDevice
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
309
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
310 if score == 0:
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
311 for pDevice in devices:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
312 var props: VkPhysicalDeviceProperties
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
313 vkGetPhysicalDeviceProperties(pDevice, addr(props))
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
314 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU and props.limits.maxImageDimension2D > score:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
315 score = props.limits.maxImageDimension2D
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
316 result = pDevice
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
317
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
318 assert score > 0, "Unable to find integrated or discrete GPU"
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
319
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
320
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
321 proc GetDirectMemoryTypeIndex(): uint32 =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
322 var physicalProperties: VkPhysicalDeviceMemoryProperties
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
323 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr(physicalProperties))
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
324
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
325 var biggestHeap: uint64 = 0
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
326 result = high(uint32)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
327 # try to find host-visible type
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
328 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
329 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
330 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
331 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
332 if size > biggestHeap:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
333 biggestHeap = size
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
334 result = i
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
335 assert result != high(uint32), "There is not host visible memory. This is likely a driver bug."
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
336
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
337 proc GetQueueFamily(pDevice: VkPhysicalDevice, qType: VkQueueFlagBits): uint32 =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
338 var nQueuefamilies: uint32
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
339 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, nil)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
340 var queuFamilies = newSeq[VkQueueFamilyProperties](nQueuefamilies)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
341 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, queuFamilies.ToCPointer)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
342 for i in 0'u32 ..< nQueuefamilies:
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
343 if qType in toEnums(queuFamilies[i].queueFlags):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
344 return i
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
345 assert false, &"Queue of type {qType} not found"
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
346
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
347 proc GetQueue(device: VkDevice, queueFamilyIndex: uint32, qType: VkQueueFlagBits): VkQueue =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
348 vkGetDeviceQueue(
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
349 device,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
350 queueFamilyIndex,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
351 0,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
352 addr(result),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
353 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
354
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
355 proc GetSurfaceFormat(): VkFormat =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
356 # EVERY windows driver and almost every linux driver should support this
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
357 VK_FORMAT_B8G8R8A8_SRGB
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
358
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
359 template WithSingleUseCommandBuffer(device: VkDevice, cmd, body: untyped): untyped =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
360 block:
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
361 var
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
362 commandBufferPool: VkCommandPool
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
363 createInfo = VkCommandPoolCreateInfo(
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
364 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
365 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
366 queueFamilyIndex: vulkan.queueFamilyIndex,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
367 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
368 checkVkResult vkCreateCommandPool(device, addr createInfo, nil, addr(commandBufferPool))
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
369 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
370 `cmd` {.inject.}: VkCommandBuffer
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
371 allocInfo = VkCommandBufferAllocateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
372 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
373 commandPool: commandBufferPool,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
374 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
375 commandBufferCount: 1,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
376 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
377 checkVkResult device.vkAllocateCommandBuffers(addr allocInfo, addr(`cmd`))
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
378 var beginInfo = VkCommandBufferBeginInfo(
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
379 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
380 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
381 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
382 checkVkResult `cmd`.vkBeginCommandBuffer(addr beginInfo)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
383
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
384 body
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
385
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
386 checkVkResult `cmd`.vkEndCommandBuffer()
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
387 var submitInfo = VkSubmitInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
388 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
389 commandBufferCount: 1,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
390 pCommandBuffers: addr(`cmd`),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
391 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
392
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
393 var
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
394 fence: VkFence
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
395 fenceInfo = VkFenceCreateInfo(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
396 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
397 # flags: toBits [VK_FENCE_CREATE_SIGNALED_BIT]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
398 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
399 checkVkResult device.vkCreateFence(addr(fenceInfo), nil, addr(fence))
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
400 checkVkResult vkQueueSubmit(vulkan.queue, 1, addr(submitInfo), fence)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
401 checkVkResult vkWaitForFences(device, 1, addr fence, false, high(uint64))
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
402 vkDestroyCommandPool(device, commandBufferPool, nil)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
403
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
404
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
405 proc UpdateGPUBuffer(gpuData: GPUData) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
406 if gpuData.size == 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
407 return
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
408 when UsesDirectMemory(gpuData):
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
409 copyMem(cast[pointer](cast[uint64](gpuData.buffer.memory.data) + gpuData.buffer.offset + gpuData.offset), gpuData.datapointer, gpuData.size)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
410 else:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
411 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
412 stagingBuffer: VkBuffer
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
413 createInfo = VkBufferCreateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
414 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
415 flags: VkBufferCreateFlags(0),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
416 size: gpuData.size,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
417 usage: toBits([VK_BUFFER_USAGE_TRANSFER_SRC_BIT]),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
418 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
419 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
420 checkVkResult vkCreateBuffer(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
421 device = vulkan.device,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
422 pCreateInfo = addr(createInfo),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
423 pAllocator = nil,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
424 pBuffer = addr(stagingBuffer),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
425 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
426 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
427 stagingMemory: VkDeviceMemory
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
428 stagingPtr: pointer
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
429 memoryAllocationInfo = VkMemoryAllocateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
430 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
431 allocationSize: gpuData.buffer.AllocationSize(),
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
432 memoryTypeIndex: GetDirectMemoryTypeIndex(),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
433 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
434 checkVkResult vkAllocateMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
435 vulkan.device,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
436 addr(memoryAllocationInfo),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
437 nil,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
438 addr(stagingMemory),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
439 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
440 checkVkResult vkBindBufferMemory(vulkan.device, stagingBuffer, stagingMemory, 0)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
441 checkVkResult vkMapMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
442 device = vulkan.device,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
443 memory = stagingMemory,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
444 offset = 0'u64,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
445 size = VK_WHOLE_SIZE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
446 flags = VkMemoryMapFlags(0),
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
447 ppData = addr(stagingPtr)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
448 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
449 copyMem(stagingPtr, gpuData.datapointer, gpuData.size)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
450 var stagingRange = VkMappedMemoryRange(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
451 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
452 memory: stagingMemory,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
453 size: VK_WHOLE_SIZE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
454 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
455 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, 1, addr(stagingRange))
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
456
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
457 WithSingleUseCommandBuffer(vulkan.device, commandBuffer):
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
458 var copyRegion = VkBufferCopy(size: gpuData.size)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
459 vkCmdCopyBuffer(commandBuffer, stagingBuffer, gpuData.buffer.vk, 1, addr(copyRegion))
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
460
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
461 vkDestroyBuffer(vulkan.device, stagingBuffer, nil)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
462 vkFreeMemory(vulkan.device, stagingMemory, nil)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
463
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
464 proc UpdateAllGPUBuffers[T](value: T) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
465 for name, fieldvalue in value.fieldPairs():
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
466 when typeof(fieldvalue) is GPUData:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
467 UpdateGPUBuffer(fieldvalue)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
468
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
469 proc AssertCompatible(TShader, TDescriptorSet: typedesc, DescriptorSetIndex: static int) =
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
470 ForDescriptorSets(default(TShader), setNumber, descriptorSet):
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
471 if setNumber == DescriptorSetIndex:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
472 assert typeof(descriptorSet) is TDescriptorSet
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
473
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
474 proc CreateDescriptorSet[T, TShader](
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
475 renderData: RenderData,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
476 pipeline: Pipeline[TShader],
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
477 value: T,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
478 setNumber: static int
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
479 ): array[INFLIGHTFRAMES.int, VkDescriptorSet] =
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
480
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
481 static: AssertCompatible(TShader, T, setNumber)
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
482
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
483 var layouts = newSeqWith(result.len, pipeline.descriptorSetLayouts[setNumber])
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
484 var allocInfo = VkDescriptorSetAllocateInfo(
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
485 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
486 descriptorPool: renderData.descriptorPool,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
487 descriptorSetCount: uint32(layouts.len),
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
488 pSetLayouts: layouts.ToCPointer,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
489 )
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
490 checkVkResult vkAllocateDescriptorSets(vulkan.device, addr(allocInfo), result.ToCPointer)
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
491
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
492 converter toVkIndexType(indexType: IndexType): VkIndexType =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
493 case indexType:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
494 of None: VK_INDEX_TYPE_NONE_KHR
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
495 of UInt8: VK_INDEX_TYPE_UINT8_EXT
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
496 of UInt16: VK_INDEX_TYPE_UINT16
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
497 of UInt32: VK_INDEX_TYPE_UINT32
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
498
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
499 proc CreateRenderPass(format: VkFormat): VkRenderPass =
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
500 var
sam <sam@basx.dev>
parents: 1165
diff changeset
501 attachments = @[VkAttachmentDescription(
sam <sam@basx.dev>
parents: 1165
diff changeset
502 format: format,
sam <sam@basx.dev>
parents: 1165
diff changeset
503 samples: VK_SAMPLE_COUNT_1_BIT,
sam <sam@basx.dev>
parents: 1165
diff changeset
504 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
sam <sam@basx.dev>
parents: 1165
diff changeset
505 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
sam <sam@basx.dev>
parents: 1165
diff changeset
506 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
sam <sam@basx.dev>
parents: 1165
diff changeset
507 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
sam <sam@basx.dev>
parents: 1165
diff changeset
508 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
sam <sam@basx.dev>
parents: 1165
diff changeset
509 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
sam <sam@basx.dev>
parents: 1165
diff changeset
510 )]
sam <sam@basx.dev>
parents: 1165
diff changeset
511 dependencies = @[VkSubpassDependency(
sam <sam@basx.dev>
parents: 1165
diff changeset
512 srcSubpass: VK_SUBPASS_EXTERNAL,
sam <sam@basx.dev>
parents: 1165
diff changeset
513 dstSubpass: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
514 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
515 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
516 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
517 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
518 )]
sam <sam@basx.dev>
parents: 1165
diff changeset
519 outputs = @[
sam <sam@basx.dev>
parents: 1165
diff changeset
520 VkAttachmentReference(
sam <sam@basx.dev>
parents: 1165
diff changeset
521 attachment: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
522 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
sam <sam@basx.dev>
parents: 1165
diff changeset
523 )
sam <sam@basx.dev>
parents: 1165
diff changeset
524 ]
sam <sam@basx.dev>
parents: 1165
diff changeset
525
sam <sam@basx.dev>
parents: 1165
diff changeset
526 var subpassesList = [
sam <sam@basx.dev>
parents: 1165
diff changeset
527 VkSubpassDescription(
sam <sam@basx.dev>
parents: 1165
diff changeset
528 flags: VkSubpassDescriptionFlags(0),
sam <sam@basx.dev>
parents: 1165
diff changeset
529 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS,
sam <sam@basx.dev>
parents: 1165
diff changeset
530 inputAttachmentCount: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
531 pInputAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
532 colorAttachmentCount: uint32(outputs.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
533 pColorAttachments: outputs.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
534 pResolveAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
535 pDepthStencilAttachment: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
536 preserveAttachmentCount: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
537 pPreserveAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
538 )
sam <sam@basx.dev>
parents: 1165
diff changeset
539 ]
sam <sam@basx.dev>
parents: 1165
diff changeset
540
sam <sam@basx.dev>
parents: 1165
diff changeset
541 var createInfo = VkRenderPassCreateInfo(
sam <sam@basx.dev>
parents: 1165
diff changeset
542 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
sam <sam@basx.dev>
parents: 1165
diff changeset
543 attachmentCount: uint32(attachments.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
544 pAttachments: attachments.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
545 subpassCount: uint32(subpassesList.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
546 pSubpasses: subpassesList.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
547 dependencyCount: uint32(dependencies.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
548 pDependencies: dependencies.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
549 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
550 checkVkResult vulkan.device.vkCreateRenderPass(addr(createInfo), nil, addr(result))
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
551
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
552 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
553 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
554 case stage
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
555 of VK_SHADER_STAGE_VERTEX_BIT: "vert"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
556 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
557 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
558 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
559 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
560 of VK_SHADER_STAGE_COMPUTE_BIT: "comp"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
561 else: ""
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
562
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
563 when defined(nimcheck): # will not run if nimcheck is running
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
564 return result
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
565
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
566 let
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
567 stagename = stage2string(stage)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
568 shaderHash = hash(shaderSource)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
569 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
570
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
571 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
572 echo "shader of type ", stage
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
573 for i, line in enumerate(shaderSource.splitlines()):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
574 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
575 # 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
576 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
577 when defined(windows):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
578 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
579 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
580 echo "run: ", command
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
581 discard StaticExecChecked(
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
582 command = command,
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
583 input = shaderSource
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
584 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
585 else:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
586 echo &"shaderfile {shaderfile} is up-to-date"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
587
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
588 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
589 let shaderbinary = staticRead shaderfile.replace("\\", "/")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
590 else:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
591 let shaderbinary = staticRead shaderfile
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
592
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
593 var i = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
594 while i < shaderbinary.len:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
595 result.add(
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
596 (uint32(shaderbinary[i + 0]) shl 0) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
597 (uint32(shaderbinary[i + 1]) shl 8) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
598 (uint32(shaderbinary[i + 2]) shl 16) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
599 (uint32(shaderbinary[i + 3]) shl 24)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
600 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
601 i += 4
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
602
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
603 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
604 const GLSL_VERSION = "450"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
605 var vsInput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
606 var vsOutput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
607 var fsInput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
608 var fsOutput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
609 var uniforms: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
610 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
611 var vsInputLocation = 0'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
612 var passLocation = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
613 var fsOutputLocation = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
614
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
615 var descriptorSetCount = 0
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
616 for fieldname, value in fieldPairs(shader):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
617 # 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
618 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute):
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
619 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
620 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
621 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
622 vsInputLocation += NLocationSlots(value)
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
623
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
624 # 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
625 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
626 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
627 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
628 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
629 passLocation.inc
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
630
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
631 # fragment shader output
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
632 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
633 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
634 fsOutputLocation.inc
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
635
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
636 # descriptor sets
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
637 # need to consider 4 cases: uniform block, texture, uniform block array, texture array
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
638 elif hasCustomPragma(value, DescriptorSet):
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
639 assert descriptorSetCount < MAX_DESCRIPTORSETS, &"{tt.name(TShader)}: maximum {MAX_DESCRIPTORSETS} allowed"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
640
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
641 var descriptorBinding = 0
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
642 for descriptorName, descriptorValue in fieldPairs(value):
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
643
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
644 when typeof(descriptorValue) is Texture:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
645 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(descriptorValue) & " " & descriptorName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
646 descriptorBinding.inc
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
647
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
648 elif typeof(descriptorValue) is GPUValue:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
649 uniforms.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform T" & descriptorName & " {"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
650 when typeof(descriptorValue.data) is object:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
651 for blockFieldName, blockFieldValue in descriptorValue.data.fieldPairs():
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
652 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
653 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
654 uniforms.add "} " & descriptorName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
655 elif typeof(descriptorValue.data) is array:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
656 for blockFieldName, blockFieldValue in default(elementType(descriptorValue.data)).fieldPairs():
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
657 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
658 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
659 uniforms.add "} " & descriptorName & "[" & $descriptorValue.data.len & "];"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
660 descriptorBinding.inc
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
661 elif typeof(descriptorValue) is array:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
662 when elementType(descriptorValue) is Texture:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
663 let arrayDecl = "[" & $typeof(descriptorValue).len & "]"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
664 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(descriptorValue))) & " " & descriptorName & "" & arrayDecl & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
665 descriptorBinding.inc
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
666 else:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
667 {.error: "Unsupported shader descriptor field " & descriptorName.}
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
668 descriptorSetCount.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
669 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
670 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
671 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
672 {.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
673
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
674 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
675 vsInput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
676 uniforms &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
677 samplers &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
678 vsOutput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
679 @[shader.vertexCode]).join("\n")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
680
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
681 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
682 fsInput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
683 uniforms &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
684 samplers &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
685 fsOutput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
686 @[shader.fragmentCode]).join("\n")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
687
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
688 proc CompileShader[TShader](shader: static TShader): ShaderObject[TShader] =
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
689 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
690
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
691 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
692 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
693
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
694 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
695 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
696 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
697 pCode: vertexBinary.ToCPointer,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
698 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
699 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader))
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
700 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
701 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
702 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
703 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
704 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
705 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader))
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
706
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
707
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
708 proc CreatePipeline[TShader](
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
709 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
710 shader: ShaderObject[TShader],
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
711 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
712 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
713 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
714 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
715 descriptorPoolLimit = 1024
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
716 ): Pipeline[TShader] =
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
717 # create pipeline
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
718
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
719 ForDescriptorSets(default(TShader), setNumber, descriptorSet):
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
720 var layoutbindings: seq[VkDescriptorSetLayoutBinding]
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
721 ForDescriptorFields(descriptorSet, fieldName, descriptorType, descriptorCount, descriptorBindingNumber):
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
722 layoutbindings.add VkDescriptorSetLayoutBinding(
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
723 binding: descriptorBindingNumber,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
724 descriptorType: descriptorType,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
725 descriptorCount: descriptorCount,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
726 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS),
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
727 pImmutableSamplers: nil,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
728 )
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
729 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo(
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
730 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
731 bindingCount: layoutbindings.len.uint32,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
732 pBindings: layoutbindings.ToCPointer
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
733 )
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
734 checkVkResult vkCreateDescriptorSetLayout(
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
735 vulkan.device,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
736 addr(layoutCreateInfo),
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
737 nil,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
738 addr(result.descriptorSetLayouts[setNumber])
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
739 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
740 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
741 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
742 setLayoutCount: result.descriptorSetLayouts.len.uint32,
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
743 pSetLayouts: result.descriptorSetLayouts.ToCPointer,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
744 # pushConstantRangeCount: uint32(pushConstants.len),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
745 # pPushConstantRanges: pushConstants.ToCPointer,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
746 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
747 checkVkResult vkCreatePipelineLayout(vulkan.device, addr(pipelineLayoutInfo), nil, addr(result.layout))
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
748
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
749 let stages = [
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
750 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
751 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
752 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
753 module: shader.vertexShader,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
754 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
755 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
756 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
757 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
758 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
759 module: shader.fragmentShader,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
760 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
761 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
762 ]
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
763 var
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
764 bindings: seq[VkVertexInputBindingDescription]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
765 attributes: seq[VkVertexInputAttributeDescription]
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
766 var inputBindingNumber = 0'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
767 var location = 0'u32
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
768 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
769 bindings.add VkVertexInputBindingDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
770 binding: inputBindingNumber,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
771 stride: sizeof(value).uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
772 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
773 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
774 # 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
775 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
776 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value):
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
777 attributes.add VkVertexInputAttributeDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
778 binding: inputBindingNumber,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
779 location: location,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
780 format: VkType(value),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
781 offset: i * perDescriptorSize,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
782 )
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
783 location += NLocationSlots(value)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
784 inc inputBindingNumber
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
785
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
786 let
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
787 vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
788 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
789 vertexBindingDescriptionCount: uint32(bindings.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
790 pVertexBindingDescriptions: bindings.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
791 vertexAttributeDescriptionCount: uint32(attributes.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
792 pVertexAttributeDescriptions: attributes.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
793 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
794 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
795 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
796 topology: topology,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
797 primitiveRestartEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
798 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
799 viewportState = VkPipelineViewportStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
800 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
801 viewportCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
802 scissorCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
803 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
804 rasterizer = VkPipelineRasterizationStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
805 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
806 depthClampEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
807 rasterizerDiscardEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
808 polygonMode: polygonMode,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
809 lineWidth: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
810 cullMode: toBits [cullMode],
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
811 frontFace: frontFace,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
812 depthBiasEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
813 depthBiasConstantFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
814 depthBiasClamp: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
815 depthBiasSlopeFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
816 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
817 multisampling = VkPipelineMultisampleStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
818 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
819 sampleShadingEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
820 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
821 minSampleShading: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
822 pSampleMask: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
823 alphaToCoverageEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
824 alphaToOneEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
825 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
826 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
827 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
828 blendEnable: VK_TRUE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
829 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
830 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
831 colorBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
832 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
833 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
834 alphaBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
835 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
836 colorBlending = VkPipelineColorBlendStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
837 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
838 logicOpEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
839 attachmentCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
840 pAttachments: addr(colorBlendAttachment),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
841 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
842 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
843 dynamicState = VkPipelineDynamicStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
844 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
845 dynamicStateCount: dynamicStates.len.uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
846 pDynamicStates: dynamicStates.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
847 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
848 let createInfo = VkGraphicsPipelineCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
849 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
850 stageCount: 2,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
851 pStages: stages.ToCPointer,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
852 pVertexInputState: addr(vertexInputInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
853 pInputAssemblyState: addr(inputAssembly),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
854 pViewportState: addr(viewportState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
855 pRasterizationState: addr(rasterizer),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
856 pMultisampleState: addr(multisampling),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
857 pDepthStencilState: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
858 pColorBlendState: addr(colorBlending),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
859 pDynamicState: addr(dynamicState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
860 layout: result.layout,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
861 renderPass: renderPass,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
862 subpass: 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
863 basePipelineHandle: VkPipeline(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
864 basePipelineIndex: -1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
865 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
866 checkVkResult vkCreateGraphicsPipelines(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
867 vulkan.device,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
868 VkPipelineCache(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
869 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
870 addr(createInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
871 nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
872 addr(result.pipeline)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
873 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
874
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
875 proc AllocateIndirectMemory(size: uint64): IndirectGPUMemory =
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
876 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
877 result.size = size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
878 result.needsTransfer = true
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
879
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
880 # find a good memory type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
881 var physicalProperties: VkPhysicalDeviceMemoryProperties
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
882 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
883
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
884 var biggestHeap: uint64 = 0
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
885 var memoryTypeIndex = high(uint32)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
886 # try to find non-host-visible type
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
887 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
888 if not (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags)):
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
889 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
890 if size > biggestHeap:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
891 biggestHeap = size
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
892 memoryTypeIndex = i
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
893
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
894 # If we did not found a device-only memory type, let's just take the biggest overall
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
895 if memoryTypeIndex == high(uint32):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
896 result.needsTransfer = false
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
897 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
898 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
899 if size > biggestHeap:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
900 biggestHeap = size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
901 memoryTypeIndex = i
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
902
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
903 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
904 var allocationInfo = VkMemoryAllocateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
905 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
906 allocationSize: result.size,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
907 memoryTypeIndex: memoryTypeIndex,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
908 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
909 checkVkResult vkAllocateMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
910 vulkan.device,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
911 addr allocationInfo,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
912 nil,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
913 addr result.vk
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
914 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
915
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
916 proc AllocateDirectMemory(size: uint64): DirectGPUMemory =
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
917 result.size = size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
918 result.needsFlush = true
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
919
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
920 # find a good memory type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
921 var physicalProperties: VkPhysicalDeviceMemoryProperties
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
922 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties)
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
923
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
924 var biggestHeap: uint64 = 0
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
925 var memoryTypeIndex = high(uint32)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
926 # try to find host-visible type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
927 for i in 0 ..< physicalProperties.memoryTypeCount:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
928 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
929 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
930 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
931 if size > biggestHeap:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
932 biggestHeap = size
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
933 memoryTypeIndex = i
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
934 result.needsFlush = not (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT in flags)
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
935
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
936 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
937 var allocationInfo = VkMemoryAllocateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
938 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
939 allocationSize: result.size,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
940 memoryTypeIndex: GetDirectMemoryTypeIndex(),
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
941 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
942 checkVkResult vkAllocateMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
943 vulkan.device,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
944 addr allocationInfo,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
945 nil,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
946 addr result.vk
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
947 )
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
948 checkVkResult vkMapMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
949 device = vulkan.device,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
950 memory = result.vk,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
951 offset = 0'u64,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
952 size = result.size,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
953 flags = VkMemoryMapFlags(0),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
954 ppData = addr(result.data)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
955 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
956
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
957 proc AllocateIndirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
958 if size == 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
959 return
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
960 var buffer = Buffer[IndirectGPUMemory](size: size)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
961
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
962 let usageFlags = case btype:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
963 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
964 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
965 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
966
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
967 # iterate through memory areas to find big enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
968 # TODO: dynamically expand memory allocations
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
969 for (memory, usedOffset) in renderData.indirectMemory.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
970 if memory.size - usedOffset >= size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
971 buffer.offset = usedOffset
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
972 # create buffer
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
973 var createInfo = VkBufferCreateInfo(
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
974 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
975 flags: VkBufferCreateFlags(0),
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
976 size: buffer.size,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
977 usage: toBits(usageFlags),
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
978 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
979 )
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
980 checkVkResult vkCreateBuffer(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
981 device = vulkan.device,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
982 pCreateInfo = addr createInfo,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
983 pAllocator = nil,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
984 pBuffer = addr(buffer.vk)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
985 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
986 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
987 renderData.indirectBuffers.add (buffer, btype, 0'u64)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
988 # update memory area offset
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
989 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
990 return
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
991
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
992 assert false, "Did not find allocated memory region with enough space"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
993
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
994 proc AllocateDirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
995 if size == 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
996 return
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
997
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
998 var buffer = Buffer[DirectGPUMemory](size: size)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
999
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1000 let usageFlags = case btype:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1001 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1002 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1003 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1004
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1005 # iterate through memory areas to find big enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1006 # TODO: dynamically expand memory allocations
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1007 for (memory, usedOffset) in renderData.directMemory.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1008 if memory.size - usedOffset >= size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1009 buffer.offset = usedOffset
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1010 # create buffer
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1011 var createInfo = VkBufferCreateInfo(
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1012 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1013 flags: VkBufferCreateFlags(0),
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1014 size: buffer.size,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1015 usage: toBits(usageFlags),
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1016 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1017 )
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1018 checkVkResult vkCreateBuffer(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1019 device = vulkan.device,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1020 pCreateInfo = addr createInfo,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1021 pAllocator = nil,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1022 pBuffer = addr(buffer.vk)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1023 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1024 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1025 renderData.directBuffers.add (buffer, btype, 0'u64)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1026 # update memory area offset
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1027 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1028 return
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1029
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1030 assert false, "Did not find allocated memory region with enough space"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1031
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1032 proc InitRenderData(descriptorPoolLimit = 1024'u32): RenderData =
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1033 # allocate descriptor pools
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1034 var poolSizes = [
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1035 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit),
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1036 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit),
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1037 ]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1038 var poolInfo = VkDescriptorPoolCreateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1039 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1040 poolSizeCount: poolSizes.len.uint32,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1041 pPoolSizes: poolSizes.ToCPointer,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1042 maxSets: descriptorPoolLimit,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1043 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1044 checkVkResult vkCreateDescriptorPool(vulkan.device, addr(poolInfo), nil, addr(result.descriptorPool))
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1045
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1046 # allocate some memory
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1047 var initialAllocationSize = 1_000_000_000'u64 # TODO: make this more dynamic or something
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1048 result.indirectMemory = @[(memory: AllocateIndirectMemory(size = initialAllocationSize), usedOffset: 0'u64)]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1049 result.directMemory = @[(memory: AllocateDirectMemory(size = initialAllocationSize), usedOffset: 0'u64)]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1050
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1051 proc FlushDirectMemory(renderData: RenderData) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1052 var flushRegions = newSeqOfCap[VkMappedMemoryRange](renderData.directMemory.len)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1053 for entry in renderData.directMemory:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1054 if entry.usedOffset > 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1055 flushRegions.add VkMappedMemoryRange(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1056 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1057 memory: entry.memory.vk,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1058 size: entry.usedOffset,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1059 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1060 if flushRegions.len > 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1061 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, flushRegions.len.uint32, flushRegions.ToCPointer())
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1062
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1063 # For the Get*BufferSize:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1064 # BUFFER_ALIGNMENT is just added for a rough estimate, to ensure we have enough space to align when binding
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1065 proc GetIndirectBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1066 for name, value in fieldPairs(data):
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1067 when not hasCustomPragma(value, VertexIndices):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1068 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1069 when UsesIndirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1070 result += value.size + BUFFER_ALIGNMENT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1071 proc GetDirectBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1072 for name, value in fieldPairs(data):
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1073 when not hasCustomPragma(value, VertexIndices):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1074 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1075 when UsesDirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1076 result += value.size + BUFFER_ALIGNMENT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1077 proc GetIndirectIndexBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1078 for name, value in fieldPairs(data):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1079 when hasCustomPragma(value, VertexIndices):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1080 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1081 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1082 when UsesIndirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1083 result += value.size + BUFFER_ALIGNMENT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1084 proc GetDirectIndexBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1085 for name, value in fieldPairs(data):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1086 when hasCustomPragma(value, VertexIndices):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1087 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1088 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1089 when UsesDirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1090 result += value.size + BUFFER_ALIGNMENT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1091
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1092 proc AssignIndirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1093 for name, value in fieldPairs(data):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1094 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1095 when UsesIndirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1096 # find next buffer of correct type with enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1097 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1098 var foundBuffer = false
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1099 for (buffer, bt, offset) in renderData.indirectBuffers.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1100 if bt == btype and buffer.size - offset >= value.size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1101 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1102 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1103 value.buffer = buffer
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1104 value.offset = offset
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1105 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1106 foundBuffer = true
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1107 break
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1108 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1109 proc AssignDirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1110 for name, value in fieldPairs(data):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1111 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1112 when UsesDirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1113 # find next buffer of correct type with enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1114 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1115 var foundBuffer = false
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1116 for (buffer, bt, offset) in renderData.directBuffers.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1117 if bt == btype and buffer.size - offset >= value.size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1118 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1119 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1120 value.buffer = buffer
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1121 value.offset = offset
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1122 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1123 foundBuffer = true
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1124 break
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1125 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'"
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1126
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1127 proc HasGPUValueField[T](name: static string): bool {.compileTime.} =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1128 for fieldname, value in default(T).fieldPairs():
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1129 when typeof(value) is GPUValue and fieldname == name: return true
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1130 return false
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1131
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1132 template WithGPUValueField(obj: object, name: static string, fieldvalue, body: untyped): untyped =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1133 # HasGPUValueField MUST be used to check if this is supported
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1134 for fieldname, value in obj.fieldPairs():
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1135 when fieldname == name:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1136 block:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1137 let `fieldvalue` {.inject.} = value
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1138 body
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1139
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1140 proc WriteDescriptors[TShader, TUniforms, TGlobals](renderData: RenderData, uniforms: TUniforms, globals: TGlobals) =
1165
58694b30b9cb add: some stuff
sam <sam@basx.dev>
parents: 1164
diff changeset
1141 var descriptorSetWrites: seq[VkWriteDescriptorSet]
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1142 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber):
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1143 for frameInFlight in 0 ..< renderData.descriptorSets.len:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1144 when descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1145 when HasGPUValueField[TUniforms](fieldName):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1146 WithGPUValueField(uniforms, fieldName, gpuValue):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1147 let bufferInfo = VkDescriptorBufferInfo(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1148 buffer: gpuValue.buffer.vk,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1149 offset: gpuValue.buffer.offset,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1150 range: gpuValue.buffer.size,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1151 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1152 descriptorSetWrites.add VkWriteDescriptorSet(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1153 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1154 dstSet: renderData.descriptorSets[frameInFlight],
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1155 dstBinding: descriptorBindingNumber,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1156 dstArrayElement: uint32(0),
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1157 descriptorType: descriptorType,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1158 descriptorCount: descriptorCount,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1159 pImageInfo: nil,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1160 pBufferInfo: addr(bufferInfo),
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1161 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1162 elif HasGPUValueField[TGlobals](fieldName):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1163 WithGPUValueField(globals, fieldName, theValue):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1164 let bufferInfo = VkDescriptorBufferInfo(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1165 buffer: theValue.buffer.vk,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1166 offset: theValue.buffer.offset,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1167 range: theValue.buffer.size,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1168 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1169 descriptorSetWrites.add VkWriteDescriptorSet(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1170 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1171 dstSet: renderData.descriptorSets[frameInFlight],
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1172 dstBinding: descriptorBindingNumber,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1173 dstArrayElement: uint32(0),
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1174 descriptorType: descriptorType,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1175 descriptorCount: descriptorCount,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1176 pImageInfo: nil,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1177 pBufferInfo: addr(bufferInfo),
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1178 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1179 else:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1180 {.error: "Unable to find field '" & fieldName & "' in uniforms or globals".}
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1181 elif descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1182 # TODO
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1183 let imageInfo = VkDescriptorImageInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1184 sampler: VkSampler(0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1185 imageView: VkImageView(0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1186 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1187 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1188 descriptorSetWrites.add VkWriteDescriptorSet(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1189 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1190 dstSet: renderData.descriptorSets[frameInFlight],
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1191 dstBinding: descriptorBindingNumber,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1192 dstArrayElement: 0'u32,
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1193 descriptorType: descriptorType,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1194 descriptorCount: descriptorCount,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1195 pImageInfo: addr(imageInfo),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1196 pBufferInfo: nil,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1197 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1198 else:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1199 assert false, "Unsupported descriptor type"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1200 echo descriptorSetWrites
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1201 vkUpdateDescriptorSets(vulkan.device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil)
1165
58694b30b9cb add: some stuff
sam <sam@basx.dev>
parents: 1164
diff changeset
1202
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
1203 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1204 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1205 #[
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1206 commandBuffer.vkCmdBindDescriptorSets(
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1207 VK_PIPELINE_BIND_POINT_GRAPHICS,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1208 pipeline.layout,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1209 0,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1210 1,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1211 addr pipeline.descriptorSets[currentFrameInFlight],
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1212 0,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1213 nil,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1214 )
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1215 ]#
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1216
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1217 proc AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals: typedesc) =
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1218 # TODO: overhaul this
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1219 for inputName, inputValue in default(TShader).fieldPairs:
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1220 var foundField = false
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1221
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1222 # Vertex input data
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1223 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
1224 assert typeof(inputValue) is SupportedGPUType
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1225 for meshName, meshValue in default(TMesh).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1226 when meshName == inputName:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1227 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
1228 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
1229 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
1230 foundField = true
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1231 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
1232
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1233 # Instance input data
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1234 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
1235 assert typeof(inputValue) is SupportedGPUType
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1236 for instanceName, instanceValue in default(TInstance).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1237 when instanceName == inputName:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1238 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
1239 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
1240 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
1241 foundField = true
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1242 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
1243
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1244 # Texture
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1245 elif typeof(inputValue) is Texture:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1246 for uniformName, uniformValue in default(TUniforms).fieldPairs:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1247 when uniformName == inputName:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1248 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1249 assert typeof(uniformValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue)) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1250 foundField = true
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1251 for globalName, globalValue in default(TGlobals).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1252 when globalName == inputName:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1253 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
1254 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
1255 foundField = true
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1256 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
1257
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1258 # Uniform block
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1259 elif typeof(inputValue) is object:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1260 for uniformName, uniformValue in default(TUniforms).fieldPairs:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1261 when uniformName == inputName:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1262 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue))
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1263 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1264 assert typeof(uniformValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue.data)) & "'"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1265 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1266 for globalName, globalValue in default(TGlobals).fieldPairs:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1267 when globalName == inputName:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1268 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
1269 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
1270 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
1271 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1272 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
1273
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1274 # 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
1275 elif typeof(inputValue) is array:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1276
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1277 # texture-array
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1278 when elementType(inputValue) is Texture:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1279 for uniformName, uniformValue in default(TUniforms).fieldPairs:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1280 when uniformName == inputName:
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
1281 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1282 assert typeof(uniformValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue)) & "'"
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
1283 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
1284 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
1285 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
1286 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
1287 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
1288 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
1289 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
1290
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1291 # uniform-block array
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1292 elif elementType(inputValue) is object:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1293 for uniformName, uniformValue in default(TUniforms).fieldPairs:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1294 when uniformName == inputName:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1295 assert uniformValue is GPUValue, "global attribute '" & uniformName & "' must be of type 'GPUValue' but is of type " & tt.name(typeof(uniformValue))
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1296 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once"
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1297 assert typeof(uniformValue.data) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but uniform attribute is of type '" & tt.name(typeof(uniformValue.data)) & "'"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1298 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1299 for globalName, globalValue in default(TGlobals).fieldPairs:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1300 when globalName == inputName:
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1301 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
1302 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
1303 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
1304 foundField = true
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1305 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
1306
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1307
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1308 proc Render[TShader, TUniforms, TGlobals, TMesh, TInstance](
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1309 commandBuffer: VkCommandBuffer,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1310 pipeline: Pipeline[TShader],
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1311 uniforms: TUniforms,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1312 globals: TGlobals,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1313 mesh: TMesh,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1314 instances: TInstance,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1315 ) =
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1316 discard
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1317 # static: AssertCompatible(TShader, TMesh, TInstance, TUniforms, TGlobals)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1318 #[
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1319 if renderable.vertexBuffers.len > 0:
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1320 commandBuffer.vkCmdBindVertexBuffers(
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1321 firstBinding = 0'u32,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1322 bindingCount = uint32(renderable.vertexBuffers.len),
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1323 pBuffers = renderable.vertexBuffers.ToCPointer(),
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1324 pOffsets = renderable.bufferOffsets.ToCPointer()
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1325 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1326 if renderable.indexType != None:
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1327 commandBuffer.vkCmdBindIndexBuffer(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1328 renderable.indexBuffer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1329 renderable.indexBufferOffset,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1330 renderable.indexType,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1331 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1332 commandBuffer.vkCmdDrawIndexed(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1333 indexCount = renderable.indexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1334 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1335 firstIndex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1336 vertexOffset = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1337 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1338 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1339 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1340 commandBuffer.vkCmdDraw(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1341 vertexCount = renderable.vertexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1342 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1343 firstVertex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1344 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1345 )
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1346 ]#
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1347
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1348 when isMainModule:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1349 import semicongine/platform/window
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1350 import semicongine/vulkan/instance
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1351 import semicongine/vulkan/device
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1352 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
1353 import std/options
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1354
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1355 type
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1356 MeshA = object
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1357 position: GPUArray[Vec3f, IndirectGPUMemory]
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1358 indices {.VertexIndices.}: GPUArray[uint16, IndirectGPUMemory]
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1359 InstanceA = object
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1360 rotation: GPUArray[Vec4f, IndirectGPUMemory]
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1361 objPosition: GPUArray[Vec3f, IndirectGPUMemory]
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
1362 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
1363 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
1364 baseColor: Vec3f
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1365 UniformsA = object
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1366 defaultTexture: Texture
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1367 defaultMaterial: GPUValue[MaterialA, IndirectGPUMemory]
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1368 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1369 materialTextures: array[3, Texture]
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1370 ShaderSettings = object
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1371 gamma: float32
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1372 GlobalsA = object
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1373 fontAtlas: Texture
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1374 settings: GPUValue[ShaderSettings, IndirectGPUMemory]
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1375
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1376 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
1377 # vertex input
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1378 position {.VertexAttribute.}: Vec3f
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1379 objPosition {.InstanceAttribute.}: Vec3f
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1380 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
1381 # intermediate
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1382 test {.Pass.}: float32
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1383 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
1384 # output
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1385 color {.ShaderOutput.}: Vec4f
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1386 # descriptor sets
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1387 globals {.DescriptorSet.}: GlobalsA
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1388 uniforms {.DescriptorSet.}: UniformsA
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
1389 # code
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1390 vertexCode: string = "void main() {}"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1391 fragmentCode: string = "void main() {}"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1392
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1393 let w = CreateWindow("test2")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1394 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")
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1395
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1396 # TODO: remove those ugly wrappers
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1397 let theInstance = w.CreateInstance(
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1398 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0),
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1399 instanceExtensions = @[],
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1400 layers = @["VK_LAYER_KHRONOS_validation"],
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1401 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1402
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1403 let dev = theInstance.CreateDevice(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1404 theInstance.GetPhysicalDevices().FilterBestGraphics(),
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1405 enabledExtensions = @[],
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1406 theInstance.GetPhysicalDevices().FilterBestGraphics().FilterForGraphicsPresentationQueues()
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1407 ).vk
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1408 let frameWidth = 100'u32
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1409 let frameHeight = 100'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1410
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1411 # TODO: pack this stuff into a setup method and condense everything a bit
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1412 let pDevice = theInstance.vk.GetPhysicalDevice()
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1413 let qfi = pDevice.GetQueueFamily(VK_QUEUE_GRAPHICS_BIT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1414 vulkan = VulkanGlobals(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1415 instance: theInstance.vk,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1416 device: dev,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1417 physicalDevice: pDevice,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1418 queueFamilyIndex: qfi,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1419 queue: dev.GetQueue(qfi, VK_QUEUE_GRAPHICS_BIT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1420 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1421
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1422 var myMesh1 = MeshA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1423 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
1424 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1425 var uniforms1 = UniformsA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1426 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1427 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1428 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1429 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1430 ]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1431 materialTextures: [
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1432 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
1433 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
1434 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
1435 ]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1436 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1437 var instances1 = InstanceA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1438 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
1439 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
1440 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1441 var myGlobals: GlobalsA
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1442
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1443 # setup for rendering (TODO: swapchain & framebuffers)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1444 let renderpass = CreateRenderPass(GetSurfaceFormat())
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1445
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1446 # shaders
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1447 const shader = ShaderA()
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1448 let shaderObject = CompileShader(shader)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1449 var pipeline1 = CreatePipeline(renderPass = renderpass, shader = shaderObject)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1450
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1451 var renderdata = InitRenderData()
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1452
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1453 # TODO: Textures
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1454 # upload all textures
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1455 # write descriptors for textures and uniform buffers
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1456
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1457 # buffer allocation
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1458 var
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1459 indirectVertexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1460 directVertexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1461 indirectIndexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1462 directIndexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1463 indirectUniformSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1464 directUniformSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1465
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1466 # buffer allocation
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1467
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1468 echo "Allocating GPU buffers"
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1469 indirectVertexSizes += GetIndirectBufferSizes(myMesh1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1470 indirectVertexSizes += GetIndirectBufferSizes(instances1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1471 AllocateIndirectBuffer(renderdata, indirectVertexSizes, VertexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1472
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1473 directVertexSizes += GetDirectBufferSizes(myMesh1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1474 directVertexSizes += GetDirectBufferSizes(instances1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1475 AllocateDirectBuffer(renderdata, directVertexSizes, VertexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1476
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1477 indirectIndexSizes += GetIndirectIndexBufferSizes(myMesh1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1478 AllocateIndirectBuffer(renderdata, indirectIndexSizes, IndexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1479
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1480 directIndexSizes += GetDirectIndexBufferSizes(myMesh1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1481 AllocateDirectBuffer(renderdata, directIndexSizes, IndexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1482
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1483 indirectUniformSizes += GetIndirectBufferSizes(uniforms1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1484 indirectUniformSizes += GetIndirectBufferSizes(myGlobals)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1485 AllocateIndirectBuffer(renderdata, indirectUniformSizes, UniformBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1486
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1487 directUniformSizes += GetDirectBufferSizes(uniforms1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1488 directUniformSizes += GetDirectBufferSizes(myGlobals)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1489 AllocateDirectBuffer(renderdata, directUniformSizes, UniformBuffer)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1490
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1491 # buffer assignment
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1492 #
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1493 echo "Assigning buffers to GPUData fields"
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1494
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1495 # for meshes we do:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1496 renderdata.AssignIndirectBuffers(VertexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1497 renderdata.AssignDirectBuffers(VertexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1498 renderdata.AssignIndirectBuffers(IndexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1499 renderdata.AssignDirectBuffers(IndexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1500
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1501 # for instances we do:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1502 renderdata.AssignIndirectBuffers(VertexBuffer, instances1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1503 renderdata.AssignDirectBuffers(VertexBuffer, instances1)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1504
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1505 # for uniforms/globals we do:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1506 renderdata.AssignIndirectBuffers(UniformBuffer, uniforms1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1507 renderdata.AssignDirectBuffers(UniformBuffer, uniforms1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1508 renderdata.AssignIndirectBuffers(UniformBuffer, myGlobals)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1509 renderdata.AssignDirectBuffers(UniformBuffer, myGlobals)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1510
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1511 # buffer filling
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1512
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1513 echo "Copying all data to GPU memory"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1514
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1515 # copy everything to GPU
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1516 UpdateAllGPUBuffers(myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1517 UpdateAllGPUBuffers(instances1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1518 UpdateAllGPUBuffers(uniforms1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1519 UpdateAllGPUBuffers(myGlobals)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1520 renderdata.FlushDirectMemory()
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1521
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1522 var s1 = CreateDescriptorSet(renderdata, pipeline1, myGlobals, 0)
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1523 var s2 = CreateDescriptorSet(renderdata, pipeline1, uniforms1, 1)
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1524
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1525 # descriptors
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1526 # WriteDescriptors[ShaderA, UniformsA, GlobalsA](renderdata, uniforms1, myGlobals)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1527
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1528 # create descriptor sets
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1529 #[
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1530 var descriptorSets: array[INFLIGHTFRAMES.int, VkDescriptorSet]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1531 var layouts = newSeqWith(descriptorSets.len, pipeline.descriptorSetLayouts)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1532 var allocInfo = VkDescriptorSetAllocateInfo(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1533 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1534 descriptorPool: pool,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1535 descriptorSetCount: uint32(layouts.len),
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1536 pSetLayouts: layouts.ToCPointer,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1537 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1538 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), descriptorSets.ToCPointer)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1539 ]#
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1540
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1541
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1542
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1543 # command buffer
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1544 var
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1545 commandBufferPool: VkCommandPool
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1546 createInfo = VkCommandPoolCreateInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1547 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1548 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1549 queueFamilyIndex: vulkan.queueFamilyIndex,
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1550 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1551 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1552 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1553 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1554 allocInfo = VkCommandBufferAllocateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1555 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1556 commandPool: commandBufferPool,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1557 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1558 commandBufferCount: INFLIGHTFRAMES,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1559 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1560 checkVkResult vkAllocateCommandBuffers(vulkan.device, 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
1561
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1562 # start command buffer
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1563 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1564 let
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1565 currentFramebuffer = VkFramebuffer(0) # TODO
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1566 currentFrameInFlight = 1
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1567 cmd = cmdBuffers[currentFrameInFlight]
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1568 beginInfo = VkCommandBufferBeginInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1569 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1570 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1571 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1572 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1573 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
1574
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1575 # start renderpass
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1576 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1577 var
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1578 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))]
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1579 renderPassInfo = VkRenderPassBeginInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1580 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1581 renderPass: renderpass,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1582 framebuffer: currentFramebuffer, # TODO
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1583 renderArea: VkRect2D(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1584 offset: VkOffset2D(x: 0, y: 0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1585 extent: VkExtent2D(width: frameWidth, height: frameHeight),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1586 ),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1587 clearValueCount: uint32(clearColors.len),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1588 pClearValues: clearColors.ToCPointer(),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1589 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1590 viewport = VkViewport(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1591 x: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1592 y: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1593 width: frameWidth.float32,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1594 height: frameHeight.float32,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1595 minDepth: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1596 maxDepth: 1.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1597 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1598 scissor = VkRect2D(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1599 offset: VkOffset2D(x: 0, y: 0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1600 extent: VkExtent2D(width: frameWidth, height: frameHeight)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1601 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1602 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
1603
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1604 # setup viewport
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1605 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1606 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1607
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1608 # bind pipeline, will be loop
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1609 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1610 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1611
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1612 # render object, will be loop
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1613 block:
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1614 Render(cmd, pipeline1, uniforms1, myGlobals, myMesh1, instances1)
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1615
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1616 vkCmdEndRenderPass(cmd)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1617 checkVkResult cmd.vkEndCommandBuffer()