annotate static_utils.nim @ 1182:e9a212e9cdf7 compiletime-tests

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