comparison tests/test_rendering.nim @ 1252:01e9f41d35b1

add:support for push constants
author sam <sam@basx.dev>
date Fri, 26 Jul 2024 23:04:01 +0700
parents e8b3dc80e48e
children 4cf9872f7bb6
comparison
equal deleted inserted replaced
1251:3f98ad20a9d3 1252:01e9f41d35b1
9 9
10 proc test_01_triangle(time: float32) = 10 proc test_01_triangle(time: float32) =
11 var renderdata = InitRenderData() 11 var renderdata = InitRenderData()
12 12
13 type 13 type
14 PushConstant = object
15 scale: float32
14 Shader = object 16 Shader = object
15 position {.VertexAttribute.}: Vec3f 17 position {.VertexAttribute.}: Vec3f
16 color {.VertexAttribute.}: Vec3f 18 color {.VertexAttribute.}: Vec3f
19 pushConstant {.PushConstantAttribute.}: PushConstant
17 fragmentColor {.Pass.}: Vec3f 20 fragmentColor {.Pass.}: Vec3f
18 outColor {.ShaderOutput.}: Vec4f 21 outColor {.ShaderOutput.}: Vec4f
19 # code 22 # code
20 vertexCode: string = """void main() { 23 vertexCode: string = """void main() {
21 fragmentColor = color; 24 fragmentColor = color;
22 gl_Position = vec4(position, 1);}""" 25 gl_Position = vec4(position * pushConstant.scale, 1);}"""
23 fragmentCode: string = """void main() { 26 fragmentCode: string = """void main() {
24 outColor = vec4(fragmentColor, 1);}""" 27 outColor = vec4(fragmentColor, 1);}"""
25 TriangleMesh = object 28 TriangleMesh = object
26 position: GPUArray[Vec3f, VertexBuffer] 29 position: GPUArray[Vec3f, VertexBuffer]
27 color: GPUArray[Vec3f, VertexBuffer] 30 color: GPUArray[Vec3f, VertexBuffer]
41 44
42 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): 45 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
43 46
44 WithPipeline(commandbuffer, pipeline): 47 WithPipeline(commandbuffer, pipeline):
45 48
46 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) 49 RenderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh, pushConstant = PushConstant(scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000)))
47 50
48 # cleanup 51 # cleanup
49 checkVkResult vkDeviceWaitIdle(vulkan.device) 52 checkVkResult vkDeviceWaitIdle(vulkan.device)
50 DestroyPipeline(pipeline) 53 DestroyPipeline(pipeline)
51 DestroyRenderData(renderdata) 54 DestroyRenderData(renderdata)
131 Material = object 134 Material = object
132 baseColor: Vec3f 135 baseColor: Vec3f
133 136
134 Uniforms = object 137 Uniforms = object
135 material: GPUValue[Material, UniformBuffer] 138 material: GPUValue[Material, UniformBuffer]
136 texture1: Image[RGBA] 139 texture1: Image[BGRA]
137 140
138 QuadShader = object 141 QuadShader = object
139 position {.VertexAttribute.}: Vec3f 142 position {.VertexAttribute.}: Vec3f
140 fragmentColor {.Pass.}: Vec3f 143 fragmentColor {.Pass.}: Vec3f
141 uv {.Pass.}: Vec2f 144 uv {.Pass.}: Vec2f
152 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}""" 155 outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}"""
153 QuadMesh = object 156 QuadMesh = object
154 position: GPUArray[Vec3f, VertexBuffer] 157 position: GPUArray[Vec3f, VertexBuffer]
155 indices: GPUArray[uint16, IndexBuffer] 158 indices: GPUArray[uint16, IndexBuffer]
156 159
157 let R = RGBA([255'u8, 0'u8, 0'u8, 255'u8]) 160 let R = BGRA([255'u8, 0'u8, 0'u8, 255'u8])
158 let G = RGBA([0'u8, 255'u8, 0'u8, 255'u8]) 161 let G = BGRA([0'u8, 255'u8, 0'u8, 255'u8])
159 let B = RGBA([0'u8, 0'u8, 255'u8, 255'u8]) 162 let B = BGRA([0'u8, 0'u8, 255'u8, 255'u8])
160 let W = RGBA([255'u8, 255'u8, 255'u8, 255'u8]) 163 let W = BGRA([255'u8, 255'u8, 255'u8, 255'u8])
161 var 164 var
162 quad = QuadMesh( 165 quad = QuadMesh(
163 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), 166 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
164 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), 167 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
165 ) 168 )
166 uniforms1 = asDescriptorSet( 169 uniforms1 = asDescriptorSet(
167 Uniforms( 170 Uniforms(
168 material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer), 171 material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer),
169 texture1: Image[RGBA](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], interpolation: VK_FILTER_NEAREST), 172 texture1: Image[BGRA](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST),
170 ) 173 )
171 ) 174 )
172 uniforms2 = asDescriptorSet( 175 uniforms2 = asDescriptorSet(
173 Uniforms( 176 Uniforms(
174 material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer), 177 material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer),
175 texture1: Image[RGBA](width: 2, height: 2, data: @[R, G, B, W]), 178 texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]),
176 ) 179 )
177 ) 180 )
178 181
179 AssignBuffers(renderdata, quad) 182 AssignBuffers(renderdata, quad)
180 AssignBuffers(renderdata, uniforms1) 183 AssignBuffers(renderdata, uniforms1)
269 material: [ 272 material: [
270 asGPUValue(Material(baseColor: NewVec3f(1, 1, 0)), UniformBuffer), 273 asGPUValue(Material(baseColor: NewVec3f(1, 1, 0)), UniformBuffer),
271 asGPUValue(Material(baseColor: NewVec3f(1, 0, 1)), UniformBuffer), 274 asGPUValue(Material(baseColor: NewVec3f(1, 0, 1)), UniformBuffer),
272 ], 275 ],
273 texture1: [ 276 texture1: [
274 Image[Gray](width: 2, height: 2, data: @[W, G, G, W], interpolation: VK_FILTER_NEAREST), 277 Image[Gray](width: 2, height: 2, data: @[W, G, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST),
275 Image[Gray](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], interpolation: VK_FILTER_NEAREST), 278 Image[Gray](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST),
276 ], 279 ],
277 ), 280 ),
278 ) 281 )
279 var otherset1 = asDescriptorSet( 282 var otherset1 = asDescriptorSet(
280 OtherSet( 283 OtherSet(
520 proc test_07_png_texture(time: float32) = 523 proc test_07_png_texture(time: float32) =
521 var renderdata = InitRenderData() 524 var renderdata = InitRenderData()
522 525
523 type 526 type
524 Uniforms = object 527 Uniforms = object
525 texture1: Image[RGBA] 528 texture1: Image[BGRA]
526 Shader = object 529 Shader = object
527 position {.VertexAttribute.}: Vec3f 530 position {.VertexAttribute.}: Vec3f
528 uv {.VertexAttribute.}: Vec2f 531 uv {.VertexAttribute.}: Vec2f
529 fragmentUv {.Pass.}: Vec2f 532 fragmentUv {.Pass.}: Vec2f
530 outColor {.ShaderOutput.}: Vec4f 533 outColor {.ShaderOutput.}: Vec4f
556 renderdata.FlushAllMemory() 559 renderdata.FlushAllMemory()
557 560
558 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) 561 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass)
559 var uniforms1 = asDescriptorSet( 562 var uniforms1 = asDescriptorSet(
560 Uniforms( 563 Uniforms(
561 texture1: LoadImage[RGBA]("art.png"), 564 texture1: LoadImage[BGRA]("art.png"),
562 ) 565 )
563 ) 566 )
564 UploadImages(renderdata, uniforms1) 567 UploadImages(renderdata, uniforms1)
565 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) 568 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1)
566 569
588 591
589 var renderdata = InitRenderData() 592 var renderdata = InitRenderData()
590 593
591 type 594 type
592 Uniforms = object 595 Uniforms = object
593 frameTexture: Image[RGBA] 596 frameTexture: Image[BGRA]
594 TriangleShader = object 597 TriangleShader = object
595 position {.VertexAttribute.}: Vec3f 598 position {.VertexAttribute.}: Vec3f
596 color {.VertexAttribute.}: Vec3f 599 color {.VertexAttribute.}: Vec3f
597 fragmentColor {.Pass.}: Vec3f 600 fragmentColor {.Pass.}: Vec3f
598 outColor {.ShaderOutput.}: Vec4f 601 outColor {.ShaderOutput.}: Vec4f
637 position: asGPUArray([NewVec2f(-1, -1), NewVec2f(-1, 1), NewVec2f(1, 1), NewVec2f(1, -1)], VertexBuffer), 640 position: asGPUArray([NewVec2f(-1, -1), NewVec2f(-1, 1), NewVec2f(1, 1), NewVec2f(1, -1)], VertexBuffer),
638 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), 641 indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer),
639 ) 642 )
640 var uniforms1 = asDescriptorSet( 643 var uniforms1 = asDescriptorSet(
641 Uniforms( 644 Uniforms(
642 frameTexture: Image[RGBA](width: vulkan.swapchain.width, height: vulkan.swapchain.height, isRenderTarget: true), 645 frameTexture: Image[BGRA](width: vulkan.swapchain.width, height: vulkan.swapchain.height, isRenderTarget: true),
643 ) 646 )
644 ) 647 )
645 AssignBuffers(renderdata, mesh) 648 AssignBuffers(renderdata, mesh)
646 AssignBuffers(renderdata, quad) 649 AssignBuffers(renderdata, quad)
647 UploadImages(renderdata, uniforms1) 650 UploadImages(renderdata, uniforms1)