zink: allow direct memory mapping for any COHERENT+CACHED buffer

some drivers may provide this in heaps that get used by non-staging resources,
so avoid extra copies in that case

unlike the previous attempt at this optimization, this utilizes the screen-based
context for thread-safe transfers, which should avoid races/crashes

fix #8171

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21452>
This commit is contained in:
Mike Blumenkrantz
2022-10-24 11:58:13 -04:00
committed by Marge Bot
parent d78de2a962
commit 52f27cda05
2 changed files with 16 additions and 3 deletions

View File

@@ -30,6 +30,7 @@
#include "zink_batch.h"
#define VK_VIS_VRAM (VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
#define VK_STAGING_RAM (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT)
#define VK_LAZY_VRAM (VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)

View File

@@ -1898,15 +1898,22 @@ zink_buffer_map(struct pipe_context *pctx,
goto success;
usage |= PIPE_MAP_UNSYNCHRONIZED;
} else if (!(usage & PIPE_MAP_UNSYNCHRONIZED) &&
(((usage & PIPE_MAP_READ) && !(usage & PIPE_MAP_PERSISTENT) && res->base.b.usage != PIPE_USAGE_STAGING) || !res->obj->host_visible)) {
assert(!(usage & (TC_TRANSFER_MAP_THREADED_UNSYNC | PIPE_MAP_THREAD_SAFE)));
if (!res->obj->host_visible || !(usage & PIPE_MAP_ONCE)) {
(((usage & PIPE_MAP_READ) && !(usage & PIPE_MAP_PERSISTENT) &&
((res->obj->bo->base.placement & VK_STAGING_RAM) != VK_STAGING_RAM)) ||
!res->obj->host_visible)) {
assert(!(usage & (TC_TRANSFER_MAP_THREADED_UNSYNC)));
if (!res->obj->host_visible || res->base.b.flags & PIPE_RESOURCE_FLAG_DONT_MAP_DIRECTLY) {
overwrite:
trans->offset = box->x % screen->info.props.limits.minMemoryMapAlignment;
trans->staging_res = pipe_buffer_create(&screen->base, PIPE_BIND_LINEAR, PIPE_USAGE_STAGING, box->width + trans->offset);
if (!trans->staging_res)
goto fail;
struct zink_resource *staging_res = zink_resource(trans->staging_res);
if (usage & PIPE_MAP_THREAD_SAFE) {
/* this map can't access the passed context: use the copy context */
zink_screen_lock_context(screen);
ctx = screen->copy_context;
}
zink_copy_buffer(ctx, staging_res, res, trans->offset, box->x, box->width);
res = staging_res;
usage &= ~PIPE_MAP_UNSYNCHRONIZED;
@@ -1975,10 +1982,15 @@ overwrite:
res->obj->persistent_maps++;
success:
/* ensure the copy context gets unlocked */
if (ctx == screen->copy_context)
zink_screen_unlock_context(screen);
*transfer = &trans->base.b;
return ptr;
fail:
if (ctx == screen->copy_context)
zink_screen_unlock_context(screen);
destroy_transfer(ctx, trans);
return NULL;
}