move OpenGLBuffer class to core system

This commit is contained in:
wmayer
2018-08-16 01:30:23 +02:00
parent 59b8f21585
commit 266f82bca4
4 changed files with 255 additions and 133 deletions

View File

@@ -55,6 +55,7 @@
#include <Gui/SoFCInteractiveElement.h>
#include <Gui/SoFCSelectionAction.h>
#include <Gui/GLBuffer.h>
#include "SoFCIndexedFaceSet.h"
#define RENDER_GL_VAO
@@ -66,141 +67,10 @@ using namespace MeshGui;
#if defined RENDER_GL_VAO
class CoinOpenGLBuffer {
public:
CoinOpenGLBuffer(GLenum type)
: target(type)
, bufferId(0)
, context(-1)
, currentContext(-1)
, glue(0)
{
SoContextHandler::addContextDestructionCallback(context_destruction_cb, this);
}
~CoinOpenGLBuffer()
{
SoContextHandler::removeContextDestructionCallback(context_destruction_cb, this);
destroy();
}
void setCurrentContext(uint32_t ctx)
{
currentContext = ctx;
glue = cc_glglue_instance(currentContext);
}
bool create()
{
if (bufferId > 0)
return true;
#ifdef FC_OS_WIN32
PFNGLGENBUFFERSPROC glGenBuffersARB = (PFNGLGENBUFFERSPROC)cc_glglue_getprocaddress(glue, "glGenBuffersARB");
#endif
glGenBuffersARB(1, &bufferId);
context = currentContext;
return true;
}
bool isCreated() const
{
return (bufferId > 0);
}
void destroy()
{
// schedule delete for all allocated GL resources
if (bufferId > 0) {
void * ptr0 = (void*) ((uintptr_t) bufferId);
SoGLCacheContextElement::scheduleDeleteCallback(context, buffer_delete, ptr0);
bufferId = 0;
}
}
void allocate(const void *data, int count)
{
if (bufferId > 0) {
#ifdef FC_OS_WIN32
PFNGLBUFFERDATAPROC glBufferDataARB = (PFNGLBUFFERDATAPROC)cc_glglue_getprocaddress(glue, "glBufferDataARB");
#endif
glBufferDataARB(target, count, data, GL_STATIC_DRAW);
}
}
bool bind()
{
if (bufferId) {
if (context != currentContext) {
SoDebugError::postWarning("CoinOpenGLBuffer::bind",
"buffer not created");
return false;
}
#ifdef FC_OS_WIN32
PFNGLBINDBUFFERPROC glBindBufferARB = (PFNGLBINDBUFFERPROC)cc_glglue_getprocaddress(glue, "glBindBufferARB");
#endif
glBindBufferARB(target, bufferId);
return true;
}
return false;
}
void release()
{
if (bufferId) {
#ifdef FC_OS_WIN32
PFNGLBINDBUFFERPROC glBindBufferARB = (PFNGLBINDBUFFERPROC)cc_glglue_getprocaddress(glue, "glBindBufferARB");
#endif
glBindBufferARB(target, 0);
}
}
GLuint getBufferId() const
{
return bufferId;
}
uint32_t getBoundContext() const
{
return context;
}
int size() const
{
GLint value = -1;
if (bufferId > 0) {
#ifdef FC_OS_WIN32
PFNGLGETBUFFERPARAMETERIVPROC glGetBufferParameteriv = (PFNGLGETBUFFERPARAMETERIVPROC)cc_glglue_getprocaddress(glue, "glGetBufferParameterivARB");
#endif
glGetBufferParameteriv(target, GL_BUFFER_SIZE, &value);
}
return value;
}
private:
static void context_destruction_cb(uint32_t context, void * userdata)
{
CoinOpenGLBuffer * self = static_cast<CoinOpenGLBuffer*>(userdata);
if (self->context == context && self->bufferId) {
#ifdef FC_OS_WIN32
const cc_glglue * glue = cc_glglue_instance((int) context);
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)cc_glglue_getprocaddress(glue, "glDeleteBuffersARB");
#endif
GLuint buffer = self->bufferId;
glDeleteBuffersARB(1, &buffer);
self->context = -1;
self->bufferId = 0;
}
}
static void buffer_delete(void * closure, uint32_t contextid)
{
const cc_glglue * glue = cc_glglue_instance((int) contextid);
GLuint id = (GLuint) ((uintptr_t) closure);
cc_glglue_glDeleteBuffers(glue, 1, &id);
}
GLenum target;
GLuint bufferId;
uint32_t context;
uint32_t currentContext;
const cc_glglue* glue;
};
class MeshRenderer::Private {
public:
CoinOpenGLBuffer vertices;
CoinOpenGLBuffer indices;
Gui::OpenGLBuffer vertices;
Gui::OpenGLBuffer indices;
const SbColor * pcolors;
SoMaterialBindingElement::Binding matbinding;
bool initialized;