2018-02-19 23:50:52 +01:00
|
|
|
|
|
|
|
// Gawain vertex array IDs
|
|
|
|
//
|
|
|
|
// This code is part of the Gawain library, with modifications
|
|
|
|
// specific to integration with Blender.
|
|
|
|
//
|
|
|
|
// Copyright 2016 Mike Erwin, Clément Foucault
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
|
|
|
|
// the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.#include "buffer_id.h"
|
|
|
|
|
2018-02-22 12:39:57 +01:00
|
|
|
#include "gwn_batch_private.h"
|
2018-02-19 23:50:52 +01:00
|
|
|
#include "gwn_vertex_array_id.h"
|
|
|
|
#include "gwn_context.h"
|
|
|
|
#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" {
|
|
|
|
extern int BLI_thread_is_main(void); // Blender-specific function
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool thread_is_main()
|
|
|
|
{
|
|
|
|
// "main" here means the GL context's thread
|
|
|
|
return BLI_thread_is_main();
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-03 13:53:20 +02:00
|
|
|
#endif
|
2018-02-19 23:50:52 +01:00
|
|
|
|
|
|
|
struct Gwn_Context {
|
|
|
|
GLuint default_vao;
|
2018-03-19 10:47:01 +01:00
|
|
|
std::unordered_set<Gwn_Batch*> batches; // Batches that have VAOs from this context
|
2018-02-19 23:50:52 +01:00
|
|
|
std::vector<GLuint> orphaned_vertarray_ids;
|
|
|
|
std::mutex orphans_mutex; // todo: try spinlock instead
|
|
|
|
#if TRUST_NO_ONE
|
|
|
|
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
|
|
|
|
|
|
|
Gwn_Context()
|
|
|
|
{
|
|
|
|
thread_is_used = false;
|
|
|
|
}
|
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)
|
|
|
|
thread_local Gwn_Context* active_ctx = NULL;
|
|
|
|
#else
|
2018-02-19 23:50:52 +01:00
|
|
|
static thread_local Gwn_Context* active_ctx = NULL;
|
2018-02-22 19:22:50 +13:00
|
|
|
#endif
|
2018-02-19 23:50:52 +01:00
|
|
|
|
|
|
|
static void clear_orphans(Gwn_Context* ctx)
|
|
|
|
{
|
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
if (!ctx->orphaned_vertarray_ids.empty())
|
|
|
|
{
|
|
|
|
unsigned orphan_ct = (unsigned)ctx->orphaned_vertarray_ids.size();
|
|
|
|
glDeleteVertexArrays(orphan_ct, ctx->orphaned_vertarray_ids.data());
|
|
|
|
ctx->orphaned_vertarray_ids.clear();
|
|
|
|
}
|
|
|
|
ctx->orphans_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
Gwn_Context* GWN_context_create(void)
|
|
|
|
{
|
|
|
|
#if TRUST_NO_ONE
|
2018-07-03 12:32:01 +02:00
|
|
|
// assert(thread_is_main());
|
2018-02-19 23:50:52 +01:00
|
|
|
#endif
|
2018-02-22 14:31:10 +01:00
|
|
|
Gwn_Context* ctx = new Gwn_Context;
|
2018-02-19 23:50:52 +01:00
|
|
|
glGenVertexArrays(1, &ctx->default_vao);
|
|
|
|
GWN_context_active_set(ctx);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// to be called after GWN_context_active_set(ctx_to_destroy)
|
|
|
|
void GWN_context_discard(Gwn_Context* ctx)
|
|
|
|
{
|
|
|
|
#if TRUST_NO_ONE
|
|
|
|
// Make sure no other thread has locked it.
|
|
|
|
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-02-22 12:39:57 +01:00
|
|
|
// delete remaining vaos
|
|
|
|
while (!ctx->batches.empty())
|
|
|
|
{
|
|
|
|
// this removes the array entry
|
2018-03-19 10:47:01 +01:00
|
|
|
gwn_batch_vao_cache_clear(*ctx->batches.begin());
|
2018-02-22 12:39:57 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ctx can be NULL
|
|
|
|
void GWN_context_active_set(Gwn_Context* ctx)
|
|
|
|
{
|
|
|
|
#if TRUST_NO_ONE
|
|
|
|
if (active_ctx)
|
2018-02-21 18:58:29 -03:00
|
|
|
active_ctx->thread_is_used = false;
|
2018-02-19 23:50:52 +01: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-02-19 23:50:52 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (ctx)
|
|
|
|
clear_orphans(ctx);
|
|
|
|
active_ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gwn_Context* GWN_context_active_get(void)
|
|
|
|
{
|
|
|
|
return active_ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint GWN_vao_default(void)
|
|
|
|
{
|
|
|
|
#if TRUST_NO_ONE
|
|
|
|
assert(active_ctx); // need at least an active context
|
2018-02-21 18:58:29 -03:00
|
|
|
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-02-20 01:55:19 +01:00
|
|
|
GLuint GWN_vao_alloc(void)
|
2018-02-19 23:50:52 +01:00
|
|
|
{
|
|
|
|
#if TRUST_NO_ONE
|
|
|
|
assert(active_ctx); // need at least an active context
|
2018-02-21 18:58:29 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this can be called from multiple thread
|
2018-02-20 01:55:19 +01:00
|
|
|
void GWN_vao_free(GLuint vao_id, Gwn_Context* ctx)
|
2018-02-19 23:50:52 +01:00
|
|
|
{
|
2018-02-21 18:58:29 -03:00
|
|
|
#if TRUST_NO_ONE
|
|
|
|
assert(ctx);
|
|
|
|
#endif
|
2018-02-19 23:50:52 +01:00
|
|
|
if (ctx == active_ctx)
|
|
|
|
glDeleteVertexArrays(1, &vao_id);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx->orphans_mutex.lock();
|
|
|
|
ctx->orphaned_vertarray_ids.emplace_back(vao_id);
|
|
|
|
ctx->orphans_mutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 12:39:57 +01:00
|
|
|
|
|
|
|
void gwn_context_add_batch(Gwn_Context* ctx, Gwn_Batch* batch)
|
|
|
|
{
|
2018-03-19 10:47:01 +01:00
|
|
|
ctx->batches.emplace(batch);
|
2018-02-22 12:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void gwn_context_remove_batch(Gwn_Context* ctx, Gwn_Batch* batch)
|
|
|
|
{
|
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-02-22 12:39:57 +01:00
|
|
|
}
|