2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2016 by Mike Erwin. All rights reserved. */
|
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
|
|
|
|
2021-07-03 23:08:40 +10:00
|
|
|
/* TODO: Create cmake option. */
|
2022-03-22 12:38:28 +01:00
|
|
|
#if WITH_OPENGL
|
2022-03-22 13:44:15 +01:00
|
|
|
# define WITH_OPENGL_BACKEND 1
|
2022-03-22 12:38:28 +01:00
|
|
|
#endif
|
2020-08-07 17:00:28 +02:00
|
|
|
|
2018-07-19 15:48:13 +02:00
|
|
|
#include "BLI_assert.h"
|
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2018-07-17 21:11:23 +02:00
|
|
|
#include "GPU_context.h"
|
2018-07-19 15:48:13 +02:00
|
|
|
#include "GPU_framebuffer.h"
|
|
|
|
|
|
2020-08-07 17:00:28 +02:00
|
|
|
#include "gpu_backend.hh"
|
2020-08-09 01:21:34 +02:00
|
|
|
#include "gpu_batch_private.hh"
|
2020-08-08 03:01:45 +02:00
|
|
|
#include "gpu_context_private.hh"
|
2019-08-14 15:27:10 +02:00
|
|
|
#include "gpu_matrix_private.h"
|
2018-07-19 15:48:13 +02:00
|
|
|
|
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
|
2020-08-07 17:00:28 +02:00
|
|
|
|
2018-02-19 23:50:52 +01:00
|
|
|
#include <mutex>
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <vector>
|
2018-02-19 23:50:52 +01:00
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
Context::Context()
|
2020-08-08 01:18:18 +02:00
|
|
|
{
|
|
|
|
|
thread_ = pthread_self();
|
|
|
|
|
is_active_ = false;
|
|
|
|
|
matrix_state = GPU_matrix_state_create();
|
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
|
|
|
{
|
2020-08-08 01:18:18 +02:00
|
|
|
GPU_matrix_state_discard(matrix_state);
|
2020-08-17 00:34:06 +02:00
|
|
|
delete state_manager;
|
2020-08-31 15:13:26 +02:00
|
|
|
delete front_left;
|
|
|
|
|
delete back_left;
|
|
|
|
|
delete front_right;
|
|
|
|
|
delete back_right;
|
2020-08-31 15:14:47 +02:00
|
|
|
delete imm;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // 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
|
|
|
|
2020-08-06 04:30:38 +02:00
|
|
|
GPUContext *GPU_context_create(void *ghost_window)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2020-11-06 17:49:09 +01:00
|
|
|
if (GPUBackend::get() == nullptr) {
|
2021-07-03 23:08:40 +10:00
|
|
|
/* TODO: move where it make sense. */
|
2020-08-07 17:00:28 +02:00
|
|
|
GPU_backend_init(GPU_BACKEND_OPENGL);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
Context *ctx = GPUBackend::get()->context_alloc(ghost_window);
|
2020-08-06 04:30:38 +02:00
|
|
|
|
2020-09-08 04:12:12 +02:00
|
|
|
GPU_context_active_set(wrap(ctx));
|
|
|
|
|
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_);
|
2019-04-17 06:17:24 +02:00
|
|
|
delete ctx;
|
2020-11-06 17:49:09 +01:00
|
|
|
active_ctx = nullptr;
|
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_);
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (active_ctx) {
|
2020-08-08 01:18:18 +02:00
|
|
|
active_ctx->deactivate();
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2020-08-08 01:18:18 +02:00
|
|
|
|
|
|
|
|
active_ctx = ctx;
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (ctx) {
|
2020-08-08 01:18:18 +02:00
|
|
|
ctx->activate();
|
2019-04-17 06:17:24 +02: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.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static std::mutex main_context_mutex;
|
|
|
|
|
|
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
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name GPU Begin/end work blocks
|
|
|
|
|
*
|
|
|
|
|
* 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);
|
|
|
|
|
backend->render_begin();
|
|
|
|
|
}
|
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);
|
|
|
|
|
backend->render_end();
|
|
|
|
|
}
|
2022-03-22 13:44:15 +01:00
|
|
|
void GPU_render_step()
|
|
|
|
|
{
|
|
|
|
|
GPUBackend *backend = GPUBackend::get();
|
2022-03-22 12:38:28 +01:00
|
|
|
BLI_assert(backend);
|
|
|
|
|
backend->render_step();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
2020-08-07 17:00:28 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Backend selection
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2022-06-21 15:41:36 +02:00
|
|
|
static GPUBackend *g_backend = nullptr;
|
2020-08-07 17:00:28 +02:00
|
|
|
|
2022-03-22 12:38:28 +01:00
|
|
|
bool GPU_backend_supported(eGPUBackendType type)
|
|
|
|
|
{
|
|
|
|
|
switch (type) {
|
|
|
|
|
case GPU_BACKEND_OPENGL:
|
|
|
|
|
#ifdef WITH_OPENGL_BACKEND
|
|
|
|
|
return true;
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
case GPU_BACKEND_METAL:
|
|
|
|
|
#ifdef WITH_METAL_BACKEND
|
|
|
|
|
return MTLBackend::metal_is_supported();
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
|
|
|
|
default:
|
|
|
|
|
BLI_assert(false && "No backend specified");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 17:00:28 +02:00
|
|
|
void GPU_backend_init(eGPUBackendType backend_type)
|
|
|
|
|
{
|
2020-11-06 17:49:09 +01:00
|
|
|
BLI_assert(g_backend == nullptr);
|
2022-03-22 12:38:28 +01:00
|
|
|
BLI_assert(GPU_backend_supported(backend_type));
|
2020-08-07 17:00:28 +02:00
|
|
|
|
|
|
|
|
switch (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:
|
|
|
|
|
g_backend = new GLBackend;
|
|
|
|
|
break;
|
2022-03-22 12:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
#ifdef WITH_METAL_BACKEND
|
|
|
|
|
case GPU_BACKEND_METAL:
|
|
|
|
|
g_backend = new MTLBackend;
|
|
|
|
|
break;
|
2020-08-07 17:00:28 +02:00
|
|
|
#endif
|
|
|
|
|
default:
|
|
|
|
|
BLI_assert(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 00:31:20 -05:00
|
|
|
void GPU_backend_exit()
|
2020-08-07 17:00:28 +02:00
|
|
|
{
|
2021-07-03 23:08:40 +10:00
|
|
|
/* TODO: assert no resource left. Currently UI textures are still not freed in their context
|
2020-08-11 14:58:01 -04:00
|
|
|
* correctly. */
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|