Beatmup
Beatmup::GL::LinearMapping::Matrix Class Reference

Real-valued matrix usable by GPU. More...

Inheritance diagram for Beatmup::GL::LinearMapping::Matrix:
Beatmup::GL::TextureHandler Beatmup::Object

Public Member Functions

 Matrix (GraphicPipeline &gpu, int width, int height, const bool floatingPoint)
 
 Matrix (GraphicPipeline &gpu, int width, int height, const float *values, const bool floatingPoint)
 Creates a matrix in GPU memory. More...
 
void bind (GraphicPipeline &gpu, int textureUnit) const
 
int getMatrixWidth () const
 
int getMatrixHeight () const
 
float getScale () const
 
float getOffset () const
 
const int getWidth () const
 Width of the texture in pixels. More...
 
const int getHeight () const
 Height of the texture in pixels. More...
 
const int getDepth () const
 Depth of the texture in pixels. More...
 
const TextureFormat getTextureFormat () const
 Returns the texture format specifying how the shader must interpret the data. More...
 
- Public Member Functions inherited from Beatmup::GL::TextureHandler
 ~TextureHandler ()
 
float getAspectRatio () const
 Aspect ratio of the texture. More...
 
float getInvAspectRatio () const
 Inverse of the aspect ratio of the texture. More...
 
const bool isFloatingPoint () const
 
const int getNumberOfChannels () const
 Returns number of channels containing in the texture. More...
 
bool hasValidHandle () const
 Returns true if the texture handle points to a valid texture. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Private Member Functions

void prepare (GraphicPipeline &gpu, bool)
 

Private Attributes

const TextureFormat format
 
const int texWidth
 
const int texHeight
 texture size More...
 
const int width
 
const int height
 matrix size More...
 
float mapScale
 scaling applied to the matrix coefficients to optimize the fixed-point range use More...
 
float mapOffset
 offset applied to the matrix coefficients after scaling to optimize the fixed-point range use More...
 

Friends

class LinearMapping
 

Additional Inherited Members

- Public Types inherited from Beatmup::GL::TextureHandler
enum  TextureFormat {
  Rx8 , RGBx8 , RGBAx8 , Rx32f ,
  RGBx32f , RGBAx32f , OES_Ext
}
 Texture format, specifies how the texture should be interpreted on the shader side. More...
 
- Static Public Member Functions inherited from Beatmup::GL::TextureHandler
static const char * textureFormatToString (const TextureFormat &)
 
- Static Public Attributes inherited from Beatmup::GL::TextureHandler
static const int TEXTURE_FORMAT_BYTES_PER_PIXEL []
 size of a texel in bytes for different texture formats More...
 
- Protected Member Functions inherited from Beatmup::GL::TextureHandler
 TextureHandler ()
 
virtual void prepare (GraphicPipeline &gpu)
 Prepares (eventually uploads) texture data on GPU. More...
 
void invalidate (RecycleBin &)
 Forces disposing the texture data, e.g. More...
 
- Protected Attributes inherited from Beatmup::GL::TextureHandler
handle_t textureHandle
 

Detailed Description

Real-valued matrix usable by GPU.

Definition at line 163 of file linear_mapping.cpp.

Constructor & Destructor Documentation

◆ Matrix() [1/2]

Beatmup::GL::LinearMapping::Matrix::Matrix ( GraphicPipeline gpu,
int  width,
int  height,
const bool  floatingPoint 
)

Definition at line 212 of file linear_mapping.cpp.

212  :
213  format(floatingPoint ? TextureFormat::RGBAx32f : TextureFormat::RGBAx8),
214  texWidth(width), texHeight(floatingPoint ? height / 4 : height / 2),
216 {
217 #ifdef BEATMUP_OPENGLVERSION_GLES20
218  if (floatingPoint)
219  throw RuntimeError("Floating-point linear mapping is not supported in ES 2.0");
220 #endif
221  RuntimeError::check(height % 4 == 0, "Matrix height must be a multiple of four.");
222 
223  // init texture with zeros
224  const int numPix = 4 * getWidth() * getHeight();
225  std::vector<uint8_t> zeros(numPix * TextureHandler::TEXTURE_FORMAT_BYTES_PER_PIXEL[format]);
226  if (floatingPoint)
227  memset(zeros.data(), 0, zeros.size());
228  else
229  for (int i = 0; i < numPix; i += 2)
230  packFloatTo16bit(0, zeros[i], zeros[i + 1]);
231 
232  // setup texture
233  glGenTextures(1, &textureHandle);
234  glActiveTexture(GL_TEXTURE0);
235  glBindTexture(GL_TEXTURE_2D, textureHandle);
236 #ifdef BEATMUP_OPENGLVERSION_GLES20
237  glTexImage2D(GL_TEXTURE_2D,
238  0,
240  getWidth(), getHeight(),
241  0,
244  zeros.data()
245  );
246 #else
247  glTexStorage2D(GL_TEXTURE_2D, 1, BITMAP_INTERNALFORMATS[format], getWidth(), getHeight());
248  glTexSubImage2D(GL_TEXTURE_2D,
249  0, 0, 0, getWidth(), getHeight(),
252  zeros.data()
253  );
254 #endif
255 }
float mapScale
scaling applied to the matrix coefficients to optimize the fixed-point range use
const int getWidth() const
Width of the texture in pixels.
float mapOffset
offset applied to the matrix coefficients after scaling to optimize the fixed-point range use
const int getHeight() const
Height of the texture in pixels.
static void packFloatTo16bit(const float value, uint8_t &lsb, uint8_t &msb)
Packs a floating point value into a 16-bit fixed-point value.
const GLuint BITMAP_INTERNALFORMATS[]
Definition: bgl.h:147
const GLuint BITMAP_PIXELTYPES[]
Mapping of bitmap pixel formats to GL pixel types.
Definition: bgl.h:160
const GLuint BITMAP_PIXELFORMATS[]
Mapping of bitmap pixel formats to GL pixel formats.
Definition: bgl.h:80

◆ Matrix() [2/2]

Beatmup::GL::LinearMapping::Matrix::Matrix ( GraphicPipeline gpu,
int  width,
int  height,
const float *  values,
const bool  floatingPoint 
)

Creates a matrix in GPU memory.

The memory layout depends on the data format. For floating point data four consecutive rows are packed into color channels. The first texture row looks like this ("x,y" for "col,row" in the input matrix): r 0,0 1,0 2,0 3,0 4,0 ... g 0,1 1,1 2,1 3,1 4,1 ... b 0,2 1,2 2,2 3,2 4,2 ... a 0,3 1,3 2,3 3,3 4,3 ... In 16-bit fixed point case, two consecutive columns are packed into color channels, alternating the least and most significant bytes: r 0,0L 0,0M 0,1L 0,1M ... g 1,0L 1,0M 1,1L 1,1M ... b 2,0L 2,0M 2,1L 2,1M ... a 3,0L 3,0M 3,1L 3,1M ...

Parameters
[in]gpuA graphic pipeline instance
[in]widthMatrix width (number of columns)
[in]heightMatrix height (number of rows)
[in]valuesMatrix coefficients in scanline order (rows)
[in]floatingPointIf true, the matrix coefficients are stored in floating point format (16-bit fixed point otherwise).

Definition at line 258 of file linear_mapping.cpp.

258  :
259  format(floatingPoint ? TextureFormat::RGBAx32f : TextureFormat::RGBAx8),
260  texWidth(width),
261  texHeight(floatingPoint ? height / 4 : height / 2),
263 {
264 #ifdef BEATMUP_OPENGLVERSION_GLES20
265  if (floatingPoint)
266  throw RuntimeError("Floating-point linear mapping is not supported in ES 2.0");
267 #endif
268  if (floatingPoint)
269  RuntimeError::check(height % 4 == 0, "Matrix height must be a multiple of four.");
270  else {
271  RuntimeError::check(width % 4 == 0, "Matrix width must be a multiple of four.");
272  RuntimeError::check(height % 2 == 0, "Matrix height must be pair.");
273  }
274 
275  // init texture handler
276  glGenTextures(1, &textureHandle);
277  glActiveTexture(GL_TEXTURE0);
278  glBindTexture(GL_TEXTURE_2D, textureHandle);
279 
280  // floating point compute mode: r, g, b, a store consecutive rows, not columns
281  if (floatingPoint) {
282 #ifndef BEATMUP_OPENGLVERSION_GLES20
283  std::vector<float> textureData(4 * getWidth() * getHeight());
284  for (int y = 0, i = 0; y < height; y += 4)
285  for (int x = 0; x < width; ++x, i += 4) {
286  textureData[i + 0] = values[y * width + x];
287  textureData[i + 1] = values[(y + 1) * width + x];
288  textureData[i + 2] = values[(y + 2) * width + x];
289  textureData[i + 3] = values[(y + 3) * width + x];
290  }
291 
292  glTexStorage2D(GL_TEXTURE_2D, 1, BITMAP_INTERNALFORMATS[format], getWidth(), getHeight());
293  glTexSubImage2D(GL_TEXTURE_2D,
294  0, 0, 0, getWidth(), getHeight(),
297  textureData.data()
298  );
299 #endif
300  }
301 
302  // fixed point compute mode: r, g, b, a store 4 consecutive rows (all LSB or all MSB)
303  else {
304  // measure magnitude
305  float minVal, maxVal;
306  findMinMax(values, width * height, minVal, maxVal);
307  mapScale = maxVal > minVal ? (Fixed16<8>::max() - Fixed16<8>::min()) / (maxVal - minVal) : 1;
308  mapOffset = maxVal > minVal ? Fixed16<8>::min() - minVal * mapScale : 0;
309 
310  // convert to fixed point
311  std::vector<uint8_t> textureData(4 * getWidth() * getHeight());
312  for (int y = 0, i = 0; y < height; y += 2)
313  for (int x = 0; x < width; x += 4, i += 16) {
314  packFloatTo16bit(mapOffset + mapScale * values[y * width + x + 0], textureData[i + 0], textureData[i + 4]);
315  packFloatTo16bit(mapOffset + mapScale * values[y * width + x + 1], textureData[i + 1], textureData[i + 5]);
316  packFloatTo16bit(mapOffset + mapScale * values[y * width + x + 2], textureData[i + 2], textureData[i + 6]);
317  packFloatTo16bit(mapOffset + mapScale * values[y * width + x + 3], textureData[i + 3], textureData[i + 7]);
318  packFloatTo16bit(mapOffset + mapScale * values[(y + 1) * width + x + 0], textureData[i + 8], textureData[i + 12]);
319  packFloatTo16bit(mapOffset + mapScale * values[(y + 1) * width + x + 1], textureData[i + 9], textureData[i + 13]);
320  packFloatTo16bit(mapOffset + mapScale * values[(y + 1) * width + x + 2], textureData[i + 10], textureData[i + 14]);
321  packFloatTo16bit(mapOffset + mapScale * values[(y + 1) * width + x + 3], textureData[i + 11], textureData[i + 15]);
322  }
323 
324 #ifdef BEATMUP_OPENGLVERSION_GLES20
325  glTexImage2D(GL_TEXTURE_2D,
326  0,
328  getWidth(), getHeight(),
329  0,
332  textureData.data()
333  );
334 #else
335  glTexStorage2D(GL_TEXTURE_2D, 1, BITMAP_INTERNALFORMATS[format], getWidth(), getHeight());
336  glTexSubImage2D(GL_TEXTURE_2D,
337  0, 0, 0, getWidth(), getHeight(),
340  textureData.data()
341  );
342 #endif
343  }
344 }
static void findMinMax(const float *values, const int count, float &minVal, float &maxVal)
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
jobject jlong jint jint y
jobject jlong jint x

Member Function Documentation

◆ prepare()

void Beatmup::GL::LinearMapping::Matrix::prepare ( GraphicPipeline gpu,
bool   
)
private

Definition at line 347 of file linear_mapping.cpp.

347  {
348  glBindTexture(GL_TEXTURE_2D, textureHandle);
349 }

◆ bind()

void Beatmup::GL::LinearMapping::Matrix::bind ( GraphicPipeline gpu,
int  textureUnit 
) const

Definition at line 352 of file linear_mapping.cpp.

352  {
353  glActiveTexture(GL_TEXTURE0 + textureUnit);
354  glBindTexture(GL_TEXTURE_2D, textureHandle);
355  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
356  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
357 #ifdef BEATMUP_DEBUG
358  GLException::check("binding matrix");
359 #endif
360 }

◆ getMatrixWidth()

int Beatmup::GL::LinearMapping::Matrix::getMatrixWidth ( ) const
inline

Definition at line 200 of file linear_mapping.cpp.

200 { return width; }

◆ getMatrixHeight()

int Beatmup::GL::LinearMapping::Matrix::getMatrixHeight ( ) const
inline

Definition at line 201 of file linear_mapping.cpp.

201 { return height; }

◆ getScale()

float Beatmup::GL::LinearMapping::Matrix::getScale ( ) const
inline

Definition at line 202 of file linear_mapping.cpp.

202 { return mapScale; }

◆ getOffset()

float Beatmup::GL::LinearMapping::Matrix::getOffset ( ) const
inline

Definition at line 203 of file linear_mapping.cpp.

203 { return mapOffset; }

◆ getWidth()

const int Beatmup::GL::LinearMapping::Matrix::getWidth ( ) const
inlinevirtual

Width of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 205 of file linear_mapping.cpp.

205 { return texWidth; }

◆ getHeight()

const int Beatmup::GL::LinearMapping::Matrix::getHeight ( ) const
inlinevirtual

Height of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 206 of file linear_mapping.cpp.

206 { return texHeight; }

◆ getDepth()

const int Beatmup::GL::LinearMapping::Matrix::getDepth ( ) const
inlinevirtual

Depth of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 207 of file linear_mapping.cpp.

207 { return 1; }

◆ getTextureFormat()

const TextureFormat Beatmup::GL::LinearMapping::Matrix::getTextureFormat ( ) const
inlinevirtual

Returns the texture format specifying how the shader must interpret the data.

Implements Beatmup::GL::TextureHandler.

Definition at line 208 of file linear_mapping.cpp.

208 { return format; }

Friends And Related Function Documentation

◆ LinearMapping

friend class LinearMapping
friend

Definition at line 164 of file linear_mapping.cpp.

Member Data Documentation

◆ format

const TextureFormat Beatmup::GL::LinearMapping::Matrix::format
private

Definition at line 166 of file linear_mapping.cpp.

◆ texWidth

const int Beatmup::GL::LinearMapping::Matrix::texWidth
private

Definition at line 167 of file linear_mapping.cpp.

◆ texHeight

const int Beatmup::GL::LinearMapping::Matrix::texHeight
private

texture size

Definition at line 167 of file linear_mapping.cpp.

◆ width

const int Beatmup::GL::LinearMapping::Matrix::width
private

Definition at line 168 of file linear_mapping.cpp.

◆ height

const int Beatmup::GL::LinearMapping::Matrix::height
private

matrix size

Definition at line 168 of file linear_mapping.cpp.

◆ mapScale

float Beatmup::GL::LinearMapping::Matrix::mapScale
private

scaling applied to the matrix coefficients to optimize the fixed-point range use

Definition at line 169 of file linear_mapping.cpp.

◆ mapOffset

float Beatmup::GL::LinearMapping::Matrix::mapOffset
private

offset applied to the matrix coefficients after scaling to optimize the fixed-point range use

Definition at line 170 of file linear_mapping.cpp.


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