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
56 lines
1.4 KiB
C++
56 lines
1.4 KiB
C++
/* SPDX-FileCopyrightText: 2025 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#pragma once
|
|
|
|
#include "BLI_vector.hh"
|
|
#include "GPU_context.hh"
|
|
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
namespace blender::gpu {
|
|
|
|
/* Abstracts the creation and management of secondary threads with GPU contexts.
|
|
* Must be created from the main thread.
|
|
* Threads and their context remain alive until destruction. */
|
|
class GPUWorker {
|
|
private:
|
|
Vector<std::unique_ptr<std::thread>> threads_;
|
|
std::condition_variable condition_var_;
|
|
std::mutex mutex_;
|
|
std::atomic<bool> terminate_ = false;
|
|
|
|
public:
|
|
enum class ContextType {
|
|
/* Use the main GPU context on the worker threads. */
|
|
Main,
|
|
/* Use a different secondary GPU context for each worker thread. */
|
|
PerThread,
|
|
};
|
|
|
|
/**
|
|
* \param threads_count: Number of threads to span.
|
|
* \param context_type: The type of context each thread uses.
|
|
* \param run_cb: The callback function that will be called by a thread on `wake_up()`.
|
|
*/
|
|
GPUWorker(uint32_t threads_count, ContextType context_type, std::function<void()> run_cb);
|
|
~GPUWorker();
|
|
|
|
/* Wake up a single thread. */
|
|
void wake_up()
|
|
{
|
|
condition_var_.notify_one();
|
|
}
|
|
|
|
private:
|
|
void run(std::shared_ptr<GPUSecondaryContext> context, std::function<void()> run_cb);
|
|
};
|
|
|
|
} // namespace blender::gpu
|