From 487926aa8658fe3e533ab68a36f3b2aff07db716 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Wed, 8 Dec 2021 15:24:04 -0800 Subject: [PATCH] venus: add vn_cs_encoder_storage_type It generalizes cs->indirect. Signed-off-by: Chia-I Wu Reviewed-by: Ryan Neph Reviewed-by: Yiwei Zhang Part-of: --- src/virtio/vulkan/vn_command_buffer.c | 3 ++- src/virtio/vulkan/vn_cs.c | 16 ++++++++++------ src/virtio/vulkan/vn_cs.h | 23 ++++++++++++++++------- src/virtio/vulkan/vn_instance.c | 11 ++++++----- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/virtio/vulkan/vn_command_buffer.c b/src/virtio/vulkan/vn_command_buffer.c index 41a3828..7909795 100644 --- a/src/virtio/vulkan/vn_command_buffer.c +++ b/src/virtio/vulkan/vn_command_buffer.c @@ -560,7 +560,8 @@ vn_AllocateCommandBuffers(VkDevice device, list_addtail(&cmd->head, &pool->command_buffers); cmd->state = VN_COMMAND_BUFFER_STATE_INITIAL; - vn_cs_encoder_init_indirect(&cmd->cs, dev->instance, 16 * 1024); + vn_cs_encoder_init(&cmd->cs, dev->instance, + VN_CS_ENCODER_STORAGE_SHMEM_ARRAY, 16 * 1024); VkCommandBuffer cmd_handle = vn_command_buffer_to_handle(cmd); pCommandBuffers[i] = cmd_handle; diff --git a/src/virtio/vulkan/vn_cs.c b/src/virtio/vulkan/vn_cs.c index abb3517..9dc5c71 100644 --- a/src/virtio/vulkan/vn_cs.c +++ b/src/virtio/vulkan/vn_cs.c @@ -88,20 +88,24 @@ vn_cs_encoder_gc_buffers(struct vn_cs_encoder *enc) } void -vn_cs_encoder_init_indirect(struct vn_cs_encoder *enc, - struct vn_instance *instance, - size_t min_size) +vn_cs_encoder_init(struct vn_cs_encoder *enc, + struct vn_instance *instance, + enum vn_cs_encoder_storage_type storage_type, + size_t min_size) { + /* VN_CS_ENCODER_INITIALIZER* should be used instead */ + assert(storage_type != VN_CS_ENCODER_STORAGE_POINTER); + memset(enc, 0, sizeof(*enc)); enc->instance = instance; + enc->storage_type = storage_type; enc->min_buffer_size = min_size; - enc->indirect = true; } void vn_cs_encoder_fini(struct vn_cs_encoder *enc) { - if (unlikely(!enc->indirect)) + if (unlikely(enc->storage_type == VN_CS_ENCODER_STORAGE_POINTER)) return; for (uint32_t i = 0; i < enc->buffer_count; i++) @@ -163,7 +167,7 @@ vn_cs_encoder_grow_buffer_array(struct vn_cs_encoder *enc) bool vn_cs_encoder_reserve_internal(struct vn_cs_encoder *enc, size_t size) { - if (unlikely(!enc->indirect)) + if (unlikely(enc->storage_type == VN_CS_ENCODER_STORAGE_POINTER)) return false; if (enc->buffer_count >= enc->buffer_max) { diff --git a/src/virtio/vulkan/vn_cs.h b/src/virtio/vulkan/vn_cs.h index 8c59d5b..3bd8c05 100644 --- a/src/virtio/vulkan/vn_cs.h +++ b/src/virtio/vulkan/vn_cs.h @@ -15,6 +15,7 @@ #define VN_CS_ENCODER_INITIALIZER_LOCAL(storage, size) \ (struct vn_cs_encoder) \ { \ + .storage_type = VN_CS_ENCODER_STORAGE_POINTER, \ .buffers = &VN_CS_ENCODER_BUFFER_INITIALIZER(storage), \ .buffer_count = 1, .buffer_max = 1, .current_buffer_size = size, \ .cur = storage, .end = (const void *)(storage) + (size), \ @@ -23,9 +24,9 @@ #define VN_CS_ENCODER_INITIALIZER(buf, size) \ (struct vn_cs_encoder) \ { \ - .buffers = (buf), .buffer_count = 1, .buffer_max = 1, \ - .current_buffer_size = size, .cur = (buf)->base, \ - .end = (buf)->base + (size), \ + .storage_type = VN_CS_ENCODER_STORAGE_POINTER, .buffers = (buf), \ + .buffer_count = 1, .buffer_max = 1, .current_buffer_size = size, \ + .cur = (buf)->base, .end = (buf)->base + (size), \ } #define VN_CS_DECODER_INITIALIZER(storage, size) \ @@ -34,6 +35,13 @@ .cur = storage, .end = (const void *)(storage) + (size), \ } +enum vn_cs_encoder_storage_type { + /* a pointer to an externally-managed storage */ + VN_CS_ENCODER_STORAGE_POINTER, + /* an array of dynamically allocated shmems */ + VN_CS_ENCODER_STORAGE_SHMEM_ARRAY, +}; + struct vn_cs_encoder_buffer { struct vn_renderer_shmem *shmem; size_t offset; @@ -43,8 +51,8 @@ struct vn_cs_encoder_buffer { struct vn_cs_encoder { struct vn_instance *instance; /* TODO shmem cache */ + enum vn_cs_encoder_storage_type storage_type; size_t min_buffer_size; - bool indirect; bool fatal_error; @@ -70,9 +78,10 @@ struct vn_cs_decoder { }; void -vn_cs_encoder_init_indirect(struct vn_cs_encoder *enc, - struct vn_instance *instance, - size_t min_size); +vn_cs_encoder_init(struct vn_cs_encoder *enc, + struct vn_instance *instance, + enum vn_cs_encoder_storage_type storage_type, + size_t min_size); void vn_cs_encoder_fini(struct vn_cs_encoder *enc); diff --git a/src/virtio/vulkan/vn_instance.c b/src/virtio/vulkan/vn_instance.c index 793bc9e..96ce612 100644 --- a/src/virtio/vulkan/vn_instance.c +++ b/src/virtio/vulkan/vn_instance.c @@ -160,8 +160,8 @@ vn_instance_init_ring(struct vn_instance *instance) vn_renderer_submit_simple(instance->renderer, create_ring_data, vn_cs_encoder_get_len(&local_enc)); - vn_cs_encoder_init_indirect(&instance->ring.upload, instance, - 1 * 1024 * 1024); + vn_cs_encoder_init(&instance->ring.upload, instance, + VN_CS_ENCODER_STORAGE_SHMEM_ARRAY, 1 * 1024 * 1024); mtx_init(&instance->ring.roundtrip_mutex, mtx_plain); instance->ring.roundtrip_next = 1; @@ -478,7 +478,8 @@ static struct vn_cs_encoder * vn_instance_ring_cs_upload_locked(struct vn_instance *instance, const struct vn_cs_encoder *cs) { - assert(!cs->indirect && cs->buffer_count == 1); + assert(cs->storage_type == VN_CS_ENCODER_STORAGE_POINTER && + cs->buffer_count == 1); const void *cs_data = cs->buffers[0].base; const size_t cs_size = cs->total_committed_size; assert(cs_size == vn_cs_encoder_get_len(cs)); @@ -505,11 +506,11 @@ vn_instance_ring_submit_locked(struct vn_instance *instance, struct vn_ring *ring = &instance->ring.ring; const bool direct = vn_instance_submission_can_direct(instance, cs); - if (!direct && !cs->indirect) { + if (!direct && cs->storage_type == VN_CS_ENCODER_STORAGE_POINTER) { cs = vn_instance_ring_cs_upload_locked(instance, cs); if (!cs) return VK_ERROR_OUT_OF_HOST_MEMORY; - assert(cs->indirect); + assert(cs->storage_type != VN_CS_ENCODER_STORAGE_POINTER); } struct vn_instance_submission submit; -- 2.7.4