From 5f830e14ea6e15fb6c17d08d01792b72dd18b609 Mon Sep 17 00:00:00 2001 From: jdotcollins <37229443+jdotcollins@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:35:51 -0700 Subject: [PATCH] vo_gpu{,_next}: drop cached user shaders that leave glsl-shaders Both renderers cache the contents of every file ever listed in --glsl-shaders, keyed by the path string, until the renderer is destroyed. Editing a shader and setting the list again therefore keeps using the old text, even after removing the shader and adding it back. On vo=gpu-next a shader that failed to parse also stays failed under its path until the renderer is recreated, even after the file was fixed. The cache also grows with every path that was ever loaded, which matters for scripts that load edited copies under new names. Cache entries whose path is no longer in the list are now dropped when the list is applied, after the hooks were rebuilt from the new list. Removing a shader and adding it again (for example with 'change-list glsl-shaders toggle') now reads the file again; shaders that stay in the list keep their cached contents, as before. Fixes #18919. --- DOCS/man/options.rst | 4 ++++ video/out/gpu/video.c | 26 ++++++++++++++++++++++++++ video/out/gpu_next/video.c | 26 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/DOCS/man/options.rst b/DOCS/man/options.rst index bc357b5a28813..e2d48e09551a6 100644 --- a/DOCS/man/options.rst +++ b/DOCS/man/options.rst @@ -6653,6 +6653,10 @@ them. and overwrites the internal list with it. The latter is a path list option (see `List Options`_ for details). + A shader file is read when it is added to the list. A file that stays in + the list is not read again; to load an edited file, remove it from the + list and add it again. + .. warning:: The syntax is not stable yet and may change any time. diff --git a/video/out/gpu/video.c b/video/out/gpu/video.c index 6122e5142268c..489e02b88f54f 100644 --- a/video/out/gpu/video.c +++ b/video/out/gpu/video.c @@ -2300,6 +2300,31 @@ static void load_user_shaders(struct gl_video *p, char **shaders) } } +// Drop cached shader files that are no longer in the list, so that removing a +// shader and adding it again reads the file again, and so that the cache does +// not grow without bound. Must run after the hooks were rebuilt from the list, +// because the hooks point into the cached file contents. +static void evict_unused_files(struct gl_video *p, char **shaders) +{ + int kept = 0; + for (int n = 0; n < p->num_files; n++) { + bool used = false; + for (int i = 0; shaders && shaders[i]; i++) { + if (strcmp(p->files[n].path, shaders[i]) == 0) { + used = true; + break; + } + } + if (used) { + p->files[kept++] = p->files[n]; + } else { + talloc_free(p->files[n].path); + talloc_free(p->files[n].body.start); + } + } + p->num_files = kept; +} + static void gl_video_setup_hooks(struct gl_video *p) { gl_video_reset_hooks(p); @@ -2321,6 +2346,7 @@ static void gl_video_setup_hooks(struct gl_video *p) } load_user_shaders(p, p->opts.user_shaders); + evict_unused_files(p, p->opts.user_shaders); } // sample from video textures, set "color" variable to yuv value diff --git a/video/out/gpu_next/video.c b/video/out/gpu_next/video.c index 1f07c0e5d4e45..249e0dc0eca70 100644 --- a/video/out/gpu_next/video.c +++ b/video/out/gpu_next/video.c @@ -2568,6 +2568,31 @@ static const struct pl_hook *load_hook(struct mppl_video *p, const char *path) return hook; } +// Destroy cached hooks whose shader is no longer in the list, so that removing +// a shader and adding it again reads the file again (also after it failed to +// parse), and so that the cache does not grow without bound. Must run after the +// renderer's hook list was rebuilt from the current list. +static void evict_unused_hooks(struct mppl_video *p, char **shaders) +{ + int kept = 0; + for (int i = 0; i < p->num_user_hooks; i++) { + bool used = false; + for (int n = 0; shaders && shaders[n]; n++) { + if (strcmp(p->user_hooks[i].path, shaders[n]) == 0) { + used = true; + break; + } + } + if (used) { + p->user_hooks[kept++] = p->user_hooks[i]; + } else { + pl_mpv_user_shader_destroy(&p->user_hooks[i].hook); + talloc_free(p->user_hooks[i].path); + } + } + p->num_user_hooks = kept; +} + static void update_icc_opts(struct mppl_video *p, const struct mp_icc_opts *opts) { if (!opts) @@ -2867,6 +2892,7 @@ static void update_render_options(struct mppl_video *p) } pars->params.hooks = p->hooks; + evict_unused_hooks(p, opts->user_shaders); MP_DBG(p, "Render options updated, flushing renderer cache.\n"); p->flush_cache = p->paused || !p->next_opts->inter_preserve;