nvk: Add BO recycling to the command pool

Command BOs (nvk_cmd_bo) are automatically mapped recyclable BOs to use
for upload and push buffers.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24326>
This commit is contained in:
Faith Ekstrand 2023-01-30 20:11:58 -06:00 committed by Marge Bot
parent 366fa3c7e1
commit 903c000055
2 changed files with 86 additions and 0 deletions

View file

@ -1,6 +1,38 @@
#include "nvk_cmd_pool.h"
#include "nvk_device.h"
#include "nvk_physical_device.h"
static VkResult
nvk_cmd_bo_create(struct nvk_cmd_pool *pool, struct nvk_cmd_bo **bo_out)
{
struct nvk_device *dev = nvk_cmd_pool_device(pool);
struct nvk_cmd_bo *bo;
bo = vk_zalloc(&pool->vk.alloc, sizeof(*bo), 8,
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
if (bo == NULL)
return vk_error(pool, VK_ERROR_OUT_OF_HOST_MEMORY);
uint32_t flags = NOUVEAU_WS_BO_GART | NOUVEAU_WS_BO_MAP;
bo->bo = nouveau_ws_bo_new_mapped(dev->pdev->dev, NVK_CMD_BO_SIZE, 0,
flags, NOUVEAU_WS_BO_WR, &bo->map);
if (bo->bo == NULL) {
vk_free(&pool->vk.alloc, bo);
return vk_error(pool, VK_ERROR_OUT_OF_DEVICE_MEMORY);
}
*bo_out = bo;
return VK_SUCCESS;
}
static void
nvk_cmd_bo_destroy(struct nvk_cmd_pool *pool, struct nvk_cmd_bo *bo)
{
nouveau_ws_bo_unmap(bo->bo, bo->map);
nouveau_ws_bo_destroy(bo->bo);
vk_free(&pool->vk.alloc, bo);
}
VKAPI_ATTR VkResult VKAPI_CALL
nvk_CreateCommandPool(VkDevice _device,
@ -23,11 +55,42 @@ nvk_CreateCommandPool(VkDevice _device,
return result;
}
list_inithead(&pool->free_bos);
*pCmdPool = nvk_cmd_pool_to_handle(pool);
return VK_SUCCESS;
}
static void
nvk_cmd_pool_destroy_bos(struct nvk_cmd_pool *pool)
{
list_for_each_entry_safe(struct nvk_cmd_bo, bo, &pool->free_bos, link)
nvk_cmd_bo_destroy(pool, bo);
list_inithead(&pool->free_bos);
}
VkResult
nvk_cmd_pool_alloc_bo(struct nvk_cmd_pool *pool, struct nvk_cmd_bo **bo_out)
{
if (!list_is_empty(&pool->free_bos)) {
struct nvk_cmd_bo *bo =
list_first_entry(&pool->free_bos, struct nvk_cmd_bo, link);
list_del(&bo->link);
*bo_out = bo;
return VK_SUCCESS;
}
return nvk_cmd_bo_create(pool, bo_out);
}
void
nvk_cmd_pool_free_bo_list(struct nvk_cmd_pool *pool, struct list_head *bos)
{
list_splicetail(bos, &pool->free_bos);
}
VKAPI_ATTR void VKAPI_CALL
nvk_DestroyCommandPool(VkDevice _device,
VkCommandPool commandPool,
@ -39,6 +102,7 @@ nvk_DestroyCommandPool(VkDevice _device,
if (!pool)
return;
nvk_cmd_pool_destroy_bos(pool);
vk_command_pool_finish(&pool->vk);
vk_free2(&device->vk.alloc, pAllocator, pool);
}
@ -51,4 +115,5 @@ nvk_TrimCommandPool(VkDevice device,
VK_FROM_HANDLE(nvk_cmd_pool, pool, commandPool);
vk_command_pool_trim(&pool->vk, flags);
nvk_cmd_pool_destroy_bos(pool);
}

View file

@ -5,8 +5,23 @@
#include "vk_command_pool.h"
#define NVK_CMD_BO_SIZE 64*1024
/* Recyclable command buffer BO, used for both push buffers and upload */
struct nvk_cmd_bo {
struct nouveau_ws_bo *bo;
void *map;
/** Link in nvk_cmd_pool::free_bos or nvk_cmd_buffer::bos */
struct list_head link;
};
struct nvk_cmd_pool {
struct vk_command_pool vk;
/** List of nvk_cmd_bo */
struct list_head free_bos;
};
VK_DEFINE_NONDISP_HANDLE_CASTS(nvk_cmd_pool, vk.base, VkCommandPool,
@ -18,4 +33,10 @@ nvk_cmd_pool_device(struct nvk_cmd_pool *pool)
return (struct nvk_device *)pool->vk.base.device;
}
VkResult nvk_cmd_pool_alloc_bo(struct nvk_cmd_pool *pool,
struct nvk_cmd_bo **bo_out);
void nvk_cmd_pool_free_bo_list(struct nvk_cmd_pool *pool,
struct list_head *bos);
#endif /* NVK_CMD_POOL_H */