Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 96 additions & 52 deletions src/asahi/vulkan/hk_descriptor_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ write_sampled_image_view_desc_g17p_single(struct hk_descriptor_set *set,
static_assert(sizeof(struct hk_sampled_image_descriptor) ==
HK_TEXTURE_DESCRIPTOR_AGX3_SIZE +
sizeof(struct hk_sampled_image_descriptor_sw),
"G17P sampled descriptor is texture then software words");
"AGX3 sampled descriptor is texture then software words");

if (set->layout->descriptor_generation != 9 || !info)
const unsigned generation = set->layout->descriptor_generation;
if ((generation != 9 && generation != 10) || !info)
return false;

if (descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLER) {
Expand All @@ -198,15 +199,14 @@ write_sampled_image_view_desc_g17p_single(struct hk_descriptor_set *set,
struct hk_sampled_image_descriptor desc = {
.sw =
{
.sampler = sampler->planes[0].hw->key,
.sampler_index = hk_sampler_descriptor_index(
set->layout, sampler->planes[0].hw->index),
.clamp_0_sampler_index_or_negative = -1,
.lod_bias_fp16 = sampler->lod_bias_fp16,
},
};
write_g17p_sampler_bias(&desc.sw, sampler, 0);
if (!hk_set_null_native_texture(9, &desc.tex))
if (!hk_set_null_native_texture(generation, &desc.tex))
return false;
write_desc(set, binding, elem, &desc, sizeof(desc));
return true;
Expand All @@ -216,7 +216,8 @@ write_sampled_image_view_desc_g17p_single(struct hk_descriptor_set *set,
return false;

VK_FROM_HANDLE(hk_image_view, view, info->imageView);
if (view->plane_count != 1 || view->g17p_uscaled_normalized)
if (view->plane_count != 1 ||
(generation == 9 && view->g17p_uscaled_normalized))
return false;

const struct hk_sampler *sampler = NULL;
Expand All @@ -236,23 +237,28 @@ write_sampled_image_view_desc_g17p_single(struct hk_descriptor_set *set,
.tex = ia ? view->planes[0].ia : view->planes[0].sampled,
.sw =
{
.sampler_index = sampler
? hk_sampler_descriptor_index(
set->layout, sampler->planes[0].hw->index)
: hk_sampler_descriptor_index(set->layout, 0),
.clamp_0_sampler_index_or_negative = -1,
.sampler_index =
sampler
? hk_sampler_descriptor_index(set->layout,
sampler->planes[0].hw->index)
: (generation == 9 ? hk_sampler_descriptor_index(set->layout, 0)
: 0),
.clamp_0_sampler_index_or_negative =
(sampler || generation == 9) ? -1 : 0,
.lod_bias_fp16 = sampler ? sampler->lod_bias_fp16 : 0,
.min_lod_fp16 = view->sampled_min_lod_fp16,
.min_lod_uint16 = view->sampled_min_lod_uint16,
.scaled_conversion = view->g17p_scaled_conversion,
.scaled_conversion =
generation == 9 ? view->g17p_scaled_conversion : 0,
.sampled_heap_index = ia ? view->planes[0].input_heap_index
: view->planes[0].sampled_heap_index,
},
};
if (sampler)
if (sampler) {
write_g17p_sampler_bias(&desc.sw, sampler, 0);
else
} else if (generation == 9 && !ia) {
desc.sw.border[0] = hk_border_swizzle_word(view);
}

write_desc(set, binding, elem, &desc, sizeof(desc));
return true;
Expand Down Expand Up @@ -1074,15 +1080,22 @@ static void hk_descriptor_pool_free(struct hk_descriptor_pool *pool,
static void
hk_descriptor_set_destroy(struct hk_device *dev,
struct hk_descriptor_pool *pool,
struct hk_descriptor_set *set)
struct hk_descriptor_set *set, bool recycle)
{
list_del(&set->link);
if (set->size > 0)
if (set->size > 0 && !pool->linear)
hk_descriptor_pool_free(pool, set->addr, set->size);
const bool can_recycle =
recycle && set->layout->vk.dynamic_descriptor_count == 0;
vk_descriptor_set_layout_unref(&dev->vk, &set->layout->vk);
free(set->storage_backings);

vk_object_free(&dev->vk, NULL, set);
if (can_recycle) {
vk_object_base_finish(&set->base);
list_add(&set->link, &pool->free_sets);
} else {
vk_object_free(&dev->vk, NULL, set);
}
}

static void
Expand All @@ -1091,7 +1104,13 @@ hk_destroy_descriptor_pool(struct hk_device *dev,
struct hk_descriptor_pool *pool)
{
list_for_each_entry_safe(struct hk_descriptor_set, set, &pool->sets, link)
hk_descriptor_set_destroy(dev, pool, set);
hk_descriptor_set_destroy(dev, pool, set, false);

list_for_each_entry_safe(struct hk_descriptor_set, set, &pool->free_sets,
link) {
list_del(&set->link);
vk_free(&dev->vk.alloc, set);
}

util_vma_heap_finish(&pool->heap);
agx_bo_unreference(&dev->dev, pool->bo);
Expand All @@ -1115,6 +1134,9 @@ hk_CreateDescriptorPool(VkDevice _device,
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);

list_inithead(&pool->sets);
list_inithead(&pool->free_sets);
pool->linear =
!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);

/* The heap must be a valid empty heap before anything can fail: the error
* paths below run hk_destroy_descriptor_pool(), which calls
Expand Down Expand Up @@ -1206,9 +1228,11 @@ hk_CreateDescriptorPool(VkDevice _device,
* make that extra space available to the client.
*/
assert(pool->bo->size >= bo_size);
/* Re-initialising is safe: the heap above was created empty, so there
* are no holes to leak here. */
util_vma_heap_init(&pool->heap, pool->bo->va->addr, pool->bo->size);
if (!pool->linear) {
/* Re-initialising is safe: the heap above was created empty, so there
* are no holes to leak here. */
util_vma_heap_init(&pool->heap, pool->bo->va->addr, pool->bo->size);
}
}

*pDescriptorPool = hk_descriptor_pool_to_handle(pool);
Expand All @@ -1220,6 +1244,18 @@ hk_descriptor_pool_alloc(struct hk_descriptor_pool *pool, uint64_t size,
uint64_t alignment, uint64_t *addr_out, void **map_out)
{
assert(size > 0);
if (pool->linear) {
if (!pool->bo)
return VK_ERROR_OUT_OF_POOL_MEMORY;
uint64_t offset = align64(pool->linear_offset, alignment);
if (offset > pool->bo->size || size > pool->bo->size - offset)
return VK_ERROR_OUT_OF_POOL_MEMORY;
pool->linear_offset = offset + size;
*addr_out = pool->bo->va->addr + offset;
*map_out = pool->mapped_ptr + offset;
return VK_SUCCESS;
}

uint64_t addr = util_vma_heap_alloc(&pool->heap, size, alignment);
if (addr == 0)
return VK_ERROR_OUT_OF_POOL_MEMORY;
Expand Down Expand Up @@ -1253,14 +1289,22 @@ hk_descriptor_set_create(struct hk_device *dev, struct hk_descriptor_pool *pool,
struct hk_descriptor_set *set;
VkResult result;

uint32_t mem_size =
sizeof(struct hk_descriptor_set) +
layout->vk.dynamic_descriptor_count * sizeof(struct hk_buffer_address);

set =
vk_object_zalloc(&dev->vk, NULL, mem_size, VK_OBJECT_TYPE_DESCRIPTOR_SET);
if (!set)
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
if (layout->vk.dynamic_descriptor_count == 0 &&
!list_is_empty(&pool->free_sets)) {
set = list_first_entry(&pool->free_sets, struct hk_descriptor_set, link);
list_del(&set->link);
memset(set, 0, sizeof(*set));
vk_object_base_init(&dev->vk, &set->base, VK_OBJECT_TYPE_DESCRIPTOR_SET);
} else {
uint32_t mem_size =
sizeof(struct hk_descriptor_set) +
layout->vk.dynamic_descriptor_count * sizeof(struct hk_buffer_address);

set = vk_object_zalloc(&dev->vk, NULL, mem_size,
VK_OBJECT_TYPE_DESCRIPTOR_SET);
if (!set)
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
}

set->size = layout->non_variable_descriptor_buffer_size;

Expand Down Expand Up @@ -1303,34 +1347,30 @@ hk_descriptor_set_create(struct hk_device *dev, struct hk_descriptor_pool *pool,
vk_descriptor_set_layout_ref(&layout->vk);
set->layout = layout;

if (layout->binding_count && !layout->vk.flags &&
if (layout->has_compute_storage_backing &&
agx_chip_native_or_m5(dev->dev.chip)) {
for (uint32_t b = 0; b < layout->binding_count; ++b) {
const struct hk_descriptor_set_binding_layout *binding = &layout->binding[b];
if (binding->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER && !binding->flags &&
binding->array_size == 1) {
set->storage_backings = calloc(layout->binding_count, sizeof(*set->storage_backings));
break;
}
}
set->storage_backings =
calloc(layout->binding_count, sizeof(*set->storage_backings));
}

for (uint32_t b = 0; b < layout->binding_count; b++) {
if (layout->binding[b].type != VK_DESCRIPTOR_TYPE_SAMPLER &&
layout->binding[b].type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
continue;
if (layout->has_immutable_samplers) {
for (uint32_t b = 0; b < layout->binding_count; b++) {
if (layout->binding[b].type != VK_DESCRIPTOR_TYPE_SAMPLER &&
layout->binding[b].type != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
continue;

if (layout->binding[b].immutable_samplers == NULL)
continue;
if (layout->binding[b].immutable_samplers == NULL)
continue;

uint32_t array_size = layout->binding[b].array_size;
if (layout->binding[b].flags &
VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT)
array_size = variable_count;
uint32_t array_size = layout->binding[b].array_size;
if (layout->binding[b].flags &
VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT)
array_size = variable_count;

for (uint32_t j = 0; j < array_size; j++) {
write_sampled_image_view_desc(set, NULL, b, j,
layout->binding[b].type);
for (uint32_t j = 0; j < array_size; j++) {
write_sampled_image_view_desc(set, NULL, b, j,
layout->binding[b].type);
}
}
}

Expand All @@ -1350,6 +1390,7 @@ hk_AllocateDescriptorSets(VkDevice device,

VkResult result = VK_SUCCESS;
uint32_t i;
const uint64_t saved_linear_offset = pool->linear_offset;

struct hk_descriptor_set *set = NULL;

Expand Down Expand Up @@ -1381,6 +1422,8 @@ hk_AllocateDescriptorSets(VkDevice device,
if (result != VK_SUCCESS) {
hk_FreeDescriptorSets(device, pAllocateInfo->descriptorPool, i,
pDescriptorSets);
if (pool->linear)
pool->linear_offset = saved_linear_offset;
for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
pDescriptorSets[i] = VK_NULL_HANDLE;
}
Expand All @@ -1400,7 +1443,7 @@ hk_FreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool,
VK_FROM_HANDLE(hk_descriptor_set, set, pDescriptorSets[i]);

if (set)
hk_descriptor_set_destroy(dev, pool, set);
hk_descriptor_set_destroy(dev, pool, set, true);
}
return VK_SUCCESS;
}
Expand All @@ -1426,7 +1469,8 @@ hk_ResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
VK_FROM_HANDLE(hk_descriptor_pool, pool, descriptorPool);

list_for_each_entry_safe(struct hk_descriptor_set, set, &pool->sets, link)
hk_descriptor_set_destroy(dev, pool, set);
hk_descriptor_set_destroy(dev, pool, set, true);
pool->linear_offset = 0;

return VK_SUCCESS;
}
Expand Down
7 changes: 7 additions & 0 deletions src/asahi/vulkan/hk_descriptor_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@ struct hk_descriptor_pool {
struct vk_object_base base;

struct list_head sets;
/* Recycled zero-dynamic-descriptor set objects, returned on pool reset. */
struct list_head free_sets;

struct agx_bo *bo;
uint8_t *mapped_ptr;
struct util_vma_heap heap;

/* Pools without VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT allocate
* monotonically from linear_offset and reset to 0 on vkResetDescriptorPool. */
bool linear;
uint64_t linear_offset;

/* The pool BO is CPU-cached: every CPU write is cleaned to the point of
* coherency as it is made, and queue submission fences those cleans. */
bool cpu_cached;
Expand Down
7 changes: 7 additions & 0 deletions src/asahi/vulkan/hk_descriptor_set_layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ hk_CreateDescriptorSetLayout(VkDevice device,

layout->binding[b].array_size = binding->descriptorCount;

if (!pCreateInfo->flags &&
binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
!layout->binding[b].flags && binding->descriptorCount == 1 &&
(binding->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT))
layout->has_compute_storage_backing = true;

if (vk_descriptor_type_is_dynamic(binding->descriptorType)) {
layout->binding[b].dynamic_buffer_index = dynamic_buffer_count;
dynamic_buffer_count += binding->descriptorCount;
Expand All @@ -401,6 +407,7 @@ hk_CreateDescriptorSetLayout(VkDevice device,
type_list, &stride, &alignment);

if (binding_has_immutable_samplers(binding)) {
layout->has_immutable_samplers = true;
layout->binding[b].immutable_samplers = samplers;
for (uint32_t i = 0; i < binding->descriptorCount; i++)
*samplers++ = hk_sampler_from_handle(binding->pImmutableSamplers[i]);
Expand Down
8 changes: 8 additions & 0 deletions src/asahi/vulkan/hk_descriptor_set_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ struct hk_descriptor_set_layout {
* UPDATE_AFTER_BIND or UPDATE_UNUSED_WHILE_PENDING. */
bool has_update_after_bind;

/* True if any single-element STORAGE_BUFFER binding is visible to compute
* shaders and needs CPU backing provenance (hk_descriptor_set::storage_backings). */
bool has_compute_storage_backing;

/* True if any binding carries immutable samplers that must be initialized on
* vkAllocateDescriptorSets(). */
bool has_immutable_samplers;

/* Size of the descriptor buffer for this descriptor set */
/* Does not contain the size needed for variable count descriptors */
uint32_t non_variable_descriptor_buffer_size;
Expand Down