Module: Mesa
Branch: staging/23.1
Commit: 9bfd616f1980a489e2aa3223a8040a2dff5efc01
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=9bfd616f1980a489e2aa3223a8040a2dff5efc01

Author: Mike Blumenkrantz <[email protected]>
Date:   Thu Apr 13 13:54:24 2023 -0400

zink: make general bo allocation more robust by iterating

previously there was a fallback path here (broken by f6d3a5755f6)
which would attempt to demote BAR allocations to other heaps on failure
to avoid oom

this was great, but it's not the most robust solution, which is to iterate
all the memory types matching the given heap and try them in addition to having
a demotion fallback

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22567>

---

 src/gallium/drivers/zink/zink_resource.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/gallium/drivers/zink/zink_resource.c 
b/src/gallium/drivers/zink/zink_resource.c
index 19fb67b4d21..a5649d89409 100644
--- a/src/gallium/drivers/zink/zink_resource.c
+++ b/src/gallium/drivers/zink/zink_resource.c
@@ -1077,19 +1077,26 @@ resource_object_create(struct zink_screen *screen, 
const struct pipe_resource *t
    }
 
 retry:
-   mai.memoryTypeIndex = zink_mem_type_idx_from_bits(screen, heap, 
reqs.memoryTypeBits);
-   assert(reqs.memoryTypeBits & BITFIELD_BIT(mai.memoryTypeIndex));
-   obj->bo = zink_bo(zink_bo_create(screen, reqs.size, alignment, heap, 
mai.pNext ? ZINK_ALLOC_NO_SUBALLOC : 0, mai.memoryTypeIndex, mai.pNext));
-   if (!obj->bo) {
-      if (heap == ZINK_HEAP_DEVICE_LOCAL_VISIBLE) {
-         if (templ->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT || templ->usage == 
PIPE_USAGE_DYNAMIC)
-            heap = ZINK_HEAP_HOST_VISIBLE_COHERENT;
-         else
-            heap = ZINK_HEAP_DEVICE_LOCAL;
-         goto retry;
+   /* iterate over all available memory types to reduce chance of oom */
+   for (unsigned i = 0; !obj->bo && i < screen->heap_count[heap]; i++) {
+      if (!(reqs.memoryTypeBits & BITFIELD_BIT(screen->heap_map[heap][i])))
+         continue;
+
+      mai.memoryTypeIndex = screen->heap_map[heap][i];
+      obj->bo = zink_bo(zink_bo_create(screen, reqs.size, alignment, heap, 
mai.pNext ? ZINK_ALLOC_NO_SUBALLOC : 0, mai.memoryTypeIndex, mai.pNext));
+      if (!obj->bo) {
+         if (heap == ZINK_HEAP_DEVICE_LOCAL_VISIBLE) {
+            /* demote BAR allocations to a different heap on failure to avoid 
oom */
+            if (templ->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT || templ->usage 
== PIPE_USAGE_DYNAMIC)
+               heap = ZINK_HEAP_HOST_VISIBLE_COHERENT;
+            else
+               heap = ZINK_HEAP_DEVICE_LOCAL;
+            goto retry;
+         }
       }
-      goto fail2;
    }
+   if (!obj->bo)
+      goto fail2;
    if (aflags == ZINK_ALLOC_SPARSE) {
       obj->size = templ->width0;
    } else {

Reply via email to