2018-07-17 14:46:44 +02:00
|
|
|
/*
|
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): Blender Foundation, Clément Foucault
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
/** \file blender/gpu/gpu_vertex_array_id.cpp
|
2018-07-17 14:46:44 +02:00
|
|
|
* \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
|
|
|
|
|
*/
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-17 21:11:23 +02:00
|
|
|
#include "gpu_batch_private.h"
|
|
|
|
|
#include "GPU_vertex_array_id.h"
|
|
|
|
|
#include "GPU_context.h"
|
2018-02-19 23:50:52 +01:00
|
|
|
#include <vector>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <mutex>
|
2018-03-19 10:47:01 +01:00
|
|
|
#include <unordered_set>
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-02 19:39:50 +02:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-03 13:53:20 +02:00
|
|
|
#if 0
|
2018-02-19 23:50:52 +01:00
|
|
|
extern "C" {
|
2018-07-17 14:46:44 +02:00
|
|
|
extern int BLI_thread_is_main(void); /* Blender-specific function */
|
2018-02-19 23:50:52 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-17 14:46:44 +02:00
|
|
|
static bool thread_is_main() {
|
|
|
|
|
/* "main" here means the GL context's thread */
|
2018-02-19 23:50:52 +01:00
|
|
|
return BLI_thread_is_main();
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
2018-07-03 13:53:20 +02:00
|
|
|
#endif
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
struct GPUContext {
|
2018-02-19 23:50:52 +01:00
|
|
|
GLuint default_vao;
|
2018-07-18 00:12:21 +02:00
|
|
|
std::unordered_set<GPUBatch*> batches; /* Batches that have VAOs from this context */
|
2018-02-19 23:50:52 +01:00
|
|
|
std::vector<GLuint> orphaned_vertarray_ids;
|
2018-07-17 14:46:44 +02:00
|
|
|
std::mutex orphans_mutex; /* todo: try spinlock instead */
|
2018-02-19 23:50:52 +01:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-17 14:46:44 +02:00
|
|
|
pthread_t thread; /* Thread on which this context is active. */
|
2018-02-21 18:58:29 -03:00
|
|
|
bool thread_is_used;
|
2018-02-22 14:31:10 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUContext() {
|
2018-02-22 14:31:10 +01:00
|
|
|
thread_is_used = false;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-22 19:22:50 +13:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1800)
|
|
|
|
|
#define thread_local __declspec(thread)
|
2018-07-18 00:12:21 +02:00
|
|
|
thread_local GPUContext* active_ctx = NULL;
|
2018-02-22 19:22:50 +13:00
|
|
|
#else
|
2018-07-18 00:12:21 +02:00
|
|
|
static thread_local GPUContext* active_ctx = NULL;
|
2018-02-22 19:22:50 +13:00
|
|
|
#endif
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
static void clear_orphans(GPUContext* ctx)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-02-19 23:50:52 +01:00
|
|
|
ctx->orphans_mutex.lock();
|
2018-07-17 14:46:44 +02:00
|
|
|
if (!ctx->orphaned_vertarray_ids.empty()) {
|
|
|
|
|
uint orphan_len = (uint)ctx->orphaned_vertarray_ids.size();
|
2018-07-08 13:05:41 +02:00
|
|
|
glDeleteVertexArrays(orphan_len, ctx->orphaned_vertarray_ids.data());
|
2018-02-19 23:50:52 +01:00
|
|
|
ctx->orphaned_vertarray_ids.clear();
|
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
ctx->orphans_mutex.unlock();
|
|
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUContext* GPU_context_create(void)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-02-19 23:50:52 +01:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-17 14:46:44 +02:00
|
|
|
/* assert(thread_is_main()); */
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUContext* ctx = new GPUContext;
|
2018-02-19 23:50:52 +01:00
|
|
|
glGenVertexArrays(1, &ctx->default_vao);
|
2018-07-18 00:12:21 +02:00
|
|
|
GPU_context_active_set(ctx);
|
2018-02-19 23:50:52 +01:00
|
|
|
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) */
|
|
|
|
|
void GPU_context_discard(GPUContext* ctx)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-02-19 23:50:52 +01:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-17 14:46:44 +02:00
|
|
|
/* Make sure no other thread has locked it. */
|
2018-02-19 23:50:52 +01:00
|
|
|
assert(ctx == active_ctx);
|
2018-02-21 18:58:29 -03:00
|
|
|
assert(pthread_equal(pthread_self(), ctx->thread));
|
2018-02-19 23:50:52 +01:00
|
|
|
assert(ctx->orphaned_vertarray_ids.empty());
|
|
|
|
|
#endif
|
2018-07-17 14:46:44 +02:00
|
|
|
/* delete remaining vaos */
|
|
|
|
|
while (!ctx->batches.empty()) {
|
|
|
|
|
/* this removes the array entry */
|
2018-07-18 00:12:21 +02:00
|
|
|
GPU_batch_vao_cache_clear(*ctx->batches.begin());
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
glDeleteVertexArrays(1, &ctx->default_vao);
|
2018-02-22 14:31:10 +01:00
|
|
|
delete ctx;
|
2018-02-19 23:50:52 +01:00
|
|
|
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-18 00:12:21 +02: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
|
2018-07-17 14:46:44 +02:00
|
|
|
if (active_ctx) {
|
2018-02-21 18:58:29 -03:00
|
|
|
active_ctx->thread_is_used = false;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
|
|
|
|
/* Make sure no other context is already bound to this thread. */
|
|
|
|
|
if (ctx) {
|
|
|
|
|
/* Make sure no other thread has locked it. */
|
2018-02-21 18:58:29 -03:00
|
|
|
assert(ctx->thread_is_used == false);
|
2018-02-19 23:50:52 +01:00
|
|
|
ctx->thread = pthread_self();
|
2018-02-21 18:58:29 -03:00
|
|
|
ctx->thread_is_used = true;
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
2018-07-17 14:46:44 +02:00
|
|
|
if (ctx) {
|
2018-02-19 23:50:52 +01:00
|
|
|
clear_orphans(ctx);
|
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
active_ctx = ctx;
|
|
|
|
|
}
|
2018-02-19 23:50:52 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
GPUContext* GPU_context_active_get(void)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-02-19 23:50:52 +01: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
|
|
|
{
|
2018-02-19 23:50:52 +01:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-17 14:46:44 +02:00
|
|
|
assert(active_ctx); /* need at least an active context */
|
|
|
|
|
assert(pthread_equal(pthread_self(), active_ctx->thread)); /* context has been activated by another thread! */
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
|
|
|
|
return active_ctx->default_vao;
|
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_alloc(void)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-02-19 23:50:52 +01:00
|
|
|
#if TRUST_NO_ONE
|
2018-07-17 14:46:44 +02:00
|
|
|
assert(active_ctx); /* need at least an active context */
|
|
|
|
|
assert(pthread_equal(pthread_self(), active_ctx->thread)); /* context has been activated by another thread! */
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
|
|
|
|
clear_orphans(active_ctx);
|
|
|
|
|
|
|
|
|
|
GLuint new_vao_id = 0;
|
|
|
|
|
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-17 14:46:44 +02:00
|
|
|
/* this can be called from multiple thread */
|
2018-07-18 00:12:21 +02:00
|
|
|
void GPU_vao_free(GLuint vao_id, GPUContext* ctx)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-02-21 18:58:29 -03:00
|
|
|
#if TRUST_NO_ONE
|
|
|
|
|
assert(ctx);
|
|
|
|
|
#endif
|
2018-07-17 14:46:44 +02:00
|
|
|
if (ctx == active_ctx) {
|
2018-02-19 23:50:52 +01:00
|
|
|
glDeleteVertexArrays(1, &vao_id);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2018-02-19 23:50:52 +01:00
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
|
ctx->orphaned_vertarray_ids.emplace_back(vao_id);
|
|
|
|
|
ctx->orphans_mutex.unlock();
|
|
|
|
|
}
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-22 12:39:57 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
void gpu_context_add_batch(GPUContext* ctx, GPUBatch* batch)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-03-19 10:47:01 +01:00
|
|
|
ctx->batches.emplace(batch);
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|
2018-02-22 12:39:57 +01:00
|
|
|
|
2018-07-18 00:12:21 +02:00
|
|
|
void gpu_context_remove_batch(GPUContext* ctx, GPUBatch* batch)
|
2018-07-17 14:46:44 +02:00
|
|
|
{
|
2018-04-04 17:24:57 +02:00
|
|
|
ctx->orphans_mutex.lock();
|
2018-03-19 10:47:01 +01:00
|
|
|
ctx->batches.erase(batch);
|
2018-04-04 17:24:57 +02:00
|
|
|
ctx->orphans_mutex.unlock();
|
2018-07-17 14:46:44 +02:00
|
|
|
}
|