annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
1 import std/os
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
2 import std/monotimes
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
3 import std/times
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
4 import std/options
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
5
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
6 import semicongine
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
7
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
8 type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
9 MeshA = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
10 position: GPUArray[Vec3f, VertexBuffer]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
11 indices: GPUArray[uint16, IndexBuffer]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
12 InstanceA = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
13 rotation: GPUArray[Vec4f, VertexBuffer]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
14 objPosition: GPUArray[Vec3f, VertexBuffer]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
15 MaterialA = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
16 reflection: float32
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
17 baseColor: Vec3f
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
18 UniformsA = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
19 defaultTexture: Texture[TVec4[uint8]]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
20 defaultMaterial: GPUValue[MaterialA, UniformBuffer]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
21 materials: GPUValue[array[3, MaterialA], UniformBuffer]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
22 materialTextures: array[3, Texture[TVec4[uint8]]]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
23 ShaderSettings = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
24 gamma: float32
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
25 GlobalsA = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
26 fontAtlas: Texture[TVec4[uint8]]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
27 settings: GPUValue[ShaderSettings, UniformBuffer]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
28
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
29 ShaderA = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
30 # vertex input
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
31 position {.VertexAttribute.}: Vec3f
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
32 # objPosition {.InstanceAttribute.}: Vec3f
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
33 # rotation {.InstanceAttribute.}: Vec4f
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
34 # intermediate
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
35 test {.Pass.}: float32
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
36 test1 {.PassFlat.}: Vec3f
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
37 # output
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
38 color {.ShaderOutput.}: Vec4f
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
39 # descriptor sets
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
40 globals: DescriptorSet[GlobalsA, GlobalSet]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
41 uniforms: DescriptorSet[UniformsA, MaterialSet]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
42 # code
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
43 vertexCode: string = """void main() {
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
44 gl_Position = vec4(position, 1);
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
45 }"""
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
46 fragmentCode: string = """void main() {
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
47 color = vec4(1, 0, 0, 1);
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
48 }"""
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
49
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
50 var myMesh1 = MeshA(
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
51 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(-0.5, 0.5, ), NewVec3f(0, -0.5, ), NewVec3f(0.5, 0.5, )]),
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
52 indices: GPUArray[uint16, IndexBuffer](data: @[0'u16, 1'u16, 2'u16])
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
53 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
54 var uniforms1 = DescriptorSet[UniformsA, MaterialSet](
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
55 data: UniformsA(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
56 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
57 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
58 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
59 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
60 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
61 ]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
62 materialTextures: [
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
63 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
64 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
65 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
66 ]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
67 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
68 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
69 var instances1 = InstanceA(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
70 rotation: GPUArray[Vec4f, VertexBuffer](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
71 objPosition: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
72 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
73 var myGlobals = DescriptorSet[GlobalsA, GlobalSet](
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
74 data: GlobalsA(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
75 fontAtlas: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
76 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
77 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
78 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
79
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
80 let mainRenderpass = CreatePresentationRenderPass()
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
81 var swapchain = InitSwapchain(renderpass = mainRenderpass).get()
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
82 var pipeline1 = CreatePipeline[ShaderA](renderPass = mainRenderpass)
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
83
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
84 var renderdata = InitRenderData()
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
85
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
86 # buffer assignment
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
87 echo "Assigning buffers to GPUData fields"
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
88
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
89 AssignBuffers(renderdata, myMesh1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
90 AssignBuffers(renderdata, instances1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
91 AssignBuffers(renderdata, myGlobals)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
92 AssignBuffers(renderdata, uniforms1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
93
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
94 renderdata.UploadTextures(myGlobals)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
95 renderdata.UploadTextures(uniforms1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
96
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
97 # copy everything to GPU
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
98 echo "Copying all data to GPU memory"
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
99 UpdateAllGPUBuffers(myMesh1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
100 UpdateAllGPUBuffers(instances1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
101 UpdateAllGPUBuffers(uniforms1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
102 UpdateAllGPUBuffers(myGlobals)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
103 renderdata.FlushAllMemory()
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
104
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
105 # descriptors
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
106 echo "Writing descriptors"
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
107 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
108 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
109
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
110
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
111 # main loop
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
112 var t = getMonoTime()
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
113 while UpdateInputs():
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
114 WithNextFrame(swapchain, framebuffer, commandbuffer):
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
115 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
116 WithPipeline(commandbuffer, pipeline1):
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
117 WithBind(commandBuffer, myGlobals, uniforms1, pipeline1, swapchain.currentFiF.int):
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
118 Render(
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
119 commandbuffer = commandbuffer,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
120 pipeline = pipeline1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
121 globalSet = myGlobals,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
122 materialSet = uniforms1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
123 mesh = myMesh1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
124 # instances = instances1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
125 )
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
126 echo (getMonoTime() - t).inMicroseconds.float / 1000.0
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
127 t = getMonoTime()
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
128
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
129 # cleanup
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
130 checkVkResult vkDeviceWaitIdle(vulkan.device)
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
131 DestroyPipeline(pipeline1)
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
132 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
133 DestroyRenderData(renderdata)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
134 DestroySwapchain(swapchain)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
135 DestroyVulkan()