Mercurial > games > semicongine
annotate tests/test_rendering.nim @ 1214:04e446a7eb2b compiletime-tests
add: multipass renderer, finish tets for now
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 20:11:55 +0700 |
parents | f9919ea98e5b |
children | 55896320c8bf |
rev | line source |
---|---|
1203 | 1 import std/options |
1204 | 2 import std/random |
3 | |
1203 | 4 import ../semicongine |
5 | |
6 var | |
7 mainRenderpass: VkRenderPass | |
8 swapchain: Swapchain | |
9 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
10 proc test_01_triangle(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) = |
1203 | 11 var renderdata = InitRenderData() |
12 | |
13 type | |
14 TrianglShader = object | |
15 position {.VertexAttribute.}: Vec3f | |
16 color {.VertexAttribute.}: Vec3f | |
17 fragmentColor {.Pass.}: Vec3f | |
18 outColor {.ShaderOutput.}: Vec4f | |
19 # code | |
20 vertexCode: string = """void main() { | |
21 fragmentColor = color; | |
22 gl_Position = vec4(position, 1);}""" | |
23 fragmentCode: string = """void main() { | |
24 outColor = vec4(fragmentColor, 1);}""" | |
25 TriangleMesh = object | |
26 position: GPUArray[Vec3f, VertexBuffer] | |
27 color: GPUArray[Vec3f, VertexBuffer] | |
28 var mesh = TriangleMesh( | |
1204 | 29 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), |
1203 | 30 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), |
31 ) | |
1204 | 32 AssignBuffers(renderdata, mesh) |
33 renderdata.FlushAllMemory() | |
1203 | 34 |
35 var | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
36 pipeline = CreatePipeline[TrianglShader](renderPass = renderPass, samples = samples) |
1203 | 37 |
1204 | 38 var c = 0 |
39 while UpdateInputs() and c < nFrames: | |
1203 | 40 WithNextFrame(swapchain, framebuffer, commandbuffer): |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
41 WithRenderPass(renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
1203 | 42 WithPipeline(commandbuffer, pipeline): |
1204 | 43 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) |
44 inc c | |
1203 | 45 |
46 # cleanup | |
1204 | 47 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1203 | 48 DestroyPipeline(pipeline) |
49 DestroyRenderData(renderdata) | |
50 | |
51 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
52 proc test_02_triangle_quad_instanced(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) = |
1204 | 53 var renderdata = InitRenderData() |
54 | |
55 type | |
56 SomeShader = object | |
57 position {.VertexAttribute.}: Vec3f | |
58 color {.VertexAttribute.}: Vec3f | |
59 pos {.InstanceAttribute.}: Vec3f | |
60 scale {.InstanceAttribute.}: float32 | |
61 fragmentColor {.Pass.}: Vec3f | |
62 outColor {.ShaderOutput.}: Vec4f | |
63 # code | |
64 vertexCode: string = """void main() { | |
65 fragmentColor = color; | |
66 gl_Position = vec4((position * scale) + pos, 1);}""" | |
67 fragmentCode: string = """void main() { | |
68 outColor = vec4(fragmentColor, 1);}""" | |
69 TriangleMesh = object | |
70 position: GPUArray[Vec3f, VertexBuffer] | |
71 color: GPUArray[Vec3f, VertexBuffer] | |
72 QuadMesh = object | |
73 position: GPUArray[Vec3f, VertexBuffer] | |
74 color: GPUArray[Vec3f, VertexBuffer] | |
75 indices: GPUArray[uint16, IndexBuffer] | |
76 Instances = object | |
77 pos: GPUArray[Vec3f, VertexBuffer] | |
78 scale: GPUArray[float32, VertexBuffer] | |
79 var tri = TriangleMesh( | |
80 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), | |
81 color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), | |
82 ) | |
83 var quad = QuadMesh( | |
84 position: asGPUArray([NewVec3f(-0.3, -0.3), NewVec3f(-0.3, 0.3), NewVec3f(0.3, 0.3), NewVec3f(0.3, -0.3)], VertexBuffer), | |
85 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | |
86 color: asGPUArray([NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1)], VertexBuffer), | |
87 ) | |
88 | |
89 var instancesA: Instances | |
90 for n in 1..100: | |
91 instancesA.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32)) | |
92 instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32) | |
93 var instancesB: Instances | |
94 for n in 1..100: | |
95 instancesB.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32)) | |
96 instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) | |
97 | |
98 AssignBuffers(renderdata, tri) | |
99 AssignBuffers(renderdata, quad) | |
100 AssignBuffers(renderdata, instancesA) | |
101 AssignBuffers(renderdata, instancesB) | |
102 renderdata.FlushAllMemory() | |
103 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
104 var pipeline = CreatePipeline[SomeShader](renderPass = renderPass, samples = samples) |
1204 | 105 |
106 var c = 0 | |
107 while UpdateInputs() and c < nFrames: | |
108 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
109 WithRenderPass(renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
1204 | 110 WithPipeline(commandbuffer, pipeline): |
111 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) | |
112 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) | |
113 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) | |
114 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) | |
115 inc c | |
116 | |
117 # cleanup | |
118 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
119 DestroyPipeline(pipeline) | |
120 DestroyRenderData(renderdata) | |
121 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
122 proc test_03_simple_descriptorset(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) = |
1210 | 123 var renderdata = InitRenderData() |
124 | |
125 type | |
126 Material = object | |
127 baseColor: Vec3f | |
128 | |
129 Uniforms = object | |
130 material: GPUValue[Material, UniformBuffer] | |
1212
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
131 texture1: Texture[TVec4[uint8]] |
1210 | 132 |
133 QuadShader = object | |
134 position {.VertexAttribute.}: Vec3f | |
135 fragmentColor {.Pass.}: Vec3f | |
136 uv {.Pass.}: Vec2f | |
137 outColor {.ShaderOutput.}: Vec4f | |
138 descriptorSets {.DescriptorSets.}: (Uniforms, ) | |
139 # code | |
140 vertexCode: string = """void main() { | |
141 fragmentColor = material.baseColor; | |
142 gl_Position = vec4(position, 1); | |
143 gl_Position.x += ((material.baseColor.b - 0.5) * 2) - 0.5; | |
144 uv = position.xy + 0.5; | |
145 }""" | |
146 fragmentCode: string = """void main() { | |
147 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}""" | |
148 QuadMesh = object | |
149 position: GPUArray[Vec3f, VertexBuffer] | |
150 indices: GPUArray[uint16, IndexBuffer] | |
151 | |
1212
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
152 let R = TVec4[uint8]([255'u8, 0'u8, 0'u8, 255'u8]) |
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
153 let G = TVec4[uint8]([0'u8, 255'u8, 0'u8, 255'u8]) |
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
154 let B = TVec4[uint8]([0'u8, 0'u8, 255'u8, 255'u8]) |
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
155 let W = TVec4[uint8]([255'u8, 255'u8, 255'u8, 255'u8]) |
1210 | 156 var |
157 quad = QuadMesh( | |
158 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), | |
159 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), | |
160 ) | |
161 uniforms1 = asDescriptorSet( | |
162 Uniforms( | |
163 material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer), | |
1212
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
164 texture1: Texture[TVec4[uint8]](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], interpolation: VK_FILTER_NEAREST), |
1210 | 165 ) |
166 ) | |
167 uniforms2 = asDescriptorSet( | |
168 Uniforms( | |
169 material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer), | |
1212
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
170 texture1: Texture[TVec4[uint8]](width: 2, height: 2, data: @[R, G, B, W]), |
518a952eccbf
did: increase texture format compatability
sam <sam@basx.dev>
parents:
1211
diff
changeset
|
171 ) |
1210 | 172 ) |
173 | |
174 AssignBuffers(renderdata, quad) | |
175 AssignBuffers(renderdata, uniforms1) | |
176 AssignBuffers(renderdata, uniforms2) | |
177 UploadTextures(renderdata, uniforms1) | |
178 UploadTextures(renderdata, uniforms2) | |
179 renderdata.FlushAllMemory() | |
180 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
181 var pipeline = CreatePipeline[QuadShader](renderPass = renderPass, samples = samples) |
1210 | 182 |
183 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) | |
184 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2) | |
185 | |
186 var c = 0 | |
187 while UpdateInputs() and c < nFrames: | |
188 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
189 WithRenderPass(renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
1210 | 190 WithPipeline(commandbuffer, pipeline): |
191 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF): | |
192 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | |
193 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF): | |
194 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) | |
195 inc c | |
196 | |
197 # cleanup | |
198 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
199 DestroyPipeline(pipeline) | |
200 DestroyRenderData(renderdata) | |
201 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
202 proc test_04_multiple_descriptorsets(nFrames: int, renderPass: VkRenderPass, samples = VK_SAMPLE_COUNT_1_BIT) = |
1204 | 203 var renderdata = InitRenderData() |
1203 | 204 |
1204 | 205 type |
206 RenderSettings = object | |
1209 | 207 brigthness: float32 |
1210 | 208 Material = object |
209 baseColor: Vec3f | |
1209 | 210 ObjectSettings = object |
1210 | 211 scale: float32 |
212 materialIndex: uint32 | |
213 Constants = object | |
214 offset: Vec2f | |
215 | |
216 ConstSet = object | |
217 constants: GPUValue[Constants, UniformBuffer] | |
1209 | 218 MainSet = object |
219 renderSettings: GPUValue[RenderSettings, UniformBufferMapped] | |
1210 | 220 material: array[2, GPUValue[Material, UniformBuffer]] |
221 texture1: array[2, Texture[TVec1[uint8]]] | |
1209 | 222 OtherSet = object |
1210 | 223 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped] |
1209 | 224 |
1204 | 225 QuadShader = object |
226 position {.VertexAttribute.}: Vec3f | |
227 fragmentColor {.Pass.}: Vec3f | |
1210 | 228 uv {.Pass.}: Vec2f |
1204 | 229 outColor {.ShaderOutput.}: Vec4f |
1210 | 230 descriptorSets {.DescriptorSets.}: (ConstSet, MainSet, OtherSet) |
1204 | 231 # code |
232 vertexCode: string = """void main() { | |
1210 | 233 fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness; |
234 gl_Position = vec4(position * objectSettings.scale, 1); | |
235 gl_Position.xy += constants.offset.xy; | |
236 gl_Position.x += material[objectSettings.materialIndex].baseColor.b - 0.5; | |
237 uv = position.xy + 0.5; | |
238 }""" | |
1204 | 239 fragmentCode: string = """void main() { |
1210 | 240 outColor = vec4(fragmentColor * texture(texture1[objectSettings.materialIndex], uv).rrr, 1); |
241 }""" | |
1204 | 242 QuadMesh = object |
243 position: GPUArray[Vec3f, VertexBuffer] | |
244 indices: GPUArray[uint16, IndexBuffer] | |
1203 | 245 |
1204 | 246 var quad = QuadMesh( |
1209 | 247 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), |
1204 | 248 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), |
1209 | 249 ) |
1210 | 250 var constset = asDescriptorSet( |
251 ConstSet( | |
252 constants: asGPUValue(Constants(offset: NewVec2f(-0.3, 0.2)), UniformBuffer), | |
253 ) | |
254 ) | |
255 let G = TVec1[uint8]([50'u8]) | |
256 let W = TVec1[uint8]([255'u8]) | |
257 var mainset = asDescriptorSet( | |
1209 | 258 MainSet( |
259 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped), | |
1210 | 260 material: [ |
261 asGPUValue(Material(baseColor: NewVec3f(1, 1, 0)), UniformBuffer), | |
262 asGPUValue(Material(baseColor: NewVec3f(1, 0, 1)), UniformBuffer), | |
263 ], | |
264 texture1: [ | |
265 Texture[TVec1[uint8]](width: 2, height: 2, data: @[W, G, G, W], interpolation: VK_FILTER_NEAREST), | |
266 Texture[TVec1[uint8]](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], interpolation: VK_FILTER_NEAREST), | |
267 ], | |
268 ), | |
269 ) | |
270 var otherset1 = asDescriptorSet( | |
271 OtherSet( | |
272 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped), | |
1209 | 273 ) |
1204 | 274 ) |
1210 | 275 var otherset2 = asDescriptorSet( |
1209 | 276 OtherSet( |
1210 | 277 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped), |
1209 | 278 ) |
1204 | 279 ) |
1209 | 280 |
1204 | 281 AssignBuffers(renderdata, quad) |
1210 | 282 AssignBuffers(renderdata, constset) |
283 AssignBuffers(renderdata, mainset) | |
284 AssignBuffers(renderdata, otherset1) | |
285 AssignBuffers(renderdata, otherset2) | |
286 UploadTextures(renderdata, mainset) | |
1204 | 287 renderdata.FlushAllMemory() |
288 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
289 var pipeline = CreatePipeline[QuadShader](renderPass = renderPass, samples = samples) |
1204 | 290 |
1210 | 291 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], constset) |
292 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[1], mainset) | |
293 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset1) | |
294 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[2], otherset2) | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
295 |
1204 | 296 var c = 0 |
297 while UpdateInputs() and c < nFrames: | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
298 TimeAndLog: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
299 WithNextFrame(swapchain, framebuffer, commandbuffer): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
300 WithRenderPass(renderPass, 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
|
301 WithPipeline(commandbuffer, pipeline): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
302 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
303 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
304 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
305 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) |
1210 | 306 mainset.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32) |
307 otherset1.data.objectSettings.data.scale = 0.5 + (c.float32 / nFrames.float32) | |
308 UpdateGPUBuffer(mainset.data.renderSettings) | |
309 UpdateGPUBuffer(otherset1.data.objectSettings) | |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
310 renderdata.FlushAllMemory() |
1204 | 311 inc c |
312 | |
313 # cleanup | |
1203 | 314 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1204 | 315 DestroyPipeline(pipeline) |
316 DestroyRenderData(renderdata) | |
317 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
318 proc test_05_triangle_2pass(nFrames: int, samples = VK_SAMPLE_COUNT_1_BIT) = |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
319 var |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
320 (offscreenRP, presentRP) = CreateIndirectPresentationRenderPass() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
321 swapchain = InitSwapchain(renderpass = presentRP).get() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
322 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
323 var renderdata = InitRenderData() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
324 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
325 type |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
326 Uniforms = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
327 frameTexture: Texture[TVec4[uint8]] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
328 TriangleShader = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
329 position {.VertexAttribute.}: Vec3f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
330 color {.VertexAttribute.}: Vec3f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
331 fragmentColor {.Pass.}: Vec3f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
332 outColor {.ShaderOutput.}: Vec4f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
333 # code |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
334 vertexCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
335 fragmentColor = color; |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
336 gl_Position = vec4(position, 1);}""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
337 fragmentCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
338 outColor = vec4(fragmentColor, 1);}""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
339 PresentShader = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
340 position {.VertexAttribute.}: Vec2f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
341 uv {.Pass.}: Vec2f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
342 outColor {.ShaderOutput.}: Vec4f |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
343 descriptorSets {.DescriptorSets.}: (Uniforms, ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
344 # code |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
345 vertexCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
346 uv = ((position + 1) * 0.5) * vec2(1, -1); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
347 gl_Position = vec4(position, 0, 1);}""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
348 fragmentCode: string = """void main() { |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
349 vec2 uv1 = uv + vec2(0.001, 0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
350 vec2 uv2 = uv + vec2(0.001, -0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
351 vec2 uv3 = uv + vec2(-0.001, 0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
352 vec2 uv4 = uv + vec2(-0.001, -0.001); |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
353 outColor = ( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
354 texture(frameTexture, uv1) + |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
355 texture(frameTexture, uv2) + |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
356 texture(frameTexture, uv3) + |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
357 texture(frameTexture, uv4) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
358 ) / 4; |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
359 }""" |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
360 TriangleMesh = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
361 position: GPUArray[Vec3f, VertexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
362 color: GPUArray[Vec3f, VertexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
363 QuadMesh = object |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
364 position: GPUArray[Vec2f, VertexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
365 indices: GPUArray[uint16, IndexBuffer] |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
366 var mesh = TriangleMesh( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
367 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
|
368 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
|
369 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
370 var quad = QuadMesh( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
371 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
|
372 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
|
373 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
374 var uniforms1 = asDescriptorSet( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
375 Uniforms( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
376 frameTexture: Texture[TVec4[uint8]](width: swapchain.width, height: swapchain.height, isRenderTarget: true), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
377 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
378 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
379 var uniforms2 = asDescriptorSet( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
380 Uniforms( |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
381 frameTexture: Texture[TVec4[uint8]](width: swapchain.width, height: swapchain.height, isRenderTarget: true), |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
382 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
383 ) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
384 AssignBuffers(renderdata, mesh) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
385 AssignBuffers(renderdata, quad) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
386 UploadTextures(renderdata, uniforms1) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
387 UploadTextures(renderdata, uniforms2) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
388 renderdata.FlushAllMemory() |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
389 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
390 var |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
391 drawPipeline = CreatePipeline[TriangleShader](renderPass = offscreenRP, samples = samples) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
392 presentPipeline = CreatePipeline[PresentShader](renderPass = presentRP, samples = samples) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
393 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
394 InitDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms1) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
395 InitDescriptorSet(renderdata, presentPipeline.descriptorSetLayouts[0], uniforms2) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
396 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
397 var offscreenFB = svkCreateFramebuffer(offscreenRP, swapchain.width, swapchain.height, [uniforms1.data.frameTexture.imageview]) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
398 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
399 var c = 0 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
400 while UpdateInputs() and c < nFrames: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
401 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
402 TimeAndLog: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
403 WithNextFrame(swapchain, framebuffer, commandbuffer): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
404 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
405 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
|
406 WithPipeline(commandbuffer, drawPipeline): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
407 Render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
408 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
409 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
|
410 WithPipeline(commandbuffer, presentPipeline): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
411 WithBind(commandbuffer, (uniforms1, ), presentPipeline, swapchain.currentFiF): |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
412 Render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
413 inc c |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
414 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
415 # cleanup |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
416 checkVkResult vkDeviceWaitIdle(vulkan.device) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
417 DestroyPipeline(presentPipeline) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
418 DestroyPipeline(drawPipeline) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
419 DestroyRenderData(renderdata) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
420 vkDestroyRenderPass(vulkan.device, offscreenRP, nil) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
421 vkDestroyRenderPass(vulkan.device, presentRP, nil) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
422 vkDestroyFramebuffer(vulkan.device, offscreenFB, nil) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
423 DestroySwapchain(swapchain) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
424 |
1204 | 425 when isMainModule: |
1211 | 426 var nFrames = 2000 |
1204 | 427 InitVulkan() |
428 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
429 |
1204 | 430 # test normal |
431 block: | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
432 mainRenderpass = CreateDirectPresentationRenderPass() |
1204 | 433 swapchain = InitSwapchain(renderpass = mainRenderpass).get() |
434 | |
435 # tests a simple triangle with minimalistic shader and vertex format | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
436 test_01_triangle(nFrames, renderPass = mainRenderpass) |
1204 | 437 |
438 # tests instanced triangles and quads, mixing meshes and instances | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
439 test_02_triangle_quad_instanced(nFrames, renderPass = mainRenderpass) |
1204 | 440 |
1211 | 441 # teste descriptor sets |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
442 test_03_simple_descriptorset(nFrames, renderPass = mainRenderpass) |
1210 | 443 |
1211 | 444 # tests multiple descriptor sets and arrays |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
445 test_04_multiple_descriptorsets(nFrames, renderPass = mainRenderpass) |
1204 | 446 |
447 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
448 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) | |
449 DestroySwapchain(swapchain) | |
450 | |
451 # test MSAA | |
452 block: | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
453 mainRenderpass = CreateDirectPresentationRenderPass(samples = VK_SAMPLE_COUNT_4_BIT) |
1204 | 454 swapchain = InitSwapchain(renderpass = mainRenderpass, samples = VK_SAMPLE_COUNT_4_BIT).get() |
455 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
456 test_01_triangle(nFrames, renderPass = mainRenderpass, VK_SAMPLE_COUNT_4_BIT) |
1204 | 457 |
458 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
459 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) | |
460 DestroySwapchain(swapchain) | |
461 | |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
462 # test multiple render passes |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
463 block: |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
464 test_05_triangle_2pass(999999999) |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1213
diff
changeset
|
465 |
1203 | 466 DestroyVulkan() |