Mercurial > games > semicongine
annotate test1.nim @ 1193:5aa1184fa5eb compiletime-tests
did: a few more things
author | sam <sam@basx.dev> |
---|---|
date | Mon, 08 Jul 2024 16:15:33 +0700 |
parents | 90bf0cab1d02 |
children | 82feceae80b1 |
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() |
1191 | 78 |
79 # shaders | |
1192 | 80 var pipeline1 = CreatePipeline[ShaderA](renderPass = renderpass) |
1191 | 81 |
82 var renderdata = InitRenderData() | |
83 | |
84 # buffer assignment | |
85 echo "Assigning buffers to GPUData fields" | |
86 | |
87 AssignBuffers(renderdata, myMesh1) | |
88 AssignBuffers(renderdata, instances1) | |
89 AssignBuffers(renderdata, myGlobals) | |
90 AssignBuffers(renderdata, uniforms1) | |
91 | |
92 renderdata.UploadTextures(myGlobals) | |
93 renderdata.UploadTextures(uniforms1) | |
94 | |
95 # copy everything to GPU | |
96 echo "Copying all data to GPU memory" | |
97 UpdateAllGPUBuffers(myMesh1) | |
98 UpdateAllGPUBuffers(instances1) | |
99 UpdateAllGPUBuffers(uniforms1) | |
100 UpdateAllGPUBuffers(myGlobals) | |
101 renderdata.FlushAllMemory() | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
102 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
103 |
1191 | 104 # descriptors |
105 echo "Writing descriptors" | |
1192 | 106 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(GlobalSet), myGlobals) |
107 InitDescriptorSet(renderdata, pipeline1.GetLayoutFor(MaterialSet), uniforms1) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
108 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
109 |
1191 | 110 # command buffer |
111 var | |
112 commandBufferPool: VkCommandPool | |
113 createInfo = VkCommandPoolCreateInfo( | |
114 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | |
115 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT], | |
1193 | 116 queueFamilyIndex: vulkan.graphicsQueueFamily, |
1191 | 117 ) |
118 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool) | |
119 var | |
120 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] | |
121 allocInfo = VkCommandBufferAllocateInfo( | |
122 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | |
123 commandPool: commandBufferPool, | |
124 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY, | |
125 commandBufferCount: INFLIGHTFRAMES, | |
126 ) | |
127 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
|
128 |
1191 | 129 # start command buffer |
130 block: | |
131 let | |
132 currentFramebuffer = VkFramebuffer(0) # TODO | |
133 currentFrameInFlight = 1 | |
134 cmd = cmdBuffers[currentFrameInFlight] | |
135 beginInfo = VkCommandBufferBeginInfo( | |
136 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | |
137 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
|
138 ) |
1191 | 139 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
140 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
141 |
1191 | 142 # start renderpass |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
143 block: |
1191 | 144 var |
145 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] | |
146 renderPassInfo = VkRenderPassBeginInfo( | |
147 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | |
148 renderPass: renderpass, | |
149 framebuffer: currentFramebuffer, # TODO | |
150 renderArea: VkRect2D( | |
151 offset: VkOffset2D(x: 0, y: 0), | |
152 extent: VkExtent2D(width: frameWidth, height: frameHeight), | |
153 ), | |
154 clearValueCount: uint32(clearColors.len), | |
155 pClearValues: clearColors.ToCPointer(), | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
156 ) |
1191 | 157 viewport = VkViewport( |
158 x: 0.0, | |
159 y: 0.0, | |
160 width: frameWidth.float32, | |
161 height: frameHeight.float32, | |
162 minDepth: 0.0, | |
163 maxDepth: 1.0, | |
164 ) | |
165 scissor = VkRect2D( | |
166 offset: VkOffset2D(x: 0, y: 0), | |
167 extent: VkExtent2D(width: frameWidth, height: frameHeight) | |
168 ) | |
169 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
|
170 |
1191 | 171 # setup viewport |
172 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport)) | |
173 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor)) | |
174 | |
175 # bind pipeline, will be loop | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
176 block: |
1191 | 177 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
178 |
1191 | 179 # render object, will be loop |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
180 block: |
1191 | 181 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1) |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
182 |
1191 | 183 vkCmdEndRenderPass(cmd) |
184 checkVkResult cmd.vkEndCommandBuffer() |