From ad241b15a9e517dd4c4e8d7b1d5dab7c3a74b37c Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Tue, 9 Mar 2021 13:49:00 -0500 Subject: [PATCH] vk: consolidate dynamic descriptor binding sorting this code was duplicated across several drivers Reviewed-by: Adam Jackson turnip changes Reviewed-by: Hyunjun Ko Part-of: --- src/amd/vulkan/radv_descriptor_set.c | 31 ++--------- src/broadcom/vulkan/v3dv_descriptor_set.c | 43 ++------------- src/freedreno/vulkan/tu_descriptor_set.c | 34 +----------- .../frontends/lavapipe/lvp_descriptor_set.c | 27 +-------- src/vulkan/util/meson.build | 2 + src/vulkan/util/vk_descriptors.c | 55 +++++++++++++++++++ src/vulkan/util/vk_descriptors.h | 43 +++++++++++++++ 7 files changed, 115 insertions(+), 120 deletions(-) create mode 100644 src/vulkan/util/vk_descriptors.c create mode 100644 src/vulkan/util/vk_descriptors.h diff --git a/src/amd/vulkan/radv_descriptor_set.c b/src/amd/vulkan/radv_descriptor_set.c index 8a07ea9b0f8..5bb22b24c2b 100644 --- a/src/amd/vulkan/radv_descriptor_set.c +++ b/src/amd/vulkan/radv_descriptor_set.c @@ -29,6 +29,7 @@ #include "util/mesa-sha1.h" #include "radv_private.h" #include "sid.h" +#include "vk_descriptors.h" #include "vk_format.h" #include "vk_util.h" @@ -46,28 +47,6 @@ static bool has_equal_immutable_samplers(const VkSampler *samplers, uint32_t cou return true; } -static int binding_compare(const void* av, const void *bv) -{ - const VkDescriptorSetLayoutBinding *a = (const VkDescriptorSetLayoutBinding*)av; - const VkDescriptorSetLayoutBinding *b = (const VkDescriptorSetLayoutBinding*)bv; - - return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0; -} - -static VkDescriptorSetLayoutBinding * -create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, unsigned count) { - VkDescriptorSetLayoutBinding *sorted_bindings = malloc(MAX2(count * sizeof(VkDescriptorSetLayoutBinding), 1)); - if (!sorted_bindings) - return NULL; - - if (count) { - memcpy(sorted_bindings, bindings, count * sizeof(VkDescriptorSetLayoutBinding)); - qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), binding_compare); - } - - return sorted_bindings; -} - static bool radv_mutable_descriptor_type_size_alignment(const VkMutableDescriptorTypeListVALVE *list, uint64_t *out_size, uint64_t *out_align) { @@ -176,8 +155,8 @@ VkResult radv_CreateDescriptorSetLayout( } else set_layout->ycbcr_sampler_offsets_offset = 0; - VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(pCreateInfo->pBindings, - pCreateInfo->bindingCount); + VkDescriptorSetLayoutBinding *bindings = vk_create_sorted_bindings(pCreateInfo->pBindings, + pCreateInfo->bindingCount); if (!bindings) { vk_object_base_finish(&set_layout->base); vk_free2(&device->vk.alloc, pAllocator, set_layout); @@ -358,8 +337,8 @@ void radv_GetDescriptorSetLayoutSupport(VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo, VkDescriptorSetLayoutSupport* pSupport) { - VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(pCreateInfo->pBindings, - pCreateInfo->bindingCount); + VkDescriptorSetLayoutBinding *bindings = vk_create_sorted_bindings(pCreateInfo->pBindings, + pCreateInfo->bindingCount); if (!bindings) { pSupport->supported = false; return; diff --git a/src/broadcom/vulkan/v3dv_descriptor_set.c b/src/broadcom/vulkan/v3dv_descriptor_set.c index 7b41f918ce8..9cfdc15f5dd 100644 --- a/src/broadcom/vulkan/v3dv_descriptor_set.c +++ b/src/broadcom/vulkan/v3dv_descriptor_set.c @@ -21,6 +21,7 @@ * IN THE SOFTWARE. */ +#include "vk_descriptors.h" #include "vk_util.h" #include "v3dv_private.h" @@ -557,40 +558,6 @@ v3dv_ResetDescriptorPool(VkDevice _device, return VK_SUCCESS; } -static int -binding_compare(const void *av, const void *bv) -{ - const VkDescriptorSetLayoutBinding *a = - (const VkDescriptorSetLayoutBinding *) av; - const VkDescriptorSetLayoutBinding *b = - (const VkDescriptorSetLayoutBinding *) bv; - - return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0; -} - -static VkDescriptorSetLayoutBinding * -create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, - unsigned count, - struct v3dv_device *device, - const VkAllocationCallbacks *pAllocator) -{ - VkDescriptorSetLayoutBinding *sorted_bindings = - vk_alloc2(&device->vk.alloc, pAllocator, - count * sizeof(VkDescriptorSetLayoutBinding), - 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); - - if (!sorted_bindings) - return NULL; - - memcpy(sorted_bindings, bindings, - count * sizeof(VkDescriptorSetLayoutBinding)); - - qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), - binding_compare); - - return sorted_bindings; -} - VkResult v3dv_CreateDescriptorSetLayout(VkDevice _device, const VkDescriptorSetLayoutCreateInfo *pCreateInfo, @@ -643,9 +610,8 @@ v3dv_CreateDescriptorSetLayout(VkDevice _device, VkDescriptorSetLayoutBinding *bindings = NULL; if (pCreateInfo->bindingCount > 0) { assert(max_binding >= 0); - bindings = create_sorted_bindings(pCreateInfo->pBindings, - pCreateInfo->bindingCount, - device, pAllocator); + bindings = vk_create_sorted_bindings(pCreateInfo->pBindings, + pCreateInfo->bindingCount); if (!bindings) { vk_object_free(&device->vk, pAllocator, set_layout); return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY); @@ -719,8 +685,7 @@ v3dv_CreateDescriptorSetLayout(VkDevice _device, binding->descriptorCount; } - if (bindings) - vk_free2(&device->vk.alloc, pAllocator, bindings); + free(bindings); set_layout->descriptor_count = descriptor_count; set_layout->dynamic_offset_count = dynamic_offset_count; diff --git a/src/freedreno/vulkan/tu_descriptor_set.c b/src/freedreno/vulkan/tu_descriptor_set.c index 765e3c7913d..0042a0c8067 100644 --- a/src/freedreno/vulkan/tu_descriptor_set.c +++ b/src/freedreno/vulkan/tu_descriptor_set.c @@ -44,37 +44,9 @@ #include #include "util/mesa-sha1.h" +#include "vk_descriptors.h" #include "vk_util.h" -static int -binding_compare(const void *av, const void *bv) -{ - const VkDescriptorSetLayoutBinding *a = - (const VkDescriptorSetLayoutBinding *) av; - const VkDescriptorSetLayoutBinding *b = - (const VkDescriptorSetLayoutBinding *) bv; - - return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0; -} - -static VkDescriptorSetLayoutBinding * -create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, - unsigned count) -{ - VkDescriptorSetLayoutBinding *sorted_bindings = - malloc(count * sizeof(VkDescriptorSetLayoutBinding)); - if (!sorted_bindings) - return NULL; - - memcpy(sorted_bindings, bindings, - count * sizeof(VkDescriptorSetLayoutBinding)); - - qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), - binding_compare); - - return sorted_bindings; -} - static uint32_t descriptor_size(VkDescriptorType type) { @@ -160,7 +132,7 @@ tu_CreateDescriptorSetLayout( struct tu_sampler_ycbcr_conversion *ycbcr_samplers = (void*) &samplers[immutable_sampler_count]; - VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings( + VkDescriptorSetLayoutBinding *bindings = vk_create_sorted_bindings( pCreateInfo->pBindings, pCreateInfo->bindingCount); if (!bindings) { vk_object_free(&device->vk, pAllocator, set_layout); @@ -275,7 +247,7 @@ tu_GetDescriptorSetLayoutSupport( const VkDescriptorSetLayoutCreateInfo *pCreateInfo, VkDescriptorSetLayoutSupport *pSupport) { - VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings( + VkDescriptorSetLayoutBinding *bindings = vk_create_sorted_bindings( pCreateInfo->pBindings, pCreateInfo->bindingCount); if (!bindings) { pSupport->supported = false; diff --git a/src/gallium/frontends/lavapipe/lvp_descriptor_set.c b/src/gallium/frontends/lavapipe/lvp_descriptor_set.c index 697107bd3ce..18de5bf6baf 100644 --- a/src/gallium/frontends/lavapipe/lvp_descriptor_set.c +++ b/src/gallium/frontends/lavapipe/lvp_descriptor_set.c @@ -22,31 +22,10 @@ */ #include "lvp_private.h" +#include "vk_descriptors.h" #include "vk_util.h" #include "u_math.h" -static int binding_compare(const void* av, const void *bv) -{ - const VkDescriptorSetLayoutBinding *a = (const VkDescriptorSetLayoutBinding*)av; - const VkDescriptorSetLayoutBinding *b = (const VkDescriptorSetLayoutBinding*)bv; - - return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0; -} - -static VkDescriptorSetLayoutBinding * -create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, unsigned count) { - VkDescriptorSetLayoutBinding *sorted_bindings = malloc(MAX2(count * sizeof(VkDescriptorSetLayoutBinding), 1)); - if (!sorted_bindings) - return NULL; - - if (count) { - memcpy(sorted_bindings, bindings, count * sizeof(VkDescriptorSetLayoutBinding)); - qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), binding_compare); - } - - return sorted_bindings; -} - VKAPI_ATTR VkResult VKAPI_CALL lvp_CreateDescriptorSetLayout( VkDevice _device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo, @@ -100,8 +79,8 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_CreateDescriptorSetLayout( set_layout->shader_stages = 0; set_layout->size = 0; - VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(pCreateInfo->pBindings, - pCreateInfo->bindingCount); + VkDescriptorSetLayoutBinding *bindings = vk_create_sorted_bindings(pCreateInfo->pBindings, + pCreateInfo->bindingCount); if (!bindings) { vk_object_base_finish(&set_layout->base); vk_free2(&device->vk.alloc, pAllocator, set_layout); diff --git a/src/vulkan/util/meson.build b/src/vulkan/util/meson.build index 792a7e645c6..6338b0b92d3 100644 --- a/src/vulkan/util/meson.build +++ b/src/vulkan/util/meson.build @@ -29,6 +29,8 @@ files_vulkan_util = files( 'vk_debug_report.h', 'vk_deferred_operation.c', 'vk_deferred_operation.h', + 'vk_descriptors.c', + 'vk_descriptors.h', 'vk_device.c', 'vk_device.h', 'vk_format.c', diff --git a/src/vulkan/util/vk_descriptors.c b/src/vulkan/util/vk_descriptors.c new file mode 100644 index 00000000000..611d96df6d1 --- /dev/null +++ b/src/vulkan/util/vk_descriptors.c @@ -0,0 +1,55 @@ +/* + * Copyright © 2016 Red Hat. + * Copyright © 2016 Bas Nieuwenhuizen + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include "vk_descriptors.h" +#include "util/macros.h" + +static int +binding_compare(const void* av, const void *bv) +{ + const VkDescriptorSetLayoutBinding *a = (const VkDescriptorSetLayoutBinding*)av; + const VkDescriptorSetLayoutBinding *b = (const VkDescriptorSetLayoutBinding*)bv; + + return (a->binding < b->binding) ? -1 : (a->binding > b->binding) ? 1 : 0; +} + +VkDescriptorSetLayoutBinding * +vk_create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, unsigned count) +{ + VkDescriptorSetLayoutBinding *sorted_bindings = malloc(MAX2(count * sizeof(VkDescriptorSetLayoutBinding), 1)); + if (!sorted_bindings) + return NULL; + + if (count) { + memcpy(sorted_bindings, bindings, count * sizeof(VkDescriptorSetLayoutBinding)); + qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), binding_compare); + } else { + /* just an empty struct */ + memset(sorted_bindings, 0, sizeof(VkDescriptorSetLayoutBinding)); + } + + return sorted_bindings; +} diff --git a/src/vulkan/util/vk_descriptors.h b/src/vulkan/util/vk_descriptors.h new file mode 100644 index 00000000000..2d588da2527 --- /dev/null +++ b/src/vulkan/util/vk_descriptors.h @@ -0,0 +1,43 @@ +/* + * Copyright © 2016 Red Hat. + * Copyright © 2016 Bas Nieuwenhuizen + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef VK_DESCRIPTORS_H +#define VK_DESCRIPTORS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +VkDescriptorSetLayoutBinding * +vk_create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, unsigned count); + +#ifdef __cplusplus +} +#endif + + + +#endif