annotate static_utils.nim @ 1188:427c11c0aeb1 compiletime-tests

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