Beatmup
Beatmup::GL::AbstractProgram Class Reference

Basic GLSL program. More...

#include <program.h>

Inheritance diagram for Beatmup::GL::AbstractProgram:
Beatmup::GL::RecycleBin::Item Beatmup::GL::ComputeProgram Beatmup::GL::Program Beatmup::GL::RenderingProgram

Public Member Functions

 AbstractProgram (const GraphicPipeline &gpu)
 
 ~AbstractProgram ()
 
void enable (const GraphicPipeline &gpu)
 
ChunkgetBinary () const
 
void loadBinary (const Chunk &binary)
 
handle_t findUniformLocation (const char *name)
 Retrieves uniform variable location by its name. More...
 
handle_t findAttribLocation (const char *name)
 Retrieves attribute location by its name. More...
 
handle_t getUniformLocation (const std::string &name)
 Retrieves uniform variable location by its name. More...
 
handle_t getAttribLocation (const std::string &name)
 Retrieves attribute location by its name. More...
 
void setInteger (const std::string &name, const int value, bool safe=false)
 Assigns a value to a specific integer variable in the program. More...
 
void setUnsignedInteger (const std::string &name, const unsigned int value, bool safe=false)
 
void setFloat (const std::string &name, const float value, bool safe=false)
 Assigns a value to a specific floating point variable in the program. More...
 
void setVector2 (const std::string &name, const float x, const float y)
 
void setVector3 (const std::string &name, const float x, const float y, const float z)
 
void setVector4 (const std::string &name, const float x, const float y, const float z, const float w)
 
void setVector4 (const std::string &name, const color4i &color, const float outRange=1.0f)
 
void setMatrix2 (const std::string &name, const Matrix2 &mat)
 
void setMatrix3 (const std::string &name, const Matrix2 &mat, const Point &pos)
 
void setMatrix3 (const std::string &name, const AffineMapping &mapping)
 
void setIntegerArray (const std::string &name, const int *values, const int length)
 
void setIntegerArray (const std::string &name, const int firstValue, const int length)
 
void setFloatArray (const std::string &name, const float *values, const int length)
 
void setVec2Array (const std::string &name, const float *xy, const int length)
 
void setVec4Array (const std::string &name, const float *xyzw, const int length)
 
void bindSampler (GraphicPipeline &gpu, GL::TextureHandler &image, const char *uniformId, TextureParam param)
 
void bindImage (GraphicPipeline &gpu, GL::TextureHandler &image, const char *uniformId, bool read, bool write)
 
void bindAtomicCounter (GraphicPipeline &gpu, AtomicCounter &counter, int unit)
 
- Public Member Functions inherited from Beatmup::GL::RecycleBin::Item
 Item ()
 
virtual ~Item ()
 

Protected Member Functions

handle_t getHandle () const
 
void assertLinked () const
 
void clearCaches ()
 

Private Member Functions

 AbstractProgram (const AbstractProgram &)=delete
 disabling copying constructor More...
 

Private Attributes

std::map< std::string, handle_tuniformsCache
 
std::map< std::string, handle_tattribsCache
 
handle_t handle
 

Detailed Description

Basic GLSL program.

Shaders are controlled by the user.

Definition at line 135 of file program.h.

Constructor & Destructor Documentation

◆ AbstractProgram() [1/2]

Beatmup::GL::AbstractProgram::AbstractProgram ( const AbstractProgram )
privatedelete

disabling copying constructor

◆ AbstractProgram() [2/2]

AbstractProgram::AbstractProgram ( const GraphicPipeline gpu)

Definition at line 216 of file program.cpp.

216  {
217  handle = glCreateProgram();
218 }

◆ ~AbstractProgram()

AbstractProgram::~AbstractProgram ( )

Definition at line 221 of file program.cpp.

221  {
222  glDeleteProgram(handle);
223 }

Member Function Documentation

◆ getHandle()

handle_t Beatmup::GL::AbstractProgram::getHandle ( ) const
inlineprotected

Definition at line 142 of file program.h.

142 { return handle; }

◆ assertLinked()

void AbstractProgram::assertLinked ( ) const
protected

Definition at line 226 of file program.cpp.

226  {
227  GLint status;
228  glGetProgramiv(handle, GL_LINK_STATUS, &status);
229  if (status == GL_TRUE)
230  return;
231  GLint logLength;
232  glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &logLength);
233  if (logLength > 0) {
234  GLchar* log = (GLchar*)malloc(logLength);
235  glGetProgramInfoLog(handle, logLength, &logLength, log);
236  throw GL::GLException((char*)log);
237  free(log);
238  }
239  else
240  throw GL::GLException("Program linking failed (no log).");
241 }
GPU exception.
Definition: bgl.h:56

◆ clearCaches()

void AbstractProgram::clearCaches ( )
protected

Definition at line 244 of file program.cpp.

244  {
245  uniformsCache.clear();
246  attribsCache.clear();
247 }
std::map< std::string, handle_t > uniformsCache
Definition: program.h:137
std::map< std::string, handle_t > attribsCache
Definition: program.h:137

◆ enable()

void AbstractProgram::enable ( const GraphicPipeline gpu)

Definition at line 250 of file program.cpp.

250  {
251  glUseProgram(handle);
252  GL::GLException::check("enabling a program");
253 }
static void check(const std::string &info)
Definition: bgl.h:62

◆ getBinary()

Chunk * AbstractProgram::getBinary ( ) const

Definition at line 257 of file program.cpp.

257  {
258  GLsizei length;
259  glGetProgramiv(handle, GL_PROGRAM_BINARY_LENGTH, &length);
260  GL::GLException::check("querying program size");
261 
262  // setting up a chunk: first sizeof(GLenum) bytes store the binary format
263  Chunk* result = new Chunk(sizeof(GLenum) + (size_t)length);
264  glGetProgramBinary(handle, length, nullptr, result->ptr<GLenum>(0), result->ptr<GLenum>(1));
265  GL::GLException::check("getting binary");
266  return result;
267 }
Simply a piece of binary data of a specific size.
Definition: chunkfile.h:210
Beatmup::IntPoint result

◆ loadBinary()

void AbstractProgram::loadBinary ( const Chunk binary)

Definition at line 270 of file program.cpp.

270  {
271  // convention: first sizeof(GLenum) bytes store the binary format
272  glProgramBinary(handle, binary.at<GLenum>(0), binary.ptr<GLenum>(1), binary.size() - sizeof(GLenum));
273  GL::GLException::check("loading program binary");
274 }
datatype * ptr(size_t offset=0)
Definition: chunkfile.h:264
datatype at(size_t offset) const
Definition: chunkfile.h:271
size_t size() const
Definition: chunkfile.h:257

◆ findUniformLocation()

GL::handle_t AbstractProgram::findUniformLocation ( const char *  name)

Retrieves uniform variable location by its name.

May be slow.

Parameters
[in]nameThe variable name

Definition at line 278 of file program.cpp.

278  {
279  return glGetUniformLocation(handle, name);
280 }
return(jlong) new Beatmup jlong jstring name

◆ findAttribLocation()

GL::handle_t AbstractProgram::findAttribLocation ( const char *  name)

Retrieves attribute location by its name.

May be slow.

Parameters
[in]nameThe attribute name

Definition at line 283 of file program.cpp.

283  {
284  return glGetAttribLocation(handle, name);
285 }

◆ getUniformLocation()

GL::handle_t AbstractProgram::getUniformLocation ( const std::string &  name)

Retrieves uniform variable location by its name.

Uses chache. Faster when called more than once.

Parameters
[in]nameThe variable name

Definition at line 288 of file program.cpp.

288  {
289  auto it = uniformsCache.find(name);
290  if (it != uniformsCache.end())
291  return it->second;
292  handle_t location = glGetUniformLocation(handle, name.c_str());
293  uniformsCache[name] = location;
294  return location;
295 }
unsigned int handle_t
A reference to a GPU resource.
Definition: basic_types.h:61

◆ getAttribLocation()

GL::handle_t AbstractProgram::getAttribLocation ( const std::string &  name)

Retrieves attribute location by its name.

Uses chache. Faster when called more than once.

Parameters
[in]nameThe attribute name

Definition at line 298 of file program.cpp.

298  {
299  auto it = attribsCache.find(name);
300  if (it != attribsCache.end())
301  return it->second;
302  handle_t location = glGetAttribLocation(handle, name.c_str());
303  attribsCache[name] = location;
304  return location;
305 }

◆ setInteger()

void AbstractProgram::setInteger ( const std::string &  name,
const int  value,
bool  safe = false 
)

Assigns a value to a specific integer variable in the program.

Parameters
namethe variable name
valuethe value to assign
safeif true check if the target variable exists before assigning

Definition at line 308 of file program.cpp.

308  {
309  if (safe) {
310  GLint location = getUniformLocation(name);
311  if (location == -1)
312  return;
313  glUniform1i(location, (GLint)value);
314  }
315  else
316  glUniform1i(getUniformLocation(name), (GLint)value);
317 #ifdef BEATMUP_DEBUG
318  GL::GLException::check("setting integer in program");
319 #endif
320 }
handle_t getUniformLocation(const std::string &name)
Retrieves uniform variable location by its name.
Definition: program.cpp:288

◆ setUnsignedInteger()

void AbstractProgram::setUnsignedInteger ( const std::string &  name,
const unsigned int  value,
bool  safe = false 
)

Definition at line 323 of file program.cpp.

323  {
324  if (safe) {
325  GLint location = getUniformLocation(name);
326  if (location == -1)
327  return;
328 #ifdef BEATMUP_OPENGLVERSION_GLES20
329  glUniform1i(location, (GLint)value);
330 #else
331  glUniform1ui(location, (GLuint)value);
332 #endif
333  }
334  else {
335 #ifdef BEATMUP_OPENGLVERSION_GLES20
336  glUniform1i(getUniformLocation(name), (GLint)value);
337 #else
338  glUniform1ui(getUniformLocation(name), (GLuint)value);
339 #endif
340  }
341 #ifdef BEATMUP_DEBUG
342  GL::GLException::check("setting integer in program");
343 #endif
344 }

◆ setFloat()

void AbstractProgram::setFloat ( const std::string &  name,
const float  value,
bool  safe = false 
)

Assigns a value to a specific floating point variable in the program.

Parameters
namethe variable name
valuethe value to assign
safeif true check if the target variable exists before assigning

Definition at line 347 of file program.cpp.

347  {
348  if (safe) {
349  GLint location = getUniformLocation(name);
350  if (location == -1)
351  return;
352  glUniform1f(location, (GLfloat)value);
353  }
354  else
355  glUniform1f(getUniformLocation(name), (GLfloat)value);
356 #ifdef BEATMUP_DEBUG
357  GL::GLException::check("setting float in program");
358 #endif
359 }

◆ setVector2()

void AbstractProgram::setVector2 ( const std::string &  name,
const float  x,
const float  y 
)

Definition at line 362 of file program.cpp.

362  {
363  glUniform2f(getUniformLocation(name), x, y);
364 #ifdef BEATMUP_DEBUG
365  GL::GLException::check("setting vector 2 in program");
366 #endif
367 }
jobject jlong jint jint y
jobject jlong jint x

◆ setVector3()

void AbstractProgram::setVector3 ( const std::string &  name,
const float  x,
const float  y,
const float  z 
)

Definition at line 370 of file program.cpp.

370  {
371  glUniform3f(getUniformLocation(name), x, y, z);
372 #ifdef BEATMUP_DEBUG
373  GL::GLException::check("setting vector 3 in program");
374 #endif
375 }
jlong jstring jint jint jint z

◆ setVector4() [1/2]

void AbstractProgram::setVector4 ( const std::string &  name,
const float  x,
const float  y,
const float  z,
const float  w 
)

Definition at line 378 of file program.cpp.

378  {
379  glUniform4f(getUniformLocation(name), x, y, z, w);
380 #ifdef BEATMUP_DEBUG
381  GL::GLException::check("setting vector 4 in program");
382 #endif
383 }
jlong jstring jint jint jint jint w

◆ setVector4() [2/2]

void AbstractProgram::setVector4 ( const std::string &  name,
const color4i color,
const float  outRange = 1.0f 
)

Definition at line 386 of file program.cpp.

386  {
387  const float scale = outRange / 255;
388  setVector4(name, scale * color.r, scale * color.g, scale * color.b, scale * color.a);
389 }
void setVector4(const std::string &name, const float x, const float y, const float z, const float w)
Definition: program.cpp:378
JNIEnv jlong jint jint jint jint jfloat scale

◆ setMatrix2()

void AbstractProgram::setMatrix2 ( const std::string &  name,
const Matrix2 mat 
)

Definition at line 392 of file program.cpp.

392  {
393  GLfloat m[4] = { 1, 0, 0, 1 };
394  mat.getElements(m[0], m[2], m[1], m[3]);
395  glUniformMatrix2fv(getUniformLocation(name), 1, false, m);
396 #ifdef BEATMUP_DEBUG
397  GL::GLException::check("setting matrix 2x2 in program");
398 #endif
399 }
* mat

◆ setMatrix3() [1/2]

void AbstractProgram::setMatrix3 ( const std::string &  name,
const Matrix2 mat,
const Point pos 
)

Definition at line 402 of file program.cpp.

402  {
403  GLfloat m[9] = { 1, 0, 0, 0, 1, 0, pos.x, pos.y, 1 };
404  mat.getElements(m[0], m[3], m[1], m[4]);
405  glUniformMatrix3fv(getUniformLocation(name), 1, false, m);
406 #ifdef BEATMUP_DEBUG
407  GL::GLException::check("setting matrix 3x3 in program");
408 #endif
409 }

◆ setMatrix3() [2/2]

void AbstractProgram::setMatrix3 ( const std::string &  name,
const AffineMapping mapping 
)

Definition at line 412 of file program.cpp.

412  {
414 }
void setMatrix3(const std::string &name, const Matrix2 &mat, const Point &pos)
Definition: program.cpp:402
Beatmup::AffineMapping & mapping

◆ setIntegerArray() [1/2]

void AbstractProgram::setIntegerArray ( const std::string &  name,
const int *  values,
const int  length 
)

Definition at line 417 of file program.cpp.

417  {
418  if (sizeof(GLint) != sizeof(int)) {
419  GLint* convValues = new GLint[length];
420  for (int i = 0; i < length; ++i)
421  convValues[i] = values[i];
422  glUniform1iv(getUniformLocation(name), length, convValues);
423  delete[] convValues;
424  }
425  else {
426  glUniform1iv(getUniformLocation(name), length, values);
427  }
428 #ifdef BEATMUP_DEBUG
429  GL::GLException::check("setting integer array in program");
430 #endif
431 }

◆ setIntegerArray() [2/2]

void AbstractProgram::setIntegerArray ( const std::string &  name,
const int  firstValue,
const int  length 
)

Definition at line 434 of file program.cpp.

434  {
435  static const int STORAGE_LEN = 8;
436  GLint storage[STORAGE_LEN];
437  const bool alloc = length > STORAGE_LEN;
438  GLint* values = alloc ? new GLint[length] : storage;
439  for (int i = 0; i < length; ++i)
440  values[i] = firstValue + i;
441  glUniform1iv(getUniformLocation(name), length, values);
442  if (alloc)
443  delete[] values;
444 #ifdef BEATMUP_DEBUG
445  GL::GLException::check("setting integer array in program");
446 #endif
447 }

◆ setFloatArray()

void AbstractProgram::setFloatArray ( const std::string &  name,
const float *  values,
const int  length 
)

Definition at line 450 of file program.cpp.

450  {
451  glUniform1fv(getUniformLocation(name), length, values);
452 #ifdef BEATMUP_DEBUG
453  GL::GLException::check("setting float array in program");
454 #endif
455 }

◆ setVec2Array()

void AbstractProgram::setVec2Array ( const std::string &  name,
const float *  xy,
const int  length 
)

Definition at line 458 of file program.cpp.

458  {
459  glUniform2fv(getUniformLocation(name), length, xy);
460 #ifdef BEATMUP_DEBUG
461  GL::GLException::check("setting vec2 array in program");
462 #endif
463 }
jlong jintArray xy

◆ setVec4Array()

void AbstractProgram::setVec4Array ( const std::string &  name,
const float *  xyzw,
const int  length 
)

Definition at line 466 of file program.cpp.

466  {
467  glUniform4fv(getUniformLocation(name), length, xyzw);
468 #ifdef BEATMUP_DEBUG
469  GL::GLException::check("setting vec4 array in program");
470 #endif
471 }

◆ bindSampler()

void AbstractProgram::bindSampler ( GraphicPipeline gpu,
GL::TextureHandler image,
const char *  uniformId,
TextureParam  param 
)

Definition at line 474 of file program.cpp.

474  {
475  handle_t uniform = getUniformLocation(uniformId);
476  GLint unit;
477  glGetUniformiv(getHandle(), uniform, &unit);
478  GL::GLException::check("binding sampler in program");
479  gpu.bind(image, unit, param);
480 }
handle_t getHandle() const
Definition: program.h:142
void bind(GL::TextureHandler &texture, size_t texUnit, const TextureParam param)
Definition: pipeline.cpp:881

◆ bindImage()

void AbstractProgram::bindImage ( GraphicPipeline gpu,
GL::TextureHandler image,
const char *  uniformId,
bool  read,
bool  write 
)

Definition at line 483 of file program.cpp.

483  {
484  handle_t uniform = getUniformLocation(uniformId);
485  GLint unit;
486  glGetUniformiv(getHandle(), uniform, &unit);
487  GL::GLException::check("binding image in program");
488  gpu.bind(image, unit, read, write);
489 }
void write(Bitmap &bitmap, Args &&... args)
Calls a Func< WriterClass >::process(access, params) that writes to a bitmap of any kind,...
Definition: processing.h:90
void read(Bitmap &bitmap, Args &&... args)
Calls a Func< ReaderClass >::process(access, params), where.
Definition: processing.h:49

◆ bindAtomicCounter()

void AbstractProgram::bindAtomicCounter ( GraphicPipeline gpu,
AtomicCounter counter,
int  unit 
)

Definition at line 493 of file program.cpp.

493  {
494  glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, unit, counter.handle);
495  GL::GLException::check("binding atomic counter");
496 }

Member Data Documentation

◆ uniformsCache

std::map<std::string, handle_t> Beatmup::GL::AbstractProgram::uniformsCache
private

Definition at line 137 of file program.h.

◆ attribsCache

std::map<std::string, handle_t> Beatmup::GL::AbstractProgram::attribsCache
private

Definition at line 137 of file program.h.

◆ handle

handle_t Beatmup::GL::AbstractProgram::handle
private

Definition at line 138 of file program.h.


The documentation for this class was generated from the following files: