annotate static_utils.nim @ 1185:565fcfde427a compiletime-tests

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