| 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/opengl/Shader.h> |
| 13 |
#include <mrpt/opengl/opengl_api.h> |
| 14 |
|
| 15 |
#include <iostream> |
| 16 |
#include <list> |
| 17 |
#include <mutex> |
| 18 |
#include <thread> |
| 19 |
|
| 20 |
using namespace std; |
| 21 |
using namespace mrpt; |
| 22 |
using namespace mrpt::opengl; |
| 23 |
|
| 24 |
// ========= clearPendingIfPossible ============= |
| 25 |
namespace mrpt::opengl::internal |
| 26 |
{ |
| 27 |
// pending to clear (ppc) lists: |
| 28 |
static std::list<std::shared_ptr<Program::Data>> pptc; |
| 29 |
static std::list<std::shared_ptr<Shader::Data>> spptc; |
| 30 |
static bool inClearPendingIfPossible = false; |
| 31 |
static std::mutex pendingToClear_mtx; |
| 32 |
|
| 33 |
// Frees those programs that were still waiting since they were attempted to |
| 34 |
// delete from a wrong thread. If we are now at that thread, clean up: |
| 35 |
void clearPendingIfPossible() |
| 36 |
{ |
| 37 |
std::lock_guard<std::mutex> lck(pendingToClear_mtx); |
| 38 |
inClearPendingIfPossible = true; |
| 39 |
|
| 40 |
for (auto it = pptc.begin(); it != pptc.end();) |
| 41 |
{ |
| 42 |
if (!*it) |
| 43 |
{ |
| 44 |
it = pptc.erase(it); |
| 45 |
continue; |
| 46 |
} |
| 47 |
if ((*it)->linkedThread == std::this_thread::get_id()) |
| 48 |
{ |
| 49 |
(*it)->destroy(); |
| 50 |
it = pptc.erase(it); |
| 51 |
continue; |
| 52 |
} |
| 53 |
else |
| 54 |
{ |
| 55 |
++it; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
for (auto it = spptc.begin(); it != spptc.end();) |
| 60 |
{ |
| 61 |
if (!*it) |
| 62 |
{ |
| 63 |
it = spptc.erase(it); |
| 64 |
continue; |
| 65 |
} |
| 66 |
if ((*it)->creationThread == std::this_thread::get_id()) |
| 67 |
{ |
| 68 |
(*it)->destroy(); |
| 69 |
it = spptc.erase(it); |
| 70 |
continue; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
++it; |
| 75 |
} |
| 76 |
} |
| 77 |
inClearPendingIfPossible = false; |
| 78 |
} |
| 79 |
} // namespace mrpt::opengl::internal |
| 80 |
|
| 81 |
// ============ Shader ============ |
| 82 |
Shader::~Shader() { clear(); } |
| 83 |
Shader& Shader::operator=(Shader&& o) |
| 84 |
{ |
| 85 |
m_data = std::move(o.m_data); |
| 86 |
o.m_data.reset(); |
| 87 |
return *this; |
| 88 |
} |
| 89 |
Shader::Shader(Shader&& o) |
| 90 |
{ |
| 91 |
m_data = std::move(o.m_data); |
| 92 |
o.m_data.reset(); |
| 93 |
} |
| 94 |
|
| 95 |
void Shader::clear() |
| 96 |
{ |
| 97 |
if (!m_data || !m_data->shader) return; |
| 98 |
|
| 99 |
// If we are in the same thread that created us, ok, clean up. |
| 100 |
// Otherwise, postpone it for later on: |
| 101 |
if (m_data->creationThread == std::this_thread::get_id()) |
| 102 |
{ |
| 103 |
m_data->destroy(); |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
// Postpone (except if we are already in the global dtor of the queue!) |
| 108 |
if (!m_data->inPostponedDestructionQueue) |
| 109 |
{ |
| 110 |
std::lock_guard<std::mutex> lck(internal::pendingToClear_mtx); |
| 111 |
m_data->inPostponedDestructionQueue = true; |
| 112 |
internal::spptc.emplace_back(m_data); |
| 113 |
} |
| 114 |
m_data = std::make_shared<Data>(); |
| 115 |
} |
| 116 |
// if (!internal::inClearPendingIfPossible) |
| 117 |
// internal::clearPendingIfPossible(); |
| 118 |
} |
| 119 |
|
| 120 |
void Shader::Data::destroy() |
| 121 |
{ |
| 122 |
if (!shader) return; |
| 123 |
|
| 124 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 125 |
// See clear() comments |
| 126 |
ASSERT_(creationThread == std::this_thread::get_id()); |
| 127 |
|
| 128 |
glDeleteShader(shader); |
| 129 |
#endif |
| 130 |
shader = 0; |
| 131 |
} |
| 132 |
|
| 133 |
bool Shader::compile( |
| 134 |
unsigned int type, |
| 135 |
const std::vector<std::string>& shaderCode, |
| 136 |
mrpt::optional_ref<std::string> outErrorMessages) |
| 137 |
{ |
| 138 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 139 |
clear(); |
| 140 |
|
| 141 |
const auto nShaderCodes = shaderCode.size(); |
| 142 |
ASSERT_(nShaderCodes >= 1); |
| 143 |
|
| 144 |
m_data->creationThread = std::this_thread::get_id(); |
| 145 |
|
| 146 |
m_data->shader = glCreateShader(static_cast<GLenum>(type)); |
| 147 |
|
| 148 |
std::vector<const GLchar*> sources(nShaderCodes); |
| 149 |
std::vector<GLint> lengths(nShaderCodes); |
| 150 |
|
| 151 |
for (size_t i = 0; i < nShaderCodes; i++) |
| 152 |
{ |
| 153 |
sources[i] = shaderCode[i].c_str(); |
| 154 |
lengths[i] = shaderCode[i].size(); |
| 155 |
} |
| 156 |
|
| 157 |
glShaderSource(m_data->shader, nShaderCodes, sources.data(), lengths.data()); |
| 158 |
glCompileShader(m_data->shader); |
| 159 |
|
| 160 |
GLint shader_ok; |
| 161 |
glGetShaderiv(m_data->shader, GL_COMPILE_STATUS, &shader_ok); |
| 162 |
if (!shader_ok) |
| 163 |
{ |
| 164 |
GLint log_length; |
| 165 |
std::string log; |
| 166 |
glGetShaderiv(m_data->shader, GL_INFO_LOG_LENGTH, &log_length); |
| 167 |
log.resize(log_length); |
| 168 |
glGetShaderInfoLog(m_data->shader, log_length, NULL, &log[0]); |
| 169 |
|
| 170 |
if (outErrorMessages) |
| 171 |
outErrorMessages.value().get() = std::move(log); |
| 172 |
else |
| 173 |
std::cerr << "[Shader::compile] Compile error: " << log << "\n"; |
| 174 |
|
| 175 |
glDeleteShader(m_data->shader); |
| 176 |
m_data->shader = 0; |
| 177 |
return false; // error |
| 178 |
} |
| 179 |
|
| 180 |
return true; // ok |
| 181 |
#else |
| 182 |
THROW_EXCEPTION("MRPT built without OpenGL support."); |
| 183 |
#endif |
| 184 |
} |
| 185 |
|
| 186 |
// ========= Program ============= |
| 187 |
Program::~Program() { clear(); } |
| 188 |
|
| 189 |
void Program::clear() |
| 190 |
{ |
| 191 |
if (!m_data || !m_data->program) return; |
| 192 |
|
| 193 |
// If we are in the same thread that created us, ok, clean up. |
| 194 |
// Otherwise, postpone it for later on: |
| 195 |
if (m_data->linkedThread == std::this_thread::get_id()) |
| 196 |
{ |
| 197 |
m_data->destroy(); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
// Postpone (except if we are already in the global dtor of the queue!) |
| 202 |
if (!m_data->inPostponedDestructionQueue) |
| 203 |
{ |
| 204 |
std::lock_guard<std::mutex> lck(internal::pendingToClear_mtx); |
| 205 |
m_data->inPostponedDestructionQueue = true; |
| 206 |
internal::pptc.emplace_back(m_data); |
| 207 |
} |
| 208 |
m_data = std::make_shared<Data>(); |
| 209 |
} |
| 210 |
if (!internal::inClearPendingIfPossible) internal::clearPendingIfPossible(); |
| 211 |
} |
| 212 |
|
| 213 |
void Program::Data::destroy() |
| 214 |
{ |
| 215 |
if (!program) return; |
| 216 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 217 |
|
| 218 |
// See clear() comments |
| 219 |
ASSERT_(linkedThread == std::this_thread::get_id()); |
| 220 |
|
| 221 |
// 1) Detach shaders from program. |
| 222 |
for (const Shader& s : shaders) glDetachShader(program, s.handle()); |
| 223 |
|
| 224 |
// 2) Delete program. |
| 225 |
glDeleteProgram(program); |
| 226 |
|
| 227 |
// 3) Delete shaders. |
| 228 |
shaders.clear(); |
| 229 |
|
| 230 |
// 4) Delete all variables: |
| 231 |
uniforms.clear(); |
| 232 |
attribs.clear(); |
| 233 |
#endif |
| 234 |
|
| 235 |
program = 0; |
| 236 |
} |
| 237 |
|
| 238 |
bool Program::linkProgram( |
| 239 |
std::vector<Shader>& shaders, mrpt::optional_ref<std::string> outErrorMessages) |
| 240 |
{ |
| 241 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 242 |
clear(); |
| 243 |
|
| 244 |
#if defined(MRPT_OS_LINUX) |
| 245 |
// Workaround to enfore wxWidgets to use GLSL>=3.3 even for wxWidgets<3.0.4 |
| 246 |
// See CWxGLCanvasBase::CWxGLCanvasBase. |
| 247 |
if (!::getenv("MESA_GL_VERSION_OVERRIDE")) |
| 248 |
{ |
| 249 |
::setenv("MESA_GL_VERSION_OVERRIDE", "3.3", 1 /*overwrite*/); |
| 250 |
} |
| 251 |
#endif |
| 252 |
|
| 253 |
m_data->program = glCreateProgram(); |
| 254 |
CHECK_OPENGL_ERROR(); |
| 255 |
ASSERT_(m_data->program != 0); |
| 256 |
|
| 257 |
// Take ownership of shaders: |
| 258 |
m_data->shaders = std::move(shaders); |
| 259 |
m_data->linkedThread = std::this_thread::get_id(); |
| 260 |
|
| 261 |
for (const auto& shader : m_data->shaders) glAttachShader(m_data->program, shader.handle()); |
| 262 |
|
| 263 |
glLinkProgram(m_data->program); |
| 264 |
CHECK_OPENGL_ERROR(); |
| 265 |
|
| 266 |
GLint program_ok; |
| 267 |
glGetProgramiv(m_data->program, GL_LINK_STATUS, &program_ok); |
| 268 |
if (!program_ok) |
| 269 |
{ |
| 270 |
GLint log_length; |
| 271 |
std::string log; |
| 272 |
glGetProgramiv(m_data->program, GL_INFO_LOG_LENGTH, &log_length); |
| 273 |
log.resize(log_length); |
| 274 |
glGetProgramInfoLog(m_data->program, log_length, NULL, &log[0]); |
| 275 |
|
| 276 |
if (outErrorMessages) |
| 277 |
outErrorMessages.value().get() = std::move(log); |
| 278 |
else |
| 279 |
std::cerr << "[Program::linkProgram] Link error: " << log << "\n"; |
| 280 |
clear(); |
| 281 |
return false; // error |
| 282 |
} |
| 283 |
|
| 284 |
return true; |
| 285 |
#else |
| 286 |
THROW_EXCEPTION("MRPT built without OpenGL support."); |
| 287 |
#endif |
| 288 |
} |
| 289 |
|
| 290 |
void Program::declareUniform(const std::string& name) |
| 291 |
{ |
| 292 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 293 |
ASSERT_(!empty()); |
| 294 |
|
| 295 |
if (m_data->uniforms.count(name) != 0) |
| 296 |
THROW_EXCEPTION_FMT("declareUniform: Name `%s` already registered", name.c_str()); |
| 297 |
|
| 298 |
const auto ret = glGetUniformLocation(m_data->program, static_cast<const GLchar*>(name.c_str())); |
| 299 |
if (ret < 0) |
| 300 |
THROW_EXCEPTION_FMT( |
| 301 |
"declareUniform: glGetUniformLocation() returned error for `%s`", name.c_str()); |
| 302 |
m_data->uniforms[name] = ret; |
| 303 |
#else |
| 304 |
THROW_EXCEPTION("MRPT built without OpenGL support."); |
| 305 |
#endif |
| 306 |
} |
| 307 |
void Program::declareAttribute(const std::string& name) |
| 308 |
{ |
| 309 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 310 |
ASSERT_(!empty()); |
| 311 |
|
| 312 |
if (m_data->attribs.count(name) != 0) |
| 313 |
THROW_EXCEPTION_FMT("declareAttribute: Name `%s` already registered", name.c_str()); |
| 314 |
|
| 315 |
const auto ret = glGetAttribLocation(m_data->program, static_cast<const GLchar*>(name.c_str())); |
| 316 |
if (ret < 0) |
| 317 |
THROW_EXCEPTION_FMT( |
| 318 |
"declareAttribute: glGetAttribLocation() returned error for `%s`", name.c_str()); |
| 319 |
m_data->attribs[name] = ret; |
| 320 |
|
| 321 |
#else |
| 322 |
THROW_EXCEPTION("MRPT built without OpenGL support."); |
| 323 |
#endif |
| 324 |
} |
| 325 |
|
| 326 |
void Program::dumpProgramDescription(std::ostream& o) const |
| 327 |
{ |
| 328 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 329 |
ASSERT_(!empty()); |
| 330 |
|
| 331 |
GLint count; |
| 332 |
|
| 333 |
GLint size; // size of the variable |
| 334 |
GLenum type; // type of the variable (float, vec3 or mat4, etc) |
| 335 |
|
| 336 |
const GLsizei bufSize = 32; // maximum name length |
| 337 |
GLchar name[bufSize]; // variable name in GLSL |
| 338 |
GLsizei length; // name length |
| 339 |
|
| 340 |
// Attributes |
| 341 |
glGetProgramiv(m_data->program, GL_ACTIVE_ATTRIBUTES, &count); |
| 342 |
o << mrpt::format("Active Attributes: %d\n", count); |
| 343 |
|
| 344 |
for (GLint i = 0; i < count; i++) |
| 345 |
{ |
| 346 |
glGetActiveAttrib(m_data->program, (GLuint)i, bufSize, &length, &size, &type, name); |
| 347 |
|
| 348 |
o << mrpt::format("Attribute #%d Type: %u Name: %s\n", i, type, name); |
| 349 |
} |
| 350 |
|
| 351 |
// Uniforms |
| 352 |
glGetProgramiv(m_data->program, GL_ACTIVE_UNIFORMS, &count); |
| 353 |
printf("Active Uniforms: %d\n", count); |
| 354 |
|
| 355 |
for (GLint i = 0; i < count; i++) |
| 356 |
{ |
| 357 |
glGetActiveUniform(m_data->program, (GLuint)i, bufSize, &length, &size, &type, name); |
| 358 |
|
| 359 |
o << mrpt::format("Uniform #%d Type: %u Name: %s\n", i, type, name); |
| 360 |
} |
| 361 |
#endif |
| 362 |
} |
| 363 |
|
| 364 |
void Program::use() |
| 365 |
{ |
| 366 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 367 |
glUseProgram(programId()); |
| 368 |
CHECK_OPENGL_ERROR(); |
| 369 |
#endif |
| 370 |
} |
| 371 |
|
| 372 |
void Program::setInt(const char* uniformName, int value) const |
| 373 |
{ |
| 374 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 375 |
glUniform1i(uniformId(uniformName), value); |
| 376 |
#endif |
| 377 |
} |
| 378 |
|
| 379 |
void Program::setFloat(const char* uniformName, float value, bool failIfNotExists) const |
| 380 |
{ |
| 381 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 382 |
if (!failIfNotExists && !hasUniform(uniformName)) return; |
| 383 |
|
| 384 |
glUniform1f(uniformId(uniformName), value); |
| 385 |
#endif |
| 386 |
} |
| 387 |
|
| 388 |
void Program::setFloat3(const char* uniformName, float v1, float v2, float v3) const |
| 389 |
{ |
| 390 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 391 |
glUniform3f(uniformId(uniformName), v1, v2, v3); |
| 392 |
#endif |
| 393 |
} |
| 394 |
|
| 395 |
void Program::setFloat4(const char* uniformName, float v1, float v2, float v3, float v4) const |
| 396 |
{ |
| 397 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 398 |
glUniform4f(uniformId(uniformName), v1, v2, v3, v4); |
| 399 |
#endif |
| 400 |
} |