Beatmup
linear_mapping.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, 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/pipeline.h"
21 #include "../gpu/program_bank.h"
22 #include <array>
23 
24 namespace Beatmup {
25  namespace GL {
26  /**
27  Real-valued vector usable by GPU
28  */
29  class Vector : public GL::TextureHandler {
30  public:
31  /**
32  Vector data format
33  */
34  enum class Format {
35  TEXTURE, //!< 8 bit per element covering [0, 1] range
36  FIXED16, //!< 16 bit per element
37  FLOAT, //!< 32 bit per element, floating point
38  };
39  private:
41  const TextureFormat texFormat; //!< texture format of the texture handler
42  const Format format; //!< data format
43  const int size; //!< number of samples in the vector
45 
46  void prepare(GraphicPipeline& gpu);
47  public:
48  static const Format DEFAULT_FORMAT; // default format option depending on the device capabilities (FLOAT if available, FIXED16 otherwise)
49 
50  Vector(Context& context, GraphicPipeline& gpu, const int size, const Format format);
51 
52  /**
53  Creates a vector in GPU memory
54  \param[in] context A context
55  \param[in] gpu A graphic pipeline instance
56  \param[in] size Size of the vector (number of scalar dimensions)
57  \param[in] format Vector data format used to store the values in GPU memory
58  \param[in] values Vector elements values
59  \param[in] remap If `true` and a fixed-point data format is used, the stored values are remapped to fit the range of the coresponding fixed-point representation.
60  */
61  Vector(Context& context, GraphicPipeline& gpu, const int size, const Format format, const float* values, const bool remap = false);
62 
63  ~Vector();
64 
65  /**
66  Grabs vector values back from GPU to user memory.
67  \param[in] gpu A graphic pipeline instance
68  \param[in] output The output vector
69  */
70  void fetch(GraphicPipeline& gpu, std::vector<float>& output) const;
71 
72  inline Format getDataFormat() const { return format; }
73 
74  /**
75  Returns the length of the vector
76  */
77  inline int getSize() const { return size; }
78 
79  /**
80  Returns memory size in bytes taken by the vector.
81  */
82  size_t getMemorySize() const;
83 
84 
85  inline const int getWidth() const { return 1; }
86  inline const int getHeight() const { return format == Format::FIXED16 ? size / 2 : size / 4; }
87  inline const int getDepth() const { return 1; }
88  inline const TextureFormat getTextureFormat() const { return texFormat; }
89 
90  /**
91  \brief Provides the scale factor applied to the vector values sent to GPU.
92  The scale factor is applied if remapping is enabled in the constructor and a fixed-point representation is used. The vector values are remapped to match the dynamics range
93  of the fixed point representation: [gpu texture values] = a * [original values] + b
94  \return the scale factor a.
95  */
96  inline float getMappingScale() const { return mapScale; }
97 
98  /**
99  \brief Provides the constant offset added to the vector values sent to GPU.
100  The offset is added if remapping is enabled in the constructor and a fixed-point representation is used. The vector values are remapped to match the dynamics range
101  of the fixed point representation: [gpu texture values] = a * [original values] + b
102  \return the offset value b.
103  */
104  inline float getMappingOffset() const { return mapOffset; }
105  };
106 
107 
108  /**
109  Evaluates expression A*x + b = y for a matrix A and vectors x and b using GPU.
110  b is optional.
111  x is of float/texture format.
112  y might be in floating point, 16 bit fixed point or texture format.
113  */
115  private:
116  static const int MULT_STAGE_STEPS = 2; //!< number of 4*4 blocks processed at the multiplication stage
117  static const int SUM_STAGE_STEPS = 4; //!< number of vectors summed per shader at the summation stage
118  class Matrix;
119 
121  Matrix* buffer[2]; //!< intermediate buffers used at summation stage in a circular fashion
122  Matrix* matrix; //!< the matrix ("A")
123  Vector* bias; //!< optional bias vector ("b")
124  RenderingProgram* multStage; //!< multiplication stage program: small pieces of the vector are multiplied by small pieces of the matrix
125  RenderingProgram* sumStage; //!< summation stage program: the pieces are collected together (repeated multiple times)
126  RenderingProgram* lastSumStage; //!< last summation stage program: adding bias and clamping output
127  ProgramBank* programBank; //!< if not null, manages the programs
128  std::array<float, 4> multStageDelta;
129  std::vector<std::array<float, SUM_STAGE_STEPS>> sumStageDelta;
131  std::vector<Beatmup::Rectangle> sumStageTexCoords;
132  const int leftPadding; //!< zero pixels added on the left side in the buffers to sum for whatever the input size is
133  const bool forceFixed16Storage; //!< if `true`, 16 bit fixed-point storages are used even if floating point compute is supported by the GPU
134  bool fixed16Input; //!< if `true`, the input vector `x` is stored using 16 bit fixed-point format (float or texture otherwise)
135  bool fixed16Output; //!< if `true`, the output vector `y` is stored using 16 bit fixed-point format (float or texture otherwise)
136  bool ready;
137 
138  protected:
139  /**
140  Prepares the mapping for application (builds its GPU programs).
141  \param[in] gpu A GraphicPipeline instance
142  \param[in] output Texture handler representing the output vector `y`
143  \param[in] input Texture handler representing the output vector `x`
144  \param[in,out] bank A program bank to store the GPU programs and share with other mappings. May be null.
145  */
146  void prepare(GraphicPipeline& gpu, TextureHandler& output, TextureHandler& input, ProgramBank* bank = nullptr);
147 
148  void process(GraphicPipeline& gpu, TextureHandler& output, TextureHandler& input);
149 
150  public:
151  /**
152  Instantiates LinearMapping.
153  \param context A context instance
154  \param forceFixed16 Force 16 bit fixed-point storage.
155  */
156  LinearMapping(Context& context, bool forceFixed16 = false);
157  ~LinearMapping();
158 
159  void setMatrix(GraphicPipeline& gpu, const int width, const int height, const float* values);
160  void setBias(GraphicPipeline& gpu, const int height, const float* values);
161 
163  };
164  }
165 }
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
Evaluates expression A*x + b = y for a matrix A and vectors x and b using GPU.
Matrix * matrix
the matrix ("A")
RenderingProgram * lastSumStage
last summation stage program: adding bias and clamping output
Vector * bias
optional bias vector ("b")
LinearMapping(Context &context, bool forceFixed16=false)
Instantiates LinearMapping.
const int leftPadding
zero pixels added on the left side in the buffers to sum for whatever the input size is
const bool forceFixed16Storage
if true, 16 bit fixed-point storages are used even if floating point compute is supported by the GPU
bool fixed16Output
if true, the output vector y is stored using 16 bit fixed-point format (float or texture otherwise)
void setBias(GraphicPipeline &gpu, const int height, const float *values)
Matrix * buffer[2]
intermediate buffers used at summation stage in a circular fashion
void prepare(GraphicPipeline &gpu, TextureHandler &output, TextureHandler &input, ProgramBank *bank=nullptr)
Prepares the mapping for application (builds its GPU programs).
RenderingProgram * multStage
multiplication stage program: small pieces of the vector are multiplied by small pieces of the matrix
void process(GraphicPipeline &gpu, TextureHandler &output, TextureHandler &input)
static const int SUM_STAGE_STEPS
number of vectors summed per shader at the summation stage
ProgramBank * programBank
if not null, manages the programs
RenderingProgram * sumStage
summation stage program: the pieces are collected together (repeated multiple times)
bool fixed16Input
if true, the input vector x is stored using 16 bit fixed-point format (float or texture otherwise)
std::vector< std::array< float, SUM_STAGE_STEPS > > sumStageDelta
static const int MULT_STAGE_STEPS
number of 4*4 blocks processed at the multiplication stage
std::vector< Beatmup::Rectangle > sumStageTexCoords
void setMatrix(GraphicPipeline &gpu, const int width, const int height, const float *values)
void operator()(GraphicPipeline &gpu, TextureHandler &result, TextureHandler &input)
Beatmup::Rectangle multStageTexCoords
std::array< float, 4 > multStageDelta
Stores linked GLSL programs and their associated fragment shader codes.
Definition: program_bank.h:31
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.
Real-valued vector usable by GPU.
const int getHeight() const
Height of the texture in pixels.
const TextureFormat getTextureFormat() const
Returns the texture format specifying how the shader must interpret the data.
const Format format
data format
const int size
number of samples in the vector
void fetch(GraphicPipeline &gpu, std::vector< float > &output) const
Grabs vector values back from GPU to user memory.
float getMappingOffset() const
Provides the constant offset added to the vector values sent to GPU.
const int getWidth() const
Width of the texture in pixels.
float getMappingScale() const
Provides the scale factor applied to the vector values sent to GPU.
int getSize() const
Returns the length of the vector.
Vector(Context &context, GraphicPipeline &gpu, const int size, const Format format)
const int getDepth() const
Depth of the texture in pixels.
Format
Vector data format.
@ FIXED16
16 bit per element
@ TEXTURE
8 bit per element covering [0, 1] range
@ FLOAT
32 bit per element, floating point
Format getDataFormat() const
const TextureFormat texFormat
texture format of the texture handler
size_t getMemorySize() const
Returns memory size in bytes taken by the vector.
static const Format DEFAULT_FORMAT
void prepare(GraphicPipeline &gpu)
Prepares (eventually uploads) texture data on GPU.
Internal low-level GPU control API.
Definition: pipeline.h:33
Real-valued matrix usable by GPU.
jlong jint width
Beatmup::IntPoint result
jlong jint jint height