annotate test1.nim @ 1204:e2901100a596 compiletime-tests

add: tests, some fixes, some helpers
author sam <sam@basx.dev>
date Mon, 15 Jul 2024 23:51:17 +0700
parents a8864fe6fe6e
children
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
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1202
diff changeset
50 InitVulkan()
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1202
diff changeset
51
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
52 var myMesh1 = MeshA(
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1202
diff changeset
53 position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer),
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1202
diff changeset
54 indices: asGPUArray([0'u16, 1'u16, 2'u16], IndexBuffer),
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
55 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
56 var uniforms1 = DescriptorSet[UniformsA, MaterialSet](
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
57 data: UniformsA(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
58 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
59 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
60 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
61 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
62 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
63 ]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
64 materialTextures: [
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 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
67 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
68 ]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
69 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
70 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
71 var instances1 = InstanceA(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
72 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
73 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
74 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
75 var myGlobals = DescriptorSet[GlobalsA, GlobalSet](
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
76 data: GlobalsA(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
77 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
78 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
79 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
80 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
81
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
82 let mainRenderpass = CreatePresentationRenderPass()
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
83 var swapchain = InitSwapchain(renderpass = mainRenderpass).get()
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
84 var pipeline1 = CreatePipeline[ShaderA](renderPass = mainRenderpass)
1191
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 var renderdata = InitRenderData()
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
87
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
88 # buffer assignment
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
89 echo "Assigning buffers to GPUData fields"
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
90
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
91 AssignBuffers(renderdata, myMesh1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
92 AssignBuffers(renderdata, instances1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
93 AssignBuffers(renderdata, myGlobals)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
94 AssignBuffers(renderdata, uniforms1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
95
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
96 renderdata.UploadTextures(myGlobals)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
97 renderdata.UploadTextures(uniforms1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
98
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
99 # copy everything to GPU
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
100 echo "Copying all data to GPU memory"
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
101 renderdata.FlushAllMemory()
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
102
1191
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
103 # descriptors
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents: 1190
diff changeset
104 echo "Writing descriptors"
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
105 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
106 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
107
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
108
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
109 # main loop
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
110 var t = getMonoTime()
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
111 while UpdateInputs():
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
112 WithNextFrame(swapchain, framebuffer, commandbuffer):
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
113 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
114 WithPipeline(commandbuffer, pipeline1):
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
115 WithBind(commandBuffer, myGlobals, uniforms1, pipeline1, swapchain.currentFiF.int):
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
116 Render(
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
117 commandbuffer = commandbuffer,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
118 pipeline = pipeline1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
119 globalSet = myGlobals,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
120 materialSet = uniforms1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
121 mesh = myMesh1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
122 # instances = instances1,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
123 )
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
124 echo (getMonoTime() - t).inMicroseconds.float / 1000.0
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
125 t = getMonoTime()
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
126
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
127 # cleanup
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
128 checkVkResult vkDeviceWaitIdle(vulkan.device)
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
129 DestroyPipeline(pipeline1)
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
130 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
131 DestroyRenderData(renderdata)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
132 DestroySwapchain(swapchain)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
133 DestroyVulkan()