| 1 |
/* |
| 2 |
* Copyright 2024 Valve Corporation |
| 3 |
* Copyright 2024 Alyssa Rosenzweig |
| 4 |
* Copyright 2022-2023 Collabora Ltd. and Red Hat Inc. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include "hk_descriptor_set.h" |
| 8 |
#include "asahi/lib/agx_bo.h" |
| 9 |
#include "util/half_float.h" |
| 10 |
#include "vulkan/vulkan_core.h" |
| 11 |
|
| 12 |
#include "agx_helpers.h" |
| 13 |
#include "hk_buffer.h" |
| 14 |
#include "hk_buffer_view.h" |
| 15 |
#include "hk_descriptor_set_layout.h" |
| 16 |
#include "hk_device.h" |
| 17 |
#include "hk_entrypoints.h" |
| 18 |
#include "hk_image_view.h" |
| 19 |
#include "hk_physical_device.h" |
| 20 |
#include "hk_sampler.h" |
| 21 |
|
| 22 |
static inline uint32_t |
| 23 |
align_u32(uint32_t v, uint32_t a) |
| 24 |
{ |
| 25 |
assert(a != 0 && a == (a & -a)); |
| 26 |
return (v + a - 1) & ~(a - 1); |
| 27 |
} |
| 28 |
|
| 29 |
static inline void * |
| 30 |
desc_ubo_data(struct hk_descriptor_set *set, uint32_t binding, uint32_t elem, |
| 31 |
uint32_t *size_out) |
| 32 |
{ |
| 33 |
const struct hk_descriptor_set_binding_layout *binding_layout = |
| 34 |
&set->layout->binding[binding]; |
| 35 |
|
| 36 |
uint32_t offset = binding_layout->offset + elem * binding_layout->stride; |
| 37 |
assert(offset < set->size); |
| 38 |
|
| 39 |
if (size_out != NULL) |
| 40 |
*size_out = set->size - offset; |
| 41 |
|
| 42 |
return (char *)set->mapped_ptr + offset; |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
write_desc(struct hk_descriptor_set *set, uint32_t binding, uint32_t elem, |
| 47 |
const void *desc_data, size_t desc_size) |
| 48 |
{ |
| 49 |
ASSERTED uint32_t dst_size; |
| 50 |
void *dst = desc_ubo_data(set, binding, elem, &dst_size); |
| 51 |
assert(desc_size <= dst_size); |
| 52 |
memcpy(dst, desc_data, desc_size); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
write_sampled_image_view_desc(struct hk_descriptor_set *set, |
| 57 |
const VkDescriptorImageInfo *const info, |
| 58 |
uint32_t binding, uint32_t elem, |
| 59 |
VkDescriptorType descriptor_type) |
| 60 |
{ |
| 61 |
struct hk_sampled_image_descriptor desc[3] = {}; |
| 62 |
|
| 63 |
/* "Zero" initialize */ |
| 64 |
for (unsigned i = 0; i < 3; ++i) { |
| 65 |
agx_set_null_texture(&desc[i].tex); |
| 66 |
} |
| 67 |
|
| 68 |
uint8_t plane_count = 1; |
| 69 |
bool ia = (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT); |
| 70 |
|
| 71 |
if (descriptor_type != VK_DESCRIPTOR_TYPE_SAMPLER && info && |
| 72 |
info->imageView != VK_NULL_HANDLE) { |
| 73 |
VK_FROM_HANDLE(hk_image_view, view, info->imageView); |
| 74 |
|
| 75 |
plane_count = view->plane_count; |
| 76 |
for (uint8_t plane = 0; plane < plane_count; plane++) { |
| 77 |
desc[plane].tex = |
| 78 |
ia ? view->planes[plane].ia : view->planes[plane].sampled; |
| 79 |
|
| 80 |
float min_lod = MAX2(view->vk.min_lod - view->vk.base_mip_level, 0.0); |
| 81 |
|
| 82 |
desc[plane].min_lod_fp16 = _mesa_float_to_half(min_lod); |
| 83 |
desc[plane].min_lod_uint16 = min_lod; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if (descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLER || |
| 88 |
descriptor_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
| 89 |
const struct hk_descriptor_set_binding_layout *binding_layout = |
| 90 |
&set->layout->binding[binding]; |
| 91 |
|
| 92 |
struct hk_sampler *sampler; |
| 93 |
if (binding_layout->immutable_samplers) { |
| 94 |
sampler = binding_layout->immutable_samplers[elem]; |
| 95 |
} else { |
| 96 |
sampler = hk_sampler_from_handle(info->sampler); |
| 97 |
} |
| 98 |
|
| 99 |
if (sampler->has_border) |
| 100 |
assert(plane_count == 1); |
| 101 |
else |
| 102 |
plane_count = MAX2(plane_count, sampler->plane_count); |
| 103 |
|
| 104 |
for (uint8_t plane = 0; plane < plane_count; plane++) { |
| 105 |
/* We need to replicate the last sampler plane out to all image |
| 106 |
* planes due to sampler table entry limitations. See |
| 107 |
* hk_CreateSampler in hk_sampler.c for more details. |
| 108 |
*/ |
| 109 |
uint8_t sampler_plane = MIN2(plane, sampler->plane_count - 1); |
| 110 |
assert(sampler->planes[sampler_plane].hw->index < (1 << 12)); |
| 111 |
|
| 112 |
/* All bindless samplers are indexed from 28 in hardware, add here so |
| 113 |
* we don't have to care in the shader. |
| 114 |
*/ |
| 115 |
desc[plane].sampler_index = |
| 116 |
sampler->planes[sampler_plane].hw->index + 28; |
| 117 |
desc[plane].sampler = sampler->planes[sampler_plane].hw->key; |
| 118 |
desc[plane].lod_bias_fp16 = sampler->lod_bias_fp16; |
| 119 |
desc[plane].clamp_0_sampler_index_or_negative = -1; |
| 120 |
} |
| 121 |
|
| 122 |
if (sampler->has_border) { |
| 123 |
assert(sampler->plane_count == 2); |
| 124 |
desc[0].clamp_0_sampler_index_or_negative = |
| 125 |
sampler->planes[1].hw->index + 28; |
| 126 |
|
| 127 |
assert(desc[0].clamp_0_sampler_index_or_negative >= 0 && |
| 128 |
"we have a border colour"); |
| 129 |
|
| 130 |
static_assert(sizeof(desc[0].border) == sizeof(sampler->custom_border), |
| 131 |
"fixed format"); |
| 132 |
|
| 133 |
memcpy(desc[0].border, sampler->custom_border.uint32, |
| 134 |
sizeof(sampler->custom_border)); |
| 135 |
} |
| 136 |
} |
| 137 |
write_desc(set, binding, elem, desc, sizeof(desc[0]) * plane_count); |
| 138 |
} |
| 139 |
|
| 140 |
static void |
| 141 |
write_storage_image_view_desc(struct hk_descriptor_set *set, |
| 142 |
const VkDescriptorImageInfo *const info, |
| 143 |
uint32_t binding, uint32_t elem) |
| 144 |
{ |
| 145 |
struct hk_storage_image_descriptor desc = {}; |
| 146 |
|
| 147 |
if (info && info->imageView != VK_NULL_HANDLE) { |
| 148 |
VK_FROM_HANDLE(hk_image_view, view, info->imageView); |
| 149 |
|
| 150 |
/* Storage images are always single plane */ |
| 151 |
assert(view->plane_count == 1); |
| 152 |
uint8_t plane = 0; |
| 153 |
|
| 154 |
desc.tex = view->planes[plane].ro_storage; |
| 155 |
desc.pbe = view->planes[plane].storage; |
| 156 |
} else { |
| 157 |
agx_set_null_texture(&desc.tex); |
| 158 |
agx_set_null_pbe(&desc.pbe); |
| 159 |
} |
| 160 |
|
| 161 |
write_desc(set, binding, elem, &desc, sizeof(desc)); |
| 162 |
} |
| 163 |
|
| 164 |
static struct hk_buffer_address |
| 165 |
get_buffer_address(const VkDescriptorBufferInfo *const info, bool read_only) |
| 166 |
{ |
| 167 |
VK_FROM_HANDLE(hk_buffer, buffer, info->buffer); |
| 168 |
const struct hk_addr_range addr_range = |
| 169 |
hk_buffer_addr_range(buffer, info->offset, info->range, read_only); |
| 170 |
assert(addr_range.range <= UINT32_MAX); |
| 171 |
|
| 172 |
return (struct hk_buffer_address){ |
| 173 |
.base_addr = addr_range.addr, |
| 174 |
.size = addr_range.range, |
| 175 |
}; |
| 176 |
} |
| 177 |
|
| 178 |
static void |
| 179 |
write_buffer_desc(struct hk_descriptor_set *set, |
| 180 |
const VkDescriptorBufferInfo *const info, uint32_t binding, |
| 181 |
uint32_t elem, bool read_only) |
| 182 |
{ |
| 183 |
const struct hk_buffer_address desc = get_buffer_address(info, read_only); |
| 184 |
|
| 185 |
write_desc(set, binding, elem, &desc, sizeof(desc)); |
| 186 |
} |
| 187 |
|
| 188 |
static void |
| 189 |
write_dynamic_buffer_desc(struct hk_descriptor_set *set, |
| 190 |
const VkDescriptorBufferInfo *const info, |
| 191 |
uint32_t binding, uint32_t elem, bool read_only) |
| 192 |
{ |
| 193 |
const struct hk_descriptor_set_binding_layout *binding_layout = |
| 194 |
&set->layout->binding[binding]; |
| 195 |
|
| 196 |
struct hk_buffer_address *desc = |
| 197 |
&set->dynamic_buffers[binding_layout->dynamic_buffer_index + elem]; |
| 198 |
|
| 199 |
*desc = get_buffer_address(info, read_only); |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
write_buffer_view_desc(struct hk_descriptor_set *set, |
| 204 |
const VkBufferView bufferView, uint32_t binding, |
| 205 |
uint32_t elem) |
| 206 |
{ |
| 207 |
struct hk_storage_image_descriptor desc = {}; |
| 208 |
if (bufferView != VK_NULL_HANDLE) { |
| 209 |
VK_FROM_HANDLE(hk_buffer_view, view, bufferView); |
| 210 |
|
| 211 |
desc.tex = view->tex; |
| 212 |
desc.pbe = view->pbe; |
| 213 |
} else { |
| 214 |
agx_set_null_texture(&desc.tex); |
| 215 |
agx_set_null_pbe(&desc.pbe); |
| 216 |
} |
| 217 |
|
| 218 |
write_desc(set, binding, elem, &desc, sizeof(desc)); |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
write_inline_uniform_data(struct hk_descriptor_set *set, |
| 223 |
const VkWriteDescriptorSetInlineUniformBlock *info, |
| 224 |
uint32_t binding, uint32_t offset) |
| 225 |
{ |
| 226 |
assert(set->layout->binding[binding].stride == 1); |
| 227 |
write_desc(set, binding, offset, info->pData, info->dataSize); |
| 228 |
} |
| 229 |
|
| 230 |
VKAPI_ATTR void VKAPI_CALL |
| 231 |
hk_UpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount, |
| 232 |
const VkWriteDescriptorSet *pDescriptorWrites, |
| 233 |
uint32_t descriptorCopyCount, |
| 234 |
const VkCopyDescriptorSet *pDescriptorCopies) |
| 235 |
{ |
| 236 |
for (uint32_t w = 0; w < descriptorWriteCount; w++) { |
| 237 |
const VkWriteDescriptorSet *write = &pDescriptorWrites[w]; |
| 238 |
VK_FROM_HANDLE(hk_descriptor_set, set, write->dstSet); |
| 239 |
|
| 240 |
switch (write->descriptorType) { |
| 241 |
case VK_DESCRIPTOR_TYPE_SAMPLER: |
| 242 |
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 243 |
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 244 |
case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 245 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 246 |
write_sampled_image_view_desc( |
| 247 |
set, write->pImageInfo + j, write->dstBinding, |
| 248 |
write->dstArrayElement + j, write->descriptorType); |
| 249 |
} |
| 250 |
break; |
| 251 |
|
| 252 |
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 253 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 254 |
write_storage_image_view_desc(set, write->pImageInfo + j, |
| 255 |
write->dstBinding, |
| 256 |
write->dstArrayElement + j); |
| 257 |
} |
| 258 |
break; |
| 259 |
|
| 260 |
case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 261 |
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 262 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 263 |
write_buffer_view_desc(set, write->pTexelBufferView[j], |
| 264 |
write->dstBinding, |
| 265 |
write->dstArrayElement + j); |
| 266 |
} |
| 267 |
break; |
| 268 |
|
| 269 |
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 270 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 271 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 272 |
write_buffer_desc( |
| 273 |
set, write->pBufferInfo + j, write->dstBinding, |
| 274 |
write->dstArrayElement + j, |
| 275 |
write->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 276 |
} |
| 277 |
break; |
| 278 |
|
| 279 |
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 280 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 281 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 282 |
write_dynamic_buffer_desc( |
| 283 |
set, write->pBufferInfo + j, write->dstBinding, |
| 284 |
write->dstArrayElement + j, |
| 285 |
write->descriptorType == |
| 286 |
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC); |
| 287 |
} |
| 288 |
break; |
| 289 |
|
| 290 |
case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK: { |
| 291 |
const VkWriteDescriptorSetInlineUniformBlock *write_inline = |
| 292 |
vk_find_struct_const(write->pNext, |
| 293 |
WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK); |
| 294 |
assert(write_inline->dataSize == write->descriptorCount); |
| 295 |
write_inline_uniform_data(set, write_inline, write->dstBinding, |
| 296 |
write->dstArrayElement); |
| 297 |
break; |
| 298 |
} |
| 299 |
|
| 300 |
default: |
| 301 |
break; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
for (uint32_t i = 0; i < descriptorCopyCount; i++) { |
| 306 |
const VkCopyDescriptorSet *copy = &pDescriptorCopies[i]; |
| 307 |
VK_FROM_HANDLE(hk_descriptor_set, src, copy->srcSet); |
| 308 |
VK_FROM_HANDLE(hk_descriptor_set, dst, copy->dstSet); |
| 309 |
|
| 310 |
const struct hk_descriptor_set_binding_layout *src_binding_layout = |
| 311 |
&src->layout->binding[copy->srcBinding]; |
| 312 |
const struct hk_descriptor_set_binding_layout *dst_binding_layout = |
| 313 |
&dst->layout->binding[copy->dstBinding]; |
| 314 |
|
| 315 |
if (dst_binding_layout->stride > 0 && src_binding_layout->stride > 0) { |
| 316 |
for (uint32_t j = 0; j < copy->descriptorCount; j++) { |
| 317 |
ASSERTED uint32_t dst_max_size, src_max_size; |
| 318 |
void *dst_map = desc_ubo_data( |
| 319 |
dst, copy->dstBinding, copy->dstArrayElement + j, &dst_max_size); |
| 320 |
const void *src_map = desc_ubo_data( |
| 321 |
src, copy->srcBinding, copy->srcArrayElement + j, &src_max_size); |
| 322 |
const uint32_t copy_size = |
| 323 |
MIN2(dst_binding_layout->stride, src_binding_layout->stride); |
| 324 |
assert(copy_size <= dst_max_size && copy_size <= src_max_size); |
| 325 |
memcpy(dst_map, src_map, copy_size); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
if (vk_descriptor_type_is_dynamic(src_binding_layout->type)) { |
| 330 |
const uint32_t dst_dyn_start = |
| 331 |
dst_binding_layout->dynamic_buffer_index + copy->dstArrayElement; |
| 332 |
const uint32_t src_dyn_start = |
| 333 |
src_binding_layout->dynamic_buffer_index + copy->srcArrayElement; |
| 334 |
typed_memcpy(&dst->dynamic_buffers[dst_dyn_start], |
| 335 |
&src->dynamic_buffers[src_dyn_start], |
| 336 |
copy->descriptorCount); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
void |
| 342 |
hk_push_descriptor_set_update(struct hk_push_descriptor_set *push_set, |
| 343 |
struct hk_descriptor_set_layout *layout, |
| 344 |
uint32_t write_count, |
| 345 |
const VkWriteDescriptorSet *writes) |
| 346 |
{ |
| 347 |
assert(layout->non_variable_descriptor_buffer_size < sizeof(push_set->data)); |
| 348 |
struct hk_descriptor_set set = { |
| 349 |
.layout = layout, |
| 350 |
.size = sizeof(push_set->data), |
| 351 |
.mapped_ptr = push_set->data, |
| 352 |
}; |
| 353 |
|
| 354 |
for (uint32_t w = 0; w < write_count; w++) { |
| 355 |
const VkWriteDescriptorSet *write = &writes[w]; |
| 356 |
assert(write->dstSet == VK_NULL_HANDLE); |
| 357 |
|
| 358 |
switch (write->descriptorType) { |
| 359 |
case VK_DESCRIPTOR_TYPE_SAMPLER: |
| 360 |
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 361 |
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 362 |
case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 363 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 364 |
write_sampled_image_view_desc( |
| 365 |
&set, write->pImageInfo + j, write->dstBinding, |
| 366 |
write->dstArrayElement + j, write->descriptorType); |
| 367 |
} |
| 368 |
break; |
| 369 |
|
| 370 |
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 371 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 372 |
write_storage_image_view_desc(&set, write->pImageInfo + j, |
| 373 |
write->dstBinding, |
| 374 |
write->dstArrayElement + j); |
| 375 |
} |
| 376 |
break; |
| 377 |
|
| 378 |
case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 379 |
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 380 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 381 |
write_buffer_view_desc(&set, write->pTexelBufferView[j], |
| 382 |
write->dstBinding, |
| 383 |
write->dstArrayElement + j); |
| 384 |
} |
| 385 |
break; |
| 386 |
|
| 387 |
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 388 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 389 |
for (uint32_t j = 0; j < write->descriptorCount; j++) { |
| 390 |
write_buffer_desc( |
| 391 |
&set, write->pBufferInfo + j, write->dstBinding, |
| 392 |
write->dstArrayElement + j, |
| 393 |
write->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 394 |
} |
| 395 |
break; |
| 396 |
|
| 397 |
default: |
| 398 |
break; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
static void hk_descriptor_pool_free(struct hk_descriptor_pool *pool, |
| 404 |
uint64_t addr, uint64_t size); |
| 405 |
|
| 406 |
static void |
| 407 |
hk_descriptor_set_destroy(struct hk_device *dev, |
| 408 |
struct hk_descriptor_pool *pool, |
| 409 |
struct hk_descriptor_set *set) |
| 410 |
{ |
| 411 |
list_del(&set->link); |
| 412 |
if (set->size > 0) |
| 413 |
hk_descriptor_pool_free(pool, set->addr, set->size); |
| 414 |
vk_descriptor_set_layout_unref(&dev->vk, &set->layout->vk); |
| 415 |
|
| 416 |
vk_object_free(&dev->vk, NULL, set); |
| 417 |
} |
| 418 |
|
| 419 |
static void |
| 420 |
hk_destroy_descriptor_pool(struct hk_device *dev, |
| 421 |
const VkAllocationCallbacks *pAllocator, |
| 422 |
struct hk_descriptor_pool *pool) |
| 423 |
{ |
| 424 |
list_for_each_entry_safe(struct hk_descriptor_set, set, &pool->sets, link) |
| 425 |
hk_descriptor_set_destroy(dev, pool, set); |
| 426 |
|
| 427 |
util_vma_heap_finish(&pool->heap); |
| 428 |
agx_bo_unreference(&dev->dev, pool->bo); |
| 429 |
|
| 430 |
vk_object_free(&dev->vk, pAllocator, pool); |
| 431 |
} |
| 432 |
|
| 433 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 434 |
hk_CreateDescriptorPool(VkDevice _device, |
| 435 |
const VkDescriptorPoolCreateInfo *pCreateInfo, |
| 436 |
const VkAllocationCallbacks *pAllocator, |
| 437 |
VkDescriptorPool *pDescriptorPool) |
| 438 |
{ |
| 439 |
VK_FROM_HANDLE(hk_device, dev, _device); |
| 440 |
struct hk_physical_device *pdev = hk_device_physical(dev); |
| 441 |
struct hk_descriptor_pool *pool; |
| 442 |
|
| 443 |
pool = vk_object_zalloc(&dev->vk, pAllocator, sizeof(*pool), |
| 444 |
VK_OBJECT_TYPE_DESCRIPTOR_POOL); |
| 445 |
if (!pool) |
| 446 |
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 447 |
|
| 448 |
list_inithead(&pool->sets); |
| 449 |
|
| 450 |
const VkMutableDescriptorTypeCreateInfoEXT *mutable_info = |
| 451 |
vk_find_struct_const(pCreateInfo->pNext, |
| 452 |
MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_EXT); |
| 453 |
|
| 454 |
uint32_t max_align = 0; |
| 455 |
for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) { |
| 456 |
const VkMutableDescriptorTypeListEXT *type_list = NULL; |
| 457 |
if (pCreateInfo->pPoolSizes[i].type == VK_DESCRIPTOR_TYPE_MUTABLE_EXT && |
| 458 |
mutable_info && i < mutable_info->mutableDescriptorTypeListCount) |
| 459 |
type_list = &mutable_info->pMutableDescriptorTypeLists[i]; |
| 460 |
|
| 461 |
uint32_t stride, alignment; |
| 462 |
hk_descriptor_stride_align_for_type(pdev, pCreateInfo->pPoolSizes[i].type, |
| 463 |
type_list, &stride, &alignment); |
| 464 |
max_align = MAX2(max_align, alignment); |
| 465 |
} |
| 466 |
|
| 467 |
uint64_t bo_size = 0; |
| 468 |
for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) { |
| 469 |
const VkMutableDescriptorTypeListEXT *type_list = NULL; |
| 470 |
if (pCreateInfo->pPoolSizes[i].type == VK_DESCRIPTOR_TYPE_MUTABLE_EXT && |
| 471 |
mutable_info && i < mutable_info->mutableDescriptorTypeListCount) |
| 472 |
type_list = &mutable_info->pMutableDescriptorTypeLists[i]; |
| 473 |
|
| 474 |
uint32_t stride, alignment; |
| 475 |
hk_descriptor_stride_align_for_type(pdev, pCreateInfo->pPoolSizes[i].type, |
| 476 |
type_list, &stride, &alignment); |
| 477 |
bo_size += |
| 478 |
MAX2(stride, max_align) * pCreateInfo->pPoolSizes[i].descriptorCount; |
| 479 |
} |
| 480 |
|
| 481 |
/* Individual descriptor sets are aligned to the min UBO alignment to |
| 482 |
* ensure that we don't end up with unaligned data access in any shaders. |
| 483 |
* This means that each descriptor buffer allocated may burn up to 16B of |
| 484 |
* extra space to get the right alignment. (Technically, it's at most 28B |
| 485 |
* because we're always going to start at least 4B aligned but we're being |
| 486 |
* conservative here.) Allocate enough extra space that we can chop it |
| 487 |
* into maxSets pieces and align each one of them to 32B. |
| 488 |
*/ |
| 489 |
bo_size += HK_MIN_UBO_ALIGNMENT * pCreateInfo->maxSets; |
| 490 |
|
| 491 |
if (bo_size) { |
| 492 |
pool->bo = agx_bo_create(&dev->dev, bo_size, 0, 0, "Descriptor pool"); |
| 493 |
if (!pool->bo) { |
| 494 |
hk_destroy_descriptor_pool(dev, pAllocator, pool); |
| 495 |
return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 496 |
} |
| 497 |
|
| 498 |
pool->mapped_ptr = agx_bo_map(pool->bo); |
| 499 |
|
| 500 |
/* The BO may be larger thanks to GPU page alignment. We may as well |
| 501 |
* make that extra space available to the client. |
| 502 |
*/ |
| 503 |
assert(pool->bo->size >= bo_size); |
| 504 |
util_vma_heap_init(&pool->heap, pool->bo->va->addr, pool->bo->size); |
| 505 |
} else { |
| 506 |
util_vma_heap_init(&pool->heap, 0, 0); |
| 507 |
} |
| 508 |
|
| 509 |
*pDescriptorPool = hk_descriptor_pool_to_handle(pool); |
| 510 |
return VK_SUCCESS; |
| 511 |
} |
| 512 |
|
| 513 |
static VkResult |
| 514 |
hk_descriptor_pool_alloc(struct hk_descriptor_pool *pool, uint64_t size, |
| 515 |
uint64_t alignment, uint64_t *addr_out, void **map_out) |
| 516 |
{ |
| 517 |
assert(size > 0); |
| 518 |
uint64_t addr = util_vma_heap_alloc(&pool->heap, size, alignment); |
| 519 |
if (addr == 0) |
| 520 |
return VK_ERROR_OUT_OF_POOL_MEMORY; |
| 521 |
|
| 522 |
assert(addr >= pool->bo->va->addr); |
| 523 |
assert(addr + size <= pool->bo->va->addr + pool->bo->size); |
| 524 |
uint64_t offset = addr - pool->bo->va->addr; |
| 525 |
|
| 526 |
*addr_out = addr; |
| 527 |
*map_out = pool->mapped_ptr + offset; |
| 528 |
|
| 529 |
return VK_SUCCESS; |
| 530 |
} |
| 531 |
|
| 532 |
static void |
| 533 |
hk_descriptor_pool_free(struct hk_descriptor_pool *pool, uint64_t addr, |
| 534 |
uint64_t size) |
| 535 |
{ |
| 536 |
assert(size > 0); |
| 537 |
assert(addr >= pool->bo->va->addr); |
| 538 |
assert(addr + size <= pool->bo->va->addr + pool->bo->size); |
| 539 |
util_vma_heap_free(&pool->heap, addr, size); |
| 540 |
} |
| 541 |
|
| 542 |
static VkResult |
| 543 |
hk_descriptor_set_create(struct hk_device *dev, struct hk_descriptor_pool *pool, |
| 544 |
struct hk_descriptor_set_layout *layout, |
| 545 |
uint32_t variable_count, |
| 546 |
struct hk_descriptor_set **out_set) |
| 547 |
{ |
| 548 |
struct hk_descriptor_set *set; |
| 549 |
VkResult result; |
| 550 |
|
| 551 |
uint32_t mem_size = |
| 552 |
sizeof(struct hk_descriptor_set) + |
| 553 |
layout->vk.dynamic_descriptor_count * sizeof(struct hk_buffer_address); |
| 554 |
|
| 555 |
set = |
| 556 |
vk_object_zalloc(&dev->vk, NULL, mem_size, VK_OBJECT_TYPE_DESCRIPTOR_SET); |
| 557 |
if (!set) |
| 558 |
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 559 |
|
| 560 |
set->size = layout->non_variable_descriptor_buffer_size; |
| 561 |
|
| 562 |
if (layout->binding_count > 0 && |
| 563 |
(layout->binding[layout->binding_count - 1].flags & |
| 564 |
VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT)) { |
| 565 |
uint32_t stride = layout->binding[layout->binding_count - 1].stride; |
| 566 |
|
| 567 |
/* Off by one so we don't underallocate the end. Otherwise vkd3d-proton |
| 568 |
* descriptor-performance underallocates. |
| 569 |
*/ |
| 570 |
set->size += stride * (variable_count + 1); |
| 571 |
} |
| 572 |
|
| 573 |
set->size = align64(set->size, HK_MIN_UBO_ALIGNMENT); |
| 574 |
|
| 575 |
if (set->size > 0) { |
| 576 |
result = hk_descriptor_pool_alloc(pool, set->size, HK_MIN_UBO_ALIGNMENT, |
| 577 |
&set->addr, &set->mapped_ptr); |
| 578 |
if (result != VK_SUCCESS) { |
| 579 |
vk_object_free(&dev->vk, NULL, set); |
| 580 |
return result; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
vk_descriptor_set_layout_ref(&layout->vk); |
| 585 |
set->layout = layout; |
| 586 |
|
| 587 |
for (uint32_t b = 0; b < layout->binding_count; b++) { |
| 588 |
if (layout->binding[b].type != VK_DESCRIPTOR_TYPE_SAMPLER && |
| 589 |
layout->binding[b].type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) |
| 590 |
continue; |
| 591 |
|
| 592 |
if (layout->binding[b].immutable_samplers == NULL) |
| 593 |
continue; |
| 594 |
|
| 595 |
uint32_t array_size = layout->binding[b].array_size; |
| 596 |
if (layout->binding[b].flags & |
| 597 |
VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT) |
| 598 |
array_size = variable_count; |
| 599 |
|
| 600 |
for (uint32_t j = 0; j < array_size; j++) { |
| 601 |
write_sampled_image_view_desc(set, NULL, b, j, |
| 602 |
layout->binding[b].type); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
list_addtail(&set->link, &pool->sets); |
| 607 |
*out_set = set; |
| 608 |
|
| 609 |
return VK_SUCCESS; |
| 610 |
} |
| 611 |
|
| 612 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 613 |
hk_AllocateDescriptorSets(VkDevice device, |
| 614 |
const VkDescriptorSetAllocateInfo *pAllocateInfo, |
| 615 |
VkDescriptorSet *pDescriptorSets) |
| 616 |
{ |
| 617 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 618 |
VK_FROM_HANDLE(hk_descriptor_pool, pool, pAllocateInfo->descriptorPool); |
| 619 |
|
| 620 |
VkResult result = VK_SUCCESS; |
| 621 |
uint32_t i; |
| 622 |
|
| 623 |
struct hk_descriptor_set *set = NULL; |
| 624 |
|
| 625 |
const VkDescriptorSetVariableDescriptorCountAllocateInfo *var_desc_count = |
| 626 |
vk_find_struct_const( |
| 627 |
pAllocateInfo->pNext, |
| 628 |
DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO); |
| 629 |
|
| 630 |
/* allocate a set of buffers for each shader to contain descriptors */ |
| 631 |
for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) { |
| 632 |
VK_FROM_HANDLE(hk_descriptor_set_layout, layout, |
| 633 |
pAllocateInfo->pSetLayouts[i]); |
| 634 |
/* If descriptorSetCount is zero or this structure is not included in |
| 635 |
* the pNext chain, then the variable lengths are considered to be zero. |
| 636 |
*/ |
| 637 |
const uint32_t variable_count = |
| 638 |
var_desc_count && var_desc_count->descriptorSetCount > 0 |
| 639 |
? var_desc_count->pDescriptorCounts[i] |
| 640 |
: 0; |
| 641 |
|
| 642 |
result = |
| 643 |
hk_descriptor_set_create(dev, pool, layout, variable_count, &set); |
| 644 |
if (result != VK_SUCCESS) |
| 645 |
break; |
| 646 |
|
| 647 |
pDescriptorSets[i] = hk_descriptor_set_to_handle(set); |
| 648 |
} |
| 649 |
|
| 650 |
if (result != VK_SUCCESS) { |
| 651 |
hk_FreeDescriptorSets(device, pAllocateInfo->descriptorPool, i, |
| 652 |
pDescriptorSets); |
| 653 |
for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) { |
| 654 |
pDescriptorSets[i] = VK_NULL_HANDLE; |
| 655 |
} |
| 656 |
} |
| 657 |
return result; |
| 658 |
} |
| 659 |
|
| 660 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 661 |
hk_FreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, |
| 662 |
uint32_t descriptorSetCount, |
| 663 |
const VkDescriptorSet *pDescriptorSets) |
| 664 |
{ |
| 665 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 666 |
VK_FROM_HANDLE(hk_descriptor_pool, pool, descriptorPool); |
| 667 |
|
| 668 |
for (uint32_t i = 0; i < descriptorSetCount; i++) { |
| 669 |
VK_FROM_HANDLE(hk_descriptor_set, set, pDescriptorSets[i]); |
| 670 |
|
| 671 |
if (set) |
| 672 |
hk_descriptor_set_destroy(dev, pool, set); |
| 673 |
} |
| 674 |
return VK_SUCCESS; |
| 675 |
} |
| 676 |
|
| 677 |
VKAPI_ATTR void VKAPI_CALL |
| 678 |
hk_DestroyDescriptorPool(VkDevice device, VkDescriptorPool _pool, |
| 679 |
const VkAllocationCallbacks *pAllocator) |
| 680 |
{ |
| 681 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 682 |
VK_FROM_HANDLE(hk_descriptor_pool, pool, _pool); |
| 683 |
|
| 684 |
if (!_pool) |
| 685 |
return; |
| 686 |
|
| 687 |
hk_destroy_descriptor_pool(dev, pAllocator, pool); |
| 688 |
} |
| 689 |
|
| 690 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 691 |
hk_ResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, |
| 692 |
VkDescriptorPoolResetFlags flags) |
| 693 |
{ |
| 694 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 695 |
VK_FROM_HANDLE(hk_descriptor_pool, pool, descriptorPool); |
| 696 |
|
| 697 |
list_for_each_entry_safe(struct hk_descriptor_set, set, &pool->sets, link) |
| 698 |
hk_descriptor_set_destroy(dev, pool, set); |
| 699 |
|
| 700 |
return VK_SUCCESS; |
| 701 |
} |
| 702 |
|
| 703 |
static void |
| 704 |
hk_descriptor_set_write_template( |
| 705 |
struct hk_descriptor_set *set, |
| 706 |
const struct vk_descriptor_update_template *template, const void *data) |
| 707 |
{ |
| 708 |
for (uint32_t i = 0; i < template->entry_count; i++) { |
| 709 |
const struct vk_descriptor_template_entry *entry = &template->entries[i]; |
| 710 |
|
| 711 |
switch (entry->type) { |
| 712 |
case VK_DESCRIPTOR_TYPE_SAMPLER: |
| 713 |
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 714 |
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 715 |
case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 716 |
for (uint32_t j = 0; j < entry->array_count; j++) { |
| 717 |
const VkDescriptorImageInfo *info = |
| 718 |
data + entry->offset + j * entry->stride; |
| 719 |
|
| 720 |
write_sampled_image_view_desc(set, info, entry->binding, |
| 721 |
entry->array_element + j, |
| 722 |
entry->type); |
| 723 |
} |
| 724 |
break; |
| 725 |
|
| 726 |
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 727 |
for (uint32_t j = 0; j < entry->array_count; j++) { |
| 728 |
const VkDescriptorImageInfo *info = |
| 729 |
data + entry->offset + j * entry->stride; |
| 730 |
|
| 731 |
write_storage_image_view_desc(set, info, entry->binding, |
| 732 |
entry->array_element + j); |
| 733 |
} |
| 734 |
break; |
| 735 |
|
| 736 |
case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 737 |
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 738 |
for (uint32_t j = 0; j < entry->array_count; j++) { |
| 739 |
const VkBufferView *bview = |
| 740 |
data + entry->offset + j * entry->stride; |
| 741 |
|
| 742 |
write_buffer_view_desc(set, *bview, entry->binding, |
| 743 |
entry->array_element + j); |
| 744 |
} |
| 745 |
break; |
| 746 |
|
| 747 |
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 748 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 749 |
for (uint32_t j = 0; j < entry->array_count; j++) { |
| 750 |
const VkDescriptorBufferInfo *info = |
| 751 |
data + entry->offset + j * entry->stride; |
| 752 |
|
| 753 |
write_buffer_desc(set, info, entry->binding, |
| 754 |
entry->array_element + j, |
| 755 |
entry->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 756 |
} |
| 757 |
break; |
| 758 |
|
| 759 |
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 760 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 761 |
for (uint32_t j = 0; j < entry->array_count; j++) { |
| 762 |
const VkDescriptorBufferInfo *info = |
| 763 |
data + entry->offset + j * entry->stride; |
| 764 |
|
| 765 |
write_dynamic_buffer_desc( |
| 766 |
set, info, entry->binding, entry->array_element + j, |
| 767 |
entry->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC); |
| 768 |
} |
| 769 |
break; |
| 770 |
|
| 771 |
case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK: |
| 772 |
write_desc(set, entry->binding, entry->array_element, |
| 773 |
data + entry->offset, entry->array_count); |
| 774 |
break; |
| 775 |
|
| 776 |
default: |
| 777 |
break; |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
VKAPI_ATTR void VKAPI_CALL |
| 783 |
hk_UpdateDescriptorSetWithTemplate( |
| 784 |
VkDevice device, VkDescriptorSet descriptorSet, |
| 785 |
VkDescriptorUpdateTemplate descriptorUpdateTemplate, const void *pData) |
| 786 |
{ |
| 787 |
VK_FROM_HANDLE(hk_descriptor_set, set, descriptorSet); |
| 788 |
VK_FROM_HANDLE(vk_descriptor_update_template, template, |
| 789 |
descriptorUpdateTemplate); |
| 790 |
|
| 791 |
hk_descriptor_set_write_template(set, template, pData); |
| 792 |
} |
| 793 |
|
| 794 |
void |
| 795 |
hk_push_descriptor_set_update_template( |
| 796 |
struct hk_push_descriptor_set *push_set, |
| 797 |
struct hk_descriptor_set_layout *layout, |
| 798 |
const struct vk_descriptor_update_template *template, const void *data) |
| 799 |
{ |
| 800 |
struct hk_descriptor_set tmp_set = { |
| 801 |
.layout = layout, |
| 802 |
.size = sizeof(push_set->data), |
| 803 |
.mapped_ptr = push_set->data, |
| 804 |
}; |
| 805 |
hk_descriptor_set_write_template(&tmp_set, template, data); |
| 806 |
} |