Mercurial > games > semicongine
annotate semiconginev2/rendering.nim @ 1253:c4f98eb4bb05
fix: a few things
author | sam <sam@basx.dev> |
---|---|
date | Fri, 26 Jul 2024 23:39:24 +0700 |
parents | 01e9f41d35b1 |
children | bfb75c934f4e |
rev | line source |
---|---|
1192 | 1 # in this file: |
2 # - const defintions for rendering | |
3 # - custom pragma defintions for rendering | |
4 # - type defintions for rendering | |
5 # - some utils code that is used in mutiple rendering files | |
6 # - inclusion of all rendering files | |
7 | |
8 | |
9 # const definitions | |
1197 | 10 const INFLIGHTFRAMES* = 2'u32 |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
11 const BUFFER_ALIGNMENT = 64'u64 # align offsets inside buffers along this alignment |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
12 const MEMORY_BLOCK_ALLOCATION_SIZE = 100_000_000'u64 # ca. 100mb per block, seems reasonable |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
13 const BUFFER_ALLOCATION_SIZE = 9_000_000'u64 # ca. 9mb per block, seems reasonable, can put 10 buffers into one memory block |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
14 const MAX_DESCRIPTORSETS = 4 |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
15 const SURFACE_FORMAT* = VK_FORMAT_B8G8R8A8_SRGB |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
16 const DEPTH_FORMAT* = VK_FORMAT_D32_SFLOAT |
1253 | 17 const PUSH_CONSTANT_SIZE = 128 |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
18 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
19 # custom pragmas to classify shader attributes |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
20 template VertexAttribute* {.pragma.} |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
21 template InstanceAttribute* {.pragma.} |
1252 | 22 template PushConstantAttribute* {.pragma.} |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
23 template Pass* {.pragma.} |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
24 template PassFlat* {.pragma.} |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
25 template ShaderOutput* {.pragma.} |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
26 template DescriptorSets* {.pragma.} |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
27 |
1193 | 28 # there is a big, bad global vulkan object |
29 # believe me, this makes everything much, much easier | |
30 | |
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
1218
diff
changeset
|
31 when defined(windows): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
1218
diff
changeset
|
32 include ./rendering/platform/windows |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
1218
diff
changeset
|
33 when defined(linux): |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
1218
diff
changeset
|
34 include ./rendering/platform/linux |
1193 | 35 |
36 type | |
37 VulkanGlobals* = object | |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
38 # populated through InitVulkan proc |
1193 | 39 instance*: VkInstance |
40 device*: VkDevice | |
41 physicalDevice*: VkPhysicalDevice | |
42 surface: VkSurfaceKHR | |
43 window: NativeWindow | |
44 graphicsQueueFamily*: uint32 | |
45 graphicsQueue*: VkQueue | |
1199 | 46 debugMessenger: VkDebugUtilsMessengerEXT |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
47 # populated through the InitSwapchain proc |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
48 swapchain*: Swapchain |
1197 | 49 # unclear as of yet |
50 anisotropy*: float32 = 0 # needs to be enable during device creation | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
51 Renderpass* = ref object |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
52 vk*: VkRenderPass |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
53 samples*: VkSampleCountFlagBits |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
54 depthBuffer*: bool |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
55 Swapchain* = ref object |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
56 # parameters to InitSwapchain, required for swapchain recreation |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
57 renderPass*: RenderPass |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
58 vSync: bool |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
59 # populated through InitSwapchain proc |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
60 vk: VkSwapchainKHR |
1214
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
61 width*: uint32 |
04e446a7eb2b
add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents:
1212
diff
changeset
|
62 height*: uint32 |
1195
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
63 framebuffers: seq[VkFramebuffer] |
cfba2b7e00d0
did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents:
1194
diff
changeset
|
64 framebufferViews: seq[VkImageView] |
1198 | 65 currentFramebufferIndex: uint32 |
66 commandBufferPool: VkCommandPool | |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
67 # depth buffer stuff, if enabled |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
68 depthImage: VkImage |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
69 depthImageView*: VkImageView |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
70 depthMemory: VkDeviceMemory |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
71 # MSAA stuff, if enabled |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
72 msaaImage: VkImage |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
73 msaaImageView*: VkImageView |
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
74 msaaMemory: VkDeviceMemory |
1198 | 75 # frame-in-flight handling |
76 currentFiF: range[0 .. (INFLIGHTFRAMES - 1).int] | |
1197 | 77 queueFinishedFence*: array[INFLIGHTFRAMES.int, VkFence] |
78 imageAvailableSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore] | |
79 renderFinishedSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore] | |
1198 | 80 commandBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer] |
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
81 oldSwapchain: Swapchain |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
82 oldSwapchainCounter: int # swaps until old swapchain will be destroyed |
1193 | 83 |
84 var vulkan*: VulkanGlobals | |
1226 | 85 var fullscreen: bool |
1193 | 86 |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
87 type |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
88 # type aliases |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
89 SupportedGPUType = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
90 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
91 # shader related types |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
92 DescriptorSet*[T: object] = object |
1192 | 93 data*: T |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
94 vk: array[INFLIGHTFRAMES.int, VkDescriptorSet] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
95 Pipeline*[TShader] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
96 vk: VkPipeline |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
97 vertexShaderModule: VkShaderModule |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
98 fragmentShaderModule: VkShaderModule |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
99 layout: VkPipelineLayout |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
100 descriptorSetLayouts*: array[MAX_DESCRIPTORSETS, VkDescriptorSetLayout] |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
101 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
102 # memory/buffer related types |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
103 MemoryBlock* = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
104 vk: VkDeviceMemory |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
105 size: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
106 rawPointer: pointer # if not nil, this is mapped memory |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
107 offsetNextFree: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
108 BufferType* = enum |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
109 VertexBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
110 VertexBufferMapped |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
111 IndexBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
112 IndexBufferMapped |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
113 UniformBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
114 UniformBufferMapped |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
115 Buffer* = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
116 vk: VkBuffer |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
117 size: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
118 rawPointer: pointer # if not nil, buffer is using mapped memory |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
119 offsetNextFree: uint64 |
1231 | 120 memoryOffset: uint64 |
121 memory: VkDeviceMemory | |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
122 GPUArray*[T: SupportedGPUType, TBuffer: static BufferType] = object |
1192 | 123 data*: seq[T] |
1202 | 124 buffer*: Buffer |
125 offset*: uint64 | |
1210 | 126 GPUValue*[T: object, TBuffer: static BufferType] = object |
1192 | 127 data*: T |
1231 | 128 buffer*: Buffer |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
129 offset: uint64 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
130 GPUData = GPUArray | GPUValue |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
131 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
132 RenderData* = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
133 descriptorPool: VkDescriptorPool |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
134 memory: array[VK_MAX_MEMORY_TYPES.int, seq[MemoryBlock]] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
135 buffers: array[BufferType, seq[Buffer]] |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
136 images: seq[VkImage] |
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
137 imageViews: seq[VkImageView] |
1201 | 138 samplers: seq[VkSampler] |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
139 |
1192 | 140 template ForDescriptorFields(shader: typed, fieldname, valuename, typename, countname, bindingNumber, body: untyped): untyped = |
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
141 var `bindingNumber` {.inject.} = 0'u32 |
1192 | 142 for theFieldname, value in fieldPairs(shader): |
1228 | 143 when typeof(value) is Image: |
1192 | 144 block: |
145 const `fieldname` {.inject.} = theFieldname | |
146 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER | |
147 const `countname` {.inject.} = 1'u32 | |
148 let `valuename` {.inject.} = value | |
149 body | |
150 `bindingNumber`.inc | |
1210 | 151 elif typeof(value) is GPUValue: |
1192 | 152 block: |
153 const `fieldname` {.inject.} = theFieldname | |
154 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER | |
155 const `countname` {.inject.} = 1'u32 | |
156 let `valuename` {.inject.} = value | |
157 body | |
158 `bindingNumber`.inc | |
159 elif typeof(value) is array: | |
1228 | 160 when elementType(value) is Image: |
1192 | 161 block: |
162 const `fieldname` {.inject.} = theFieldname | |
163 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER | |
164 const `countname` {.inject.} = uint32(typeof(value).len) | |
165 let `valuename` {.inject.} = value | |
166 body | |
167 `bindingNumber`.inc | |
1210 | 168 elif elementType(value) is GPUValue: |
1192 | 169 block: |
170 const `fieldname` {.inject.} = theFieldname | |
171 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER | |
1210 | 172 const `countname` {.inject.} = len(value).uint32 |
1192 | 173 let `valuename` {.inject.} = value |
174 body | |
175 `bindingNumber`.inc | |
176 else: | |
177 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(value)).} | |
1210 | 178 else: |
179 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(value)).} | |
1192 | 180 |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
181 include ./rendering/vulkan_wrappers |
1199 | 182 include ./rendering/renderpasses |
1194 | 183 include ./rendering/swapchain |
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
184 include ./rendering/shaders |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
185 include ./rendering/renderer |
1193 | 186 |
1199 | 187 proc debugCallback( |
188 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT, | |
189 messageTypes: VkDebugUtilsMessageTypeFlagsEXT, | |
190 pCallbackData: ptr VkDebugUtilsMessengerCallbackDataEXT, | |
191 userData: pointer | |
192 ): VkBool32 {.cdecl.} = | |
193 const LOG_LEVEL_MAPPING = { | |
194 VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: lvlDebug, | |
195 VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: lvlInfo, | |
196 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: lvlWarn, | |
197 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: lvlError, | |
198 }.toTable | |
199 log LOG_LEVEL_MAPPING[messageSeverity], &"{toEnums messageTypes}: {pCallbackData.pMessage}" | |
200 if messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: | |
201 stderr.writeLine "-----------------------------------" | |
202 stderr.write getStackTrace() | |
203 stderr.writeLine LOG_LEVEL_MAPPING[messageSeverity], &"{toEnums messageTypes}: {pCallbackData.pMessage}" | |
204 stderr.writeLine "-----------------------------------" | |
205 let errorMsg = getStackTrace() & &"\n{toEnums messageTypes}: {pCallbackData.pMessage}" | |
206 raise newException(Exception, errorMsg) | |
207 return false | |
1193 | 208 |
1204 | 209 proc InitVulkan*(appName: string = "semicongine app") = |
1193 | 210 |
211 # instance creation | |
1198 | 212 |
213 # enagle all kind of debug stuff | |
1193 | 214 when not defined(release): |
215 let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface", "VK_EXT_debug_utils"] | |
216 let layers: seq[string] = if hasValidationLayer(): @["VK_LAYER_KHRONOS_validation"] else: @[] | |
1198 | 217 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") |
1193 | 218 else: |
219 let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface"] | |
220 let layers: seq[string] | |
221 | |
222 var | |
223 layersC = allocCStringArray(layers) | |
224 instanceExtensionsC = allocCStringArray(requiredExtensions) | |
225 defer: | |
226 deallocCStringArray(layersC) | |
227 deallocCStringArray(instanceExtensionsC) | |
228 | |
229 var | |
230 appinfo = VkApplicationInfo( | |
231 sType: VK_STRUCTURE_TYPE_APPLICATION_INFO, | |
232 pApplicationName: appName, | |
233 pEngineName: "semicongine", | |
234 apiVersion: VK_MAKE_API_VERSION(0, 1, 3, 0), | |
235 ) | |
236 createinfo = VkInstanceCreateInfo( | |
237 sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, | |
238 pApplicationInfo: addr(appinfo), | |
239 enabledLayerCount: layers.len.uint32, | |
240 ppEnabledLayerNames: layersC, | |
241 enabledExtensionCount: requiredExtensions.len.uint32, | |
242 ppEnabledExtensionNames: instanceExtensionsC | |
243 ) | |
1204 | 244 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(vulkan.instance)) |
245 loadVulkan(vulkan.instance) | |
1193 | 246 |
247 # load extensions | |
248 # | |
249 for extension in requiredExtensions: | |
1204 | 250 loadExtension(vulkan.instance, $extension) |
251 vulkan.window = CreateWindow(appName) | |
252 vulkan.surface = CreateNativeSurface(vulkan.instance, vulkan.window) | |
1193 | 253 |
254 # logical device creation | |
255 | |
256 # TODO: allowing support for physical devices without hasUniformBufferStandardLayout | |
257 # would require us to ship different shaders, so we don't support standard layout | |
258 # if that will be added, check the function vulkan/shaders.nim:glslUniforms and update accordingly | |
259 # let hasUniformBufferStandardLayout = "VK_KHR_uniform_buffer_standard_layout" in physicalDevice.getExtensions() | |
260 # var deviceExtensions = @["VK_KHR_swapchain", "VK_KHR_uniform_buffer_standard_layout"] | |
261 var deviceExtensions = @["VK_KHR_swapchain"] | |
262 for extension in deviceExtensions: | |
1204 | 263 loadExtension(vulkan.instance, extension) |
1193 | 264 |
1199 | 265 when not defined(release): |
266 var debugMessengerCreateInfo = VkDebugUtilsMessengerCreateInfoEXT( | |
267 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, | |
268 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT.items.toSeq.toBits, | |
269 messageType: VkDebugUtilsMessageTypeFlagBitsEXT.items.toSeq.toBits, | |
270 pfnUserCallback: debugCallback, | |
271 pUserData: nil, | |
272 ) | |
273 checkVkResult vkCreateDebugUtilsMessengerEXT( | |
1204 | 274 vulkan.instance, |
1199 | 275 addr(debugMessengerCreateInfo), |
276 nil, | |
1204 | 277 addr(vulkan.debugMessenger) |
1199 | 278 ) |
279 | |
1193 | 280 # get physical device and graphics queue family |
1204 | 281 vulkan.physicalDevice = GetBestPhysicalDevice(vulkan.instance) |
282 vulkan.graphicsQueueFamily = GetQueueFamily(vulkan.physicalDevice, VK_QUEUE_GRAPHICS_BIT) | |
1193 | 283 |
284 let | |
285 priority = cfloat(1) | |
286 queueInfo = VkDeviceQueueCreateInfo( | |
287 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, | |
1204 | 288 queueFamilyIndex: vulkan.graphicsQueueFamily, |
1193 | 289 queueCount: 1, |
290 pQueuePriorities: addr(priority), | |
291 ) | |
292 deviceExtensionsC = allocCStringArray(deviceExtensions) | |
293 defer: deallocCStringArray(deviceExtensionsC) | |
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1239
diff
changeset
|
294 let enabledFeatures = VkPhysicalDeviceFeatures( |
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1239
diff
changeset
|
295 fillModeNonSolid: true, |
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1239
diff
changeset
|
296 wideLines: true, |
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1239
diff
changeset
|
297 largePoints: true, |
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1239
diff
changeset
|
298 ) |
1193 | 299 var createDeviceInfo = VkDeviceCreateInfo( |
300 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, | |
301 queueCreateInfoCount: 1, | |
302 pQueueCreateInfos: addr(queueInfo), | |
303 enabledLayerCount: 0, | |
304 ppEnabledLayerNames: nil, | |
305 enabledExtensionCount: uint32(deviceExtensions.len), | |
306 ppEnabledExtensionNames: deviceExtensionsC, | |
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1239
diff
changeset
|
307 pEnabledFeatures: addr(enabledFeatures), |
1193 | 308 ) |
309 checkVkResult vkCreateDevice( | |
1204 | 310 physicalDevice = vulkan.physicalDevice, |
1193 | 311 pCreateInfo = addr createDeviceInfo, |
312 pAllocator = nil, | |
1204 | 313 pDevice = addr vulkan.device |
1193 | 314 ) |
1204 | 315 vulkan.graphicsQueue = svkGetDeviceQueue(vulkan.device, vulkan.graphicsQueueFamily, VK_QUEUE_GRAPHICS_BIT) |
1193 | 316 |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
317 proc ClearSwapchain*() = |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
318 assert vulkan.swapchain != nil, "Swapchain has not been initialized yet" |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
319 DestroySwapchain(vulkan.swapchain) |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
320 vulkan.swapchain = nil |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
321 |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
322 proc SetupSwapchain*(renderPass: RenderPass, vSync: bool = false) = |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
323 assert vulkan.swapchain == nil, "Swapchain has already been initialized yet" |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
324 vulkan.swapchain = InitSwapchain(renderPass, vSync = vSync) |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
325 |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
326 proc DestroyVulkan*() = |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
327 if vulkan.swapchain != nil: |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
328 DestroySwapchain(vulkan.swapchain) |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
329 vkDestroyDevice(vulkan.device, nil) |
1201 | 330 vkDestroySurfaceKHR(vulkan.instance, vulkan.surface, nil) |
331 vkDestroyDebugUtilsMessengerEXT(vulkan.instance, vulkan.debugMessenger, nil) | |
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1199
diff
changeset
|
332 vkDestroyInstance(vulkan.instance, nil) |
1226 | 333 |
334 proc ShowSystemCursor*() = vulkan.window.ShowSystemCursor() | |
335 proc HideSystemCursor*() = vulkan.window.HideSystemCursor() | |
336 proc Fullscreen*(): bool = fullscreen | |
337 proc `Fullscreen=`*(enable: bool) = | |
338 if enable != fullscreen: | |
339 fullscreen = enable | |
340 vulkan.window.Fullscreen(fullscreen) | |
341 | |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
342 proc GetAspectRatio*(): float32 = |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
343 assert vulkan.swapchain != nil, "Swapchain has not been initialized yet" |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
344 vulkan.swapchain.width.float32 / vulkan.swapchain.height.float32 |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
345 |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
346 proc currentFiF*(): int = |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
347 assert vulkan.swapchain != nil, "Swapchain has not been initialized yet" |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1234
diff
changeset
|
348 vulkan.swapchain.currentFiF |
1226 | 349 |
350 proc MaxFramebufferSampleCount*(maxSamples = VK_SAMPLE_COUNT_8_BIT): VkSampleCountFlagBits = | |
351 let limits = svkGetPhysicalDeviceProperties().limits | |
352 let available = VkSampleCountFlags( | |
353 limits.framebufferColorSampleCounts.uint32 and limits.framebufferDepthSampleCounts.uint32 | |
354 ).toEnums | |
355 return min(max(available), maxSamples) | |
356 | |
357 | |
1228 | 358 proc `[]`*(image: Image, x, y: uint32): auto = |
359 assert x < image.width, &"{x} < {image.width} is not true" | |
360 assert y < image.height, &"{y} < {image.height} is not true" | |
1226 | 361 |
1234
841e12f33c47
add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
1233
diff
changeset
|
362 image.data[y * image.width + x] |
1226 | 363 |
1228 | 364 proc `[]=`*[T](image: var Image[T], x, y: uint32, value: T) = |
365 assert x < image.width | |
366 assert y < image.height | |
1226 | 367 |
1234
841e12f33c47
add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
1233
diff
changeset
|
368 image.data[y * image.width + x] = value |