lavapipe: add a mapping for BDA
authorMike Blumenkrantz <michael.blumenkrantz@gmail.com>
Wed, 31 May 2023 15:59:25 +0000 (11:59 -0400)
committerMarge Bot <emma+marge@anholt.net>
Thu, 8 Jun 2023 01:22:01 +0000 (01:22 +0000)
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 <airlied@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23394>

src/gallium/frontends/lavapipe/lvp_device.c
src/gallium/frontends/lavapipe/lvp_image.c
src/gallium/frontends/lavapipe/lvp_private.h

index eaf9491..90be3bd 100644 (file)
@@ -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);
index d796dca..41cf865 100644 (file)
@@ -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(
index ca912ee..97da27b 100644 (file)
@@ -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;
 };