Beatmup
Beatmup::ImageShader Class Reference

A GLSL program to process images. More...

#include <image_shader.h>

Inheritance diagram for Beatmup::ImageShader:
Beatmup::GL::VariablesBundle Beatmup::LockableObject Beatmup::Object

Classes

class  NoSource
 Expection thrown if no shader source is provided. More...
 

Public Member Functions

 ImageShader (GL::RecycleBin &recycleBin)
 
 ImageShader (Context &ctx)
 
 ~ImageShader ()
 
void setSourceCode (const std::string &sourceCode)
 Passes new source code to the fragment shader. More...
 
void setOutputClipping (const IntRectangle &rectangle)
 Sets output clipping area. More...
 
void prepare (GraphicPipeline &gpu, GL::TextureHandler *input, const TextureParam texParam, AbstractBitmap *output, const AffineMapping &mapping)
 Conducts required preparations for blending. More...
 
void prepare (GraphicPipeline &gpu, GL::TextureHandler *input, AbstractBitmap *output)
 
void prepare (GraphicPipeline &gpu, AbstractBitmap *output)
 Conducts required preparations for blending. More...
 
void bindSamplerArray (const char *uniformId, int startingUnit, int numUnits)
 Binds a bunch of texture units to a uniform sampler array variable. More...
 
void process (GraphicPipeline &gpu)
 Apply the shader to produce an image. More...
 
bool usesContext (Context &context) const
 Returns true if the shader has ressources attached to a given context. More...
 
- Public Member Functions inherited from Beatmup::GL::VariablesBundle
void clear ()
 Removes all stored variables. More...
 
void setInteger (std::string name, int value)
 Sets a scalar integer uniform value. More...
 
void setInteger (std::string name, int x, int y)
 
void setInteger (std::string name, int x, int y, int z)
 
void setInteger (std::string name, int x, int y, int z, int w)
 
void setFloat (std::string name, float value)
 Sets a scalar float uniform value. More...
 
void setFloat (std::string name, float x, float y)
 
void setFloat (std::string name, float x, float y, float z)
 
void setFloat (std::string name, float x, float y, float z, float w)
 
void setFloatMatrix2 (std::string name, const float matrix[4])
 Sets a float 2*2 matrix variable value. More...
 
void setFloatMatrix3 (std::string name, const float matrix[9])
 Sets a float 3*3 matrix variable value. More...
 
void setFloatMatrix4 (std::string name, const float matrix[16])
 Sets a float 4*4 matrix variable value. More...
 
void setFloatMatrix4 (std::string name, const Color::Matrix &matrix)
 Sets a float 4*4 matrix variable value from a Color::Matrix instance. More...
 
void setFloatArray (std::string name, const std::vector< float > &values)
 Sets a float array variable value. More...
 
float getFloat (const std::string &name) const
 Retrieves a value of a scalar float uniform variable by its name. More...
 
- Public Member Functions inherited from Beatmup::LockableObject
void lock ()
 
void unlock ()
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Attributes

static const std::string INPUT_IMAGE_DECL_TYPE = GL::FragmentShader::DIALECT_SAMPLER_DECL_TYPE
 A virtual input image type defined at shader compile time by ordinary texture or OES texture sampler depending on the input bound. More...
 
static const std::string INPUT_IMAGE_ID = "image"
 Shader variable name referring to the input image. More...
 
static const std::string CODE_HEADER
 Shader code header containing necessary declarations. More...
 

Private Member Functions

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

Private Attributes

GL::RecycleBinrecycleBin
 
GL::RenderingProgramprogram
 
std::string sourceCode
 last passed fragment shader source code More...
 
bool upToDate
 if true, the program is up-to-date with respect to the source code More...
 
GL::TextureHandler::TextureFormat inputFormat
 last used input texture format; when changed, the shader is recompiled More...
 
IntRectangle outputClipRect
 output clip rectangle: only this specified area of the output image will be changed More...
 

Additional Inherited Members

- Protected Member Functions inherited from Beatmup::GL::VariablesBundle
void apply (Program &program)
 

Detailed Description

A GLSL program to process images.

Definition at line 34 of file image_shader.h.

Constructor & Destructor Documentation

◆ ImageShader() [1/3]

Beatmup::ImageShader::ImageShader ( const ImageShader )
privatedelete

disabling copying constructor

◆ ImageShader() [2/3]

ImageShader::ImageShader ( GL::RecycleBin recycleBin)

Definition at line 55 of file image_shader.cpp.

55  :
57  program(nullptr),
58  upToDate(false),
59  inputFormat(GL::TextureHandler::TextureFormat::RGBx8)
60 {}
bool upToDate
if true, the program is up-to-date with respect to the source code
Definition: image_shader.h:40
GL::TextureHandler::TextureFormat inputFormat
last used input texture format; when changed, the shader is recompiled
Definition: image_shader.h:41
GL::RecycleBin & recycleBin
Definition: image_shader.h:37
GL::RenderingProgram * program
Definition: image_shader.h:38

◆ ImageShader() [3/3]

ImageShader::ImageShader ( Context ctx)

Definition at line 63 of file image_shader.cpp.

63  :
65 {}
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
ImageShader(const ImageShader &)=delete
disabling copying constructor
Beatmup::Context * ctx

◆ ~ImageShader()

ImageShader::~ImageShader ( )

Definition at line 68 of file image_shader.cpp.

68  {
70 }
void put(Item *item)
Puts an item into the recycle bin.
Definition: recycle_bin.cpp:73

Member Function Documentation

◆ setSourceCode()

void ImageShader::setSourceCode ( const std::string &  sourceCode)

Passes new source code to the fragment shader.

The new source code will be compiled and linked when next rendering occurs.

Definition at line 73 of file image_shader.cpp.

73  {
74  lock();
75  this->sourceCode = sourceCode;
76  upToDate = false;
77  unlock();
78 }
std::string sourceCode
last passed fragment shader source code
Definition: image_shader.h:39

◆ setOutputClipping()

void ImageShader::setOutputClipping ( const IntRectangle rectangle)

Sets output clipping area.

Only this specified area of the output bitmap will be changed by executing the shader. This must be called before prepare().

Parameters
[in]rectangleThe output clipping area in pixels

Definition at line 81 of file image_shader.cpp.

81  {
82  this->outputClipRect = rectangle;
83 }
IntRectangle outputClipRect
output clip rectangle: only this specified area of the output image will be changed
Definition: image_shader.h:42

◆ prepare() [1/3]

void ImageShader::prepare ( GraphicPipeline gpu,
GL::TextureHandler input,
const TextureParam  texParam,
AbstractBitmap output,
const AffineMapping mapping 
)

Conducts required preparations for blending.

Compiles shaders and links the rendering program if not yet.

Parameters
gpuGraphic pipeline instance
inputShader input image. This is optional, but the shader is considered to have at least one input image if this function is used.
texParamInput texture parameter
outputImage to write shader output to (optional)
mappingGeometric transformation to apply when filling output

Definition at line 86 of file image_shader.cpp.

86  {
87  LockGuard lock(this);
88  if (sourceCode.empty())
89  throw NoSource();
90 
91  // check if the input format changes
92  if (input && input->getTextureFormat() != inputFormat)
93  upToDate = false;
94 
95 
96  // make program ready if not yet or if not up to date
97  if (!program || !upToDate) {
98  inputFormat = input ? input->getTextureFormat() : GL::TextureHandler::TextureFormat::RGBx8;
99 
100  // link program
101  GL::Extensions textureExtension = inputFormat == GL::TextureHandler::TextureFormat::OES_Ext ? GL::Extensions::EXTERNAL_TEXTURE : GL::Extensions::NONE;
102  GL::FragmentShader fragmentShader(gpu, sourceCode, GL::Extensions::BEATMUP_DIALECT + textureExtension);
103  if (!program) {
104  program = new GL::RenderingProgram(gpu, fragmentShader);
105  }
106  else {
107  program->link(gpu, fragmentShader);
108  }
109 
110  upToDate = true;
111  }
112 
113  // enable program
114  program->enable(gpu);
115  if (input)
118 
119  // bind output
120  if (output) {
121  if (!outputClipRect.empty())
122  gpu.bindOutput(*output, outputClipRect);
123  else
124  gpu.bindOutput(*output);
125  }
126 
127  // bind input
128  if (input)
129  gpu.bind(*input, 0, texParam);
130  // Binding order matters: texture unit 0 is used for input now.
131 
134  !output || outputClipRect.empty() ? mapping :
136  );
137 
138  // apply bundle
139  apply(*program);
140 }
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
bool empty() const
Definition: geometry.h:149
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
void setMatrix3(const std::string &name, const Matrix2 &mat, const Point &pos)
Definition: program.cpp:402
GLSL fragment shader.
Definition: program.h:107
GLSL program to render images Makes use of default vertex attributes to pass the texture coordinates ...
Definition: program.h:240
void link(const GraphicPipeline &gpu, const FragmentShader &)
Definition: program.cpp:543
static const char * MODELVIEW_MATRIX_ID
Modelview matrix (mapping input geometry to output) shader variable name in vertex shader.
virtual const TextureFormat getTextureFormat() const =0
Returns the texture format specifying how the shader must interpret the data.
void apply(Program &program)
const ImageResolution & getDisplayResolution() const
Definition: pipeline.cpp:916
void bindOutput(AbstractBitmap &bitmap)
Binds a bitmap to the pipeline output.
Definition: pipeline.cpp:891
void setTextureCoordinates(const Rectangle &coords)
Specifies texture coordinates for the next rendering pass.
Definition: pipeline.cpp:966
void bind(GL::TextureHandler &texture, size_t texUnit, const TextureParam param)
Definition: pipeline.cpp:881
static const std::string INPUT_IMAGE_ID
Shader variable name referring to the input image.
Definition: image_shader.h:109
static AffineMapping getOutputCropMapping(const ImageResolution &out, const IntRectangle &outputClipRect)
Extensions
Supported OpenGL estensions.
Definition: program.h:63
@ NONE
no extension
Definition: program.h:64
@ BEATMUP_DIALECT
pseudo-extension enabling Beatmup GLSL dialect
Definition: program.h:65
@ EXTERNAL_TEXTURE
GL_OES_EGL_image_external_essl3 if available or GL_OES_EGL_image_external.
Definition: program.h:66
Beatmup::AffineMapping & mapping

◆ prepare() [2/3]

void ImageShader::prepare ( GraphicPipeline gpu,
GL::TextureHandler input,
AbstractBitmap output 
)

Definition at line 143 of file image_shader.cpp.

143  {
145 }
static const AffineMapping IDENTITY
Definition: geometry.h:717
void prepare(GraphicPipeline &gpu, GL::TextureHandler *input, const TextureParam texParam, AbstractBitmap *output, const AffineMapping &mapping)
Conducts required preparations for blending.
@ INTERP_LINEAR
bilinear pixel interpolation

◆ prepare() [3/3]

void ImageShader::prepare ( GraphicPipeline gpu,
AbstractBitmap output 
)

Conducts required preparations for blending.

Compiles shaders and links the rendering program if not yet. This function is used for shaders having no inputs.

Parameters
gpuGraphic pipeline instance
outputImage to write shader output to (optional)

Definition at line 148 of file image_shader.cpp.

148  {
149  LockGuard lock(this);
150  if (sourceCode.empty())
151  throw NoSource();
152 
153  // link program if not yet
154  if (!program || !upToDate) {
155  // link program
157  if (!program) {
158  program = new GL::RenderingProgram(gpu, fragmentShader);
159  }
160  else {
161  program->link(gpu, fragmentShader);
162  }
163 
164  upToDate = true;
165  }
166 
167  // enable program
168  program->enable(gpu);
169 
170  // bind output
171  if (output) {
172  if (!outputClipRect.empty())
173  gpu.bindOutput(*output, outputClipRect);
174  else
175  gpu.bindOutput(*output);
176  }
177 
178  // set up mapping
183  );
184 
185  // apply bundle
186  apply(*program);
187 }

◆ bindSamplerArray()

void ImageShader::bindSamplerArray ( const char *  uniformId,
int  startingUnit,
int  numUnits 
)

Binds a bunch of texture units to a uniform sampler array variable.

Parameters
[in]uniformIdThe uniform array variable name
[in]startingUnitFirst texture unit to be bound to the first element of the array
[in]numUnitsNumber of texture units to bind (likely matches the length of the array)

Definition at line 190 of file image_shader.cpp.

190  {
191  program->setIntegerArray(uniformId, startingUnit, numUnits);
192 }
void setIntegerArray(const std::string &name, const int *values, const int length)
Definition: program.cpp:417

◆ process()

void ImageShader::process ( GraphicPipeline gpu)

Apply the shader to produce an image.

Parameters
gpuA graphic pipeline instance

Definition at line 195 of file image_shader.cpp.

195  {
196  program->blend();
197 }
void blend(bool onScreen)
Definition: program.cpp:548

◆ usesContext()

bool Beatmup::ImageShader::usesContext ( Context context) const
inline

Returns true if the shader has ressources attached to a given context.

Definition at line 98 of file image_shader.h.

98 { return context.getGpuRecycleBin() == &recycleBin; }

Member Data Documentation

◆ recycleBin

GL::RecycleBin& Beatmup::ImageShader::recycleBin
private

Definition at line 37 of file image_shader.h.

◆ program

GL::RenderingProgram* Beatmup::ImageShader::program
private

Definition at line 38 of file image_shader.h.

◆ sourceCode

std::string Beatmup::ImageShader::sourceCode
private

last passed fragment shader source code

Definition at line 39 of file image_shader.h.

◆ upToDate

bool Beatmup::ImageShader::upToDate
private

if true, the program is up-to-date with respect to the source code

Definition at line 40 of file image_shader.h.

◆ inputFormat

GL::TextureHandler::TextureFormat Beatmup::ImageShader::inputFormat
private

last used input texture format; when changed, the shader is recompiled

Definition at line 41 of file image_shader.h.

◆ outputClipRect

IntRectangle Beatmup::ImageShader::outputClipRect
private

output clip rectangle: only this specified area of the output image will be changed

Definition at line 42 of file image_shader.h.

◆ INPUT_IMAGE_DECL_TYPE

const std::string ImageShader::INPUT_IMAGE_DECL_TYPE = GL::FragmentShader::DIALECT_SAMPLER_DECL_TYPE
static

A virtual input image type defined at shader compile time by ordinary texture or OES texture sampler depending on the input bound.

Definition at line 104 of file image_shader.h.

◆ INPUT_IMAGE_ID

const std::string ImageShader::INPUT_IMAGE_ID = "image"
static

Shader variable name referring to the input image.

Definition at line 109 of file image_shader.h.

◆ CODE_HEADER

const std::string ImageShader::CODE_HEADER
static
Initial value:
=
"uniform " + INPUT_IMAGE_DECL_TYPE + " " + INPUT_IMAGE_ID +";\n" +
static const char * DECLARE_TEXTURE_COORDINATES_IN_FRAG
Declaring texture coordinates in fragment shader.
static const std::string INPUT_IMAGE_DECL_TYPE
A virtual input image type defined at shader compile time by ordinary texture or OES texture sampler ...
Definition: image_shader.h:104

Shader code header containing necessary declarations.

Definition at line 114 of file image_shader.h.


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