annotate semiconginev2/rendering.nim @ 1240:42eeb59f3a43

add: more tests, line and point rendering
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 12:42:35 +0700
parents 69489a678141
children e8b3dc80e48e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
1 # in this file:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
2 # - const defintions for rendering
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
3 # - custom pragma defintions for rendering
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
4 # - type defintions for rendering
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
5 # - some utils code that is used in mutiple rendering files
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
6 # - inclusion of all rendering files
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
7
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
8
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
9 # const definitions
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
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
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
26 # there is a big, bad global vulkan object
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
27 # believe me, this makes everything much, much easier
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
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
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
33
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
34 type
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
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
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
37 instance*: VkInstance
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
38 device*: VkDevice
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
39 physicalDevice*: VkPhysicalDevice
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
40 surface: VkSurfaceKHR
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
41 window: NativeWindow
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
42 graphicsQueueFamily*: uint32
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
43 graphicsQueue*: VkQueue
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
44 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
45 # 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
46 swapchain*: Swapchain
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
47 # unclear as of yet
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
48 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
49 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
50 vk*: VkRenderPass
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
51 samples*: VkSampleCountFlagBits
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
52 depthBuffer*: bool
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
53 Swapchain* = ref object
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
54 # 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
55 renderPass*: RenderPass
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
56 vSync: bool
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
57 # populated through InitSwapchain proc
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
58 vk: VkSwapchainKHR
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1212
diff changeset
59 width*: uint32
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1212
diff changeset
60 height*: uint32
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
61 framebuffers: seq[VkFramebuffer]
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
62 framebufferViews: seq[VkImageView]
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
63 currentFramebufferIndex: uint32
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
64 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
65 # 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
66 depthImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
67 depthImageView*: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
68 depthMemory: VkDeviceMemory
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
69 # 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
70 msaaImage: VkImage
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
71 msaaImageView*: VkImageView
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
72 msaaMemory: VkDeviceMemory
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
73 # frame-in-flight handling
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
74 currentFiF: range[0 .. (INFLIGHTFRAMES - 1).int]
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
75 queueFinishedFence*: array[INFLIGHTFRAMES.int, VkFence]
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
76 imageAvailableSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore]
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
77 renderFinishedSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore]
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
78 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
79 oldSwapchain: Swapchain
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
80 oldSwapchainCounter: int # swaps until old swapchain will be destroyed
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
81
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
82 var vulkan*: VulkanGlobals
1226
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
83 var fullscreen: bool
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
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
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
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
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
121 memoryOffset: uint64
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
122 memory: VkDeviceMemory
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
123 Image*[T: PixelType] = object
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
124 width*: uint32
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
125 height*: uint32
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
126 interpolation*: VkFilter = VK_FILTER_LINEAR
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
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
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
134 data*: seq[T]
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
135 buffer*: Buffer
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
136 offset*: uint64
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
137 GPUValue*[T: object, TBuffer: static BufferType] = object
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
138 data*: T
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
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
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
149 samplers: seq[VkSampler]
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
150
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
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
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
153 for theFieldname, value in fieldPairs(shader):
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
154 when typeof(value) is Image:
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
155 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
156 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
157 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
158 const `countname` {.inject.} = 1'u32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
159 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
160 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
161 `bindingNumber`.inc
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
162 elif typeof(value) is GPUValue:
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
163 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
164 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
165 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
166 const `countname` {.inject.} = 1'u32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
167 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
168 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
169 `bindingNumber`.inc
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
170 elif typeof(value) is array:
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
171 when elementType(value) is Image:
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
172 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
173 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
174 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
175 const `countname` {.inject.} = uint32(typeof(value).len)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
176 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
177 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
178 `bindingNumber`.inc
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
179 elif elementType(value) is GPUValue:
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
180 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
181 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
182 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
183 const `countname` {.inject.} = len(value).uint32
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
184 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
185 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
186 `bindingNumber`.inc
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
187 else:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
188 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(value)).}
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
189 else:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1205
diff changeset
190 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(value)).}
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
191
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
192 include ./rendering/vulkan_wrappers
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
193 include ./rendering/renderpasses
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
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
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
197
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
198 proc debugCallback(
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
199 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
200 messageTypes: VkDebugUtilsMessageTypeFlagsEXT,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
201 pCallbackData: ptr VkDebugUtilsMessengerCallbackDataEXT,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
202 userData: pointer
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
203 ): VkBool32 {.cdecl.} =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
204 const LOG_LEVEL_MAPPING = {
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
205 VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: lvlDebug,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
206 VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: lvlInfo,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
207 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: lvlWarn,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
208 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: lvlError,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
209 }.toTable
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
210 log LOG_LEVEL_MAPPING[messageSeverity], &"{toEnums messageTypes}: {pCallbackData.pMessage}"
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
211 if messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
212 stderr.writeLine "-----------------------------------"
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
213 stderr.write getStackTrace()
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
214 stderr.writeLine LOG_LEVEL_MAPPING[messageSeverity], &"{toEnums messageTypes}: {pCallbackData.pMessage}"
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
215 stderr.writeLine "-----------------------------------"
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
216 let errorMsg = getStackTrace() & &"\n{toEnums messageTypes}: {pCallbackData.pMessage}"
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
217 raise newException(Exception, errorMsg)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
218 return false
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
219
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
220 proc InitVulkan*(appName: string = "semicongine app") =
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
221
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
222 # instance creation
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
223
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
224 # enagle all kind of debug stuff
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
225 when not defined(release):
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
226 let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface", "VK_EXT_debug_utils"]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
227 let layers: seq[string] = if hasValidationLayer(): @["VK_LAYER_KHRONOS_validation"] else: @[]
1198
96a094cd0c78 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1197
diff changeset
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
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
229 else:
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
230 let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface"]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
231 let layers: seq[string]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
232
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
233 var
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
234 layersC = allocCStringArray(layers)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
235 instanceExtensionsC = allocCStringArray(requiredExtensions)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
236 defer:
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
237 deallocCStringArray(layersC)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
238 deallocCStringArray(instanceExtensionsC)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
239
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
240 var
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
241 appinfo = VkApplicationInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
242 sType: VK_STRUCTURE_TYPE_APPLICATION_INFO,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
243 pApplicationName: appName,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
244 pEngineName: "semicongine",
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
245 apiVersion: VK_MAKE_API_VERSION(0, 1, 3, 0),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
246 )
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
247 createinfo = VkInstanceCreateInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
248 sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
249 pApplicationInfo: addr(appinfo),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
250 enabledLayerCount: layers.len.uint32,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
251 ppEnabledLayerNames: layersC,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
252 enabledExtensionCount: requiredExtensions.len.uint32,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
253 ppEnabledExtensionNames: instanceExtensionsC
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
254 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
255 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(vulkan.instance))
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
256 loadVulkan(vulkan.instance)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
257
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
258 # load extensions
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
259 #
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
260 for extension in requiredExtensions:
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
261 loadExtension(vulkan.instance, $extension)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
262 vulkan.window = CreateWindow(appName)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
263 vulkan.surface = CreateNativeSurface(vulkan.instance, vulkan.window)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
264
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
265 # logical device creation
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
266
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
267 # TODO: allowing support for physical devices without hasUniformBufferStandardLayout
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
268 # would require us to ship different shaders, so we don't support standard layout
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
269 # if that will be added, check the function vulkan/shaders.nim:glslUniforms and update accordingly
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
270 # let hasUniformBufferStandardLayout = "VK_KHR_uniform_buffer_standard_layout" in physicalDevice.getExtensions()
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
271 # var deviceExtensions = @["VK_KHR_swapchain", "VK_KHR_uniform_buffer_standard_layout"]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
272 var deviceExtensions = @["VK_KHR_swapchain"]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
273 for extension in deviceExtensions:
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
274 loadExtension(vulkan.instance, extension)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
275
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
276 when not defined(release):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
277 var debugMessengerCreateInfo = VkDebugUtilsMessengerCreateInfoEXT(
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
278 sType: VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
279 messageSeverity: VkDebugUtilsMessageSeverityFlagBitsEXT.items.toSeq.toBits,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
280 messageType: VkDebugUtilsMessageTypeFlagBitsEXT.items.toSeq.toBits,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
281 pfnUserCallback: debugCallback,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
282 pUserData: nil,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
283 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
284 checkVkResult vkCreateDebugUtilsMessengerEXT(
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
285 vulkan.instance,
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
286 addr(debugMessengerCreateInfo),
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
287 nil,
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
288 addr(vulkan.debugMessenger)
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
289 )
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents: 1198
diff changeset
290
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
291 # get physical device and graphics queue family
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
292 vulkan.physicalDevice = GetBestPhysicalDevice(vulkan.instance)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
293 vulkan.graphicsQueueFamily = GetQueueFamily(vulkan.physicalDevice, VK_QUEUE_GRAPHICS_BIT)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
294
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
295 let
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
296 priority = cfloat(1)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
297 queueInfo = VkDeviceQueueCreateInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
298 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
299 queueFamilyIndex: vulkan.graphicsQueueFamily,
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
300 queueCount: 1,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
301 pQueuePriorities: addr(priority),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
302 )
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
303 deviceExtensionsC = allocCStringArray(deviceExtensions)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
304 defer: deallocCStringArray(deviceExtensionsC)
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
305 let enabledFeatures = VkPhysicalDeviceFeatures(
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
306 fillModeNonSolid: true,
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
307 wideLines: true,
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
308 largePoints: true,
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
309 )
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
310 var createDeviceInfo = VkDeviceCreateInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
311 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
312 queueCreateInfoCount: 1,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
313 pQueueCreateInfos: addr(queueInfo),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
314 enabledLayerCount: 0,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
315 ppEnabledLayerNames: nil,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
316 enabledExtensionCount: uint32(deviceExtensions.len),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
317 ppEnabledExtensionNames: deviceExtensionsC,
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
318 pEnabledFeatures: addr(enabledFeatures),
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
319 )
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
320 checkVkResult vkCreateDevice(
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
321 physicalDevice = vulkan.physicalDevice,
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
322 pCreateInfo = addr createDeviceInfo,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
323 pAllocator = nil,
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
324 pDevice = addr vulkan.device
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
325 )
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
326 vulkan.graphicsQueue = svkGetDeviceQueue(vulkan.device, vulkan.graphicsQueueFamily, VK_QUEUE_GRAPHICS_BIT)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
327
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1234
diff changeset
328 proc ClearSwapchain*() =
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1234
diff changeset
329 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
330 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
331 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
332
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1234
diff changeset
333 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
334 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
335 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
336
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
337 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
338 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
339 DestroySwapchain(vulkan.swapchain)
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
340 vkDestroyDevice(vulkan.device, nil)
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
341 vkDestroySurfaceKHR(vulkan.instance, vulkan.surface, nil)
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
342 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
343 vkDestroyInstance(vulkan.instance, nil)
1226
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
344
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
345 proc ShowSystemCursor*() = vulkan.window.ShowSystemCursor()
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
346 proc HideSystemCursor*() = vulkan.window.HideSystemCursor()
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
347 proc Fullscreen*(): bool = fullscreen
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
348 proc `Fullscreen=`*(enable: bool) =
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
349 if enable != fullscreen:
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
350 fullscreen = enable
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
351 vulkan.window.Fullscreen(fullscreen)
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
352
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1234
diff changeset
353 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
354 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
355 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
356
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1234
diff changeset
357 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
358 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
359 vulkan.swapchain.currentFiF
1226
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
360
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
361 proc MaxFramebufferSampleCount*(maxSamples = VK_SAMPLE_COUNT_8_BIT): VkSampleCountFlagBits =
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
362 let limits = svkGetPhysicalDeviceProperties().limits
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
363 let available = VkSampleCountFlags(
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
364 limits.framebufferColorSampleCounts.uint32 and limits.framebufferDepthSampleCounts.uint32
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
365 ).toEnums
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
366 return min(max(available), maxSamples)
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
367
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
368
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
369 proc `[]`*(image: Image, x, y: uint32): auto =
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
370 assert x < image.width, &"{x} < {image.width} is not true"
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
371 assert y < image.height, &"{y} < {image.height} is not true"
1226
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
372
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents: 1233
diff changeset
373 image.data[y * image.width + x]
1226
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
374
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
375 proc `[]=`*[T](image: var Image[T], x, y: uint32, value: T) =
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
376 assert x < image.width
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1226
diff changeset
377 assert y < image.height
1226
c8e3037aca66 add: contrib stuff
sam <sam@basx.dev>
parents: 1224
diff changeset
378
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents: 1233
diff changeset
379 image.data[y * image.width + x] = value