Mercurial > games > semicongine
annotate test1.nim @ 1198:96a094cd0c78 compiletime-tests
sync to notebook in bedroom
author | sam <sam@basx.dev> |
---|---|
date | Sat, 13 Jul 2024 19:26:03 +0700 |
parents | f6a0dc7ad052 |
children | ba1af13233ee |
rev | line source |
---|---|
1197 | 1 import std/os |
2 import std/options | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
3 |
1191 | 4 import semicongine |
5 | |
6 type | |
7 MeshA = object | |
8 position: GPUArray[Vec3f, VertexBuffer] | |
9 indices: GPUArray[uint16, IndexBuffer] | |
10 InstanceA = object | |
11 rotation: GPUArray[Vec4f, VertexBuffer] | |
12 objPosition: GPUArray[Vec3f, VertexBuffer] | |
13 MaterialA = object | |
14 reflection: float32 | |
15 baseColor: Vec3f | |
16 UniformsA = object | |
17 defaultTexture: Texture[TVec4[uint8]] | |
18 defaultMaterial: GPUValue[MaterialA, UniformBuffer] | |
19 materials: GPUValue[array[3, MaterialA], UniformBuffer] | |
20 materialTextures: array[3, Texture[TVec4[uint8]]] | |
21 ShaderSettings = object | |
22 gamma: float32 | |
23 GlobalsA = object | |
24 fontAtlas: Texture[TVec4[uint8]] | |
25 settings: GPUValue[ShaderSettings, UniformBuffer] | |
26 | |
27 ShaderA = object | |
28 # vertex input | |
29 position {.VertexAttribute.}: Vec3f | |
30 objPosition {.InstanceAttribute.}: Vec3f | |
31 rotation {.InstanceAttribute.}: Vec4f | |
32 # intermediate | |
33 test {.Pass.}: float32 | |
34 test1 {.PassFlat.}: Vec3f | |
35 # output | |
36 color {.ShaderOutput.}: Vec4f | |
37 # descriptor sets | |
38 globals: DescriptorSet[GlobalsA, GlobalSet] | |
39 uniforms: DescriptorSet[UniformsA, MaterialSet] | |
40 # code | |
41 vertexCode: string = "void main() {}" | |
42 fragmentCode: string = "void main() {}" | |
43 | |
44 let frameWidth = 100'u32 | |
45 let frameHeight = 100'u32 | |
46 | |
47 var myMesh1 = MeshA( | |
48 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
49 ) | |
50 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( | |
51 data: UniformsA( | |
52 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
53 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [ | |
54 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
55 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
56 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
57 ]), | |
58 materialTextures: [ | |
59 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
60 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
61 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
62 ] | |
63 ) | |
64 ) | |
65 var instances1 = InstanceA( | |
66 rotation: GPUArray[Vec4f, VertexBuffer](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
67 objPosition: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
68 ) | |
69 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]( | |
70 data: GlobalsA( | |
71 fontAtlas: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
72 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0)) | |
73 ) | |
74 ) | |
75 | |
1192 | 76 let renderpass = CreatePresentationRenderPass() |
1198 | 77 var swapchainResult = InitSwapchain(renderpass = renderpass) |
1196 | 78 assert swapchainResult.isSome() |
79 var swapchain = swapchainResult.get() | |
1191 | 80 |
81 # shaders | |
1192 | 82 var pipeline1 = CreatePipeline[ShaderA](renderPass = renderpass) |
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 |
1191 | 111 # start command buffer |
1198 | 112 while true: |
113 RecordRenderingCommands(swapchain, framebuffer, commandbuffer): | |
1191 | 114 var |
115 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
116 renderPassInfo = VkRenderPassBeginInfo( | |
117 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
118 renderPass: renderpass, | |
1198 | 119 framebuffer: framebuffer, |
1191 | 120 renderArea: VkRect2D( |
121 offset: VkOffset2D(x: 0, y: 0), | |
122 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
123 ), | |
124 clearValueCount: uint32(clearColors.len), | |
125 pClearValues: clearColors.ToCPointer(), | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
126 ) |
1191 | 127 viewport = VkViewport( |
128 x: 0.0, | |
129 y: 0.0, | |
130 width: frameWidth.float32, | |
131 height: frameHeight.float32, | |
132 minDepth: 0.0, | |
133 maxDepth: 1.0, | |
134 ) | |
135 scissor = VkRect2D( | |
136 offset: VkOffset2D(x: 0, y: 0), | |
137 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
138 ) | |
1198 | 139 vkCmdBeginRenderPass(commandbuffer, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
140 |
1191 | 141 # setup viewport |
1198 | 142 vkCmdSetViewport(commandbuffer, firstViewport = 0, viewportCount = 1, addr(viewport)) |
143 vkCmdSetScissor(commandbuffer, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
1191 | 144 |
145 # bind pipeline, will be loop | |
1197 | 146 # block: |
1198 | 147 # Bind(pipeline1, commandbuffer, currentFrameInFlight = currentFrameInFlight) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
148 |
1191 | 149 # render object, will be loop |
1197 | 150 # block: |
1198 | 151 # Render(commandbuffer, pipeline1, myGlobals, uniforms1, myMesh1, instances1) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
152 |
1198 | 153 vkCmdEndRenderPass(commandbuffer) |