From a407186dbf5774ee9ee3300920a769efd8431f99 Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Mon, 16 Sep 2024 14:03:14 +0200 Subject: [PATCH] GPU: Make shader cache clearing backend independent Parallel shader compilation introduced `GPU_shader_cache_dir_clear_old`. The implementation was specific to OpenGL and could not be overwritten by other backends. This PR improves the implementation so the backend can have its own implementation. This is needed for upcoming changes to the Vulkan backend where we want to use similar mechanisms to speed up shader compilation and caching. Pull Request: https://projects.blender.org/blender/blender/pulls/127680 --- source/blender/gpu/GPU_compilation_subprocess.hh | 2 -- source/blender/gpu/GPU_shader.hh | 2 ++ source/blender/gpu/dummy/dummy_backend.hh | 1 + source/blender/gpu/intern/gpu_backend.hh | 1 + source/blender/gpu/intern/gpu_shader.cc | 5 +++++ source/blender/gpu/metal/mtl_backend.hh | 1 + source/blender/gpu/opengl/gl_backend.hh | 8 ++++++++ source/blender/gpu/opengl/gl_compilation_subprocess.cc | 4 +++- source/blender/gpu/opengl/gl_compilation_subprocess.hh | 2 ++ source/blender/gpu/vulkan/vk_backend.hh | 2 ++ source/blender/windowmanager/intern/wm_init_exit.cc | 5 +---- 11 files changed, 26 insertions(+), 7 deletions(-) diff --git a/source/blender/gpu/GPU_compilation_subprocess.hh b/source/blender/gpu/GPU_compilation_subprocess.hh index eeb769fb7fd..64ca388ccec 100644 --- a/source/blender/gpu/GPU_compilation_subprocess.hh +++ b/source/blender/gpu/GPU_compilation_subprocess.hh @@ -10,6 +10,4 @@ void GPU_compilation_subprocess_run(const char *subprocess_name); -void GPU_shader_cache_dir_clear_old(); - #endif diff --git a/source/blender/gpu/GPU_shader.hh b/source/blender/gpu/GPU_shader.hh index d78a0825c30..7f6dc0eb8d2 100644 --- a/source/blender/gpu/GPU_shader.hh +++ b/source/blender/gpu/GPU_shader.hh @@ -388,6 +388,8 @@ int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin); */ void GPU_shader_compile_static(); +void GPU_shader_cache_dir_clear_old(); + /** DEPRECATED: Use hard-coded buffer location instead. */ enum GPUUniformBlockBuiltin { GPU_UNIFORM_BLOCK_VIEW = 0, /* viewBlock */ diff --git a/source/blender/gpu/dummy/dummy_backend.hh b/source/blender/gpu/dummy/dummy_backend.hh index 566bb679c1d..258f58b5f42 100644 --- a/source/blender/gpu/dummy/dummy_backend.hh +++ b/source/blender/gpu/dummy/dummy_backend.hh @@ -93,6 +93,7 @@ class DummyBackend : public GPUBackend { { return new DummyVertexBuffer; } + void shader_cache_dir_clear_old() override {} void render_begin() override {} void render_end() override {} void render_step() override {} diff --git a/source/blender/gpu/intern/gpu_backend.hh b/source/blender/gpu/intern/gpu_backend.hh index fe938ebb6df..0b1df0e46d9 100644 --- a/source/blender/gpu/intern/gpu_backend.hh +++ b/source/blender/gpu/intern/gpu_backend.hh @@ -55,6 +55,7 @@ class GPUBackend { virtual UniformBuf *uniformbuf_alloc(size_t size, const char *name) = 0; virtual StorageBuf *storagebuf_alloc(size_t size, GPUUsageType usage, const char *name) = 0; virtual VertBuf *vertbuf_alloc() = 0; + virtual void shader_cache_dir_clear_old() = 0; /* Render Frame Coordination -- * Used for performing per-frame actions globally */ diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc index cd79392ced3..616ea6ac67c 100644 --- a/source/blender/gpu/intern/gpu_shader.cc +++ b/source/blender/gpu/intern/gpu_shader.cc @@ -352,6 +352,11 @@ void GPU_shader_compile_static() gpu_shader_create_info_compile(""); } +void GPU_shader_cache_dir_clear_old() +{ + GPUBackend::get()->shader_cache_dir_clear_old(); +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/gpu/metal/mtl_backend.hh b/source/blender/gpu/metal/mtl_backend.hh index b22bdf8f711..0bcda681f19 100644 --- a/source/blender/gpu/metal/mtl_backend.hh +++ b/source/blender/gpu/metal/mtl_backend.hh @@ -71,6 +71,7 @@ class MTLBackend : public GPUBackend { UniformBuf *uniformbuf_alloc(size_t size, const char *name) override; StorageBuf *storagebuf_alloc(size_t size, GPUUsageType usage, const char *name) override; VertBuf *vertbuf_alloc() override; + void shader_cache_dir_clear_old() override {} /* Render Frame Coordination. */ void render_begin() override; diff --git a/source/blender/gpu/opengl/gl_backend.hh b/source/blender/gpu/opengl/gl_backend.hh index 13dfd9a3774..5d5417dae45 100644 --- a/source/blender/gpu/opengl/gl_backend.hh +++ b/source/blender/gpu/opengl/gl_backend.hh @@ -17,6 +17,7 @@ #endif #include "gl_batch.hh" +#include "gl_compilation_subprocess.hh" #include "gl_compute.hh" #include "gl_context.hh" #include "gl_drawlist.hh" @@ -165,6 +166,13 @@ class GLBackend : public GPUBackend { glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0); } + void shader_cache_dir_clear_old() override + { +#if BLI_SUBPROCESS_SUPPORT + GL_shader_cache_dir_clear_old(); +#endif + } + /* Render Frame Coordination */ void render_begin() override{}; void render_end() override{}; diff --git a/source/blender/gpu/opengl/gl_compilation_subprocess.cc b/source/blender/gpu/opengl/gl_compilation_subprocess.cc index 8f0de0b12c6..92634e40bc3 100644 --- a/source/blender/gpu/opengl/gl_compilation_subprocess.cc +++ b/source/blender/gpu/opengl/gl_compilation_subprocess.cc @@ -283,7 +283,8 @@ void GPU_compilation_subprocess_run(const char *subprocess_name) GHOST_DisposeSystem(ghost_system); } -void GPU_shader_cache_dir_clear_old() +namespace blender::gpu { +void GL_shader_cache_dir_clear_old() { std::string cache_dir = cache_dir_get(); @@ -302,5 +303,6 @@ void GPU_shader_cache_dir_clear_old() } BLI_filelist_free(entries, dir_len); } +} // namespace blender::gpu #endif diff --git a/source/blender/gpu/opengl/gl_compilation_subprocess.hh b/source/blender/gpu/opengl/gl_compilation_subprocess.hh index 0cdbf1db34d..4ccad743906 100644 --- a/source/blender/gpu/opengl/gl_compilation_subprocess.hh +++ b/source/blender/gpu/opengl/gl_compilation_subprocess.hh @@ -40,6 +40,8 @@ struct ShaderBinaryHeader { static_assert(sizeof(ShaderBinaryHeader) == compilation_subprocess_shared_memory_size, "Size must match the shared memory size"); +void GL_shader_cache_dir_clear_old(); + } // namespace blender::gpu #endif diff --git a/source/blender/gpu/vulkan/vk_backend.hh b/source/blender/gpu/vulkan/vk_backend.hh index 38863a14e10..dc7f8d590ab 100644 --- a/source/blender/gpu/vulkan/vk_backend.hh +++ b/source/blender/gpu/vulkan/vk_backend.hh @@ -76,6 +76,8 @@ class VKBackend : public GPUBackend { StorageBuf *storagebuf_alloc(size_t size, GPUUsageType usage, const char *name) override; VertBuf *vertbuf_alloc() override; + void shader_cache_dir_clear_old() override {} + /* Render Frame Coordination -- * Used for performing per-frame actions globally */ void render_begin() override; diff --git a/source/blender/windowmanager/intern/wm_init_exit.cc b/source/blender/windowmanager/intern/wm_init_exit.cc index b3bec2e706e..7391179ab45 100644 --- a/source/blender/windowmanager/intern/wm_init_exit.cc +++ b/source/blender/windowmanager/intern/wm_init_exit.cc @@ -656,6 +656,7 @@ void WM_exit_ex(bContext *C, const bool do_python_exit, const bool do_user_exit_ DRW_gpu_context_enable_ex(false); UI_exit(); GPU_pass_cache_free(); + GPU_shader_cache_dir_clear_old(); GPU_exit(); DRW_gpu_context_disable_ex(false); DRW_gpu_context_destroy(); @@ -691,10 +692,6 @@ void WM_exit_ex(bContext *C, const bool do_python_exit, const bool do_user_exit_ BKE_tempdir_session_purge(); -#if defined(WITH_OPENGL_BACKEND) && BLI_SUBPROCESS_SUPPORT - GPU_shader_cache_dir_clear_old(); -#endif - /* Logging cannot be called after exiting (#CLOG_INFO, #CLOG_WARN etc will crash). * So postpone exiting until other sub-systems that may use logging have shut down. */ CLG_exit();