Path: blob/21.2-virgl/src/amd/vulkan/radv_descriptor_set.c
7219 views
/*1* Copyright © 2016 Red Hat.2* Copyright © 2016 Bas Nieuwenhuizen3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*/23#include <assert.h>24#include <fcntl.h>25#include <stdbool.h>26#include <string.h>2728#include "util/mesa-sha1.h"29#include "radv_private.h"30#include "sid.h"31#include "vk_descriptors.h"32#include "vk_format.h"33#include "vk_util.h"3435static bool36has_equal_immutable_samplers(const VkSampler *samplers, uint32_t count)37{38if (!samplers)39return false;40for (uint32_t i = 1; i < count; ++i) {41if (memcmp(radv_sampler_from_handle(samplers[0])->state,42radv_sampler_from_handle(samplers[i])->state, 16)) {43return false;44}45}46return true;47}4849static bool50radv_mutable_descriptor_type_size_alignment(const VkMutableDescriptorTypeListVALVE *list,51uint64_t *out_size, uint64_t *out_align)52{53uint32_t max_size = 0;54uint32_t max_align = 0;5556for (uint32_t i = 0; i < list->descriptorTypeCount; i++) {57uint32_t size = 0;58uint32_t align = 0;5960switch (list->pDescriptorTypes[i]) {61case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:62case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:63case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:64case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:65size = 16;66align = 16;67break;68case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:69case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:70size = 64;71align = 32;72break;73default:74return false;75}7677max_size = MAX2(max_size, size);78max_align = MAX2(max_align, align);79}8081*out_size = max_size;82*out_align = max_align;83return true;84}8586VkResult87radv_CreateDescriptorSetLayout(VkDevice _device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo,88const VkAllocationCallbacks *pAllocator,89VkDescriptorSetLayout *pSetLayout)90{91RADV_FROM_HANDLE(radv_device, device, _device);92struct radv_descriptor_set_layout *set_layout;9394assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);95const VkDescriptorSetLayoutBindingFlagsCreateInfo *variable_flags =96vk_find_struct_const(pCreateInfo->pNext, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO);97const VkMutableDescriptorTypeCreateInfoVALVE *mutable_info =98vk_find_struct_const(pCreateInfo->pNext, MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_VALVE);99100uint32_t num_bindings = 0;101uint32_t immutable_sampler_count = 0;102uint32_t ycbcr_sampler_count = 0;103for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {104num_bindings = MAX2(num_bindings, pCreateInfo->pBindings[j].binding + 1);105if ((pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||106pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&107pCreateInfo->pBindings[j].pImmutableSamplers) {108immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;109110bool has_ycbcr_sampler = false;111for (unsigned i = 0; i < pCreateInfo->pBindings[j].descriptorCount; ++i) {112if (radv_sampler_from_handle(pCreateInfo->pBindings[j].pImmutableSamplers[i])113->ycbcr_sampler)114has_ycbcr_sampler = true;115}116117if (has_ycbcr_sampler)118ycbcr_sampler_count += pCreateInfo->pBindings[j].descriptorCount;119}120}121122uint32_t samplers_offset = offsetof(struct radv_descriptor_set_layout, binding[num_bindings]);123size_t size = samplers_offset + immutable_sampler_count * 4 * sizeof(uint32_t);124if (ycbcr_sampler_count > 0) {125/* Store block of offsets first, followed by the conversion descriptors (padded to the struct126* alignment) */127size += num_bindings * sizeof(uint32_t);128size = ALIGN(size, alignof(struct radv_sampler_ycbcr_conversion));129size += ycbcr_sampler_count * sizeof(struct radv_sampler_ycbcr_conversion);130}131132set_layout =133vk_zalloc2(&device->vk.alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);134if (!set_layout)135return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);136137vk_object_base_init(&device->vk, &set_layout->base, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT);138139set_layout->flags = pCreateInfo->flags;140set_layout->layout_size = size;141142/* We just allocate all the samplers at the end of the struct */143uint32_t *samplers = (uint32_t *)&set_layout->binding[num_bindings];144struct radv_sampler_ycbcr_conversion *ycbcr_samplers = NULL;145uint32_t *ycbcr_sampler_offsets = NULL;146147if (ycbcr_sampler_count > 0) {148ycbcr_sampler_offsets = samplers + 4 * immutable_sampler_count;149set_layout->ycbcr_sampler_offsets_offset = (char *)ycbcr_sampler_offsets - (char *)set_layout;150151uintptr_t first_ycbcr_sampler_offset =152(uintptr_t)ycbcr_sampler_offsets + sizeof(uint32_t) * num_bindings;153first_ycbcr_sampler_offset =154ALIGN(first_ycbcr_sampler_offset, alignof(struct radv_sampler_ycbcr_conversion));155ycbcr_samplers = (struct radv_sampler_ycbcr_conversion *)first_ycbcr_sampler_offset;156} else157set_layout->ycbcr_sampler_offsets_offset = 0;158159VkDescriptorSetLayoutBinding *bindings = NULL;160VkResult result =161vk_create_sorted_bindings(pCreateInfo->pBindings, pCreateInfo->bindingCount, &bindings);162if (result != VK_SUCCESS) {163vk_object_base_finish(&set_layout->base);164vk_free2(&device->vk.alloc, pAllocator, set_layout);165return vk_error(device->instance, result);166}167168set_layout->binding_count = num_bindings;169set_layout->shader_stages = 0;170set_layout->dynamic_shader_stages = 0;171set_layout->has_immutable_samplers = false;172set_layout->size = 0;173174uint32_t buffer_count = 0;175uint32_t dynamic_offset_count = 0;176177for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {178const VkDescriptorSetLayoutBinding *binding = bindings + j;179uint32_t b = binding->binding;180uint32_t alignment = 0;181unsigned binding_buffer_count = 0;182uint32_t descriptor_count = binding->descriptorCount;183bool has_ycbcr_sampler = false;184185/* main image + fmask */186uint32_t max_sampled_image_descriptors = 2;187188if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER &&189binding->pImmutableSamplers) {190for (unsigned i = 0; i < binding->descriptorCount; ++i) {191struct radv_sampler_ycbcr_conversion *conversion =192radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler;193194if (conversion) {195has_ycbcr_sampler = true;196max_sampled_image_descriptors = MAX2(max_sampled_image_descriptors,197vk_format_get_plane_count(conversion->format));198}199}200}201202switch (binding->descriptorType) {203case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:204case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:205assert(!(pCreateInfo->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));206set_layout->binding[b].dynamic_offset_count = 1;207set_layout->dynamic_shader_stages |= binding->stageFlags;208set_layout->binding[b].size = 0;209binding_buffer_count = 1;210alignment = 1;211break;212case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:213case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:214case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:215case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:216set_layout->binding[b].size = 16;217binding_buffer_count = 1;218alignment = 16;219break;220case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:221case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:222case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:223/* main descriptor + fmask descriptor */224set_layout->binding[b].size = 64;225binding_buffer_count = 1;226alignment = 32;227break;228case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:229/* main descriptor + fmask descriptor + sampler */230set_layout->binding[b].size = 96;231binding_buffer_count = 1;232alignment = 32;233break;234case VK_DESCRIPTOR_TYPE_SAMPLER:235set_layout->binding[b].size = 16;236alignment = 16;237break;238case VK_DESCRIPTOR_TYPE_MUTABLE_VALVE: {239uint64_t mutable_size = 0, mutable_align = 0;240radv_mutable_descriptor_type_size_alignment(&mutable_info->pMutableDescriptorTypeLists[j],241&mutable_size, &mutable_align);242assert(mutable_size && mutable_align);243set_layout->binding[b].size = mutable_size;244binding_buffer_count = 1;245alignment = mutable_align;246break;247}248case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:249alignment = 16;250set_layout->binding[b].size = descriptor_count;251descriptor_count = 1;252break;253case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:254set_layout->binding[b].size = 16;255alignment = 16;256break;257default:258break;259}260261set_layout->size = align(set_layout->size, alignment);262set_layout->binding[b].type = binding->descriptorType;263set_layout->binding[b].array_size = descriptor_count;264set_layout->binding[b].offset = set_layout->size;265set_layout->binding[b].buffer_offset = buffer_count;266set_layout->binding[b].dynamic_offset_offset = dynamic_offset_count;267268if (variable_flags && binding->binding < variable_flags->bindingCount &&269(variable_flags->pBindingFlags[binding->binding] &270VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {271assert(272!binding->pImmutableSamplers); /* Terribly ill defined how many samplers are valid */273assert(binding->binding == num_bindings - 1);274275set_layout->has_variable_descriptors = true;276}277278if ((binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||279binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&280binding->pImmutableSamplers) {281set_layout->binding[b].immutable_samplers_offset = samplers_offset;282set_layout->binding[b].immutable_samplers_equal =283has_equal_immutable_samplers(binding->pImmutableSamplers, binding->descriptorCount);284set_layout->has_immutable_samplers = true;285286for (uint32_t i = 0; i < binding->descriptorCount; i++)287memcpy(samplers + 4 * i,288&radv_sampler_from_handle(binding->pImmutableSamplers[i])->state, 16);289290/* Don't reserve space for the samplers if they're not accessed. */291if (set_layout->binding[b].immutable_samplers_equal) {292if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER &&293max_sampled_image_descriptors <= 2)294set_layout->binding[b].size -= 32;295else if (binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER)296set_layout->binding[b].size -= 16;297}298samplers += 4 * binding->descriptorCount;299samplers_offset += 4 * sizeof(uint32_t) * binding->descriptorCount;300301if (has_ycbcr_sampler) {302ycbcr_sampler_offsets[b] = (const char *)ycbcr_samplers - (const char *)set_layout;303for (uint32_t i = 0; i < binding->descriptorCount; i++) {304if (radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler)305ycbcr_samplers[i] =306*radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler;307else308ycbcr_samplers[i].format = VK_FORMAT_UNDEFINED;309}310ycbcr_samplers += binding->descriptorCount;311}312}313314set_layout->size += descriptor_count * set_layout->binding[b].size;315buffer_count += descriptor_count * binding_buffer_count;316dynamic_offset_count += descriptor_count * set_layout->binding[b].dynamic_offset_count;317set_layout->shader_stages |= binding->stageFlags;318}319320free(bindings);321322set_layout->buffer_count = buffer_count;323set_layout->dynamic_offset_count = dynamic_offset_count;324325*pSetLayout = radv_descriptor_set_layout_to_handle(set_layout);326327return VK_SUCCESS;328}329330void331radv_DestroyDescriptorSetLayout(VkDevice _device, VkDescriptorSetLayout _set_layout,332const VkAllocationCallbacks *pAllocator)333{334RADV_FROM_HANDLE(radv_device, device, _device);335RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, _set_layout);336337if (!set_layout)338return;339340vk_object_base_finish(&set_layout->base);341vk_free2(&device->vk.alloc, pAllocator, set_layout);342}343344void345radv_GetDescriptorSetLayoutSupport(VkDevice device,346const VkDescriptorSetLayoutCreateInfo *pCreateInfo,347VkDescriptorSetLayoutSupport *pSupport)348{349VkDescriptorSetLayoutBinding *bindings = NULL;350VkResult result =351vk_create_sorted_bindings(pCreateInfo->pBindings, pCreateInfo->bindingCount, &bindings);352if (result != VK_SUCCESS) {353pSupport->supported = false;354return;355}356357const VkDescriptorSetLayoutBindingFlagsCreateInfo *variable_flags =358vk_find_struct_const(pCreateInfo->pNext, DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO);359VkDescriptorSetVariableDescriptorCountLayoutSupport *variable_count = vk_find_struct(360(void *)pCreateInfo->pNext, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT);361const VkMutableDescriptorTypeCreateInfoVALVE *mutable_info =362vk_find_struct_const(pCreateInfo->pNext, MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_VALVE);363if (variable_count) {364variable_count->maxVariableDescriptorCount = 0;365}366367bool supported = true;368uint64_t size = 0;369for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {370const VkDescriptorSetLayoutBinding *binding = bindings + i;371372uint64_t descriptor_size = 0;373uint64_t descriptor_alignment = 1;374uint32_t descriptor_count = binding->descriptorCount;375switch (binding->descriptorType) {376case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:377case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:378break;379case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:380case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:381case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:382case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:383descriptor_size = 16;384descriptor_alignment = 16;385break;386case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:387case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:388case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:389descriptor_size = 64;390descriptor_alignment = 32;391break;392case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:393if (!has_equal_immutable_samplers(binding->pImmutableSamplers, descriptor_count)) {394descriptor_size = 64;395} else {396descriptor_size = 96;397}398descriptor_alignment = 32;399break;400case VK_DESCRIPTOR_TYPE_SAMPLER:401if (!has_equal_immutable_samplers(binding->pImmutableSamplers, descriptor_count)) {402descriptor_size = 16;403descriptor_alignment = 16;404}405break;406case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:407descriptor_alignment = 16;408descriptor_size = descriptor_count;409descriptor_count = 1;410break;411case VK_DESCRIPTOR_TYPE_MUTABLE_VALVE:412if (!radv_mutable_descriptor_type_size_alignment(413&mutable_info->pMutableDescriptorTypeLists[i], &descriptor_size,414&descriptor_alignment)) {415supported = false;416}417break;418case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:419descriptor_size = 16;420descriptor_alignment = 16;421break;422default:423break;424}425426if (size && !align_u64(size, descriptor_alignment)) {427supported = false;428}429size = align_u64(size, descriptor_alignment);430431uint64_t max_count = INT32_MAX;432if (binding->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)433max_count = INT32_MAX - size;434else if (descriptor_size)435max_count = (INT32_MAX - size) / descriptor_size;436437if (max_count < descriptor_count) {438supported = false;439}440if (variable_flags && binding->binding < variable_flags->bindingCount && variable_count &&441(variable_flags->pBindingFlags[binding->binding] &442VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {443variable_count->maxVariableDescriptorCount = MIN2(UINT32_MAX, max_count);444}445size += descriptor_count * descriptor_size;446}447448free(bindings);449450pSupport->supported = supported;451}452453/*454* Pipeline layouts. These have nothing to do with the pipeline. They are455* just multiple descriptor set layouts pasted together.456*/457458VkResult459radv_CreatePipelineLayout(VkDevice _device, const VkPipelineLayoutCreateInfo *pCreateInfo,460const VkAllocationCallbacks *pAllocator,461VkPipelineLayout *pPipelineLayout)462{463RADV_FROM_HANDLE(radv_device, device, _device);464struct radv_pipeline_layout *layout;465struct mesa_sha1 ctx;466467assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);468469layout = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*layout), 8,470VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);471if (layout == NULL)472return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);473474vk_object_base_init(&device->vk, &layout->base, VK_OBJECT_TYPE_PIPELINE_LAYOUT);475476layout->num_sets = pCreateInfo->setLayoutCount;477478unsigned dynamic_offset_count = 0;479uint16_t dynamic_shader_stages = 0;480481_mesa_sha1_init(&ctx);482for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {483RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, pCreateInfo->pSetLayouts[set]);484layout->set[set].layout = set_layout;485486layout->set[set].dynamic_offset_start = dynamic_offset_count;487488for (uint32_t b = 0; b < set_layout->binding_count; b++) {489dynamic_offset_count += set_layout->binding[b].array_size * set_layout->binding[b].dynamic_offset_count;490dynamic_shader_stages |= set_layout->dynamic_shader_stages;491}492493/* Hash the entire set layout except for the vk_object_base. The494* rest of the set layout is carefully constructed to not have495* pointers so a full hash instead of a per-field hash should be ok. */496_mesa_sha1_update(&ctx, (const char *)set_layout + sizeof(struct vk_object_base),497set_layout->layout_size - sizeof(struct vk_object_base));498}499500layout->dynamic_offset_count = dynamic_offset_count;501layout->dynamic_shader_stages = dynamic_shader_stages;502layout->push_constant_size = 0;503504for (unsigned i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {505const VkPushConstantRange *range = pCreateInfo->pPushConstantRanges + i;506layout->push_constant_size = MAX2(layout->push_constant_size, range->offset + range->size);507}508509layout->push_constant_size = align(layout->push_constant_size, 16);510_mesa_sha1_update(&ctx, &layout->push_constant_size, sizeof(layout->push_constant_size));511_mesa_sha1_final(&ctx, layout->sha1);512*pPipelineLayout = radv_pipeline_layout_to_handle(layout);513514return VK_SUCCESS;515}516517void518radv_DestroyPipelineLayout(VkDevice _device, VkPipelineLayout _pipelineLayout,519const VkAllocationCallbacks *pAllocator)520{521RADV_FROM_HANDLE(radv_device, device, _device);522RADV_FROM_HANDLE(radv_pipeline_layout, pipeline_layout, _pipelineLayout);523524if (!pipeline_layout)525return;526527vk_object_base_finish(&pipeline_layout->base);528vk_free2(&device->vk.alloc, pAllocator, pipeline_layout);529}530531static VkResult532radv_descriptor_set_create(struct radv_device *device, struct radv_descriptor_pool *pool,533const struct radv_descriptor_set_layout *layout,534const uint32_t *variable_count, struct radv_descriptor_set **out_set)535{536struct radv_descriptor_set *set;537uint32_t buffer_count = layout->buffer_count;538if (variable_count) {539unsigned stride = 1;540if (layout->binding[layout->binding_count - 1].type == VK_DESCRIPTOR_TYPE_SAMPLER ||541layout->binding[layout->binding_count - 1].type ==542VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)543stride = 0;544buffer_count =545layout->binding[layout->binding_count - 1].buffer_offset + *variable_count * stride;546}547unsigned range_offset =548sizeof(struct radv_descriptor_set_header) + sizeof(struct radeon_winsys_bo *) * buffer_count;549const unsigned dynamic_offset_count = layout->dynamic_offset_count;550unsigned mem_size =551range_offset + sizeof(struct radv_descriptor_range) * dynamic_offset_count;552553if (pool->host_memory_base) {554if (pool->host_memory_end - pool->host_memory_ptr < mem_size)555return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);556557set = (struct radv_descriptor_set *)pool->host_memory_ptr;558pool->host_memory_ptr += mem_size;559memset(set->descriptors, 0, sizeof(struct radeon_winsys_bo *) * buffer_count);560} else {561set = vk_alloc2(&device->vk.alloc, NULL, mem_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);562563if (!set)564return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);565}566567memset(set, 0, mem_size);568569vk_object_base_init(&device->vk, &set->header.base, VK_OBJECT_TYPE_DESCRIPTOR_SET);570571if (dynamic_offset_count) {572set->header.dynamic_descriptors =573(struct radv_descriptor_range *)((uint8_t *)set + range_offset);574}575576set->header.layout = layout;577set->header.buffer_count = buffer_count;578uint32_t layout_size = layout->size;579if (variable_count) {580uint32_t stride = layout->binding[layout->binding_count - 1].size;581if (layout->binding[layout->binding_count - 1].type ==582VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)583stride = 1;584585layout_size = layout->binding[layout->binding_count - 1].offset + *variable_count * stride;586}587layout_size = align_u32(layout_size, 32);588set->header.size = layout_size;589590if (!pool->host_memory_base && pool->entry_count == pool->max_entry_count) {591vk_free2(&device->vk.alloc, NULL, set);592return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);593}594595/* try to allocate linearly first, so that we don't spend596* time looking for gaps if the app only allocates &597* resets via the pool. */598if (pool->current_offset + layout_size <= pool->size) {599set->header.bo = pool->bo;600set->header.mapped_ptr = (uint32_t *)(pool->mapped_ptr + pool->current_offset);601set->header.va = pool->bo ? (radv_buffer_get_va(set->header.bo) + pool->current_offset) : 0;602if (!pool->host_memory_base) {603pool->entries[pool->entry_count].offset = pool->current_offset;604pool->entries[pool->entry_count].size = layout_size;605pool->entries[pool->entry_count].set = set;606pool->entry_count++;607}608pool->current_offset += layout_size;609} else if (!pool->host_memory_base) {610uint64_t offset = 0;611int index;612613for (index = 0; index < pool->entry_count; ++index) {614if (pool->entries[index].offset - offset >= layout_size)615break;616offset = pool->entries[index].offset + pool->entries[index].size;617}618619if (pool->size - offset < layout_size) {620vk_free2(&device->vk.alloc, NULL, set);621return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);622}623set->header.bo = pool->bo;624set->header.mapped_ptr = (uint32_t *)(pool->mapped_ptr + offset);625set->header.va = pool->bo ? (radv_buffer_get_va(set->header.bo) + offset) : 0;626memmove(&pool->entries[index + 1], &pool->entries[index],627sizeof(pool->entries[0]) * (pool->entry_count - index));628pool->entries[index].offset = offset;629pool->entries[index].size = layout_size;630pool->entries[index].set = set;631pool->entry_count++;632} else633return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);634635if (layout->has_immutable_samplers) {636for (unsigned i = 0; i < layout->binding_count; ++i) {637if (!layout->binding[i].immutable_samplers_offset ||638layout->binding[i].immutable_samplers_equal)639continue;640641unsigned offset = layout->binding[i].offset / 4;642if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)643offset += radv_combined_image_descriptor_sampler_offset(layout->binding + i) / 4;644645const uint32_t *samplers =646(const uint32_t *)((const char *)layout + layout->binding[i].immutable_samplers_offset);647for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {648memcpy(set->header.mapped_ptr + offset, samplers + 4 * j, 16);649offset += layout->binding[i].size / 4;650}651}652}653*out_set = set;654return VK_SUCCESS;655}656657static void658radv_descriptor_set_destroy(struct radv_device *device, struct radv_descriptor_pool *pool,659struct radv_descriptor_set *set, bool free_bo)660{661assert(!pool->host_memory_base);662663if (free_bo && !pool->host_memory_base) {664for (int i = 0; i < pool->entry_count; ++i) {665if (pool->entries[i].set == set) {666memmove(&pool->entries[i], &pool->entries[i + 1],667sizeof(pool->entries[i]) * (pool->entry_count - i - 1));668--pool->entry_count;669break;670}671}672}673vk_object_base_finish(&set->header.base);674vk_free2(&device->vk.alloc, NULL, set);675}676677static void678radv_destroy_descriptor_pool(struct radv_device *device, const VkAllocationCallbacks *pAllocator,679struct radv_descriptor_pool *pool)680{681if (!pool->host_memory_base) {682for (int i = 0; i < pool->entry_count; ++i) {683radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);684}685}686687if (pool->bo)688device->ws->buffer_destroy(device->ws, pool->bo);689if (pool->host_bo)690vk_free2(&device->vk.alloc, pAllocator, pool->host_bo);691692vk_object_base_finish(&pool->base);693vk_free2(&device->vk.alloc, pAllocator, pool);694}695696VkResult697radv_CreateDescriptorPool(VkDevice _device, const VkDescriptorPoolCreateInfo *pCreateInfo,698const VkAllocationCallbacks *pAllocator,699VkDescriptorPool *pDescriptorPool)700{701RADV_FROM_HANDLE(radv_device, device, _device);702struct radv_descriptor_pool *pool;703uint64_t size = sizeof(struct radv_descriptor_pool);704uint64_t bo_size = 0, bo_count = 0, range_count = 0;705706const VkMutableDescriptorTypeCreateInfoVALVE *mutable_info =707vk_find_struct_const(pCreateInfo->pNext, MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_VALVE);708709vk_foreach_struct(ext, pCreateInfo->pNext)710{711switch (ext->sType) {712case VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT: {713const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT *info =714(const struct VkDescriptorPoolInlineUniformBlockCreateInfoEXT *)ext;715/* the sizes are 4 aligned, and we need to align to at716* most 32, which needs at most 28 bytes extra per717* binding. */718bo_size += 28llu * info->maxInlineUniformBlockBindings;719break;720}721default:722break;723}724}725726for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) {727if (pCreateInfo->pPoolSizes[i].type != VK_DESCRIPTOR_TYPE_SAMPLER)728bo_count += pCreateInfo->pPoolSizes[i].descriptorCount;729730switch (pCreateInfo->pPoolSizes[i].type) {731case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:732case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:733range_count += pCreateInfo->pPoolSizes[i].descriptorCount;734break;735case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:736case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:737case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:738case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:739case VK_DESCRIPTOR_TYPE_SAMPLER:740case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:741/* 32 as we may need to align for images */742bo_size += 32 * pCreateInfo->pPoolSizes[i].descriptorCount;743break;744case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:745case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:746case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:747bo_size += 64 * pCreateInfo->pPoolSizes[i].descriptorCount;748break;749case VK_DESCRIPTOR_TYPE_MUTABLE_VALVE:750/* Per spec, if a mutable descriptor type list is provided for the pool entry, we751* allocate enough memory to hold any subset of that list.752* If there is no mutable descriptor type list available,753* we must allocate enough for any supported mutable descriptor type, i.e. 64 bytes. */754if (mutable_info && i < mutable_info->mutableDescriptorTypeListCount) {755uint64_t mutable_size, mutable_alignment;756if (radv_mutable_descriptor_type_size_alignment(757&mutable_info->pMutableDescriptorTypeLists[i], &mutable_size,758&mutable_alignment)) {759/* 32 as we may need to align for images */760mutable_size = align(mutable_size, 32);761bo_size += mutable_size * pCreateInfo->pPoolSizes[i].descriptorCount;762}763} else {764bo_size += 64 * pCreateInfo->pPoolSizes[i].descriptorCount;765}766break;767case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:768bo_size += 96 * pCreateInfo->pPoolSizes[i].descriptorCount;769break;770case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:771bo_size += pCreateInfo->pPoolSizes[i].descriptorCount;772break;773default:774break;775}776}777778if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {779uint64_t host_size = pCreateInfo->maxSets * sizeof(struct radv_descriptor_set);780host_size += sizeof(struct radeon_winsys_bo *) * bo_count;781host_size += sizeof(struct radv_descriptor_range) * range_count;782size += host_size;783} else {784size += sizeof(struct radv_descriptor_pool_entry) * pCreateInfo->maxSets;785}786787pool = vk_alloc2(&device->vk.alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);788if (!pool)789return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);790791memset(pool, 0, sizeof(*pool));792793vk_object_base_init(&device->vk, &pool->base, VK_OBJECT_TYPE_DESCRIPTOR_POOL);794795if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {796pool->host_memory_base = (uint8_t *)pool + sizeof(struct radv_descriptor_pool);797pool->host_memory_ptr = pool->host_memory_base;798pool->host_memory_end = (uint8_t *)pool + size;799}800801if (bo_size) {802if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_HOST_ONLY_BIT_VALVE)) {803VkResult result = device->ws->buffer_create(804device->ws, bo_size, 32, RADEON_DOMAIN_VRAM,805RADEON_FLAG_NO_INTERPROCESS_SHARING | RADEON_FLAG_READ_ONLY | RADEON_FLAG_32BIT,806RADV_BO_PRIORITY_DESCRIPTOR, 0, &pool->bo);807if (result != VK_SUCCESS) {808radv_destroy_descriptor_pool(device, pAllocator, pool);809return vk_error(device->instance, result);810}811pool->mapped_ptr = (uint8_t *)device->ws->buffer_map(pool->bo);812if (!pool->mapped_ptr) {813radv_destroy_descriptor_pool(device, pAllocator, pool);814return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);815}816} else {817pool->host_bo =818vk_alloc2(&device->vk.alloc, pAllocator, bo_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);819if (!pool->host_bo) {820radv_destroy_descriptor_pool(device, pAllocator, pool);821return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);822}823pool->mapped_ptr = pool->host_bo;824}825}826pool->size = bo_size;827pool->max_entry_count = pCreateInfo->maxSets;828829*pDescriptorPool = radv_descriptor_pool_to_handle(pool);830return VK_SUCCESS;831}832833void834radv_DestroyDescriptorPool(VkDevice _device, VkDescriptorPool _pool,835const VkAllocationCallbacks *pAllocator)836{837RADV_FROM_HANDLE(radv_device, device, _device);838RADV_FROM_HANDLE(radv_descriptor_pool, pool, _pool);839840if (!pool)841return;842843radv_destroy_descriptor_pool(device, pAllocator, pool);844}845846VkResult847radv_ResetDescriptorPool(VkDevice _device, VkDescriptorPool descriptorPool,848VkDescriptorPoolResetFlags flags)849{850RADV_FROM_HANDLE(radv_device, device, _device);851RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);852853if (!pool->host_memory_base) {854for (int i = 0; i < pool->entry_count; ++i) {855radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);856}857pool->entry_count = 0;858}859860pool->current_offset = 0;861pool->host_memory_ptr = pool->host_memory_base;862863return VK_SUCCESS;864}865866VkResult867radv_AllocateDescriptorSets(VkDevice _device, const VkDescriptorSetAllocateInfo *pAllocateInfo,868VkDescriptorSet *pDescriptorSets)869{870RADV_FROM_HANDLE(radv_device, device, _device);871RADV_FROM_HANDLE(radv_descriptor_pool, pool, pAllocateInfo->descriptorPool);872873VkResult result = VK_SUCCESS;874uint32_t i;875struct radv_descriptor_set *set = NULL;876877const VkDescriptorSetVariableDescriptorCountAllocateInfo *variable_counts = vk_find_struct_const(878pAllocateInfo->pNext, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO);879const uint32_t zero = 0;880881/* allocate a set of buffers for each shader to contain descriptors */882for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {883RADV_FROM_HANDLE(radv_descriptor_set_layout, layout, pAllocateInfo->pSetLayouts[i]);884885const uint32_t *variable_count = NULL;886if (layout->has_variable_descriptors && variable_counts) {887if (i < variable_counts->descriptorSetCount)888variable_count = variable_counts->pDescriptorCounts + i;889else890variable_count = &zero;891}892893assert(!(layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));894895result = radv_descriptor_set_create(device, pool, layout, variable_count, &set);896if (result != VK_SUCCESS)897break;898899pDescriptorSets[i] = radv_descriptor_set_to_handle(set);900}901902if (result != VK_SUCCESS) {903radv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool, i, pDescriptorSets);904for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {905pDescriptorSets[i] = VK_NULL_HANDLE;906}907}908return result;909}910911VkResult912radv_FreeDescriptorSets(VkDevice _device, VkDescriptorPool descriptorPool, uint32_t count,913const VkDescriptorSet *pDescriptorSets)914{915RADV_FROM_HANDLE(radv_device, device, _device);916RADV_FROM_HANDLE(radv_descriptor_pool, pool, descriptorPool);917918for (uint32_t i = 0; i < count; i++) {919RADV_FROM_HANDLE(radv_descriptor_set, set, pDescriptorSets[i]);920921if (set && !pool->host_memory_base)922radv_descriptor_set_destroy(device, pool, set, true);923}924return VK_SUCCESS;925}926927static void928write_texel_buffer_descriptor(struct radv_device *device, struct radv_cmd_buffer *cmd_buffer,929unsigned *dst, struct radeon_winsys_bo **buffer_list,930const VkBufferView _buffer_view)931{932RADV_FROM_HANDLE(radv_buffer_view, buffer_view, _buffer_view);933934if (!buffer_view) {935memset(dst, 0, 4 * 4);936if (!cmd_buffer)937*buffer_list = NULL;938return;939}940941memcpy(dst, buffer_view->state, 4 * 4);942943if (cmd_buffer)944radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer_view->bo);945else946*buffer_list = buffer_view->bo;947}948949static void950write_buffer_descriptor(struct radv_device *device, struct radv_cmd_buffer *cmd_buffer,951unsigned *dst, struct radeon_winsys_bo **buffer_list,952const VkDescriptorBufferInfo *buffer_info)953{954RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);955956if (!buffer) {957memset(dst, 0, 4 * 4);958if (!cmd_buffer)959*buffer_list = NULL;960return;961}962963uint64_t va = radv_buffer_get_va(buffer->bo);964uint32_t range = buffer_info->range;965966if (buffer_info->range == VK_WHOLE_SIZE)967range = buffer->size - buffer_info->offset;968969/* robustBufferAccess is relaxed enough to allow this (in combination970* with the alignment/size we return from vkGetBufferMemoryRequirements)971* and this allows the shader compiler to create more efficient 8/16-bit972* buffer accesses. */973range = align(range, 4);974975va += buffer_info->offset + buffer->offset;976977uint32_t rsrc_word3 =978S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) | S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |979S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) | S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);980981if (device->physical_device->rad_info.chip_class >= GFX10) {982rsrc_word3 |= S_008F0C_FORMAT(V_008F0C_GFX10_FORMAT_32_FLOAT) |983S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) | S_008F0C_RESOURCE_LEVEL(1);984} else {985rsrc_word3 |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |986S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);987}988989dst[0] = va;990dst[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);991dst[2] = range;992dst[3] = rsrc_word3;993994if (cmd_buffer)995radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer->bo);996else997*buffer_list = buffer->bo;998}9991000static void1001write_block_descriptor(struct radv_device *device, struct radv_cmd_buffer *cmd_buffer, void *dst,1002const VkWriteDescriptorSet *writeset)1003{1004const VkWriteDescriptorSetInlineUniformBlockEXT *inline_ub =1005vk_find_struct_const(writeset->pNext, WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);10061007memcpy(dst, inline_ub->pData, inline_ub->dataSize);1008}10091010static void1011write_dynamic_buffer_descriptor(struct radv_device *device, struct radv_descriptor_range *range,1012struct radeon_winsys_bo **buffer_list,1013const VkDescriptorBufferInfo *buffer_info)1014{1015RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);1016uint64_t va;1017unsigned size;10181019if (!buffer) {1020range->va = 0;1021*buffer_list = NULL;1022return;1023}10241025va = radv_buffer_get_va(buffer->bo);1026size = buffer_info->range;10271028if (buffer_info->range == VK_WHOLE_SIZE)1029size = buffer->size - buffer_info->offset;10301031/* robustBufferAccess is relaxed enough to allow this (in combination1032* with the alignment/size we return from vkGetBufferMemoryRequirements)1033* and this allows the shader compiler to create more efficient 8/16-bit1034* buffer accesses. */1035size = align(size, 4);10361037va += buffer_info->offset + buffer->offset;1038range->va = va;1039range->size = size;10401041*buffer_list = buffer->bo;1042}10431044static void1045write_image_descriptor(struct radv_device *device, struct radv_cmd_buffer *cmd_buffer,1046unsigned size, unsigned *dst, struct radeon_winsys_bo **buffer_list,1047VkDescriptorType descriptor_type, const VkDescriptorImageInfo *image_info)1048{1049RADV_FROM_HANDLE(radv_image_view, iview, image_info->imageView);1050union radv_descriptor *descriptor;10511052if (!iview) {1053memset(dst, 0, size);1054if (!cmd_buffer)1055*buffer_list = NULL;1056return;1057}10581059if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {1060descriptor = &iview->storage_descriptor;1061} else {1062descriptor = &iview->descriptor;1063}10641065memcpy(dst, descriptor, size);10661067if (cmd_buffer)1068radv_cs_add_buffer(device->ws, cmd_buffer->cs, iview->image->bo);1069else1070*buffer_list = iview->image->bo;1071}10721073static void1074write_combined_image_sampler_descriptor(struct radv_device *device,1075struct radv_cmd_buffer *cmd_buffer, unsigned sampler_offset,1076unsigned *dst, struct radeon_winsys_bo **buffer_list,1077VkDescriptorType descriptor_type,1078const VkDescriptorImageInfo *image_info, bool has_sampler)1079{1080RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);10811082write_image_descriptor(device, cmd_buffer, sampler_offset, dst, buffer_list, descriptor_type,1083image_info);1084/* copy over sampler state */1085if (has_sampler) {1086memcpy(dst + sampler_offset / sizeof(*dst), sampler->state, 16);1087}1088}10891090static void1091write_sampler_descriptor(struct radv_device *device, unsigned *dst,1092const VkDescriptorImageInfo *image_info)1093{1094RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);10951096memcpy(dst, sampler->state, 16);1097}10981099static void1100write_accel_struct(void *ptr, VkAccelerationStructureKHR _accel_struct)1101{1102RADV_FROM_HANDLE(radv_acceleration_structure, accel_struct, _accel_struct);1103uint64_t va = radv_accel_struct_get_va(accel_struct);1104memcpy(ptr, &va, sizeof(va));1105}11061107void1108radv_update_descriptor_sets(struct radv_device *device, struct radv_cmd_buffer *cmd_buffer,1109VkDescriptorSet dstSetOverride, uint32_t descriptorWriteCount,1110const VkWriteDescriptorSet *pDescriptorWrites,1111uint32_t descriptorCopyCount,1112const VkCopyDescriptorSet *pDescriptorCopies)1113{1114uint32_t i, j;1115for (i = 0; i < descriptorWriteCount; i++) {1116const VkWriteDescriptorSet *writeset = &pDescriptorWrites[i];1117RADV_FROM_HANDLE(radv_descriptor_set, set,1118dstSetOverride ? dstSetOverride : writeset->dstSet);1119const struct radv_descriptor_set_binding_layout *binding_layout =1120set->header.layout->binding + writeset->dstBinding;1121uint32_t *ptr = set->header.mapped_ptr;1122struct radeon_winsys_bo **buffer_list = set->descriptors;1123/* Immutable samplers are not copied into push descriptors when they are1124* allocated, so if we are writing push descriptors we have to copy the1125* immutable samplers into them now.1126*/1127const bool copy_immutable_samplers = cmd_buffer &&1128binding_layout->immutable_samplers_offset &&1129!binding_layout->immutable_samplers_equal;1130const uint32_t *samplers = radv_immutable_samplers(set->header.layout, binding_layout);1131const VkWriteDescriptorSetAccelerationStructureKHR *accel_structs = NULL;11321133ptr += binding_layout->offset / 4;11341135if (writeset->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {1136write_block_descriptor(device, cmd_buffer, (uint8_t *)ptr + writeset->dstArrayElement,1137writeset);1138continue;1139} else if (writeset->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) {1140accel_structs =1141vk_find_struct_const(writeset->pNext, WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR);1142}11431144ptr += binding_layout->size * writeset->dstArrayElement / 4;1145buffer_list += binding_layout->buffer_offset;1146buffer_list += writeset->dstArrayElement;1147for (j = 0; j < writeset->descriptorCount; ++j) {1148switch (writeset->descriptorType) {1149case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:1150case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {1151unsigned idx = writeset->dstArrayElement + j;1152idx += binding_layout->dynamic_offset_offset;1153assert(!(set->header.layout->flags &1154VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));1155write_dynamic_buffer_descriptor(device, set->header.dynamic_descriptors + idx,1156buffer_list, writeset->pBufferInfo + j);1157break;1158}1159case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:1160case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:1161write_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,1162writeset->pBufferInfo + j);1163break;1164case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:1165case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:1166write_texel_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,1167writeset->pTexelBufferView[j]);1168break;1169case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:1170case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:1171case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:1172write_image_descriptor(device, cmd_buffer, 64, ptr, buffer_list,1173writeset->descriptorType, writeset->pImageInfo + j);1174break;1175case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {1176unsigned sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout);1177write_combined_image_sampler_descriptor(1178device, cmd_buffer, sampler_offset, ptr, buffer_list, writeset->descriptorType,1179writeset->pImageInfo + j, !binding_layout->immutable_samplers_offset);1180if (copy_immutable_samplers) {1181const unsigned idx = writeset->dstArrayElement + j;1182memcpy((char *)ptr + sampler_offset, samplers + 4 * idx, 16);1183}1184break;1185}1186case VK_DESCRIPTOR_TYPE_SAMPLER:1187if (!binding_layout->immutable_samplers_offset) {1188write_sampler_descriptor(device, ptr, writeset->pImageInfo + j);1189} else if (copy_immutable_samplers) {1190unsigned idx = writeset->dstArrayElement + j;1191memcpy(ptr, samplers + 4 * idx, 16);1192}1193break;1194case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:1195write_accel_struct(ptr, accel_structs->pAccelerationStructures[j]);1196break;1197default:1198break;1199}1200ptr += binding_layout->size / 4;1201++buffer_list;1202}1203}12041205for (i = 0; i < descriptorCopyCount; i++) {1206const VkCopyDescriptorSet *copyset = &pDescriptorCopies[i];1207RADV_FROM_HANDLE(radv_descriptor_set, src_set, copyset->srcSet);1208RADV_FROM_HANDLE(radv_descriptor_set, dst_set, copyset->dstSet);1209const struct radv_descriptor_set_binding_layout *src_binding_layout =1210src_set->header.layout->binding + copyset->srcBinding;1211const struct radv_descriptor_set_binding_layout *dst_binding_layout =1212dst_set->header.layout->binding + copyset->dstBinding;1213uint32_t *src_ptr = src_set->header.mapped_ptr;1214uint32_t *dst_ptr = dst_set->header.mapped_ptr;1215struct radeon_winsys_bo **src_buffer_list = src_set->descriptors;1216struct radeon_winsys_bo **dst_buffer_list = dst_set->descriptors;12171218src_ptr += src_binding_layout->offset / 4;1219dst_ptr += dst_binding_layout->offset / 4;12201221if (src_binding_layout->type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {1222src_ptr += copyset->srcArrayElement / 4;1223dst_ptr += copyset->dstArrayElement / 4;12241225memcpy(dst_ptr, src_ptr, copyset->descriptorCount);1226continue;1227}12281229src_ptr += src_binding_layout->size * copyset->srcArrayElement / 4;1230dst_ptr += dst_binding_layout->size * copyset->dstArrayElement / 4;12311232src_buffer_list += src_binding_layout->buffer_offset;1233src_buffer_list += copyset->srcArrayElement;12341235dst_buffer_list += dst_binding_layout->buffer_offset;1236dst_buffer_list += copyset->dstArrayElement;12371238/* In case of copies between mutable descriptor types1239* and non-mutable descriptor types. */1240size_t copy_size = MIN2(src_binding_layout->size, dst_binding_layout->size);12411242for (j = 0; j < copyset->descriptorCount; ++j) {1243switch (src_binding_layout->type) {1244case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:1245case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {1246unsigned src_idx = copyset->srcArrayElement + j;1247unsigned dst_idx = copyset->dstArrayElement + j;1248struct radv_descriptor_range *src_range, *dst_range;1249src_idx += src_binding_layout->dynamic_offset_offset;1250dst_idx += dst_binding_layout->dynamic_offset_offset;12511252src_range = src_set->header.dynamic_descriptors + src_idx;1253dst_range = dst_set->header.dynamic_descriptors + dst_idx;1254*dst_range = *src_range;1255break;1256}1257default:1258memcpy(dst_ptr, src_ptr, copy_size);1259}1260src_ptr += src_binding_layout->size / 4;1261dst_ptr += dst_binding_layout->size / 4;12621263if (src_binding_layout->type != VK_DESCRIPTOR_TYPE_SAMPLER &&1264src_binding_layout->type != VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) {1265/* Sampler descriptors don't have a buffer list. */1266dst_buffer_list[j] = src_buffer_list[j];1267}1268}1269}1270}12711272void1273radv_UpdateDescriptorSets(VkDevice _device, uint32_t descriptorWriteCount,1274const VkWriteDescriptorSet *pDescriptorWrites,1275uint32_t descriptorCopyCount,1276const VkCopyDescriptorSet *pDescriptorCopies)1277{1278RADV_FROM_HANDLE(radv_device, device, _device);12791280radv_update_descriptor_sets(device, NULL, VK_NULL_HANDLE, descriptorWriteCount,1281pDescriptorWrites, descriptorCopyCount, pDescriptorCopies);1282}12831284VkResult1285radv_CreateDescriptorUpdateTemplate(VkDevice _device,1286const VkDescriptorUpdateTemplateCreateInfo *pCreateInfo,1287const VkAllocationCallbacks *pAllocator,1288VkDescriptorUpdateTemplate *pDescriptorUpdateTemplate)1289{1290RADV_FROM_HANDLE(radv_device, device, _device);1291RADV_FROM_HANDLE(radv_descriptor_set_layout, set_layout, pCreateInfo->descriptorSetLayout);1292const uint32_t entry_count = pCreateInfo->descriptorUpdateEntryCount;1293const size_t size = sizeof(struct radv_descriptor_update_template) +1294sizeof(struct radv_descriptor_update_template_entry) * entry_count;1295struct radv_descriptor_update_template *templ;1296uint32_t i;12971298templ = vk_alloc2(&device->vk.alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);1299if (!templ)1300return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);13011302vk_object_base_init(&device->vk, &templ->base, VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE);13031304templ->entry_count = entry_count;13051306if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR) {1307RADV_FROM_HANDLE(radv_pipeline_layout, pipeline_layout, pCreateInfo->pipelineLayout);13081309/* descriptorSetLayout should be ignored for push descriptors1310* and instead it refers to pipelineLayout and set.1311*/1312assert(pCreateInfo->set < MAX_SETS);1313set_layout = pipeline_layout->set[pCreateInfo->set].layout;13141315templ->bind_point = pCreateInfo->pipelineBindPoint;1316}13171318for (i = 0; i < entry_count; i++) {1319const VkDescriptorUpdateTemplateEntry *entry = &pCreateInfo->pDescriptorUpdateEntries[i];1320const struct radv_descriptor_set_binding_layout *binding_layout =1321set_layout->binding + entry->dstBinding;1322const uint32_t buffer_offset = binding_layout->buffer_offset + entry->dstArrayElement;1323const uint32_t *immutable_samplers = NULL;1324uint32_t dst_offset;1325uint32_t dst_stride;13261327/* dst_offset is an offset into dynamic_descriptors when the descriptor1328is dynamic, and an offset into mapped_ptr otherwise */1329switch (entry->descriptorType) {1330case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:1331case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:1332assert(pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET);1333dst_offset = binding_layout->dynamic_offset_offset + entry->dstArrayElement;1334dst_stride = 0; /* Not used */1335break;1336default:1337switch (entry->descriptorType) {1338case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:1339case VK_DESCRIPTOR_TYPE_SAMPLER:1340/* Immutable samplers are copied into push descriptors when they are pushed */1341if (pCreateInfo->templateType ==1342VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR &&1343binding_layout->immutable_samplers_offset &&1344!binding_layout->immutable_samplers_equal) {1345immutable_samplers =1346radv_immutable_samplers(set_layout, binding_layout) + entry->dstArrayElement * 4;1347}1348break;1349default:1350break;1351}1352dst_offset = binding_layout->offset / 4;1353if (entry->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)1354dst_offset += entry->dstArrayElement / 4;1355else1356dst_offset += binding_layout->size * entry->dstArrayElement / 4;13571358dst_stride = binding_layout->size / 4;1359break;1360}13611362templ->entry[i] = (struct radv_descriptor_update_template_entry){1363.descriptor_type = entry->descriptorType,1364.descriptor_count = entry->descriptorCount,1365.src_offset = entry->offset,1366.src_stride = entry->stride,1367.dst_offset = dst_offset,1368.dst_stride = dst_stride,1369.buffer_offset = buffer_offset,1370.has_sampler = !binding_layout->immutable_samplers_offset,1371.sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout),1372.immutable_samplers = immutable_samplers};1373}13741375*pDescriptorUpdateTemplate = radv_descriptor_update_template_to_handle(templ);1376return VK_SUCCESS;1377}13781379void1380radv_DestroyDescriptorUpdateTemplate(VkDevice _device,1381VkDescriptorUpdateTemplate descriptorUpdateTemplate,1382const VkAllocationCallbacks *pAllocator)1383{1384RADV_FROM_HANDLE(radv_device, device, _device);1385RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);13861387if (!templ)1388return;13891390vk_object_base_finish(&templ->base);1391vk_free2(&device->vk.alloc, pAllocator, templ);1392}13931394void1395radv_update_descriptor_set_with_template(struct radv_device *device,1396struct radv_cmd_buffer *cmd_buffer,1397struct radv_descriptor_set *set,1398VkDescriptorUpdateTemplate descriptorUpdateTemplate,1399const void *pData)1400{1401RADV_FROM_HANDLE(radv_descriptor_update_template, templ, descriptorUpdateTemplate);1402uint32_t i;14031404for (i = 0; i < templ->entry_count; ++i) {1405struct radeon_winsys_bo **buffer_list = set->descriptors + templ->entry[i].buffer_offset;1406uint32_t *pDst = set->header.mapped_ptr + templ->entry[i].dst_offset;1407const uint8_t *pSrc = ((const uint8_t *)pData) + templ->entry[i].src_offset;1408uint32_t j;14091410if (templ->entry[i].descriptor_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {1411memcpy((uint8_t *)pDst, pSrc, templ->entry[i].descriptor_count);1412continue;1413}14141415for (j = 0; j < templ->entry[i].descriptor_count; ++j) {1416switch (templ->entry[i].descriptor_type) {1417case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:1418case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {1419const unsigned idx = templ->entry[i].dst_offset + j;1420assert(!(set->header.layout->flags &1421VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));1422write_dynamic_buffer_descriptor(device, set->header.dynamic_descriptors + idx,1423buffer_list, (struct VkDescriptorBufferInfo *)pSrc);1424break;1425}1426case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:1427case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:1428write_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,1429(struct VkDescriptorBufferInfo *)pSrc);1430break;1431case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:1432case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:1433write_texel_buffer_descriptor(device, cmd_buffer, pDst, buffer_list,1434*(VkBufferView *)pSrc);1435break;1436case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:1437case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:1438case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:1439write_image_descriptor(device, cmd_buffer, 64, pDst, buffer_list,1440templ->entry[i].descriptor_type,1441(struct VkDescriptorImageInfo *)pSrc);1442break;1443case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:1444write_combined_image_sampler_descriptor(1445device, cmd_buffer, templ->entry[i].sampler_offset, pDst, buffer_list,1446templ->entry[i].descriptor_type, (struct VkDescriptorImageInfo *)pSrc,1447templ->entry[i].has_sampler);1448if (templ->entry[i].immutable_samplers) {1449memcpy((char *)pDst + templ->entry[i].sampler_offset,1450templ->entry[i].immutable_samplers + 4 * j, 16);1451}1452break;1453case VK_DESCRIPTOR_TYPE_SAMPLER:1454if (templ->entry[i].has_sampler)1455write_sampler_descriptor(device, pDst, (struct VkDescriptorImageInfo *)pSrc);1456else if (templ->entry[i].immutable_samplers)1457memcpy(pDst, templ->entry[i].immutable_samplers + 4 * j, 16);1458break;1459case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:1460write_accel_struct(pDst, *(const VkAccelerationStructureKHR *)pSrc);1461break;1462default:1463break;1464}1465pSrc += templ->entry[i].src_stride;1466pDst += templ->entry[i].dst_stride;1467++buffer_list;1468}1469}1470}14711472void1473radv_UpdateDescriptorSetWithTemplate(VkDevice _device, VkDescriptorSet descriptorSet,1474VkDescriptorUpdateTemplate descriptorUpdateTemplate,1475const void *pData)1476{1477RADV_FROM_HANDLE(radv_device, device, _device);1478RADV_FROM_HANDLE(radv_descriptor_set, set, descriptorSet);14791480radv_update_descriptor_set_with_template(device, NULL, set, descriptorUpdateTemplate, pData);1481}14821483VkResult1484radv_CreateSamplerYcbcrConversion(VkDevice _device,1485const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,1486const VkAllocationCallbacks *pAllocator,1487VkSamplerYcbcrConversion *pYcbcrConversion)1488{1489RADV_FROM_HANDLE(radv_device, device, _device);1490struct radv_sampler_ycbcr_conversion *conversion = NULL;14911492conversion = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*conversion), 8,1493VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);14941495if (conversion == NULL)1496return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);14971498vk_object_base_init(&device->vk, &conversion->base, VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION);14991500conversion->format = pCreateInfo->format;1501conversion->ycbcr_model = pCreateInfo->ycbcrModel;1502conversion->ycbcr_range = pCreateInfo->ycbcrRange;1503conversion->components = pCreateInfo->components;1504conversion->chroma_offsets[0] = pCreateInfo->xChromaOffset;1505conversion->chroma_offsets[1] = pCreateInfo->yChromaOffset;1506conversion->chroma_filter = pCreateInfo->chromaFilter;15071508*pYcbcrConversion = radv_sampler_ycbcr_conversion_to_handle(conversion);1509return VK_SUCCESS;1510}15111512void1513radv_DestroySamplerYcbcrConversion(VkDevice _device, VkSamplerYcbcrConversion ycbcrConversion,1514const VkAllocationCallbacks *pAllocator)1515{1516RADV_FROM_HANDLE(radv_device, device, _device);1517RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion, ycbcr_conversion, ycbcrConversion);15181519if (!ycbcr_conversion)1520return;15211522vk_object_base_finish(&ycbcr_conversion->base);1523vk_free2(&device->vk.alloc, pAllocator, ycbcr_conversion);1524}152515261527