annotate static_utils.nim @ 1183:850450bfe2a2 compiletime-tests

sync from bedroom to office
author sam <sam@basx.dev>
date Thu, 04 Jul 2024 07:30:08 +0700
parents e9a212e9cdf7
children 3f43c7163029
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1 import std/os
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
2 import std/enumerate
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
3 import std/hashes
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
4 import std/macros
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
5 import std/strformat
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
6 import std/strutils
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
7 import std/sequtils
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
8 import std/typetraits as tt
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
9
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
10 import semicongine/core/utils
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
11 import semicongine/core/vector
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
12 import semicongine/core/matrix
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
13 import semicongine/core/vulkanapi
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
14
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
15 template VertexAttribute {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
16 template InstanceAttribute {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
17 template Pass {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
18 template PassFlat {.pragma.}
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
19 template ShaderOutput {.pragma.}
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
20 template VertexIndices {.pragma.}
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
21
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
22 const INFLIGHTFRAMES = 2'u32
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
23 const MEMORY_ALIGNMENT = 65536'u64 # Align buffers inside memory along this alignment
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
24 const BUFFER_ALIGNMENT = 64'u64 # align offsets inside buffers along this alignment
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
25
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
26 # some globals that will (likely?) never change during the life time of the engine
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
27 type
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
28 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]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
29
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
30 ShaderObject[TShader] = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
31 vertexShader: VkShaderModule
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
32 fragmentShader: VkShaderModule
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
33
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
34 VulkanGlobals = object
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
35 instance: VkInstance
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
36 device: VkDevice
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
37 physicalDevice: VkPhysicalDevice
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
38 queueFamilyIndex: uint32
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
39 queue: VkQueue
1181
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
40
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
41 IndexType = enum
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
42 None, UInt8, UInt16, UInt32
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
43
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
44 IndirectGPUMemory = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
45 vk: VkDeviceMemory
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
46 size: uint64
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
47 needsTransfer: bool # usually true
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
48 DirectGPUMemory = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
49 vk: VkDeviceMemory
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
50 size: uint64
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
51 data: pointer
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
52 needsFlush: bool # usually true
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
53 GPUMemory = IndirectGPUMemory | DirectGPUMemory
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
54
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
55 Buffer[TMemory: GPUMemory] = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
56 memory: TMemory
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
57 vk: VkBuffer
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
58 offset: uint64
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
59 size: uint64
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
60 Texture[Channels: static int, TMemory: GPUMemory] = object
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
61 memory: TMemory
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
62 vk: VkImage
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
63 imageview: VkImageView
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
64 sampler: VkSampler
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
65 offset: uint64
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
66 size: uint64
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
67 width: int
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
68 data: seq[array[Channels, uint8]]
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
69
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
70 GPUArray[T: SupportedGPUType, TMemory: GPUMemory] = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
71 data: seq[T]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
72 buffer: Buffer[TMemory]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
73 offset: uint64
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
74 GPUValue[T: object|array, TMemory: GPUMemory] = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
75 data: T
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
76 buffer: Buffer[TMemory]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
77 offset: uint64
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
78 GPUData = GPUArray | GPUValue
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
79
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
80 DescriptorSetType = enum
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
81 GlobalSet
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
82 MaterialSet
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
83 DescriptorSet[T: object, sType: static DescriptorSetType] = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
84 data: T
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
85 vk: array[INFLIGHTFRAMES.int, VkDescriptorSet]
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
86
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
87 Pipeline[TShader] = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
88 vk: VkPipeline
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
89 layout: VkPipelineLayout
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
90 descriptorSetLayouts: array[DescriptorSetType, VkDescriptorSetLayout]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
91 BufferType = enum
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
92 VertexBuffer, IndexBuffer, UniformBuffer
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
93 RenderData = object
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
94 descriptorPool: VkDescriptorPool
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
95 # tuple is memory and offset to next free allocation in that memory
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
96 indirectMemory: seq[tuple[memory: IndirectGPUMemory, usedOffset: uint64]]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
97 directMemory: seq[tuple[memory: DirectGPUMemory, usedOffset: uint64]]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
98 indirectBuffers: seq[tuple[buffer: Buffer[IndirectGPUMemory], btype: BufferType, usedOffset: uint64]]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
99 directBuffers: seq[tuple[buffer: Buffer[DirectGPUMemory], btype: BufferType, usedOffset: uint64]]
1181
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
100
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
101 var vulkan: VulkanGlobals
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
102
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
103 func alignedTo[T: SomeInteger](value: T, alignment: T): T =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
104 let remainder = value mod alignment
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
105 if remainder == 0:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
106 return value
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
107 else:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
108 return value + alignment - remainder
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
109
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
110 func VkType[T: SupportedGPUType](value: T): VkFormat =
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
111 when T is float32: VK_FORMAT_R32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
112 elif T is float64: VK_FORMAT_R64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
113 elif T is int8: VK_FORMAT_R8_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
114 elif T is int16: VK_FORMAT_R16_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
115 elif T is int32: VK_FORMAT_R32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
116 elif T is int64: VK_FORMAT_R64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
117 elif T is uint8: VK_FORMAT_R8_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
118 elif T is uint16: VK_FORMAT_R16_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
119 elif T is uint32: VK_FORMAT_R32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
120 elif T is uint64: VK_FORMAT_R64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
121 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
122 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
123 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
124 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
125 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
126 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
127 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
128 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
129 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
130 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
131 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
132 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
133 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
134 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
135 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
136 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
137 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
138 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
139 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
140 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
141 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
142 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
143 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
144 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
145 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
146 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
147 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
148 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
149 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
150 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
151 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
152 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
153 else: {.error: "Unsupported data type on GPU".}
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
154
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
155 func GlslType[T: SupportedGPUType|Texture](value: T): string =
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
156 when T is float32: "float"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
157 elif T is float64: "double"
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
158 elif T is int8 or T is int16 or T is int32 or T is int64: "int"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
159 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
160 elif T is TVec2[int32]: "ivec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
161 elif T is TVec2[int64]: "ivec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
162 elif T is TVec3[int32]: "ivec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
163 elif T is TVec3[int64]: "ivec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
164 elif T is TVec4[int32]: "ivec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
165 elif T is TVec4[int64]: "ivec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
166 elif T is TVec2[uint32]: "uvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
167 elif T is TVec2[uint64]: "uvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
168 elif T is TVec3[uint32]: "uvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
169 elif T is TVec3[uint64]: "uvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
170 elif T is TVec4[uint32]: "uvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
171 elif T is TVec4[uint64]: "uvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
172 elif T is TVec2[float32]: "vec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
173 elif T is TVec2[float64]: "dvec2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
174 elif T is TVec3[float32]: "vec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
175 elif T is TVec3[float64]: "dvec3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
176 elif T is TVec4[float32]: "vec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
177 elif T is TVec4[float64]: "dvec4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
178 elif T is TMat2[float32]: "mat2"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
179 elif T is TMat2[float64]: "dmat2"
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
180 elif T is TMat23[float32]: "mat23"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
181 elif T is TMat23[float64]: "dmat23"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
182 elif T is TMat32[float32]: "mat32"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
183 elif T is TMat32[float64]: "dmat32"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
184 elif T is TMat3[float32]: "mat3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
185 elif T is TMat3[float64]: "dmat3"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
186 elif T is TMat34[float32]: "mat34"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
187 elif T is TMat34[float64]: "dmat34"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
188 elif T is TMat43[float32]: "mat43"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
189 elif T is TMat43[float64]: "dmat43"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
190 elif T is TMat4[float32]: "mat4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
191 elif T is TMat4[float64]: "dmat4"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
192 elif T is Texture: "sampler2D"
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
193 else: {.error: "Unsupported data type on GPU".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
194
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
195 template ForVertexDataFields(shader: typed, fieldname, valuename, isinstancename, body: untyped): untyped =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
196 for theFieldname, value in fieldPairs(shader):
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
197 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
198 when not typeof(value) is seq:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
199 {.error: "field '" & theFieldname & "' needs to be a seq".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
200 when not typeof(value) is SupportedGPUType:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
201 {.error: "field '" & theFieldname & "' is not a supported GPU type".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
202 block:
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
203 const `fieldname` {.inject.} = theFieldname
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
204 let `valuename` {.inject.} = value
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
205 const `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
206 body
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
207
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
208 template ForDescriptorFields(shader: typed, fieldname, valuename, typename, countname, bindingNumber, body: untyped): untyped =
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
209 var `bindingNumber` {.inject.} = 1'u32
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
210 for theFieldname, value in fieldPairs(shader):
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
211 when typeof(value) is Texture:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
212 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
213 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
214 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
215 const `countname` {.inject.} = 1'u32
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
216 let `valuename` {.inject.} = value
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
217 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
218 `bindingNumber`.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
219 elif typeof(value) is object:
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
220 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
221 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
222 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
223 const `countname` {.inject.} = 1'u32
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
224 let `valuename` {.inject.} = value
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
225 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
226 `bindingNumber`.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
227 elif typeof(value) is array:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
228 when elementType(value) is Texture:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
229 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
230 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
231 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
232 const `countname` {.inject.} = uint32(typeof(value).len)
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
233 let `valuename` {.inject.} = value
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
234 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
235 `bindingNumber`.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
236 elif elementType(value) is object:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
237 block:
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
238 const `fieldname` {.inject.} = theFieldname
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
239 const `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
240 const `countname` {.inject.} = uint32(typeof(value).len)
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
241 let `valuename` {.inject.} = value
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
242 body
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
243 `bindingNumber`.inc
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
244 else:
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
245 {.error: "Unsupported descriptor type: " & tt.name(typeof(value)).}
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
246
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
247 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
248 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
249 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
250 elif T is TMat32[float32] or T is TMat32[float64] or T is TMat3[float32] or T is TMat3[float64] or T is TMat34[float32] or T is TMat34[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
251 3
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
252 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
253 4
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
254 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
255 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
256
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
257 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
258 #[
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
259 single location:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
260 - any scalar
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
261 - any 16-bit vector
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
262 - any 32-bit vector
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
263 - any 64-bit vector that has max. 2 components
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
264 16-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
265 32-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
266 64-bit scalar and 2-component vector types.
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
267 two locations
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
268 64-bit three- and four-component vectors
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
269 ]#
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
270 when T is TVec3[int64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
271 T is TVec4[int64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
272 T is TVec3[uint64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
273 T is TVec4[uint64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
274 T is TVec3[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
275 T is TVec4[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
276 T is TMat23[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
277 T is TMat3[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
278 T is TMat34[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
279 T is TMat43[float64] or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
280 T is TMat4[float64]:
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
281 return 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
282 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
283 return 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
284
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
285 template sType(descriptorSet: DescriptorSet): untyped =
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
286 get(genericParams(typeof(gpuData)), 1)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
287
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
288 template UsesIndirectMemory(gpuData: GPUData): untyped =
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
289 get(genericParams(typeof(gpuData)), 1) is IndirectGPUMemory
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
290 template UsesDirectMemory(gpuData: GPUData): untyped =
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
291 get(genericParams(typeof(gpuData)), 1) is DirectGPUMemory
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
292
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
293 template size(gpuArray: GPUArray): uint64 =
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
294 (gpuArray.data.len * sizeof(elementType(gpuArray.data))).uint64
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
295 template size(gpuValue: GPUValue): uint64 =
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
296 sizeof(gpuValue.data).uint64
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
297
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
298 template datapointer(gpuArray: GPUArray): pointer =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
299 addr(gpuArray.data[0])
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
300 template datapointer(gpuValue: GPUValue): pointer =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
301 addr(gpuValue.data)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
302
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
303 proc AllocationSize(buffer: Buffer): uint64 =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
304 var req: VkMemoryRequirements
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
305 vkGetBufferMemoryRequirements(vulkan.device, buffer.vk, addr(req))
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
306 return req.size
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
307
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
308 proc GetPhysicalDevice(instance: VkInstance): VkPhysicalDevice =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
309 var nDevices: uint32
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
310 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), nil)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
311 var devices = newSeq[VkPhysicalDevice](nDevices)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
312 checkVkResult vkEnumeratePhysicalDevices(instance, addr(nDevices), devices.ToCPointer)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
313
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
314 var score = 0'u32
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
315 for pDevice in devices:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
316 var props: VkPhysicalDeviceProperties
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
317 vkGetPhysicalDeviceProperties(pDevice, addr(props))
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
318 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU and props.limits.maxImageDimension2D > score:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
319 score = props.limits.maxImageDimension2D
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
320 result = pDevice
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
321
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
322 if score == 0:
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
323 for pDevice in devices:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
324 var props: VkPhysicalDeviceProperties
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
325 vkGetPhysicalDeviceProperties(pDevice, addr(props))
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
326 if props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU and props.limits.maxImageDimension2D > score:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
327 score = props.limits.maxImageDimension2D
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
328 result = pDevice
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
329
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
330 assert score > 0, "Unable to find integrated or discrete GPU"
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
331
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
332
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
333 proc GetDirectMemoryTypeIndex(): uint32 =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
334 var physicalProperties: VkPhysicalDeviceMemoryProperties
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
335 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr(physicalProperties))
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
336
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
337 var biggestHeap: uint64 = 0
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
338 result = high(uint32)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
339 # try to find host-visible type
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
340 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
341 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
342 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
343 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
344 if size > biggestHeap:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
345 biggestHeap = size
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
346 result = i
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
347 assert result != high(uint32), "There is not host visible memory. This is likely a driver bug."
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
348
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
349 proc GetQueueFamily(pDevice: VkPhysicalDevice, qType: VkQueueFlagBits): uint32 =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
350 var nQueuefamilies: uint32
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
351 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, nil)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
352 var queuFamilies = newSeq[VkQueueFamilyProperties](nQueuefamilies)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
353 vkGetPhysicalDeviceQueueFamilyProperties(pDevice, addr nQueuefamilies, queuFamilies.ToCPointer)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
354 for i in 0'u32 ..< nQueuefamilies:
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
355 if qType in toEnums(queuFamilies[i].queueFlags):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
356 return i
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
357 assert false, &"Queue of type {qType} not found"
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
358
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
359 proc GetQueue(device: VkDevice, queueFamilyIndex: uint32, qType: VkQueueFlagBits): VkQueue =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
360 vkGetDeviceQueue(
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
361 device,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
362 queueFamilyIndex,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
363 0,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
364 addr(result),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
365 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
366
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
367 proc GetSurfaceFormat(): VkFormat =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
368 # EVERY windows driver and almost every linux driver should support this
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
369 VK_FORMAT_B8G8R8A8_SRGB
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
370
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
371 template WithSingleUseCommandBuffer(device: VkDevice, cmd, body: untyped): untyped =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
372 block:
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
373 var
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
374 commandBufferPool: VkCommandPool
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
375 createInfo = VkCommandPoolCreateInfo(
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
376 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
377 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
378 queueFamilyIndex: vulkan.queueFamilyIndex,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
379 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
380 checkVkResult vkCreateCommandPool(device, addr createInfo, nil, addr(commandBufferPool))
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
381 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
382 `cmd` {.inject.}: VkCommandBuffer
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
383 allocInfo = VkCommandBufferAllocateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
384 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
385 commandPool: commandBufferPool,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
386 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
387 commandBufferCount: 1,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
388 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
389 checkVkResult device.vkAllocateCommandBuffers(addr allocInfo, addr(`cmd`))
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
390 var beginInfo = VkCommandBufferBeginInfo(
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
391 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
392 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
393 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
394 checkVkResult `cmd`.vkBeginCommandBuffer(addr beginInfo)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
395
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
396 body
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
397
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
398 checkVkResult `cmd`.vkEndCommandBuffer()
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
399 var submitInfo = VkSubmitInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
400 sType: VK_STRUCTURE_TYPE_SUBMIT_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
401 commandBufferCount: 1,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
402 pCommandBuffers: addr(`cmd`),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
403 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
404
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
405 var
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
406 fence: VkFence
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
407 fenceInfo = VkFenceCreateInfo(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
408 sType: VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
409 # flags: toBits [VK_FENCE_CREATE_SIGNALED_BIT]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
410 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
411 checkVkResult device.vkCreateFence(addr(fenceInfo), nil, addr(fence))
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
412 checkVkResult vkQueueSubmit(vulkan.queue, 1, addr(submitInfo), fence)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
413 checkVkResult vkWaitForFences(device, 1, addr fence, false, high(uint64))
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
414 vkDestroyCommandPool(device, commandBufferPool, nil)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
415
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
416
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
417 proc UpdateGPUBuffer(gpuData: GPUData) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
418 if gpuData.size == 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
419 return
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
420 when UsesDirectMemory(gpuData):
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
421 copyMem(cast[pointer](cast[uint64](gpuData.buffer.memory.data) + gpuData.buffer.offset + gpuData.offset), gpuData.datapointer, gpuData.size)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
422 else:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
423 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
424 stagingBuffer: VkBuffer
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
425 createInfo = VkBufferCreateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
426 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
427 flags: VkBufferCreateFlags(0),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
428 size: gpuData.size,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
429 usage: toBits([VK_BUFFER_USAGE_TRANSFER_SRC_BIT]),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
430 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
431 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
432 checkVkResult vkCreateBuffer(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
433 device = vulkan.device,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
434 pCreateInfo = addr(createInfo),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
435 pAllocator = nil,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
436 pBuffer = addr(stagingBuffer),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
437 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
438 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
439 stagingMemory: VkDeviceMemory
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
440 stagingPtr: pointer
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
441 memoryAllocationInfo = VkMemoryAllocateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
442 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
443 allocationSize: gpuData.buffer.AllocationSize(),
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
444 memoryTypeIndex: GetDirectMemoryTypeIndex(),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
445 )
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
446 checkVkResult vkAllocateMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
447 vulkan.device,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
448 addr(memoryAllocationInfo),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
449 nil,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
450 addr(stagingMemory),
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
451 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
452 checkVkResult vkBindBufferMemory(vulkan.device, stagingBuffer, stagingMemory, 0)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
453 checkVkResult vkMapMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
454 device = vulkan.device,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
455 memory = stagingMemory,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
456 offset = 0'u64,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
457 size = VK_WHOLE_SIZE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
458 flags = VkMemoryMapFlags(0),
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
459 ppData = addr(stagingPtr)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
460 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
461 copyMem(stagingPtr, gpuData.datapointer, gpuData.size)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
462 var stagingRange = VkMappedMemoryRange(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
463 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
464 memory: stagingMemory,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
465 size: VK_WHOLE_SIZE,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
466 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
467 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, 1, addr(stagingRange))
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
468
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
469 WithSingleUseCommandBuffer(vulkan.device, commandBuffer):
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
470 var copyRegion = VkBufferCopy(size: gpuData.size)
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
471 vkCmdCopyBuffer(commandBuffer, stagingBuffer, gpuData.buffer.vk, 1, addr(copyRegion))
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
472
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
473 vkDestroyBuffer(vulkan.device, stagingBuffer, nil)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
474 vkFreeMemory(vulkan.device, stagingMemory, nil)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
475
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
476 proc UpdateAllGPUBuffers[T](value: T) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
477 for name, fieldvalue in value.fieldPairs():
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
478 when typeof(fieldvalue) is GPUData:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
479 UpdateGPUBuffer(fieldvalue)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
480
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
481 proc InitDescriptorSet(
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
482 renderData: RenderData,
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
483 layout: VkDescriptorSetLayout,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
484 descriptorSet: var DescriptorSet,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
485 ) =
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
486 # santization checks
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
487 for name, value in descriptorSet.data.fieldPairs:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
488 when typeof(value) is GPUValue:
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
489 assert value.buffer.vk.Valid
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
490 # TODO:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
491 # when typeof(value) is Texture:
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
492 # assert value.texture.vk.Valid
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
493
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
494 # allocate
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
495 var layouts = newSeqWith(descriptorSet.vk.len, layout)
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
496 var allocInfo = VkDescriptorSetAllocateInfo(
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
497 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
498 descriptorPool: renderData.descriptorPool,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
499 descriptorSetCount: uint32(layouts.len),
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
500 pSetLayouts: layouts.ToCPointer,
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
501 )
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
502 checkVkResult vkAllocateDescriptorSets(vulkan.device, addr(allocInfo), descriptorSet.vk.ToCPointer)
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
503
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
504 # write
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
505 var descriptorSetWrites = newSeq[VkWriteDescriptorSet](descriptorSet.vk.len)
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
506
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
507 var descriptorBinding = 0
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
508 ForDescriptorFields(descriptorSet.data, fieldName, fieldValue, descriptorType, descriptorCount, descriptorBindingNumber):
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
509 for i in 0 ..< descriptorSet.vk.len:
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
510 when typeof(fieldValue) is GPUValue:
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
511 let bufferInfo = VkDescriptorBufferInfo(
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
512 buffer: fieldValue.buffer.vk,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
513 offset: fieldValue.buffer.offset,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
514 range: fieldValue.buffer.size,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
515 )
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
516 descriptorSetWrites[i] = VkWriteDescriptorSet(
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
517 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
518 dstSet: descriptorSet.vk[i],
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
519 dstBinding: descriptorBindingNumber,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
520 dstArrayElement: uint32(0),
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
521 descriptorType: descriptorType,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
522 descriptorCount: descriptorCount,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
523 pImageInfo: nil,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
524 pBufferInfo: addr(bufferInfo),
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
525 )
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
526 elif typeof(fieldValue) is Texture:
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
527 let imageInfo = VkDescriptorImageInfo(
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
528 sampler: fieldValue.sampler,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
529 imageView: fieldValue.imageView,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
530 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
531 )
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
532 descriptorSetWrites[i] = VkWriteDescriptorSet(
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
533 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
534 dstSet: descriptorSet.vk[i],
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
535 dstBinding: descriptorBindingNumber,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
536 dstArrayElement: uint32(0),
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
537 descriptorType: descriptorType,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
538 descriptorCount: descriptorCount,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
539 pImageInfo: addr(imageInfo),
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
540 pBufferInfo: nil,
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
541 )
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
542 else:
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
543 {.error: "Unsupported descriptor type: " & tt.name(typeof(fieldValue)).}
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
544
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
545
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
546 vkUpdateDescriptorSets(vulkan.device, descriptorSetWrites.len.uint32, descriptorSetWrites.ToCPointer, 0, nil)
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
547
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
548 #[
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
549 proc WriteDescriptors[TShader, TUniforms, TGlobals](renderData: RenderData, uniforms: TUniforms, globals: TGlobals) =
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
550 var descriptorSetWrites: seq[VkWriteDescriptorSet]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
551 ForDescriptorFields(default(TShader), fieldName, descriptorType, descriptorCount, descriptorBindingNumber):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
552 for frameInFlight in 0 ..< renderData.descriptorSets.len:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
553 when descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
554 when HasGPUValueField[TUniforms](fieldName):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
555 WithGPUValueField(uniforms, fieldName, gpuValue):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
556 let bufferInfo = VkDescriptorBufferInfo(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
557 buffer: gpuValue.buffer.vk,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
558 offset: gpuValue.buffer.offset,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
559 range: gpuValue.buffer.size,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
560 )
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
561 descriptorSetWrites.add VkWriteDescriptorSet(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
562 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
563 dstSet: renderData.descriptorSets[frameInFlight],
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
564 dstBinding: descriptorBindingNumber,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
565 dstArrayElement: uint32(0),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
566 descriptorType: descriptorType,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
567 descriptorCount: descriptorCount,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
568 pImageInfo: nil,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
569 pBufferInfo: addr(bufferInfo),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
570 )
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
571 elif HasGPUValueField[TGlobals](fieldName):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
572 WithGPUValueField(globals, fieldName, theValue):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
573 let bufferInfo = VkDescriptorBufferInfo(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
574 buffer: theValue.buffer.vk,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
575 offset: theValue.buffer.offset,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
576 range: theValue.buffer.size,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
577 )
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
578 descriptorSetWrites.add VkWriteDescriptorSet(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
579 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
580 dstSet: renderData.descriptorSets[frameInFlight],
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
581 dstBinding: descriptorBindingNumber,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
582 dstArrayElement: uint32(0),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
583 descriptorType: descriptorType,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
584 descriptorCount: descriptorCount,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
585 pImageInfo: nil,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
586 pBufferInfo: addr(bufferInfo),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
587 )
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
588 else:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
589 {.error: "Unable to find field '" & fieldName & "' in uniforms or globals".}
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
590 elif descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
591 # TODO
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
592 let imageInfo = VkDescriptorImageInfo(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
593 sampler: VkSampler(0),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
594 imageView: VkImageView(0),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
595 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
596 )
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
597 descriptorSetWrites.add VkWriteDescriptorSet(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
598 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
599 dstSet: renderData.descriptorSets[frameInFlight],
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
600 dstBinding: descriptorBindingNumber,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
601 dstArrayElement: 0'u32,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
602 descriptorType: descriptorType,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
603 descriptorCount: descriptorCount,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
604 pImageInfo: addr(imageInfo),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
605 pBufferInfo: nil,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
606 )
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
607 else:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
608 assert false, "Unsupported descriptor type"
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
609 vkUpdateDescriptorSets(vulkan.device, uint32(descriptorSetWrites.len), descriptorSetWrites.ToCPointer, 0, nil)
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
610 ]#
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
611
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
612
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
613 converter toVkIndexType(indexType: IndexType): VkIndexType =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
614 case indexType:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
615 of None: VK_INDEX_TYPE_NONE_KHR
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
616 of UInt8: VK_INDEX_TYPE_UINT8_EXT
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
617 of UInt16: VK_INDEX_TYPE_UINT16
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
618 of UInt32: VK_INDEX_TYPE_UINT32
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
619
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
620 proc CreateRenderPass(format: VkFormat): VkRenderPass =
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
621 var
sam <sam@basx.dev>
parents: 1165
diff changeset
622 attachments = @[VkAttachmentDescription(
sam <sam@basx.dev>
parents: 1165
diff changeset
623 format: format,
sam <sam@basx.dev>
parents: 1165
diff changeset
624 samples: VK_SAMPLE_COUNT_1_BIT,
sam <sam@basx.dev>
parents: 1165
diff changeset
625 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
sam <sam@basx.dev>
parents: 1165
diff changeset
626 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
sam <sam@basx.dev>
parents: 1165
diff changeset
627 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
sam <sam@basx.dev>
parents: 1165
diff changeset
628 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
sam <sam@basx.dev>
parents: 1165
diff changeset
629 initialLayout: VK_IMAGE_LAYOUT_UNDEFINED,
sam <sam@basx.dev>
parents: 1165
diff changeset
630 finalLayout: VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
sam <sam@basx.dev>
parents: 1165
diff changeset
631 )]
sam <sam@basx.dev>
parents: 1165
diff changeset
632 dependencies = @[VkSubpassDependency(
sam <sam@basx.dev>
parents: 1165
diff changeset
633 srcSubpass: VK_SUBPASS_EXTERNAL,
sam <sam@basx.dev>
parents: 1165
diff changeset
634 dstSubpass: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
635 srcStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
636 srcAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
637 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
638 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT],
sam <sam@basx.dev>
parents: 1165
diff changeset
639 )]
sam <sam@basx.dev>
parents: 1165
diff changeset
640 outputs = @[
sam <sam@basx.dev>
parents: 1165
diff changeset
641 VkAttachmentReference(
sam <sam@basx.dev>
parents: 1165
diff changeset
642 attachment: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
643 layout: VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
sam <sam@basx.dev>
parents: 1165
diff changeset
644 )
sam <sam@basx.dev>
parents: 1165
diff changeset
645 ]
sam <sam@basx.dev>
parents: 1165
diff changeset
646
sam <sam@basx.dev>
parents: 1165
diff changeset
647 var subpassesList = [
sam <sam@basx.dev>
parents: 1165
diff changeset
648 VkSubpassDescription(
sam <sam@basx.dev>
parents: 1165
diff changeset
649 flags: VkSubpassDescriptionFlags(0),
sam <sam@basx.dev>
parents: 1165
diff changeset
650 pipelineBindPoint: VK_PIPELINE_BIND_POINT_GRAPHICS,
sam <sam@basx.dev>
parents: 1165
diff changeset
651 inputAttachmentCount: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
652 pInputAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
653 colorAttachmentCount: uint32(outputs.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
654 pColorAttachments: outputs.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
655 pResolveAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
656 pDepthStencilAttachment: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
657 preserveAttachmentCount: 0,
sam <sam@basx.dev>
parents: 1165
diff changeset
658 pPreserveAttachments: nil,
sam <sam@basx.dev>
parents: 1165
diff changeset
659 )
sam <sam@basx.dev>
parents: 1165
diff changeset
660 ]
sam <sam@basx.dev>
parents: 1165
diff changeset
661
sam <sam@basx.dev>
parents: 1165
diff changeset
662 var createInfo = VkRenderPassCreateInfo(
sam <sam@basx.dev>
parents: 1165
diff changeset
663 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
sam <sam@basx.dev>
parents: 1165
diff changeset
664 attachmentCount: uint32(attachments.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
665 pAttachments: attachments.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
666 subpassCount: uint32(subpassesList.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
667 pSubpasses: subpassesList.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
668 dependencyCount: uint32(dependencies.len),
sam <sam@basx.dev>
parents: 1165
diff changeset
669 pDependencies: dependencies.ToCPointer,
sam <sam@basx.dev>
parents: 1165
diff changeset
670 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
671 checkVkResult vulkan.device.vkCreateRenderPass(addr(createInfo), nil, addr(result))
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
672
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
673 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
674 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
675 case stage
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
676 of VK_SHADER_STAGE_VERTEX_BIT: "vert"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
677 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
678 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
679 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
680 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
681 of VK_SHADER_STAGE_COMPUTE_BIT: "comp"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
682 else: ""
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
683
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
684 when defined(nimcheck): # will not run if nimcheck is running
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
685 return result
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
686
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
687 let
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
688 stagename = stage2string(stage)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
689 shaderHash = hash(shaderSource)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
690 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
691
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
692 if not shaderfile.fileExists:
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
693 echo "shader of type ", stage
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
694 for i, line in enumerate(shaderSource.splitlines()):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
695 echo " ", i + 1, " ", line
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
696 # var glslExe = currentSourcePath.parentDir.parentDir.parentDir / "tools" / "glslangValidator"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
697 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
698 when defined(windows):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
699 glslExe = glslExe & "." & ExeExt
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
700 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
701 echo "run: ", command
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
702 discard StaticExecChecked(
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
703 command = command,
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
704 input = shaderSource
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
705 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
706 else:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
707 echo &"shaderfile {shaderfile} is up-to-date"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
708
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
709 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
710 let shaderbinary = staticRead shaderfile.replace("\\", "/")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
711 else:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
712 let shaderbinary = staticRead shaderfile
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
713
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
714 var i = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
715 while i < shaderbinary.len:
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
716 result.add(
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
717 (uint32(shaderbinary[i + 0]) shl 0) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
718 (uint32(shaderbinary[i + 1]) shl 8) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
719 (uint32(shaderbinary[i + 2]) shl 16) or
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
720 (uint32(shaderbinary[i + 3]) shl 24)
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
721 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
722 i += 4
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
723
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
724 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} =
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
725 const GLSL_VERSION = "450"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
726 var vsInput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
727 var vsOutput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
728 var fsInput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
729 var fsOutput: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
730 var uniforms: seq[string]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
731 var samplers: seq[string]
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
732 var vsInputLocation = 0'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
733 var passLocation = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
734 var fsOutputLocation = 0
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
735
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
736 var descriptorSetCount = 0
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
737 for fieldname, value in fieldPairs(shader):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
738 # vertex shader inputs
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
739 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute):
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
740 assert typeof(value) is SupportedGPUType
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
741 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
742 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value):
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
743 vsInputLocation += NLocationSlots(value)
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
744
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
745 # intermediate values, passed between shaders
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
746 elif hasCustomPragma(value, Pass) or hasCustomPragma(value, PassFlat):
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
747 let flat = if hasCustomPragma(value, PassFlat): "flat " else: ""
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
748 vsOutput.add "layout(location = " & $passLocation & ") " & flat & "out " & GlslType(value) & " " & fieldname & ";"
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
749 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
750 passLocation.inc
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
751
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
752 # fragment shader output
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
753 elif hasCustomPragma(value, ShaderOutput):
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
754 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";"
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
755 fsOutputLocation.inc
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
756
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
757 # descriptor sets
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
758 # need to consider 4 cases: uniform block, texture, uniform block array, texture array
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
759 elif typeof(value) is DescriptorSet:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
760 assert descriptorSetCount <= DescriptorSetType.high.int, &"{tt.name(TShader)}: maximum {DescriptorSetType.high} allowed"
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
761
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
762 var descriptorBinding = 0
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
763 for descriptorName, descriptorValue in fieldPairs(value.data):
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
764
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
765 when typeof(descriptorValue) is Texture:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
766 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(descriptorValue) & " " & descriptorName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
767 descriptorBinding.inc
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
768
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
769 elif typeof(descriptorValue) is GPUValue:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
770 uniforms.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform T" & descriptorName & " {"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
771 when typeof(descriptorValue.data) is object:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
772 for blockFieldName, blockFieldValue in descriptorValue.data.fieldPairs():
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
773 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
774 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
775 uniforms.add "} " & descriptorName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
776 elif typeof(descriptorValue.data) is array:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
777 for blockFieldName, blockFieldValue in default(elementType(descriptorValue.data)).fieldPairs():
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
778 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
779 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
780 uniforms.add "} " & descriptorName & "[" & $descriptorValue.data.len & "];"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
781 descriptorBinding.inc
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
782 elif typeof(descriptorValue) is array:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
783 when elementType(descriptorValue) is Texture:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
784 let arrayDecl = "[" & $typeof(descriptorValue).len & "]"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
785 samplers.add "layout(set=" & $descriptorSetCount & ", binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(descriptorValue))) & " " & descriptorName & "" & arrayDecl & ";"
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
786 descriptorBinding.inc
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
787 else:
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
788 {.error: "Unsupported shader descriptor field " & descriptorName.}
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
789 descriptorSetCount.inc
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
790 elif fieldname in ["vertexCode", "fragmentCode"]:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
791 discard
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
792 else:
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
793 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).}
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
794
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
795 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
796 vsInput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
797 uniforms &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
798 samplers &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
799 vsOutput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
800 @[shader.vertexCode]).join("\n")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
801
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
802 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
803 fsInput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
804 uniforms &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
805 samplers &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
806 fsOutput &
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
807 @[shader.fragmentCode]).join("\n")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
808
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
809 proc CompileShader[TShader](shader: static TShader): ShaderObject[TShader] =
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
810 const (vertexShaderSource, fragmentShaderSource) = generateShaderSource(shader)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
811
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
812 let vertexBinary = compileGlslToSPIRV(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderSource)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
813 let fragmentBinary = compileGlslToSPIRV(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderSource)
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
814
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
815 var createInfoVertex = VkShaderModuleCreateInfo(
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
816 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
817 codeSize: csize_t(vertexBinary.len * sizeof(uint32)),
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
818 pCode: vertexBinary.ToCPointer,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
819 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
820 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader))
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
821 var createInfoFragment = VkShaderModuleCreateInfo(
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
822 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
823 codeSize: csize_t(fragmentBinary.len * sizeof(uint32)),
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
824 pCode: fragmentBinary.ToCPointer,
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
825 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
826 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader))
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
827
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
828
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
829 proc CreatePipeline[TShader](
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
830 renderPass: VkRenderPass,
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
831 shader: ShaderObject[TShader],
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
832 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
833 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
834 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
835 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
836 descriptorPoolLimit = 1024
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
837 ): Pipeline[TShader] =
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
838 # create pipeline
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
839
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
840 for theFieldname, value in fieldPairs(default(TShader)):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
841 when typeof(value) is DescriptorSet:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
842 var layoutbindings: seq[VkDescriptorSetLayoutBinding]
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
843 ForDescriptorFields(value.data, fieldName, fieldValue, descriptorType, descriptorCount, descriptorBindingNumber):
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
844 layoutbindings.add VkDescriptorSetLayoutBinding(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
845 binding: descriptorBindingNumber,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
846 descriptorType: descriptorType,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
847 descriptorCount: descriptorCount,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
848 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
849 pImmutableSamplers: nil,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
850 )
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
851 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
852 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
853 bindingCount: layoutbindings.len.uint32,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
854 pBindings: layoutbindings.ToCPointer
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
855 )
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
856 checkVkResult vkCreateDescriptorSetLayout(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
857 vulkan.device,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
858 addr(layoutCreateInfo),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
859 nil,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
860 addr(result.descriptorSetLayouts[value.sType])
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
861 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
862 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
863 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
864 setLayoutCount: result.descriptorSetLayouts.len.uint32,
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
865 pSetLayouts: result.descriptorSetLayouts.ToCPointer,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
866 # pushConstantRangeCount: uint32(pushConstants.len),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
867 # pPushConstantRanges: pushConstants.ToCPointer,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
868 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
869 checkVkResult vkCreatePipelineLayout(vulkan.device, addr(pipelineLayoutInfo), nil, addr(result.layout))
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
870
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
871 let stages = [
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
872 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
873 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
874 stage: VK_SHADER_STAGE_VERTEX_BIT,
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
875 module: shader.vertexShader,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
876 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
877 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
878 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
879 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
880 stage: VK_SHADER_STAGE_FRAGMENT_BIT,
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
881 module: shader.fragmentShader,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
882 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
883 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
884 ]
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
885 var
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
886 bindings: seq[VkVertexInputBindingDescription]
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
887 attributes: seq[VkVertexInputAttributeDescription]
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
888 var inputBindingNumber = 0'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
889 var location = 0'u32
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
890 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
891 bindings.add VkVertexInputBindingDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
892 binding: inputBindingNumber,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
893 stride: sizeof(value).uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
894 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
895 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
896 # allows to submit larger data structures like Mat44, for most other types will be 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
897 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
898 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value):
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
899 attributes.add VkVertexInputAttributeDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
900 binding: inputBindingNumber,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
901 location: location,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
902 format: VkType(value),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
903 offset: i * perDescriptorSize,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
904 )
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
905 location += NLocationSlots(value)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
906 inc inputBindingNumber
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
907
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
908 let
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
909 vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
910 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
911 vertexBindingDescriptionCount: uint32(bindings.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
912 pVertexBindingDescriptions: bindings.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
913 vertexAttributeDescriptionCount: uint32(attributes.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
914 pVertexAttributeDescriptions: attributes.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
915 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
916 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
917 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
918 topology: topology,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
919 primitiveRestartEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
920 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
921 viewportState = VkPipelineViewportStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
922 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
923 viewportCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
924 scissorCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
925 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
926 rasterizer = VkPipelineRasterizationStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
927 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
928 depthClampEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
929 rasterizerDiscardEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
930 polygonMode: polygonMode,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
931 lineWidth: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
932 cullMode: toBits [cullMode],
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
933 frontFace: frontFace,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
934 depthBiasEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
935 depthBiasConstantFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
936 depthBiasClamp: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
937 depthBiasSlopeFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
938 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
939 multisampling = VkPipelineMultisampleStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
940 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
941 sampleShadingEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
942 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
943 minSampleShading: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
944 pSampleMask: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
945 alphaToCoverageEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
946 alphaToOneEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
947 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
948 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
949 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT],
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
950 blendEnable: VK_TRUE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
951 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
952 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
953 colorBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
954 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
955 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
956 alphaBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
957 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
958 colorBlending = VkPipelineColorBlendStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
959 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
960 logicOpEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
961 attachmentCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
962 pAttachments: addr(colorBlendAttachment),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
963 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
964 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
965 dynamicState = VkPipelineDynamicStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
966 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
967 dynamicStateCount: dynamicStates.len.uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
968 pDynamicStates: dynamicStates.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
969 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
970 let createInfo = VkGraphicsPipelineCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
971 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
972 stageCount: 2,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
973 pStages: stages.ToCPointer,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
974 pVertexInputState: addr(vertexInputInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
975 pInputAssemblyState: addr(inputAssembly),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
976 pViewportState: addr(viewportState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
977 pRasterizationState: addr(rasterizer),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
978 pMultisampleState: addr(multisampling),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
979 pDepthStencilState: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
980 pColorBlendState: addr(colorBlending),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
981 pDynamicState: addr(dynamicState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
982 layout: result.layout,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
983 renderPass: renderPass,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
984 subpass: 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
985 basePipelineHandle: VkPipeline(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
986 basePipelineIndex: -1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
987 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
988 checkVkResult vkCreateGraphicsPipelines(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
989 vulkan.device,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
990 VkPipelineCache(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
991 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
992 addr(createInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
993 nil,
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
994 addr(result.vk)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
995 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
996
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
997 proc AllocateIndirectMemory(size: uint64): IndirectGPUMemory =
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
998 # chooses biggest memory type that has NOT VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
999 result.size = size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1000 result.needsTransfer = true
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1001
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1002 # find a good memory type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1003 var physicalProperties: VkPhysicalDeviceMemoryProperties
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1004 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1005
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1006 var biggestHeap: uint64 = 0
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1007 var memoryTypeIndex = high(uint32)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1008 # try to find non-host-visible type
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1009 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1010 if not (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in toEnums(physicalProperties.memoryTypes[i].propertyFlags)):
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1011 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1012 if size > biggestHeap:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1013 biggestHeap = size
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1014 memoryTypeIndex = i
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1015
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1016 # If we did not found a device-only memory type, let's just take the biggest overall
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1017 if memoryTypeIndex == high(uint32):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1018 result.needsTransfer = false
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1019 for i in 0'u32 ..< physicalProperties.memoryTypeCount:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1020 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1021 if size > biggestHeap:
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1022 biggestHeap = size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1023 memoryTypeIndex = i
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1024
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1025 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1026 var allocationInfo = VkMemoryAllocateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1027 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1028 allocationSize: result.size,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1029 memoryTypeIndex: memoryTypeIndex,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1030 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1031 checkVkResult vkAllocateMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1032 vulkan.device,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1033 addr allocationInfo,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1034 nil,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1035 addr result.vk
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1036 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1037
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1038 proc AllocateDirectMemory(size: uint64): DirectGPUMemory =
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1039 result.size = size
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1040 result.needsFlush = true
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1041
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1042 # find a good memory type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1043 var physicalProperties: VkPhysicalDeviceMemoryProperties
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1044 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr physicalProperties)
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1045
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1046 var biggestHeap: uint64 = 0
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1047 var memoryTypeIndex = high(uint32)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1048 # try to find host-visible type
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1049 for i in 0 ..< physicalProperties.memoryTypeCount:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1050 let flags = toEnums(physicalProperties.memoryTypes[i].propertyFlags)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1051 if VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1052 let size = physicalProperties.memoryHeaps[physicalProperties.memoryTypes[i].heapIndex].size
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1053 if size > biggestHeap:
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1054 biggestHeap = size
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1055 memoryTypeIndex = i
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1056 result.needsFlush = not (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT in flags)
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1057
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1058 assert memoryTypeIndex != high(uint32), "Unable to find indirect memory type"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1059 var allocationInfo = VkMemoryAllocateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1060 sType: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1061 allocationSize: result.size,
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1062 memoryTypeIndex: GetDirectMemoryTypeIndex(),
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1063 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1064 checkVkResult vkAllocateMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1065 vulkan.device,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1066 addr allocationInfo,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1067 nil,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1068 addr result.vk
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1069 )
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1070 checkVkResult vkMapMemory(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1071 device = vulkan.device,
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1072 memory = result.vk,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1073 offset = 0'u64,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1074 size = result.size,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1075 flags = VkMemoryMapFlags(0),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1076 ppData = addr(result.data)
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1077 )
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1078
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1079 proc AllocateIndirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1080 if size == 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1081 return
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1082 var buffer = Buffer[IndirectGPUMemory](size: size)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1083
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1084 let usageFlags = case btype:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1085 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1086 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1087 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1088
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1089 # iterate through memory areas to find big enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1090 # TODO: dynamically expand memory allocations
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1091 for (memory, usedOffset) in renderData.indirectMemory.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1092 if memory.size - usedOffset >= size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1093 buffer.offset = usedOffset
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1094 # create buffer
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1095 var createInfo = VkBufferCreateInfo(
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1096 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1097 flags: VkBufferCreateFlags(0),
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1098 size: buffer.size,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1099 usage: toBits(usageFlags),
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1100 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1101 )
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1102 checkVkResult vkCreateBuffer(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1103 device = vulkan.device,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1104 pCreateInfo = addr createInfo,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1105 pAllocator = nil,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1106 pBuffer = addr(buffer.vk)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1107 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1108 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1109 renderData.indirectBuffers.add (buffer, btype, 0'u64)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1110 # update memory area offset
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1111 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1112 return
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1113
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1114 assert false, "Did not find allocated memory region with enough space"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1115
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1116 proc AllocateDirectBuffer(renderData: var RenderData, size: uint64, btype: BufferType) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1117 if size == 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1118 return
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1119
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1120 var buffer = Buffer[DirectGPUMemory](size: size)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1121
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1122 let usageFlags = case btype:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1123 of VertexBuffer: [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1124 of IndexBuffer: [VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1125 of UniformBuffer: [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1126
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1127 # iterate through memory areas to find big enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1128 # TODO: dynamically expand memory allocations
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1129 for (memory, usedOffset) in renderData.directMemory.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1130 if memory.size - usedOffset >= size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1131 buffer.offset = usedOffset
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1132 # create buffer
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1133 var createInfo = VkBufferCreateInfo(
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1134 sType: VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1135 flags: VkBufferCreateFlags(0),
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1136 size: buffer.size,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1137 usage: toBits(usageFlags),
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1138 sharingMode: VK_SHARING_MODE_EXCLUSIVE,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1139 )
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1140 checkVkResult vkCreateBuffer(
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1141 device = vulkan.device,
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1142 pCreateInfo = addr createInfo,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1143 pAllocator = nil,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1144 pBuffer = addr(buffer.vk)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1145 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1146 checkVkResult vkBindBufferMemory(vulkan.device, buffer.vk, memory.vk, buffer.offset)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1147 renderData.directBuffers.add (buffer, btype, 0'u64)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1148 # update memory area offset
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1149 usedOffset = alignedTo(usedOffset + size, MEMORY_ALIGNMENT)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1150 return
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1151
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1152 assert false, "Did not find allocated memory region with enough space"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1153
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1154 proc InitRenderData(descriptorPoolLimit = 1024'u32): RenderData =
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1155 # allocate descriptor pools
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1156 var poolSizes = [
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1157 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit),
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1158 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit),
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1159 ]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1160 var poolInfo = VkDescriptorPoolCreateInfo(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1161 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1162 poolSizeCount: poolSizes.len.uint32,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1163 pPoolSizes: poolSizes.ToCPointer,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1164 maxSets: descriptorPoolLimit,
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1165 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1166 checkVkResult vkCreateDescriptorPool(vulkan.device, addr(poolInfo), nil, addr(result.descriptorPool))
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1167
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1168 # allocate some memory
1181
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
1169 var initialAllocationSize = 1_000_000_000'u64 # TODO: make this more dynamic or something?
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1170 result.indirectMemory = @[(memory: AllocateIndirectMemory(size = initialAllocationSize), usedOffset: 0'u64)]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1171 result.directMemory = @[(memory: AllocateDirectMemory(size = initialAllocationSize), usedOffset: 0'u64)]
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1172
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1173 proc FlushDirectMemory(renderData: RenderData) =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1174 var flushRegions = newSeqOfCap[VkMappedMemoryRange](renderData.directMemory.len)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1175 for entry in renderData.directMemory:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1176 if entry.usedOffset > 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1177 flushRegions.add VkMappedMemoryRange(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1178 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1179 memory: entry.memory.vk,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1180 size: entry.usedOffset,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1181 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1182 if flushRegions.len > 0:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1183 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, flushRegions.len.uint32, flushRegions.ToCPointer())
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1184
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1185 # For the Get*BufferSize:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1186 # BUFFER_ALIGNMENT is just added for a rough estimate, to ensure we have enough space to align when binding
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1187 proc GetIndirectBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1188 for name, value in fieldPairs(data):
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1189 when not hasCustomPragma(value, VertexIndices):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1190 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1191 when UsesIndirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1192 result += value.size + BUFFER_ALIGNMENT
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1193 proc GetIndirectBufferSizes(data: DescriptorSet): uint64 =
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1194 GetIndirectBufferSizes(data.data)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1195 proc GetDirectBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1196 for name, value in fieldPairs(data):
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1197 when not hasCustomPragma(value, VertexIndices):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1198 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1199 when UsesDirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1200 result += value.size + BUFFER_ALIGNMENT
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1201 proc GetDirectBufferSizes(data: DescriptorSet): uint64 =
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1202 GetDirectBufferSizes(data.data)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1203 proc GetIndirectIndexBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1204 for name, value in fieldPairs(data):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1205 when hasCustomPragma(value, VertexIndices):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1206 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1207 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1208 when UsesIndirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1209 result += value.size + BUFFER_ALIGNMENT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1210 proc GetDirectIndexBufferSizes[T](data: T): uint64 =
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1211 for name, value in fieldPairs(data):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1212 when hasCustomPragma(value, VertexIndices):
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1213 static: assert typeof(value) is GPUArray, "Index buffers must be of type GPUArray"
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1214 static: assert elementType(value.data) is uint8 or elementType(value.data) is uint16 or elementType(value.data) is uint32
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1215 when UsesDirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1216 result += value.size + BUFFER_ALIGNMENT
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1217
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1218 proc AssignIndirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1219 for name, value in fieldPairs(data):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1220 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1221 when UsesIndirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1222 # find next buffer of correct type with enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1223 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1224 var foundBuffer = false
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1225 for (buffer, bt, offset) in renderData.indirectBuffers.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1226 if bt == btype and buffer.size - offset >= value.size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1227 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1228 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1229 value.buffer = buffer
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1230 value.offset = offset
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1231 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1232 foundBuffer = true
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1233 break
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1234 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'"
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1235 proc AssignIndirectBuffers(renderdata: var RenderData, btype: BufferType, data: var DescriptorSet) =
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1236 AssignIndirectBuffers(renderdata, btype, data.data)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1237 proc AssignDirectBuffers[T](renderdata: var RenderData, btype: BufferType, data: var T) =
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1238 for name, value in fieldPairs(data):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1239 when typeof(value) is GPUData:
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1240 when UsesDirectMemory(value):
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1241 # find next buffer of correct type with enough free space
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1242 if btype == IndexBuffer == value.hasCustomPragma(VertexIndices):
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1243 var foundBuffer = false
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1244 for (buffer, bt, offset) in renderData.directBuffers.mitems:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1245 if bt == btype and buffer.size - offset >= value.size:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1246 assert not value.buffer.vk.Valid, "GPUData-Buffer has already been assigned"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1247 assert buffer.vk.Valid, "RenderData-Buffer has not yet been created"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1248 value.buffer = buffer
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1249 value.offset = offset
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1250 offset = alignedTo(offset + value.size, BUFFER_ALIGNMENT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1251 foundBuffer = true
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1252 break
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1253 assert foundBuffer, &"Unable to find large enough '{btype}' for '{data}'"
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1254 proc AssignDirectBuffers(renderdata: var RenderData, btype: BufferType, data: var DescriptorSet) =
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1255 AssignDirectBuffers(renderdata, btype, data.data)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1256
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1257 proc HasGPUValueField[T](name: static string): bool {.compileTime.} =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1258 for fieldname, value in default(T).fieldPairs():
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1259 when typeof(value) is GPUValue and fieldname == name: return true
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1260 return false
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1261
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1262 template WithGPUValueField(obj: object, name: static string, fieldvalue, body: untyped): untyped =
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1263 # HasGPUValueField MUST be used to check if this is supported
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1264 for fieldname, value in obj.fieldPairs():
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1265 when fieldname == name:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1266 block:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1267 let `fieldvalue` {.inject.} = value
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1268 body
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1269
1172
sam <sam@basx.dev>
parents: 1165
diff changeset
1270 proc Bind[T](pipeline: Pipeline[T], commandBuffer: VkCommandBuffer, currentFrameInFlight: int) =
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1271 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.vk)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1272 #[
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1273 commandBuffer.vkCmdBindDescriptorSets(
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1274 VK_PIPELINE_BIND_POINT_GRAPHICS,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1275 pipeline.layout,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1276 0,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1277 1,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1278 addr pipeline.descriptorSets[currentFrameInFlight],
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1279 0,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1280 nil,
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1281 )
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1282 ]#
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1283
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1284 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals, TMaterial: typedesc) =
1181
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
1285 var descriptorSetCount = 0
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
1286
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1287 for shaderAttributeName, shaderAttribute in default(TShader).fieldPairs:
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1288 var foundField = false
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1289
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1290 # Vertex input data
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1291 when hasCustomPragma(shaderAttribute, VertexAttribute):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1292 assert typeof(shaderAttribute) is SupportedGPUType
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1293 for meshName, meshValue in default(TMesh).fieldPairs:
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1294 when meshName == shaderAttributeName:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1295 assert meshValue is GPUArray, "Mesh attribute '" & meshName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(meshValue))
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1296 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & "' has been found more than once"
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1297 assert elementType(meshValue.data) is typeof(shaderAttribute), "Shader input " & tt.name(TShader) & "." & shaderAttributeName & " is of type '" & tt.name(typeof(shaderAttribute)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue.data)) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1298 foundField = true
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1299 assert foundField, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & ": " & tt.name(typeof(shaderAttribute)) & "' not found in '" & tt.name(TMesh) & "'"
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1300
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1301 # Instance input data
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1302 elif hasCustomPragma(shaderAttribute, InstanceAttribute):
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1303 assert typeof(shaderAttribute) is SupportedGPUType
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1304 for instanceName, instanceValue in default(TInstance).fieldPairs:
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1305 when instanceName == shaderAttributeName:
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1306 assert instanceValue is GPUArray, "Instance attribute '" & instanceName & "' must be of type 'GPUArray' but is of type " & tt.name(typeof(instanceName))
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1307 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & "' has been found more than once"
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1308 assert elementType(instanceValue.data) is typeof(shaderAttribute), "Shader input " & tt.name(TShader) & "." & shaderAttributeName & " is of type '" & tt.name(typeof(shaderAttribute)) & "' but instance attribute is of type '" & tt.name(elementType(instanceValue.data)) & "'"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1309 foundField = true
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1310 assert foundField, "Shader input '" & tt.name(TShader) & "." & shaderAttributeName & ": " & tt.name(typeof(shaderAttribute)) & "' not found in '" & tt.name(TInstance) & "'"
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1311
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1312 # descriptors
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1313 elif typeof(shaderAttribute) is DescriptorSet:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1314 assert descriptorSetCount <= DescriptorSetType.high.int, &"{tt.name(TShader)}: maximum {DescriptorSetType.high} allowed"
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1315 descriptorSetCount.inc
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1316
1181
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
1317
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1318 when shaderAttribute.sType == GlobalSet:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1319 assert shaderAttribute.sType == default(TGlobals).sType, "Shader has global descriptor set of type '" & $shaderAttribute.sType & "' but matching provided type is '" & $default(TGlobals).sType & "'"
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1320 assert typeof(shaderAttribute) is TGlobals, "Shader has global descriptor set type '" & tt.name(get(genericParams(typeof(shaderAttribute)), 0)) & "' but provided type is " & tt.name(TGlobals)
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1321 elif shaderAttribute.sType == MaterialSet:
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1322 assert shaderAttribute.sType == default(TMaterial).sType, "Shader has material descriptor set of type '" & $shaderAttribute.sType & "' but matching provided type is '" & $default(TMaterial).sType & "'"
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1323 assert typeof(shaderAttribute) is TMaterial, "Shader has materialdescriptor type '" & tt.name(get(genericParams(typeof(shaderAttribute)), 0)) & "' but provided type is " & tt.name(TMaterial)
1181
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
1324
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
1325
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1326 proc Render[TShader, TGlobals, TMaterial, TMesh, TInstance](
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1327 commandBuffer: VkCommandBuffer,
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1328 pipeline: Pipeline[TShader],
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1329 globalSet: TGlobals,
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1330 materialSet: TMaterial,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1331 mesh: TMesh,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1332 instances: TInstance,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1333 ) =
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1334 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals, TMaterial)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1335 #[
1164
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1336 if renderable.vertexBuffers.len > 0:
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1337 commandBuffer.vkCmdBindVertexBuffers(
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1338 firstBinding = 0'u32,
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1339 bindingCount = uint32(renderable.vertexBuffers.len),
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1340 pBuffers = renderable.vertexBuffers.ToCPointer(),
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1341 pOffsets = renderable.bufferOffsets.ToCPointer()
7b4d4d85d9f5 add: descriptor sets allocation
sam <sam@basx.dev>
parents: 1163
diff changeset
1342 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1343 if renderable.indexType != None:
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1344 commandBuffer.vkCmdBindIndexBuffer(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1345 renderable.indexBuffer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1346 renderable.indexBufferOffset,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1347 renderable.indexType,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1348 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1349 commandBuffer.vkCmdDrawIndexed(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1350 indexCount = renderable.indexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1351 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1352 firstIndex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1353 vertexOffset = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1354 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1355 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1356 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1357 commandBuffer.vkCmdDraw(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1358 vertexCount = renderable.vertexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1359 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1360 firstVertex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1361 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1362 )
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1363 ]#
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1364
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1365 when isMainModule:
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1366 import semicongine/platform/window
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1367 import semicongine/vulkan/instance
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1368 import semicongine/vulkan/device
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1369 import semicongine/vulkan/physicaldevice
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1370 import std/options
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1371
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1372 type
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1373 MeshA = object
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1374 position: GPUArray[Vec3f, IndirectGPUMemory]
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1375 indices {.VertexIndices.}: GPUArray[uint16, IndirectGPUMemory]
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1376 InstanceA = object
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1377 rotation: GPUArray[Vec4f, IndirectGPUMemory]
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1378 objPosition: GPUArray[Vec3f, IndirectGPUMemory]
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1379 MaterialA = object
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1380 reflection: float32
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1381 baseColor: Vec3f
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1382 UniformsA = object
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
1383 defaultTexture: Texture[3, IndirectGPUMemory]
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1384 defaultMaterial: GPUValue[MaterialA, IndirectGPUMemory]
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1385 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory]
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
1386 materialTextures: array[3, Texture[3, IndirectGPUMemory]]
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1387 ShaderSettings = object
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1388 gamma: float32
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1389 GlobalsA = object
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
1390 fontAtlas: Texture[1, IndirectGPUMemory]
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1391 settings: GPUValue[ShaderSettings, IndirectGPUMemory]
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1392
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1393 ShaderA = object
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1394 # vertex input
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1395 position {.VertexAttribute.}: Vec3f
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1396 objPosition {.InstanceAttribute.}: Vec3f
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1397 rotation {.InstanceAttribute.}: Vec4f
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1398 # intermediate
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1399 test {.Pass.}: float32
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1400 test1 {.PassFlat.}: Vec3f
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1401 # output
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1402 color {.ShaderOutput.}: Vec4f
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1403 # descriptor sets
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1404 globals: DescriptorSet[GlobalsA, GlobalSet]
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1405 uniforms: DescriptorSet[UniformsA, MaterialSet]
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1406 # code
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1407 vertexCode: string = "void main() {}"
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1408 fragmentCode: string = "void main() {}"
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1409
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1410 let w = CreateWindow("test2")
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1411 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")
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1412
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1413 # TODO: remove those ugly wrappers
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1414 let theInstance = w.CreateInstance(
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1415 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0),
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1416 instanceExtensions = @[],
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1417 layers = @["VK_LAYER_KHRONOS_validation"],
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1418 )
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1419
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1420 let dev = theInstance.CreateDevice(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1421 theInstance.GetPhysicalDevices().FilterBestGraphics(),
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1422 enabledExtensions = @[],
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1423 theInstance.GetPhysicalDevices().FilterBestGraphics().FilterForGraphicsPresentationQueues()
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1424 ).vk
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1425 let frameWidth = 100'u32
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1426 let frameHeight = 100'u32
1162
46fae89cffb0 advance evenmore!
sam <sam@basx.dev>
parents: 1161
diff changeset
1427
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1428 # TODO: pack this stuff into a setup method and condense everything a bit
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1429 let pDevice = theInstance.vk.GetPhysicalDevice()
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1430 let qfi = pDevice.GetQueueFamily(VK_QUEUE_GRAPHICS_BIT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1431 vulkan = VulkanGlobals(
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1432 instance: theInstance.vk,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1433 device: dev,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1434 physicalDevice: pDevice,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1435 queueFamilyIndex: qfi,
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1436 queue: dev.GetQueue(qfi, VK_QUEUE_GRAPHICS_BIT)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1437 )
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1438
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1439 var myMesh1 = MeshA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1440 position: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, ), NewVec3f(0, 0, ), NewVec3f(0, 0, )]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1441 )
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1442 var uniforms1 = DescriptorSet[UniformsA, MaterialSet](
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1443 data: UniformsA(
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1444 materials: GPUValue[array[3, MaterialA], IndirectGPUMemory](data: [
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1445 MaterialA(reflection: 0, baseColor: NewVec3f(1, 0, 0)),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1446 MaterialA(reflection: 0.1, baseColor: NewVec3f(0, 1, 0)),
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1447 MaterialA(reflection: 0.5, baseColor: NewVec3f(0, 0, 1)),
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1448 ]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1449 materialTextures: [
1183
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
1450 Texture[3, IndirectGPUMemory](),
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
1451 Texture[3, IndirectGPUMemory](),
850450bfe2a2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1182
diff changeset
1452 Texture[3, IndirectGPUMemory](),
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1453 ]
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1454 )
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1455 )
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1456 var instances1 = InstanceA(
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1457 rotation: GPUArray[Vec4f, IndirectGPUMemory](data: @[NewVec4f(1, 0, 0, 0.1), NewVec4f(0, 1, 0, 0.1)]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1458 objPosition: GPUArray[Vec3f, IndirectGPUMemory](data: @[NewVec3f(0, 0, 0), NewVec3f(1, 1, 1)]),
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1459 )
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1460 var myGlobals = DescriptorSet[GlobalsA, GlobalSet]()
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1461
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1462 # setup for rendering (TODO: swapchain & framebuffers)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1463 let renderpass = CreateRenderPass(GetSurfaceFormat())
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1464
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1465 # shaders
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1466 const shader = ShaderA()
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1467 let shaderObject = CompileShader(shader)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1468 var pipeline1 = CreatePipeline(renderPass = renderpass, shader = shaderObject)
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1469
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1470 var renderdata = InitRenderData()
1176
511c9f7cd1da sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1173
diff changeset
1471
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1472 # TODO: Textures
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1473 # upload all textures
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1474 # write descriptors for textures and uniform buffers
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
1475
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1476 # buffer allocation
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1477 var
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1478 indirectVertexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1479 directVertexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1480 indirectIndexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1481 directIndexSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1482 indirectUniformSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1483 directUniformSizes = 0'u64
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1484
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1485 # buffer allocation
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1486
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1487 echo "Allocating GPU buffers"
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1488 indirectVertexSizes += GetIndirectBufferSizes(myMesh1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1489 indirectVertexSizes += GetIndirectBufferSizes(instances1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1490 AllocateIndirectBuffer(renderdata, indirectVertexSizes, VertexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1491
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1492 directVertexSizes += GetDirectBufferSizes(myMesh1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1493 directVertexSizes += GetDirectBufferSizes(instances1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1494 AllocateDirectBuffer(renderdata, directVertexSizes, VertexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1495
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1496 indirectIndexSizes += GetIndirectIndexBufferSizes(myMesh1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1497 AllocateIndirectBuffer(renderdata, indirectIndexSizes, IndexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1498
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1499 directIndexSizes += GetDirectIndexBufferSizes(myMesh1)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1500 AllocateDirectBuffer(renderdata, directIndexSizes, IndexBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1501
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1502 indirectUniformSizes += GetIndirectBufferSizes(uniforms1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1503 indirectUniformSizes += GetIndirectBufferSizes(myGlobals)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1504 AllocateIndirectBuffer(renderdata, indirectUniformSizes, UniformBuffer)
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1505
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1506 directUniformSizes += GetDirectBufferSizes(uniforms1)
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1507 directUniformSizes += GetDirectBufferSizes(myGlobals)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1508 AllocateDirectBuffer(renderdata, directUniformSizes, UniformBuffer)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1509
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1510 # buffer assignment
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1511 #
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1512 echo "Assigning buffers to GPUData fields"
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1513
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1514 # for meshes we do:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1515 renderdata.AssignIndirectBuffers(VertexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1516 renderdata.AssignDirectBuffers(VertexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1517 renderdata.AssignIndirectBuffers(IndexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1518 renderdata.AssignDirectBuffers(IndexBuffer, myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1519
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1520 # for instances we do:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1521 renderdata.AssignIndirectBuffers(VertexBuffer, instances1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1522 renderdata.AssignDirectBuffers(VertexBuffer, instances1)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1523
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1524 # for uniforms/globals we do:
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1525 renderdata.AssignIndirectBuffers(UniformBuffer, uniforms1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1526 renderdata.AssignDirectBuffers(UniformBuffer, uniforms1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1527 renderdata.AssignIndirectBuffers(UniformBuffer, myGlobals)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1528 renderdata.AssignDirectBuffers(UniformBuffer, myGlobals)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1529
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1530 # buffer filling
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1531
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1532 echo "Copying all data to GPU memory"
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1533
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1534 # copy everything to GPU
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1535 UpdateAllGPUBuffers(myMesh1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1536 UpdateAllGPUBuffers(instances1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1537 UpdateAllGPUBuffers(uniforms1)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1538 UpdateAllGPUBuffers(myGlobals)
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1539 renderdata.FlushDirectMemory()
1177
4ef959278451 sync from bedroom to office
sam <sam@basx.dev>
parents: 1176
diff changeset
1540
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1541
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1542 # descriptors
1181
6b66e6c837bc sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1180
diff changeset
1543 # TODO: I think we can write and assign descriptors directly after creation
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1544 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[GlobalSet], myGlobals)
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1545 InitDescriptorSet(renderdata, pipeline1.descriptorSetLayouts[MaterialSet], uniforms1)
1180
d554f8185815 add: tons of work on descriptors
sam <sam@basx.dev>
parents: 1179
diff changeset
1546 # WriteDescriptors[ShaderA, UniformsA, GlobalsA](renderdata, uniforms1, myGlobals)
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1547
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1548
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1549 # command buffer
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1550 var
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1551 commandBufferPool: VkCommandPool
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1552 createInfo = VkCommandPoolCreateInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1553 sType: VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1554 flags: toBits [VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT],
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1555 queueFamilyIndex: vulkan.queueFamilyIndex,
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1556 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1557 checkVkResult vkCreateCommandPool(vulkan.device, addr createInfo, nil, addr commandBufferPool)
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1558 var
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1559 cmdBuffers: array[INFLIGHTFRAMES.int, VkCommandBuffer]
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1560 allocInfo = VkCommandBufferAllocateInfo(
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1561 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1562 commandPool: commandBufferPool,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1563 level: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1564 commandBufferCount: INFLIGHTFRAMES,
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1565 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1566 checkVkResult vkAllocateCommandBuffers(vulkan.device, addr allocInfo, cmdBuffers.ToCPointer)
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1567
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1568 # start command buffer
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1569 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1570 let
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1571 currentFramebuffer = VkFramebuffer(0) # TODO
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1572 currentFrameInFlight = 1
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1573 cmd = cmdBuffers[currentFrameInFlight]
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1574 beginInfo = VkCommandBufferBeginInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1575 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1576 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1577 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1578 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1579 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo))
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1580
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1581 # start renderpass
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1582 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1583 var
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1584 clearColors = [VkClearValue(color: VkClearColorValue(float32: [0, 0, 0, 0]))]
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1585 renderPassInfo = VkRenderPassBeginInfo(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1586 sType: VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1587 renderPass: renderpass,
1178
ec4ff70299f2 sync from bedroom to office
sam <sam@basx.dev>
parents: 1177
diff changeset
1588 framebuffer: currentFramebuffer, # TODO
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1589 renderArea: VkRect2D(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1590 offset: VkOffset2D(x: 0, y: 0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1591 extent: VkExtent2D(width: frameWidth, height: frameHeight),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1592 ),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1593 clearValueCount: uint32(clearColors.len),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1594 pClearValues: clearColors.ToCPointer(),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1595 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1596 viewport = VkViewport(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1597 x: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1598 y: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1599 width: frameWidth.float32,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1600 height: frameHeight.float32,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1601 minDepth: 0.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1602 maxDepth: 1.0,
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1603 )
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1604 scissor = VkRect2D(
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1605 offset: VkOffset2D(x: 0, y: 0),
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1606 extent: VkExtent2D(width: frameWidth, height: frameHeight)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1607 )
1179
e1830f9b8af4 sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1178
diff changeset
1608 vkCmdBeginRenderPass(cmd, addr(renderPassInfo), VK_SUBPASS_CONTENTS_INLINE)
1163
438d32d8b14f add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents: 1162
diff changeset
1609
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1610 # setup viewport
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1611 vkCmdSetViewport(cmd, firstViewport = 0, viewportCount = 1, addr(viewport))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1612 vkCmdSetScissor(cmd, firstScissor = 0, scissorCount = 1, addr(scissor))
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1613
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1614 # bind pipeline, will be loop
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1615 block:
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1616 Bind(pipeline1, cmd, currentFrameInFlight = currentFrameInFlight)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1617
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1618 # render object, will be loop
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1619 block:
1182
e9a212e9cdf7 sync from bedroom to office
sam <sam@basx.dev>
parents: 1181
diff changeset
1620 Render(cmd, pipeline1, myGlobals, uniforms1, myMesh1, instances1)
1173
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1621
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1622 vkCmdEndRenderPass(cmd)
fafc2f14da0b more stuff
sam <sam@basx.dev>
parents: 1172
diff changeset
1623 checkVkResult cmd.vkEndCommandBuffer()