From: Lynne <dev@lynne.ee> To: ffmpeg-devel@ffmpeg.org Cc: Lynne <dev@lynne.ee> Subject: [FFmpeg-devel] [PATCH v2 03/12] vulkan: copy host-mapping buffer code from hwcontext Date: Mon, 24 Feb 2025 09:04:16 +0100 Message-ID: <20250224080434.5632-3-dev@lynne.ee> (raw) In-Reply-To: <20250224080434.5632-1-dev@lynne.ee> This is useful elsewhere. --- libavutil/vulkan.c | 153 ++++++++++++++++++++++++++++++++++++++++++++- libavutil/vulkan.h | 17 ++++- 2 files changed, 167 insertions(+), 3 deletions(-) diff --git a/libavutil/vulkan.c b/libavutil/vulkan.c index de6260b2f8..4d60ac1b3a 100644 --- a/libavutil/vulkan.c +++ b/libavutil/vulkan.c @@ -1115,6 +1115,8 @@ int ff_vk_unmap_buffers(FFVulkanContext *s, FFVkBuffer **buf, int nb_buffers, .memory = buf[i]->mem, .size = VK_WHOLE_SIZE, }; + + av_assert0(!buf[i]->host_ref); if (buf[i]->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) continue; flush_list[flush_count++] = flush_buf; @@ -1146,12 +1148,18 @@ void ff_vk_free_buf(FFVulkanContext *s, FFVkBuffer *buf) if (!buf || !s->hwctx) return; - if (buf->mapped_mem) + if (buf->mapped_mem && !buf->host_ref) ff_vk_unmap_buffer(s, buf, 0); if (buf->buf != VK_NULL_HANDLE) vk->DestroyBuffer(s->hwctx->act_dev, buf->buf, s->hwctx->alloc); if (buf->mem != VK_NULL_HANDLE) vk->FreeMemory(s->hwctx->act_dev, buf->mem, s->hwctx->alloc); + if (buf->host_ref) + av_buffer_unref(&buf->host_ref); + + buf->buf = VK_NULL_HANDLE; + buf->mem = VK_NULL_HANDLE; + buf->mapped_mem = NULL; } static void free_data_buf(void *opaque, uint8_t *data) @@ -1228,6 +1236,147 @@ int ff_vk_get_pooled_buffer(FFVulkanContext *ctx, AVBufferPool **buf_pool, return 0; } +static int create_mapped_buffer(FFVulkanContext *s, + FFVkBuffer *vkb, VkBufferUsageFlags usage, + size_t size, + VkExternalMemoryBufferCreateInfo *create_desc, + VkImportMemoryHostPointerInfoEXT *import_desc, + VkMemoryHostPointerPropertiesEXT props) +{ + int err; + VkResult ret; + FFVulkanFunctions *vk = &s->vkfn; + + VkBufferCreateInfo buf_spawn = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = create_desc, + .usage = usage, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + .size = size, + }; + VkMemoryRequirements req = { + .size = size, + .alignment = s->hprops.minImportedHostPointerAlignment, + .memoryTypeBits = props.memoryTypeBits, + }; + + err = ff_vk_alloc_mem(s, &req, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + import_desc, &vkb->flags, &vkb->mem); + if (err < 0) + return err; + + ret = vk->CreateBuffer(s->hwctx->act_dev, &buf_spawn, s->hwctx->alloc, &vkb->buf); + if (ret != VK_SUCCESS) { + vk->FreeMemory(s->hwctx->act_dev, vkb->mem, s->hwctx->alloc); + return AVERROR_EXTERNAL; + } + + ret = vk->BindBufferMemory(s->hwctx->act_dev, vkb->buf, vkb->mem, 0); + if (ret != VK_SUCCESS) { + vk->FreeMemory(s->hwctx->act_dev, vkb->mem, s->hwctx->alloc); + vk->DestroyBuffer(s->hwctx->act_dev, vkb->buf, s->hwctx->alloc); + return AVERROR_EXTERNAL; + } + + return 0; +} + +int ff_vk_host_map_buffer(FFVulkanContext *s, AVBufferRef **dst, + AVBufferRef *src, VkBufferUsageFlags usage) +{ + int err; + VkResult ret; + FFVulkanFunctions *vk = &s->vkfn; + + VkExternalMemoryBufferCreateInfo create_desc = { + .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO, + .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, + }; + VkMemoryAllocateFlagsInfo alloc_flags = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, + .flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT, + }; + VkImportMemoryHostPointerInfoEXT import_desc = { + .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT, + .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, + .pNext = usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT ? &alloc_flags : NULL, + }; + VkMemoryHostPointerPropertiesEXT props; + + AVBufferRef *ref; + FFVkBuffer *vkb; + size_t offs; + size_t buffer_size; + + *dst = NULL; + + /* Get the previous point at which mapping was possible and use it */ + offs = (uintptr_t)src->data % s->hprops.minImportedHostPointerAlignment; + import_desc.pHostPointer = src->data - offs; + + props = (VkMemoryHostPointerPropertiesEXT) { + VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT, + }; + ret = vk->GetMemoryHostPointerPropertiesEXT(s->hwctx->act_dev, + import_desc.handleType, + import_desc.pHostPointer, + &props); + if (!(ret == VK_SUCCESS && props.memoryTypeBits)) + return AVERROR(EINVAL); + + /* Ref the source buffer */ + ref = av_buffer_ref(src); + if (!ref) + return AVERROR(ENOMEM); + + /* Add the offset at the start, which gets ignored */ + buffer_size = offs + src->size; + buffer_size = FFALIGN(buffer_size, s->props.properties.limits.minMemoryMapAlignment); + buffer_size = FFALIGN(buffer_size, s->hprops.minImportedHostPointerAlignment); + + /* Create a buffer struct */ + vkb = av_mallocz(sizeof(*vkb)); + if (!vkb) { + av_buffer_unref(&ref); + return AVERROR(ENOMEM); + } + + err = create_mapped_buffer(s, vkb, usage, + buffer_size, &create_desc, &import_desc, + props); + if (err < 0) { + av_buffer_unref(&ref); + av_free(vkb); + return err; + } + + if (usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) { + VkBufferDeviceAddressInfo address_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, + .buffer = vkb->buf, + }; + vkb->address = vk->GetBufferDeviceAddress(s->hwctx->act_dev, &address_info); + } + + vkb->host_ref = ref; + vkb->virtual_offset = offs; + vkb->address += offs; + vkb->mapped_mem = src->data; + vkb->size = buffer_size - offs; + + /* Create a ref */ + *dst = av_buffer_create((uint8_t *)vkb, sizeof(*vkb), + destroy_avvkbuf, s, 0); + if (!(*dst)) { + destroy_avvkbuf(s, (uint8_t *)vkb); + *dst = NULL; + return AVERROR(ENOMEM); + } + + return 0; +} + int ff_vk_shader_add_push_const(FFVulkanShader *shd, int offset, int size, VkShaderStageFlagBits stage) { @@ -2546,7 +2695,7 @@ int ff_vk_shader_update_desc_buffer(FFVulkanContext *s, FFVkExecContext *e, } else { VkDescriptorBufferInfo desc_pool_write_info_buf = { .buffer = buf->buf, - .offset = offset, + .offset = buf->virtual_offset + offset, .range = len, }; VkWriteDescriptorSet desc_pool_write_info = { diff --git a/libavutil/vulkan.h b/libavutil/vulkan.h index c3ddf0cd6b..327ce2b286 100644 --- a/libavutil/vulkan.h +++ b/libavutil/vulkan.h @@ -96,8 +96,17 @@ typedef struct FFVkBuffer { VkPipelineStageFlags2 stage; VkAccessFlags2 access; - /* Only valid when allocated via ff_vk_get_pooled_buffer with HOST_VISIBLE */ + /* Only valid when allocated via ff_vk_get_pooled_buffer with HOST_VISIBLE or + * via ff_vk_host_map_buffer */ uint8_t *mapped_mem; + + /* Set by ff_vk_host_map_buffer. This is the offset at which the buffer data + * actually begins at. + * The address and mapped_mem fields will be offset by this amount. */ + size_t virtual_offset; + + /* If host mapping, reference to the backing host memory buffer */ + AVBufferRef *host_ref; } FFVkBuffer; typedef struct FFVkExecContext { @@ -523,6 +532,12 @@ int ff_vk_get_pooled_buffer(FFVulkanContext *ctx, AVBufferPool **buf_pool, void *create_pNext, size_t size, VkMemoryPropertyFlagBits mem_props); +/** Maps a system RAM buffer into a Vulkan buffer. + * References the source buffer. + */ +int ff_vk_host_map_buffer(FFVulkanContext *s, AVBufferRef **dst, + AVBufferRef *src, VkBufferUsageFlags usage); + /** * Create a sampler. */ -- 2.47.2 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2025-02-24 8:05 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-02-24 8:04 [FFmpeg-devel] [PATCH v2 01/12] ffv1enc_vulkan: disable autodetection of async_depth Lynne 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 02/12] vulkan: add ff_vk_create_imageview Lynne 2025-02-24 8:04 ` Lynne [this message] 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 04/12] vulkan_decode: support software-defined decoders Lynne 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 05/12] vulkan_decode: support multiple image views Lynne 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 06/12] hwcontext_vulkan: enable read/write without storage Lynne 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 07/12] vulkan: workaround BGR storage image undefined behaviour Lynne 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 08/12] ffv1enc_vulkan: refactor shaders slightly to support sharing Lynne 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 09/12] vulkan: unify handling of BGR and simplify ffv1_rct Lynne 2025-02-24 8:04 ` [FFmpeg-devel] [PATCH v2 10/12] ffv1dec: add support for hwaccels Lynne 2025-02-24 8:05 ` [FFmpeg-devel] [PATCH v2 11/12] ffv1dec: reference the current packet into the main context Lynne 2025-02-24 8:05 ` [FFmpeg-devel] [PATCH v2 12/12] ffv1dec_vulkan: add a Vulkan compute-based hardware decoding implementation Lynne
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250224080434.5632-3-dev@lynne.ee \ --to=dev@lynne.ee \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git