Mercurial > games > semicongine
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 |
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 |
50 var myMesh1 = MeshA( | |
1202 | 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]) | |
1191 | 53 ) |
54 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( | |
55 data: UniformsA( | |
56 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
57 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [ | |
58 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
59 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
60 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
61 ]), | |
62 materialTextures: [ | |
63 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
64 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
65 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
66 ] | |
67 ) | |
68 ) | |
69 var instances1 = InstanceA( | |
70 rotation: GPUArray[Vec4f, VertexBuffer](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
71 objPosition: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
72 ) | |
73 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]( | |
74 data: GlobalsA( | |
75 fontAtlas: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
76 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0)) | |
77 ) | |
78 ) | |
79 | |
1199 | 80 let mainRenderpass = CreatePresentationRenderPass() |
1202 | 81 var swapchain = InitSwapchain(renderpass = mainRenderpass).get() |
1199 | 82 var pipeline1 = CreatePipeline[ShaderA](renderPass = mainRenderpass) |
1191 | 83 |
84 var renderdata = InitRenderData() | |
85 | |
86 # buffer assignment | |
87 echo "Assigning buffers to GPUData fields" | |
88 | |
89 AssignBuffers(renderdata, myMesh1) | |
90 AssignBuffers(renderdata, instances1) | |
91 AssignBuffers(renderdata, myGlobals) | |
92 AssignBuffers(renderdata, uniforms1) | |
93 | |
94 renderdata.UploadTextures(myGlobals) | |
95 renderdata.UploadTextures(uniforms1) | |
96 | |
97 # copy everything to GPU | |
98 echo "Copying all data to GPU memory" | |
99 UpdateAllGPUBuffers(myMesh1) | |
100 UpdateAllGPUBuffers(instances1) | |
101 UpdateAllGPUBuffers(uniforms1) | |
102 UpdateAllGPUBuffers(myGlobals) | |
103 renderdata.FlushAllMemory() | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
104 |
1191 | 105 # descriptors |
106 echo "Writing descriptors" | |
1192 | 107 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals) |
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 | 111 # main loop |
1199 | 112 var t = getMonoTime() |
113 while UpdateInputs(): | |
114 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
1202 | 115 WithRenderPass(mainRenderpass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): |
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() | |
1201 | 128 |
1202 | 129 # cleanup |
1201 | 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 | 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() |