21 #define POS_FMT "%0.10f"
24 using namespace NNets;
84 static std::map<std::string, Deserializer*> map;
92 DebugAssertion::check(getDeserializersMap().
count(opType) == 0,
"A deserializer of a specific operation is already registered");
94 getDeserializersMap().emplace(opType,
this);
99 nbSizeX(nbSizeX), nbSizeY(nbSizeY), useUniformShift(false)
122 code.
printf(
"%s i00", datatype);
123 for (
int i = 1; i < nbSize * nbSize; ++i)
128 const int mid = (nbSize - 1) / 2;
132 for (
int i = 0; i < nbSize; ++i) {
133 if (i != mid) code(
", ");
141 if (!inlineSampling) {
143 for (
int i = 1; i < nbSize; ++i)
152 if (isFirstSample || this->shift !=
shift) {
154 for (
int i = 0; i < nbSize; ++i)
156 code.
printf(
"cp%d = p%d;\n", i, i);
166 code.
printf(
"%s%d%d = texture2D(%s[%d], cp%d)%s;\n",
169 code.
printf(
"%s%d%d = texture2D(%s[%d], vec2(cp%d.x, cp%d.y))%s;\n",
181 code.
printf(
"texture2D(%s[%d], ", inputName, inputIndex);
184 if (position.
x == position.
y)
185 code.
printf(
"p%d", position.
x);
187 code.
printf(
"vec2(p%d.x, p%d.y)", position.
x, position.
y);
193 code.
printf(
")%s", suffix);
200 for (
int i = 0; i < deltaSize; ++i) {
201 const float f = (float)(i + 1);
211 DebugAssertion::check(
useUniformShift,
"Uniform shift is not enabled");
231 return IntRectangle(halfKernel, halfKernel + (
n - 1) * stride);
243 ledge = (coverage -
size) / 2,
246 kernelShift =
std::min(halfKernel, halfKernel - ledge);
248 return IntRectangle(kernelShift, kernelShift + (
n - 1) * stride);
285 code.
printf(
"gl_FragColor = %s;", inputVariable);
288 code.
printf(
"gl_FragColor = 0.167 * %s;", inputVariable);
291 code.
printf(
"gl_FragColor = clamp(0.1*(%s), -0.05, 0.05) + clamp(0.05*(%s), -0.125, 0.125) + 0.05*(%s) + 0.5;", inputVariable, inputVariable, inputVariable);
305 execute(0, amount / num, 0, num);
322 (idx + 1) * amount / num,
335 if (lc ==
"sigmoid_like")
static const CustomPoint ZERO
CustomRectangle translated(numeric x, numeric y) const
Returns a translated box.
void setVec2Array(const std::string &name, const float *xy, const int length)
void setVector2(const std::string &name, const float x, const float y)
static const char * TEXTURE_COORDINATES_ID
Texture coordinates shader variable name in vertex shader.
Real-valued vector usable by GPU.
Internal low-level GPU control API.
static Rectangle getTextureCoordinates(const Rectangle &area, const IntPoint &size, const IntPoint &sampling)
Computes floating-point texture coordinates for pixel-accurate sampling: a texture gets sampled exact...
static void insanity(const char *message)
Deserializer(const char *opType)
Registers a new deserializer capable of building an operation from a listing block.
static std::map< std::string, Deserializer * > & getDeserializersMap()
virtual Size getOutputSize(int outputIndex=0) const
Returns full size of a specific operation output.
virtual void setInput(Storage::View &&storage, int index=0)
virtual Storage::View getOutput(int index=0)
Returns a storage view bound to a specific operation output.
virtual void setOutput(Storage::View &&storage, int index=0)
const ActivationFunction activationFunc
void apply(StringBuilder &code, const char *inputVariable)
Renders a GLSL code applying activation function to a specific variable and writing the result to gl_...
void execute(TaskThread &thread, GraphicPipeline &gpu)
Executes the operation.
virtual int getAmountOfWork() const =0
Returns amount of work in arbitrary units to be splitted among threads.
virtual void beforeExecute(GraphicPipeline &gpu, const int threadCount)
Called right before the operation is executed.
virtual void afterExecute(const int threadCount)
Called right after the operation is executed.
Operation 3D input/output size.
Padding
Zero padding specification.
Rectangle getTextureCoordinates(const Storage::View &storage, const int channel, const IntPoint &stride, const Size::Padding padding, const IntPoint &outputSize) const
Computes texture coordinates sampling a specific storage channel for given stride,...
void sample(StringBuilder &code, const char *inputName, const int inputIndex, const Point &shift, const bool isFirstSample=true, const char *suffix="")
Samples a neighborhood of a given texture.
void sampleInline(StringBuilder &code, const char *inputName, const int inputIndex, const IntPoint &position, const Point &shift, const char *suffix="")
void setup(const int width, const int height)
Prepares the spatial filtering operation execution.
static const char * SAMPLE_ID_PREFIX
prefix of variables declaring a neighbor sample
Point shift
current static shift of the sampling position
int getDeltasSize() const
void writeHeader(StringBuilder &code, bool useUniformShift)
Writes out the very GLSL fragment shader header required for spatial neighborhood sampling.
std::string getInputSamplingPos() const
Retrieves input sampling point position for the current fragment.
void declare(StringBuilder &code, const char *datatype, bool inlineSampling=false)
Declares GLSL fragment shader main(..) code part required for spatial neighborhood sampling.
void setupProgram(GL::Program &program)
Prepares a given program for spatial filtering.
IntRectangle getSamplingArea(const IntPoint &size, const IntPoint &stride, const Size::Padding padding) const
Implements common padding policies by computing a rectangular area of positions the sampling kernel t...
bool useUniformShift
if true, the sampling position can be shifted dynamically at every run
SpatialFilteringMixin(const int nbSizeX, const int nbSizeY)
Initializes spatial filtering mixin.
void setUniformShift(GL::Program &program, const IntPoint &shift, const IntPoint &inputSize)
Applies an offset to the sampling position at runtime.
float * deltas
array storing pixel position differences for neighborhood sampling
Maps a 3D tensor onto a storage.
int getTextureHeight() const
Returns height in pixels of all the textures.
IntPoint getChannelOrigin(int channel) const
Returns origin in pixels of a given channel within the texture containing it.
int getTextureWidth() const
Returns width in pixels of all the textures.
static void check(const datatype value, const datatype min, const datatype max, const char *message)
Toolset to build a string content.
StringBuilder & line(const std::string &append)
StringBuilder & printf(const char *format,...)
virtual ThreadIndex numThreads() const =0
virtual void synchronize()=0
Blocks until all the other threads running the same task reach the same point.
ThreadIndex currentThread() const
virtual bool isTaskAborted() const =0
Returns true if the task is asked to stop from outside.
ActivationFunction activationFunctionFromString(const std::string &str)
Returns a zero padding value from a string.
ActivationFunction
Activation function specification.
@ SIGMOID_LIKE
piecewise-linear sigmoid approximation
@ DEFAULT
default activation: 0..1 bounded ReLU (identity clipped to 0..1 range)
@ BRELU6
0.167 times identity clipped to 0..1 range
std::string lowercase(const std::string &str)
Converts a string to lower case (latin letters only).
CustomRectangle< int > IntRectangle
CustomPoint< int > IntPoint
std::string to_string(Beatmup::NNets::ActivationFunction function)
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
static const char * UNIFORM_SHIFT
uniform variable specifying an offset to apply to the sampling position in SpatialFilteringMixin
static const char * UNIFORM_DELTA
uniform variable storing spatial deltas for SpatialFilteringMixin
#define ceili(x, y)
integer division x/y with ceiling
jobject jlong jint jint y
JNIEnv jlong jint jint count
JNIEnv jobject jstring str