Files
test2/source/blender/gpu/GPU_worker.hh
Miguel Pozo a5ed5dc4bf GPU: Support deferred compilation in ShaderCompilerGeneric
Update the `ShaderCompilerGeneric` to support deferred compilation
using the batch compilation API, so we can get rid of
`drw_manager_shader`.
This approach also allows supporting non-blocking compilation
for static shaders.

This shouldn't cause any behavior changes at the moment, since batch
compilation is not yet used when parallel compilation is disabled.

This adds a `GPUWorker` and a `GPUSecondaryContext` as an easy to use
wrapper for managing secondary GPU contexts.

(Part of #133674)
Pull Request: https://projects.blender.org/blender/blender/pulls/136518
2025-04-07 15:26:25 +02:00

50 lines
1.3 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:
/**
* \param threads_count: Number of threads to span.
* \param share_context: If true, all threads will use the same secondary GPUContext,
* otherwise each thread will have its own unique GPUContext.
* \param run_cb: The callback function that will be called by a thread on `wake_up()`.
*/
GPUWorker(uint32_t threads_count, bool share_context, 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