Files
test2/source/blender/gpu/vulkan/vk_shader_compiler.hh
Jeroen Bakker e2dddea124 Fix: Vulkan: Thread safe cache folder
Vulkan shader compiler accesses the cache folder via multiple threads.
GHOST part isn't thread safe and can return and overwrite the returned
cache path. This resulted into crashes when performing background
rendering and failing test cases, loading of incorrect shaders etc.

This PR fixes this to cache the cache folder location in the
VKShaderCompiler, which is loaded via the main thread when the vulkan
backend is initialized.

Pull Request: https://projects.blender.org/blender/blender/pulls/133535
2025-01-24 12:20:43 +01:00

66 lines
1.5 KiB
C++

/* SPDX-FileCopyrightText: 2024 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#pragma once
#include "gpu_shader_private.hh"
#include "BLI_map.hh"
#include "BLI_task.h"
#include "BLI_vector.hh"
#include "shaderc/shaderc.hpp"
namespace blender::gpu {
class VKShader;
class VKShaderModule;
/**
* Vulkan shader compiler.
*
* Is used for both single threaded compilation by calling `VKShaderCompiler::compile_module` or
* batch based compilation.
*/
class VKShaderCompiler : public ShaderCompiler {
private:
std::mutex mutex_;
BatchHandle next_batch_handle_ = 1;
struct VKBatch {
Vector<Shader *> shaders;
};
Map<BatchHandle, VKBatch> batches_;
TaskPool *task_pool_ = nullptr;
public:
/**
* Cached path to the cache folder.
*
* GHOST and BKE_appdir are not thread safe. Storing the cache_dir locally to work around
* threading issues.
*/
static std::optional<std::string> cache_dir;
VKShaderCompiler();
virtual ~VKShaderCompiler();
BatchHandle batch_compile(Span<const shader::ShaderCreateInfo *> &infos) override;
bool batch_is_ready(BatchHandle handle) override;
Vector<Shader *> batch_finalize(BatchHandle &handle) override;
static bool compile_module(VKShader &shader,
shaderc_shader_kind stage,
VKShaderModule &shader_module);
static void cache_dir_clear_old();
private:
static void run(TaskPool *__restrict pool, void *task_data);
};
} // namespace blender::gpu