comparison tests/test_rendering.nim @ 1210:570ac1620fb6 compiletime-tests

fix: make uniform-block-arrays working
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 00:30:49 +0700
parents e177336cac3f
children d9799f74f5a7
comparison
equal deleted inserted replaced
1209:e177336cac3f 1210:570ac1620fb6
117 # cleanup 117 # cleanup
118 checkVkResult vkDeviceWaitIdle(vulkan.device) 118 checkVkResult vkDeviceWaitIdle(vulkan.device)
119 DestroyPipeline(pipeline) 119 DestroyPipeline(pipeline)
120 DestroyRenderData(renderdata) 120 DestroyRenderData(renderdata)
121 121
122 proc test_03_global_descriptorset(nFrames: int) = 122 proc test_03_simple_descriptorset(nFrames: int) =
123 var renderdata = InitRenderData() 123 var renderdata = InitRenderData()
124 124
125 type 125 type
126 RenderSettings = object 126 Material = object
127 brigthness: float32
128 ObjectSettings = object
129 baseColor: Vec3f 127 baseColor: Vec3f
130 MainSet = object 128
131 renderSettings: GPUValue[RenderSettings, UniformBufferMapped] 129 Uniforms = object
132 materialSettings: GPUValue[ObjectSettings, UniformBuffer] 130 material: GPUValue[Material, UniformBuffer]
133 OtherSet = object 131 texture1: Texture[TVec3[uint8]]
134 # TODO
135 132
136 QuadShader = object 133 QuadShader = object
137 position {.VertexAttribute.}: Vec3f 134 position {.VertexAttribute.}: Vec3f
138 fragmentColor {.Pass.}: Vec3f 135 fragmentColor {.Pass.}: Vec3f
136 uv {.Pass.}: Vec2f
139 outColor {.ShaderOutput.}: Vec4f 137 outColor {.ShaderOutput.}: Vec4f
140 descriptorSets {.DescriptorSets.}: (MainSet, OtherSet) 138 descriptorSets {.DescriptorSets.}: (Uniforms, )
141 # code 139 # code
142 vertexCode: string = """void main() { 140 vertexCode: string = """void main() {
143 fragmentColor = materialSettings.baseColor * renderSettings.brigthness; 141 fragmentColor = material.baseColor;
144 gl_Position = vec4(position, 1);}""" 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 }"""
145 fragmentCode: string = """void main() { 146 fragmentCode: string = """void main() {
146 outColor = vec4(fragmentColor, 1);}""" 147 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}"""
147 QuadMesh = object 148 QuadMesh = object
148 position: GPUArray[Vec3f, VertexBuffer] 149 position: GPUArray[Vec3f, VertexBuffer]
149 indices: GPUArray[uint16, IndexBuffer] 150 indices: GPUArray[uint16, IndexBuffer]
150 151
151 var quad = QuadMesh( 152 let R = TVec3[uint8]([255'u8, 0'u8, 0'u8])
152 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), 153 let G = TVec3[uint8]([0'u8, 255'u8, 0'u8])
153 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), 154 let B = TVec3[uint8]([0'u8, 0'u8, 255'u8])
154 ) 155 let W = TVec3[uint8]([255'u8, 255'u8, 255'u8])
155 var mainSet = asDescriptorSet( 156 var
156 MainSet( 157 quad = QuadMesh(
157 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped), 158 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
158 materialSettings: asGPUValue(ObjectSettings(baseColor: NewVec3f(1, 1, 0)), UniformBuffer), 159 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
159 ) 160 )
160 ) 161 uniforms1 = asDescriptorSet(
161 var settings = asDescriptorSet( 162 Uniforms(
162 OtherSet( 163 material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer),
163 # TODO 164 texture1: Texture[TVec3[uint8]](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], interpolation: VK_FILTER_NEAREST),
164 ) 165 )
165 ) 166 )
167 uniforms2 = asDescriptorSet(
168 Uniforms(
169 material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer),
170 texture1: Texture[TVec3[uint8]](width: 2, height: 2, data: @[R, G, B, W]),
171 )
172 )
166 173
167 AssignBuffers(renderdata, quad) 174 AssignBuffers(renderdata, quad)
168 AssignBuffers(renderdata, mainSet) 175 AssignBuffers(renderdata, uniforms1)
176 AssignBuffers(renderdata, uniforms2)
177 UploadTextures(renderdata, uniforms1)
178 UploadTextures(renderdata, uniforms2)
169 renderdata.FlushAllMemory() 179 renderdata.FlushAllMemory()
170 180
171 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples) 181 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples)
172 182
173 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], mainSet) 183 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
184 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms2)
174 185
175 var c = 0 186 var c = 0
176 while UpdateInputs() and c < nFrames: 187 while UpdateInputs() and c < nFrames:
177 WithNextFrame(swapchain, framebuffer, commandbuffer): 188 WithNextFrame(swapchain, framebuffer, commandbuffer):
178 WithBind(commandbuffer, (mainSet, ), pipeline, swapchain.currentFiF): 189 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
179 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): 190 WithPipeline(commandbuffer, pipeline):
180 WithPipeline(commandbuffer, pipeline): 191 WithBind(commandbuffer, (uniforms1, ), pipeline, swapchain.currentFiF):
181 Render(commandbuffer = commandbuffer, pipeline = pipeline, firstSet = mainSet, mesh = quad) 192 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
182 mainSet.data.renderSettings.data.brigthness = (c.float32 / nFrames.float32) 193 WithBind(commandbuffer, (uniforms2, ), pipeline, swapchain.currentFiF):
183 UpdateGPUBuffer(mainSet.data.renderSettings) 194 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
184 renderdata.FlushAllMemory()
185 inc c 195 inc c
186 196
187 # cleanup 197 # cleanup
188 checkVkResult vkDeviceWaitIdle(vulkan.device) 198 checkVkResult vkDeviceWaitIdle(vulkan.device)
189 DestroyPipeline(pipeline) 199 DestroyPipeline(pipeline)
190 DestroyRenderData(renderdata) 200 DestroyRenderData(renderdata)
191 201
202 proc test_04_multiple_descriptorsets(nFrames: int) =
203 var renderdata = InitRenderData()
204
205 type
206 RenderSettings = object
207 brigthness: float32
208 Material = object
209 baseColor: Vec3f
210 ObjectSettings = object
211 scale: float32
212 materialIndex: uint32
213 Constants = object
214 offset: Vec2f
215
216 ConstSet = object
217 constants: GPUValue[Constants, UniformBuffer]
218 MainSet = object
219 renderSettings: GPUValue[RenderSettings, UniformBufferMapped]
220 material: array[2, GPUValue[Material, UniformBuffer]]
221 texture1: array[2, Texture[TVec1[uint8]]]
222 OtherSet = object
223 objectSettings: GPUValue[ObjectSettings, UniformBufferMapped]
224
225 QuadShader = object
226 position {.VertexAttribute.}: Vec3f
227 fragmentColor {.Pass.}: Vec3f
228 uv {.Pass.}: Vec2f
229 outColor {.ShaderOutput.}: Vec4f
230 descriptorSets {.DescriptorSets.}: (ConstSet, MainSet, OtherSet)
231 # code
232 vertexCode: string = """void main() {
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 }"""
239 fragmentCode: string = """void main() {
240 outColor = vec4(fragmentColor * texture(texture1[objectSettings.materialIndex], uv).rrr, 1);
241 }"""
242 QuadMesh = object
243 position: GPUArray[Vec3f, VertexBuffer]
244 indices: GPUArray[uint16, IndexBuffer]
245
246 var quad = QuadMesh(
247 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
248 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
249 )
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(
258 MainSet(
259 renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped),
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),
273 )
274 )
275 var otherset2 = asDescriptorSet(
276 OtherSet(
277 objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped),
278 )
279 )
280
281 AssignBuffers(renderdata, quad)
282 AssignBuffers(renderdata, constset)
283 AssignBuffers(renderdata, mainset)
284 AssignBuffers(renderdata, otherset1)
285 AssignBuffers(renderdata, otherset2)
286 UploadTextures(renderdata, mainset)
287 renderdata.FlushAllMemory()
288
289 var pipeline = CreatePipeline[QuadShader](renderPass = mainRenderpass, samples = swapchain.samples)
290
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)
295
296 var c = 0
297 while UpdateInputs() and c < nFrames:
298 WithNextFrame(swapchain, framebuffer, commandbuffer):
299 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
300 WithPipeline(commandbuffer, pipeline):
301 WithBind(commandbuffer, (constset, mainset, otherset1), pipeline, swapchain.currentFiF):
302 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
303 WithBind(commandbuffer, (constset, mainset, otherset2), pipeline, swapchain.currentFiF):
304 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad)
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)
309 renderdata.FlushAllMemory()
310 inc c
311
312 # cleanup
313 checkVkResult vkDeviceWaitIdle(vulkan.device)
314 DestroyPipeline(pipeline)
315 DestroyRenderData(renderdata)
316
192 when isMainModule: 317 when isMainModule:
193 var nFrames = 5000 318 var nFrames = 5000
194 InitVulkan() 319 InitVulkan()
195 320
196 # test normal 321 # test normal
202 # test_01_triangle(nFrames) 327 # test_01_triangle(nFrames)
203 328
204 # tests instanced triangles and quads, mixing meshes and instances 329 # tests instanced triangles and quads, mixing meshes and instances
205 # test_02_triangle_quad_instanced(nFrames) 330 # test_02_triangle_quad_instanced(nFrames)
206 331
332 # test_03_simple_descriptorset(nFrames)
333
207 # tests 334 # tests
208 test_03_global_descriptorset(nFrames) 335 test_04_multiple_descriptorsets(nFrames)
209 336
210 checkVkResult vkDeviceWaitIdle(vulkan.device) 337 checkVkResult vkDeviceWaitIdle(vulkan.device)
211 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) 338 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
212 DestroySwapchain(swapchain) 339 DestroySwapchain(swapchain)
213 340