Beatmup
Beatmup::GLES31X2UpsamplingNetwork::Layer Class Reference

Public Member Functions

 Layer (GraphicPipeline &gpu, GL::RecycleBin &recycleBin, BitmapContentLock &lock, std::string sourceCodeTemplate, int inputZDim, int outputZDim, bool pointwise=false)
 
 ~Layer ()
 
void process (GraphicPipeline &gpu, GL::TextureHandler &input, InternalBitmap **outputs)
 
unsigned int process (GraphicPipeline &gpu, InternalBitmap **inputs, GL::StorageBuffer &output, int numOutputParts)
 
void processPointwise (GraphicPipeline &gpu, GL::StorageBuffer &input, unsigned int inputStridePix, InternalBitmap **outputs, int width, int height)
 
void processPointwise (GraphicPipeline &gpu, GL::StorageBuffer &inputFeatures, unsigned int inputStridePix, GL::TextureHandler &inputImage, AbstractBitmap &outputImage)
 

Private Member Functions

void prepare (GraphicPipeline &gpu, GL::TextureHandler *inputImage=nullptr)
 

Private Attributes

GL::RecycleBinrecycleBin
 
GL::ComputeProgramprogram
 
GL::TextureHandler::TextureFormat inputFormat
 
std::string sourceCodeTemplate
 
BitmapContentLocklock
 
unsigned int wgSize [3]
 
const int numInputs
 
const int numOutputs
 
bool prepared
 

Detailed Description

Definition at line 35 of file cnn.h.

Constructor & Destructor Documentation

◆ Layer()

GLES31X2UpsamplingNetwork::Layer::Layer ( GraphicPipeline gpu,
GL::RecycleBin recycleBin,
BitmapContentLock lock,
std::string  sourceCodeTemplate,
int  inputZDim,
int  outputZDim,
bool  pointwise = false 
)

Definition at line 65 of file cnn.cpp.

68  :
70 {
71  program = new GL::ComputeProgram(gpu);
72 
73  wgSize[2] = 1;
74 
75  const unsigned int totalLimit = (unsigned int)gpu.getLimit(GraphicPipeline::Limit::LOCAL_GROUPS_TOTAL);
76  const unsigned int yLimit = std::min<unsigned int>(
77  std::sqrt(totalLimit / wgSize[2]),
79  );
80 
81  for (wgSize[1] = 1; wgSize[1] <= yLimit; wgSize[1] <<= 1);
82 
83  wgSize[0] = std::min<unsigned int>(
84  totalLimit / (wgSize[1] * wgSize[2]),
86  );
87 }
GL::ComputeProgram * program
Definition: cnn.h:38
GLSL compute program.
int getLimit(Limit limit) const
Definition: pipeline.cpp:936

◆ ~Layer()

GLES31X2UpsamplingNetwork::Layer::~Layer ( )

Definition at line 90 of file cnn.cpp.

90  {
92 }
void put(Item *item)
Puts an item into the recycle bin.
Definition: recycle_bin.cpp:73

Member Function Documentation

◆ prepare()

void Beatmup::GLES31X2UpsamplingNetwork::Layer::prepare ( GraphicPipeline gpu,
GL::TextureHandler inputImage = nullptr 
)
private

Definition at line 32 of file cnn.cpp.

32  {
33  if (!prepared || (input && inputFormat != input->getTextureFormat())) {
34  std::string code = "#version 310 es\n";
35 
36  if (input) {
37  switch (inputFormat = input->getTextureFormat()) {
38  case GL::TextureHandler::TextureFormat::Rx8:
39  case GL::TextureHandler::TextureFormat::RGBx8:
40  case GL::TextureHandler::TextureFormat::RGBAx8:
41  case GL::TextureHandler::TextureFormat::Rx32f:
42  case GL::TextureHandler::TextureFormat::RGBx32f:
43  case GL::TextureHandler::TextureFormat::RGBAx32f:
44  code += "#define beatmupSampler sampler2D\n";
45  break;
46  case GL::TextureHandler::TextureFormat::OES_Ext:
47  code +=
48  "#extension GL_OES_EGL_image_external_essl3 : require\n"
49  "#define beatmupSampler samplerExternalOES\n"
50  "#define texelFetch texture\n";
51  break;
52  default:
54  }
55  }
56 
57  code += "layout(local_size_x = " + std::to_string(wgSize[0]) + ", local_size_y = " + std::to_string(wgSize[1]) + ", local_size_z = " + std::to_string(wgSize[2]) + ") in;\n";
58 
59  program->make(gpu, code + sourceCodeTemplate);
60  prepared = true;
61  }
62 
63 }
GL::TextureHandler::TextureFormat inputFormat
Definition: cnn.h:39
void make(const GraphicPipeline &gpu, const char *source)
Exception thrown when texture format does not match any supported format.
std::string to_string(Beatmup::NNets::ActivationFunction function)

◆ process() [1/2]

void GLES31X2UpsamplingNetwork::Layer::process ( GraphicPipeline gpu,
GL::TextureHandler input,
InternalBitmap **  outputs 
)

Definition at line 95 of file cnn.cpp.

95  {
96  prepare(gpu, &input);
97 
98  // enable
99  program->enable(gpu);
100 
101  // bind outputs
102  for (int i = 0; i < numOutputs; ++i) {
103  if (outputs[i]->getWidth() != input.getWidth() || outputs[i]->getHeight() != input.getHeight())
104  outputs[i]->reshape(input.getWidth(), input.getHeight());
105  lock.writeLock(&gpu, outputs[i], ProcessingTarget::GPU);
106  gpu.bind(*outputs[i], i, false, true);
107  }
108 
109  // bind input
110  program->setInteger("image", 0);
111  gpu.bind(input, 0, TextureParam::INTERP_NEAREST);
112 
113  // g-g-go
114  const int
115  xWorkgroups = ceili(input.getWidth(), wgSize[0]),
116  yWorkgroups = ceili(input.getHeight(), wgSize[1]);
117  program->dispatch(gpu, xWorkgroups, yWorkgroups, 1);
118 
119  // unlock outputs
120  for (int i = 0; i < numOutputs; ++i)
121  lock.unlock(outputs[i]);
122 }
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.
void writeLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for writing using a specific processing target device.
void prepare(GraphicPipeline &gpu, GL::TextureHandler *inputImage=nullptr)
Definition: cnn.cpp:32
void setInteger(const std::string &name, const int value, bool safe=false)
Assigns a value to a specific integer variable in the program.
Definition: program.cpp:308
void enable(const GraphicPipeline &gpu)
Definition: program.cpp:250
void dispatch(const GraphicPipeline &gpu, msize w, msize h, msize d) const
virtual const int getHeight() const =0
Height of the texture in pixels.
virtual const int getWidth() const =0
Width of the texture in pixels.
void bind(GL::TextureHandler &texture, size_t texUnit, const TextureParam param)
Definition: pipeline.cpp:881
void reshape(int width, int height)
Changes bitmap size.
@ INTERP_NEAREST
nearest neighbor pixel interpolation
#define ceili(x, y)
integer division x/y with ceiling
Definition: utils.hpp:21
return bitmap getWidth()

◆ process() [2/2]

unsigned int GLES31X2UpsamplingNetwork::Layer::process ( GraphicPipeline gpu,
InternalBitmap **  inputs,
GL::StorageBuffer output,
int  numOutputParts 
)

Definition at line 125 of file cnn.cpp.

125  {
126  prepare(gpu);
127 
128  // enable program
129  program->enable(gpu);
130 
131  // bind inputs
132  int bindingCtr = 0;
133  for (int i = 0; i < numInputs; ++i) {
134  InternalBitmap* input = inputs[i];
135  lock.readLock(&gpu, input, ProcessingTarget::GPU);
136  gpu.bind(*input, i, TextureParam::INTERP_NEAREST);
137  }
138  program->setIntegerArray("inFeatures", 0, numInputs);
139 
140  // bind output
141  const unsigned int
142  xWorkgroups = ceili(inputs[0]->getWidth(), wgSize[0]),
143  yWorkgroups = ceili(inputs[0]->getHeight(), wgSize[1]);
144  const unsigned int outputSize = xWorkgroups * wgSize[0] * yWorkgroups * wgSize[1] * (numOutputs * numOutputParts * 4);
145  if (output.getCurrentCapacity() < outputSize)
146  output.allocate(gpu, outputSize);
147  output.bind(gpu, 0);
148 
149  // g-g-go
150  program->dispatch(gpu, xWorkgroups, yWorkgroups, 1);
151 
152  // unlock
153  for (int i = 0; i < numInputs; ++i)
154  lock.unlock(inputs[i]);
155 
156  return xWorkgroups * wgSize[0];
157 }
void readLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for reading using a specific processing target device.
void setIntegerArray(const std::string &name, const int *values, const int length)
Definition: program.cpp:417
void bind(GraphicPipeline &gpu, int unit) const
void allocate(GraphicPipeline &gpu, const size_t sizeBytes, const void *data=nullptr)
size_t getCurrentCapacity() const
Bitmap whose memory is managed by the Beatmup engine.
return bitmap getHeight()

◆ processPointwise() [1/2]

void GLES31X2UpsamplingNetwork::Layer::processPointwise ( GraphicPipeline gpu,
GL::StorageBuffer input,
unsigned int  inputStridePix,
InternalBitmap **  outputs,
int  width,
int  height 
)

Definition at line 160 of file cnn.cpp.

160  {
161  prepare(gpu);
162 
163  // enable program
164  program->enable(gpu);
165 
166  // bind inputs
167  input.bind(gpu, 0);
168  program->setUnsignedInteger("inputStride", inputStridePix);
169 
170  // bind outputs
171  for (int i = 0; i < numOutputs; ++i) {
172  if (outputs[i]->getWidth() != width || outputs[i]->getHeight() != height)
173  outputs[i]->reshape(width, height);
174  lock.writeLock(&gpu, outputs[i], ProcessingTarget::GPU);
175  gpu.bind(*outputs[i], i, false, true);
176  }
177 
178  // g-g-go
179  const unsigned int
180  xWorkgroups = ceili(width, wgSize[0]),
181  yWorkgroups = ceili(height, wgSize[1]);
182  program->dispatch(gpu, xWorkgroups, yWorkgroups, 1);
183 
184  // unlock
185  for (int i = 0; i < numOutputs; ++i)
186  lock.unlock(outputs[i]);
187 }
void setUnsignedInteger(const std::string &name, const unsigned int value, bool safe=false)
Definition: program.cpp:323
jlong jint width
jlong jint jint height

◆ processPointwise() [2/2]

void GLES31X2UpsamplingNetwork::Layer::processPointwise ( GraphicPipeline gpu,
GL::StorageBuffer inputFeatures,
unsigned int  inputStridePix,
GL::TextureHandler inputImage,
AbstractBitmap outputImage 
)

Definition at line 190 of file cnn.cpp.

190  {
191  prepare(gpu, &inputImage);
192 
193  // enable program
194  program->enable(gpu);
195 
196  // bind outputs
197  gpu.bind(output, 1, false, true);
198 
199  // bind inputs
200  inputFeatures.bind(gpu, 0);
201  gpu.bind(inputImage, 0, TextureParam::INTERP_LINEAR);
202  program->setUnsignedInteger("inputStride", inputStridePix);
203  program->setVector2("d1", 1.0f / inputImage.getWidth(), 1.0f / inputImage.getHeight());
204 
205  // g-g-go
206  const unsigned int
207  xWorkgroups = ceili(inputImage.getWidth(), wgSize[0]),
208  yWorkgroups = ceili(inputImage.getHeight(), wgSize[1]);
209  program->dispatch(gpu, xWorkgroups, yWorkgroups, 1);
210 }
void setVector2(const std::string &name, const float x, const float y)
Definition: program.cpp:362
@ INTERP_LINEAR
bilinear pixel interpolation

Member Data Documentation

◆ recycleBin

GL::RecycleBin& Beatmup::GLES31X2UpsamplingNetwork::Layer::recycleBin
private

Definition at line 37 of file cnn.h.

◆ program

GL::ComputeProgram* Beatmup::GLES31X2UpsamplingNetwork::Layer::program
private

Definition at line 38 of file cnn.h.

◆ inputFormat

GL::TextureHandler::TextureFormat Beatmup::GLES31X2UpsamplingNetwork::Layer::inputFormat
private

Definition at line 39 of file cnn.h.

◆ sourceCodeTemplate

std::string Beatmup::GLES31X2UpsamplingNetwork::Layer::sourceCodeTemplate
private

Definition at line 40 of file cnn.h.

◆ lock

BitmapContentLock& Beatmup::GLES31X2UpsamplingNetwork::Layer::lock
private

Definition at line 41 of file cnn.h.

◆ wgSize

unsigned int Beatmup::GLES31X2UpsamplingNetwork::Layer::wgSize[3]
private

Definition at line 43 of file cnn.h.

◆ numInputs

const int Beatmup::GLES31X2UpsamplingNetwork::Layer::numInputs
private

Definition at line 44 of file cnn.h.

◆ numOutputs

const int Beatmup::GLES31X2UpsamplingNetwork::Layer::numOutputs
private

Definition at line 44 of file cnn.h.

◆ prepared

bool Beatmup::GLES31X2UpsamplingNetwork::Layer::prepared
private

Definition at line 45 of file cnn.h.


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