Beatmup
Beatmup::GL::RenderingPrograms Class Reference

Handles a collection of common rendering programs of predefined types and shared operations among these programs. More...

#include <rendering_programs.h>

Classes

class  Backend
 

Public Types

enum class  Operation {
  BLEND , MASKED_BLEND , MASKED_8BIT_BLEND , SHAPED_BLEND ,
  BLEND_EXT , MASKED_BLEND_EXT , MASKED_8BIT_BLEND_EXT , SHAPED_BLEND_EXT
}
 Standard rendering operations. More...
 

Public Member Functions

void enableProgram (GraphicPipeline *gpu, Operation program)
 Select and enable a common program. More...
 
ProgramgetCurrentProgram ()
 
void bindMask (GraphicPipeline *gpu, AbstractBitmap &mask)
 Binds a mask to a masked rendering program. More...
 
void blend (bool onScreen)
 Performs the blending operation. More...
 
void paveBackground (GraphicPipeline *gpu, TextureHandler &content, GL::TextureHandler *output)
 Fills background with a repeated texture taking 1 pixel of this texture per 1 pixel of the output. More...
 
const VertexShadergetDefaultVertexShader (const GraphicPipeline *gpu) const
 
 RenderingPrograms (GraphicPipeline *gpu)
 
 ~RenderingPrograms ()
 

Static Public Attributes

static const char * VERTEX_COORD_ATTRIB_NAME = "inVertex"
 vertex coordinate attribute name in vertex shaders More...
 
static const char * TEXTURE_COORD_ATTRIB_NAME = "inTexCoord"
 texture coordinate attribute name in vertex shaders More...
 
static const char * VERTICAL_FLIP_ID = "flipVertically"
 Vertical flipping variable name in vertex shader. More...
 
static const char * MODELVIEW_MATRIX_ID = "modelview"
 Modelview matrix (mapping input geometry to output) shader variable name in vertex shader. More...
 
static const char * TEXTURE_COORDINATES_ID = "texCoord"
 Texture coordinates shader variable name in vertex shader. More...
 
static const char * DECLARE_TEXTURE_COORDINATES_IN_FRAG = "varying highp vec2 texCoord;\n"
 Declaring texture coordinates in fragment shader. More...
 

Private Member Functions

ProgramgetProgram (const GraphicPipeline *gpu, Operation program)
 

Private Attributes

Backendbackend
 
ProgramcurrentGlProgram
 
Operation currentProgram
 
bool maskSetUp
 
VertexShader defaultVertexShader
 
std::map< Operation, RenderingProgramprograms
 

Detailed Description

Handles a collection of common rendering programs of predefined types and shared operations among these programs.

Programs compilation and linking is done in a deferred fashion, so that the first rendering pass would usually take much more time, but no time is spent on initializing programs that will never be used.

Definition at line 33 of file rendering_programs.h.

Member Enumeration Documentation

◆ Operation

Standard rendering operations.

Enumerator
BLEND 

default blending of a single image

MASKED_BLEND 

blending an image through a pixelwise mask

MASKED_8BIT_BLEND 

blending an image through a 8 bit pixelwise mask

SHAPED_BLEND 

shaping an image

BLEND_EXT 

default blending using a texture extension

MASKED_BLEND_EXT 

blending an image through a pixelwise mask (texture extension)

MASKED_8BIT_BLEND_EXT 

blending an image through a 8 bit pixelwise mask (texture extension)

SHAPED_BLEND_EXT 

shaping an image (texture extension)

Definition at line 38 of file rendering_programs.h.

38  {
39  BLEND, //!< default blending of a single image
40  MASKED_BLEND, //!< blending an image through a pixelwise mask
41  MASKED_8BIT_BLEND, //!< blending an image through a 8 bit pixelwise mask
42  SHAPED_BLEND, //!< shaping an image
43  BLEND_EXT, //!< default blending using a texture extension
44  MASKED_BLEND_EXT, //!< blending an image through a pixelwise mask (texture extension)
45  MASKED_8BIT_BLEND_EXT, //!< blending an image through a 8 bit pixelwise mask (texture extension)
46  SHAPED_BLEND_EXT, //!< shaping an image (texture extension)
47  };

Constructor & Destructor Documentation

◆ RenderingPrograms()

RenderingPrograms::RenderingPrograms ( GraphicPipeline gpu)

Definition at line 249 of file rendering_programs.cpp.

249  :
251 {}
@ BEATMUP_DIALECT
pseudo-extension enabling Beatmup GLSL dialect
Definition: program.h:65
static const char * VERTEX_SHADER_BLEND

◆ ~RenderingPrograms()

RenderingPrograms::~RenderingPrograms ( )

Definition at line 254 of file rendering_programs.cpp.

254  {
255  delete backend;
256 }

Member Function Documentation

◆ enableProgram()

void RenderingPrograms::enableProgram ( GraphicPipeline gpu,
Operation  program 
)

Select and enable a common program.

If the selected program is not yet ready, it is linked and complied.

Parameters
[in,out]gpuA graphic pipeline instance
[in,out]programThe selected common program to use

Definition at line 314 of file rendering_programs.cpp.

314  {
315  Program& glProgram = getProgram(gpu, operation);
316  currentProgram = operation;
317  currentGlProgram = &glProgram;
318  glProgram.enable(*gpu);
320  glProgram.setInteger("image", TextureUnits::IMAGE);
321  switch (operation) {
324  glProgram.setInteger("maskLookup", TextureUnits::MASK_LOOKUP);
327  glProgram.setInteger("mask", TextureUnits::MASK);
328  default: break;
329  }
330  maskSetUp = false;
331 }
static const CustomRectangle UNIT_SQUARE
Definition: geometry.h:322
void setInteger(const std::string &name, const int value, bool safe=false)
Assigns a value to a specific integer variable in the program.
Definition: program.cpp:308
void enable(const GraphicPipeline &gpu)
Definition: program.cpp:250
Regular OpenGL program.
Definition: program.h:229
Program & getProgram(const GraphicPipeline *gpu, Operation program)
@ MASKED_BLEND_EXT
blending an image through a pixelwise mask (texture extension)
@ MASKED_8BIT_BLEND_EXT
blending an image through a 8 bit pixelwise mask (texture extension)
@ MASKED_8BIT_BLEND
blending an image through a 8 bit pixelwise mask
@ MASKED_BLEND
blending an image through a pixelwise mask
void setTextureCoordinates(const Rectangle &coords)
Specifies texture coordinates for the next rendering pass.
Definition: pipeline.cpp:966
@ MASK_LOOKUP

◆ getCurrentProgram()

Program & RenderingPrograms::getCurrentProgram ( )
Returns
the currently used rendering program.

Definition at line 334 of file rendering_programs.cpp.

334  {
335  if (!currentGlProgram)
336  RuntimeError("No current program");
337  return *currentGlProgram;
338 }

◆ bindMask()

void RenderingPrograms::bindMask ( GraphicPipeline gpu,
AbstractBitmap mask 
)

Binds a mask to a masked rendering program.

Throws an exception when a mask is about to be bound to a non-masked rendering program.

Parameters
[in,out]gpuA graphic pipeline instance
[in,out]maskThe mask bitmap

Definition at line 341 of file rendering_programs.cpp.

341  {
342  Program& program = getCurrentProgram();
344  if (mask.getBitsPerPixel() < 8) {
346  program.setFloat("blockSize", 8.0f / mask.getBitsPerPixel() / mask.getWidth());
347  program.setFloat("pixOffset", 0.5f / mask.getWidth());
348  }
349  maskSetUp = true;
350 }
virtual const PixelFormat getPixelFormat() const =0
Pixel format of the bitmap.
const unsigned char getBitsPerPixel() const
Returns number of bits per pixel stored in each bitmap.
void setFloat(const std::string &name, const float value, bool safe=false)
Assigns a value to a specific floating point variable in the program.
Definition: program.cpp:347
virtual const int getWidth() const =0
Width of the texture in pixels.
void bind(GL::TextureHandler &texture, size_t texUnit, const TextureParam param)
Definition: pipeline.cpp:881
@ INTERP_NEAREST
nearest neighbor pixel interpolation

◆ blend()

void RenderingPrograms::blend ( bool  onScreen)

Performs the blending operation.

Parameters
[in]onScreenIf true, the rendering is performed on a screen, not into a bitmap.

Definition at line 353 of file rendering_programs.cpp.

353  {
354 #ifdef BEATMUP_DEBUG
356  DebugAssertion::check(maskSetUp, "Mask was not set up in masked blending");
357 #endif
358 
359  getCurrentProgram().setInteger(VERTICAL_FLIP_ID, onScreen ? 0 : 1);
360  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
361 
362 #ifdef BEATMUP_DEBUG
363  GLException::check("blending");
364 #endif
365 }
static void check(const std::string &info)
Definition: bgl.h:62
static const char * VERTICAL_FLIP_ID
Vertical flipping variable name in vertex shader.

◆ paveBackground()

void RenderingPrograms::paveBackground ( GraphicPipeline gpu,
TextureHandler content,
GL::TextureHandler output 
)

Fills background with a repeated texture taking 1 pixel of this texture per 1 pixel of the output.

Parameters
[in,out]gpuA graphic pipeline instance
[in]contentThe texture
[in]outputTarget texture handler or null if onscreen rendering

Definition at line 368 of file rendering_programs.cpp.

368  {
369  // choose program
370  switch (content.getTextureFormat()) {
371  case TextureHandler::TextureFormat::OES_Ext:
373  break;
374  default:
376  break;
377  }
378 
379  // setting texture coords, bitmap size and updating buffer data in GPU
381  (float)(output ? output->getWidth() : gpu->getDisplayResolution().getWidth() ) / content.getWidth(),
382  (float)(output ? output->getHeight() : gpu->getDisplayResolution().getHeight()) / content.getHeight()
383  ));
384 
386  currentGlProgram->setVector4("modulationColor", 1.0f, 1.0f, 1.0f, 1.0f);
388  blend(output == nullptr);
389 }
static const AffineMapping IDENTITY
Definition: geometry.h:717
void setVector4(const std::string &name, const float x, const float y, const float z, const float w)
Definition: program.cpp:378
void setMatrix3(const std::string &name, const Matrix2 &mat, const Point &pos)
Definition: program.cpp:402
void enableProgram(GraphicPipeline *gpu, Operation program)
Select and enable a common program.
@ BLEND_EXT
default blending using a texture extension
@ BLEND
default blending of a single image
static const char * MODELVIEW_MATRIX_ID
Modelview matrix (mapping input geometry to output) shader variable name in vertex shader.
void blend(bool onScreen)
Performs the blending operation.
virtual const int getHeight() const =0
Height of the texture in pixels.
const ImageResolution & getDisplayResolution() const
Definition: pipeline.cpp:916
unsigned int getWidth() const
unsigned int getHeight() const
CustomRectangle< float > Rectangle
Definition: geometry.h:627
@ REPEAT
wrapping the texture by repeating instead of clamping to edge
std::string content

◆ getDefaultVertexShader()

const VertexShader & RenderingPrograms::getDefaultVertexShader ( const GraphicPipeline gpu) const
Returns
default blending vertex shader to be used in a user-defined single-image blending program.

Definition at line 392 of file rendering_programs.cpp.

392  {
393  return defaultVertexShader;
394 }

◆ getProgram()

Program & RenderingPrograms::getProgram ( const GraphicPipeline gpu,
Operation  program 
)
private

Definition at line 259 of file rendering_programs.cpp.

259  {
260  auto& map = programs;
261  auto it = map.find(operation);
262  if (it != map.end())
263  return it->second;
264 
265  // maps shader types to internally handled shaders
266  std::string fragmentCode;
268  switch (operation) {
270  ext = ext + Extensions::EXTERNAL_TEXTURE;
271  case Operation::BLEND:
272  fragmentCode = FRAGMENT_SHADER_BLEND;
273  break;
274 
276  ext = ext + Extensions::EXTERNAL_TEXTURE;
278  fragmentCode = FRAGMENT_SHADER_BLENDMASK;
279  break;
280 
282  ext = ext + Extensions::EXTERNAL_TEXTURE;
284  fragmentCode = FRAGMENT_SHADER_BLENDMASK_8BIT;
285  break;
286 
288  ext = ext + Extensions::EXTERNAL_TEXTURE;
290  fragmentCode = FRAGMENT_SHADER_BLENDSHAPE;
291  break;
292 
293  default:
294  Insanity::insanity("Invalid rendering operation");
295  }
296 
297  // instantiate shaders
298  const bool useDefaultVertexShader = operation == Operation::BLEND || operation == Operation::BLEND_EXT;
299  VertexShader* vertexShader = useDefaultVertexShader ? &defaultVertexShader : new VertexShader(*gpu, VERTEX_SHADER_BLENDMASK, ext);
300  FragmentShader fragmentShader(*gpu, fragmentCode, ext);
301 
302  // link program
303  Program& glProgram = programs.emplace(
304  std::piecewise_construct,
305  std::forward_as_tuple(operation),
306  std::forward_as_tuple(*gpu, *vertexShader, fragmentShader)
307  ).first->second;
308  if (!useDefaultVertexShader)
309  delete vertexShader;
310  return glProgram;
311 }
GLSL fragment shader.
Definition: program.h:107
std::map< Operation, RenderingProgram > programs
@ SHAPED_BLEND_EXT
shaping an image (texture extension)
GLSL vertex shader.
Definition: program.h:93
static void insanity(const char *message)
Definition: exception.h:136
Extensions
Supported OpenGL estensions.
Definition: program.h:63
@ EXTERNAL_TEXTURE
GL_OES_EGL_image_external_essl3 if available or GL_OES_EGL_image_external.
Definition: program.h:66
static const char * VERTEX_SHADER_BLENDMASK
static const char * FRAGMENT_SHADER_BLENDMASK
static const char * FRAGMENT_SHADER_BLENDSHAPE
static const char * FRAGMENT_SHADER_BLENDMASK_8BIT
static const char * FRAGMENT_SHADER_BLEND

Member Data Documentation

◆ VERTEX_COORD_ATTRIB_NAME

const char * Beatmup::GL::RenderingPrograms::VERTEX_COORD_ATTRIB_NAME = "inVertex"
static

vertex coordinate attribute name in vertex shaders

Definition at line 50 of file rendering_programs.h.

◆ TEXTURE_COORD_ATTRIB_NAME

const char * Beatmup::GL::RenderingPrograms::TEXTURE_COORD_ATTRIB_NAME = "inTexCoord"
static

texture coordinate attribute name in vertex shaders

Definition at line 51 of file rendering_programs.h.

◆ VERTICAL_FLIP_ID

const char * Beatmup::GL::RenderingPrograms::VERTICAL_FLIP_ID = "flipVertically"
static

Vertical flipping variable name in vertex shader.

Definition at line 53 of file rendering_programs.h.

◆ MODELVIEW_MATRIX_ID

const char * Beatmup::GL::RenderingPrograms::MODELVIEW_MATRIX_ID = "modelview"
static

Modelview matrix (mapping input geometry to output) shader variable name in vertex shader.

Definition at line 54 of file rendering_programs.h.

◆ TEXTURE_COORDINATES_ID

const char * Beatmup::GL::RenderingPrograms::TEXTURE_COORDINATES_ID = "texCoord"
static

Texture coordinates shader variable name in vertex shader.

Definition at line 55 of file rendering_programs.h.

◆ DECLARE_TEXTURE_COORDINATES_IN_FRAG

const char * Beatmup::GL::RenderingPrograms::DECLARE_TEXTURE_COORDINATES_IN_FRAG = "varying highp vec2 texCoord;\n"
static

Declaring texture coordinates in fragment shader.

Definition at line 57 of file rendering_programs.h.

◆ backend

Backend* Beatmup::GL::RenderingPrograms::backend
private

Definition at line 110 of file rendering_programs.h.

◆ currentGlProgram

Program* Beatmup::GL::RenderingPrograms::currentGlProgram
private

Definition at line 111 of file rendering_programs.h.

◆ currentProgram

Operation Beatmup::GL::RenderingPrograms::currentProgram
private

Definition at line 112 of file rendering_programs.h.

◆ maskSetUp

bool Beatmup::GL::RenderingPrograms::maskSetUp
private

Definition at line 113 of file rendering_programs.h.

◆ defaultVertexShader

VertexShader Beatmup::GL::RenderingPrograms::defaultVertexShader
private

Definition at line 115 of file rendering_programs.h.

◆ programs

std::map<Operation, RenderingProgram> Beatmup::GL::RenderingPrograms::programs
private

Definition at line 116 of file rendering_programs.h.


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