comparison test1.nim @ 1202:a8864fe6fe6e compiletime-tests

add: descriptor sets binding n stuff
author sam <sam@basx.dev>
date Mon, 15 Jul 2024 00:04:33 +0700
parents d4a206b5e5b0
children e2901100a596
comparison
equal deleted inserted replaced
1201:d4a206b5e5b0 1202:a8864fe6fe6e
27 settings: GPUValue[ShaderSettings, UniformBuffer] 27 settings: GPUValue[ShaderSettings, UniformBuffer]
28 28
29 ShaderA = object 29 ShaderA = object
30 # vertex input 30 # vertex input
31 position {.VertexAttribute.}: Vec3f 31 position {.VertexAttribute.}: Vec3f
32 objPosition {.InstanceAttribute.}: Vec3f 32 # objPosition {.InstanceAttribute.}: Vec3f
33 rotation {.InstanceAttribute.}: Vec4f 33 # rotation {.InstanceAttribute.}: Vec4f
34 # intermediate 34 # intermediate
35 test {.Pass.}: float32 35 test {.Pass.}: float32
36 test1 {.PassFlat.}: Vec3f 36 test1 {.PassFlat.}: Vec3f
37 # output 37 # output
38 color {.ShaderOutput.}: Vec4f 38 color {.ShaderOutput.}: Vec4f
39 # descriptor sets 39 # descriptor sets
40 globals: DescriptorSet[GlobalsA, GlobalSet] 40 globals: DescriptorSet[GlobalsA, GlobalSet]
41 uniforms: DescriptorSet[UniformsA, MaterialSet] 41 uniforms: DescriptorSet[UniformsA, MaterialSet]
42 # code 42 # code
43 vertexCode: string = "void main() {}" 43 vertexCode: string = """void main() {
44 fragmentCode: string = "void main() {}" 44 gl_Position = vec4(position, 1);
45 45 }"""
46 let frameWidth = 100'u32 46 fragmentCode: string = """void main() {
47 let frameHeight = 100'u32 47 color = vec4(1, 0, 0, 1);
48 }"""
48 49
49 var myMesh1 = MeshA( 50 var myMesh1 = MeshA(
50 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), 51 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(-0.5, 0.5, ), NewVec3f(0, -0.5, ), NewVec3f(0.5, 0.5, )]),
52 indices: GPUArray[uint16, IndexBuffer](data: @[0'u16, 1'u16, 2'u16])
51 ) 53 )
52 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( 54 var uniforms1 = DescriptorSet[UniformsA, MaterialSet](
53 data: UniformsA( 55 data: UniformsA(
54 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), 56 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]),
55 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [ 57 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [
74 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0)) 76 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0))
75 ) 77 )
76 ) 78 )
77 79
78 let mainRenderpass = CreatePresentationRenderPass() 80 let mainRenderpass = CreatePresentationRenderPass()
79 var swapchainResult = InitSwapchain(renderpass = mainRenderpass) 81 var swapchain = InitSwapchain(renderpass = mainRenderpass).get()
80
81 assert swapchainResult.isSome()
82 var swapchain = swapchainResult.get()
83
84 # shaders
85 var pipeline1 = CreatePipeline[ShaderA](renderPass = mainRenderpass) 82 var pipeline1 = CreatePipeline[ShaderA](renderPass = mainRenderpass)
86 83
87 var renderdata = InitRenderData() 84 var renderdata = InitRenderData()
88 85
89 # buffer assignment 86 # buffer assignment
109 echo "Writing descriptors" 106 echo "Writing descriptors"
110 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals) 107 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals)
111 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1) 108 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1)
112 109
113 110
114 # start command buffer 111 # main loop
115 var t = getMonoTime() 112 var t = getMonoTime()
116 while UpdateInputs(): 113 while UpdateInputs():
117 WithNextFrame(swapchain, framebuffer, commandbuffer): 114 WithNextFrame(swapchain, framebuffer, commandbuffer):
118 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(1, 0, 0, 0)): 115 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
119 vkCmdBindPipeline(commandbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline1.vk) 116 WithPipeline(commandbuffer, pipeline1):
117 WithBind(commandBuffer, myGlobals, uniforms1, pipeline1, swapchain.currentFiF.int):
118 Render(
119 commandbuffer = commandbuffer,
120 pipeline = pipeline1,
121 globalSet = myGlobals,
122 materialSet = uniforms1,
123 mesh = myMesh1,
124 # instances = instances1,
125 )
126 echo (getMonoTime() - t).inMicroseconds.float / 1000.0
127 t = getMonoTime()
120 128
121 Render( 129 # cleanup
122 commandbuffer = commandbuffer,
123 pipeline = pipeline1,
124 globalSet = myGlobals,
125 materialSet = uniforms1,
126 mesh = myMesh1,
127 instances = instances1,
128 )
129 echo (getMonoTime() - t).inMicroseconds.float / 1000.0
130 t = getMonoTime()
131
132 checkVkResult vkDeviceWaitIdle(vulkan.device) 130 checkVkResult vkDeviceWaitIdle(vulkan.device)
133 DestroyPipeline(pipeline1) 131 DestroyPipeline(pipeline1)
134 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) 132 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
135 DestroyRenderData(renderdata) 133 DestroyRenderData(renderdata)
136 DestroySwapchain(swapchain) 134 DestroySwapchain(swapchain)