annotate semicongine/rendering.nim @ 1197:f6a0dc7ad052 compiletime-tests

sync from bedroom to office
author sam <sam@basx.dev>
date Fri, 12 Jul 2024 23:06:29 +0700
parents 82feceae80b1
children 96a094cd0c78
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
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
14
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
15 # custom pragmas to classify shader attributes
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
16 template VertexAttribute* {.pragma.}
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
17 template InstanceAttribute* {.pragma.}
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
18 template Pass* {.pragma.}
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
19 template PassFlat* {.pragma.}
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
20 template ShaderOutput* {.pragma.}
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
21
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
22 # there is a big, bad global vulkan object
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
23 # believe me, this makes everything much, much easier
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
24
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
25 include ./platform/window # for NativeWindow
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
26 include ./platform/surface # For CreateNativeSurface
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
27
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
28 type
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
29 VulkanGlobals* = object
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
30 # populated through InitVulkan proc
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
31 instance*: VkInstance
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
32 device*: VkDevice
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
33 physicalDevice*: VkPhysicalDevice
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
34 surface: VkSurfaceKHR
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
35 window: NativeWindow
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
36 graphicsQueueFamily*: uint32
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
37 graphicsQueue*: VkQueue
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
38 # unclear as of yet
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
39 anisotropy*: float32 = 0 # needs to be enable during device creation
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
40 Swapchain = object
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
41 # parameters to InitSwapchain, required for swapchain recreation
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
42 renderPass: VkRenderPass
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
43 vSync: bool
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
44 samples: VkSampleCountFlagBits
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
45 # populated through InitSwapchain proc
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
46 vk: VkSwapchainKHR
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
47 msaaImage: VkImage
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
48 msaaMemory: VkDeviceMemory
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
49 msaaImageView: VkImageView
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
50 framebuffers: seq[VkFramebuffer]
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
51 framebufferViews: seq[VkImageView]
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
52 queueFinishedFence*: array[INFLIGHTFRAMES.int, VkFence]
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
53 imageAvailableSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore]
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
54 renderFinishedSemaphore*: array[INFLIGHTFRAMES.int, VkSemaphore]
1196
82feceae80b1 sync from bedroom to office
sam <sam@basx.dev>
parents: 1195
diff changeset
55 currentFiF: range[0 .. (INFLIGHTFRAMES - 1).int]
1197
f6a0dc7ad052 sync from bedroom to office
sam <sam@basx.dev>
parents: 1196
diff changeset
56 currentFramebufferIndex: uint32
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
57
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
58 var vulkan*: VulkanGlobals
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
59
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
60 type
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
61 # type aliases
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
62 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
63 TextureType = TVec1[uint8] | TVec2[uint8] | TVec3[uint8] | TVec4[uint8]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
65 IndexType = enum
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
66 None, UInt8, UInt16, UInt32
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
67
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
68 # shader related types
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
69 DescriptorSetType* = enum
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
70 GlobalSet
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
71 MaterialSet
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
72 DescriptorSet*[T: object, sType: static DescriptorSetType] = object
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
73 data*: T
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
74 vk: array[INFLIGHTFRAMES.int, VkDescriptorSet]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
75 Pipeline*[TShader] = object
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
76 vk: VkPipeline
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
77 layout: VkPipelineLayout
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
78 descriptorSetLayouts: array[DescriptorSetType, VkDescriptorSetLayout]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
79
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
80 # memory/buffer related types
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
81 MemoryBlock* = object
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
82 vk: VkDeviceMemory
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
83 size: uint64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
84 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
85 offsetNextFree: uint64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
86 BufferType* = enum
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
87 VertexBuffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
88 VertexBufferMapped
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
89 IndexBuffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
90 IndexBufferMapped
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
91 UniformBuffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
92 UniformBufferMapped
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
93 Buffer* = object
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
94 vk: VkBuffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
95 size: uint64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
96 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
97 offsetNextFree: uint64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
98 Texture*[T: TextureType] = object
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
99 vk: VkImage
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
100 imageview: VkImageView
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
101 sampler: VkSampler
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
102 width*: uint32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
103 height*: uint32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
104 data*: seq[T]
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
105 GPUArray*[T: SupportedGPUType, TBuffer: static BufferType] = object
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
106 data*: seq[T]
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
107 buffer: Buffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
108 offset: uint64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
109 GPUValue*[T: object|array, TBuffer: static BufferType] = object
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
110 data*: T
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
111 buffer: Buffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
112 offset: uint64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
113 GPUData = GPUArray | GPUValue
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
114
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
115 RenderData* = object
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
116 descriptorPool: VkDescriptorPool
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
117 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
118 buffers: array[BufferType, seq[Buffer]]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
119
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
120 template ForDescriptorFields(shader: typed, fieldname, valuename, typename, countname, bindingNumber, body: untyped): untyped =
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
121 var `bindingNumber` {.inject.} = 1'u32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
122 for theFieldname, value in fieldPairs(shader):
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
123 when typeof(value) is Texture:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
124 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
125 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
126 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
127 const `countname` {.inject.} = 1'u32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
128 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
129 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
130 `bindingNumber`.inc
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
131 elif typeof(value) is object:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
132 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
133 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
134 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
135 const `countname` {.inject.} = 1'u32
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
136 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
137 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
138 `bindingNumber`.inc
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
139 elif typeof(value) is array:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
140 when elementType(value) is Texture:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
141 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
142 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
143 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
144 const `countname` {.inject.} = uint32(typeof(value).len)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
145 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
146 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
147 `bindingNumber`.inc
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
148 elif elementType(value) is object:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
149 block:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
150 const `fieldname` {.inject.} = theFieldname
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
151 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
152 const `countname` {.inject.} = uint32(typeof(value).len)
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
153 let `valuename` {.inject.} = value
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
154 body
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
155 `bindingNumber`.inc
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
156 else:
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
157 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(value)).}
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1191
diff changeset
158
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
159 include ./rendering/vulkan_wrappers
1194
397c681f9c0c swapchain and stuff
sam <sam@basx.dev>
parents: 1193
diff changeset
160 include ./rendering/swapchain
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
161 include ./rendering/shaders
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
162 include ./rendering/renderer
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
163
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
164
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
165 proc InitVulkan(appName: string = "semicongine app"): VulkanGlobals =
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
166
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
167 include ./platform/vulkan_extensions # for REQUIRED_PLATFORM_EXTENSIONS
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
168
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
169 # instance creation
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
170 #
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
171 when not defined(release):
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
172 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
173 let layers: seq[string] = if hasValidationLayer(): @["VK_LAYER_KHRONOS_validation"] else: @[]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
174 else:
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
175 let requiredExtensions = REQUIRED_PLATFORM_EXTENSIONS & @["VK_KHR_surface"]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
176 let layers: seq[string]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
177
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
178 var
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
179 layersC = allocCStringArray(layers)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
180 instanceExtensionsC = allocCStringArray(requiredExtensions)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
181 defer:
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
182 deallocCStringArray(layersC)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
183 deallocCStringArray(instanceExtensionsC)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
184
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
185 var
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
186 appinfo = VkApplicationInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
187 sType: VK_STRUCTURE_TYPE_APPLICATION_INFO,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
188 pApplicationName: appName,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
189 pEngineName: "semicongine",
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
190 apiVersion: VK_MAKE_API_VERSION(0, 1, 3, 0),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
191 )
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
192 createinfo = VkInstanceCreateInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
193 sType: VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
194 pApplicationInfo: addr(appinfo),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
195 enabledLayerCount: layers.len.uint32,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
196 ppEnabledLayerNames: layersC,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
197 enabledExtensionCount: requiredExtensions.len.uint32,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
198 ppEnabledExtensionNames: instanceExtensionsC
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
199 )
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
200 checkVkResult vkCreateInstance(addr(createinfo), nil, addr(result.instance))
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
201 loadVulkan(result.instance)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
202
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
203 # load extensions
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
204 #
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
205 for extension in requiredExtensions:
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
206 loadExtension(result.instance, $extension)
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
207 result.window = CreateWindow(appName)
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
208 result.surface = CreateNativeSurface(result.instance, result.window)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
209
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
210 # logical device creation
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
211
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
212 # TODO: allowing support for physical devices without hasUniformBufferStandardLayout
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
213 # 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
214 # 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
215 # 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
216 # 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
217 var deviceExtensions = @["VK_KHR_swapchain"]
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
218 for extension in deviceExtensions:
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
219 loadExtension(result.instance, extension)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
220
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
221 # get physical device and graphics queue family
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
222 result.physicalDevice = GetBestPhysicalDevice(result.instance)
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
223 result.graphicsQueueFamily = GetQueueFamily(result.physicalDevice, VK_QUEUE_GRAPHICS_BIT)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
224
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
225 let
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
226 priority = cfloat(1)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
227 queueInfo = VkDeviceQueueCreateInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
228 sType: VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
229 queueFamilyIndex: result.graphicsQueueFamily,
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
230 queueCount: 1,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
231 pQueuePriorities: addr(priority),
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 deviceExtensionsC = allocCStringArray(deviceExtensions)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
234 defer: deallocCStringArray(deviceExtensionsC)
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
235 var createDeviceInfo = VkDeviceCreateInfo(
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
236 sType: VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
237 queueCreateInfoCount: 1,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
238 pQueueCreateInfos: addr(queueInfo),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
239 enabledLayerCount: 0,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
240 ppEnabledLayerNames: nil,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
241 enabledExtensionCount: uint32(deviceExtensions.len),
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
242 ppEnabledExtensionNames: deviceExtensionsC,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
243 pEnabledFeatures: nil,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
244 )
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
245 checkVkResult vkCreateDevice(
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
246 physicalDevice = result.physicalDevice,
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
247 pCreateInfo = addr createDeviceInfo,
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
248 pAllocator = nil,
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
249 pDevice = addr result.device
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
250 )
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
251 result.graphicsQueue = svkGetDeviceQueue(result.device, result.graphicsQueueFamily, VK_QUEUE_GRAPHICS_BIT)
1193
5aa1184fa5eb did: a few more things
sam <sam@basx.dev>
parents: 1192
diff changeset
252
1195
cfba2b7e00d0 did: most of swapchain, swap still needs to be done
sam <sam@basx.dev>
parents: 1194
diff changeset
253 vulkan = InitVulkan()