2018-07-17 14:46:44 +02:00
|
|
|
/*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2016 by Mike Erwin.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
|
|
* this vao.
|
|
|
|
|
* - free can be called from any thread
|
|
|
|
|
*/
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2020-08-07 17:00:28 +02:00
|
|
|
/* TODO Create cmake option. */
|
|
|
|
|
#define WITH_OPENGL_BACKEND 1
|
|
|
|
|
|
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-06 04:30:38 +02:00
|
|
|
#include "GHOST_C-api.h"
|
|
|
|
|
|
2020-08-07 17:00:28 +02:00
|
|
|
#include "gpu_backend.hh"
|
2018-07-19 15:48:13 +02:00
|
|
|
#include "gpu_batch_private.h"
|
|
|
|
|
#include "gpu_context_private.h"
|
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
|
|
|
|
|
|
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
|
|
|
|
2018-07-19 15:48:13 +02:00
|
|
|
static std::vector<GLuint> orphaned_buffer_ids;
|
|
|
|
|
static std::vector<GLuint> orphaned_texture_ids;
|
|
|
|
|
|
|
|
|
|
static std::mutex orphans_mutex;
|
2020-08-05 15:26:49 +02:00
|
|
|
static std::mutex main_context_mutex;
|
2018-07-19 15:48:13 +02:00
|
|
|
|
2018-07-19 16:06:37 +10:00
|
|
|
static thread_local GPUContext *active_ctx = NULL;
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-19 15:48:13 +02:00
|
|
|
static void orphans_add(GPUContext *ctx, std::vector<GLuint> *orphan_list, GLuint id)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
std::mutex *mutex = (ctx) ? &ctx->orphans_mutex : &orphans_mutex;
|
2018-07-19 15:48:13 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
mutex->lock();
|
|
|
|
|
orphan_list->emplace_back(id);
|
|
|
|
|
mutex->unlock();
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void orphans_clear(GPUContext *ctx)
|
|
|
|
|
{
|
2019-08-14 23:29:46 +10:00
|
|
|
/* need at least an active context */
|
|
|
|
|
BLI_assert(ctx);
|
|
|
|
|
|
|
|
|
|
/* context has been activated by another thread! */
|
|
|
|
|
BLI_assert(pthread_equal(pthread_self(), ctx->thread));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
|
if (!ctx->orphaned_vertarray_ids.empty()) {
|
|
|
|
|
uint orphan_len = (uint)ctx->orphaned_vertarray_ids.size();
|
|
|
|
|
glDeleteVertexArrays(orphan_len, ctx->orphaned_vertarray_ids.data());
|
|
|
|
|
ctx->orphaned_vertarray_ids.clear();
|
|
|
|
|
}
|
|
|
|
|
if (!ctx->orphaned_framebuffer_ids.empty()) {
|
|
|
|
|
uint orphan_len = (uint)ctx->orphaned_framebuffer_ids.size();
|
|
|
|
|
glDeleteFramebuffers(orphan_len, ctx->orphaned_framebuffer_ids.data());
|
|
|
|
|
ctx->orphaned_framebuffer_ids.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx->orphans_mutex.unlock();
|
|
|
|
|
|
|
|
|
|
orphans_mutex.lock();
|
|
|
|
|
if (!orphaned_buffer_ids.empty()) {
|
|
|
|
|
uint orphan_len = (uint)orphaned_buffer_ids.size();
|
|
|
|
|
glDeleteBuffers(orphan_len, orphaned_buffer_ids.data());
|
|
|
|
|
orphaned_buffer_ids.clear();
|
|
|
|
|
}
|
|
|
|
|
if (!orphaned_texture_ids.empty()) {
|
|
|
|
|
uint orphan_len = (uint)orphaned_texture_ids.size();
|
|
|
|
|
glDeleteTextures(orphan_len, orphaned_texture_ids.data());
|
|
|
|
|
orphaned_texture_ids.clear();
|
|
|
|
|
}
|
|
|
|
|
orphans_mutex.unlock();
|
2018-07-17 14:46:44 +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-08-07 17:00:28 +02:00
|
|
|
if (gpu_backend_get() == NULL) {
|
|
|
|
|
/* TODO move where it make sense. */
|
|
|
|
|
GPU_backend_init(GPU_BACKEND_OPENGL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUContext *ctx = gpu_backend_get()->context_alloc(ghost_window);
|
2019-04-17 06:17:24 +02:00
|
|
|
glGenVertexArrays(1, &ctx->default_vao);
|
2020-08-06 04:30:38 +02:00
|
|
|
if (ghost_window != NULL) {
|
|
|
|
|
ctx->default_framebuffer = GHOST_GetDefaultOpenGLFramebuffer((GHOST_WindowHandle)ghost_window);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ctx->default_framebuffer = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 15:27:10 +02:00
|
|
|
ctx->matrix_state = GPU_matrix_state_create();
|
2019-04-17 06:17:24 +02:00
|
|
|
GPU_context_active_set(ctx);
|
|
|
|
|
return ctx;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
/* to be called after GPU_context_active_set(ctx_to_destroy) */
|
2018-07-19 16:06:37 +10:00
|
|
|
void GPU_context_discard(GPUContext *ctx)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
/* Make sure no other thread has locked it. */
|
|
|
|
|
BLI_assert(ctx == active_ctx);
|
|
|
|
|
BLI_assert(pthread_equal(pthread_self(), ctx->thread));
|
|
|
|
|
BLI_assert(ctx->orphaned_vertarray_ids.empty());
|
2018-07-20 13:35:08 +02:00
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
/* For now don't allow GPUFrameBuffers to be reuse in another ctx. */
|
|
|
|
|
BLI_assert(ctx->framebuffers.empty());
|
2018-07-20 13:35:08 +02:00
|
|
|
#endif
|
2019-04-17 06:17:24 +02:00
|
|
|
/* delete remaining vaos */
|
|
|
|
|
while (!ctx->batches.empty()) {
|
|
|
|
|
/* this removes the array entry */
|
|
|
|
|
GPU_batch_vao_cache_clear(*ctx->batches.begin());
|
|
|
|
|
}
|
2019-08-14 15:27:10 +02:00
|
|
|
GPU_matrix_state_discard(ctx->matrix_state);
|
2019-04-17 06:17:24 +02:00
|
|
|
glDeleteVertexArrays(1, &ctx->default_vao);
|
|
|
|
|
delete ctx;
|
|
|
|
|
active_ctx = NULL;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
/* ctx can be NULL */
|
2018-07-19 16:06:37 +10:00
|
|
|
void GPU_context_active_set(GPUContext *ctx)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-02-19 23:50:52 +01:00
|
|
|
#if TRUST_NO_ONE
|
2019-04-17 06:17:24 +02:00
|
|
|
if (active_ctx) {
|
|
|
|
|
active_ctx->thread_is_used = false;
|
|
|
|
|
}
|
|
|
|
|
/* Make sure no other context is already bound to this thread. */
|
|
|
|
|
if (ctx) {
|
|
|
|
|
/* Make sure no other thread has locked it. */
|
|
|
|
|
assert(ctx->thread_is_used == false);
|
|
|
|
|
ctx->thread = pthread_self();
|
|
|
|
|
ctx->thread_is_used = true;
|
|
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
2019-04-17 06:17:24 +02:00
|
|
|
if (ctx) {
|
|
|
|
|
orphans_clear(ctx);
|
|
|
|
|
}
|
|
|
|
|
active_ctx = ctx;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-19 16:06:37 +10:00
|
|
|
GPUContext *GPU_context_active_get(void)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return active_ctx;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GLuint GPU_vao_default(void)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(active_ctx); /* need at least an active context */
|
|
|
|
|
BLI_assert(pthread_equal(
|
|
|
|
|
pthread_self(), active_ctx->thread)); /* context has been activated by another thread! */
|
|
|
|
|
return active_ctx->default_vao;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2019-06-01 17:54:07 +02:00
|
|
|
GLuint GPU_framebuffer_default(void)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(active_ctx); /* need at least an active context */
|
|
|
|
|
BLI_assert(pthread_equal(
|
|
|
|
|
pthread_self(), active_ctx->thread)); /* context has been activated by another thread! */
|
|
|
|
|
return active_ctx->default_framebuffer;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GLuint GPU_vao_alloc(void)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GLuint new_vao_id = 0;
|
|
|
|
|
orphans_clear(active_ctx);
|
|
|
|
|
glGenVertexArrays(1, &new_vao_id);
|
|
|
|
|
return new_vao_id;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-19 15:48:13 +02:00
|
|
|
GLuint GPU_fbo_alloc(void)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GLuint new_fbo_id = 0;
|
|
|
|
|
orphans_clear(active_ctx);
|
|
|
|
|
glGenFramebuffers(1, &new_fbo_id);
|
|
|
|
|
return new_fbo_id;
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLuint GPU_buf_alloc(void)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GLuint new_buffer_id = 0;
|
|
|
|
|
orphans_clear(active_ctx);
|
|
|
|
|
glGenBuffers(1, &new_buffer_id);
|
|
|
|
|
return new_buffer_id;
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLuint GPU_tex_alloc(void)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GLuint new_texture_id = 0;
|
|
|
|
|
orphans_clear(active_ctx);
|
|
|
|
|
glGenTextures(1, &new_texture_id);
|
|
|
|
|
return new_texture_id;
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-19 16:06:37 +10:00
|
|
|
void GPU_vao_free(GLuint vao_id, GPUContext *ctx)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(ctx);
|
|
|
|
|
if (ctx == active_ctx) {
|
|
|
|
|
glDeleteVertexArrays(1, &vao_id);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
orphans_add(ctx, &ctx->orphaned_vertarray_ids, vao_id);
|
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-22 12:39:57 +01:00
|
|
|
|
2018-07-19 15:48:13 +02:00
|
|
|
void GPU_fbo_free(GLuint fbo_id, GPUContext *ctx)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(ctx);
|
|
|
|
|
if (ctx == active_ctx) {
|
|
|
|
|
glDeleteFramebuffers(1, &fbo_id);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
orphans_add(ctx, &ctx->orphaned_framebuffer_ids, fbo_id);
|
|
|
|
|
}
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPU_buf_free(GLuint buf_id)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
if (active_ctx) {
|
|
|
|
|
glDeleteBuffers(1, &buf_id);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
orphans_add(NULL, &orphaned_buffer_ids, buf_id);
|
|
|
|
|
}
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPU_tex_free(GLuint tex_id)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
if (active_ctx) {
|
|
|
|
|
glDeleteTextures(1, &tex_id);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
orphans_add(NULL, &orphaned_texture_ids, tex_id);
|
|
|
|
|
}
|
2018-07-19 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GPUBatch & GPUFrameBuffer contains respectively VAO & FBO indices
|
|
|
|
|
* which are not shared across contexts. So we need to keep track of
|
|
|
|
|
* ownership. */
|
|
|
|
|
|
2018-07-19 16:06:37 +10:00
|
|
|
void gpu_context_add_batch(GPUContext *ctx, GPUBatch *batch)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(ctx);
|
|
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
|
ctx->batches.emplace(batch);
|
|
|
|
|
ctx->orphans_mutex.unlock();
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-22 12:39:57 +01:00
|
|
|
|
2018-07-19 16:06:37 +10:00
|
|
|
void gpu_context_remove_batch(GPUContext *ctx, GPUBatch *batch)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(ctx);
|
|
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
|
ctx->batches.erase(batch);
|
|
|
|
|
ctx->orphans_mutex.unlock();
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-07-19 15:48:13 +02:00
|
|
|
|
|
|
|
|
void gpu_context_add_framebuffer(GPUContext *ctx, GPUFrameBuffer *fb)
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(ctx);
|
|
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
|
ctx->framebuffers.emplace(fb);
|
|
|
|
|
ctx->orphans_mutex.unlock();
|
2018-07-19 15:48:13 +02:00
|
|
|
#else
|
2019-04-17 06:17:24 +02:00
|
|
|
UNUSED_VARS(ctx, fb);
|
2018-07-19 15:48:13 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void gpu_context_remove_framebuffer(GPUContext *ctx, GPUFrameBuffer *fb)
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(ctx);
|
|
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
|
ctx->framebuffers.erase(fb);
|
|
|
|
|
ctx->orphans_mutex.unlock();
|
2018-07-19 15:48:13 +02:00
|
|
|
#else
|
2019-04-17 06:17:24 +02:00
|
|
|
UNUSED_VARS(ctx, fb);
|
2018-07-19 15:48:13 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
2018-07-31 18:16:08 +02:00
|
|
|
|
|
|
|
|
void gpu_context_active_framebuffer_set(GPUContext *ctx, GPUFrameBuffer *fb)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
ctx->current_fbo = fb;
|
2018-07-31 18:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUFrameBuffer *gpu_context_active_framebuffer_get(GPUContext *ctx)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return ctx->current_fbo;
|
2018-07-31 18:16:08 +02:00
|
|
|
}
|
2019-08-14 15:27:10 +02:00
|
|
|
|
|
|
|
|
struct GPUMatrixState *gpu_context_active_matrix_state_get()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(active_ctx);
|
|
|
|
|
return active_ctx->matrix_state;
|
|
|
|
|
}
|
2020-08-05 15:26:49 +02:00
|
|
|
|
|
|
|
|
void GPU_context_main_lock(void)
|
|
|
|
|
{
|
|
|
|
|
main_context_mutex.lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPU_context_main_unlock(void)
|
|
|
|
|
{
|
|
|
|
|
main_context_mutex.unlock();
|
|
|
|
|
}
|
2020-08-07 17:00:28 +02:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Backend selection
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static GPUBackend *g_backend;
|
|
|
|
|
|
|
|
|
|
void GPU_backend_init(eGPUBackendType backend_type)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(g_backend == NULL);
|
|
|
|
|
|
|
|
|
|
switch (backend_type) {
|
|
|
|
|
#if WITH_OPENGL_BACKEND
|
|
|
|
|
case GPU_BACKEND_OPENGL:
|
|
|
|
|
g_backend = new GLBackend;
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
default:
|
|
|
|
|
BLI_assert(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPU_backend_exit(void)
|
|
|
|
|
{
|
|
|
|
|
delete g_backend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPUBackend *gpu_backend_get(void)
|
|
|
|
|
{
|
|
|
|
|
return g_backend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|