2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2016 by Mike Erwin. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-07-17 14:46:44 +02:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup gpu
|
2018-07-17 14:46:44 +02:00
|
|
|
*
|
|
|
|
|
* Manage GL vertex array IDs in a thread-safe way
|
|
|
|
|
* Use these instead of glGenBuffers & its friends
|
|
|
|
|
* - alloc must be called from a thread that is bound
|
|
|
|
|
* to the context that will be used for drawing with
|
2021-05-06 08:09:05 +10:00
|
|
|
* this VAO.
|
2018-07-17 14:46:44 +02:00
|
|
|
* - free can be called from any thread
|
|
|
|
|
*/
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2024-09-06 08:28:41 +02:00
|
|
|
#include "BKE_global.hh"
|
|
|
|
|
|
2018-07-19 15:48:13 +02:00
|
|
|
#include "BLI_assert.h"
|
2025-04-07 15:26:25 +02:00
|
|
|
#include "BLI_threads.h"
|
2024-09-06 08:28:41 +02:00
|
|
|
#include "BLI_vector_set.hh"
|
2018-07-19 15:48:13 +02:00
|
|
|
|
2025-04-07 15:26:25 +02:00
|
|
|
#include "DNA_userdef_types.h"
|
|
|
|
|
|
|
|
|
|
#include "GHOST_C-api.h"
|
2025-01-26 20:08:03 +01:00
|
|
|
#include "GHOST_Types.h"
|
|
|
|
|
|
2024-03-23 01:24:18 +01:00
|
|
|
#include "GPU_context.hh"
|
2018-07-19 15:48:13 +02:00
|
|
|
|
2024-03-26 03:06:25 +01:00
|
|
|
#include "GPU_batch.hh"
|
2025-05-22 17:53:22 +02:00
|
|
|
#include "GPU_pass.hh"
|
2020-08-07 17:00:28 +02:00
|
|
|
#include "gpu_backend.hh"
|
2020-08-08 03:01:45 +02:00
|
|
|
#include "gpu_context_private.hh"
|
2024-03-23 01:24:18 +01:00
|
|
|
#include "gpu_matrix_private.hh"
|
|
|
|
|
#include "gpu_private.hh"
|
2024-07-19 15:48:00 +02:00
|
|
|
#include "gpu_shader_private.hh"
|
2018-07-19 15:48:13 +02:00
|
|
|
|
2025-04-08 14:10:01 +02:00
|
|
|
#ifdef WITH_VULKAN_BACKEND
|
|
|
|
|
# include "vk_backend.hh"
|
|
|
|
|
#endif
|
2020-08-07 17:00:28 +02:00
|
|
|
#ifdef WITH_OPENGL_BACKEND
|
|
|
|
|
# include "gl_backend.hh"
|
|
|
|
|
# include "gl_context.hh"
|
|
|
|
|
#endif
|
2022-03-22 12:38:28 +01:00
|
|
|
#ifdef WITH_METAL_BACKEND
|
|
|
|
|
# include "mtl_backend.hh"
|
|
|
|
|
#endif
|
2023-08-15 14:15:12 +02:00
|
|
|
#include "dummy_backend.hh"
|
2020-08-07 17:00:28 +02:00
|
|
|
|
2025-04-09 21:37:23 +02:00
|
|
|
#include "draw_debug.hh"
|
|
|
|
|
|
2018-02-19 23:50:52 +01:00
|
|
|
#include <mutex>
|
|
|
|
|
|
2020-08-08 01:18:18 +02:00
|
|
|
using namespace blender::gpu;
|
2018-07-19 15:48:13 +02:00
|
|
|
|
2020-11-06 17:49:09 +01:00
|
|
|
static thread_local Context *active_ctx = nullptr;
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2025-05-07 04:53:16 +02:00
|
|
|
static blender::Mutex backend_users_mutex;
|
2022-07-15 18:58:35 +02:00
|
|
|
static int num_backend_users = 0;
|
|
|
|
|
|
|
|
|
|
static void gpu_backend_create();
|
|
|
|
|
static void gpu_backend_discard();
|
|
|
|
|
|
2020-08-08 01:18:18 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
2020-09-08 04:12:12 +02:00
|
|
|
/** \name gpu::Context methods
|
2020-08-08 01:18:18 +02:00
|
|
|
* \{ */
|
2018-07-19 15:48:13 +02:00
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
namespace blender::gpu {
|
|
|
|
|
|
2022-09-01 22:22:32 +02:00
|
|
|
int Context::context_counter = 0;
|
2020-09-08 04:12:12 +02:00
|
|
|
Context::Context()
|
2020-08-08 01:18:18 +02:00
|
|
|
{
|
|
|
|
|
thread_ = pthread_self();
|
|
|
|
|
is_active_ = false;
|
|
|
|
|
matrix_state = GPU_matrix_state_create();
|
2025-02-11 18:58:46 +01:00
|
|
|
texture_pool = new TexturePool();
|
2022-09-01 22:22:32 +02:00
|
|
|
|
|
|
|
|
context_id = Context::context_counter;
|
|
|
|
|
Context::context_counter++;
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
Context::~Context()
|
2018-07-19 15:48:13 +02:00
|
|
|
{
|
2025-03-04 16:54:05 +01:00
|
|
|
/* Derived class should have called free_resources already. */
|
2025-01-06 11:32:02 +01:00
|
|
|
BLI_assert(front_left == nullptr);
|
|
|
|
|
BLI_assert(back_left == nullptr);
|
|
|
|
|
BLI_assert(front_right == nullptr);
|
|
|
|
|
BLI_assert(back_right == nullptr);
|
2025-03-04 16:54:05 +01:00
|
|
|
BLI_assert(texture_pool == nullptr);
|
2025-01-06 11:32:02 +01:00
|
|
|
|
2020-08-08 01:18:18 +02:00
|
|
|
GPU_matrix_state_discard(matrix_state);
|
2025-02-12 17:24:31 +01:00
|
|
|
GPU_BATCH_DISCARD_SAFE(procedural_points_batch);
|
|
|
|
|
GPU_BATCH_DISCARD_SAFE(procedural_lines_batch);
|
|
|
|
|
GPU_BATCH_DISCARD_SAFE(procedural_triangles_batch);
|
|
|
|
|
GPU_BATCH_DISCARD_SAFE(procedural_triangle_strips_batch);
|
|
|
|
|
GPU_VERTBUF_DISCARD_SAFE(dummy_vbo);
|
2020-08-17 00:34:06 +02:00
|
|
|
delete state_manager;
|
2025-01-06 11:32:02 +01:00
|
|
|
delete imm;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 16:54:05 +01:00
|
|
|
void Context::free_resources()
|
2025-01-06 11:32:02 +01:00
|
|
|
{
|
2020-08-31 15:13:26 +02:00
|
|
|
delete front_left;
|
|
|
|
|
delete back_left;
|
|
|
|
|
delete front_right;
|
|
|
|
|
delete back_right;
|
2025-01-06 11:32:02 +01:00
|
|
|
front_left = nullptr;
|
|
|
|
|
back_left = nullptr;
|
|
|
|
|
front_right = nullptr;
|
|
|
|
|
back_right = nullptr;
|
2025-03-04 16:54:05 +01:00
|
|
|
|
|
|
|
|
delete texture_pool;
|
|
|
|
|
texture_pool = nullptr;
|
2020-08-08 01:18:18 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-06 13:18:48 +01:00
|
|
|
bool Context::is_active_on_thread()
|
2020-08-08 01:18:18 +02:00
|
|
|
{
|
|
|
|
|
return (this == active_ctx) && pthread_equal(pthread_self(), thread_);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-06 13:18:48 +01:00
|
|
|
Context *Context::get()
|
2020-09-08 04:12:12 +02:00
|
|
|
{
|
|
|
|
|
return active_ctx;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 17:24:31 +01:00
|
|
|
VertBuf *Context::dummy_vbo_get()
|
2024-11-27 17:37:04 +01:00
|
|
|
{
|
2025-02-12 17:24:31 +01:00
|
|
|
if (this->dummy_vbo) {
|
|
|
|
|
return this->dummy_vbo;
|
2024-11-27 17:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO(fclem): get rid of this dummy VBO. */
|
|
|
|
|
GPUVertFormat format = {0};
|
|
|
|
|
GPU_vertformat_attr_add(&format, "dummy", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
|
2025-02-12 17:24:31 +01:00
|
|
|
this->dummy_vbo = GPU_vertbuf_create_with_format(format);
|
|
|
|
|
GPU_vertbuf_data_alloc(*this->dummy_vbo, 1);
|
|
|
|
|
return this->dummy_vbo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Batch *Context::procedural_points_batch_get()
|
|
|
|
|
{
|
|
|
|
|
if (procedural_points_batch) {
|
|
|
|
|
return procedural_points_batch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
procedural_points_batch = GPU_batch_create(GPU_PRIM_POINTS, dummy_vbo_get(), nullptr);
|
|
|
|
|
return procedural_points_batch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Batch *Context::procedural_lines_batch_get()
|
|
|
|
|
{
|
|
|
|
|
if (procedural_lines_batch) {
|
|
|
|
|
return procedural_lines_batch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
procedural_lines_batch = GPU_batch_create(GPU_PRIM_LINES, dummy_vbo_get(), nullptr);
|
|
|
|
|
return procedural_lines_batch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Batch *Context::procedural_triangles_batch_get()
|
|
|
|
|
{
|
|
|
|
|
if (procedural_triangles_batch) {
|
|
|
|
|
return procedural_triangles_batch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
procedural_triangles_batch = GPU_batch_create(GPU_PRIM_TRIS, dummy_vbo_get(), nullptr);
|
|
|
|
|
return procedural_triangles_batch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Batch *Context::procedural_triangle_strips_batch_get()
|
|
|
|
|
{
|
|
|
|
|
if (procedural_triangle_strips_batch) {
|
|
|
|
|
return procedural_triangle_strips_batch;
|
|
|
|
|
}
|
2024-11-27 17:37:04 +01:00
|
|
|
|
2025-02-12 17:24:31 +01:00
|
|
|
procedural_triangle_strips_batch = GPU_batch_create(
|
|
|
|
|
GPU_PRIM_TRI_STRIP, dummy_vbo_get(), nullptr);
|
|
|
|
|
return procedural_triangle_strips_batch;
|
2024-11-27 17:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
} // namespace blender::gpu
|
|
|
|
|
|
2020-08-08 01:18:18 +02:00
|
|
|
/** \} */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-08 01:18:18 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2022-09-22 17:27:51 +02:00
|
|
|
GPUContext *GPU_context_create(void *ghost_window, void *ghost_context)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2022-07-15 18:58:35 +02:00
|
|
|
{
|
|
|
|
|
std::scoped_lock lock(backend_users_mutex);
|
|
|
|
|
if (num_backend_users == 0) {
|
|
|
|
|
/* Automatically create backend when first context is created. */
|
|
|
|
|
gpu_backend_create();
|
|
|
|
|
}
|
|
|
|
|
num_backend_users++;
|
|
|
|
|
}
|
2020-08-07 17:00:28 +02:00
|
|
|
|
2022-09-22 17:27:51 +02:00
|
|
|
Context *ctx = GPUBackend::get()->context_alloc(ghost_window, ghost_context);
|
2020-08-06 04:30:38 +02:00
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
GPU_context_active_set(wrap(ctx));
|
2025-04-09 21:37:23 +02:00
|
|
|
|
|
|
|
|
blender::draw::DebugDraw::get().acquire();
|
|
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
return wrap(ctx);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
void GPU_context_discard(GPUContext *ctx_)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2020-09-08 04:12:12 +02:00
|
|
|
Context *ctx = unwrap(ctx_);
|
2025-02-21 14:35:10 +01:00
|
|
|
BLI_assert(active_ctx == ctx);
|
2025-02-21 13:47:21 +01:00
|
|
|
|
2025-04-09 21:37:23 +02:00
|
|
|
blender::draw::DebugDraw::get().release();
|
|
|
|
|
|
2025-02-21 14:35:10 +01:00
|
|
|
GPUBackend *backend = GPUBackend::get();
|
2025-02-21 13:47:21 +01:00
|
|
|
/* Flush any remaining printf while making sure we are inside render boundaries. */
|
2025-02-21 14:35:10 +01:00
|
|
|
backend->render_begin();
|
|
|
|
|
printf_end(ctx);
|
|
|
|
|
backend->render_end();
|
2025-02-21 13:47:21 +01:00
|
|
|
|
2018-02-22 14:31:10 +01:00
|
|
|
delete ctx;
|
2020-11-06 17:49:09 +01:00
|
|
|
active_ctx = nullptr;
|
2022-07-15 18:58:35 +02:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::scoped_lock lock(backend_users_mutex);
|
|
|
|
|
num_backend_users--;
|
|
|
|
|
BLI_assert(num_backend_users >= 0);
|
|
|
|
|
if (num_backend_users == 0) {
|
|
|
|
|
/* Discard backend when last context is discarded. */
|
|
|
|
|
gpu_backend_discard();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
void GPU_context_active_set(GPUContext *ctx_)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2020-09-08 04:12:12 +02:00
|
|
|
Context *ctx = unwrap(ctx_);
|
|
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
if (active_ctx) {
|
2025-05-20 12:43:58 +02:00
|
|
|
GPU_shader_unbind();
|
2020-08-08 01:18:18 +02:00
|
|
|
active_ctx->deactivate();
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2020-08-08 01:18:18 +02:00
|
|
|
|
|
|
|
|
active_ctx = ctx;
|
|
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
if (ctx) {
|
2020-08-08 01:18:18 +02:00
|
|
|
ctx->activate();
|
2025-05-22 11:54:29 +02:00
|
|
|
/* It can happen that the previous context drew with a different colorspace.
|
|
|
|
|
* In the case where the new context is drawing with the same shader that was previously bound
|
|
|
|
|
* (shader binding optimization), the uniform would not be set again because the dirty flag
|
|
|
|
|
* would not have been set (since the color space of this new context never changed). The
|
|
|
|
|
* shader would reuse the same colorspace as the previous context framebuffer (see #137855). */
|
|
|
|
|
ctx->shader_builtin_srgb_is_dirty = true;
|
2018-02-19 23:50:52 +01:00
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2021-12-08 00:31:20 -05:00
|
|
|
GPUContext *GPU_context_active_get()
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2020-09-08 04:12:12 +02:00
|
|
|
return wrap(Context::get());
|
2019-08-14 15:27:10 +02:00
|
|
|
}
|
2020-08-05 15:26:49 +02:00
|
|
|
|
2022-06-27 11:41:04 +02:00
|
|
|
void GPU_context_begin_frame(GPUContext *ctx)
|
|
|
|
|
{
|
|
|
|
|
blender::gpu::Context *_ctx = unwrap(ctx);
|
|
|
|
|
if (_ctx) {
|
|
|
|
|
_ctx->begin_frame();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPU_context_end_frame(GPUContext *ctx)
|
|
|
|
|
{
|
|
|
|
|
blender::gpu::Context *_ctx = unwrap(ctx);
|
|
|
|
|
if (_ctx) {
|
|
|
|
|
_ctx->end_frame();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 01:18:18 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Main context global mutex
|
|
|
|
|
*
|
|
|
|
|
* Used to avoid crash on some old drivers.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2025-05-07 04:53:16 +02:00
|
|
|
static blender::Mutex main_context_mutex;
|
2020-08-08 01:18:18 +02:00
|
|
|
|
2021-12-08 00:31:20 -05:00
|
|
|
void GPU_context_main_lock()
|
2020-08-05 15:26:49 +02:00
|
|
|
{
|
|
|
|
|
main_context_mutex.lock();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 00:31:20 -05:00
|
|
|
void GPU_context_main_unlock()
|
2020-08-05 15:26:49 +02:00
|
|
|
{
|
|
|
|
|
main_context_mutex.unlock();
|
|
|
|
|
}
|
2020-08-07 17:00:28 +02:00
|
|
|
|
2020-08-08 01:18:18 +02:00
|
|
|
/** \} */
|
|
|
|
|
|
2022-03-22 12:38:28 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
2023-10-19 18:53:16 +11:00
|
|
|
/** \name GPU Begin/end work blocks
|
2022-03-22 12:38:28 +01:00
|
|
|
*
|
|
|
|
|
* Used to explicitly define a per-frame block within which GPU work will happen.
|
|
|
|
|
* Used for global autoreleasepool flushing in Metal
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2022-03-22 13:44:15 +01:00
|
|
|
void GPU_render_begin()
|
|
|
|
|
{
|
|
|
|
|
GPUBackend *backend = GPUBackend::get();
|
2022-03-22 12:38:28 +01:00
|
|
|
BLI_assert(backend);
|
2022-09-27 00:53:50 +02:00
|
|
|
/* WORKAROUND: Currently a band-aid for the heist production. Has no side effect for GL backend
|
|
|
|
|
* but should be fixed for Metal. */
|
|
|
|
|
if (backend) {
|
|
|
|
|
backend->render_begin();
|
2024-08-23 16:11:18 +02:00
|
|
|
printf_begin(active_ctx);
|
2022-09-27 00:53:50 +02:00
|
|
|
}
|
2022-03-22 12:38:28 +01:00
|
|
|
}
|
2022-03-22 13:44:15 +01:00
|
|
|
void GPU_render_end()
|
|
|
|
|
{
|
|
|
|
|
GPUBackend *backend = GPUBackend::get();
|
2022-03-22 12:38:28 +01:00
|
|
|
BLI_assert(backend);
|
2023-09-28 16:13:09 +02:00
|
|
|
if (backend) {
|
2024-08-23 16:11:18 +02:00
|
|
|
printf_end(active_ctx);
|
2024-09-09 16:30:15 +02:00
|
|
|
backend->render_end();
|
2023-09-28 16:13:09 +02:00
|
|
|
}
|
2022-03-22 12:38:28 +01:00
|
|
|
}
|
2024-12-12 20:30:18 +01:00
|
|
|
void GPU_render_step(bool force_resource_release)
|
2022-03-22 13:44:15 +01:00
|
|
|
{
|
|
|
|
|
GPUBackend *backend = GPUBackend::get();
|
2022-03-22 12:38:28 +01:00
|
|
|
BLI_assert(backend);
|
2023-09-28 16:13:09 +02:00
|
|
|
if (backend) {
|
2024-07-19 15:48:00 +02:00
|
|
|
printf_end(active_ctx);
|
2024-12-12 20:30:18 +01:00
|
|
|
backend->render_step(force_resource_release);
|
2024-07-19 15:48:00 +02:00
|
|
|
printf_begin(active_ctx);
|
2023-09-28 16:13:09 +02:00
|
|
|
}
|
2025-05-22 17:53:22 +02:00
|
|
|
|
|
|
|
|
GPU_pass_cache_update();
|
2022-03-22 12:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
2020-08-07 17:00:28 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Backend selection
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2022-10-19 15:13:15 +02:00
|
|
|
static eGPUBackendType g_backend_type = GPU_BACKEND_OPENGL;
|
2022-12-21 20:54:36 +01:00
|
|
|
static std::optional<eGPUBackendType> g_backend_type_override = std::nullopt;
|
|
|
|
|
static std::optional<bool> g_backend_type_supported = std::nullopt;
|
2022-06-21 15:41:36 +02:00
|
|
|
static GPUBackend *g_backend = nullptr;
|
2024-09-30 11:21:28 +02:00
|
|
|
static GHOST_SystemHandle g_ghost_system = nullptr;
|
|
|
|
|
|
|
|
|
|
void GPU_backend_ghost_system_set(void *ghost_system_handle)
|
|
|
|
|
{
|
|
|
|
|
g_ghost_system = reinterpret_cast<GHOST_SystemHandle>(ghost_system_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *GPU_backend_ghost_system_get()
|
|
|
|
|
{
|
|
|
|
|
return g_ghost_system;
|
|
|
|
|
}
|
2020-08-07 17:00:28 +02:00
|
|
|
|
2022-10-19 15:13:15 +02:00
|
|
|
void GPU_backend_type_selection_set(const eGPUBackendType backend)
|
|
|
|
|
{
|
|
|
|
|
g_backend_type = backend;
|
2022-12-21 20:54:36 +01:00
|
|
|
g_backend_type_supported = std::nullopt;
|
2022-10-19 15:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eGPUBackendType GPU_backend_type_selection_get()
|
|
|
|
|
{
|
|
|
|
|
return g_backend_type;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 20:54:36 +01:00
|
|
|
void GPU_backend_type_selection_set_override(const eGPUBackendType backend_type)
|
|
|
|
|
{
|
|
|
|
|
g_backend_type_override = backend_type;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 19:37:22 +10:00
|
|
|
bool GPU_backend_type_selection_is_overridden()
|
2022-12-21 20:54:36 +01:00
|
|
|
{
|
|
|
|
|
return g_backend_type_override.has_value();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 19:37:22 +10:00
|
|
|
bool GPU_backend_type_selection_detect()
|
2022-12-21 20:54:36 +01:00
|
|
|
{
|
2024-09-06 08:28:41 +02:00
|
|
|
blender::VectorSet<eGPUBackendType> backends_to_check;
|
2024-10-31 15:18:29 +01:00
|
|
|
if (g_backend_type_override.has_value()) {
|
2024-09-06 08:28:41 +02:00
|
|
|
backends_to_check.add(*g_backend_type_override);
|
2022-12-21 20:54:36 +01:00
|
|
|
}
|
2023-07-19 14:16:03 +02:00
|
|
|
#if defined(WITH_OPENGL_BACKEND)
|
2024-09-06 08:28:41 +02:00
|
|
|
backends_to_check.add(GPU_BACKEND_OPENGL);
|
2023-07-19 14:16:03 +02:00
|
|
|
#elif defined(WITH_METAL_BACKEND)
|
2024-09-06 08:28:41 +02:00
|
|
|
backends_to_check.add(GPU_BACKEND_METAL);
|
2023-07-19 14:16:03 +02:00
|
|
|
#endif
|
2022-12-21 20:54:36 +01:00
|
|
|
|
2025-03-18 22:22:54 +11:00
|
|
|
#if defined(WITH_VULKAN_BACKEND)
|
|
|
|
|
backends_to_check.add(GPU_BACKEND_VULKAN);
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-12-21 20:54:36 +01:00
|
|
|
for (const eGPUBackendType backend_type : backends_to_check) {
|
|
|
|
|
GPU_backend_type_selection_set(backend_type);
|
|
|
|
|
if (GPU_backend_supported()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-09-06 08:28:41 +02:00
|
|
|
G.f |= G_FLAG_GPU_BACKEND_FALLBACK;
|
2022-12-21 20:54:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPU_backend_type_selection_set(GPU_BACKEND_NONE);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool gpu_backend_supported()
|
2022-03-22 12:38:28 +01:00
|
|
|
{
|
2022-07-15 18:58:35 +02:00
|
|
|
switch (g_backend_type) {
|
2022-03-22 12:38:28 +01:00
|
|
|
case GPU_BACKEND_OPENGL:
|
|
|
|
|
#ifdef WITH_OPENGL_BACKEND
|
|
|
|
|
return true;
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
2022-10-31 16:01:02 +01:00
|
|
|
#endif
|
|
|
|
|
case GPU_BACKEND_VULKAN:
|
|
|
|
|
#ifdef WITH_VULKAN_BACKEND
|
2024-09-06 08:28:41 +02:00
|
|
|
return VKBackend::is_supported();
|
2022-10-31 16:01:02 +01:00
|
|
|
#else
|
|
|
|
|
return false;
|
2022-03-22 12:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
case GPU_BACKEND_METAL:
|
|
|
|
|
#ifdef WITH_METAL_BACKEND
|
|
|
|
|
return MTLBackend::metal_is_supported();
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2023-08-15 14:15:12 +02:00
|
|
|
case GPU_BACKEND_NONE:
|
|
|
|
|
return true;
|
2022-03-22 12:38:28 +01:00
|
|
|
default:
|
|
|
|
|
BLI_assert(false && "No backend specified");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 20:54:36 +01:00
|
|
|
bool GPU_backend_supported()
|
|
|
|
|
{
|
|
|
|
|
if (!g_backend_type_supported.has_value()) {
|
|
|
|
|
g_backend_type_supported = gpu_backend_supported();
|
|
|
|
|
}
|
|
|
|
|
return *g_backend_type_supported;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 18:58:35 +02:00
|
|
|
static void gpu_backend_create()
|
2022-07-15 22:14:04 +10:00
|
|
|
{
|
2022-07-15 18:58:35 +02:00
|
|
|
BLI_assert(g_backend == nullptr);
|
2022-07-16 16:12:48 +10:00
|
|
|
BLI_assert(GPU_backend_supported());
|
2022-07-15 12:44:35 +02:00
|
|
|
|
2022-07-15 18:58:35 +02:00
|
|
|
switch (g_backend_type) {
|
2022-03-22 12:38:28 +01:00
|
|
|
#ifdef WITH_OPENGL_BACKEND
|
2020-08-07 17:00:28 +02:00
|
|
|
case GPU_BACKEND_OPENGL:
|
2025-04-01 17:34:35 +02:00
|
|
|
g_backend = MEM_new<GLBackend>(__func__);
|
2020-08-07 17:00:28 +02:00
|
|
|
break;
|
2022-03-22 12:38:28 +01:00
|
|
|
#endif
|
2022-10-31 16:01:02 +01:00
|
|
|
#ifdef WITH_VULKAN_BACKEND
|
|
|
|
|
case GPU_BACKEND_VULKAN:
|
2025-04-01 17:34:35 +02:00
|
|
|
g_backend = MEM_new<VKBackend>(__func__);
|
2022-10-31 16:01:02 +01:00
|
|
|
break;
|
|
|
|
|
#endif
|
2022-03-22 12:38:28 +01:00
|
|
|
#ifdef WITH_METAL_BACKEND
|
|
|
|
|
case GPU_BACKEND_METAL:
|
2025-04-01 17:34:35 +02:00
|
|
|
g_backend = MEM_new<MTLBackend>(__func__);
|
2022-03-22 12:38:28 +01:00
|
|
|
break;
|
2020-08-07 17:00:28 +02:00
|
|
|
#endif
|
2023-08-15 14:15:12 +02:00
|
|
|
case GPU_BACKEND_NONE:
|
2025-04-01 17:34:35 +02:00
|
|
|
g_backend = MEM_new<DummyBackend>(__func__);
|
2023-08-15 14:15:12 +02:00
|
|
|
break;
|
2020-08-07 17:00:28 +02:00
|
|
|
default:
|
|
|
|
|
BLI_assert(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-07-15 12:44:35 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 15:26:25 +02:00
|
|
|
void gpu_backend_init_resources()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(g_backend);
|
|
|
|
|
g_backend->init_resources();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 12:44:35 +02:00
|
|
|
void gpu_backend_delete_resources()
|
|
|
|
|
{
|
2022-07-15 16:52:01 +02:00
|
|
|
BLI_assert(g_backend);
|
2022-07-15 12:44:35 +02:00
|
|
|
g_backend->delete_resources();
|
2020-08-07 17:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 18:58:35 +02:00
|
|
|
void gpu_backend_discard()
|
2020-08-07 17:00:28 +02:00
|
|
|
{
|
2022-07-15 12:44:35 +02:00
|
|
|
/* TODO: assert no resource left. */
|
2025-04-01 17:34:35 +02:00
|
|
|
MEM_delete(g_backend);
|
2020-11-06 17:49:09 +01:00
|
|
|
g_backend = nullptr;
|
2020-08-07 17:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
OpenSubDiv: add support for an OpenGL evaluator
This evaluator is used in order to evaluate subdivision at render time, allowing for
faster renders of meshes with a subdivision surface modifier placed at the last
position in the modifier list.
When evaluating the subsurf modifier, we detect whether we can delegate evaluation
to the draw code. If so, the subdivision is first evaluated on the GPU using our own
custom evaluator (only the coarse data needs to be initially sent to the GPU), then,
buffers for the final `MeshBufferCache` are filled on the GPU using a set of
compute shaders. However, some buffers are still filled on the CPU side, if doing so
on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose
logic is hardly GPU compatible).
This is done at the mesh buffer extraction level so that the result can be readily used
in the various OpenGL engines, without having to write custom geometry or tesselation
shaders.
We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in
order to control the data layout, and interpolation. For example, we store vertex colors
as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float
types.
In order to still access the modified geometry on the CPU side, for use in modifiers
or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`.
Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will
create such a wrapper if possible. If the final subdivision surface is not needed on
the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used.
Enabling or disabling GPU subdivision can be done through the user preferences (under
Viewport -> Subdivision).
See patch description for benchmarks.
Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport
Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
|
|
|
eGPUBackendType GPU_backend_get_type()
|
|
|
|
|
{
|
2022-03-22 13:44:15 +01:00
|
|
|
|
2022-03-22 12:38:28 +01:00
|
|
|
#ifdef WITH_OPENGL_BACKEND
|
OpenSubDiv: add support for an OpenGL evaluator
This evaluator is used in order to evaluate subdivision at render time, allowing for
faster renders of meshes with a subdivision surface modifier placed at the last
position in the modifier list.
When evaluating the subsurf modifier, we detect whether we can delegate evaluation
to the draw code. If so, the subdivision is first evaluated on the GPU using our own
custom evaluator (only the coarse data needs to be initially sent to the GPU), then,
buffers for the final `MeshBufferCache` are filled on the GPU using a set of
compute shaders. However, some buffers are still filled on the CPU side, if doing so
on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose
logic is hardly GPU compatible).
This is done at the mesh buffer extraction level so that the result can be readily used
in the various OpenGL engines, without having to write custom geometry or tesselation
shaders.
We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in
order to control the data layout, and interpolation. For example, we store vertex colors
as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float
types.
In order to still access the modified geometry on the CPU side, for use in modifiers
or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`.
Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will
create such a wrapper if possible. If the final subdivision surface is not needed on
the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used.
Enabling or disabling GPU subdivision can be done through the user preferences (under
Viewport -> Subdivision).
See patch description for benchmarks.
Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport
Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
|
|
|
if (g_backend && dynamic_cast<GLBackend *>(g_backend) != nullptr) {
|
|
|
|
|
return GPU_BACKEND_OPENGL;
|
|
|
|
|
}
|
2022-03-22 12:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_METAL_BACKEND
|
|
|
|
|
if (g_backend && dynamic_cast<MTLBackend *>(g_backend) != nullptr) {
|
|
|
|
|
return GPU_BACKEND_METAL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
OpenSubDiv: add support for an OpenGL evaluator
This evaluator is used in order to evaluate subdivision at render time, allowing for
faster renders of meshes with a subdivision surface modifier placed at the last
position in the modifier list.
When evaluating the subsurf modifier, we detect whether we can delegate evaluation
to the draw code. If so, the subdivision is first evaluated on the GPU using our own
custom evaluator (only the coarse data needs to be initially sent to the GPU), then,
buffers for the final `MeshBufferCache` are filled on the GPU using a set of
compute shaders. However, some buffers are still filled on the CPU side, if doing so
on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose
logic is hardly GPU compatible).
This is done at the mesh buffer extraction level so that the result can be readily used
in the various OpenGL engines, without having to write custom geometry or tesselation
shaders.
We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in
order to control the data layout, and interpolation. For example, we store vertex colors
as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float
types.
In order to still access the modified geometry on the CPU side, for use in modifiers
or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`.
Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will
create such a wrapper if possible. If the final subdivision surface is not needed on
the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used.
Enabling or disabling GPU subdivision can be done through the user preferences (under
Viewport -> Subdivision).
See patch description for benchmarks.
Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport
Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
|
|
|
|
2022-12-02 12:51:11 +01:00
|
|
|
#ifdef WITH_VULKAN_BACKEND
|
|
|
|
|
if (g_backend && dynamic_cast<VKBackend *>(g_backend) != nullptr) {
|
|
|
|
|
return GPU_BACKEND_VULKAN;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
OpenSubDiv: add support for an OpenGL evaluator
This evaluator is used in order to evaluate subdivision at render time, allowing for
faster renders of meshes with a subdivision surface modifier placed at the last
position in the modifier list.
When evaluating the subsurf modifier, we detect whether we can delegate evaluation
to the draw code. If so, the subdivision is first evaluated on the GPU using our own
custom evaluator (only the coarse data needs to be initially sent to the GPU), then,
buffers for the final `MeshBufferCache` are filled on the GPU using a set of
compute shaders. However, some buffers are still filled on the CPU side, if doing so
on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose
logic is hardly GPU compatible).
This is done at the mesh buffer extraction level so that the result can be readily used
in the various OpenGL engines, without having to write custom geometry or tesselation
shaders.
We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in
order to control the data layout, and interpolation. For example, we store vertex colors
as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float
types.
In order to still access the modified geometry on the CPU side, for use in modifiers
or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`.
Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will
create such a wrapper if possible. If the final subdivision surface is not needed on
the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used.
Enabling or disabling GPU subdivision can be done through the user preferences (under
Viewport -> Subdivision).
See patch description for benchmarks.
Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport
Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
|
|
|
return GPU_BACKEND_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 13:18:48 +01:00
|
|
|
GPUBackend *GPUBackend::get()
|
2020-08-07 17:00:28 +02:00
|
|
|
{
|
|
|
|
|
return g_backend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
2025-04-07 15:26:25 +02:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name GPUSecondaryContext
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static GHOST_TDrawingContextType ghost_context_type()
|
|
|
|
|
{
|
|
|
|
|
switch (GPU_backend_type_selection_get()) {
|
|
|
|
|
#ifdef WITH_OPENGL_BACKEND
|
|
|
|
|
case GPU_BACKEND_OPENGL:
|
|
|
|
|
return GHOST_kDrawingContextTypeOpenGL;
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef WITH_VULKAN_BACKEND
|
|
|
|
|
case GPU_BACKEND_VULKAN:
|
|
|
|
|
return GHOST_kDrawingContextTypeVulkan;
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef WITH_METAL_BACKEND
|
|
|
|
|
case GPU_BACKEND_METAL:
|
|
|
|
|
return GHOST_kDrawingContextTypeMetal;
|
|
|
|
|
#endif
|
|
|
|
|
default:
|
|
|
|
|
BLI_assert_unreachable();
|
|
|
|
|
return GHOST_kDrawingContextTypeNone;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUSecondaryContext::GPUSecondaryContext()
|
|
|
|
|
{
|
|
|
|
|
/* Contexts can only be created on the main thread. */
|
|
|
|
|
BLI_assert(BLI_thread_is_main());
|
|
|
|
|
|
|
|
|
|
GHOST_ContextHandle main_thread_ghost_context = GHOST_GetActiveGPUContext();
|
|
|
|
|
GPUContext *main_thread_gpu_context = GPU_context_active_get();
|
|
|
|
|
|
|
|
|
|
/* GPU settings for context creation. */
|
|
|
|
|
GHOST_GPUSettings gpu_settings = {0};
|
|
|
|
|
gpu_settings.context_type = ghost_context_type();
|
|
|
|
|
if (G.debug & G_DEBUG_GPU) {
|
|
|
|
|
gpu_settings.flags |= GHOST_gpuDebugContext;
|
|
|
|
|
}
|
|
|
|
|
gpu_settings.preferred_device.index = U.gpu_preferred_index;
|
|
|
|
|
gpu_settings.preferred_device.vendor_id = U.gpu_preferred_vendor_id;
|
|
|
|
|
gpu_settings.preferred_device.device_id = U.gpu_preferred_device_id;
|
|
|
|
|
|
2025-04-29 09:02:49 +10:00
|
|
|
/* Grab the system handle. */
|
2025-04-07 15:26:25 +02:00
|
|
|
GHOST_SystemHandle ghost_system = reinterpret_cast<GHOST_SystemHandle>(
|
|
|
|
|
GPU_backend_ghost_system_get());
|
|
|
|
|
BLI_assert(ghost_system);
|
|
|
|
|
|
|
|
|
|
/* Create a Ghost GPU Context using the system handle. */
|
|
|
|
|
ghost_context_ = GHOST_CreateGPUContext(ghost_system, gpu_settings);
|
|
|
|
|
BLI_assert(ghost_context_);
|
|
|
|
|
|
|
|
|
|
/* Create a GPU context for the secondary thread to use. */
|
|
|
|
|
gpu_context_ = GPU_context_create(nullptr, ghost_context_);
|
|
|
|
|
BLI_assert(gpu_context_);
|
|
|
|
|
|
|
|
|
|
/* Release the Ghost GPU Context from this thread. */
|
|
|
|
|
GHOST_TSuccess success = GHOST_ReleaseGPUContext(
|
|
|
|
|
reinterpret_cast<GHOST_ContextHandle>(ghost_context_));
|
|
|
|
|
BLI_assert(success);
|
2025-04-08 15:18:10 +10:00
|
|
|
UNUSED_VARS_NDEBUG(success);
|
2025-04-07 15:26:25 +02:00
|
|
|
|
|
|
|
|
/* Restore the main thread contexts.
|
|
|
|
|
* (required as the above context creation also makes it active). */
|
|
|
|
|
GHOST_ActivateGPUContext(main_thread_ghost_context);
|
|
|
|
|
GPU_context_active_set(main_thread_gpu_context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUSecondaryContext::~GPUSecondaryContext()
|
|
|
|
|
{
|
|
|
|
|
/* Contexts should be destructed on the thread they were activated. */
|
|
|
|
|
BLI_assert(!BLI_thread_is_main());
|
|
|
|
|
|
|
|
|
|
GPU_context_discard(gpu_context_);
|
|
|
|
|
|
|
|
|
|
GHOST_ReleaseGPUContext(reinterpret_cast<GHOST_ContextHandle>(ghost_context_));
|
|
|
|
|
|
|
|
|
|
GHOST_SystemHandle ghost_system = reinterpret_cast<GHOST_SystemHandle>(
|
|
|
|
|
GPU_backend_ghost_system_get());
|
|
|
|
|
BLI_assert(ghost_system);
|
|
|
|
|
GHOST_DisposeGPUContext(ghost_system, reinterpret_cast<GHOST_ContextHandle>(ghost_context_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPUSecondaryContext::activate()
|
|
|
|
|
{
|
|
|
|
|
/* Contexts need to be activated in the thread they're going to be used. */
|
|
|
|
|
BLI_assert(!BLI_thread_is_main());
|
|
|
|
|
|
|
|
|
|
GHOST_ActivateGPUContext(reinterpret_cast<GHOST_ContextHandle>(ghost_context_));
|
|
|
|
|
GPU_context_active_set(gpu_context_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|