20 #include "../gpu/bgl.h"
21 #include "../gpu/texture_handler.h"
22 #include "../utils/string_builder.h"
23 #include "../utils/bitset.h"
24 #include "../exception.h"
25 #include "../platform.h"
31 using namespace NNets;
39 glActiveTexture(GL_TEXTURE0 + unit);
40 glBindTexture(GL_TEXTURE_2D, texture);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
75 kernel[0] / 2 - (kernel[0] - ((
dim[0] - 1) % stride[0]) - 1) / 2,
76 kernel[1] / 2 - (kernel[1] - ((
dim[1] - 1) % stride[1]) - 1) / 2,
77 kernel[2] / 2 - (kernel[2] - ((
dim[2] - 1) % stride[2]) - 1) / 2
81 return Size(kernel[0] / 2, kernel[1] / 2, kernel[2] / 2);
87 const uint8_t* ptr = (
const uint8_t*)data;
91 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94 #ifdef BEATMUP_OPENGLVERSION_GLES20
95 glTexImage2D(GL_TEXTURE_2D,
102 data ? ptr :
nullptr);
104 ptr += textureSizeBytes;
108 glTexSubImage2D(GL_TEXTURE_2D,
114 ptr += textureSizeBytes;
129 textures(nullptr),
size(
size), pad(pad),
130 upToDate{false, false}
137 if (depth <= maxChannels) {
141 const int channelsPerTexture =
ceili(depth, maxChannels);
142 for (
int i = 1; i*i <= channelsPerTexture; ++i)
143 if (channelsPerTexture % i == 0) {
145 packY = channelsPerTexture / i;
153 textures(nullptr),
size(
size), pad(0),
154 upToDate{false, false}
174 for (
int i = 0; i <
count; ++i)
210 for (
int i = 0; i <
count; ++i)
214 glDeleteTextures(
count, handles);
244 glPixelStorei(GL_PACK_ALIGNMENT, 1);
248 GL_RGBA, GL_UNSIGNED_BYTE,
251 ptr += textureSizeBytes;
267 for (
int i = 0; i <
count; ++i) {
288 for (
int i = 0; i < numSamples; ++i)
289 OutOfRange::check<float>(hwcData[i], 0, 1,
"Data contains a sample falling out of 0..1 range: %0.8f");
294 const int paddedWidth =
width + 2 *
pad;
295 const int paddedHeight =
height + 2 *
pad;
296 for (
int c = 0; c < depth; c+=4)
297 for (
int y = 0, i = 0;
y <
height; ++
y) {
299 for (
int x = 0;
x <
width; ++
x, ++dst, ++i) {
300 dst->
r = (uint8_t)(hwcData[i * depth + c + 0] * 255);
301 dst->
g = (uint8_t)(hwcData[i * depth + c + 1] * 255);
302 dst->
b = (uint8_t)(hwcData[i * depth + c + 2] * 255);
303 dst->
a = (uint8_t)(hwcData[i * depth + c + 3] * 255);
316 static const bool crop =
true;
321 uniform sampler2D image;
322 varying highp vec2 texCoord;
324 lowp
float v = texture2D(image, texCoord)[%d];
325 gl_FragColor = vec4(
v,
v,
v, 1.0);
412 storage = another.storage;
413 another.storage =
nullptr;
420 storage = another.storage;
421 another.storage =
nullptr;
432 for (
int i = 0; i < num; ++i) {
442 storage(view.storage)
447 OutOfRange::check(firstChannel + numChannels, 0, view.getDepth(),
"Number of channels out of range: %d");
450 for (
int i = firstChannel; i < firstChannel + numChannels; i += 4)
451 usedTextures.
set(view.textures[view.getChannelTextureNumber(i)]);
454 std::map<int, int> textureMap;
456 if (usedTextures[i]) {
462 for (
int i = firstChannel; i < firstChannel + numChannels; i += 4) {
463 const auto entry = view.channels[i / 4];
478 RuntimeError::check(num % shuffleStep == 0,
"Shuffling step *4 must be a divider of the storage depth");
483 for (
int i = 0; i < num; ++i) {
484 const int shuffled = 4 * ((shuffleStep * i) % num + (shuffleStep * i) / num);
495 const int storageChannel =
channels[channel / 4].channelIdx + (channel % 4);
496 return storage->getImage(
ctx, gpu, storageChannel);
505 return channels[channel / 4].textureIdx;
515 return storage->getChannelOrigin(ch.
channelIdx);
526 const int storageChannel = view.
channels[ channel/4 ].channelIdx;
546 DebugAssertion::check(output.
storage->
textures,
"Output storage is not allocated on GPU");
553 bool fast = this->program == &program;
558 this->program = &program;
565 fast = fast && this->outputTexture == storage.
textures[outTexture].
handle;
571 glClear(GL_COLOR_BUFFER_BIT);
591 gpu.bindOutput(output);
592 this->program = &program;
593 this->outputTexture = 0;
602 program->setIntegerArray(
name, unit, num);
603 for (
int i = 0; i < num; ++i, ++unit)
617 program->setIntegerArray(
name, unit, 1);
618 const int storageChannel = input.
channels[channel/4].channelIdx;
628 program->setInteger(
name, unit++);
640 DebugAssertion::check(!this->view,
"A view is already bound");
641 DebugAssertion::check(view.
storage->
memory,
"Storage is not allocated in RAM");
645 if (view.
channels.size() != ptrSize || !ptr) {
649 ptr =
new sample_t*[ptrSize];
664 const Storage& storage = *view->storage;
668 for (
size_t i = 0; i < ptrSize; ++i) {
670 const int storageTexNum = view->textures[view->channels[i].textureIdx];
671 ptr[i] = data + ((storageTexNum *
h + pos.
y) *
w + pos.
x);
678 #ifdef BEATMUP_ENABLE_NEON
679 if (
sizeof(
void*) == 8) {
680 i += ptrSize / 2 * 2;
681 auto _1 = vdupq_n_u64(
sizeof(sample_t*));
682 uint64_t*
p = (uint64_t*)ptr;
683 const uint64_t* stop =
p + i;
684 for (;
p < stop;
p += 2)
685 vst1q_u64(
p, vaddq_u64(vld1q_u64(
p), _1));
687 else if (
sizeof(
void*) == 4) {
688 i += ptrSize / 4 * 4;
689 auto _1 = vdupq_n_u32(
sizeof(sample_t*));
690 uint32_t*
p = (uint32_t*)ptr;
691 const uint32_t* stop =
p + i;
692 for (;
p < stop;
p += 4)
693 vst1q_u32(
p, vaddq_u32(vld1q_u32(
p), _1));
696 for (; i < ptrSize; ++i)
Makes a bitmap writable for a specific target device.
A very basic class for any image.
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
void free()
Frees the allocated memory.
datatype * ptr(int offset=0)
void set(size_t i, bool value=true)
Basic class: task and memory management, any kind of static data.
GL::RecycleBin * getGpuRecycleBin() const
void enable(const GraphicPipeline &gpu)
static void check(const std::string &info)
A wrapper for a GPU resource.
void put(Item *item)
Puts an item into the recycle bin.
GLSL program to render images Makes use of default vertex attributes to pass the texture coordinates ...
void blend(bool onScreen)
TextureFormat
Texture format, specifies how the texture should be interpreted on the shader side.
Internal low-level GPU control API.
int getLimit(Limit limit) const
void pullPixels(AbstractBitmap &bitmap)
Transfers bitmap pixels from GPU to CPU.
void bindOutput(AbstractBitmap &bitmap)
Binds a bitmap to the pipeline output.
void setTextureCoordinates(const Rectangle &coords)
Specifies texture coordinates for the next rendering pass.
@ TEXTURE_IMAGE_UNITS
maximum number of texture units per fragment shader
static void insanity(const char *message)
Bitmap whose memory is managed by the Beatmup engine.
Operation 3D input/output size.
Size getOrigin(Size kernel, Size stride, Padding padding) const
Computes operation origin in function of operation kernel, padding and stride, assuming that the curr...
Padding
Zero padding specification.
@ SAME
operation output size matches its input size for unit strides
Size transform(Size kernel, Size stride, Padding padding, int depth=0) const
Computes operation output size in function of operation kernel, padding, stride and depth,...
Binding of different input/output storages/texture handlers to a GLSL program.
bool begin(GL::Program &program, Storage::View &output, int channel)
Starts binding things to a program.
void operator()(Storage::View &input, const char *name)
Binds a storage (all of its textures) to a uniform sampler array variable.
Scans a storageview in RAM for further computations on CPU.
Scanner & operator++()
Advances pointer by one pixel in scanline order (along the horizontal axis).
void move(int x, int y)
Sets the pointer to a specific spatial position.
void unbind()
Unbinds the current view from the scanner.
void bind(Storage::View &view)
Binds a view to the scanner.
void prepare(GraphicPipeline &gpu)
Prepares (eventually uploads) texture data on GPU.
Maps a 3D tensor onto a storage.
View & operator=(View &&)
IntPoint getChannelOrigin(int channel) const
Returns origin in pixels of a given channel within the texture containing it.
InternalBitmap * getImage(Context &ctx, GraphicPipeline &gpu, int channel) const
std::vector< Channel > channels
channels of the view
int getChannelTextureNumber(int channel) const
Returns number of the texture containing a given channel.
int getNumberOfTextures() const
Returns total number of textures in the storage view.
std::vector< int > textures
indices of textures in the storage
3D tensor stored in a set of textures.
int getTextureWidth() const
Returns width in pixels of all the textures.
void push(GraphicPipeline &gpu, const void *data)
static void checkChannelNumber(int channel)
Checks whether a channel number points to the first channel in a texture.
int getNumberOfTextures() const
Returns total number of textures in the storage.
InternalBitmap * getImage(Context &ctx, GraphicPipeline &gpu, int channel) const
Converts a feature channel into a bitmap for debugging purposes.
AlignedMemory memory
data storage in RAM
const int pad
padding in pixels added along width and height dimensions
Size getSize() const
Returns storage size in pixels.
int getTextureHeight() const
Returns height in pixels of all the textures.
void free()
Deferred storage disposal: the textures are put into the GPU recycle bin associated with the context.
size_t getMemorySize() const
Storage(const Storage &)=delete
disabling copying constructor
int packY
number of blocks of 4 channels per texture (spatial packing)
bool isAllocated() const
Returns true if the storage is allocated.
int getChannelTextureNumber(int channel) const
Returns number of texture containing a given channel.
IntPoint getChannelOrigin(int channel) const
Returns origin in pixels of a given channel within the texture containing it.
void allocate()
Allocates the storage in RAM.
void pull(GraphicPipeline &gpu)
Pulls storage data from GPU memory to RAM.
static void checkMin(const datatype value, const datatype min, const char *message)
static void check(const datatype value, const datatype min, const datatype max, const char *message)
static void check(const bool condition, const std::string &message)
StringBuilder & printf(const char *format,...)
StringBuilder including a string container.
const GLuint BITMAP_INTERNALFORMATS[]
const GLuint BITMAP_PIXELTYPES[]
Mapping of bitmap pixel formats to GL pixel types.
unsigned int handle_t
A reference to a GPU resource.
@ BEATMUP_DIALECT
pseudo-extension enabling Beatmup GLSL dialect
Size::Padding paddingFromString(const std::string &str)
Returns a zero padding value from a string.
std::string lowercase(const std::string &str)
Converts a string to lower case (latin letters only).
CustomRectangle< int > IntRectangle
CustomPoint< int > IntPoint
@ QuadByte
4 channels of 8 bits per pixel (like RGBA), unsigned integer values
@ INTERP_NEAREST
nearest neighbor pixel interpolation
std::string to_string(Beatmup::NNets::ActivationFunction function)
#define BEATMUP_SHADER_CODE(...)
static void bind(int unit, GL::handle_t texture)
bool dirty
if true, the texture needs to be cleared before use
int channelIdx
channel number in its corresponding storage
#define ceili(x, y)
integer division x/y with ceiling
JNIEnv jobject jlong handle
JNIEnv jobject jint jint jint channels
JNIEnv jobject jint format
return(jlong) new Beatmup jlong jstring name
jobject jlong jint jint y
jlong jstring jint jint jint jint w
JNIEnv jlong jint jint count
Beatmup::InternalBitmap * bitmap
Beatmup::IntPoint p((int) x,(int) y)
JNIEnv jlong jfloat jfloat jfloat v
JNIEnv jobject jstring str