| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2018 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <FCConfig.h> |
| 25 |
|
| 26 |
#ifndef FC_OS_WIN32 |
| 27 |
# ifndef GL_GLEXT_PROTOTYPES |
| 28 |
# define GL_GLEXT_PROTOTYPES 1 |
| 29 |
# endif |
| 30 |
#else |
| 31 |
# include <windows.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef FC_OS_MACOSX |
| 35 |
# include <OpenGL/gl.h> |
| 36 |
# include <OpenGL/glu.h> |
| 37 |
# include <OpenGL/glext.h> |
| 38 |
#else |
| 39 |
# include <GL/gl.h> |
| 40 |
# include <GL/glu.h> |
| 41 |
# include <GL/glext.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <Inventor/elements/SoGLCacheContextElement.h> |
| 45 |
#include <Inventor/errors/SoDebugError.h> |
| 46 |
#include <Inventor/misc/SoContextHandler.h> |
| 47 |
|
| 48 |
#include <string> |
| 49 |
|
| 50 |
#include "GLBuffer.h" |
| 51 |
|
| 52 |
using namespace Gui; |
| 53 |
|
| 54 |
|
| 55 |
OpenGLBuffer::OpenGLBuffer(GLenum type) |
| 56 |
: target(type) |
| 57 |
, bufferId(0) |
| 58 |
, context(-1) |
| 59 |
, currentContext(-1) |
| 60 |
, glue(nullptr) |
| 61 |
{ |
| 62 |
SoContextHandler::addContextDestructionCallback(context_destruction_cb, this); |
| 63 |
} |
| 64 |
|
| 65 |
OpenGLBuffer::~OpenGLBuffer() |
| 66 |
{ |
| 67 |
SoContextHandler::removeContextDestructionCallback(context_destruction_cb, this); |
| 68 |
|
| 69 |
destroy(); |
| 70 |
} |
| 71 |
|
| 72 |
/*! |
| 73 |
* \brief OpenGLBuffer::isVBOSupported returns if the OpenGL driver |
| 74 |
* supports the VBO extension. |
| 75 |
* When calling this function there must be a current OpenGL context. |
| 76 |
* \return |
| 77 |
*/ |
| 78 |
bool OpenGLBuffer::isVBOSupported(uint32_t ctx) |
| 79 |
{ |
| 80 |
auto glue = cc_glglue_instance(ctx); |
| 81 |
if (!glue || !cc_glglue_has_vertex_buffer_object(glue)) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
const GLubyte* str = glGetString(GL_EXTENSIONS); |
| 85 |
if (!str) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
std::string ext = reinterpret_cast<const char*>(str); |
| 89 |
return (ext.find("GL_ARB_vertex_buffer_object") != std::string::npos); |
| 90 |
} |
| 91 |
|
| 92 |
void OpenGLBuffer::setCurrentContext(uint32_t ctx) |
| 93 |
{ |
| 94 |
currentContext = ctx; |
| 95 |
glue = cc_glglue_instance(currentContext); |
| 96 |
} |
| 97 |
|
| 98 |
bool OpenGLBuffer::create() |
| 99 |
{ |
| 100 |
if (bufferId > 0) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
if (!cc_glglue_has_vertex_buffer_object(glue)) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
cc_glglue_glGenBuffers(glue, 1, &bufferId); |
| 109 |
context = currentContext; |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
bool OpenGLBuffer::isCreated() const |
| 114 |
{ |
| 115 |
return (bufferId > 0); |
| 116 |
} |
| 117 |
|
| 118 |
void OpenGLBuffer::destroy() |
| 119 |
{ |
| 120 |
// schedule delete for all allocated GL resources |
| 121 |
if (bufferId > 0) { |
| 122 |
void* ptr0 = (void*)((uintptr_t)bufferId); |
| 123 |
SoGLCacheContextElement::scheduleDeleteCallback(context, buffer_delete, ptr0); |
| 124 |
bufferId = 0; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void OpenGLBuffer::allocate(const void* data, int count) |
| 129 |
{ |
| 130 |
if (bufferId > 0) { |
| 131 |
cc_glglue_glBufferData(glue, target, count, data, GL_STATIC_DRAW); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
bool OpenGLBuffer::bind() |
| 136 |
{ |
| 137 |
if (bufferId) { |
| 138 |
if (context != currentContext) { |
| 139 |
SoDebugError::postWarning("OpenGLBuffer::bind", "buffer not created"); |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
cc_glglue_glBindBuffer(glue, target, bufferId); |
| 144 |
return true; |
| 145 |
} |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
void OpenGLBuffer::release() |
| 150 |
{ |
| 151 |
if (bufferId) { |
| 152 |
cc_glglue_glBindBuffer(glue, target, 0); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
GLuint OpenGLBuffer::getBufferId() const |
| 157 |
{ |
| 158 |
return bufferId; |
| 159 |
} |
| 160 |
|
| 161 |
uint32_t OpenGLBuffer::getBoundContext() const |
| 162 |
{ |
| 163 |
return context; |
| 164 |
} |
| 165 |
|
| 166 |
int OpenGLBuffer::size() const |
| 167 |
{ |
| 168 |
GLint value = -1; |
| 169 |
if (bufferId > 0) { |
| 170 |
cc_glglue_glGetBufferParameteriv(glue, target, GL_BUFFER_SIZE, &value); |
| 171 |
} |
| 172 |
return value; |
| 173 |
} |
| 174 |
|
| 175 |
void OpenGLBuffer::context_destruction_cb(uint32_t context, void* userdata) |
| 176 |
{ |
| 177 |
auto self = static_cast<OpenGLBuffer*>(userdata); |
| 178 |
|
| 179 |
if (self->context == context && self->bufferId) { |
| 180 |
const cc_glglue* glue = cc_glglue_instance((int)context); |
| 181 |
GLuint buffer = self->bufferId; |
| 182 |
cc_glglue_glDeleteBuffers(glue, 1, &buffer); |
| 183 |
self->context = -1; |
| 184 |
self->bufferId = 0; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void OpenGLBuffer::buffer_delete(void* closure, uint32_t contextid) |
| 189 |
{ |
| 190 |
const cc_glglue* glue = cc_glglue_instance((int)contextid); |
| 191 |
auto id = (GLuint)((uintptr_t)closure); |
| 192 |
cc_glglue_glDeleteBuffers(glue, 1, &id); |
| 193 |
} |
| 194 |
|
| 195 |
// ---------------------------------------------------------------------------- |
| 196 |
|
| 197 |
OpenGLMultiBuffer::OpenGLMultiBuffer(GLenum type) |
| 198 |
: target(type) |
| 199 |
, currentBuf(nullptr) |
| 200 |
, currentContext(-1) |
| 201 |
, glue(nullptr) |
| 202 |
{ |
| 203 |
SoContextHandler::addContextDestructionCallback(context_destruction_cb, this); |
| 204 |
} |
| 205 |
|
| 206 |
OpenGLMultiBuffer::~OpenGLMultiBuffer() |
| 207 |
{ |
| 208 |
SoContextHandler::removeContextDestructionCallback(context_destruction_cb, this); |
| 209 |
|
| 210 |
destroy(); |
| 211 |
} |
| 212 |
|
| 213 |
void OpenGLMultiBuffer::setCurrentContext(uint32_t ctx) |
| 214 |
{ |
| 215 |
currentContext = ctx; |
| 216 |
glue = cc_glglue_instance(currentContext); |
| 217 |
currentBuf = &bufs[ctx]; |
| 218 |
} |
| 219 |
|
| 220 |
bool OpenGLMultiBuffer::create() |
| 221 |
{ |
| 222 |
if (!currentBuf) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
auto& bufferId = *currentBuf; |
| 227 |
if (bufferId > 0) { |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
if (!cc_glglue_has_vertex_buffer_object(glue)) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
cc_glglue_glGenBuffers(glue, 1, &bufferId); |
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
bool OpenGLMultiBuffer::isCreated(uint32_t context) const |
| 240 |
{ |
| 241 |
auto it = bufs.find(context); |
| 242 |
return (it != bufs.end()) && (it->second > 0); |
| 243 |
} |
| 244 |
|
| 245 |
void OpenGLMultiBuffer::destroy() |
| 246 |
{ |
| 247 |
// schedule delete for all allocated GL resources |
| 248 |
for (auto& v : bufs) { |
| 249 |
if (v.second > 0) { |
| 250 |
void* ptr0 = (void*)((uintptr_t)v.second); |
| 251 |
SoGLCacheContextElement::scheduleDeleteCallback(v.first, buffer_delete, ptr0); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
bufs.clear(); |
| 256 |
currentBuf = nullptr; |
| 257 |
} |
| 258 |
|
| 259 |
void OpenGLMultiBuffer::allocate(const void* data, int count) |
| 260 |
{ |
| 261 |
if (currentBuf && *currentBuf) { |
| 262 |
cc_glglue_glBufferData(glue, target, count, data, GL_STATIC_DRAW); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
bool OpenGLMultiBuffer::bind() |
| 267 |
{ |
| 268 |
if (currentBuf && *currentBuf) { |
| 269 |
cc_glglue_glBindBuffer(glue, target, *currentBuf); |
| 270 |
return true; |
| 271 |
} |
| 272 |
|
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
void OpenGLMultiBuffer::release() |
| 277 |
{ |
| 278 |
if (currentBuf && *currentBuf) { |
| 279 |
cc_glglue_glBindBuffer(glue, target, 0); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
GLuint OpenGLMultiBuffer::getBufferId() const |
| 284 |
{ |
| 285 |
return currentBuf ? *currentBuf : 0; |
| 286 |
} |
| 287 |
|
| 288 |
int OpenGLMultiBuffer::size() const |
| 289 |
{ |
| 290 |
GLint value = -1; |
| 291 |
if (currentBuf && *currentBuf) { |
| 292 |
cc_glglue_glGetBufferParameteriv(glue, target, GL_BUFFER_SIZE, &value); |
| 293 |
} |
| 294 |
return value; |
| 295 |
} |
| 296 |
|
| 297 |
void OpenGLMultiBuffer::context_destruction_cb(uint32_t context, void* userdata) |
| 298 |
{ |
| 299 |
auto self = static_cast<OpenGLMultiBuffer*>(userdata); |
| 300 |
|
| 301 |
auto it = self->bufs.find(context); |
| 302 |
if (it != self->bufs.end() && it->second) { |
| 303 |
const cc_glglue* glue = cc_glglue_instance((int)context); |
| 304 |
GLuint buffer = it->second; |
| 305 |
cc_glglue_glDeleteBuffers(glue, 1, &buffer); |
| 306 |
if (self->currentBuf == &it->second) { |
| 307 |
self->currentBuf = nullptr; |
| 308 |
} |
| 309 |
self->bufs.erase(it); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
void OpenGLMultiBuffer::buffer_delete(void* closure, uint32_t contextid) |
| 314 |
{ |
| 315 |
const cc_glglue* glue = cc_glglue_instance((int)contextid); |
| 316 |
auto id = (GLuint)((uintptr_t)closure); |
| 317 |
cc_glglue_glDeleteBuffers(glue, 1, &id); |
| 318 |
} |