From patchwork Sun Sep 29 09:42:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lynne X-Patchwork-Id: 51923 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:d8ca:0:b0:48e:c0f8:d0de with SMTP id dy10csp1341148vqb; Sun, 29 Sep 2024 02:43:41 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUggZlVCK1Vn9mtoFjphohY28a4oVPH02GxkZppXrUIUZuEUlX7DdReKnCZlKFrrviFCUC2/O2BZLNlWJkZ85KD@gmail.com X-Google-Smtp-Source: AGHT+IFxV4lZt7tkxi54SO6/m+z54UadHOJLp2Gi2SqSAjjaAxNjMBCrNDGiAxbwbqHTWofx2bCZ X-Received: by 2002:a17:907:86a1:b0:a8a:5ff9:bcd1 with SMTP id a640c23a62f3a-a93c4915be0mr970429666b.21.1727603021119; Sun, 29 Sep 2024 02:43:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1727603021; cv=none; d=google.com; s=arc-20240605; b=U8fAISEsywiBvKAvtpXeymiO2Rhk/yjvXA3dd5nhVrEdNfHNZKU67ak1+BUz+KLivc bD+x5BDP81Hd9jlfifGLZvd4rho6t9FUgv7R1xRXZmRr7xZkIdq9C9eAM/2Ge37+XjJs O0ZT2UWUf2XHvUSkVMiMpL0c68+jhSHGMDY8HEA5QJbgtJpv6qELmDnBgkFYCsVygb0Z Wq68XNXMIf/MB1Ji1biqdkgYuDz98heztnYW3i17igV/kJFVPHKJRJnyp7rXiIjWGtC9 x0A7JYivL4pSglPWXCic1nXtGMnxdBK/Vz/W2KGIq1seMZJLVLCZiwUxRgHFwVpdpWdB U9CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:content-transfer-encoding:cc:reply-to:from :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:delivered-to; bh=iZNUUCVLwJbt5dgrrGSR9p6TbAoAq+mzJX16cpfuExo=; fh=nenT92/WZoU6unXd3J6UhGUdod4piddKfVtctNBOh6k=; b=CimcdQj6rrK8L/h82ftRsQTSQb1K78egM88LFrzbW9Eyh0IUKQurmbtTWYHYl79cb2 fWRpOgBv+DPSeINAjJ+3/G9Mg94AW3jzTlBMWR71VXy0lZOlpErNAM4oENbNzpDWsFcM /E8kerDLnmOm2wkS8JdfO8uw/wJTNa3OI9n6Q9AckBqJjhuyWCJfrzK+yeLuSqFOvJhO M3b4AnlcbGPkXgUejyluH/cT0GpIj6bFlohWM8z1ztzAQM5KR4ryeKXFqCF8NG++wQ5C Rc7ErvASumqtCdyxiPUXrMTVxYMnfqlTn82UfAZaIhr9f3wdKPOrvSVlgtn81N1ZN+gE NdLw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a640c23a62f3a-a93c29ac3dfsi434548766b.278.2024.09.29.02.43.40; Sun, 29 Sep 2024 02:43:41 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E43C768DA6B; Sun, 29 Sep 2024 12:43:10 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vidala.lynne.ee (vidala.pars.ee [116.203.72.101]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9816768DA01 for ; Sun, 29 Sep 2024 12:43:00 +0300 (EEST) To: ffmpeg-devel@ffmpeg.org Date: Sun, 29 Sep 2024 11:42:52 +0200 Message-ID: <20240929094256.396352-5-dev@lynne.ee> X-Mailer: git-send-email 2.45.2.753.g447d99e1c3b In-Reply-To: <20240929094256.396352-1-dev@lynne.ee> References: <20240929094256.396352-1-dev@lynne.ee> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/5] vulkan: check if current buffer has finished execution before picking another X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Lynne via ffmpeg-devel From: Lynne Reply-To: FFmpeg development discussions and patches Cc: Lynne Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ucJCO5/5pvtG This saves resources, as dependencies are freed/reclaimed with a lower latency, and provies a speedup. --- libavcodec/vulkan_decode.c | 4 ++-- libavcodec/vulkan_encode.c | 2 +- libavfilter/vf_nlmeans_vulkan.c | 2 +- libavfilter/vulkan_filter.c | 6 +++--- libavutil/hwcontext_vulkan.c | 6 +++--- libavutil/vulkan.c | 15 +++++++++++---- libavutil/vulkan.h | 2 +- libavutil/vulkan_functions.h | 1 + 8 files changed, 23 insertions(+), 15 deletions(-) diff --git a/libavcodec/vulkan_decode.c b/libavcodec/vulkan_decode.c index a8b906a9dd..7d7295e05e 100644 --- a/libavcodec/vulkan_decode.c +++ b/libavcodec/vulkan_decode.c @@ -293,7 +293,7 @@ void ff_vk_decode_flush(AVCodecContext *avctx) }; VkCommandBuffer cmd_buf; - FFVkExecContext *exec = ff_vk_exec_get(&dec->exec_pool); + FFVkExecContext *exec = ff_vk_exec_get(&ctx->s, &dec->exec_pool); int had_submission = exec->had_submission; ff_vk_exec_start(&ctx->s, exec); cmd_buf = exec->buf; @@ -345,7 +345,7 @@ int ff_vk_decode_frame(AVCodecContext *avctx, size_t data_size = FFALIGN(vp->slices_size, ctx->caps.minBitstreamBufferSizeAlignment); - FFVkExecContext *exec = ff_vk_exec_get(&dec->exec_pool); + FFVkExecContext *exec = ff_vk_exec_get(&ctx->s, &dec->exec_pool); /* The current decoding reference has to be bound as an inactive reference */ VkVideoReferenceSlotInfoKHR *cur_vk_ref; diff --git a/libavcodec/vulkan_encode.c b/libavcodec/vulkan_encode.c index d187b7cdd3..6d1743c7d7 100644 --- a/libavcodec/vulkan_encode.c +++ b/libavcodec/vulkan_encode.c @@ -339,7 +339,7 @@ static int vulkan_encode_issue(AVCodecContext *avctx, size_align); /* Start command buffer recording */ - exec = vp->exec = ff_vk_exec_get(&ctx->enc_pool); + exec = vp->exec = ff_vk_exec_get(&ctx->s, &ctx->enc_pool); ff_vk_exec_start(&ctx->s, exec); cmd_buf = exec->buf; diff --git a/libavfilter/vf_nlmeans_vulkan.c b/libavfilter/vf_nlmeans_vulkan.c index 05c752925e..b413194035 100644 --- a/libavfilter/vf_nlmeans_vulkan.c +++ b/libavfilter/vf_nlmeans_vulkan.c @@ -836,7 +836,7 @@ static int nlmeans_vulkan_filter_frame(AVFilterLink *link, AVFrame *in) } /* Execution context */ - exec = ff_vk_exec_get(&s->e); + exec = ff_vk_exec_get(&s->vkctx, &s->e); ff_vk_exec_start(vkctx, exec); /* Dependencies */ diff --git a/libavfilter/vulkan_filter.c b/libavfilter/vulkan_filter.c index 5cee4572e6..8d9b416d6a 100644 --- a/libavfilter/vulkan_filter.c +++ b/libavfilter/vulkan_filter.c @@ -249,7 +249,7 @@ int ff_vk_filter_process_simple(FFVulkanContext *vkctx, FFVkExecPool *e, int nb_img_bar = 0; /* Update descriptors and init the exec context */ - FFVkExecContext *exec = ff_vk_exec_get(e); + FFVkExecContext *exec = ff_vk_exec_get(vkctx, e); ff_vk_exec_start(vkctx, exec); RET(ff_vk_exec_add_dep_frame(vkctx, exec, out_f, @@ -321,7 +321,7 @@ int ff_vk_filter_process_2pass(FFVulkanContext *vkctx, FFVkExecPool *e, int nb_img_bar = 0; /* Update descriptors and init the exec context */ - FFVkExecContext *exec = ff_vk_exec_get(e); + FFVkExecContext *exec = ff_vk_exec_get(vkctx, e); ff_vk_exec_start(vkctx, exec); RET(ff_vk_exec_add_dep_frame(vkctx, exec, in, @@ -409,7 +409,7 @@ int ff_vk_filter_process_Nin(FFVulkanContext *vkctx, FFVkExecPool *e, int nb_img_bar = 0; /* Update descriptors and init the exec context */ - FFVkExecContext *exec = ff_vk_exec_get(e); + FFVkExecContext *exec = ff_vk_exec_get(vkctx, e); ff_vk_exec_start(vkctx, exec); /* Add deps and create temporary imageviews */ diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c index 3a3aff75c0..d6500de677 100644 --- a/libavutil/hwcontext_vulkan.c +++ b/libavutil/hwcontext_vulkan.c @@ -2254,7 +2254,7 @@ static int prepare_frame(AVHWFramesContext *hwfc, FFVkExecPool *ectx, }; VkCommandBuffer cmd_buf; - FFVkExecContext *exec = ff_vk_exec_get(ectx); + FFVkExecContext *exec = ff_vk_exec_get(&p->vkctx, ectx); cmd_buf = exec->buf; ff_vk_exec_start(&p->vkctx, exec); @@ -3191,7 +3191,7 @@ static int vulkan_map_from_drm_frame_sync(AVHWFramesContext *hwfc, AVFrame *dst, } } - exec = ff_vk_exec_get(&fp->compute_exec); + exec = ff_vk_exec_get(&p->vkctx, &fp->compute_exec); cmd_buf = exec->buf; ff_vk_exec_start(&p->vkctx, exec); @@ -4099,7 +4099,7 @@ static int vulkan_transfer_frame(AVHWFramesContext *hwfc, } } - exec = ff_vk_exec_get(&fp->upload_exec); + exec = ff_vk_exec_get(&p->vkctx, &fp->upload_exec); cmd_buf = exec->buf; ff_vk_exec_start(&p->vkctx, exec); diff --git a/libavutil/vulkan.c b/libavutil/vulkan.c index ab2519bf35..849199ab4f 100644 --- a/libavutil/vulkan.c +++ b/libavutil/vulkan.c @@ -482,11 +482,18 @@ VkResult ff_vk_exec_get_query(FFVulkanContext *s, FFVkExecContext *e, pool->qd_size, qf); } -FFVkExecContext *ff_vk_exec_get(FFVkExecPool *pool) +FFVkExecContext *ff_vk_exec_get(FFVulkanContext *s, FFVkExecPool *pool) { - uint32_t idx = pool->idx++; - idx %= pool->pool_size; - return &pool->contexts[idx]; + FFVulkanFunctions *vk = &s->vkfn; + FFVkExecContext *e = &pool->contexts[pool->idx]; + + /* Check if last submission has already finished. + * If so, don't waste resources and reuse the same buffer. */ + if (vk->GetFenceStatus(s->hwctx->act_dev, e->fence) == VK_SUCCESS) + return e; + + pool->idx = (pool->idx + 1) % pool->pool_size; + return &pool->contexts[pool->idx]; } void ff_vk_exec_wait(FFVulkanContext *s, FFVkExecContext *e) diff --git a/libavutil/vulkan.h b/libavutil/vulkan.h index 47684e600d..8d60fae670 100644 --- a/libavutil/vulkan.h +++ b/libavutil/vulkan.h @@ -387,7 +387,7 @@ void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool); /** * Retrieve an execution pool. Threadsafe. */ -FFVkExecContext *ff_vk_exec_get(FFVkExecPool *pool); +FFVkExecContext *ff_vk_exec_get(FFVulkanContext *s, FFVkExecPool *pool); /** * Performs nb_queries queries and returns their results and statuses. diff --git a/libavutil/vulkan_functions.h b/libavutil/vulkan_functions.h index 91dd8b91e0..90e4d0004d 100644 --- a/libavutil/vulkan_functions.h +++ b/libavutil/vulkan_functions.h @@ -110,6 +110,7 @@ typedef enum FFVulkanExtensions { /* Fences */ \ MACRO(1, 1, FF_VK_EXT_NO_FLAG, CreateFence) \ MACRO(1, 1, FF_VK_EXT_NO_FLAG, WaitForFences) \ + MACRO(1, 1, FF_VK_EXT_NO_FLAG, GetFenceStatus) \ MACRO(1, 1, FF_VK_EXT_NO_FLAG, ResetFences) \ MACRO(1, 1, FF_VK_EXT_NO_FLAG, DestroyFence) \ \