comparison 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
comparison
equal deleted inserted replaced
1190:a3eb305bcac2 1191:239adab121a3
1 import os 1 import os
2 2
3 import semicongine/rendering 3 import semicongine
4 4
5 5 type
6 when isMainModule: 6 MeshA = object
7 import semicongine/old/platform/window 7 position: GPUArray[Vec3f, VertexBuffer]
8 import semicongine/old/vulkan/instance 8 indices: GPUArray[uint16, IndexBuffer]
9 import semicongine/old/vulkan/device 9 InstanceA = object
10 import semicongine/old/vulkan/physicaldevice 10 rotation: GPUArray[Vec4f, VertexBuffer]
11 import std/options 11 objPosition: GPUArray[Vec3f, VertexBuffer]
12 12 MaterialA = object
13 type 13 reflection: float32
14 MeshA = object 14 baseColor: Vec3f
15 position: GPUArray[Vec3f, VertexBuffer] 15 UniformsA = object
16 indices: GPUArray[uint16, IndexBuffer] 16 defaultTexture: Texture[TVec4[uint8]]
17 InstanceA = object 17 defaultMaterial: GPUValue[MaterialA, UniformBuffer]
18 rotation: GPUArray[Vec4f, VertexBuffer] 18 materials: GPUValue[array[3, MaterialA], UniformBuffer]
19 objPosition: GPUArray[Vec3f, VertexBuffer] 19 materialTextures: array[3, Texture[TVec4[uint8]]]
20 MaterialA = object 20 ShaderSettings = object
21 reflection: float32 21 gamma: float32
22 baseColor: Vec3f 22 GlobalsA = object
23 UniformsA = object 23 fontAtlas: Texture[TVec4[uint8]]
24 defaultTexture: Texture[TVec4[uint8]] 24 settings: GPUValue[ShaderSettings, UniformBuffer]
25 defaultMaterial: GPUValue[MaterialA, UniformBuffer] 25
26 materials: GPUValue[array[3, MaterialA], UniformBuffer] 26 ShaderA = object
27 materialTextures: array[3, Texture[TVec4[uint8]]] 27 # vertex input
28 ShaderSettings = object 28 position {.VertexAttribute.}: Vec3f
29 gamma: float32 29 objPosition {.InstanceAttribute.}: Vec3f
30 GlobalsA = object 30 rotation {.InstanceAttribute.}: Vec4f
31 fontAtlas: Texture[TVec4[uint8]] 31 # intermediate
32 settings: GPUValue[ShaderSettings, UniformBuffer] 32 test {.Pass.}: float32
33 33 test1 {.PassFlat.}: Vec3f
34 ShaderA = object 34 # output
35 # vertex input 35 color {.ShaderOutput.}: Vec4f
36 position {.VertexAttribute.}: Vec3f 36 # descriptor sets
37 objPosition {.InstanceAttribute.}: Vec3f 37 globals: DescriptorSet[GlobalsA, GlobalSet]
38 rotation {.InstanceAttribute.}: Vec4f 38 uniforms: DescriptorSet[UniformsA, MaterialSet]
39 # intermediate 39 # code
40 test {.Pass.}: float32 40 vertexCode: string = "void main() {}"
41 test1 {.PassFlat.}: Vec3f 41 fragmentCode: string = "void main() {}"
42 # output 42
43 color {.ShaderOutput.}: Vec4f 43 let w = CreateWindow("test2")
44 # descriptor sets 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 globals: DescriptorSet[GlobalsA, GlobalSet] 45
46 uniforms: DescriptorSet[UniformsA, MaterialSet] 46 # TODO: remove those ugly wrappers
47 # code 47 let theInstance = w.CreateInstance(
48 vertexCode: string = "void main() {}" 48 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0),
49 fragmentCode: string = "void main() {}" 49 instanceExtensions = @[],
50 50 layers = @["VK_LAYER_KHRONOS_validation"],
51 let w = CreateWindow("test2") 51 )
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") 52
53 53 let dev = theInstance.CreateDevice(
54 # TODO: remove those ugly wrappers 54 theInstance.GetPhysicalDevices().FilterBestGraphics(),
55 let theInstance = w.CreateInstance( 55 enabledExtensions = @[],
56 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), 56 theInstance.GetPhysicalDevices().FilterBestGraphics().FilterForGraphicsPresentationQueues()
57 instanceExtensions = @[], 57 ).vk
58 layers = @["VK_LAYER_KHRONOS_validation"], 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))
59 ) 98 )
60 99 )
61 let dev = theInstance.CreateDevice( 100
62 theInstance.GetPhysicalDevices().FilterBestGraphics(), 101 # setup for rendering (TODO: swapchain & framebuffers)
63 enabledExtensions = @[], 102 let renderpass = CreateRenderPass(GetSurfaceFormat())
64 theInstance.GetPhysicalDevices().FilterBestGraphics().FilterForGraphicsPresentationQueues() 103
65 ).vk 104 # shaders
66 let frameWidth = 100'u32 105 const shader = ShaderA()
67 let frameHeight = 100'u32 106 let shaderObject = CompileShader(shader)
68 107 var pipeline1 = CreatePipeline(renderPass = renderpass, shader = shaderObject)
69 # TODO: pack this stuff into a setup method and condense everything a bit 108
70 let pDevice = theInstance.vk.GetPhysicalDevice() 109 var renderdata = InitRenderData()
71 let qfi = pDevice.GetQueueFamily(VK_QUEUE_GRAPHICS_BIT) 110
72 vulkan = VulkanGlobals( 111 # buffer assignment
73 instance: theInstance.vk, 112 echo "Assigning buffers to GPUData fields"
74 device: dev, 113
75 physicalDevice: pDevice, 114 AssignBuffers(renderdata, myMesh1)
76 queueFamilyIndex: qfi, 115 AssignBuffers(renderdata, instances1)
77 queue: svkGetDeviceQueue(dev, qfi, VK_QUEUE_GRAPHICS_BIT) 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()
129
130
131 # descriptors
132 echo "Writing descriptors"
133 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[GlobalSet], myGlobals)
134 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[MaterialSet], uniforms1)
135
136
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,
78 ) 144 )
79 145 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool)
80 var myMesh1 = MeshA( 146 var
81 position: GPUArray[Vec3f, VertexBuffer](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]), 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,
82 ) 153 )
83 var uniforms1 = DescriptorSet[UniformsA, MaterialSet]( 154 checkVkResult vkAllocateCommandBuffers(vulkan.device, addr allocInfo, cmdBuffers.ToCPointer)
84 data: UniformsA( 155
85 defaultTexture: Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), 156 # start command buffer
86 materials: GPUValue[array[3, MaterialA], UniformBuffer](data: [ 157 block:
87 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)), 158 let
88 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)), 159 currentFramebuffer = VkFramebuffer(0) # TODO
89 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)), 160 currentFrameInFlight = 1
90 ]), 161 cmd = cmdBuffers[currentFrameInFlight]
91 materialTextures: [ 162 beginInfo = VkCommandBufferBeginInfo(
92 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), 163 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
93 Texture[TVec4[uint8]](width: 1, height: 1, data: @[TVec4[uint8]([0'u8, 0'u8, 0'u8, 1'u8])]), 164 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
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 ) 165 )
107 ) 166 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0))
108 167 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo))
109 # setup for rendering (TODO: swapchain & framebuffers) 168
110 let renderpass = CreateRenderPass(GetSurfaceFormat()) 169 # start renderpass
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: 170 block:
166 let 171 var
167 currentFramebuffer = VkFramebuffer(0) # TODO 172 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))]
168 currentFrameInFlight = 1 173 renderPassInfo = VkRenderPassBeginInfo(
169 cmd = cmdBuffers[currentFrameInFlight] 174 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
170 beginInfo = VkCommandBufferBeginInfo( 175 renderPass: renderpass,
171 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, 176 framebuffer: currentFramebuffer, # TODO
172 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), 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(),
173 ) 183 )
174 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) 184 viewport = VkViewport(
175 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) 185 x: 0.0,
176 186 y: 0.0,
177 # start renderpass 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)
197
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
178 block: 203 block:
179 var 204 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight)
180 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))] 205
181 renderPassInfo = VkRenderPassBeginInfo( 206 # render object, will be loop
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: 207 block:
212 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight) 208 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1)
213 209
214 # render object, will be loop 210 vkCmdEndRenderPass(cmd)
215 block: 211 checkVkResult cmd.vkEndCommandBuffer()
216 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1)
217
218 vkCmdEndRenderPass(cmd)
219 checkVkResult cmd.vkEndCommandBuffer()