Fix: Finalize CreateInfos at startup

Prevents race conditions on ShaderCreateInfo::finalize.
Previoulsy fixed by #128281, but it doesn't work for the render thread.
This commit is contained in:
Miguel Pozo
2024-10-01 20:14:56 +02:00
parent e4c802e53e
commit aed1871bd3
5 changed files with 16 additions and 17 deletions

View File

@@ -786,7 +786,7 @@ GPUPass *GPU_generate_pass(GPUMaterial *material,
pass->shader = nullptr;
pass->refcount = 1;
pass->create_info = codegen.create_info;
/* Ensure the CreateInfo is finalized in the main thread. */
/* Finalize before adding the pass to the cache, to prevent race conditions. */
pass->create_info->finalize();
pass->engine = engine;
pass->hash = codegen.hash_get();

View File

@@ -17,7 +17,6 @@
#include "gpu_shader_create_info_private.hh"
#include "BLI_string_ref.hh"
#include "BLI_threads.h"
#include "BLI_vector.hh"
#include "CLG_log.h"
@@ -46,7 +45,6 @@ bool ShaderBuilder::bake_create_infos(const char *name_starts_with_filter)
void ShaderBuilder::init_system()
{
CLG_init();
BLI_threadapi_init();
ghost_system_ = GHOST_CreateSystemBackground();
GPU_backend_ghost_system_set(ghost_system_);
}
@@ -109,7 +107,6 @@ void ShaderBuilder::exit_context()
void ShaderBuilder::exit_system()
{
GHOST_DisposeSystem(ghost_system_);
BLI_threadapi_exit();
CLG_exit();
}

View File

@@ -95,18 +95,13 @@ bool ShaderCreateInfo::is_vulkan_compatible() const
/** \} */
void ShaderCreateInfo::finalize()
void ShaderCreateInfo::finalize(const bool recursive)
{
if (finalized_) {
return;
}
finalized_ = true;
#if 0
/* TODO(Miguel Pozo): This triggers for image renders. */
BLI_assert(BLI_thread_is_main());
#endif
Set<StringRefNull> deps_merged;
validate_vertex_attributes();
@@ -117,8 +112,12 @@ void ShaderCreateInfo::finalize()
const ShaderCreateInfo &info = *reinterpret_cast<const ShaderCreateInfo *>(
gpu_shader_create_info_get(info_name.c_str()));
/* Recursive. */
const_cast<ShaderCreateInfo &>(info).finalize();
if (recursive) {
const_cast<ShaderCreateInfo &>(info).finalize(recursive);
}
else {
BLI_assert(info.finalized_);
}
interface_names_size_ += info.interface_names_size_;
@@ -585,6 +584,10 @@ void gpu_shader_create_info_init()
#endif
}
for (ShaderCreateInfo *info : g_create_infos->values()) {
info->finalize(true);
}
/* TEST */
// gpu_shader_create_info_compile(nullptr);
}

View File

@@ -1268,8 +1268,10 @@ struct ShaderCreateInfo {
* descriptors. This avoids tedious traversal in shader source creation.
* \{ */
/* WARNING: Recursive. */
void finalize();
/* WARNING: Recursive evaluation is not thread safe.
* Non-recursive evaluation expects their dependencies to be already finalized.
* (All statically declared CreateInfos are automatically finalized at startup) */
void finalize(const bool recursive = false);
std::string check_error() const;
bool is_vulkan_compatible() const;

View File

@@ -10,7 +10,6 @@
# include "BLI_fileops.hh"
# include "BLI_hash.hh"
# include "BLI_path_utils.hh"
# include "BLI_threads.h"
# include "CLG_log.h"
# include "GHOST_C-api.h"
# include "GPU_context.hh"
@@ -154,7 +153,6 @@ void GPU_compilation_subprocess_run(const char *subprocess_name)
# endif
CLG_init();
BLI_threadapi_init();
std::string name = subprocess_name;
SharedMemory shared_mem(name, compilation_subprocess_shared_memory_size, false);
@@ -284,7 +282,6 @@ void GPU_compilation_subprocess_run(const char *subprocess_name)
GPU_context_discard(gpu_context);
GHOST_DisposeGPUContext(ghost_system, ghost_context);
GHOST_DisposeSystem(ghost_system);
BLI_threadapi_exit();
}
namespace blender::gpu {