Beatmup
linear_mapping.cpp File Reference
#include "linear_mapping.h"
#include "../utils/fixed_point.h"
#include "../utils/string_builder.h"
#include "../gpu/bgl.h"
#include <cstring>

Go to the source code of this file.

Classes

class  Beatmup::GL::LinearMapping::Matrix
 Real-valued matrix usable by GPU. More...
 

Functions

static bool useFixedPointStorage (bool forceFixed16Storage)
 
void sampleVectorComponent (String &code, const char *declaration, const char *variable, const char *uniform, const char *coordinate, int delta=-1)
 
static void declareGlsl16bitFixedPointFunctions (StringBuilder &code, const char *suffix, bool pack, bool unpackVec, bool unpackScalar, unsigned int packPrecision=8, unsigned int unpackPrecision=8)
 Generates GLSL code of float to 16-bit fixed point packing/unpacking functions. More...
 
static float unpackFloatFrom16bit (const uint8_t lsb, const int msb)
 Converts 16-bit fixed-point number into a floating point number. More...
 
static void packFloatTo16bit (const float value, uint8_t &lsb, uint8_t &msb)
 Packs a floating point value into a 16-bit fixed-point value. More...
 
static void findMinMax (const float *values, const int count, float &minVal, float &maxVal)
 

Variables

static const char * UNIFORM_MATRIX = "mtrx"
 
static const char * UNIFORM_INPUT = "inp"
 
static const char * UNIFORM_BIAS = "bias"
 
static const char * UNIFORM_DELTA = "dt"
 
static const int VECTOR_TEXTURE_DIMS = 2
 
static const int VECTOR_MAIN_DIM = 1
 

Function Documentation

◆ useFixedPointStorage()

static bool useFixedPointStorage ( bool  forceFixed16Storage)
inlinestatic

Definition at line 44 of file linear_mapping.cpp.

44  {
45 #ifdef BEATMUP_OPENGLVERSION_GLES20
46  return true;
47 #else
48  return forceFixed16Storage;
49 #endif
50 }

◆ sampleVectorComponent()

void sampleVectorComponent ( String code,
const char *  declaration,
const char *  variable,
const char *  uniform,
const char *  coordinate,
int  delta = -1 
)

Definition at line 62 of file linear_mapping.cpp.

62  {
63  code.printf("%s %s = texture%dD(%s, vec%d(", declaration, variable, VECTOR_TEXTURE_DIMS, uniform, VECTOR_TEXTURE_DIMS);
64  for (int i = 0; i < VECTOR_TEXTURE_DIMS; ++i) {
65  if (i > 0)
66  code(",");
67  if (i == VECTOR_MAIN_DIM) {
68  if (delta >= 0) {
69  code.printf("%s + %s[%d]", coordinate, UNIFORM_DELTA, delta);
70  }
71  else {
72  code(coordinate);
73  }
74  }
75  else
76  code("0");
77  }
78  code.line("));");
79 }
StringBuilder & line(const std::string &append)
StringBuilder & printf(const char *format,...)
static const int VECTOR_MAIN_DIM
static const char * UNIFORM_DELTA
static const int VECTOR_TEXTURE_DIMS

◆ declareGlsl16bitFixedPointFunctions()

static void declareGlsl16bitFixedPointFunctions ( StringBuilder code,
const char *  suffix,
bool  pack,
bool  unpackVec,
bool  unpackScalar,
unsigned int  packPrecision = 8,
unsigned int  unpackPrecision = 8 
)
inlinestatic

Generates GLSL code of float to 16-bit fixed point packing/unpacking functions.

Parameters
[in,out]codeString builder containing the shader source code
[in]suffixA suffix to add to the function names
[in]packIf true, the packing function code is added
[in]unpackVecIf true, the vector unpacking function code is added (producing a vec4 from two vec4)
[in]unpackScalarIf true, the scalar unpacking function code is added (producing a float from two floats)
[in]packPrecisionNumber of bits storing the fractional part in the resulting packed value
[in]unpackPrecisionNumber of bits storing the fractional part in the input value to unpack

Definition at line 92 of file linear_mapping.cpp.

92  {
93  if (pack) {
94  code.printf("lowp vec2 pack%s(highp float v) {", suffix);
95  if (packPrecision != 8)
96  code.printf("v *= %0.1f;", packPrecision > 8 ? (float)(1 << (packPrecision - 8)) : 1.0f / (1 << (packPrecision - 8)));
97  code.line(
98  " return vec2((256.0 / 255.0) * fract(v), (1.0 / 255.0) * floor(v) + (128.0 / 255.0));"
99  "}"
100  );
101  }
102  const float
103  scaleMsb = unpackPrecision > 8 ? 1.0f / (1 << (unpackPrecision - 8)) : (float)(1 << (unpackPrecision - 8)),
104  scaleLsb = 255.0f * scaleMsb;
105  if (unpackVec)
106  code.printf(
107  "highp vec4 unpack%s(lowp vec4 lsb, lowp vec4 msb) {"
108  " return lsb * (%0.5f / 256.0) + floor(255.0 * msb - 127.5) * %0.5f;"
109  "}",
110  suffix, scaleLsb, scaleMsb
111  );
112  if (unpackScalar)
113  code.printf(
114  "highp float unpack%s(lowp float lsb, lowp float msb) {"
115  " return lsb * (%0.5f / 256.0) + floor(255.0 * msb - 127.5) * %0.5f;"
116  "}",
117  suffix, scaleLsb, scaleMsb
118  );
119 }

◆ unpackFloatFrom16bit()

static float unpackFloatFrom16bit ( const uint8_t  lsb,
const int  msb 
)
inlinestatic

Converts 16-bit fixed-point number into a floating point number.

Parameters
[in]lsbThe least significant byte of the input fixed-point value
[in]msbThe most significant byte of the input fixed-point value
Returns
the floating-point value.

Definition at line 128 of file linear_mapping.cpp.

128  {
129  return (float)Fixed16<8>::interpret( ((msb - 128) << 8) + lsb );
130 }
Fixed-point number.
Definition: fixed_point.h:33

◆ packFloatTo16bit()

static void packFloatTo16bit ( const float  value,
uint8_t &  lsb,
uint8_t &  msb 
)
inlinestatic

Packs a floating point value into a 16-bit fixed-point value.

The resulting representation matches the formulas in unpackFloatFrom16bit(..) and packFloatTo16bit(..) A biased clamping-friendly formulation is used: "0.0" on input produces the most significant channel value of "0.5".

Parameters
[in]valueThe input value to pack
[in]lsbThe least significant byte in the output fixed-point value
[in]msbThe most significant byte in the output fixed-point value

Definition at line 141 of file linear_mapping.cpp.

141  {
142  union {
143  Fixed16<8> val;
144  uint16_t bytes;
145  } ptr{ value };
146  lsb = ptr.bytes & 0xff;
147  msb = (ptr.bytes >> 8) + 128;
148 }
return(jlong) new Beatmup jlong jstring jint val

◆ findMinMax()

static void findMinMax ( const float *  values,
const int  count,
float &  minVal,
float &  maxVal 
)
inlinestatic

Definition at line 151 of file linear_mapping.cpp.

151  {
152  minVal = maxVal = values[0];
153  for (int i = 1; i < count; ++i) {
154  minVal = std::min(minVal, values[i]);
155  maxVal = std::max(maxVal, values[i]);
156  }
157 }
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
JNIEnv jlong jint jint count

Variable Documentation

◆ UNIFORM_MATRIX

const char* UNIFORM_MATRIX = "mtrx"
static

Definition at line 34 of file linear_mapping.cpp.

◆ UNIFORM_INPUT

const char* UNIFORM_INPUT = "inp"
static

Definition at line 35 of file linear_mapping.cpp.

◆ UNIFORM_BIAS

const char* UNIFORM_BIAS = "bias"
static

Definition at line 36 of file linear_mapping.cpp.

◆ UNIFORM_DELTA

const char* UNIFORM_DELTA = "dt"
static

Definition at line 37 of file linear_mapping.cpp.

◆ VECTOR_TEXTURE_DIMS

const int VECTOR_TEXTURE_DIMS = 2
static

Definition at line 40 of file linear_mapping.cpp.

◆ VECTOR_MAIN_DIM

const int VECTOR_MAIN_DIM = 1
static

Definition at line 41 of file linear_mapping.cpp.