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