Mercurial > games > semicongine
comparison test1.nim @ 1190:a3eb305bcac2 compiletime-tests
start of complete and total refactoring: the ULTIMATE
author | sam <sam@basx.dev> |
---|---|
date | Sun, 07 Jul 2024 23:36:16 +0700 |
parents | |
children | 239adab121a3 |
comparison
equal
deleted
inserted
replaced
1189:aceb7697d37c | 1190:a3eb305bcac2 |
---|---|
1 import os | |
2 | |
3 import semicongine/rendering | |
4 | |
5 | |
6 when isMainModule: | |
7 import semicongine/old/platform/window | |
8 import semicongine/old/vulkan/instance | |
9 import semicongine/old/vulkan/device | |
10 import semicongine/old/vulkan/physicaldevice | |
11 import std/options | |
12 | |
13 type | |
14 MeshA = object | |
15 position: GPUArray[Vec3f, VertexBuffer] | |
16 indices: GPUArray[uint16, IndexBuffer] | |
17 InstanceA = object | |
18 rotation: GPUArray[Vec4f, VertexBuffer] | |
19 objPosition: GPUArray[Vec3f, VertexBuffer] | |
20 MaterialA = object | |
21 reflection: float32 | |
22 baseColor: Vec3f | |
23 UniformsA = object | |
24 defaultTexture: Texture[TVec4[uint8]] | |
25 defaultMaterial: GPUValue[MaterialA, UniformBuffer] | |
26 materials: GPUValue[array[3, MaterialA], UniformBuffer] | |
27 materialTextures: array[3, Texture[TVec4[uint8]]] | |
28 ShaderSettings = object | |
29 gamma: float32 | |
30 GlobalsA = object | |
31 fontAtlas: Texture[TVec4[uint8]] | |
32 settings: GPUValue[ShaderSettings, UniformBuffer] | |
33 | |
34 ShaderA = object | |
35 # vertex input | |
36 position {.VertexAttribute.}: Vec3f | |
37 objPosition {.InstanceAttribute.}: Vec3f | |
38 rotation {.InstanceAttribute.}: Vec4f | |
39 # intermediate | |
40 test {.Pass.}: float32 | |
41 test1 {.PassFlat.}: Vec3f | |
42 # output | |
43 color {.ShaderOutput.}: Vec4f | |
44 # descriptor sets | |
45 globals: DescriptorSet[GlobalsA, GlobalSet] | |
46 uniforms: DescriptorSet[UniformsA, MaterialSet] | |
47 # code | |
48 vertexCode: string = "void main() {}" | |
49 fragmentCode: string = "void main() {}" | |
50 | |
51 let w = CreateWindow("test2") | |
52 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") | |
53 | |
54 # TODO: remove those ugly wrappers | |
55 let theInstance = w.CreateInstance( | |
56 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), | |
57 instanceExtensions = @[], | |
58 layers = @["VK_LAYER_KHRONOS_validation"], | |
59 ) | |
60 | |
61 let dev = theInstance.CreateDevice( | |
62 theInstance.GetPhysicalDevices().FilterBestGraphics(), | |
63 enabledExtensions = @[], | |
64 theInstance.GetPhysicalDevices().FilterBestGraphics().FilterForGraphicsPresentationQueues() | |
65 ).vk | |
66 let frameWidth = 100'u32 | |
67 let frameHeight = 100'u32 | |
68 | |
69 # TODO: pack this stuff into a setup method and condense everything a bit | |
70 let pDevice = theInstance.vk.GetPhysicalDevice() | |
71 let qfi = pDevice.GetQueueFamily(VK_QUEUE_GRAPHICS_BIT) | |
72 vulkan = VulkanGlobals( | |
73 instance: theInstance.vk, | |
74 device: dev, | |
75 physicalDevice: pDevice, | |
76 queueFamilyIndex: qfi, | |
77 queue: svkGetDeviceQueue(dev, qfi, VK_QUEUE_GRAPHICS_BIT) | |
78 ) | |
79 | |
80 var myMesh1 = MeshA( | |
81 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), | |
82 ) | |
83 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( | |
84 data: UniformsA( | |
85 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
86 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [ | |
87 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), | |
88 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), | |
89 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), | |
90 ]), | |
91 materialTextures: [ | |
92 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
93 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
94 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
95 ] | |
96 ) | |
97 ) | |
98 var instances1 = InstanceA( | |
99 rotation: GPUArray[Vec4f, VertexBuffer](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]), | |
100 objPosition: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]), | |
101 ) | |
102 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]( | |
103 data: GlobalsA( | |
104 fontAtlas: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), | |
105 settings: GPUValue[ShaderSettings, UniformBuffer](data: ShaderSettings(gamma: 1.0)) | |
106 ) | |
107 ) | |
108 | |
109 # setup for rendering (TODO: swapchain & framebuffers) | |
110 let renderpass = CreateRenderPass(GetSurfaceFormat()) | |
111 | |
112 # shaders | |
113 const shader = ShaderA() | |
114 let shaderObject = CompileShader(shader) | |
115 var pipeline1 = CreatePipeline(renderPass = renderpass, shader = shaderObject) | |
116 | |
117 var renderdata = InitRenderData() | |
118 | |
119 # buffer assignment | |
120 echo "Assigning buffers to GPUData fields" | |
121 | |
122 AssignBuffers(renderdata, myMesh1) | |
123 AssignBuffers(renderdata, instances1) | |
124 AssignBuffers(renderdata, myGlobals) | |
125 AssignBuffers(renderdata, uniforms1) | |
126 | |
127 renderdata.UploadTextures(myGlobals) | |
128 renderdata.UploadTextures(uniforms1) | |
129 | |
130 # copy everything to GPU | |
131 echo "Copying all data to GPU memory" | |
132 UpdateAllGPUBuffers(myMesh1) | |
133 UpdateAllGPUBuffers(instances1) | |
134 UpdateAllGPUBuffers(uniforms1) | |
135 UpdateAllGPUBuffers(myGlobals) | |
136 renderdata.FlushAllMemory() | |
137 | |
138 | |
139 # descriptors | |
140 echo "Writing descriptors" | |
141 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[GlobalSet], myGlobals) | |
142 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[MaterialSet], uniforms1) | |
143 | |
144 | |
145 # command buffer | |
146 var | |
147 commandBufferPool: VkCommandPool | |
148 createInfo = VkCommandPoolCreateInfo( | |
149 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
150 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
151 queueFamilyIndex: vulkan.queueFamilyIndex, | |
152 ) | |
153 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool) | |
154 var | |
155 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
156 allocInfo = VkCommandBufferAllocateInfo( | |
157 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
158 commandPool: commandBufferPool, | |
159 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
160 commandBufferCount: INFLIGHTFRAMES, | |
161 ) | |
162 checkVkResult vkAllocateCommandBuffers(vulkan.device, addr allocInfo, cmdBuffers.ToCPointer) | |
163 | |
164 # start command buffer | |
165 block: | |
166 let | |
167 currentFramebuffer = VkFramebuffer(0) # TODO | |
168 currentFrameInFlight = 1 | |
169 cmd = cmdBuffers[currentFrameInFlight] | |
170 beginInfo = VkCommandBufferBeginInfo( | |
171 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
172 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), | |
173 ) | |
174 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) | |
175 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) | |
176 | |
177 # start renderpass | |
178 block: | |
179 var | |
180 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
181 renderPassInfo = VkRenderPassBeginInfo( | |
182 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
183 renderPass: renderpass, | |
184 framebuffer: currentFramebuffer, # TODO | |
185 renderArea: VkRect2D( | |
186 offset: VkOffset2D(x: 0, y: 0), | |
187 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
188 ), | |
189 clearValueCount: uint32(clearColors.len), | |
190 pClearValues: clearColors.ToCPointer(), | |
191 ) | |
192 viewport = VkViewport( | |
193 x: 0.0, | |
194 y: 0.0, | |
195 width: frameWidth.float32, | |
196 height: frameHeight.float32, | |
197 minDepth: 0.0, | |
198 maxDepth: 1.0, | |
199 ) | |
200 scissor = VkRect2D( | |
201 offset: VkOffset2D(x: 0, y: 0), | |
202 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
203 ) | |
204 vkCmdBeginRenderPass(cmd, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE) | |
205 | |
206 # setup viewport | |
207 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
208 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
209 | |
210 # bind pipeline, will be loop | |
211 block: | |
212 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) | |
213 | |
214 # render object, will be loop | |
215 block: | |
216 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1) | |
217 | |
218 vkCmdEndRenderPass(cmd) | |
219 checkVkResult cmd.vkEndCommandBuffer() |