Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: add shader_cache_dir option
@ 2025-05-14 17:22 Niklas Haas
  2025-05-15 11:08 ` Andreas Rheinhardt
  0 siblings, 1 reply; 3+ messages in thread
From: Niklas Haas @ 2025-05-14 17:22 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Niklas Haas

From: Niklas Haas <git@haasn.dev>

Useful to speed up shader compilation. May significantly lower startup
times, in particular with large or complex shaders.

Sponsored-by: nxtedition
---
 doc/filters.texi            |  5 +++++
 libavfilter/vf_libplacebo.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/doc/filters.texi b/doc/filters.texi
index 679b71f290..66cb3e6c20 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -16307,6 +16307,11 @@ as a list of @var{key}=@var{value} pairs separated by ':'. The following example
 shows how to configure a custom filter kernel ("EWA LanczosSharp") and use it
 to double the input image resolution:
 
+@item shader_cache_dir
+If set to the path of a directory that exists, libplacebo will store and use
+cached shader objects in this directory. This cache is not cleaned up
+automatically.
+
 @example
 -vf "libplacebo=w=iw*2:h=ih*2:extra_opts='upscaler=custom\:upscaler_preset=ewa_lanczos\:upscaler_blur=0.9812505644269356'"
 @end example
diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
index 86e1f43dea..ca7d9e253a 100644
--- a/libavfilter/vf_libplacebo.c
+++ b/libavfilter/vf_libplacebo.c
@@ -195,6 +195,11 @@ typedef struct LibplaceboContext {
     int color_trc;
     AVDictionary *extra_opts;
 
+#if PL_API_VER >= 351
+    pl_cache cache;
+    char *shader_cache_dir;
+#endif
+
     int have_hwdevice;
 
     /* pl_render_params */
@@ -522,6 +527,21 @@ static int libplacebo_init(AVFilterContext *avctx)
         return AVERROR(ENOMEM);
     }
 
+#if PL_API_VER >= 351
+    if (s->shader_cache_dir && s->shader_cache_dir[0]) {
+        s->cache = pl_cache_create(pl_cache_params(
+            .log  = s->log,
+            .get  = pl_cache_get_dir,
+            .set  = pl_cache_set_dir,
+            .priv = s->shader_cache_dir,
+        ));
+        if (!s->cache) {
+            libplacebo_uninit(avctx);
+            return AVERROR(ENOMEM);
+        }
+    }
+#endif
+
     if (s->out_format_string) {
         s->out_format = av_get_pix_fmt(s->out_format_string);
         if (s->out_format == AV_PIX_FMT_NONE) {
@@ -676,6 +696,9 @@ static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwct
     }
 
     s->gpu = s->vulkan->gpu;
+#if PL_API_VER >= 351
+    pl_gpu_set_cache(s->gpu, s->cache);
+#endif
 
     /* Parse the user shaders, if requested */
     if (s->shader_bin_len)
@@ -714,6 +737,9 @@ static void libplacebo_uninit(AVFilterContext *avctx)
         av_freep(&s->inputs);
     }
 
+#if PL_API_VER >= 351
+    pl_cache_destroy(&s->cache);
+#endif
     pl_options_free(&s->opts);
     pl_vulkan_destroy(&s->vulkan);
     pl_log_destroy(&s->log);
@@ -1328,6 +1354,9 @@ static const AVOption libplacebo_options[] = {
     { "fillcolor", "Background fill color", OFFSET(fillcolor), AV_OPT_TYPE_COLOR, {.str = "black@0"}, .flags = DYNAMIC },
     { "corner_rounding", "Corner rounding radius", OFFSET(corner_rounding), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 1.0, .flags = DYNAMIC },
     { "extra_opts", "Pass extra libplacebo-specific options using a :-separated list of key=value pairs", OFFSET(extra_opts), AV_OPT_TYPE_DICT, .flags = DYNAMIC },
+#if PL_API_VER >= 351
+    { "shader_cache_dir",  "Set shader cache directory", OFFSET(shader_cache_dir), AV_OPT_TYPE_STRING, {.str=""}, .flags = STATIC },
+#endif
 
     {"colorspace", "select colorspace", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_SPC_NB-1, DYNAMIC, .unit = "colorspace"},
     {"auto", "keep the same colorspace",  0, AV_OPT_TYPE_CONST, {.i64=-1},                          INT_MIN, INT_MAX, STATIC, .unit = "colorspace"},
-- 
2.49.0

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: add shader_cache_dir option
  2025-05-14 17:22 [FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: add shader_cache_dir option Niklas Haas
@ 2025-05-15 11:08 ` Andreas Rheinhardt
  2025-05-15 12:13   ` Niklas Haas
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Rheinhardt @ 2025-05-15 11:08 UTC (permalink / raw)
  To: ffmpeg-devel

Niklas Haas:
> From: Niklas Haas <git@haasn.dev>
> 
> Useful to speed up shader compilation. May significantly lower startup
> times, in particular with large or complex shaders.
> 
> Sponsored-by: nxtedition
> ---
>  doc/filters.texi            |  5 +++++
>  libavfilter/vf_libplacebo.c | 29 +++++++++++++++++++++++++++++
>  2 files changed, 34 insertions(+)
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 679b71f290..66cb3e6c20 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -16307,6 +16307,11 @@ as a list of @var{key}=@var{value} pairs separated by ':'. The following example
>  shows how to configure a custom filter kernel ("EWA LanczosSharp") and use it
>  to double the input image resolution:
>  
> +@item shader_cache_dir
> +If set to the path of a directory that exists, libplacebo will store and use
> +cached shader objects in this directory. This cache is not cleaned up
> +automatically.
> +
>  @example
>  -vf "libplacebo=w=iw*2:h=ih*2:extra_opts='upscaler=custom\:upscaler_preset=ewa_lanczos\:upscaler_blur=0.9812505644269356'"
>  @end example
> diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
> index 86e1f43dea..ca7d9e253a 100644
> --- a/libavfilter/vf_libplacebo.c
> +++ b/libavfilter/vf_libplacebo.c
> @@ -195,6 +195,11 @@ typedef struct LibplaceboContext {
>      int color_trc;
>      AVDictionary *extra_opts;
>  
> +#if PL_API_VER >= 351
> +    pl_cache cache;
> +    char *shader_cache_dir;
> +#endif
> +
>      int have_hwdevice;
>  
>      /* pl_render_params */
> @@ -522,6 +527,21 @@ static int libplacebo_init(AVFilterContext *avctx)
>          return AVERROR(ENOMEM);
>      }
>  
> +#if PL_API_VER >= 351
> +    if (s->shader_cache_dir && s->shader_cache_dir[0]) {
> +        s->cache = pl_cache_create(pl_cache_params(
> +            .log  = s->log,
> +            .get  = pl_cache_get_dir,
> +            .set  = pl_cache_set_dir,
> +            .priv = s->shader_cache_dir,
> +        ));
> +        if (!s->cache) {
> +            libplacebo_uninit(avctx);
> +            return AVERROR(ENOMEM);
> +        }
> +    }
> +#endif
> +
>      if (s->out_format_string) {
>          s->out_format = av_get_pix_fmt(s->out_format_string);
>          if (s->out_format == AV_PIX_FMT_NONE) {
> @@ -676,6 +696,9 @@ static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwct
>      }
>  
>      s->gpu = s->vulkan->gpu;
> +#if PL_API_VER >= 351
> +    pl_gpu_set_cache(s->gpu, s->cache);
> +#endif
>  
>      /* Parse the user shaders, if requested */
>      if (s->shader_bin_len)
> @@ -714,6 +737,9 @@ static void libplacebo_uninit(AVFilterContext *avctx)
>          av_freep(&s->inputs);
>      }
>  
> +#if PL_API_VER >= 351
> +    pl_cache_destroy(&s->cache);
> +#endif
>      pl_options_free(&s->opts);
>      pl_vulkan_destroy(&s->vulkan);
>      pl_log_destroy(&s->log);
> @@ -1328,6 +1354,9 @@ static const AVOption libplacebo_options[] = {
>      { "fillcolor", "Background fill color", OFFSET(fillcolor), AV_OPT_TYPE_COLOR, {.str = "black@0"}, .flags = DYNAMIC },
>      { "corner_rounding", "Corner rounding radius", OFFSET(corner_rounding), AV_OPT_TYPE_FLOAT, {.dbl = 0.0}, 0.0, 1.0, .flags = DYNAMIC },
>      { "extra_opts", "Pass extra libplacebo-specific options using a :-separated list of key=value pairs", OFFSET(extra_opts), AV_OPT_TYPE_DICT, .flags = DYNAMIC },
> +#if PL_API_VER >= 351
> +    { "shader_cache_dir",  "Set shader cache directory", OFFSET(shader_cache_dir), AV_OPT_TYPE_STRING, {.str=""}, .flags = STATIC },

Why do you set the default to "" instead of NULL? This gives an
allocation even when the option is never set and makes the check for
whether it is set unnecessarily complicated.

> +#endif
>  
>      {"colorspace", "select colorspace", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_SPC_NB-1, DYNAMIC, .unit = "colorspace"},
>      {"auto", "keep the same colorspace",  0, AV_OPT_TYPE_CONST, {.i64=-1},                          INT_MIN, INT_MAX, STATIC, .unit = "colorspace"},

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: add shader_cache_dir option
  2025-05-15 11:08 ` Andreas Rheinhardt
@ 2025-05-15 12:13   ` Niklas Haas
  0 siblings, 0 replies; 3+ messages in thread
From: Niklas Haas @ 2025-05-15 12:13 UTC (permalink / raw)
  To: ffmpeg-devel

On Thu, 15 May 2025 13:08:12 +0200 Andreas Rheinhardt <andreas.rheinhardt@outlook.com> wrote:
> Why do you set the default to "" instead of NULL? This gives an
> allocation even when the option is never set and makes the check for
> whether it is set unnecessarily complicated.

I can change the default to NULL, but I would prefer to keep the check for an
empty string, just to prevent IMO unexpected behavior.

(When the intent is to write cache files to the current working directory, I
think it is better for users to set it to e.g. "./" explicitly.)

>
> > +#endif
> >
> >      {"colorspace", "select colorspace", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64=-1}, -1, AVCOL_SPC_NB-1, DYNAMIC, .unit = "colorspace"},
> >      {"auto", "keep the same colorspace",  0, AV_OPT_TYPE_CONST, {.i64=-1},                          INT_MIN, INT_MAX, STATIC, .unit = "colorspace"},
>
> _______________________________________________
> 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".
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-05-15 12:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-14 17:22 [FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: add shader_cache_dir option Niklas Haas
2025-05-15 11:08 ` Andreas Rheinhardt
2025-05-15 12:13   ` Niklas Haas

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