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
This commit is contained in:
Jeroen Bakker
2024-09-16 14:03:14 +02:00
parent b6da18172b
commit a407186dbf
11 changed files with 26 additions and 7 deletions

View File

@@ -10,6 +10,4 @@
void GPU_compilation_subprocess_run(const char *subprocess_name);
void GPU_shader_cache_dir_clear_old();
#endif

View File

@@ -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 */

View File

@@ -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 {}

View File

@@ -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 */

View File

@@ -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();
}
/** \} */
/* -------------------------------------------------------------------- */

View File

@@ -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;

View File

@@ -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{};

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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();