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