annotate semiconginev2/rendering/renderer.nim @ 1254:b0f4c8ccd49a

did: stuff to test gltf importer
author sam <sam@basx.dev>
date Sat, 27 Jul 2024 20:47:54 +0700
parents c4f98eb4bb05
children 2b5ca798f6d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
1 func pointerAddOffset[T: SomeInteger](p: pointer, offset: T): pointer =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
2 cast[pointer](cast[T](p) + offset)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
3
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
4 func usage(bType: BufferType): seq[VkBufferUsageFlagBits] =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
5 case bType:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
6 of VertexBuffer: @[VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
7 of VertexBufferMapped: @[VK_BUFFER_USAGE_VERTEX_BUFFER_BIT]
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
8 of IndexBuffer: @[VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
9 of IndexBufferMapped: @[VK_BUFFER_USAGE_INDEX_BUFFER_BIT]
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
10 of UniformBuffer: @[VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT]
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
11 of UniformBufferMapped: @[VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT]
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
12
1212
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1210
diff changeset
13 proc GetVkFormat(grayscale: bool, usage: openArray[VkImageUsageFlagBits]): VkFormat =
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1210
diff changeset
14 let formats = if grayscale: [VK_FORMAT_R8_SRGB, VK_FORMAT_R8_UNORM]
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1212
diff changeset
15 else: [VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_B8G8R8A8_UNORM]
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
16
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
17 var formatProperties = VkImageFormatProperties2(sType: VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2)
1212
518a952eccbf did: increase texture format compatability
sam <sam@basx.dev>
parents: 1210
diff changeset
18 for format in formats:
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
19 var formatInfo = VkPhysicalDeviceImageFormatInfo2(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
20 sType: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
21 format: format,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
22 thetype: VK_IMAGE_TYPE_2D,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
23 tiling: VK_IMAGE_TILING_OPTIMAL,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
24 usage: usage.toBits,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
25 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
26 let formatCheck = vkGetPhysicalDeviceImageFormatProperties2(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
27 vulkan.physicalDevice,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
28 addr formatInfo,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
29 addr formatProperties,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
30 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
31 if formatCheck == VK_SUCCESS: # found suitable format
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
32 return format
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
33 elif formatCheck == VK_ERROR_FORMAT_NOT_SUPPORTED: # nope, try to find other format
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
34 continue
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
35 else: # raise error
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
36 checkVkResult formatCheck
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
37
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
38 assert false, "Unable to find format for textures"
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
39
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
40
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
41 func alignedTo[T: SomeInteger](value: T, alignment: T): T =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
42 let remainder = value mod alignment
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
43 if remainder == 0:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
44 return value
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
45 else:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
46 return value + alignment - remainder
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
47
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
48 template sType(descriptorSet: DescriptorSet): untyped =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
49 get(genericParams(typeof(descriptorSet)), 1)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
50
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
51 template bufferType(gpuData: GPUData): untyped =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
52 typeof(gpuData).TBuffer
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
53 func NeedsMapping(bType: BufferType): bool =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
54 bType in [VertexBufferMapped, IndexBufferMapped, UniformBufferMapped]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
55 template NeedsMapping(gpuData: GPUData): untyped =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
56 gpuData.bufferType.NeedsMapping
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
57
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
58 template size(gpuArray: GPUArray): uint64 =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
59 (gpuArray.data.len * sizeof(elementType(gpuArray.data))).uint64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
60 template size(gpuValue: GPUValue): uint64 =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
61 sizeof(gpuValue.data).uint64
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
62 func size(image: Image): uint64 =
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
63 image.data.len.uint64 * sizeof(elementType(image.data)).uint64
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
64
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
65 template rawPointer(gpuArray: GPUArray): pointer =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
66 addr(gpuArray.data[0])
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
67 template rawPointer(gpuValue: GPUValue): pointer =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
68 addr(gpuValue.data)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
69
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
70 proc IsMappable(memoryTypeIndex: uint32): bool =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
71 var physicalProperties: VkPhysicalDeviceMemoryProperties
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
72 vkGetPhysicalDeviceMemoryProperties(vulkan.physicalDevice, addr(physicalProperties))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
73 let flags = toEnums(physicalProperties.memoryTypes[memoryTypeIndex].propertyFlags)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
74 return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in flags
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
75
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1190
diff changeset
76 proc InitDescriptorSet*(
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
77 renderData: RenderData,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
78 layout: VkDescriptorSetLayout,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
79 descriptorSet: var DescriptorSet,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
80 ) =
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
81
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
82 # santization checks
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
83 for theName, value in descriptorSet.data.fieldPairs:
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
84 when typeof(value) is GPUValue:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
85 assert value.buffer.vk.Valid
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
86 elif typeof(value) is Image:
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
87 assert value.vk.Valid
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
88 assert value.imageview.Valid
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
89 assert value.sampler.Valid
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
90 elif typeof(value) is array:
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
91 when elementType(value) is Image:
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
92 for t in value:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
93 assert t.vk.Valid
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
94 assert t.imageview.Valid
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
95 assert t.sampler.Valid
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
96 elif elementType(value) is GPUValue:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
97 for t in value:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
98 assert t.buffer.vk.Valid
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
99 else:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
100 {.error: "Unsupported descriptor set field: '" & theName & "'".}
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
101 else:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
102 {.error: "Unsupported descriptor set field: '" & theName & "'".}
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
103
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
104 # allocate
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
105 var layouts = newSeqWith(descriptorSet.vk.len, layout)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
106 var allocInfo = VkDescriptorSetAllocateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
107 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
108 descriptorPool: renderData.descriptorPool,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
109 descriptorSetCount: uint32(layouts.len),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
110 pSetLayouts: layouts.ToCPointer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
111 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
112 checkVkResult vkAllocateDescriptorSets(vulkan.device, addr(allocInfo), descriptorSet.vk.ToCPointer)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
113
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
114 # allocate seq with high cap to prevent realocation while adding to set
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
115 # (which invalidates pointers that are passed to the vulkan api call)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
116 var descriptorSetWrites = newSeqOfCap[VkWriteDescriptorSet](1024)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
117 var imageWrites = newSeqOfCap[VkDescriptorImageInfo](1024)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
118 var bufferWrites = newSeqOfCap[VkDescriptorBufferInfo](1024)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
119
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
120 ForDescriptorFields(descriptorSet.data, fieldName, fieldValue, descriptorType, descriptorCount, descriptorBindingNumber):
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
121 for i in 0 ..< descriptorSet.vk.len:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
122 when typeof(fieldValue) is GPUValue:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
123 bufferWrites.add VkDescriptorBufferInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
124 buffer: fieldValue.buffer.vk,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
125 offset: fieldValue.offset,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
126 range: fieldValue.size,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
127 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
128 descriptorSetWrites.add VkWriteDescriptorSet(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
129 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
130 dstSet: descriptorSet.vk[i],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
131 dstBinding: descriptorBindingNumber,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
132 dstArrayElement: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
133 descriptorType: descriptorType,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
134 descriptorCount: descriptorCount,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
135 pImageInfo: nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
136 pBufferInfo: addr(bufferWrites[^1]),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
137 )
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
138 elif typeof(fieldValue) is Image:
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
139 imageWrites.add VkDescriptorImageInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
140 sampler: fieldValue.sampler,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
141 imageView: fieldValue.imageView,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
142 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
143 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
144 descriptorSetWrites.add VkWriteDescriptorSet(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
145 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
146 dstSet: descriptorSet.vk[i],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
147 dstBinding: descriptorBindingNumber,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
148 dstArrayElement: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
149 descriptorType: descriptorType,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
150 descriptorCount: descriptorCount,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
151 pImageInfo: addr(imageWrites[^1]),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
152 pBufferInfo: nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
153 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
154 elif typeof(fieldValue) is array:
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
155 when elementType(fieldValue) is Image:
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
156 for image in fieldValue:
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
157 imageWrites.add VkDescriptorImageInfo(
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
158 sampler: image.sampler,
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
159 imageView: image.imageView,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
160 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
161 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
162 descriptorSetWrites.add VkWriteDescriptorSet(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
163 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
164 dstSet: descriptorSet.vk[i],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
165 dstBinding: descriptorBindingNumber,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
166 dstArrayElement: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
167 descriptorType: descriptorType,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
168 descriptorCount: descriptorCount,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
169 pImageInfo: addr(imageWrites[^descriptorCount.int]),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
170 pBufferInfo: nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
171 )
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
172 elif elementType(fieldValue) is GPUValue:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
173 for entry in fieldValue:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
174 bufferWrites.add VkDescriptorBufferInfo(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
175 buffer: entry.buffer.vk,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
176 offset: entry.offset,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
177 range: entry.size,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
178 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
179 descriptorSetWrites.add VkWriteDescriptorSet(
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
180 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
181 dstSet: descriptorSet.vk[i],
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
182 dstBinding: descriptorBindingNumber,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
183 dstArrayElement: 0,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
184 descriptorType: descriptorType,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
185 descriptorCount: descriptorCount,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
186 pImageInfo: nil,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
187 pBufferInfo: addr(bufferWrites[^descriptorCount.int]),
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
188 )
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
189 else:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
190 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(fieldValue)).}
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
191 else:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
192 {.error: "Unsupported descriptor type: " & typetraits.name(typeof(fieldValue)).}
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
193
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
194 vkUpdateDescriptorSets(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
195 device = vulkan.device,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
196 descriptorWriteCount = descriptorSetWrites.len.uint32,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
197 pDescriptorWrites = descriptorSetWrites.ToCPointer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
198 descriptorCopyCount = 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
199 pDescriptorCopies = nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
200 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
201
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
202 proc AllocateNewMemoryBlock(size: uint64, mType: uint32): MemoryBlock =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
203 result = MemoryBlock(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
204 vk: svkAllocateMemory(size, mType),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
205 size: size,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
206 rawPointer: nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
207 offsetNextFree: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
208 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
209 if mType.IsMappable():
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
210 checkVkResult vkMapMemory(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
211 device = vulkan.device,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
212 memory = result.vk,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
213 offset = 0'u64,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
214 size = result.size,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
215 flags = VkMemoryMapFlags(0),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
216 ppData = addr(result.rawPointer)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
217 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
218
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
219 proc FlushBuffer*(buffer: Buffer) =
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
220 var flushRegion = VkMappedMemoryRange(
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
221 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
222 memory: buffer.memory,
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
223 offset: buffer.memoryOffset,
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
224 size: buffer.size,
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
225 )
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
226 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, 1, addr(flushRegion))
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
227
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1190
diff changeset
228 proc FlushAllMemory*(renderData: RenderData) =
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
229 var flushRegions = newSeq[VkMappedMemoryRange]()
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
230 for memoryBlocks in renderData.memory:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
231 for memoryBlock in memoryBlocks:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
232 if memoryBlock.rawPointer != nil and memoryBlock.offsetNextFree > 0:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
233 flushRegions.add VkMappedMemoryRange(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
234 sType: VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
235 memory: memoryBlock.vk,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
236 size: alignedTo(memoryBlock.offsetNextFree, svkGetPhysicalDeviceProperties().limits.nonCoherentAtomSize),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
237 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
238 if flushRegions.len > 0:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
239 checkVkResult vkFlushMappedMemoryRanges(vulkan.device, flushRegions.len.uint32, flushRegions.ToCPointer())
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
240
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
241 proc AllocateNewBuffer(renderData: var RenderData, size: uint64, bufferType: BufferType): Buffer =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
242 result = Buffer(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
243 vk: svkCreateBuffer(size, bufferType.usage),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
244 size: size,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
245 rawPointer: nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
246 offsetNextFree: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
247 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
248 let memoryRequirements = svkGetBufferMemoryRequirements(result.vk)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
249 let memoryType = BestMemory(mappable = bufferType.NeedsMapping, filter = memoryRequirements.memoryTypes)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
250
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
251 # check if there is an existing allocated memory block that is large enough to be used
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
252 var selectedBlockI = -1
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
253 for i in 0 ..< renderData.memory[memoryType].len:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
254 let memoryBlock = renderData.memory[memoryType][i]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
255 if memoryBlock.size - alignedTo(memoryBlock.offsetNextFree, memoryRequirements.alignment) >= memoryRequirements.size:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
256 selectedBlockI = i
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
257 break
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
258 # otherwise, allocate a new block of memory and use that
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
259 if selectedBlockI < 0:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
260 selectedBlockI = renderData.memory[memoryType].len
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
261 renderData.memory[memoryType].add AllocateNewMemoryBlock(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
262 size = max(memoryRequirements.size, MEMORY_BLOCK_ALLOCATION_SIZE),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
263 mType = memoryType
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
264 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
265
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
266 let selectedBlock = renderData.memory[memoryType][selectedBlockI]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
267 renderData.memory[memoryType][selectedBlockI].offsetNextFree = alignedTo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
268 selectedBlock.offsetNextFree,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
269 memoryRequirements.alignment,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
270 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
271 checkVkResult vkBindBufferMemory(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
272 vulkan.device,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
273 result.vk,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
274 selectedBlock.vk,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
275 selectedBlock.offsetNextFree,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
276 )
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
277 result.memory = selectedBlock.vk
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
278 result.memoryOffset = selectedBlock.offsetNextFree
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
279 result.rawPointer = selectedBlock.rawPointer.pointerAddOffset(selectedBlock.offsetNextFree)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
280 renderData.memory[memoryType][selectedBlockI].offsetNextFree += memoryRequirements.size
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
281
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
282 proc UpdateGPUBuffer*(gpuData: GPUData, flush = false, allFrames = false) =
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
283 if gpuData.size == 0:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
284 return
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
285
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
286 when NeedsMapping(gpuData):
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
287 copyMem(pointerAddOffset(gpuData.buffer.rawPointer, gpuData.offset), gpuData.rawPointer, gpuData.size)
1231
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
288 if flush:
70f6c1cfe005 add: incomplete cube demo
sam <sam@basx.dev>
parents: 1229
diff changeset
289 FlushBuffer(gpuData.buffer)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
290 else:
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
291 WithStagingBuffer((gpuData.buffer.vk, gpuData.offset), gpuData.size, stagingPtr):
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
292 copyMem(stagingPtr, gpuData.rawPointer, gpuData.size)
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
293
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
294 proc UpdateAllGPUBuffers*[T](value: T, flush = false, allFrames = false) =
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
295 for name, fieldvalue in value.fieldPairs():
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
296 when typeof(fieldvalue) is GPUData:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
297 UpdateGPUBuffer(fieldvalue, flush = flush, allFrames = allFrames)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
298 when typeof(fieldvalue) is array:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
299 when elementType(fieldvalue) is GPUData:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
300 for entry in fieldvalue:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
301 UpdateGPUBuffer(entry, flush = flush, allFrames = allFrames)
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
302
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
303 proc AllocateGPUData(
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
304 renderdata: var RenderData,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
305 bufferType: BufferType,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
306 size: uint64,
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
307 ): (Buffer, uint64) =
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
308
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
309 # find buffer that has space
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
310 var selectedBufferI = -1
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
311
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
312 for i in 0 ..< renderData.buffers[bufferType].len:
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
313 let buffer = renderData.buffers[bufferType][i]
1240
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
314 if buffer.size - alignedTo(buffer.offsetNextFree, BUFFER_ALIGNMENT) >= size:
42eeb59f3a43 add: more tests, line and point rendering
sam <sam@basx.dev>
parents: 1239
diff changeset
315 selectedBufferI = i
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
316
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
317 # otherwise create new buffer
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
318 if selectedBufferI < 0:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
319 selectedBufferI = renderdata.buffers[bufferType].len
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
320 renderdata.buffers[bufferType].add renderdata.AllocateNewBuffer(
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
321 size = max(size, BUFFER_ALLOCATION_SIZE),
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
322 bufferType = bufferType,
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
323 )
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
324
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
325 # assigne value
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
326 let selectedBuffer = renderdata.buffers[bufferType][selectedBufferI]
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
327 renderdata.buffers[bufferType][selectedBufferI].offsetNextFree = alignedTo(
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
328 selectedBuffer.offsetNextFree,
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
329 BUFFER_ALIGNMENT
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
330 )
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
331
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
332 result[0] = selectedBuffer
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
333 result[1] = renderdata.buffers[bufferType][selectedBufferI].offsetNextFree
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
334 renderdata.buffers[bufferType][selectedBufferI].offsetNextFree += size
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
335
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
336 proc AssignBuffers*[T](renderdata: var RenderData, data: var T, uploadData = true) =
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
337 for name, value in fieldPairs(data):
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
338
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
339 when typeof(value) is GPUData:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
340 (value.buffer, value.offset) = AllocateGPUData(renderdata, value.bufferType, value.size)
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents: 1231
diff changeset
341
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents: 1231
diff changeset
342 elif typeof(value) is DescriptorSet:
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents: 1231
diff changeset
343 AssignBuffers(renderdata, value.data, uploadData = uploadData)
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents: 1231
diff changeset
344
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
345 elif typeof(value) is array:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
346 when elementType(value) is GPUValue:
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
347 for v in value.mitems:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
348 (v.buffer, v.offset) = AllocateGPUData(renderdata, v.bufferType, v.size)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
349
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
350 if uploadData:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
351 UpdateAllGPUBuffers(data, flush = true, allFrames = true)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
352
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
353 proc AssignBuffers*(renderdata: var RenderData, descriptorSet: var DescriptorSet, uploadData = true) =
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
354 AssignBuffers(renderdata, descriptorSet.data, uploadData = uploadData)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
355
1192
90bf0cab1d02 did: so many, many things
sam <sam@basx.dev>
parents: 1190
diff changeset
356 proc InitRenderData*(descriptorPoolLimit = 1024'u32): RenderData =
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
357 # allocate descriptor pools
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
358 var poolSizes = [
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
359 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: descriptorPoolLimit),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
360 VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: descriptorPoolLimit),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
361 ]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
362 var poolInfo = VkDescriptorPoolCreateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
363 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
364 poolSizeCount: poolSizes.len.uint32,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
365 pPoolSizes: poolSizes.ToCPointer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
366 maxSets: descriptorPoolLimit,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
367 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
368 checkVkResult vkCreateDescriptorPool(vulkan.device, addr(poolInfo), nil, addr(result.descriptorPool))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
369
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
370 proc DestroyRenderData*(renderData: RenderData) =
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
371 vkDestroyDescriptorPool(vulkan.device, renderData.descriptorPool, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
372
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
373 for buffers in renderData.buffers:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
374 for buffer in buffers:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
375 vkDestroyBuffer(vulkan.device, buffer.vk, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
376
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
377 for imageView in renderData.imageViews:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
378 vkDestroyImageView(vulkan.device, imageView, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
379
1201
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
380 for sampler in renderData.samplers:
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
381 vkDestroySampler(vulkan.device, sampler, nil)
d4a206b5e5b0 add: rest of the cleanup stuff
sam <sam@basx.dev>
parents: 1200
diff changeset
382
1200
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
383 for image in renderData.images:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
384 vkDestroyImage(vulkan.device, image, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
385
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
386 for memoryBlocks in renderData.memory:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
387 for memory in memoryBlocks:
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
388 vkFreeMemory(vulkan.device, memory.vk, nil)
5c6491f28dcd did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents: 1199
diff changeset
389
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
390 proc TransitionImageLayout(image: VkImage, oldLayout, newLayout: VkImageLayout) =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
391 var
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
392 barrier = VkImageMemoryBarrier(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
393 sType: VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
394 oldLayout: oldLayout,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
395 newLayout: newLayout,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
396 srcQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
397 dstQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
398 image: image,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
399 subresourceRange: VkImageSubresourceRange(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
400 aspectMask: toBits [VK_IMAGE_ASPECT_COLOR_BIT],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
401 baseMipLevel: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
402 levelCount: 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
403 baseArrayLayer: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
404 layerCount: 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
405 ),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
406 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
407 srcStage: VkPipelineStageFlagBits
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
408 dstStage: VkPipelineStageFlagBits
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
409
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
410 if oldLayout == VK_IMAGE_LAYOUT_UNDEFINED and newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
411 srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
412 barrier.srcAccessMask = VkAccessFlags(0)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
413 dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
414 barrier.dstAccessMask = [VK_ACCESS_TRANSFER_WRITE_BIT].toBits
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
415 elif oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL and newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
416 srcStage = VK_PIPELINE_STAGE_TRANSFER_BIT
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
417 barrier.srcAccessMask = [VK_ACCESS_TRANSFER_WRITE_BIT].toBits
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
418 dstStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
419 barrier.dstAccessMask = [VK_ACCESS_SHADER_READ_BIT].toBits
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
420 else:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
421 raise newException(Exception, "Unsupported layout transition!")
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
422
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
423 WithSingleUseCommandBuffer(commandBuffer):
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
424 vkCmdPipelineBarrier(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
425 commandBuffer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
426 srcStageMask = [srcStage].toBits,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
427 dstStageMask = [dstStage].toBits,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
428 dependencyFlags = VkDependencyFlags(0),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
429 memoryBarrierCount = 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
430 pMemoryBarriers = nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
431 bufferMemoryBarrierCount = 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
432 pBufferMemoryBarriers = nil,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
433 imageMemoryBarrierCount = 1,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
434 pImageMemoryBarriers = addr(barrier),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
435 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
436
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
437 proc createSampler(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
438 magFilter = VK_FILTER_LINEAR,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
439 minFilter = VK_FILTER_LINEAR,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
440 addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
441 addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
442 ): VkSampler =
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
443
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
444 let samplerInfo = VkSamplerCreateInfo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
445 sType: VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
446 magFilter: magFilter,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
447 minFilter: minFilter,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
448 addressModeU: addressModeU,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
449 addressModeV: addressModeV,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
450 addressModeW: VK_SAMPLER_ADDRESS_MODE_REPEAT,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
451 anisotropyEnable: vulkan.anisotropy > 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
452 maxAnisotropy: vulkan.anisotropy,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
453 borderColor: VK_BORDER_COLOR_INT_OPAQUE_BLACK,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
454 unnormalizedCoordinates: VK_FALSE,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
455 compareEnable: VK_FALSE,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
456 compareOp: VK_COMPARE_OP_ALWAYS,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
457 mipmapMode: VK_SAMPLER_MIPMAP_MODE_LINEAR,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
458 mipLodBias: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
459 minLod: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
460 maxLod: 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
461 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
462 checkVkResult vkCreateSampler(vulkan.device, addr(samplerInfo), nil, addr(result))
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
463
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
464 proc createVulkanImage(renderData: var RenderData, image: var Image) =
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
465 assert image.vk == VkImage(0), "Image has already been created"
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1212
diff changeset
466 var usage = @[VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_USAGE_SAMPLED_BIT]
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
467 if image.isRenderTarget:
1214
04e446a7eb2b add: multipass renderer, finish tets for now
sam <sam@basx.dev>
parents: 1212
diff changeset
468 usage.add VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents: 1231
diff changeset
469 let format = GetVkFormat(grayscale = elementType(image.data) is Gray, usage = usage)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
470
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
471 image.vk = svkCreate2DImage(image.width, image.height, format, usage, image.samples)
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
472 renderData.images.add image.vk
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1240
diff changeset
473 image.sampler = createSampler(
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1240
diff changeset
474 magFilter = image.magInterpolation,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1240
diff changeset
475 minFilter = image.minInterpolation,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1240
diff changeset
476 addressModeU = image.wrapU,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1240
diff changeset
477 addressModeV = image.wrapV,
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1240
diff changeset
478 )
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
479 renderData.samplers.add image.sampler
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
480
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
481 let memoryRequirements = image.vk.svkGetImageMemoryRequirements()
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
482 let memoryType = BestMemory(mappable = false, filter = memoryRequirements.memoryTypes)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
483 # check if there is an existing allocated memory block that is large enough to be used
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
484 var selectedBlockI = -1
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
485 for i in 0 ..< renderData.memory[memoryType].len:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
486 let memoryBlock = renderData.memory[memoryType][i]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
487 if memoryBlock.size - alignedTo(memoryBlock.offsetNextFree, memoryRequirements.alignment) >= memoryRequirements.size:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
488 selectedBlockI = i
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
489 break
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
490 # otherwise, allocate a new block of memory and use that
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
491 if selectedBlockI < 0:
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
492 selectedBlockI = renderData.memory[memoryType].len
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
493 renderData.memory[memoryType].add AllocateNewMemoryBlock(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
494 size = max(memoryRequirements.size, MEMORY_BLOCK_ALLOCATION_SIZE),
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
495 mType = memoryType
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
496 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
497 let selectedBlock = renderData.memory[memoryType][selectedBlockI]
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
498 renderData.memory[memoryType][selectedBlockI].offsetNextFree = alignedTo(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
499 selectedBlock.offsetNextFree,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
500 memoryRequirements.alignment,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
501 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
502
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
503 checkVkResult vkBindImageMemory(
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
504 vulkan.device,
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
505 image.vk,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
506 selectedBlock.vk,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
507 renderData.memory[memoryType][selectedBlockI].offsetNextFree,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
508 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
509 renderData.memory[memoryType][selectedBlockI].offsetNextFree += memoryRequirements.size
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
510
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
511 # imageview can only be created after memory is bound
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
512 image.imageview = svkCreate2DImageView(image.vk, format)
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
513 renderData.imageViews.add image.imageview
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
514
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
515 # data transfer and layout transition
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
516 TransitionImageLayout(image.vk, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
1229
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
517 if image.data.len > 0:
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
518 WithStagingBuffer(
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
519 (image.vk, image.width, image.height),
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
520 memoryRequirements.size,
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
521 stagingPtr
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
522 ):
5dcb503ef0c0 did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents: 1228
diff changeset
523 copyMem(stagingPtr, image.data.ToCPointer, image.size)
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
524 TransitionImageLayout(image.vk, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
525
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
526
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
527 proc UploadImages*(renderdata: var RenderData, descriptorSet: var DescriptorSet) =
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
528 for name, value in fieldPairs(descriptorSet.data):
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
529 when typeof(value) is Image:
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
530 renderdata.createVulkanImage(value)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
531 elif typeof(value) is array:
1228
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
532 when elementType(value) is Image:
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
533 for image in value.mitems:
4e465583ea32 did: rename texture to image
sam <sam@basx.dev>
parents: 1218
diff changeset
534 renderdata.createVulkanImage(image)
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
535
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
536 type EMPTY = object # only used for static assertions
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
537
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
538 proc assertAllDescriptorsBound(A, B, C, D, TShader: typedesc) =
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
539 var foundDescriptorSets = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
540 for attrName, attrValue in default(TShader).fieldPairs():
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
541 when hasCustomPragma(attrValue, DescriptorSets):
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
542 assert not foundDescriptorSets, "Only one shader attribute is allowed to have the pragma 'DescriptorSets'"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
543 when not (A is EMPTY): assert typeof(attrValue[0]) is A
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
544 when not (B is EMPTY): assert typeof(attrValue[1]) is B
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
545 when not (C is EMPTY): assert typeof(attrValue[2]) is C
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
546 when not (D is EMPTY): assert typeof(attrValue[3]) is D
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
547
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
548 var hasBoundDescriptorSets {.compileTime.} = false # okay, I am not sure if this is clean, unproblematic or sane. Just trying to get some comptime-validation
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
549 var hasDescriptorSets {.compileTime} = false
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
550
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
551 template WithBind*[A, B, C, D, TShader](commandBuffer: VkCommandBuffer, sets: (DescriptorSet[A], DescriptorSet[B], DescriptorSet[C], DescriptorSet[D]), pipeline: Pipeline[TShader], body: untyped): untyped =
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
552 static: assertAllDescriptorsBound(A, B, C, D, TShader)
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
553 block:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
554 var descriptorSets: seq[VkDescriptorSet]
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
555 for dSet in sets.fields:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
556 assert dSet.vk[currentFiF()].Valid, "DescriptorSet not initialized, maybe forgot to call InitDescriptorSet"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
557 descriptorSets.add dSet.vk[currentFiF()]
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
558 svkCmdBindDescriptorSets(commandBuffer, descriptorSets, pipeline.layout)
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
559 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
560 assert not hasBoundDescriptorSets, "Cannot call WithBind nested"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
561 hasBoundDescriptorSets = true
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
562 body
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
563 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
564 hasBoundDescriptorSets = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
565 template WithBind*[A, B, C, TShader](commandBuffer: VkCommandBuffer, sets: (DescriptorSet[A], DescriptorSet[B], DescriptorSet[C]), pipeline: Pipeline[TShader], body: untyped): untyped =
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
566 static: assertAllDescriptorsBound(A, B, C, EMPTY, TShader)
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
567 block:
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
568 var descriptorSets: seq[VkDescriptorSet]
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
569 for dSet in sets.fields:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
570 assert dSet.vk[currentFiF()].Valid, "DescriptorSet not initialized, maybe forgot to call InitDescriptorSet"
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
571 descriptorSets.add dSet.vk[currentFiF()]
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
572 svkCmdBindDescriptorSets(commandBuffer, descriptorSets, pipeline.layout)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
573 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
574 assert not hasBoundDescriptorSets, "Cannot call WithBind nested"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
575 hasBoundDescriptorSets = true
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
576 body
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
577 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
578 hasBoundDescriptorSets = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
579 template WithBind*[A, B, TShader](commandBuffer: VkCommandBuffer, sets: (DescriptorSet[A], DescriptorSet[B]), pipeline: Pipeline[TShader], body: untyped): untyped =
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
580 static: assertAllDescriptorsBound(A, B, EMPTY, EMPTY, TShader)
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
581 block:
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
582 var descriptorSets: seq[VkDescriptorSet]
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
583 for dSet in sets.fields:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
584 assert dSet.vk[currentFiF()].Valid, "DescriptorSet not initialized, maybe forgot to call InitDescriptorSet"
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
585 descriptorSets.add dSet.vk[currentFiF()]
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
586 svkCmdBindDescriptorSets(commandBuffer, descriptorSets, pipeline.layout)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
587 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
588 assert not hasBoundDescriptorSets, "Cannot call WithBind nested"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
589 hasBoundDescriptorSets = true
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
590 body
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
591 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
592 hasBoundDescriptorSets = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
593 template WithBind*[A, TShader](commandBuffer: VkCommandBuffer, sets: (DescriptorSet[A], ), pipeline: Pipeline[TShader], body: untyped): untyped =
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
594 static: assertAllDescriptorsBound(A, EMPTY, EMPTY, EMPTY, TShader)
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
595 block:
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
596 var descriptorSets: seq[VkDescriptorSet]
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
597 for dSet in sets.fields:
1239
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
598 assert dSet.vk[currentFiF()].Valid, "DescriptorSet not initialized, maybe forgot to call InitDescriptorSet"
69489a678141 add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents: 1236
diff changeset
599 descriptorSets.add dSet.vk[currentFiF()]
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
600 svkCmdBindDescriptorSets(commandBuffer, descriptorSets, pipeline.layout)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
601 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
602 assert not hasBoundDescriptorSets, "Cannot call WithBind nested"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
603 hasBoundDescriptorSets = true
1205
f7530247a21f did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents: 1204
diff changeset
604 body
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
605 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
606 hasBoundDescriptorSets = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
607
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
608 proc assertCanRenderMesh(TShader, TMesh, TInstance: typedesc) =
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
609 for attrName, attrValue in default(TShader).fieldPairs:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
610 if hasCustomPragma(attrValue, VertexAttribute):
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
611 var foundAttr = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
612 for meshAttrName, meshAttrValue in default(TMesh).fieldPairs:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
613 if attrName == meshAttrName:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
614 assert typeof(meshAttrValue) is GPUArray, "Mesh attribute '" & attrName & "' must be a GPUArray"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
615 assert typeof(attrValue) is elementType(meshAttrValue.data), "Type of shader attribute and mesh attribute '" & attrName & "' is not the same"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
616 foundAttr = true
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
617 assert foundAttr, "Attribute '" & attrName & "' is not provided in mesh type '" & name(TMesh) & "'"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
618 if hasCustomPragma(attrValue, InstanceAttribute):
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
619 var foundAttr = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
620 for instAttrName, instAttrValue in default(TInstance).fieldPairs:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
621 if attrName == instAttrName:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
622 assert typeof(instAttrValue) is GPUArray, "Instance attribute '" & attrName & "' must be a GPUArray"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
623 assert foundAttr == false, "Attribute '" & attrName & "' is defined in Mesh and Instance, can only be one"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
624 assert typeof(attrValue) is elementType(instAttrValue.data), "Type of shader attribute and mesh attribute '" & attrName & "' is not the same"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
625 foundAttr = true
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
626 assert foundAttr, "Attribute '" & attrName & "' is not provided in instance type '" & name(TInstance) & "'"
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
627
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
628 proc Render*[TShader, TMesh, TInstance](
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
629 commandBuffer: VkCommandBuffer,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
630 pipeline: Pipeline[TShader],
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
631 mesh: TMesh,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
632 instances: TInstance,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
633 ) =
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
634
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
635 static: assertCanRenderMesh(TShader, TMesh, TInstance)
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
636 static:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
637 hasDescriptorSets = false
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
638 for attrName, attrValue in default(TShader).fieldPairs():
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
639 if attrValue.hasCustomPragma(DescriptorSets):
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
640 hasDescriptorSets = true
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
641 if hasDescriptorSets:
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
642 assert hasBoundDescriptorSets, "Shader uses descriptor sets, but none are bound"
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
643
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
644 var vertexBuffers: seq[VkBuffer]
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
645 var vertexBuffersOffsets: seq[uint64]
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
646 var elementCount = 0'u32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
647 var instanceCount = 1'u32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
648
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
649 for shaderAttributeName, shaderAttribute in default(TShader).fieldPairs:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
650 when hasCustomPragma(shaderAttribute, VertexAttribute):
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
651 for meshName, meshValue in mesh.fieldPairs:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
652 when meshName == shaderAttributeName:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
653 vertexBuffers.add meshValue.buffer.vk
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
654 vertexBuffersOffsets.add meshValue.offset
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
655 elementCount = meshValue.data.len.uint32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
656 elif hasCustomPragma(shaderAttribute, InstanceAttribute):
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
657 for instanceName, instanceValue in instances.fieldPairs:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
658 when instanceName == shaderAttributeName:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
659 vertexBuffers.add instanceValue.buffer.vk
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
660 vertexBuffersOffsets.add instanceValue.offset
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
661 instanceCount = instanceValue.data.len.uint32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
662
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
663 if vertexBuffers.len > 0:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
664 vkCmdBindVertexBuffers(
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
665 commandBuffer = commandBuffer,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
666 firstBinding = 0'u32,
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
667 bindingCount = uint32(vertexBuffers.len),
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
668 pBuffers = vertexBuffers.ToCPointer(),
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
669 pOffsets = vertexBuffersOffsets.ToCPointer()
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
670 )
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
671
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
672 var indexBuffer: VkBuffer
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
673 var indexBufferOffset: uint64
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
674 var indexType = VK_INDEX_TYPE_NONE_KHR
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
675
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
676 for meshName, meshValue in mesh.fieldPairs:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
677 when typeof(meshValue) is GPUArray[uint8, IndexBuffer]:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
678 indexBuffer = meshValue.buffer.vk
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
679 indexBufferOffset = meshValue.offset
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
680 indexType = VK_INDEX_TYPE_UINT8_EXT
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
681 elementCount = meshValue.data.len.uint32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
682 elif typeof(meshValue) is GPUArray[uint16, IndexBuffer]:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
683 indexBuffer = meshValue.buffer.vk
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
684 indexBufferOffset = meshValue.offset
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
685 indexType = VK_INDEX_TYPE_UINT16
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
686 elementCount = meshValue.data.len.uint32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
687 elif typeof(meshValue) is GPUArray[uint32, IndexBuffer]:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
688 indexBuffer = meshValue.buffer.vk
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
689 indexBufferOffset = meshValue.offset
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
690 indexType = VK_INDEX_TYPE_UINT32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
691 elementCount = meshValue.data.len.uint32
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
692
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
693 assert elementCount > 0
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
694
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
695 if indexType != VK_INDEX_TYPE_NONE_KHR:
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
696 vkCmdBindIndexBuffer(
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
697 commandBuffer,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
698 indexBuffer,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
699 indexBufferOffset,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
700 indexType,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
701 )
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
702 vkCmdDrawIndexed(
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
703 commandBuffer = commandBuffer,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
704 indexCount = elementCount,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
705 instanceCount = instanceCount,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
706 firstIndex = 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
707 vertexOffset = 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
708 firstInstance = 0
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
709 )
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
710 else:
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
711 vkCmdDraw(
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
712 commandBuffer = commandBuffer,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
713 vertexCount = elementCount,
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
714 instanceCount = instanceCount,
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
715 firstVertex = 0,
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
716 firstInstance = 0
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff changeset
717 )
1202
a8864fe6fe6e add: descriptor sets binding n stuff
sam <sam@basx.dev>
parents: 1201
diff changeset
718
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
719 proc Render*[TShader, TMesh](
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
720 commandBuffer: VkCommandBuffer,
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
721 pipeline: Pipeline[TShader],
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
722 mesh: TMesh,
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
723 ) =
1210
570ac1620fb6 fix: make uniform-block-arrays working
sam <sam@basx.dev>
parents: 1209
diff changeset
724 Render(commandBuffer, pipeline, mesh, EMPTY())
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents: 1202
diff changeset
725
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
726 proc assertValidPushConstantType(TShader, TPushConstant: typedesc) =
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
727 assert sizeof(TPushConstant) <= PUSH_CONSTANT_SIZE, "Push constant values must be <= 128 bytes"
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
728 var foundPushConstant = false
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
729 for fieldname, fieldvalue in default(TShader).fieldPairs():
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
730 when hasCustomPragma(fieldvalue, PushConstantAttribute):
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
731 assert typeof(fieldvalue) is TPushConstant, "Provided push constant has not same type as declared in shader"
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
732 assert foundPushConstant == false, "More than on push constant found in shader"
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
733 foundPushConstant = true
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
734 assert foundPushConstant == true, "No push constant found in shader"
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
735
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
736 proc RenderWithPushConstant*[TShader, TMesh, TInstance, TPushConstant](
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
737 commandBuffer: VkCommandBuffer,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
738 pipeline: Pipeline[TShader],
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
739 mesh: TMesh,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
740 instances: TInstance,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
741 pushConstant: TPushConstant,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
742 ) =
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
743 static: assertValidPushConstantType(TShader, TPushConstant)
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
744 vkCmdPushConstants(
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
745 commandBuffer = commandBuffer,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
746 layout = pipeline.layout,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
747 stageFlags = VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS),
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
748 offset = 0,
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
749 size = PUSH_CONSTANT_SIZE,
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
750 pValues = addr(pushConstant)
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
751 );
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
752 Render(commandBuffer, pipeline, mesh, instances)
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
753 proc RenderWithPushConstant*[TShader, TMesh, TPushConstant](
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
754 commandBuffer: VkCommandBuffer,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
755 pipeline: Pipeline[TShader],
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
756 mesh: TMesh,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
757 pushConstant: TPushConstant,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
758 ) =
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
759 static: assertValidPushConstantType(TShader, TPushConstant)
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
760 vkCmdPushConstants(
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
761 commandBuffer = commandBuffer,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
762 layout = pipeline.layout,
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
763 stageFlags = VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS),
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
764 offset = 0,
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
765 size = PUSH_CONSTANT_SIZE,
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
766 pValues = addr(pushConstant)
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
767 );
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
768 Render(commandBuffer, pipeline, mesh, EMPTY())
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1247
diff changeset
769
1203
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents: 1202
diff changeset
770 proc asGPUArray*[T](data: openArray[T], bufferType: static BufferType): auto =
6360c8d17ce0 did: preprations to add rendering tests
sam <sam@basx.dev>
parents: 1202
diff changeset
771 GPUArray[T, bufferType](data: @data)
1204
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
772
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
773 proc asGPUValue*[T](data: T, bufferType: static BufferType): auto =
e2901100a596 add: tests, some fixes, some helpers
sam <sam@basx.dev>
parents: 1203
diff changeset
774 GPUValue[T, bufferType](data: data)
1209
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1205
diff changeset
775
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1205
diff changeset
776 proc asDescriptorSet*[T](data: T): auto =
e177336cac3f sync to notebook in bedroom
sam <sam@basx.dev>
parents: 1205
diff changeset
777 DescriptorSet[T](data: data)