| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "opengl-precomp.h" // Precompiled header |
| 11 |
// |
| 12 |
#include <mrpt/core/Clock.h> |
| 13 |
#include <mrpt/core/backtrace.h> |
| 14 |
#include <mrpt/core/exceptions.h> |
| 15 |
#include <mrpt/core/get_env.h> |
| 16 |
#include <mrpt/opengl/Buffer.h> |
| 17 |
#include <mrpt/opengl/opengl_api.h> |
| 18 |
|
| 19 |
#include <cstdlib> |
| 20 |
#include <iostream> |
| 21 |
|
| 22 |
using namespace mrpt::opengl; |
| 23 |
|
| 24 |
Buffer::Buffer(const Buffer::Type type) : m_impl(type) {} |
| 25 |
|
| 26 |
Buffer::RAII_Impl::RAII_Impl(Buffer::Type t) : type(t) {} |
| 27 |
Buffer::RAII_Impl::~RAII_Impl() |
| 28 |
{ |
| 29 |
// Free opengl resources: |
| 30 |
destroy(); |
| 31 |
} |
| 32 |
|
| 33 |
void Buffer::RAII_Impl::create() |
| 34 |
{ |
| 35 |
destroy(); |
| 36 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 37 |
GLuint buffer; |
| 38 |
glGenBuffers(1, &buffer); |
| 39 |
this->buffer_id = buffer; |
| 40 |
this->created_from = std::this_thread::get_id(); |
| 41 |
created = true; |
| 42 |
#endif |
| 43 |
} |
| 44 |
|
| 45 |
void Buffer::RAII_Impl::destroy() |
| 46 |
{ |
| 47 |
if (!created) return; |
| 48 |
|
| 49 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 50 |
static const bool showErrs = (::getenv("MRPT_REVEAL_OPENGL_BUFFER_LEAKS") != nullptr); |
| 51 |
|
| 52 |
if (created_from == std::this_thread::get_id()) |
| 53 |
{ |
| 54 |
unbind(); |
| 55 |
glDeleteBuffers(1, &buffer_id); |
| 56 |
} |
| 57 |
else if (showErrs) |
| 58 |
{ |
| 59 |
// at least, emit a warning: |
| 60 |
static thread_local double tLast = 0; |
| 61 |
auto tNow = mrpt::Clock::nowDouble(); |
| 62 |
if (tNow - tLast > 2.0) |
| 63 |
{ |
| 64 |
tLast = tNow; |
| 65 |
|
| 66 |
mrpt::TCallStackBackTrace bt; |
| 67 |
mrpt::callStackBackTrace(bt); |
| 68 |
|
| 69 |
std::cerr << "[Buffer::RAII_Impl] *Warning* Leaking memory " |
| 70 |
"since Buffer was acquired from a different thread " |
| 71 |
"and cannot free it from this thread, call stack:" |
| 72 |
<< bt.asString() << std::endl; |
| 73 |
} |
| 74 |
} |
| 75 |
#endif |
| 76 |
buffer_id = 0; |
| 77 |
created = false; |
| 78 |
} |
| 79 |
|
| 80 |
void Buffer::RAII_Impl::bind() |
| 81 |
{ |
| 82 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 83 |
ASSERT_(created); |
| 84 |
glBindBuffer(static_cast<GLenum>(type), buffer_id); |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
void Buffer::RAII_Impl::unbind() |
| 89 |
{ |
| 90 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 91 |
if (!created) return; |
| 92 |
if (created_from != std::this_thread::get_id()) return; |
| 93 |
|
| 94 |
glBindBuffer(static_cast<GLenum>(type), 0); |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
void Buffer::RAII_Impl::allocate(const void* data, int byteCount) |
| 99 |
{ |
| 100 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 101 |
ASSERT_(created); |
| 102 |
glBufferData(static_cast<GLenum>(type), byteCount, data, static_cast<GLenum>(usage)); |
| 103 |
#endif |
| 104 |
} |