"Fossies" - the Fresh Open Source Software Archive

Member "mesa-20.1.8/src/freedreno/vulkan/tu_descriptor_set.c" (16 Sep 2020, 47446 Bytes) of package /linux/misc/mesa-20.1.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "tu_descriptor_set.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 20.1.5_vs_20.2.0-rc1.

    1 /*
    2  * Copyright © 2016 Red Hat.
    3  * Copyright © 2016 Bas Nieuwenhuizen
    4  *
    5  * Permission is hereby granted, free of charge, to any person obtaining a
    6  * copy of this software and associated documentation files (the "Software"),
    7  * to deal in the Software without restriction, including without limitation
    8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    9  * and/or sell copies of the Software, and to permit persons to whom the
   10  * Software is furnished to do so, subject to the following conditions:
   11  *
   12  * The above copyright notice and this permission notice (including the next
   13  * paragraph) shall be included in all copies or substantial portions of the
   14  * Software.
   15  *
   16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   22  * DEALINGS IN THE SOFTWARE.
   23  */
   24 
   25 /**
   26  * @file
   27  *
   28  * We use the bindless descriptor model, which maps fairly closely to how
   29  * Vulkan descriptor sets work. The two exceptions are input attachments and
   30  * dynamic descriptors, which have to be patched when recording command
   31  * buffers. We reserve an extra descriptor set for these. This descriptor set
   32  * contains all the input attachments in the pipeline, in order, and then all
   33  * the dynamic descriptors. The dynamic descriptors are stored in the CPU-side
   34  * datastructure for each tu_descriptor_set, and then combined into one big
   35  * descriptor set at CmdBindDescriptors time/draw time.
   36  */
   37 
   38 #include "tu_private.h"
   39 
   40 #include <assert.h>
   41 #include <fcntl.h>
   42 #include <stdbool.h>
   43 #include <string.h>
   44 #include <unistd.h>
   45 
   46 #include "util/mesa-sha1.h"
   47 #include "vk_util.h"
   48 
   49 static int
   50 binding_compare(const void *av, const void *bv)
   51 {
   52    const VkDescriptorSetLayoutBinding *a =
   53       (const VkDescriptorSetLayoutBinding *) av;
   54    const VkDescriptorSetLayoutBinding *b =
   55       (const VkDescriptorSetLayoutBinding *) bv;
   56 
   57    return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0;
   58 }
   59 
   60 static VkDescriptorSetLayoutBinding *
   61 create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings,
   62                        unsigned count)
   63 {
   64    VkDescriptorSetLayoutBinding *sorted_bindings =
   65       malloc(count * sizeof(VkDescriptorSetLayoutBinding));
   66    if (!sorted_bindings)
   67       return NULL;
   68 
   69    memcpy(sorted_bindings, bindings,
   70           count * sizeof(VkDescriptorSetLayoutBinding));
   71 
   72    qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding),
   73          binding_compare);
   74 
   75    return sorted_bindings;
   76 }
   77 
   78 static uint32_t
   79 descriptor_size(VkDescriptorType type)
   80 {
   81    switch (type) {
   82    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
   83    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
   84    case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
   85       /* These are remapped to the special driver-managed descriptor set,
   86        * hence they don't take up any space in the original descriptor set:
   87        */
   88       return 0;
   89    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
   90       /* We make offsets and sizes all 16 dwords, to match how the hardware
   91        * interprets indices passed to sample/load/store instructions in
   92        * multiples of 16 dwords.  This means that "normal" descriptors are all
   93        * of size 16, with padding for smaller descriptors like uniform storage
   94        * descriptors which are less than 16 dwords. However combined images
   95        * and samplers are actually two descriptors, so they have size 2.
   96        */
   97       return A6XX_TEX_CONST_DWORDS * 4 * 2;
   98    default:
   99       return A6XX_TEX_CONST_DWORDS * 4;
  100    }
  101 }
  102 
  103 VkResult
  104 tu_CreateDescriptorSetLayout(
  105    VkDevice _device,
  106    const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
  107    const VkAllocationCallbacks *pAllocator,
  108    VkDescriptorSetLayout *pSetLayout)
  109 {
  110    TU_FROM_HANDLE(tu_device, device, _device);
  111    struct tu_descriptor_set_layout *set_layout;
  112 
  113    assert(pCreateInfo->sType ==
  114           VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
  115    const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT *variable_flags =
  116       vk_find_struct_const(
  117          pCreateInfo->pNext,
  118          DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT);
  119 
  120    uint32_t max_binding = 0;
  121    uint32_t immutable_sampler_count = 0;
  122    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
  123       max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
  124       if ((pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
  125            pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&
  126            pCreateInfo->pBindings[j].pImmutableSamplers) {
  127          immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
  128       }
  129    }
  130 
  131    uint32_t samplers_offset = sizeof(struct tu_descriptor_set_layout) +
  132       (max_binding + 1) * sizeof(set_layout->binding[0]);
  133    uint32_t size = samplers_offset + immutable_sampler_count * A6XX_TEX_SAMP_DWORDS * 4;
  134 
  135    set_layout = vk_alloc2(&device->alloc, pAllocator, size, 8,
  136                           VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  137    if (!set_layout)
  138       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  139 
  140    set_layout->flags = pCreateInfo->flags;
  141 
  142    /* We just allocate all the immutable samplers at the end of the struct */
  143    struct tu_sampler *samplers = (void*) &set_layout->binding[max_binding + 1];
  144 
  145    VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(
  146       pCreateInfo->pBindings, pCreateInfo->bindingCount);
  147    if (!bindings) {
  148       vk_free2(&device->alloc, pAllocator, set_layout);
  149       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  150    }
  151 
  152    set_layout->binding_count = max_binding + 1;
  153    set_layout->shader_stages = 0;
  154    set_layout->has_immutable_samplers = false;
  155    set_layout->size = 0;
  156    set_layout->dynamic_ubo = 0;
  157 
  158    memset(set_layout->binding, 0,
  159           size - sizeof(struct tu_descriptor_set_layout));
  160 
  161    uint32_t dynamic_offset_count = 0;
  162    uint32_t input_attachment_count = 0;
  163    uint32_t buffer_count = 0;
  164 
  165    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
  166       const VkDescriptorSetLayoutBinding *binding = bindings + j;
  167       uint32_t b = binding->binding;
  168 
  169       set_layout->binding[b].type = binding->descriptorType;
  170       set_layout->binding[b].array_size = binding->descriptorCount;
  171       set_layout->binding[b].offset = set_layout->size;
  172       set_layout->binding[b].buffer_offset = buffer_count;
  173       set_layout->binding[b].dynamic_offset_offset = dynamic_offset_count;
  174       set_layout->binding[b].input_attachment_offset = input_attachment_count;
  175       set_layout->binding[b].size = descriptor_size(binding->descriptorType);
  176       set_layout->binding[b].shader_stages = binding->stageFlags;
  177 
  178       if (variable_flags && binding->binding < variable_flags->bindingCount &&
  179           (variable_flags->pBindingFlags[binding->binding] &
  180            VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {
  181          assert(!binding->pImmutableSamplers); /* Terribly ill defined  how
  182                                                   many samplers are valid */
  183          assert(binding->binding == max_binding);
  184 
  185          set_layout->has_variable_descriptors = true;
  186       }
  187 
  188       if ((binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
  189            binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&
  190           binding->pImmutableSamplers) {
  191          set_layout->binding[b].immutable_samplers_offset = samplers_offset;
  192          set_layout->has_immutable_samplers = true;
  193 
  194          for (uint32_t i = 0; i < binding->descriptorCount; i++)
  195             samplers[i] = *tu_sampler_from_handle(binding->pImmutableSamplers[i]);
  196 
  197          samplers += binding->descriptorCount;
  198          samplers_offset += sizeof(struct tu_sampler) * binding->descriptorCount;
  199       }
  200 
  201       set_layout->size +=
  202          binding->descriptorCount * set_layout->binding[b].size;
  203       if (binding->descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER &&
  204           binding->descriptorType != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
  205          buffer_count += binding->descriptorCount;
  206       if (binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC ||
  207           binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
  208          if (binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
  209             STATIC_ASSERT(MAX_DYNAMIC_BUFFERS <= 8 * sizeof(set_layout->dynamic_ubo));
  210             set_layout->dynamic_ubo |=
  211                ((1u << binding->descriptorCount) - 1) << dynamic_offset_count;
  212          }
  213 
  214          dynamic_offset_count += binding->descriptorCount;
  215       }
  216       if (binding->descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
  217          input_attachment_count += binding->descriptorCount;
  218       set_layout->shader_stages |= binding->stageFlags;
  219    }
  220 
  221    free(bindings);
  222 
  223    set_layout->dynamic_offset_count = dynamic_offset_count;
  224    set_layout->input_attachment_count = input_attachment_count;
  225    set_layout->buffer_count = buffer_count;
  226 
  227    *pSetLayout = tu_descriptor_set_layout_to_handle(set_layout);
  228 
  229    return VK_SUCCESS;
  230 }
  231 
  232 void
  233 tu_DestroyDescriptorSetLayout(VkDevice _device,
  234                               VkDescriptorSetLayout _set_layout,
  235                               const VkAllocationCallbacks *pAllocator)
  236 {
  237    TU_FROM_HANDLE(tu_device, device, _device);
  238    TU_FROM_HANDLE(tu_descriptor_set_layout, set_layout, _set_layout);
  239 
  240    if (!set_layout)
  241       return;
  242 
  243    vk_free2(&device->alloc, pAllocator, set_layout);
  244 }
  245 
  246 void
  247 tu_GetDescriptorSetLayoutSupport(
  248    VkDevice device,
  249    const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
  250    VkDescriptorSetLayoutSupport *pSupport)
  251 {
  252    VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(
  253       pCreateInfo->pBindings, pCreateInfo->bindingCount);
  254    if (!bindings) {
  255       pSupport->supported = false;
  256       return;
  257    }
  258 
  259    const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT *variable_flags =
  260       vk_find_struct_const(
  261          pCreateInfo->pNext,
  262          DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT);
  263    VkDescriptorSetVariableDescriptorCountLayoutSupportEXT *variable_count =
  264       vk_find_struct(
  265          (void *) pCreateInfo->pNext,
  266          DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_LAYOUT_SUPPORT_EXT);
  267    if (variable_count) {
  268       variable_count->maxVariableDescriptorCount = 0;
  269    }
  270 
  271    bool supported = true;
  272    uint64_t size = 0;
  273    for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++) {
  274       const VkDescriptorSetLayoutBinding *binding = bindings + i;
  275 
  276       uint64_t descriptor_sz = descriptor_size(binding->descriptorType);
  277       uint64_t descriptor_alignment = 8;
  278 
  279       if (size && !align_u64(size, descriptor_alignment)) {
  280          supported = false;
  281       }
  282       size = align_u64(size, descriptor_alignment);
  283 
  284       uint64_t max_count = UINT64_MAX;
  285       if (descriptor_sz)
  286          max_count = (UINT64_MAX - size) / descriptor_sz;
  287 
  288       if (max_count < binding->descriptorCount) {
  289          supported = false;
  290       }
  291       if (variable_flags && binding->binding < variable_flags->bindingCount &&
  292           variable_count &&
  293           (variable_flags->pBindingFlags[binding->binding] &
  294            VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT)) {
  295          variable_count->maxVariableDescriptorCount =
  296             MIN2(UINT32_MAX, max_count);
  297       }
  298       size += binding->descriptorCount * descriptor_sz;
  299    }
  300 
  301    free(bindings);
  302 
  303    pSupport->supported = supported;
  304 }
  305 
  306 /*
  307  * Pipeline layouts.  These have nothing to do with the pipeline.  They are
  308  * just multiple descriptor set layouts pasted together.
  309  */
  310 
  311 VkResult
  312 tu_CreatePipelineLayout(VkDevice _device,
  313                         const VkPipelineLayoutCreateInfo *pCreateInfo,
  314                         const VkAllocationCallbacks *pAllocator,
  315                         VkPipelineLayout *pPipelineLayout)
  316 {
  317    TU_FROM_HANDLE(tu_device, device, _device);
  318    struct tu_pipeline_layout *layout;
  319    struct mesa_sha1 ctx;
  320 
  321    assert(pCreateInfo->sType ==
  322           VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
  323 
  324    layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
  325                       VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  326    if (layout == NULL)
  327       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  328 
  329    layout->num_sets = pCreateInfo->setLayoutCount;
  330    layout->input_attachment_count = 0;
  331    layout->dynamic_offset_count = 0;
  332 
  333    unsigned dynamic_offset_count = 0, input_attachment_count = 0;
  334 
  335    _mesa_sha1_init(&ctx);
  336    for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
  337       TU_FROM_HANDLE(tu_descriptor_set_layout, set_layout,
  338                      pCreateInfo->pSetLayouts[set]);
  339       layout->set[set].layout = set_layout;
  340       layout->set[set].dynamic_offset_start = dynamic_offset_count;
  341       layout->set[set].input_attachment_start = input_attachment_count;
  342       dynamic_offset_count += set_layout->dynamic_offset_count;
  343       input_attachment_count += set_layout->input_attachment_count;
  344 
  345       for (uint32_t b = 0; b < set_layout->binding_count; b++) {
  346          if (set_layout->binding[b].immutable_samplers_offset)
  347             _mesa_sha1_update(
  348                &ctx,
  349                tu_immutable_samplers(set_layout, set_layout->binding + b),
  350                set_layout->binding[b].array_size * 4 * sizeof(uint32_t));
  351       }
  352       _mesa_sha1_update(
  353          &ctx, set_layout->binding,
  354          sizeof(set_layout->binding[0]) * set_layout->binding_count);
  355    }
  356 
  357    layout->dynamic_offset_count = dynamic_offset_count;
  358    layout->input_attachment_count = input_attachment_count;
  359    layout->push_constant_size = 0;
  360 
  361    for (unsigned i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
  362       const VkPushConstantRange *range = pCreateInfo->pPushConstantRanges + i;
  363       layout->push_constant_size =
  364          MAX2(layout->push_constant_size, range->offset + range->size);
  365    }
  366 
  367    layout->push_constant_size = align(layout->push_constant_size, 16);
  368    _mesa_sha1_update(&ctx, &layout->push_constant_size,
  369                      sizeof(layout->push_constant_size));
  370    _mesa_sha1_final(&ctx, layout->sha1);
  371    *pPipelineLayout = tu_pipeline_layout_to_handle(layout);
  372 
  373    return VK_SUCCESS;
  374 }
  375 
  376 void
  377 tu_DestroyPipelineLayout(VkDevice _device,
  378                          VkPipelineLayout _pipelineLayout,
  379                          const VkAllocationCallbacks *pAllocator)
  380 {
  381    TU_FROM_HANDLE(tu_device, device, _device);
  382    TU_FROM_HANDLE(tu_pipeline_layout, pipeline_layout, _pipelineLayout);
  383 
  384    if (!pipeline_layout)
  385       return;
  386    vk_free2(&device->alloc, pAllocator, pipeline_layout);
  387 }
  388 
  389 #define EMPTY 1
  390 
  391 static VkResult
  392 tu_descriptor_set_create(struct tu_device *device,
  393             struct tu_descriptor_pool *pool,
  394             const struct tu_descriptor_set_layout *layout,
  395             const uint32_t *variable_count,
  396             struct tu_descriptor_set **out_set)
  397 {
  398    struct tu_descriptor_set *set;
  399    uint32_t buffer_count = layout->buffer_count;
  400    if (variable_count) {
  401       unsigned stride = 1;
  402       if (layout->binding[layout->binding_count - 1].type == VK_DESCRIPTOR_TYPE_SAMPLER ||
  403           layout->binding[layout->binding_count - 1].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
  404          stride = 0;
  405       buffer_count = layout->binding[layout->binding_count - 1].buffer_offset +
  406                      *variable_count * stride;
  407    }
  408    unsigned dynamic_offset = sizeof(struct tu_descriptor_set) +
  409       sizeof(struct tu_bo *) * buffer_count;
  410    unsigned mem_size = dynamic_offset +
  411       A6XX_TEX_CONST_DWORDS * 4 * (layout->dynamic_offset_count +
  412                                    layout->input_attachment_count);;
  413 
  414    if (pool->host_memory_base) {
  415       if (pool->host_memory_end - pool->host_memory_ptr < mem_size)
  416          return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
  417 
  418       set = (struct tu_descriptor_set*)pool->host_memory_ptr;
  419       pool->host_memory_ptr += mem_size;
  420    } else {
  421       set = vk_alloc2(&device->alloc, NULL, mem_size, 8,
  422                       VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  423 
  424       if (!set)
  425          return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  426    }
  427 
  428    memset(set, 0, mem_size);
  429 
  430    if (layout->dynamic_offset_count + layout->input_attachment_count > 0) {
  431       set->dynamic_descriptors = (uint32_t *)((uint8_t*)set + dynamic_offset);
  432    }
  433 
  434    set->layout = layout;
  435    set->pool = pool;
  436    uint32_t layout_size = layout->size;
  437    if (variable_count) {
  438       assert(layout->has_variable_descriptors);
  439       uint32_t stride = layout->binding[layout->binding_count - 1].size;
  440       layout_size = layout->binding[layout->binding_count - 1].offset +
  441                     *variable_count * stride;
  442    }
  443 
  444    if (layout_size) {
  445       set->size = layout_size;
  446 
  447       if (!pool->host_memory_base && pool->entry_count == pool->max_entry_count) {
  448          vk_free2(&device->alloc, NULL, set);
  449          return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
  450       }
  451 
  452       /* try to allocate linearly first, so that we don't spend
  453        * time looking for gaps if the app only allocates &
  454        * resets via the pool. */
  455       if (pool->current_offset + layout_size <= pool->size) {
  456          set->mapped_ptr = (uint32_t*)(pool->bo.map + pool->current_offset);
  457          set->va = pool->bo.iova + pool->current_offset;
  458          if (!pool->host_memory_base) {
  459             pool->entries[pool->entry_count].offset = pool->current_offset;
  460             pool->entries[pool->entry_count].size = layout_size;
  461             pool->entries[pool->entry_count].set = set;
  462             pool->entry_count++;
  463          }
  464          pool->current_offset += layout_size;
  465       } else if (!pool->host_memory_base) {
  466          uint64_t offset = 0;
  467          int index;
  468 
  469          for (index = 0; index < pool->entry_count; ++index) {
  470             if (pool->entries[index].offset - offset >= layout_size)
  471                break;
  472             offset = pool->entries[index].offset + pool->entries[index].size;
  473          }
  474 
  475          if (pool->size - offset < layout_size) {
  476             vk_free2(&device->alloc, NULL, set);
  477             return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
  478          }
  479 
  480          set->mapped_ptr = (uint32_t*)(pool->bo.map + offset);
  481          set->va = pool->bo.iova + offset;
  482          memmove(&pool->entries[index + 1], &pool->entries[index],
  483             sizeof(pool->entries[0]) * (pool->entry_count - index));
  484          pool->entries[index].offset = offset;
  485          pool->entries[index].size = layout_size;
  486          pool->entries[index].set = set;
  487          pool->entry_count++;
  488       } else
  489          return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
  490    }
  491 
  492    if (layout->has_immutable_samplers) {
  493       for (unsigned i = 0; i < layout->binding_count; ++i) {
  494          if (!layout->binding[i].immutable_samplers_offset)
  495             continue;
  496 
  497          unsigned offset = layout->binding[i].offset / 4;
  498          if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
  499             offset += A6XX_TEX_CONST_DWORDS;
  500 
  501          const struct tu_sampler *samplers =
  502             (const struct tu_sampler *)((const char *)layout +
  503                                layout->binding[i].immutable_samplers_offset);
  504          for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {
  505             memcpy(set->mapped_ptr + offset, samplers + j,
  506                    sizeof(struct tu_sampler));
  507             offset += layout->binding[i].size / 4;
  508          }
  509       }
  510    }
  511 
  512    *out_set = set;
  513    return VK_SUCCESS;
  514 }
  515 
  516 static void
  517 tu_descriptor_set_destroy(struct tu_device *device,
  518              struct tu_descriptor_pool *pool,
  519              struct tu_descriptor_set *set,
  520              bool free_bo)
  521 {
  522    assert(!pool->host_memory_base);
  523 
  524    if (free_bo && set->size && !pool->host_memory_base) {
  525       uint32_t offset = (uint8_t*)set->mapped_ptr - (uint8_t*)pool->bo.map;
  526       for (int i = 0; i < pool->entry_count; ++i) {
  527          if (pool->entries[i].offset == offset) {
  528             memmove(&pool->entries[i], &pool->entries[i+1],
  529                sizeof(pool->entries[i]) * (pool->entry_count - i - 1));
  530             --pool->entry_count;
  531             break;
  532          }
  533       }
  534    }
  535    vk_free2(&device->alloc, NULL, set);
  536 }
  537 
  538 VkResult
  539 tu_CreateDescriptorPool(VkDevice _device,
  540                         const VkDescriptorPoolCreateInfo *pCreateInfo,
  541                         const VkAllocationCallbacks *pAllocator,
  542                         VkDescriptorPool *pDescriptorPool)
  543 {
  544    TU_FROM_HANDLE(tu_device, device, _device);
  545    struct tu_descriptor_pool *pool;
  546    uint64_t size = sizeof(struct tu_descriptor_pool);
  547    uint64_t bo_size = 0, bo_count = 0, dynamic_count = 0;
  548 
  549    for (unsigned i = 0; i < pCreateInfo->poolSizeCount; ++i) {
  550       if (pCreateInfo->pPoolSizes[i].type != VK_DESCRIPTOR_TYPE_SAMPLER)
  551          bo_count += pCreateInfo->pPoolSizes[i].descriptorCount;
  552 
  553       switch(pCreateInfo->pPoolSizes[i].type) {
  554       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
  555       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
  556       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
  557          dynamic_count += pCreateInfo->pPoolSizes[i].descriptorCount;
  558       default:
  559          break;
  560       }
  561 
  562       bo_size += descriptor_size(pCreateInfo->pPoolSizes[i].type) *
  563                            pCreateInfo->pPoolSizes[i].descriptorCount;
  564    }
  565 
  566    if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
  567       uint64_t host_size = pCreateInfo->maxSets * sizeof(struct tu_descriptor_set);
  568       host_size += sizeof(struct tu_bo*) * bo_count;
  569       host_size += A6XX_TEX_CONST_DWORDS * 4 * dynamic_count;
  570       size += host_size;
  571    } else {
  572       size += sizeof(struct tu_descriptor_pool_entry) * pCreateInfo->maxSets;
  573    }
  574 
  575    pool = vk_alloc2(&device->alloc, pAllocator, size, 8,
  576                     VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  577    if (!pool)
  578       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
  579 
  580    memset(pool, 0, sizeof(*pool));
  581 
  582    if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
  583       pool->host_memory_base = (uint8_t*)pool + sizeof(struct tu_descriptor_pool);
  584       pool->host_memory_ptr = pool->host_memory_base;
  585       pool->host_memory_end = (uint8_t*)pool + size;
  586    }
  587 
  588    if (bo_size) {
  589       VkResult ret;
  590 
  591       ret = tu_bo_init_new(device, &pool->bo, bo_size);
  592       assert(ret == VK_SUCCESS);
  593 
  594       ret = tu_bo_map(device, &pool->bo);
  595       assert(ret == VK_SUCCESS);
  596    }
  597    pool->size = bo_size;
  598    pool->max_entry_count = pCreateInfo->maxSets;
  599 
  600    *pDescriptorPool = tu_descriptor_pool_to_handle(pool);
  601    return VK_SUCCESS;
  602 }
  603 
  604 void
  605 tu_DestroyDescriptorPool(VkDevice _device,
  606                          VkDescriptorPool _pool,
  607                          const VkAllocationCallbacks *pAllocator)
  608 {
  609    TU_FROM_HANDLE(tu_device, device, _device);
  610    TU_FROM_HANDLE(tu_descriptor_pool, pool, _pool);
  611 
  612    if (!pool)
  613       return;
  614 
  615    if (!pool->host_memory_base) {
  616       for(int i = 0; i < pool->entry_count; ++i) {
  617          tu_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
  618       }
  619    }
  620 
  621    if (pool->size)
  622       tu_bo_finish(device, &pool->bo);
  623    vk_free2(&device->alloc, pAllocator, pool);
  624 }
  625 
  626 VkResult
  627 tu_ResetDescriptorPool(VkDevice _device,
  628                        VkDescriptorPool descriptorPool,
  629                        VkDescriptorPoolResetFlags flags)
  630 {
  631    TU_FROM_HANDLE(tu_device, device, _device);
  632    TU_FROM_HANDLE(tu_descriptor_pool, pool, descriptorPool);
  633 
  634    if (!pool->host_memory_base) {
  635       for(int i = 0; i < pool->entry_count; ++i) {
  636          tu_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
  637       }
  638       pool->entry_count = 0;
  639    }
  640 
  641    pool->current_offset = 0;
  642    pool->host_memory_ptr = pool->host_memory_base;
  643 
  644    return VK_SUCCESS;
  645 }
  646 
  647 VkResult
  648 tu_AllocateDescriptorSets(VkDevice _device,
  649                           const VkDescriptorSetAllocateInfo *pAllocateInfo,
  650                           VkDescriptorSet *pDescriptorSets)
  651 {
  652    TU_FROM_HANDLE(tu_device, device, _device);
  653    TU_FROM_HANDLE(tu_descriptor_pool, pool, pAllocateInfo->descriptorPool);
  654 
  655    VkResult result = VK_SUCCESS;
  656    uint32_t i;
  657    struct tu_descriptor_set *set = NULL;
  658 
  659    const VkDescriptorSetVariableDescriptorCountAllocateInfoEXT *variable_counts =
  660       vk_find_struct_const(pAllocateInfo->pNext, DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT);
  661    const uint32_t zero = 0;
  662 
  663    /* allocate a set of buffers for each shader to contain descriptors */
  664    for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
  665       TU_FROM_HANDLE(tu_descriptor_set_layout, layout,
  666              pAllocateInfo->pSetLayouts[i]);
  667 
  668       const uint32_t *variable_count = NULL;
  669       if (variable_counts) {
  670          if (i < variable_counts->descriptorSetCount)
  671             variable_count = variable_counts->pDescriptorCounts + i;
  672          else
  673             variable_count = &zero;
  674       }
  675 
  676       assert(!(layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
  677 
  678       result = tu_descriptor_set_create(device, pool, layout, variable_count, &set);
  679       if (result != VK_SUCCESS)
  680          break;
  681 
  682       pDescriptorSets[i] = tu_descriptor_set_to_handle(set);
  683    }
  684 
  685    if (result != VK_SUCCESS) {
  686       tu_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
  687                i, pDescriptorSets);
  688       for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
  689          pDescriptorSets[i] = VK_NULL_HANDLE;
  690       }
  691    }
  692    return result;
  693 }
  694 
  695 VkResult
  696 tu_FreeDescriptorSets(VkDevice _device,
  697                       VkDescriptorPool descriptorPool,
  698                       uint32_t count,
  699                       const VkDescriptorSet *pDescriptorSets)
  700 {
  701    TU_FROM_HANDLE(tu_device, device, _device);
  702    TU_FROM_HANDLE(tu_descriptor_pool, pool, descriptorPool);
  703 
  704    for (uint32_t i = 0; i < count; i++) {
  705       TU_FROM_HANDLE(tu_descriptor_set, set, pDescriptorSets[i]);
  706 
  707       if (set && !pool->host_memory_base)
  708          tu_descriptor_set_destroy(device, pool, set, true);
  709    }
  710    return VK_SUCCESS;
  711 }
  712 
  713 static void write_texel_buffer_descriptor(struct tu_device *device,
  714                                           struct tu_cmd_buffer *cmd_buffer,
  715                                           unsigned *dst,
  716                                           struct tu_bo **buffer_list,
  717                                           const VkBufferView buffer_view)
  718 {
  719    TU_FROM_HANDLE(tu_buffer_view, view, buffer_view);
  720 
  721    memcpy(dst, view->descriptor, sizeof(view->descriptor));
  722 
  723    if (cmd_buffer)
  724       tu_bo_list_add(&cmd_buffer->bo_list, view->buffer->bo, MSM_SUBMIT_BO_READ);
  725    else
  726       *buffer_list = view->buffer->bo;
  727 }
  728 
  729 static uint32_t get_range(struct tu_buffer *buf, VkDeviceSize offset,
  730                           VkDeviceSize range)
  731 {
  732    if (range == VK_WHOLE_SIZE) {
  733       return buf->size - offset;
  734    } else {
  735       return range;
  736    }
  737 }
  738 
  739 static void write_buffer_descriptor(struct tu_device *device,
  740                                     struct tu_cmd_buffer *cmd_buffer,
  741                                     unsigned *dst,
  742                                     struct tu_bo **buffer_list,
  743                                     const VkDescriptorBufferInfo *buffer_info)
  744 {
  745    TU_FROM_HANDLE(tu_buffer, buffer, buffer_info->buffer);
  746 
  747    uint64_t va = tu_buffer_iova(buffer) + buffer_info->offset;
  748    uint32_t range = get_range(buffer, buffer_info->offset, buffer_info->range);
  749    range = ALIGN_POT(range, 4) / 4;
  750    dst[0] =
  751       A6XX_IBO_0_TILE_MODE(TILE6_LINEAR) | A6XX_IBO_0_FMT(FMT6_32_UINT);
  752    dst[1] = range;
  753    dst[2] =
  754       A6XX_IBO_2_UNK4 | A6XX_IBO_2_TYPE(A6XX_TEX_1D) | A6XX_IBO_2_UNK31;
  755    dst[3] = 0;
  756    dst[4] = A6XX_IBO_4_BASE_LO(va);
  757    dst[5] = A6XX_IBO_5_BASE_HI(va >> 32);
  758    for (int i = 6; i < A6XX_TEX_CONST_DWORDS; i++)
  759       dst[i] = 0;
  760 
  761    if (cmd_buffer)
  762       tu_bo_list_add(&cmd_buffer->bo_list, buffer->bo, MSM_SUBMIT_BO_READ);
  763    else
  764       *buffer_list = buffer->bo;
  765 }
  766 
  767 static void write_ubo_descriptor(struct tu_device *device,
  768                                  struct tu_cmd_buffer *cmd_buffer,
  769                                  unsigned *dst,
  770                                  struct tu_bo **buffer_list,
  771                                  const VkDescriptorBufferInfo *buffer_info)
  772 {
  773    TU_FROM_HANDLE(tu_buffer, buffer, buffer_info->buffer);
  774 
  775    uint32_t range = get_range(buffer, buffer_info->offset, buffer_info->range);
  776    /* The HW range is in vec4 units */
  777    range = ALIGN_POT(range, 16) / 16;
  778    uint64_t va = tu_buffer_iova(buffer) + buffer_info->offset;
  779    dst[0] = A6XX_UBO_0_BASE_LO(va);
  780    dst[1] = A6XX_UBO_1_BASE_HI(va >> 32) | A6XX_UBO_1_SIZE(range);
  781 
  782    if (cmd_buffer)
  783       tu_bo_list_add(&cmd_buffer->bo_list, buffer->bo, MSM_SUBMIT_BO_READ);
  784    else
  785       *buffer_list = buffer->bo;
  786 }
  787 
  788 static void
  789 write_image_descriptor(struct tu_device *device,
  790              struct tu_cmd_buffer *cmd_buffer,
  791              unsigned *dst,
  792              struct tu_bo **buffer_list,
  793              VkDescriptorType descriptor_type,
  794              const VkDescriptorImageInfo *image_info)
  795 {
  796    TU_FROM_HANDLE(tu_image_view, iview, image_info->imageView);
  797 
  798    if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
  799       memcpy(dst, iview->storage_descriptor, sizeof(iview->storage_descriptor));
  800    } else {
  801       memcpy(dst, iview->descriptor, sizeof(iview->descriptor));
  802    }
  803 
  804    if (cmd_buffer)
  805       tu_bo_list_add(&cmd_buffer->bo_list, iview->image->bo, MSM_SUBMIT_BO_READ);
  806    else
  807       *buffer_list = iview->image->bo;
  808 }
  809 
  810 static void
  811 write_combined_image_sampler_descriptor(struct tu_device *device,
  812                struct tu_cmd_buffer *cmd_buffer,
  813                unsigned sampler_offset,
  814                unsigned *dst,
  815                struct tu_bo **buffer_list,
  816                VkDescriptorType descriptor_type,
  817                const VkDescriptorImageInfo *image_info,
  818                bool has_sampler)
  819 {
  820    TU_FROM_HANDLE(tu_sampler, sampler, image_info->sampler);
  821 
  822    write_image_descriptor(device, cmd_buffer, dst, buffer_list,
  823                           descriptor_type, image_info);
  824    /* copy over sampler state */
  825    if (has_sampler) {
  826       memcpy(dst + A6XX_TEX_CONST_DWORDS, sampler, sizeof(*sampler));
  827    }
  828 }
  829 
  830 static void
  831 write_sampler_descriptor(struct tu_device *device,
  832                          unsigned *dst,
  833                          const VkDescriptorImageInfo *image_info)
  834 {
  835    TU_FROM_HANDLE(tu_sampler, sampler, image_info->sampler);
  836 
  837    memcpy(dst, sampler, sizeof(*sampler));
  838 }
  839 
  840 void
  841 tu_update_descriptor_sets(struct tu_device *device,
  842                           struct tu_cmd_buffer *cmd_buffer,
  843                           VkDescriptorSet dstSetOverride,
  844                           uint32_t descriptorWriteCount,
  845                           const VkWriteDescriptorSet *pDescriptorWrites,
  846                           uint32_t descriptorCopyCount,
  847                           const VkCopyDescriptorSet *pDescriptorCopies)
  848 {
  849    uint32_t i, j;
  850    for (i = 0; i < descriptorWriteCount; i++) {
  851       const VkWriteDescriptorSet *writeset = &pDescriptorWrites[i];
  852       TU_FROM_HANDLE(tu_descriptor_set, set,
  853                        dstSetOverride ? dstSetOverride : writeset->dstSet);
  854       const struct tu_descriptor_set_binding_layout *binding_layout =
  855          set->layout->binding + writeset->dstBinding;
  856       uint32_t *ptr = set->mapped_ptr;
  857       struct tu_bo **buffer_list = set->buffers;
  858 
  859       ptr += binding_layout->offset / 4;
  860 
  861       ptr += (binding_layout->size / 4) * writeset->dstArrayElement;
  862       buffer_list += binding_layout->buffer_offset;
  863       buffer_list += writeset->dstArrayElement;
  864       for (j = 0; j < writeset->descriptorCount; ++j) {
  865          switch(writeset->descriptorType) {
  866          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
  867             assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
  868             unsigned idx = writeset->dstArrayElement + j;
  869             idx += set->layout->input_attachment_count + binding_layout->dynamic_offset_offset;
  870             write_ubo_descriptor(device, cmd_buffer,
  871                                  set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
  872                                  buffer_list, writeset->pBufferInfo + j);
  873             break;
  874          }
  875          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
  876             write_ubo_descriptor(device, cmd_buffer, ptr, buffer_list,
  877                      writeset->pBufferInfo + j);
  878             break;
  879          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
  880             assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
  881             unsigned idx = writeset->dstArrayElement + j;
  882             idx += set->layout->input_attachment_count + binding_layout->dynamic_offset_offset;
  883             write_buffer_descriptor(device, cmd_buffer,
  884                                     set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
  885                                     buffer_list, writeset->pBufferInfo + j);
  886             break;
  887          }
  888          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
  889             write_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
  890                      writeset->pBufferInfo + j);
  891             break;
  892          case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
  893          case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
  894             write_texel_buffer_descriptor(device, cmd_buffer, ptr, buffer_list,
  895                      writeset->pTexelBufferView[j]);
  896             break;
  897          case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
  898          case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
  899             write_image_descriptor(device, cmd_buffer, ptr, buffer_list,
  900                                    writeset->descriptorType,
  901                                    writeset->pImageInfo + j);
  902             break;
  903          case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
  904             unsigned idx = writeset->dstArrayElement + j;
  905             idx += binding_layout->input_attachment_offset;
  906             write_image_descriptor(device, cmd_buffer,
  907                                     set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
  908                                     buffer_list, writeset->descriptorType,
  909                                     writeset->pImageInfo + j);
  910             break;
  911          }
  912          case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
  913             write_combined_image_sampler_descriptor(device, cmd_buffer,
  914                                                     A6XX_TEX_CONST_DWORDS * 4,
  915                                                     ptr, buffer_list,
  916                                                     writeset->descriptorType,
  917                                                     writeset->pImageInfo + j,
  918                                                     !binding_layout->immutable_samplers_offset);
  919             break;
  920          case VK_DESCRIPTOR_TYPE_SAMPLER:
  921             write_sampler_descriptor(device, ptr, writeset->pImageInfo + j);
  922             break;
  923          default:
  924             unreachable("unimplemented descriptor type");
  925             break;
  926          }
  927          ptr += binding_layout->size / 4;
  928          ++buffer_list;
  929       }
  930    }
  931 
  932    for (i = 0; i < descriptorCopyCount; i++) {
  933       const VkCopyDescriptorSet *copyset = &pDescriptorCopies[i];
  934       TU_FROM_HANDLE(tu_descriptor_set, src_set,
  935                        copyset->srcSet);
  936       TU_FROM_HANDLE(tu_descriptor_set, dst_set,
  937                        copyset->dstSet);
  938       const struct tu_descriptor_set_binding_layout *src_binding_layout =
  939          src_set->layout->binding + copyset->srcBinding;
  940       const struct tu_descriptor_set_binding_layout *dst_binding_layout =
  941          dst_set->layout->binding + copyset->dstBinding;
  942       uint32_t *src_ptr = src_set->mapped_ptr;
  943       uint32_t *dst_ptr = dst_set->mapped_ptr;
  944       struct tu_bo **src_buffer_list = src_set->buffers;
  945       struct tu_bo **dst_buffer_list = dst_set->buffers;
  946 
  947       src_ptr += src_binding_layout->offset / 4;
  948       dst_ptr += dst_binding_layout->offset / 4;
  949 
  950       src_ptr += src_binding_layout->size * copyset->srcArrayElement / 4;
  951       dst_ptr += dst_binding_layout->size * copyset->dstArrayElement / 4;
  952 
  953       src_buffer_list += src_binding_layout->buffer_offset;
  954       src_buffer_list += copyset->srcArrayElement;
  955 
  956       dst_buffer_list += dst_binding_layout->buffer_offset;
  957       dst_buffer_list += copyset->dstArrayElement;
  958 
  959       for (j = 0; j < copyset->descriptorCount; ++j) {
  960          switch (src_binding_layout->type) {
  961          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
  962          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
  963             unsigned src_idx = copyset->srcArrayElement + j;
  964             unsigned dst_idx = copyset->dstArrayElement + j;
  965             src_idx += src_set->layout->input_attachment_count;
  966             dst_idx += dst_set->layout->input_attachment_count;
  967             src_idx += src_binding_layout->dynamic_offset_offset;
  968             dst_idx += dst_binding_layout->dynamic_offset_offset;
  969 
  970             uint32_t *src_dynamic, *dst_dynamic;
  971             src_dynamic = src_set->dynamic_descriptors + src_idx * A6XX_TEX_CONST_DWORDS;
  972             dst_dynamic = dst_set->dynamic_descriptors + dst_idx * A6XX_TEX_CONST_DWORDS;
  973             memcpy(dst_dynamic, src_dynamic, A6XX_TEX_CONST_DWORDS * 4);
  974             break;
  975          }
  976          case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
  977             unsigned src_idx = copyset->srcArrayElement + j;
  978             unsigned dst_idx = copyset->dstArrayElement + j;
  979             src_idx += src_binding_layout->input_attachment_offset;
  980             dst_idx += dst_binding_layout->input_attachment_offset;
  981 
  982             uint32_t *src_dynamic, *dst_dynamic;
  983             src_dynamic = src_set->dynamic_descriptors + src_idx * A6XX_TEX_CONST_DWORDS;
  984             dst_dynamic = dst_set->dynamic_descriptors + dst_idx * A6XX_TEX_CONST_DWORDS;
  985             memcpy(dst_dynamic, src_dynamic, A6XX_TEX_CONST_DWORDS * 4);
  986             break;
  987          }
  988          default:
  989             memcpy(dst_ptr, src_ptr, src_binding_layout->size);
  990          }
  991 
  992          src_ptr += src_binding_layout->size / 4;
  993          dst_ptr += dst_binding_layout->size / 4;
  994 
  995          if (src_binding_layout->type != VK_DESCRIPTOR_TYPE_SAMPLER) {
  996             /* Sampler descriptors don't have a buffer list. */
  997             dst_buffer_list[j] = src_buffer_list[j];
  998          }
  999       }
 1000    }
 1001 }
 1002 
 1003 void
 1004 tu_UpdateDescriptorSets(VkDevice _device,
 1005                         uint32_t descriptorWriteCount,
 1006                         const VkWriteDescriptorSet *pDescriptorWrites,
 1007                         uint32_t descriptorCopyCount,
 1008                         const VkCopyDescriptorSet *pDescriptorCopies)
 1009 {
 1010    TU_FROM_HANDLE(tu_device, device, _device);
 1011 
 1012    tu_update_descriptor_sets(device, NULL, VK_NULL_HANDLE,
 1013                              descriptorWriteCount, pDescriptorWrites,
 1014                              descriptorCopyCount, pDescriptorCopies);
 1015 }
 1016 
 1017 VkResult
 1018 tu_CreateDescriptorUpdateTemplate(
 1019    VkDevice _device,
 1020    const VkDescriptorUpdateTemplateCreateInfo *pCreateInfo,
 1021    const VkAllocationCallbacks *pAllocator,
 1022    VkDescriptorUpdateTemplate *pDescriptorUpdateTemplate)
 1023 {
 1024    TU_FROM_HANDLE(tu_device, device, _device);
 1025    TU_FROM_HANDLE(tu_descriptor_set_layout, set_layout,
 1026                   pCreateInfo->descriptorSetLayout);
 1027    const uint32_t entry_count = pCreateInfo->descriptorUpdateEntryCount;
 1028    const size_t size =
 1029       sizeof(struct tu_descriptor_update_template) +
 1030       sizeof(struct tu_descriptor_update_template_entry) * entry_count;
 1031    struct tu_descriptor_update_template *templ;
 1032 
 1033    templ = vk_alloc2(&device->alloc, pAllocator, size, 8,
 1034                      VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
 1035    if (!templ)
 1036       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 1037 
 1038    templ->entry_count = entry_count;
 1039 
 1040    if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR) {
 1041       TU_FROM_HANDLE(tu_pipeline_layout, pipeline_layout, pCreateInfo->pipelineLayout);
 1042 
 1043       /* descriptorSetLayout should be ignored for push descriptors
 1044        * and instead it refers to pipelineLayout and set.
 1045        */
 1046       assert(pCreateInfo->set < MAX_SETS);
 1047       set_layout = pipeline_layout->set[pCreateInfo->set].layout;
 1048    }
 1049 
 1050    for (uint32_t i = 0; i < entry_count; i++) {
 1051       const VkDescriptorUpdateTemplateEntry *entry = &pCreateInfo->pDescriptorUpdateEntries[i];
 1052 
 1053       const struct tu_descriptor_set_binding_layout *binding_layout =
 1054          set_layout->binding + entry->dstBinding;
 1055       const uint32_t buffer_offset = binding_layout->buffer_offset +
 1056          entry->dstArrayElement;
 1057       uint32_t dst_offset, dst_stride;
 1058 
 1059       /* dst_offset is an offset into dynamic_descriptors when the descriptor 
 1060        * is dynamic, and an offset into mapped_ptr otherwise.
 1061        */
 1062       switch (entry->descriptorType) {
 1063       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
 1064       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
 1065          dst_offset = (set_layout->input_attachment_count +
 1066             binding_layout->dynamic_offset_offset +
 1067             entry->dstArrayElement) * A6XX_TEX_CONST_DWORDS;
 1068          dst_stride = A6XX_TEX_CONST_DWORDS;
 1069          break;
 1070       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
 1071          dst_offset = (binding_layout->input_attachment_offset +
 1072             entry->dstArrayElement) * A6XX_TEX_CONST_DWORDS;
 1073          dst_stride = A6XX_TEX_CONST_DWORDS;
 1074          break;
 1075       default:
 1076          dst_offset = binding_layout->offset / 4;
 1077          dst_offset += (binding_layout->size * entry->dstArrayElement) / 4;
 1078          dst_stride = binding_layout->size / 4;
 1079       }
 1080 
 1081       templ->entry[i] = (struct tu_descriptor_update_template_entry) {
 1082          .descriptor_type = entry->descriptorType,
 1083          .descriptor_count = entry->descriptorCount,
 1084          .src_offset = entry->offset,
 1085          .src_stride = entry->stride,
 1086          .dst_offset = dst_offset,
 1087          .dst_stride = dst_stride,
 1088          .buffer_offset = buffer_offset,
 1089          .has_sampler = !binding_layout->immutable_samplers_offset,
 1090       };
 1091    }
 1092 
 1093    *pDescriptorUpdateTemplate =
 1094       tu_descriptor_update_template_to_handle(templ);
 1095 
 1096    return VK_SUCCESS;
 1097 }
 1098 
 1099 void
 1100 tu_DestroyDescriptorUpdateTemplate(
 1101    VkDevice _device,
 1102    VkDescriptorUpdateTemplate descriptorUpdateTemplate,
 1103    const VkAllocationCallbacks *pAllocator)
 1104 {
 1105    TU_FROM_HANDLE(tu_device, device, _device);
 1106    TU_FROM_HANDLE(tu_descriptor_update_template, templ,
 1107                   descriptorUpdateTemplate);
 1108 
 1109    if (!templ)
 1110       return;
 1111 
 1112    vk_free2(&device->alloc, pAllocator, templ);
 1113 }
 1114 
 1115 void
 1116 tu_update_descriptor_set_with_template(
 1117    struct tu_device *device,
 1118    struct tu_cmd_buffer *cmd_buffer,
 1119    struct tu_descriptor_set *set,
 1120    VkDescriptorUpdateTemplate descriptorUpdateTemplate,
 1121    const void *pData)
 1122 {
 1123    TU_FROM_HANDLE(tu_descriptor_update_template, templ,
 1124                   descriptorUpdateTemplate);
 1125 
 1126    for (uint32_t i = 0; i < templ->entry_count; i++) {
 1127       uint32_t *ptr = set->mapped_ptr;
 1128       const void *src = ((const char *) pData) + templ->entry[i].src_offset;
 1129       struct tu_bo **buffer_list = set->buffers;
 1130 
 1131       ptr += templ->entry[i].dst_offset;
 1132       buffer_list += templ->entry[i].buffer_offset;
 1133       unsigned dst_offset = templ->entry[i].dst_offset;
 1134       for (unsigned j = 0; j < templ->entry[i].descriptor_count; ++j) {
 1135          switch(templ->entry[i].descriptor_type) {
 1136          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
 1137             assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
 1138             write_ubo_descriptor(device, cmd_buffer,
 1139                                  set->dynamic_descriptors + dst_offset,
 1140                                  buffer_list, src);
 1141             break;
 1142          }
 1143          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
 1144             write_ubo_descriptor(device, cmd_buffer, ptr, buffer_list, src);
 1145             break;
 1146          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
 1147             assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
 1148             write_buffer_descriptor(device, cmd_buffer,
 1149                                     set->dynamic_descriptors + dst_offset,
 1150                                     buffer_list, src);
 1151             break;
 1152          }
 1153          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
 1154             write_buffer_descriptor(device, cmd_buffer, ptr, buffer_list, src);
 1155             break;
 1156          case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
 1157          case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
 1158             write_texel_buffer_descriptor(device, cmd_buffer, ptr,
 1159                                           buffer_list, *(VkBufferView *) src);
 1160             break;
 1161          case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
 1162          case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
 1163             write_image_descriptor(device, cmd_buffer, ptr, buffer_list,
 1164                                    templ->entry[i].descriptor_type,
 1165                                    src);
 1166             break;
 1167          case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
 1168             write_image_descriptor(device, cmd_buffer,
 1169                                     set->dynamic_descriptors + dst_offset,
 1170                                     buffer_list, templ->entry[i].descriptor_type,
 1171                                     src);
 1172             break;
 1173          }
 1174          case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
 1175             write_combined_image_sampler_descriptor(device, cmd_buffer,
 1176                                                     A6XX_TEX_CONST_DWORDS * 4,
 1177                                                     ptr, buffer_list,
 1178                                                     templ->entry[i].descriptor_type,
 1179                                                     src,
 1180                                                     templ->entry[i].has_sampler);
 1181             break;
 1182          case VK_DESCRIPTOR_TYPE_SAMPLER:
 1183             write_sampler_descriptor(device, ptr, src);
 1184             break;
 1185          default:
 1186             unreachable("unimplemented descriptor type");
 1187             break;
 1188          }
 1189          src = (char *) src + templ->entry[i].src_stride;
 1190          ptr += templ->entry[i].dst_stride;
 1191          dst_offset += templ->entry[i].dst_stride;
 1192          ++buffer_list;
 1193       }
 1194    }
 1195 }
 1196 
 1197 void
 1198 tu_UpdateDescriptorSetWithTemplate(
 1199    VkDevice _device,
 1200    VkDescriptorSet descriptorSet,
 1201    VkDescriptorUpdateTemplate descriptorUpdateTemplate,
 1202    const void *pData)
 1203 {
 1204    TU_FROM_HANDLE(tu_device, device, _device);
 1205    TU_FROM_HANDLE(tu_descriptor_set, set, descriptorSet);
 1206 
 1207    tu_update_descriptor_set_with_template(device, NULL, set,
 1208                                           descriptorUpdateTemplate, pData);
 1209 }
 1210 
 1211 VkResult
 1212 tu_CreateSamplerYcbcrConversion(
 1213    VkDevice device,
 1214    const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,
 1215    const VkAllocationCallbacks *pAllocator,
 1216    VkSamplerYcbcrConversion *pYcbcrConversion)
 1217 {
 1218    *pYcbcrConversion = VK_NULL_HANDLE;
 1219    return VK_SUCCESS;
 1220 }
 1221 
 1222 void
 1223 tu_DestroySamplerYcbcrConversion(VkDevice device,
 1224                                  VkSamplerYcbcrConversion ycbcrConversion,
 1225                                  const VkAllocationCallbacks *pAllocator)
 1226 {
 1227    /* Do nothing. */
 1228 }