Mercurial > games > semicongine
annotate test1.nim @ 1196:82feceae80b1 compiletime-tests
sync from bedroom to office
author | sam <sam@basx.dev> |
---|---|
date | Tue, 09 Jul 2024 22:53:38 +0700 |
parents | 5aa1184fa5eb |
children | f6a0dc7ad052 |
rev | line source |
---|---|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
1 import os |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
2 |
1191 | 3 import semicongine |
4 | |
5 type | |
6 MeshA = object | |
7 position: GPUArray[Vec3f, VertexBuffer] | |
8 indices: GPUArray[uint16, IndexBuffer] | |
9 InstanceA = object | |
10 rotation: GPUArray[Vec4f, VertexBuffer] | |
11 objPosition: GPUArray[Vec3f, VertexBuffer] | |
12 MaterialA = object | |
13 reflection: float32 | |
14 baseColor: Vec3f | |
15 UniformsA = object | |
16 defaultTexture: Texture[TVec4[uint8]] | |
17 defaultMaterial: GPUValue[MaterialA, UniformBuffer] | |
18 materials: GPUValue[array[3, MaterialA], UniformBuffer] | |
19 materialTextures: array[3, Texture[TVec4[uint8]]] | |
20 ShaderSettings = object | |
21 gamma: float32 | |
22 GlobalsA = object | |
23 fontAtlas: Texture[TVec4[uint8]] | |
24 settings: GPUValue[ShaderSettings, UniformBuffer] | |
25 | |
26 ShaderA = object | |
27 # vertex input | |
28 position {.VertexAttribute.}: Vec3f | |
29 objPosition {.InstanceAttribute.}: Vec3f | |
30 rotation {.InstanceAttribute.}: Vec4f | |
31 # intermediate | |
32 test {.Pass.}: float32 | |
33 test1 {.PassFlat.}: Vec3f | |
34 # output | |
35 color {.ShaderOutput.}: Vec4f | |
36 # descriptor sets | |
37 globals: DescriptorSet[GlobalsA, GlobalSet] | |
38 uniforms: DescriptorSet[UniformsA, MaterialSet] | |
39 # code | |
40 vertexCode: string = "void main() {}" | |
41 fragmentCode: string = "void main() {}" | |
42 | |
43 putEnv("VK_LAYER_ENABLES", "VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_AMD,VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_NVIDIA,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXTVK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT") | |
44 | |
45 let frameWidth = 100'u32 | |
46 let frameHeight = 100'u32 | |
47 | |
48 var myMesh1 = MeshA( | |
49 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
50 ) | |
51 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( | |
52 data: UniformsA( | |
53 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
54 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [ | |
55 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
56 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
57 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
58 ]), | |
59 materialTextures: [ | |
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 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
63 ] | |
64 ) | |
65 ) | |
66 var instances1 = InstanceA( | |
67 rotation: GPUArray[Vec4f, VertexBuffer](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
68 objPosition: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
69 ) | |
70 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]( | |
71 data: GlobalsA( | |
72 fontAtlas: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
73 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0)) | |
74 ) | |
75 ) | |
76 | |
1192 | 77 let renderpass = CreatePresentationRenderPass() |
1196 | 78 var swapchainResult = InitSwapchain(renderpass) |
79 assert swapchainResult.isSome() | |
80 var swapchain = swapchainResult.get() | |
1191 | 81 |
82 # shaders | |
1192 | 83 var pipeline1 = CreatePipeline[ShaderA](renderPass = renderpass) |
1191 | 84 |
85 var renderdata = InitRenderData() | |
86 | |
87 # buffer assignment | |
88 echo "Assigning buffers to GPUData fields" | |
89 | |
90 AssignBuffers(renderdata, myMesh1) | |
91 AssignBuffers(renderdata, instances1) | |
92 AssignBuffers(renderdata, myGlobals) | |
93 AssignBuffers(renderdata, uniforms1) | |
94 | |
95 renderdata.UploadTextures(myGlobals) | |
96 renderdata.UploadTextures(uniforms1) | |
97 | |
98 # copy everything to GPU | |
99 echo "Copying all data to GPU memory" | |
100 UpdateAllGPUBuffers(myMesh1) | |
101 UpdateAllGPUBuffers(instances1) | |
102 UpdateAllGPUBuffers(uniforms1) | |
103 UpdateAllGPUBuffers(myGlobals) | |
104 renderdata.FlushAllMemory() | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
105 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
106 |
1191 | 107 # descriptors |
108 echo "Writing descriptors" | |
1192 | 109 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals) |
110 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
111 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
112 |
1196 | 113 |
114 | |
115 | |
116 | |
1191 | 117 # command buffer |
118 var | |
119 commandBufferPool: VkCommandPool | |
120 createInfo = VkCommandPoolCreateInfo( | |
121 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
122 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
1193 | 123 queueFamilyIndex: vulkan.graphicsQueueFamily, |
1191 | 124 ) |
125 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool) | |
126 var | |
127 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
128 allocInfo = VkCommandBufferAllocateInfo( | |
129 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
130 commandPool: commandBufferPool, | |
131 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
132 commandBufferCount: INFLIGHTFRAMES, | |
133 ) | |
134 checkVkResult vkAllocateCommandBuffers(vulkan.device, addr allocInfo, cmdBuffers.ToCPointer) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
135 |
1191 | 136 # start command buffer |
137 block: | |
138 let | |
139 currentFramebuffer = VkFramebuffer(0) # TODO | |
140 currentFrameInFlight = 1 | |
141 cmd = cmdBuffers[currentFrameInFlight] | |
142 beginInfo = VkCommandBufferBeginInfo( | |
143 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
144 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
145 ) |
1191 | 146 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
147 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
148 |
1191 | 149 # start renderpass |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
150 block: |
1191 | 151 var |
152 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
153 renderPassInfo = VkRenderPassBeginInfo( | |
154 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
155 renderPass: renderpass, | |
156 framebuffer: currentFramebuffer, # TODO | |
157 renderArea: VkRect2D( | |
158 offset: VkOffset2D(x: 0, y: 0), | |
159 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
160 ), | |
161 clearValueCount: uint32(clearColors.len), | |
162 pClearValues: clearColors.ToCPointer(), | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
163 ) |
1191 | 164 viewport = VkViewport( |
165 x: 0.0, | |
166 y: 0.0, | |
167 width: frameWidth.float32, | |
168 height: frameHeight.float32, | |
169 minDepth: 0.0, | |
170 maxDepth: 1.0, | |
171 ) | |
172 scissor = VkRect2D( | |
173 offset: VkOffset2D(x: 0, y: 0), | |
174 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
175 ) | |
176 vkCmdBeginRenderPass(cmd, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
177 |
1191 | 178 # setup viewport |
179 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
180 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
181 | |
182 # bind pipeline, will be loop | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
183 block: |
1191 | 184 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
185 |
1191 | 186 # render object, will be loop |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
187 block: |
1191 | 188 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
189 |
1191 | 190 vkCmdEndRenderPass(cmd) |
191 checkVkResult cmd.vkEndCommandBuffer() |