Files
test2/source/blender/gpu/intern/gpu_context.cc
Clément Foucault 00a8d006fe GPU: Move Polyline shader to primitive expansion
This port is not so straightforward.

This shader is used in different configurations and is
available to python bindings. So we need to keep
compatibility with different attributes configurations.

This is why attributes are loaded per component and a
uniform sets the length of the component.

Since this shader can be used from both the imm and batch
API, we need to inject some workarounds to bind the buffers
correctly.

The end result is still less versatile than the previous
metal workaround (i.e.: more attribute fetch mode supported),
but it is also way less code.

### Limitations:
The new shader has some limitation:
- Both `color` and `pos` attributes need to be `F32`.
- Each attribute needs to be 4byte aligned.
- Fetch type needs to be `GPU_FETCH_FLOAT`.
- Primitive type needs to be `GPU_PRIM_LINES`, `GPU_PRIM_LINE_STRIP` or `GPU_PRIM_LINE_LOOP`.
- If drawing using an index buffer, it must contain no primitive restart.

Rel #127493

Co-authored-by: Jeroen Bakker <jeroen@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/129315
2024-11-27 17:37:04 +01:00

425 lines
8.9 KiB
C++

/* SPDX-FileCopyrightText: 2016 by Mike Erwin. All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*
* 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
* this VAO.
* - free can be called from any thread
*/
#include "GHOST_C-api.h"
#include "BKE_global.hh"
#include "BLI_assert.h"
#include "BLI_utildefines.h"
#include "BLI_vector_set.hh"
#include "GPU_context.hh"
#include "GPU_framebuffer.hh"
#include "GPU_batch.hh"
#include "gpu_backend.hh"
#include "gpu_context_private.hh"
#include "gpu_matrix_private.hh"
#include "gpu_private.hh"
#include "gpu_shader_private.hh"
#ifdef WITH_OPENGL_BACKEND
# include "gl_backend.hh"
# include "gl_context.hh"
#endif
#ifdef WITH_VULKAN_BACKEND
# include "vk_backend.hh"
#endif
#ifdef WITH_METAL_BACKEND
# include "mtl_backend.hh"
#endif
#include "dummy_backend.hh"
#include <mutex>
#include <vector>
using namespace blender::gpu;
static thread_local Context *active_ctx = nullptr;
static std::mutex backend_users_mutex;
static int num_backend_users = 0;
static void gpu_backend_create();
static void gpu_backend_discard();
/* -------------------------------------------------------------------- */
/** \name gpu::Context methods
* \{ */
namespace blender::gpu {
int Context::context_counter = 0;
Context::Context()
{
thread_ = pthread_self();
is_active_ = false;
matrix_state = GPU_matrix_state_create();
context_id = Context::context_counter;
Context::context_counter++;
}
Context::~Context()
{
GPU_matrix_state_discard(matrix_state);
GPU_BATCH_DISCARD_SAFE(polyline_batch);
delete state_manager;
delete front_left;
delete back_left;
delete front_right;
delete back_right;
delete imm;
}
bool Context::is_active_on_thread()
{
return (this == active_ctx) && pthread_equal(pthread_self(), thread_);
}
Context *Context::get()
{
return active_ctx;
}
Batch *Context::polyline_batch_get()
{
if (polyline_batch) {
return polyline_batch;
}
/* TODO(fclem): get rid of this dummy VBO. */
GPUVertFormat format = {0};
GPU_vertformat_attr_add(&format, "dummy", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
blender::gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
GPU_vertbuf_data_alloc(*vbo, 1);
polyline_batch = GPU_batch_create_ex(GPU_PRIM_TRIS, vbo, nullptr, GPU_BATCH_OWNS_VBO);
return polyline_batch;
}
} // namespace blender::gpu
/** \} */
/* -------------------------------------------------------------------- */
GPUContext *GPU_context_create(void *ghost_window, void *ghost_context)
{
{
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++;
}
Context *ctx = GPUBackend::get()->context_alloc(ghost_window, ghost_context);
GPU_context_active_set(wrap(ctx));
return wrap(ctx);
}
void GPU_context_discard(GPUContext *ctx_)
{
Context *ctx = unwrap(ctx_);
delete ctx;
active_ctx = nullptr;
{
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();
}
}
}
void GPU_context_active_set(GPUContext *ctx_)
{
Context *ctx = unwrap(ctx_);
if (active_ctx) {
active_ctx->deactivate();
}
active_ctx = ctx;
if (ctx) {
ctx->activate();
}
}
GPUContext *GPU_context_active_get()
{
return wrap(Context::get());
}
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();
}
}
/* -------------------------------------------------------------------- */
/** \name Main context global mutex
*
* Used to avoid crash on some old drivers.
* \{ */
static std::mutex main_context_mutex;
void GPU_context_main_lock()
{
main_context_mutex.lock();
}
void GPU_context_main_unlock()
{
main_context_mutex.unlock();
}
/** \} */
/* -------------------------------------------------------------------- */
/** \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
* \{ */
void GPU_render_begin()
{
GPUBackend *backend = GPUBackend::get();
BLI_assert(backend);
/* 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();
printf_begin(active_ctx);
}
}
void GPU_render_end()
{
GPUBackend *backend = GPUBackend::get();
BLI_assert(backend);
if (backend) {
printf_end(active_ctx);
backend->render_end();
}
}
void GPU_render_step()
{
GPUBackend *backend = GPUBackend::get();
BLI_assert(backend);
if (backend) {
printf_end(active_ctx);
backend->render_step();
printf_begin(active_ctx);
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Backend selection
* \{ */
static eGPUBackendType g_backend_type = GPU_BACKEND_OPENGL;
static std::optional<eGPUBackendType> g_backend_type_override = std::nullopt;
static std::optional<bool> g_backend_type_supported = std::nullopt;
static GPUBackend *g_backend = nullptr;
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;
}
void GPU_backend_type_selection_set(const eGPUBackendType backend)
{
g_backend_type = backend;
g_backend_type_supported = std::nullopt;
}
eGPUBackendType GPU_backend_type_selection_get()
{
return g_backend_type;
}
void GPU_backend_type_selection_set_override(const eGPUBackendType backend_type)
{
g_backend_type_override = backend_type;
}
bool GPU_backend_type_selection_is_overridden()
{
return g_backend_type_override.has_value();
}
bool GPU_backend_type_selection_detect()
{
blender::VectorSet<eGPUBackendType> backends_to_check;
if (g_backend_type_override.has_value()) {
backends_to_check.add(*g_backend_type_override);
}
#if defined(WITH_OPENGL_BACKEND)
backends_to_check.add(GPU_BACKEND_OPENGL);
#elif defined(WITH_METAL_BACKEND)
backends_to_check.add(GPU_BACKEND_METAL);
#endif
for (const eGPUBackendType backend_type : backends_to_check) {
GPU_backend_type_selection_set(backend_type);
if (GPU_backend_supported()) {
return true;
}
G.f |= G_FLAG_GPU_BACKEND_FALLBACK;
}
GPU_backend_type_selection_set(GPU_BACKEND_NONE);
return false;
}
static bool gpu_backend_supported()
{
switch (g_backend_type) {
case GPU_BACKEND_OPENGL:
#ifdef WITH_OPENGL_BACKEND
return true;
#else
return false;
#endif
case GPU_BACKEND_VULKAN:
#ifdef WITH_VULKAN_BACKEND
return VKBackend::is_supported();
#else
return false;
#endif
case GPU_BACKEND_METAL:
#ifdef WITH_METAL_BACKEND
return MTLBackend::metal_is_supported();
#else
return false;
#endif
case GPU_BACKEND_NONE:
return true;
default:
BLI_assert(false && "No backend specified");
return false;
}
}
bool GPU_backend_supported()
{
if (!g_backend_type_supported.has_value()) {
g_backend_type_supported = gpu_backend_supported();
}
return *g_backend_type_supported;
}
static void gpu_backend_create()
{
BLI_assert(g_backend == nullptr);
BLI_assert(GPU_backend_supported());
switch (g_backend_type) {
#ifdef WITH_OPENGL_BACKEND
case GPU_BACKEND_OPENGL:
g_backend = new GLBackend;
break;
#endif
#ifdef WITH_VULKAN_BACKEND
case GPU_BACKEND_VULKAN:
g_backend = new VKBackend;
break;
#endif
#ifdef WITH_METAL_BACKEND
case GPU_BACKEND_METAL:
g_backend = new MTLBackend;
break;
#endif
case GPU_BACKEND_NONE:
g_backend = new DummyBackend;
break;
default:
BLI_assert(0);
break;
}
}
void gpu_backend_delete_resources()
{
BLI_assert(g_backend);
g_backend->delete_resources();
}
void gpu_backend_discard()
{
/* TODO: assert no resource left. */
delete g_backend;
g_backend = nullptr;
}
eGPUBackendType GPU_backend_get_type()
{
#ifdef WITH_OPENGL_BACKEND
if (g_backend && dynamic_cast<GLBackend *>(g_backend) != nullptr) {
return GPU_BACKEND_OPENGL;
}
#endif
#ifdef WITH_METAL_BACKEND
if (g_backend && dynamic_cast<MTLBackend *>(g_backend) != nullptr) {
return GPU_BACKEND_METAL;
}
#endif
#ifdef WITH_VULKAN_BACKEND
if (g_backend && dynamic_cast<VKBackend *>(g_backend) != nullptr) {
return GPU_BACKEND_VULKAN;
}
#endif
return GPU_BACKEND_NONE;
}
GPUBackend *GPUBackend::get()
{
return g_backend;
}
/** \} */