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