Mercurial > games > semicongine
annotate test1.nim @ 1191:239adab121a3 compiletime-tests
sync from bedroom to office
author | sam <sam@basx.dev> |
---|---|
date | Mon, 08 Jul 2024 01:16:53 +0700 |
parents | a3eb305bcac2 |
children | 90bf0cab1d02 |
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 let w = CreateWindow("test2") | |
44 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") | |
45 | |
46 # TODO: remove those ugly wrappers | |
47 let theInstance = w.CreateInstance( | |
48 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), | |
49 instanceExtensions = @[], | |
50 layers = @["VK_LAYER_KHRONOS_validation"], | |
51 ) | |
52 | |
53 let dev = theInstance.CreateDevice( | |
54 theInstance.GetPhysicalDevices().FilterBestGraphics(), | |
55 enabledExtensions = @[], | |
56 theInstance.GetPhysicalDevices().FilterBestGraphics().FilterForGraphicsPresentationQueues() | |
57 ).vk | |
58 let frameWidth = 100'u32 | |
59 let frameHeight = 100'u32 | |
60 | |
61 # TODO: pack this stuff into a setup method and condense everything a bit | |
62 let pDevice = theInstance.vk.GetPhysicalDevice() | |
63 let qfi = pDevice.GetQueueFamily(VK_QUEUE_GRAPHICS_BIT) | |
64 vulkan = VulkanGlobals( | |
65 instance: theInstance.vk, | |
66 device: dev, | |
67 physicalDevice: pDevice, | |
68 queueFamilyIndex: qfi, | |
69 queue: svkGetDeviceQueue(dev, qfi, VK_QUEUE_GRAPHICS_BIT) | |
70 ) | |
71 | |
72 var myMesh1 = MeshA( | |
73 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
74 ) | |
75 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( | |
76 data: UniformsA( | |
77 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
78 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [ | |
79 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
80 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
81 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
82 ]), | |
83 materialTextures: [ | |
84 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
85 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
86 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
87 ] | |
88 ) | |
89 ) | |
90 var instances1 = InstanceA( | |
91 rotation: GPUArray[Vec4f, VertexBuffer](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
92 objPosition: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
93 ) | |
94 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]( | |
95 data: GlobalsA( | |
96 fontAtlas: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
97 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0)) | |
98 ) | |
99 ) | |
100 | |
101 # setup for rendering (TODO: swapchain & framebuffers) | |
102 let renderpass = CreateRenderPass(GetSurfaceFormat()) | |
103 | |
104 # shaders | |
105 const shader = ShaderA() | |
106 let shaderObject = CompileShader(shader) | |
107 var pipeline1 = CreatePipeline(renderPass = renderpass, shader = shaderObject) | |
108 | |
109 var renderdata = InitRenderData() | |
110 | |
111 # buffer assignment | |
112 echo "Assigning buffers to GPUData fields" | |
113 | |
114 AssignBuffers(renderdata, myMesh1) | |
115 AssignBuffers(renderdata, instances1) | |
116 AssignBuffers(renderdata, myGlobals) | |
117 AssignBuffers(renderdata, uniforms1) | |
118 | |
119 renderdata.UploadTextures(myGlobals) | |
120 renderdata.UploadTextures(uniforms1) | |
121 | |
122 # copy everything to GPU | |
123 echo "Copying all data to GPU memory" | |
124 UpdateAllGPUBuffers(myMesh1) | |
125 UpdateAllGPUBuffers(instances1) | |
126 UpdateAllGPUBuffers(uniforms1) | |
127 UpdateAllGPUBuffers(myGlobals) | |
128 renderdata.FlushAllMemory() | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
129 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
130 |
1191 | 131 # descriptors |
132 echo "Writing descriptors" | |
133 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[GlobalSet], myGlobals) | |
134 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[MaterialSet], uniforms1) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
135 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
136 |
1191 | 137 # command buffer |
138 var | |
139 commandBufferPool: VkCommandPool | |
140 createInfo = VkCommandPoolCreateInfo( | |
141 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
142 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
143 queueFamilyIndex: vulkan.queueFamilyIndex, | |
144 ) | |
145 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool) | |
146 var | |
147 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
148 allocInfo = VkCommandBufferAllocateInfo( | |
149 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
150 commandPool: commandBufferPool, | |
151 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
152 commandBufferCount: INFLIGHTFRAMES, | |
153 ) | |
154 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
|
155 |
1191 | 156 # start command buffer |
157 block: | |
158 let | |
159 currentFramebuffer = VkFramebuffer(0) # TODO | |
160 currentFrameInFlight = 1 | |
161 cmd = cmdBuffers[currentFrameInFlight] | |
162 beginInfo = VkCommandBufferBeginInfo( | |
163 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
164 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
|
165 ) |
1191 | 166 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
167 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
168 |
1191 | 169 # start renderpass |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
170 block: |
1191 | 171 var |
172 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
173 renderPassInfo = VkRenderPassBeginInfo( | |
174 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
175 renderPass: renderpass, | |
176 framebuffer: currentFramebuffer, # TODO | |
177 renderArea: VkRect2D( | |
178 offset: VkOffset2D(x: 0, y: 0), | |
179 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
180 ), | |
181 clearValueCount: uint32(clearColors.len), | |
182 pClearValues: clearColors.ToCPointer(), | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
183 ) |
1191 | 184 viewport = VkViewport( |
185 x: 0.0, | |
186 y: 0.0, | |
187 width: frameWidth.float32, | |
188 height: frameHeight.float32, | |
189 minDepth: 0.0, | |
190 maxDepth: 1.0, | |
191 ) | |
192 scissor = VkRect2D( | |
193 offset: VkOffset2D(x: 0, y: 0), | |
194 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
195 ) | |
196 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
|
197 |
1191 | 198 # setup viewport |
199 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
200 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
201 | |
202 # bind pipeline, will be loop | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
203 block: |
1191 | 204 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
205 |
1191 | 206 # render object, will be loop |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
207 block: |
1191 | 208 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
209 |
1191 | 210 vkCmdEndRenderPass(cmd) |
211 checkVkResult cmd.vkEndCommandBuffer() |