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