Mercurial > games > semicongine
annotate test1.nim @ 1201:d4a206b5e5b0 compiletime-tests
add: rest of the cleanup stuff
author | sam <sam@basx.dev> |
---|---|
date | Sun, 14 Jul 2024 21:30:52 +0700 |
parents | 5c6491f28dcd |
children | a8864fe6fe6e |
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 | |
32 objPosition {.InstanceAttribute.}: Vec3f | |
33 rotation {.InstanceAttribute.}: Vec4f | |
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 | |
43 vertexCode: string = "void main() {}" | |
44 fragmentCode: string = "void main() {}" | |
45 | |
46 let frameWidth = 100'u32 | |
47 let frameHeight = 100'u32 | |
48 | |
49 var myMesh1 = MeshA( | |
50 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
51 ) | |
52 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( | |
53 data: UniformsA( | |
54 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: [ | |
56 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
57 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
58 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
59 ]), | |
60 materialTextures: [ | |
61 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
62 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
63 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
64 ] | |
65 ) | |
66 ) | |
67 var instances1 = InstanceA( | |
68 rotation: GPUArray[Vec4f, VertexBuffer](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
69 objPosition: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
70 ) | |
71 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]( | |
72 data: GlobalsA( | |
73 fontAtlas: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
74 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0)) | |
75 ) | |
76 ) | |
77 | |
1199 | 78 let mainRenderpass = CreatePresentationRenderPass() |
79 var swapchainResult = InitSwapchain(renderpass = mainRenderpass) | |
80 | |
1196 | 81 assert swapchainResult.isSome() |
82 var swapchain = swapchainResult.get() | |
1191 | 83 |
84 # shaders | |
1199 | 85 var pipeline1 = CreatePipeline[ShaderA](renderPass = mainRenderpass) |
1191 | 86 |
87 var renderdata = InitRenderData() | |
88 | |
89 # buffer assignment | |
90 echo "Assigning buffers to GPUData fields" | |
91 | |
92 AssignBuffers(renderdata, myMesh1) | |
93 AssignBuffers(renderdata, instances1) | |
94 AssignBuffers(renderdata, myGlobals) | |
95 AssignBuffers(renderdata, uniforms1) | |
96 | |
97 renderdata.UploadTextures(myGlobals) | |
98 renderdata.UploadTextures(uniforms1) | |
99 | |
100 # copy everything to GPU | |
101 echo "Copying all data to GPU memory" | |
102 UpdateAllGPUBuffers(myMesh1) | |
103 UpdateAllGPUBuffers(instances1) | |
104 UpdateAllGPUBuffers(uniforms1) | |
105 UpdateAllGPUBuffers(myGlobals) | |
106 renderdata.FlushAllMemory() | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
107 |
1191 | 108 # descriptors |
109 echo "Writing descriptors" | |
1192 | 110 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals) |
111 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
112 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
113 |
1191 | 114 # start command buffer |
1199 | 115 var t = getMonoTime() |
116 while UpdateInputs(): | |
117 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
118 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(1, 0, 0, 0)): | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
119 vkCmdBindPipeline(commandbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline1.vk) |
1201 | 120 |
121 Render( | |
122 commandbuffer = commandbuffer, | |
123 pipeline = pipeline1, | |
124 globalSet = myGlobals, | |
125 materialSet = uniforms1, | |
126 mesh = myMesh1, | |
127 instances = instances1, | |
128 ) | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
129 echo (getMonoTime() - t).inMicroseconds.float / 1000.0 |
1199 | 130 t = getMonoTime() |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
131 |
1201 | 132 checkVkResult vkDeviceWaitIdle(vulkan.device) |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
133 DestroyPipeline(pipeline1) |
1201 | 134 vkDestroyRenderPass(vulkan.device, mainRenderpass, nil) |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
135 DestroyRenderData(renderdata) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
136 DestroySwapchain(swapchain) |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
137 DestroyVulkan() |