Part of #136993. Share as much of the ShaderCompiler implementations as possible. Remove the ShaderCompiler/ShaderCompilerGeneric split and make most of its functions non virtual. Move the `get_compiler` function from `Context` to `GPUBackend` and creation/deletion to `GPUBackend::init/delete_resources`. Add a `batch_cancel` function to `ShaderCompiler` (needed for the GPUPass refactor). As a nice extra, the multithreaded OpenGL compilation has become faster too. The barbershop materials + EEVEE static shaders have gone from 27s to 22s. I have not observed any performance difference on Vulkan or Metal. Pull Request: https://projects.blender.org/blender/blender/pulls/136676
52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
/* SPDX-FileCopyrightText: 2025 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "GPU_worker.hh"
|
|
|
|
namespace blender::gpu {
|
|
|
|
GPUWorker::GPUWorker(uint32_t threads_count,
|
|
ContextType context_type,
|
|
std::function<void()> run_cb)
|
|
{
|
|
for (int i : IndexRange(threads_count)) {
|
|
UNUSED_VARS(i);
|
|
std::shared_ptr<GPUSecondaryContext> thread_context =
|
|
context_type == ContextType::PerThread ? std::make_shared<GPUSecondaryContext>() : nullptr;
|
|
threads_.append(std::make_unique<std::thread>([=]() { this->run(thread_context, run_cb); }));
|
|
}
|
|
}
|
|
|
|
GPUWorker::~GPUWorker()
|
|
{
|
|
terminate_ = true;
|
|
condition_var_.notify_all();
|
|
for (std::unique_ptr<std::thread> &thread : threads_) {
|
|
thread->join();
|
|
}
|
|
}
|
|
|
|
void GPUWorker::run(std::shared_ptr<GPUSecondaryContext> context, std::function<void()> run_cb)
|
|
{
|
|
if (context) {
|
|
context->activate();
|
|
}
|
|
|
|
/* Loop until we get the terminate signal. */
|
|
while (!terminate_) {
|
|
{
|
|
/* Wait until wake_up() */
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
condition_var_.wait(lock);
|
|
}
|
|
if (terminate_) {
|
|
continue;
|
|
}
|
|
|
|
run_cb();
|
|
}
|
|
}
|
|
|
|
} // namespace blender::gpu
|