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