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