Mercurial > games > semicongine
annotate tests/test_rendering.nim @ 1234:841e12f33c47
add: text & font rendering, not tested yet
author | sam <sam@basx.dev> |
---|---|
date | Sat, 20 Jul 2024 00:03:57 +0700 |
parents | 1cf698973dca |
children | c70fee6568f6 |
rev | line source |
---|---|
1233
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
1 import std/os |
1231 | 2 import std/sequtils |
1232 | 3 import std/monotimes |
4 import std/times | |
1203 | 5 import std/options |
1204 | 6 import std/random |
7 | |
1223
55896320c8bf
fix: references to new package not updated
sam <sam@basx.dev>
parents:
1214
diff
changeset
|
8 import ../semiconginev2 |
1203 | 9 |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
10 proc test_01_triangle(nFrames: int, swapchain: var Swapchain) = |
1203 | 11 var renderdata = InitRenderData() |
12 | |
13 type | |
14 TrianglShader = object | |
15 position {.VertexAttribute.}: Vec3f | |
16 color {.VertexAttribute.}: Vec3f | |
17 fragmentColor {.Pass.}: Vec3f | |
18 outColor {.ShaderOutput.}: Vec4f | |
19 # code | |
20 vertexCode: string = """void main() { | |
21 fragmentColor = color; | |
22 gl_Position = vec4(position, 1);}""" | |
23 fragmentCode: string = """void main() { | |
24 outColor = vec4(fragmentColor, 1);}""" | |
25 TriangleMesh = object | |
26 position: GPUArray[Vec3f, VertexBuffer] | |
27 color: GPUArray[Vec3f, VertexBuffer] | |
28 var mesh = TriangleMesh( | |
1204 | 29 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), |
1203 | 30 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), |
31 ) | |
1204 | 32 AssignBuffers(renderdata, mesh) |
33 renderdata.FlushAllMemory() | |
1203 | 34 |
35 var | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
36 pipeline = CreatePipeline[TrianglShader](renderPass = swapchain.renderPass) |
1203 | 37 |
1204 | 38 var c = 0 |
39 while UpdateInputs() and c < nFrames: | |
1203 | 40 WithNextFrame(swapchain, framebuffer, commandbuffer): |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
41 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
1203 | 42 WithPipeline(commandbuffer, pipeline): |
1204 | 43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
44 inc c | |
1203 | 45 |
46 # cleanup | |
1204 | 47 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1203 | 48 DestroyPipeline(pipeline) |
49 DestroyRenderData(renderdata) | |
50 | |
51 | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
52 proc test_02_triangle_quad_instanced(nFrames: int, swapchain: var Swapchain) = |
1204 | 53 var renderdata = InitRenderData() |
54 | |
55 type | |
56 SomeShader = object | |
57 position {.VertexAttribute.}: Vec3f | |
58 color {.VertexAttribute.}: Vec3f | |
59 pos {.InstanceAttribute.}: Vec3f | |
60 scale {.InstanceAttribute.}: float32 | |
61 fragmentColor {.Pass.}: Vec3f | |
62 outColor {.ShaderOutput.}: Vec4f | |
63 # code | |
64 vertexCode: string = """void main() { | |
65 fragmentColor = color; | |
66 gl_Position = vec4((position * scale) + pos, 1);}""" | |
67 fragmentCode: string = """void main() { | |
68 outColor = vec4(fragmentColor, 1);}""" | |
69 TriangleMesh = object | |
70 position: GPUArray[Vec3f, VertexBuffer] | |
71 color: GPUArray[Vec3f, VertexBuffer] | |
72 QuadMesh = object | |
73 position: GPUArray[Vec3f, VertexBuffer] | |
74 color: GPUArray[Vec3f, VertexBuffer] | |
75 indices: GPUArray[uint16, IndexBuffer] | |
76 Instances = object | |
77 pos: GPUArray[Vec3f, VertexBuffer] | |
78 scale: GPUArray[float32, VertexBuffer] | |
79 var tri = TriangleMesh( | |
80 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), | |
81 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), | |
82 ) | |
83 var quad = QuadMesh( | |
84 position: asGPUArray([NewVec3f(-0.3, -0.3), NewVec3f(-0.3, 0.3), NewVec3f(0.3, 0.3), NewVec3f(0.3, -0.3)], VertexBuffer), | |
85 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | |
86 color: asGPUArray([NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1)], VertexBuffer), | |
87 ) | |
88 | |
89 var instancesA: Instances | |
90 for n in 1..100: | |
91 instancesA.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32)) | |
92 instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32) | |
93 var instancesB: Instances | |
94 for n in 1..100: | |
95 instancesB.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32)) | |
96 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) | |
97 | |
98 AssignBuffers(renderdata, tri) | |
99 AssignBuffers(renderdata, quad) | |
100 AssignBuffers(renderdata, instancesA) | |
101 AssignBuffers(renderdata, instancesB) | |
102 renderdata.FlushAllMemory() | |
103 | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
104 var pipeline = CreatePipeline[SomeShader](renderPass = swapchain.renderPass) |
1204 | 105 |
106 var c = 0 | |
107 while UpdateInputs() and c < nFrames: | |
108 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
109 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
1204 | 110 WithPipeline(commandbuffer, pipeline): |
111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) | |
112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) | |
113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) | |
114 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) | |
115 inc c | |
116 | |
117 # cleanup | |
118 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
119 DestroyPipeline(pipeline) | |
120 DestroyRenderData(renderdata) | |
121 | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
122 proc test_03_simple_descriptorset(nFrames: int, swapchain: var Swapchain) = |
1210 | 123 var renderdata = InitRenderData() |
124 | |
125 type | |
126 Material = object | |
127 baseColor: Vec3f | |
128 | |
129 Uniforms = object | |
130 material: GPUValue[Material, UniformBuffer] | |
1228 | 131 texture1: Image[TVec4[uint8]] |
1210 | 132 |
133 QuadShader = object | |
134 position {.VertexAttribute.}: Vec3f | |
135 fragmentColor {.Pass.}: Vec3f | |
136 uv {.Pass.}: Vec2f | |
137 outColor {.ShaderOutput.}: Vec4f | |
138 descriptorSets {.DescriptorSets.}: (Uniforms, ) | |
139 # code | |
140 vertexCode: string = """void main() { | |
141 fragmentColor = material.baseColor; | |
142 gl_Position = vec4(position, 1); | |
143 gl_Position.x += ((material.baseColor.b - 0.5) * 2) - 0.5; | |
144 uv = position.xy + 0.5; | |
145 }""" | |
146 fragmentCode: string = """void main() { | |
147 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}""" | |
148 QuadMesh = object | |
149 position: GPUArray[Vec3f, VertexBuffer] | |
150 indices: GPUArray[uint16, IndexBuffer] | |
151 | |
1212
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
152 let R = TVec4[uint8]([255'u8, 0'u8, 0'u8, 255'u8]) |
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
153 let G = TVec4[uint8]([0'u8, 255'u8, 0'u8, 255'u8]) |
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
154 let B = TVec4[uint8]([0'u8, 0'u8, 255'u8, 255'u8]) |
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
155 let W = TVec4[uint8]([255'u8, 255'u8, 255'u8, 255'u8]) |
1210 | 156 var |
157 quad = QuadMesh( | |
158 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), | |
159 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | |
160 ) | |
161 uniforms1 = asDescriptorSet( | |
162 Uniforms( | |
163 material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer), | |
1228 | 164 texture1: Image[TVec4[uint8]](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], interpolation: VK_FILTER_NEAREST), |
1210 | 165 ) |
166 ) | |
167 uniforms2 = asDescriptorSet( | |
168 Uniforms( | |
169 material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer), | |
1228 | 170 texture1: Image[TVec4[uint8]](width: 2, height: 2, data: @[R, G, B, W]), |
1212
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
171 ) |
1210 | 172 ) |
173 | |
174 AssignBuffers(renderdata, quad) | |
175 AssignBuffers(renderdata, uniforms1) | |
176 AssignBuffers(renderdata, uniforms2) | |
1228 | 177 UploadImages(renderdata, uniforms1) |
178 UploadImages(renderdata, uniforms2) | |
1210 | 179 renderdata.FlushAllMemory() |
180 | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
181 var pipeline = CreatePipeline[QuadShader](renderPass = swapchain.renderPass) |
1210 | 182 |
183 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | |
184 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) | |
185 | |
186 var c = 0 | |
187 while UpdateInputs() and c < nFrames: | |
188 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
189 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
1210 | 190 WithPipeline(commandbuffer, pipeline): |
191 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): | |
192 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | |
193 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF): | |
194 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | |
195 inc c | |
196 | |
197 # cleanup | |
198 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
199 DestroyPipeline(pipeline) | |
200 DestroyRenderData(renderdata) | |
201 | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
202 proc test_04_multiple_descriptorsets(nFrames: int, swapchain: var Swapchain) = |
1204 | 203 var renderdata = InitRenderData() |
1203 | 204 |
1204 | 205 type |
206 RenderSettings = object | |
1209 | 207 brigthness: float32 |
1210 | 208 Material = object |
209 baseColor: Vec3f | |
1209 | 210 ObjectSettings = object |
1210 | 211 scale: float32 |
212 materialIndex: uint32 | |
213 Constants = object | |
214 offset: Vec2f | |
215 | |
216 ConstSet = object | |
217 constants: GPUValue[Constants, UniformBuffer] | |
1209 | 218 MainSet = object |
219 renderSettings: GPUValue[RenderSettings, UniformBufferMapped] | |
1210 | 220 material: array[2, GPUValue[Material, UniformBuffer]] |
1228 | 221 texture1: array[2, Image[TVec1[uint8]]] |
1209 | 222 OtherSet = object |
1210 | 223 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped] |
1209 | 224 |
1204 | 225 QuadShader = object |
226 position {.VertexAttribute.}: Vec3f | |
227 fragmentColor {.Pass.}: Vec3f | |
1210 | 228 uv {.Pass.}: Vec2f |
1204 | 229 outColor {.ShaderOutput.}: Vec4f |
1210 | 230 descriptorSets {.DescriptorSets.}: (ConstSet, MainSet, OtherSet) |
1204 | 231 # code |
232 vertexCode: string = """void main() { | |
1210 | 233 fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness; |
234 gl_Position = vec4(position * objectSettings.scale, 1); | |
235 gl_Position.xy += constants.offset.xy; | |
236 gl_Position.x += material[objectSettings.materialIndex].baseColor.b - 0.5; | |
237 uv = position.xy + 0.5; | |
238 }""" | |
1204 | 239 fragmentCode: string = """void main() { |
1210 | 240 outColor = vec4(fragmentColor * texture(texture1[objectSettings.materialIndex], uv).rrr, 1); |
241 }""" | |
1204 | 242 QuadMesh = object |
243 position: GPUArray[Vec3f, VertexBuffer] | |
244 indices: GPUArray[uint16, IndexBuffer] | |
1203 | 245 |
1204 | 246 var quad = QuadMesh( |
1209 | 247 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), |
1204 | 248 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
1209 | 249 ) |
1210 | 250 var constset = asDescriptorSet( |
251 ConstSet( | |
252 constants: asGPUValue(Constants(offset: NewVec2f(-0.3, 0.2)), UniformBuffer), | |
253 ) | |
254 ) | |
255 let G = TVec1[uint8]([50'u8]) | |
256 let W = TVec1[uint8]([255'u8]) | |
257 var mainset = asDescriptorSet( | |
1209 | 258 MainSet( |
259 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped), | |
1210 | 260 material: [ |
261 asGPUValue(Material(baseColor: NewVec3f(1, 1, 0)), UniformBuffer), | |
262 asGPUValue(Material(baseColor: NewVec3f(1, 0, 1)), UniformBuffer), | |
263 ], | |
264 texture1: [ | |
1228 | 265 Image[TVec1[uint8]](width: 2, height: 2, data: @[W, G, G, W], interpolation: VK_FILTER_NEAREST), |
266 Image[TVec1[uint8]](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], interpolation: VK_FILTER_NEAREST), | |
1210 | 267 ], |
268 ), | |
269 ) | |
270 var otherset1 = asDescriptorSet( | |
271 OtherSet( | |
272 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped), | |
1209 | 273 ) |
1204 | 274 ) |
1210 | 275 var otherset2 = asDescriptorSet( |
1209 | 276 OtherSet( |
1210 | 277 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped), |
1209 | 278 ) |
1204 | 279 ) |
1209 | 280 |
1204 | 281 AssignBuffers(renderdata, quad) |
1210 | 282 AssignBuffers(renderdata, constset) |
283 AssignBuffers(renderdata, mainset) | |
284 AssignBuffers(renderdata, otherset1) | |
285 AssignBuffers(renderdata, otherset2) | |
1228 | 286 UploadImages(renderdata, mainset) |
1204 | 287 renderdata.FlushAllMemory() |
288 | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
289 var pipeline = CreatePipeline[QuadShader](renderPass = swapchain.renderPass) |
1204 | 290 |
1210 | 291 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset) |
292 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset) | |
293 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) | |
294 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
295 |
1204 | 296 var c = 0 |
297 while UpdateInputs() and c < nFrames: | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
298 TimeAndLog: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
299 WithNextFrame(swapchain, framebuffer, commandbuffer): |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
300 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
301 WithPipeline(commandbuffer, pipeline): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
302 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
303 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
304 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
305 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
1210 | 306 mainset.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32) |
307 otherset1.data.objectSettings.data.scale = 0.5 + (c.float32 / nFrames.float32) | |
308 UpdateGPUBuffer(mainset.data.renderSettings) | |
309 UpdateGPUBuffer(otherset1.data.objectSettings) | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
310 renderdata.FlushAllMemory() |
1204 | 311 inc c |
312 | |
313 # cleanup | |
1203 | 314 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1204 | 315 DestroyPipeline(pipeline) |
316 DestroyRenderData(renderdata) | |
317 | |
1231 | 318 proc test_05_cube(nFrames: int, swapchain: var Swapchain) = |
319 type | |
320 | |
321 UniformData = object | |
1232 | 322 mvp: Mat4 |
1231 | 323 Uniforms = object |
324 data: GPUValue[UniformData, UniformBufferMapped] | |
325 CubeShader = object | |
326 position {.VertexAttribute.}: Vec3f | |
327 color {.VertexAttribute.}: Vec4f | |
328 fragmentColor {.Pass.}: Vec4f | |
329 outColor {.ShaderOutput.}: Vec4f | |
330 descriptorSets {.DescriptorSets.}: (Uniforms, ) | |
331 # code | |
332 vertexCode = """void main() { | |
333 fragmentColor = color; | |
1232 | 334 gl_Position = vec4(position, 1) * data.mvp; |
1231 | 335 }""" |
336 fragmentCode = """void main() { | |
337 outColor = fragmentColor; | |
338 }""" | |
339 Mesh = object | |
340 position: GPUArray[Vec3f, VertexBuffer] | |
341 normals: GPUArray[Vec3f, VertexBuffer] | |
342 color: GPUArray[Vec4f, VertexBuffer] | |
343 | |
344 let quad = @[ | |
345 NewVec3f(-0.5, -0.5), NewVec3f(-0.5, +0.5), NewVec3f(+0.5, +0.5), | |
346 NewVec3f(+0.5, +0.5), NewVec3f(+0.5, -0.5), NewVec3f(-0.5, -0.5), | |
347 ] | |
1232 | 348 proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] = |
1231 | 349 for v in data: |
1232 | 350 result.add mat * v |
1231 | 351 |
352 var | |
353 vertices: seq[Vec3f] | |
354 colors: seq[Vec4f] | |
355 normals: seq[Vec3f] | |
356 | |
357 # front, red | |
358 vertices.add quad.transf(Translate(0, 0, -0.5)) | |
359 colors.add newSeqWith(6, NewVec4f(1, 0, 0, 1)) | |
360 normals.add newSeqWith(6, NewVec3f(0, 0, -1)) | |
361 | |
362 # back, cyan | |
363 vertices.add quad.transf(Rotate(PI, Y) * Translate(0, 0, -0.5)) | |
364 colors.add newSeqWith(6, NewVec4f(0, 1, 1, 1)) | |
365 normals.add newSeqWith(6, NewVec3f(0, 0, 1)) | |
366 | |
367 # right, green | |
368 vertices.add quad.transf(Rotate(PI / 2, Y) * Translate(0, 0, -0.5)) | |
369 colors.add newSeqWith(6, NewVec4f(0, 1, 0, 1)) | |
370 normals.add newSeqWith(6, NewVec3f(-1, 0, 0)) | |
371 | |
372 # left, magenta | |
373 vertices.add quad.transf(Rotate(-PI / 2, Y) * Translate(0, 0, -0.5)) | |
374 colors.add newSeqWith(6, NewVec4f(1, 0, 1, 1)) | |
375 normals.add newSeqWith(6, NewVec3f(1, 0, 0)) | |
376 | |
377 # bottom, blue | |
378 vertices.add quad.transf(Rotate(PI / 2, X) * Translate(0, 0, -0.5)) | |
379 colors.add newSeqWith(6, NewVec4f(0, 0, 1, 1)) | |
380 normals.add newSeqWith(6, NewVec3f(0, -1, 0)) | |
381 | |
382 # top, yellow | |
383 vertices.add quad.transf(Rotate(-PI / 2, X) * Translate(0, 0, -0.5)) | |
384 colors.add newSeqWith(6, NewVec4f(1, 1, 0, 1)) | |
385 normals.add newSeqWith(6, NewVec3f(0, 1, 0)) | |
386 | |
387 var renderdata = InitRenderData() | |
388 | |
389 var mesh = Mesh( | |
390 position: asGPUArray(vertices, VertexBuffer), | |
391 color: asGPUArray(colors, VertexBuffer), | |
392 normals: asGPUArray(normals, VertexBuffer), | |
393 ) | |
394 AssignBuffers(renderdata, mesh) | |
395 | |
396 var floor = Mesh( | |
397 position: asGPUArray(quad.transf(Scale(10, 10, 10) * Rotate(-PI / 2, X) * Translate(0, 0, 0.05)), VertexBuffer), | |
398 color: asGPUArray(newSeqWith(6, NewVec4f(0.1, 0.1, 0.1, 1)), VertexBuffer), | |
399 normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), | |
400 ) | |
401 AssignBuffers(renderdata, floor) | |
402 | |
403 var uniforms1 = asDescriptorSet( | |
404 Uniforms( | |
1232 | 405 data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped) |
1231 | 406 ) |
407 ) | |
408 AssignBuffers(renderdata, uniforms1) | |
409 | |
410 renderdata.FlushAllMemory() | |
411 | |
412 var pipeline = CreatePipeline[CubeShader](renderPass = swapchain.renderPass) | |
413 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | |
414 | |
415 var c = 0 | |
1232 | 416 var tStart = getMonoTime() |
417 var t = tStart | |
1231 | 418 while UpdateInputs() and c < nFrames: |
419 | |
1233
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
420 let tStartLoop = getMonoTime() - tStart |
1232 | 421 |
422 uniforms1.data.data.data.mvp = ( | |
423 Perspective(-PI / 2, GetAspectRatio(swapchain), 0.01, 100) * | |
424 Translate(0, 0, 2) * | |
425 Rotate(PI / 4, X) * | |
1233
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
426 Rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y) |
1232 | 427 ) |
1231 | 428 UpdateGPUBuffer(uniforms1.data.data, flush = true) |
429 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
430 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | |
431 WithPipeline(commandbuffer, pipeline): | |
432 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): | |
433 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) | |
434 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor) | |
1233
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
435 |
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
436 let tEndLoop = getMonoTime() - tStart |
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
437 let looptime = tEndLoop - tStartLoop |
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
438 let waitTime = 16_666 - looptime.inMicroseconds |
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
439 if waitTime > 0: |
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
440 echo "sleep ", waitTime / 1000 |
1cf698973dca
did: cleanup tests and old platform layer files
sam <sam@basx.dev>
parents:
1232
diff
changeset
|
441 sleep((waitTime / 1000).int) |
1231 | 442 inc c |
443 | |
444 # cleanup | |
445 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
446 DestroyPipeline(pipeline) | |
447 DestroyRenderData(renderdata) | |
448 | |
449 proc test_06_triangle_2pass(nFrames: int, depthBuffer: bool, samples: VkSampleCountFlagBits) = | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
450 var |
1230 | 451 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
452 swapchain = InitSwapchain(renderpass = presentRP).get() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
453 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
454 var renderdata = InitRenderData() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
455 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
456 type |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
457 Uniforms = object |
1228 | 458 frameTexture: Image[TVec4[uint8]] |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
459 TriangleShader = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
460 position {.VertexAttribute.}: Vec3f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
461 color {.VertexAttribute.}: Vec3f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
462 fragmentColor {.Pass.}: Vec3f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
463 outColor {.ShaderOutput.}: Vec4f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
464 # code |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
465 vertexCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
466 fragmentColor = color; |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
467 gl_Position = vec4(position, 1);}""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
468 fragmentCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
469 outColor = vec4(fragmentColor, 1);}""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
470 PresentShader = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
471 position {.VertexAttribute.}: Vec2f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
472 uv {.Pass.}: Vec2f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
473 outColor {.ShaderOutput.}: Vec4f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
474 descriptorSets {.DescriptorSets.}: (Uniforms, ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
475 # code |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
476 vertexCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
477 uv = ((position + 1) * 0.5) * vec2(1, -1); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
478 gl_Position = vec4(position, 0, 1);}""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
479 fragmentCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
480 vec2 uv1 = uv + vec2(0.001, 0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
481 vec2 uv2 = uv + vec2(0.001, -0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
482 vec2 uv3 = uv + vec2(-0.001, 0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
483 vec2 uv4 = uv + vec2(-0.001, -0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
484 outColor = ( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
485 texture(frameTexture, uv1) + |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
486 texture(frameTexture, uv2) + |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
487 texture(frameTexture, uv3) + |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
488 texture(frameTexture, uv4) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
489 ) / 4; |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
490 }""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
491 TriangleMesh = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
492 position: GPUArray[Vec3f, VertexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
493 color: GPUArray[Vec3f, VertexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
494 QuadMesh = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
495 position: GPUArray[Vec2f, VertexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
496 indices: GPUArray[uint16, IndexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
497 var mesh = TriangleMesh( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
498 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
499 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
500 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
501 var quad = QuadMesh( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
502 position: asGPUArray([NewVec2f(-1, -1), NewVec2f(-1, 1), NewVec2f(1, 1), NewVec2f(1, -1)], VertexBuffer), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
503 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
504 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
505 var uniforms1 = asDescriptorSet( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
506 Uniforms( |
1228 | 507 frameTexture: Image[TVec4[uint8]](width: swapchain.width, height: swapchain.height, isRenderTarget: true), |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
508 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
509 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
510 AssignBuffers(renderdata, mesh) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
511 AssignBuffers(renderdata, quad) |
1228 | 512 UploadImages(renderdata, uniforms1) |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
513 renderdata.FlushAllMemory() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
514 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
515 var |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
516 drawPipeline = CreatePipeline[TriangleShader](renderPass = offscreenRP) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
517 presentPipeline = CreatePipeline[PresentShader](renderPass = presentRP) |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
518 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
519 InitDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms1) |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
520 |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
521 # create depth buffer images (will not use the one in the swapchain |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
522 var |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
523 depthImage: VkImage |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
524 depthImageView: VkImageView |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
525 depthMemory: VkDeviceMemory |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
526 if offscreenRP.depthBuffer: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
527 depthImage = svkCreate2DImage( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
528 width = swapchain.width, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
529 height = swapchain.height, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
530 format = DEPTH_FORMAT, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
531 usage = [VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT], |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
532 samples = offscreenRP.samples, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
533 ) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
534 let requirements = svkGetImageMemoryRequirements(depthImage) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
535 depthMemory = svkAllocateMemory( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
536 requirements.size, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
537 BestMemory(mappable = false, filter = requirements.memoryTypes) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
538 ) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
539 checkVkResult vkBindImageMemory( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
540 vulkan.device, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
541 depthImage, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
542 depthMemory, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
543 0, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
544 ) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
545 depthImageView = svkCreate2DImageView( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
546 image = depthImage, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
547 format = DEPTH_FORMAT, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
548 aspect = VK_IMAGE_ASPECT_DEPTH_BIT |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
549 ) |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
550 |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
551 # create msaa images (will not use the one in the swapchain |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
552 var |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
553 msaaImage: VkImage |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
554 msaaImageView: VkImageView |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
555 msaaMemory: VkDeviceMemory |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
556 if offscreenRP.samples != VK_SAMPLE_COUNT_1_BIT: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
557 msaaImage = svkCreate2DImage( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
558 width = swapchain.width, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
559 height = swapchain.height, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
560 format = SURFACE_FORMAT, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
561 usage = [VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT], |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
562 samples = offscreenRP.samples, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
563 ) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
564 let requirements = svkGetImageMemoryRequirements(msaaImage) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
565 msaaMemory = svkAllocateMemory( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
566 requirements.size, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
567 BestMemory(mappable = false, filter = requirements.memoryTypes) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
568 ) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
569 checkVkResult vkBindImageMemory( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
570 vulkan.device, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
571 msaaImage, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
572 msaaMemory, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
573 0, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
574 ) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
575 msaaImageView = svkCreate2DImageView(image = msaaImage, format = SURFACE_FORMAT) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
576 |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
577 var attachments: seq[VkImageView] |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
578 if offscreenRP.samples == VK_SAMPLE_COUNT_1_BIT: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
579 if offscreenRP.depthBuffer: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
580 attachments = @[uniforms1.data.frameTexture.imageview, depthImageView] |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
581 else: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
582 attachments = @[uniforms1.data.frameTexture.imageview] |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
583 else: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
584 if offscreenRP.depthBuffer: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
585 attachments = @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview] |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
586 else: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
587 attachments = @[msaaImageView, uniforms1.data.frameTexture.imageview] |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
588 echo attachments |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
589 var offscreenFB = svkCreateFramebuffer( |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
590 offscreenRP.vk, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
591 swapchain.width, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
592 swapchain.height, |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
593 attachments |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
594 ) |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
595 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
596 var c = 0 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
597 while UpdateInputs() and c < nFrames: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
598 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
599 TimeAndLog: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
600 WithNextFrame(swapchain, framebuffer, commandbuffer): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
601 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
602 WithRenderPass(offscreenRP, offscreenFB, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
603 WithPipeline(commandbuffer, drawPipeline): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
604 Render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
605 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
606 WithRenderPass(presentRP, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
607 WithPipeline(commandbuffer, presentPipeline): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
608 WithBind(commandbuffer, (uniforms1, ), presentPipeline, swapchain.currentFiF): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
609 Render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
610 inc c |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
611 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
612 # cleanup |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
613 checkVkResult vkDeviceWaitIdle(vulkan.device) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
614 DestroyPipeline(presentPipeline) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
615 DestroyPipeline(drawPipeline) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
616 DestroyRenderData(renderdata) |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
617 if depthImage.Valid: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
618 vkDestroyImageView(vulkan.device, depthImageView, nil) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
619 vkDestroyImage(vulkan.device, depthImage, nil) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
620 vkFreeMemory(vulkan.device, depthMemory, nil) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
621 if msaaImage.Valid: |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
622 vkDestroyImageView(vulkan.device, msaaImageView, nil) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
623 vkDestroyImage(vulkan.device, msaaImage, nil) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
624 vkFreeMemory(vulkan.device, msaaMemory, nil) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
625 vkDestroyRenderPass(vulkan.device, offscreenRP.vk, nil) |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
626 vkDestroyRenderPass(vulkan.device, presentRP.vk, nil) |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
627 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
628 DestroySwapchain(swapchain) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
629 |
1204 | 630 when isMainModule: |
1231 | 631 var nFrames = 3000 |
1204 | 632 InitVulkan() |
633 | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
634 var mainRenderpass: RenderPass |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
635 var renderPasses = [ |
1231 | 636 # (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), |
637 # (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
638 (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), |
1231 | 639 # (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
640 ] |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
641 |
1204 | 642 # test normal |
1230 | 643 for i, (depthBuffer, samples) in renderPasses: |
644 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) | |
645 var swapchain = InitSwapchain(renderpass = renderpass).get() | |
1204 | 646 |
1231 | 647 #[ |
1230 | 648 # tests a simple triangle with minimalistic shader and vertex format |
649 test_01_triangle(nFrames, swapchain) | |
650 | |
651 # tests instanced triangles and quads, mixing meshes and instances | |
652 test_02_triangle_quad_instanced(nFrames, swapchain) | |
1210 | 653 |
1230 | 654 # teste descriptor sets |
655 test_03_simple_descriptorset(nFrames, swapchain) | |
1204 | 656 |
1230 | 657 # tests multiple descriptor sets and arrays |
658 test_04_multiple_descriptorsets(nFrames, swapchain) | |
1231 | 659 ]# |
660 | |
661 # rotating cube | |
1232 | 662 test_05_cube(999999999, swapchain) |
1204 | 663 |
1230 | 664 checkVkResult vkDeviceWaitIdle(vulkan.device) |
665 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | |
666 DestroySwapchain(swapchain) | |
1204 | 667 |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
668 # test multiple render passes |
1231 | 669 # for i, (depthBuffer, samples) in renderPasses: |
670 # test_06_triangle_2pass(nFrames, depthBuffer, samples) | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
671 |
1203 | 672 DestroyVulkan() |