From ea9ded410a4031c83e7c27c570856ce7e8dd29c1 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Wed, 31 May 2023 11:59:25 -0400 Subject: [PATCH] lavapipe: add a mapping for BDA when passing around BDA, it's important to be able to link the pointer back to the pipe_resource since BDA doesn't have an explicit lifetime this mapping enables cmds to receive a BDA pointer and then map it back to a pipe_resource in order to avoid gymnastics with dynamically creating pipe_resource objects which may or may not be able to be freed Acked-by: Dave Airlie Part-of: --- src/gallium/frontends/lavapipe/lvp_device.c | 5 +++++ src/gallium/frontends/lavapipe/lvp_image.c | 17 +++++++++++++++-- src/gallium/frontends/lavapipe/lvp_private.h | 2 ++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/gallium/frontends/lavapipe/lvp_device.c b/src/gallium/frontends/lavapipe/lvp_device.c index eaf9491..90be3bd 100644 --- a/src/gallium/frontends/lavapipe/lvp_device.c +++ b/src/gallium/frontends/lavapipe/lvp_device.c @@ -1527,6 +1527,8 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_CreateDevice( shstate.type = PIPE_SHADER_IR_NIR; shstate.ir.nir = b.shader; device->noop_fs = device->queue.ctx->create_fs_state(device->queue.ctx, &shstate); + _mesa_hash_table_init(&device->bda, NULL, _mesa_hash_pointer, _mesa_key_pointer_equal); + simple_mtx_init(&device->bda_lock, mtx_plain); *pDevice = lvp_device_to_handle(device); @@ -1544,6 +1546,9 @@ VKAPI_ATTR void VKAPI_CALL lvp_DestroyDevice( if (device->queue.last_fence) device->pscreen->fence_reference(device->pscreen, &device->queue.last_fence, NULL); + ralloc_free(device->bda.table); + simple_mtx_destroy(&device->bda_lock); + lvp_queue_finish(&device->queue); vk_device_finish(&device->vk); vk_free(&device->vk.alloc, device); diff --git a/src/gallium/frontends/lavapipe/lvp_image.c b/src/gallium/frontends/lavapipe/lvp_image.c index d796dca..41cf865 100644 --- a/src/gallium/frontends/lavapipe/lvp_image.c +++ b/src/gallium/frontends/lavapipe/lvp_image.c @@ -436,18 +436,31 @@ VKAPI_ATTR void VKAPI_CALL lvp_DestroyBuffer( if (!_buffer) return; + char *ptr = (char*)buffer->pmem + buffer->offset; + if (ptr) { + simple_mtx_lock(&device->bda_lock); + struct hash_entry *he = _mesa_hash_table_search(&device->bda, ptr); + if (he) + _mesa_hash_table_remove(&device->bda, he); + simple_mtx_unlock(&device->bda_lock); + } pipe_resource_reference(&buffer->bo, NULL); vk_object_base_finish(&buffer->base); vk_free2(&device->vk.alloc, pAllocator, buffer); } VKAPI_ATTR VkDeviceAddress VKAPI_CALL lvp_GetBufferDeviceAddress( - VkDevice device, + VkDevice _device, const VkBufferDeviceAddressInfo* pInfo) { + LVP_FROM_HANDLE(lvp_device, device, _device); LVP_FROM_HANDLE(lvp_buffer, buffer, pInfo->buffer); + char *ptr = (char*)buffer->pmem + buffer->offset; + simple_mtx_lock(&device->bda_lock); + _mesa_hash_table_insert(&device->bda, ptr, buffer); + simple_mtx_unlock(&device->bda_lock); - return (VkDeviceAddress)(uintptr_t)((char*)buffer->pmem + buffer->offset); + return (VkDeviceAddress)(uintptr_t)ptr; } VKAPI_ATTR uint64_t VKAPI_CALL lvp_GetBufferOpaqueCaptureAddress( diff --git a/src/gallium/frontends/lavapipe/lvp_private.h b/src/gallium/frontends/lavapipe/lvp_private.h index ca912ee..97da27b 100644 --- a/src/gallium/frontends/lavapipe/lvp_private.h +++ b/src/gallium/frontends/lavapipe/lvp_private.h @@ -191,6 +191,8 @@ struct lvp_device { struct lvp_physical_device *physical_device; struct pipe_screen *pscreen; void *noop_fs; + simple_mtx_t bda_lock; + struct hash_table bda; bool poison_mem; bool print_cmds; }; -- 2.7.4