Beatmup
Beatmup::GL::Vector Class Reference

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

#include <linear_mapping.h>

Inheritance diagram for Beatmup::GL::Vector:
Beatmup::GL::TextureHandler Beatmup::Object

Public Types

enum class  Format { TEXTURE , FIXED16 , FLOAT }
 Vector data format. More...
 
- 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...
 

Public Member Functions

 Vector (Context &context, GraphicPipeline &gpu, const int size, const Format format)
 
 Vector (Context &context, GraphicPipeline &gpu, const int size, const Format format, const float *values, const bool remap=false)
 Creates a vector in GPU memory. More...
 
 ~Vector ()
 
void fetch (GraphicPipeline &gpu, std::vector< float > &output) const
 Grabs vector values back from GPU to user memory. More...
 
Format getDataFormat () const
 
int getSize () const
 Returns the length of the vector. More...
 
size_t getMemorySize () const
 Returns memory size in bytes taken by the vector. More...
 
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...
 
float getMappingScale () const
 Provides the scale factor applied to the vector values sent to GPU. More...
 
float getMappingOffset () const
 Provides the constant offset added to the vector values sent to GPU. 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 ()
 

Static Public Attributes

static const Format DEFAULT_FORMAT = Vector::Format::FLOAT
 
- 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...
 

Private Member Functions

void prepare (GraphicPipeline &gpu)
 Prepares (eventually uploads) texture data on GPU. More...
 

Private Attributes

Contextcontext
 
const TextureFormat texFormat
 texture format of the texture handler More...
 
const Format format
 data format More...
 
const int size
 number of samples in the vector More...
 
float mapScale
 
float mapOffset
 

Additional Inherited Members

- Static Public Member Functions inherited from Beatmup::GL::TextureHandler
static const char * textureFormatToString (const TextureFormat &)
 
- Protected Member Functions inherited from Beatmup::GL::TextureHandler
 TextureHandler ()
 
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 vector usable by GPU.

Definition at line 29 of file linear_mapping.h.

Member Enumeration Documentation

◆ Format

Vector data format.

Enumerator
TEXTURE 

8 bit per element covering [0, 1] range

FIXED16 

16 bit per element

FLOAT 

32 bit per element, floating point

Definition at line 34 of file linear_mapping.h.

34  {
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  };

Constructor & Destructor Documentation

◆ Vector() [1/2]

Vector::Vector ( Context context,
GraphicPipeline gpu,
const int  size,
const Format  format 
)

Definition at line 363 of file linear_mapping.cpp.

363  :
364  context(context),
365  texFormat(format == Format::FLOAT ? TextureHandler::TextureFormat::RGBAx32f : TextureHandler::TextureFormat::RGBAx8),
366  format(format),
367  size(size),
368  mapScale(1), mapOffset(0)
369 {
370 #ifdef BEATMUP_OPENGLVERSION_GLES20
371  if (format == Format::FLOAT)
372  throw RuntimeError("Floating-point vectors are not supported in ES 2.0");
373 #endif
374  RuntimeError::check(size % 4 == 0, "Vector size must be a multiple of four.");
375 
376  // init texture handler
377  glGenTextures(1, &textureHandle);
378  glActiveTexture(GL_TEXTURE0);
379  glBindTexture(GL_TEXTURE_2D, textureHandle);
380 
381 #ifdef BEATMUP_OPENGLVERSION_GLES20
382  glTexImage2D(GL_TEXTURE_2D,
383  0,
385  getWidth(), getHeight(),
386  0,
389  nullptr
390  );
391 #else
392  glTexStorage2D(GL_TEXTURE_2D, 1, BITMAP_INTERNALFORMATS[texFormat], getWidth(), getHeight());
393 #endif
394 }
const int getHeight() const
Height of the texture in pixels.
const Format format
data format
const int size
number of samples in the vector
const int getWidth() const
Width of the texture in pixels.
@ FLOAT
32 bit per element, floating point
const TextureFormat texFormat
texture format of the texture handler
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
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

◆ Vector() [2/2]

Vector::Vector ( Context context,
GraphicPipeline gpu,
const int  size,
const Format  format,
const float *  values,
const bool  remap = false 
)

Creates a vector in GPU memory.

Parameters
[in]contextA context
[in]gpuA graphic pipeline instance
[in]sizeSize of the vector (number of scalar dimensions)
[in]formatVector data format used to store the values in GPU memory
[in]valuesVector elements values
[in]remapIf true and a fixed-point data format is used, the stored values are remapped to fit the range of the coresponding fixed-point representation.

Definition at line 397 of file linear_mapping.cpp.

397  :
398  context(context),
399  texFormat(format == Format::FLOAT ? TextureHandler::TextureFormat::RGBAx32f : TextureHandler::TextureFormat::RGBAx8),
400  format(format),
401  size(size),
402  mapScale(1), mapOffset(0)
403 {
404 #ifdef BEATMUP_OPENGLVERSION_GLES20
405  if (format == Format::FLOAT)
406  throw RuntimeError("Floating-point vectors are not supported in ES 2.0");
407 #endif
408  RuntimeError::check(size % 4 == 0, "Vector size must be a multiple of four.");
409 
410  glGenTextures(1, &textureHandle);
411  glActiveTexture(GL_TEXTURE0);
412  glBindTexture(GL_TEXTURE_2D, textureHandle);
413 
414  if (format != Format::FLOAT) {
415  // remap
416  if (remap) {
417  float minVal, maxVal;
418  findMinMax(values, size, minVal, maxVal);
419  if (maxVal > minVal) {
420  if (format == Format::FIXED16) {
421  mapScale = (Fixed16<8>::max() - Fixed16<8>::min()) / (maxVal - minVal);
422  mapOffset = Fixed16<8>::min() - minVal * mapScale;
423  }
424  else if (format == Format::TEXTURE) {
425  mapScale = 1.0f / (maxVal - minVal);
426  mapOffset = - minVal * mapScale;
427  }
428  }
429  }
430 
431  // convert data from floating point
432  std::vector<uint8_t> textureData(4 * getHeight());
433  if (format == Format::FIXED16)
434  for (int i = 0; i < size; ++i)
435  packFloatTo16bit(mapOffset + mapScale * values[i], textureData[2 * i], textureData[2 * i + 1]);
436  else if (format == Format::TEXTURE)
437  for (int i = 0; i < size; ++i) {
438  const float val = mapOffset + mapScale * values[i];
439  textureData[i] = val <= 0.0f ? 0 : val >= 1.0f ? 255 : (uint8_t)roundf_fast(val * 255);
440  }
441  else
442  Insanity::insanity("Invalid vector data format");
443 
444 #ifdef BEATMUP_OPENGLVERSION_GLES20
445  glTexImage2D(GL_TEXTURE_2D,
446  0,
448  getWidth(), getHeight(),
449  0,
452  textureData.data()
453  );
454 #else
455  glTexStorage2D(GL_TEXTURE_2D, 1, BITMAP_INTERNALFORMATS[texFormat], getWidth(), getHeight());
456  glTexSubImage2D(GL_TEXTURE_2D,
457  0, 0, 0, getWidth(), getHeight(),
460  textureData.data()
461  );
462 #endif
463  }
464 
465  else {
466 #ifndef BEATMUP_OPENGLVERSION_GLES20
467  glTexStorage2D(GL_TEXTURE_2D, 1, BITMAP_INTERNALFORMATS[texFormat], getWidth(), getHeight());
468  glTexSubImage2D(GL_TEXTURE_2D,
469  0, 0, 0, getWidth(), getHeight(),
472  values
473  );
474 #endif
475  }
476 }
@ FIXED16
16 bit per element
@ TEXTURE
8 bit per element covering [0, 1] range
static void insanity(const char *message)
Definition: exception.h:136
static void findMinMax(const float *values, const int count, float &minVal, float &maxVal)
static void packFloatTo16bit(const float value, uint8_t &lsb, uint8_t &msb)
Packs a floating point value into a 16-bit fixed-point value.
static float max()
Definition: fixed_point.h:111
static float min()
Definition: fixed_point.h:103
#define roundf_fast(X)
rounding (nearest integer)
Definition: utils.hpp:22
return(jlong) new Beatmup jlong jstring jint val

◆ ~Vector()

Vector::~Vector ( )

Definition at line 479 of file linear_mapping.cpp.

479  {
481 }
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
void invalidate(RecycleBin &)
Forces disposing the texture data, e.g.

Member Function Documentation

◆ prepare()

void Vector::prepare ( GraphicPipeline gpu)
privatevirtual

Prepares (eventually uploads) texture data on GPU.

Called only by the context managing thread.

Parameters
[in]gpuGraphic pipeline instance

Reimplemented from Beatmup::GL::TextureHandler.

Definition at line 484 of file linear_mapping.cpp.

484  {
485  glBindTexture(GL_TEXTURE_2D, textureHandle);
486 }

◆ fetch()

void Vector::fetch ( GraphicPipeline gpu,
std::vector< float > &  output 
) const

Grabs vector values back from GPU to user memory.

Parameters
[in]gpuA graphic pipeline instance
[in]outputThe output vector

Definition at line 489 of file linear_mapping.cpp.

489  {
490  output.resize(size);
492 
493  if (format == Format::FLOAT)
494  glReadPixels(0, 0, getWidth(), getHeight(),
495  GL_RGBA, GL_FLOAT,
496  output.data()
497  );
498  else if (format == Format::FIXED16) {
499  glReadPixels(0, 0, getWidth(), getHeight(),
500  GL_RGBA, GL_UNSIGNED_BYTE,
501  (void*)output.data()
502  );
503 
504  // convert values to float; use the same buffer, scan in inverse order
505  const uint8_t* ptr = (const uint8_t*)output.data() + 2 * size;
506  const size_t lastNum = output.size() - 1;
507  for (size_t i = 0; i <= lastNum; ++i) {
508  ptr -= 2;
509  output[lastNum - i] = unpackFloatFrom16bit(ptr[0], ptr[1]);
510  }
511  }
512  else if (format == Format::TEXTURE) {
513  glReadPixels(0, 0, getWidth(), getHeight(),
514  GL_RGBA, GL_UNSIGNED_BYTE,
515  (void*)output.data()
516  );
517 
518  // convert values to float; use the same buffer, scan in inverse order
519  const uint8_t* ptr = (const uint8_t*)output.data() + size;
520  const size_t lastNum = output.size() - 1;
521  for (size_t i = 0; i <= lastNum; ++i) {
522  --ptr;
523  output[lastNum - i] = *ptr / 255.0f;
524  }
525  }
526  else
527  Insanity::insanity("Unsupported vector format");
528 
529  // remap back if needed
530  if (mapScale != 1 || mapOffset != 0)
531  for (auto& _ : output)
532  _ = (_ - mapOffset) / mapScale;
533 }
void bindOutput(AbstractBitmap &bitmap)
Binds a bitmap to the pipeline output.
Definition: pipeline.cpp:891
static float unpackFloatFrom16bit(const uint8_t lsb, const int msb)
Converts 16-bit fixed-point number into a floating point number.

◆ getDataFormat()

Format Beatmup::GL::Vector::getDataFormat ( ) const
inline

Definition at line 72 of file linear_mapping.h.

72 { return format; }

◆ getSize()

int Beatmup::GL::Vector::getSize ( ) const
inline

Returns the length of the vector.

Definition at line 77 of file linear_mapping.h.

77 { return size; }

◆ getMemorySize()

size_t Vector::getMemorySize ( ) const

Returns memory size in bytes taken by the vector.

Definition at line 536 of file linear_mapping.cpp.

536  {
537  switch (format) {
538  case Format::FLOAT:
539  return sizeof(float) * (size_t)size;
540  case Format::FIXED16:
541  return 2 * (size_t)size;
542  case Format::TEXTURE:
543  return (size_t)size;
544  }
545  Insanity::insanity("Unsupported vector format");
546  return 0;
547 }

◆ getWidth()

const int Beatmup::GL::Vector::getWidth ( ) const
inlinevirtual

Width of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 85 of file linear_mapping.h.

85 { return 1; }

◆ getHeight()

const int Beatmup::GL::Vector::getHeight ( ) const
inlinevirtual

Height of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 86 of file linear_mapping.h.

86 { return format == Format::FIXED16 ? size / 2 : size / 4; }

◆ getDepth()

const int Beatmup::GL::Vector::getDepth ( ) const
inlinevirtual

Depth of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 87 of file linear_mapping.h.

87 { return 1; }

◆ getTextureFormat()

const TextureFormat Beatmup::GL::Vector::getTextureFormat ( ) const
inlinevirtual

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

Implements Beatmup::GL::TextureHandler.

Definition at line 88 of file linear_mapping.h.

88 { return texFormat; }

◆ getMappingScale()

float Beatmup::GL::Vector::getMappingScale ( ) const
inline

Provides the scale factor applied to the vector values sent to GPU.

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 of the fixed point representation: [gpu texture values] = a * [original values] + b

Returns
the scale factor a.

Definition at line 96 of file linear_mapping.h.

96 { return mapScale; }

◆ getMappingOffset()

float Beatmup::GL::Vector::getMappingOffset ( ) const
inline

Provides the constant offset added to the vector values sent to GPU.

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 of the fixed point representation: [gpu texture values] = a * [original values] + b

Returns
the offset value b.

Definition at line 104 of file linear_mapping.h.

104 { return mapOffset; }

Member Data Documentation

◆ context

Context& Beatmup::GL::Vector::context
private

Definition at line 40 of file linear_mapping.h.

◆ texFormat

const TextureFormat Beatmup::GL::Vector::texFormat
private

texture format of the texture handler

Definition at line 41 of file linear_mapping.h.

◆ format

const Format Beatmup::GL::Vector::format
private

data format

Definition at line 42 of file linear_mapping.h.

◆ size

const int Beatmup::GL::Vector::size
private

number of samples in the vector

Definition at line 43 of file linear_mapping.h.

◆ mapScale

float Beatmup::GL::Vector::mapScale
private

Definition at line 44 of file linear_mapping.h.

◆ mapOffset

float Beatmup::GL::Vector::mapOffset
private

Definition at line 44 of file linear_mapping.h.

◆ DEFAULT_FORMAT

const Vector::Format Vector::DEFAULT_FORMAT = Vector::Format::FLOAT
static

Definition at line 48 of file linear_mapping.h.


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