Beatmup
image_shader.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../gpu/variables_bundle.h"
21 #include "../gpu/pipeline.h"
22 #include "../context.h"
23 #include "../exception.h"
24 #include <mutex>
25 #include <string>
26 #include <map>
27 namespace Beatmup {
28  namespace GL {
29  class Program;
30  }
31  /**
32  A GLSL program to process images
33  */
35  ImageShader(const ImageShader&) = delete; //!< disabling copying constructor
36  private:
39  std::string sourceCode; //!< last passed fragment shader source code
40  bool upToDate; //!< if `true`, the program is up-to-date with respect to the source code
41  GL::TextureHandler::TextureFormat inputFormat; //!< last used input texture format; when changed, the shader is recompiled
42  IntRectangle outputClipRect; //!< output clip rectangle: only this specified area of the output image will be changed
43 
44  public:
47  ~ImageShader();
48 
49  /**
50  Passes new source code to the fragment shader.
51  The new source code will be compiled and linked when next rendering occurs.
52  */
53  void setSourceCode(const std::string& sourceCode);
54 
55  /**
56  \brief Sets output clipping area.
57  Only this specified area of the output bitmap will be changed by executing the shader. This must be called before prepare().
58  \param[in] rectangle The output clipping area in pixels
59  */
60  void setOutputClipping(const IntRectangle& rectangle);
61 
62  /**
63  \brief Conducts required preparations for blending. Compiles shaders and links the rendering program if not yet.
64  \param gpu Graphic pipeline instance
65  \param input Shader input image. This is optional, but the shader is considered to have at least one input image if this function is used.
66  \param texParam Input texture parameter
67  \param output Image to write shader output to (optional)
68  \param mapping Geometric transformation to apply when filling output
69  */
70  void prepare(GraphicPipeline& gpu, GL::TextureHandler* input, const TextureParam texParam, AbstractBitmap* output, const AffineMapping& mapping);
71  void prepare(GraphicPipeline& gpu, GL::TextureHandler* input, AbstractBitmap* output);
72 
73  /**
74  \brief Conducts required preparations for blending. Compiles shaders and links the rendering program if not yet.
75  This function is used for shaders having no inputs.
76  \param gpu Graphic pipeline instance
77  \param output Image to write shader output to (optional)
78  */
79  void prepare(GraphicPipeline& gpu, AbstractBitmap* output);
80 
81  /**
82  \brief Binds a bunch of texture units to a uniform sampler array variable.
83  \param[in] uniformId The uniform array variable name
84  \param[in] startingUnit First texture unit to be bound to the first element of the array
85  \param[in] numUnits Number of texture units to bind (likely matches the length of the array)
86  */
87  void bindSamplerArray(const char* uniformId, int startingUnit, int numUnits);
88 
89  /**
90  \brief Apply the shader to produce an image.
91  \param gpu A graphic pipeline instance
92  */
93  void process(GraphicPipeline& gpu);
94 
95  /**
96  Returns `true` if the shader has ressources attached to a given context.
97  */
98  inline bool usesContext(Context& context) const { return context.getGpuRecycleBin() == &recycleBin; }
99 
100  /**
101  A virtual input image type defined at shader compile time by ordinary texture
102  or OES texture sampler depending on the input bound
103  */
104  static const std::string INPUT_IMAGE_DECL_TYPE;
105 
106  /**
107  Shader variable name referring to the input image
108  */
109  static const std::string INPUT_IMAGE_ID;
110 
111  /**
112  Shader code header containing necessary declarations
113  */
114  static const std::string CODE_HEADER;
115 
116  /**
117  Expection thrown if no shader source is provided
118  */
119  class NoSource : public Exception {
120  public:
121  NoSource() : Exception("Layer shader has no source code") {}
122  };
123  };}
A very basic class for any image.
2x3 affine mapping containing a 2x2 matrix and a 2D point
Definition: geometry.h:639
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
Base class for all exceptions.
Definition: exception.h:37
Stores references to GPU resources that will not be used anymore and needed to be recycled in a threa...
Definition: recycle_bin.h:34
GLSL program to render images Makes use of default vertex attributes to pass the texture coordinates ...
Definition: program.h:240
TextureFormat
Texture format, specifies how the texture should be interpreted on the shader side.
Internal low-level GPU control API.
Definition: pipeline.h:33
Expection thrown if no shader source is provided.
Definition: image_shader.h:119
A GLSL program to process images.
Definition: image_shader.h:34
void process(GraphicPipeline &gpu)
Apply the shader to produce an image.
bool upToDate
if true, the program is up-to-date with respect to the source code
Definition: image_shader.h:40
void setOutputClipping(const IntRectangle &rectangle)
Sets output clipping area.
void prepare(GraphicPipeline &gpu, GL::TextureHandler *input, const TextureParam texParam, AbstractBitmap *output, const AffineMapping &mapping)
Conducts required preparations for blending.
static const std::string CODE_HEADER
Shader code header containing necessary declarations.
Definition: image_shader.h:114
bool usesContext(Context &context) const
Returns true if the shader has ressources attached to a given context.
Definition: image_shader.h:98
GL::TextureHandler::TextureFormat inputFormat
last used input texture format; when changed, the shader is recompiled
Definition: image_shader.h:41
void setSourceCode(const std::string &sourceCode)
Passes new source code to the fragment shader.
GL::RecycleBin & recycleBin
Definition: image_shader.h:37
std::string sourceCode
last passed fragment shader source code
Definition: image_shader.h:39
IntRectangle outputClipRect
output clip rectangle: only this specified area of the output image will be changed
Definition: image_shader.h:42
GL::RenderingProgram * program
Definition: image_shader.h:38
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
ImageShader(const ImageShader &)=delete
disabling copying constructor
void bindSamplerArray(const char *uniformId, int startingUnit, int numUnits)
Binds a bunch of texture units to a uniform sampler array variable.
static const std::string INPUT_IMAGE_ID
Shader variable name referring to the input image.
Definition: image_shader.h:109
TextureParam
Parameters of binding a texture to a texture unit on GPU.
Beatmup::Context * ctx
Beatmup::AffineMapping & mapping