Mesa (master): vulkan: Use VK_MULTIALLOC in CreateRenderPass

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Mar 10 18:29:58 UTC 2021


Module: Mesa
Branch: master
Commit: c7345bd1fb62e02ce9c457f4b9342906ed8a333f
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=c7345bd1fb62e02ce9c457f4b9342906ed8a333f

Author: Jason Ekstrand <jason.ekstrand at intel.com>
Date:   Wed Mar 10 08:27:14 2021 -0600

vulkan: Use VK_MULTIALLOC in CreateRenderPass

The variable-length stack allocations are causing issues with ubsan when
the array size is zero.  Also, a heap allocation is probably safer.

Reviewed-by: Eric Anholt <eric at anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8857>

---

 src/vulkan/util/vk_render_pass.c | 45 ++++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/vulkan/util/vk_render_pass.c b/src/vulkan/util/vk_render_pass.c
index 81b01a79a67..9588a41568f 100644
--- a/src/vulkan/util/vk_render_pass.c
+++ b/src/vulkan/util/vk_render_pass.c
@@ -21,6 +21,7 @@
  * IN THE SOFTWARE.
  */
 
+#include "vk_alloc.h"
 #include "vk_common_entrypoints.h"
 #include "vk_device.h"
 #include "vk_util.h"
@@ -51,13 +52,12 @@ vk_common_CreateRenderPass(VkDevice _device,
 {
    VK_FROM_HANDLE(vk_device, device, _device);
 
-   /* note: these counts shouldn't be excessively high, so allocating it all
-    * on the stack should be OK..
-    * also note preserve attachments aren't translated, currently unused
-    */
-   VkAttachmentDescription2 attachments[pCreateInfo->attachmentCount];
-   VkSubpassDescription2 subpasses[pCreateInfo->subpassCount];
-   VkSubpassDependency2 dependencies[pCreateInfo->dependencyCount];
+   VkRenderPassCreateInfo2 *create_info;
+   VkAttachmentDescription2 *attachments;
+   VkSubpassDescription2 *subpasses;
+   VkSubpassDependency2 *dependencies;
+   VkAttachmentReference2 *references;
+
    uint32_t reference_count = 0;
    for (uint32_t i = 0; i < pCreateInfo->subpassCount; i++) {
       reference_count += pCreateInfo->pSubpasses[i].inputAttachmentCount;
@@ -67,8 +67,18 @@ vk_common_CreateRenderPass(VkDevice _device,
       if (pCreateInfo->pSubpasses[i].pDepthStencilAttachment)
          reference_count += 1;
    }
-   VkAttachmentReference2 reference[reference_count];
-   VkAttachmentReference2 *reference_ptr = reference;
+
+   VK_MULTIALLOC(ma);
+   vk_multialloc_add(&ma, &create_info, 1);
+   vk_multialloc_add(&ma, &subpasses, pCreateInfo->subpassCount);
+   vk_multialloc_add(&ma, &attachments, pCreateInfo->attachmentCount);
+   vk_multialloc_add(&ma, &dependencies, pCreateInfo->dependencyCount);
+   vk_multialloc_add(&ma, &references, reference_count);
+   if (!vk_multialloc_alloc2(&ma, &device->alloc, pAllocator,
+                             VK_SYSTEM_ALLOCATION_SCOPE_COMMAND))
+      return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+   VkAttachmentReference2 *reference_ptr = references;
 
    VkRenderPassMultiviewCreateInfo *multiview_info = NULL;
    vk_foreach_struct(ext, pCreateInfo->pNext) {
@@ -132,7 +142,7 @@ vk_common_CreateRenderPass(VkDevice _device,
       }
    }
 
-   assert(reference_ptr == reference + reference_count);
+   assert(reference_ptr == references + reference_count);
 
    for (uint32_t i = 0; i < pCreateInfo->dependencyCount; i++) {
       dependencies[i] = (VkSubpassDependency2) {
@@ -152,7 +162,7 @@ vk_common_CreateRenderPass(VkDevice _device,
          dependencies[i].viewOffset = multiview_info->pViewOffsets[i];
    }
 
-   VkRenderPassCreateInfo2 create_info = {
+   *create_info = (VkRenderPassCreateInfo2) {
       .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2,
       .pNext = pCreateInfo->pNext,
       .flags = pCreateInfo->flags,
@@ -165,10 +175,15 @@ vk_common_CreateRenderPass(VkDevice _device,
    };
 
    if (multiview_info) {
-      create_info.correlatedViewMaskCount = multiview_info->correlationMaskCount;
-      create_info.pCorrelatedViewMasks = multiview_info->pCorrelationMasks;
+      create_info->correlatedViewMaskCount = multiview_info->correlationMaskCount;
+      create_info->pCorrelatedViewMasks = multiview_info->pCorrelationMasks;
    }
 
-   return device->dispatch_table.CreateRenderPass2(_device, &create_info,
-                                                   pAllocator, pRenderPass);
+   VkResult result =
+      device->dispatch_table.CreateRenderPass2(_device, create_info,
+                                               pAllocator, pRenderPass);
+
+   vk_free2(&device->alloc, pAllocator, create_info);
+
+   return result;
 }



More information about the mesa-commit mailing list