Beatmup
pipeline.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../geometry.h"
21 #include "../gpu/texture_handler.h"
22 #include "../bitmap/abstract_bitmap.h"
23 #include "../bitmap/pixel_arithmetic.h"
24 #include "program.h"
25 #include "../gpu/rendering_programs.h"
26 
27 
28 namespace Beatmup {
29  /**
30  Internal low-level GPU control API.
31  Hides OpenGL stuff from intsances of AbstractTask operating with images. A GraphicPipeline instance is likely a singleton accessible from a single thread.
32  */
34  friend class GL::TextureHandler;
35  private:
36  class Impl;
38 
40 
41  GraphicPipeline(const GraphicPipeline&) = delete; // disabling copy constructor
42 
43  public:
44  /**
45  Graphic pipeline working mode setting some common OpenGL switches
46  */
47  enum class Mode {
48  RENDERING, //!< Textures are images to be blended together to produce another image
49  INFERENCE //!< Textures are feature maps computed in fragment shaders
50  };
51 
52  /**
53  GPU characteristics
54  */
55  enum class Limit {
56  TEXTURE_IMAGE_UNITS, //!< maximum number of texture units per fragment shader
57  FRAGMENT_UNIFORM_VECTORS, //!< maximum number of 4-dimensional uniform vectors per fragment shader
62  SHARED_MEM,
63  };
64 
65  static const int
66  ATTRIB_VERTEX_COORD = 0, //!< vertex coordinate attribute index in the VBO
67  ATTRIB_TEXTURE_COORD = 1; //!< texture coordinate attribute index in the VBO
68 
71 
72  void switchDisplay(void* data);
73 
74  void swapBuffers();
75 
76  /**
77  Binds a bitmap to the pipeline output.
78  \param[in] bitmap A bitmap to be filled with pixels on the next render pass.
79  */
81 
82  /**
83  Binds a bitmap to the pipeline output.
84  \param[in] bitmap A bitmap to be filled with pixels on the next render pass.
85  \param[in] viewport Output viewport in pixels: only this area of the bitmap will be affected
86  */
87  void bindOutput(AbstractBitmap& bitmap, const IntRectangle& viewport);
88 
89  void bindOutput(GL::TextureHandler& texture);
90 
91  void bindOutput(GL::handle_t texture);
92 
93  /**
94  Unbinds a bitmap from output and switches to screen
95  */
96  void unbindOutput();
97 
99 
100  void bind(GL::TextureHandler& texture, size_t texUnit, const TextureParam param);
101  void bind(GL::TextureHandler& texture, size_t imageUnit, bool read, bool write);
102 
103  /**
104  Transfers bitmap pixels from GPU to CPU. The bitmap is assumed locked.
105  */
107 
108  /**
109  Transfers bitmap pixels from CPU to GPU. The bitmap is assumed locked.
110  */
112 
113  /**
114  Waits until all operations submitted to GPU are finished.
115  */
116  void flush();
117 
118  int getLimit(Limit limit) const;
119 
120  /**
121  Switches GPU mode
122  */
123  void switchMode(Mode mode);
124 
127 
128  const char* getGpuVendorString() const;
129  const char* getGpuRendererString() const;
130 
131  /**
132  Returns GLSL language version supported by the GPU context being used.
133  */
134  int getGlslVersion() const;
135 
136  /**
137  Returns `true` if the GPU context is OpenGL ES-compliant.
138  */
139  bool isGlEsCompliant() const;
140 
141  /**
142  Specifies texture coordinates for the next rendering pass.
143  \param[in] coords Normalized texture coordinates
144  */
145  void setTextureCoordinates(const Rectangle& coords);
146 
147  /**
148  Specifies texture coordinates for the next rendering pass.
149  \param[in] leftTop Top left output image corner texture coordinates
150  \param[in] rightTop Top right output image corner texture coordinates
151  \param[in] leftBottom Bottom left output image corner texture coordinates
152  \param[in] rightBottom Bottom right output image corner texture coordinates
153  */
154  void setTextureCoordinates(const Point& leftTop, const Point& rightTop, const Point& leftBottom, const Point& rightBottom);
155 
156  /**
157  Specifies texture coordinates for the next rendering pass.
158  \param[in] area A closed rectangle in pixels of a texture to sample; all its corners are valid sampling
159  locations.
160  \param[in] size Size in pixels of the input texture
161  \param[in] sampling Number of resulting samples covering the area
162  */
163  inline void setTextureCoordinates(const Rectangle& area, const IntPoint& size, const IntPoint& sampling) {
165  }
166 
167  /**
168  Computes floating-point texture coordinates for pixel-accurate sampling: a texture gets sampled exactly
169  at specified pixel locations.
170  \param[in] area A closed rectangle in pixels of a texture to sample; all its corners are valid sampling
171  locations.
172  \param[in] size Size in pixels of the input texture
173  \param[in] sampling Number of resulting samples covering the area
174  */
175  static Rectangle getTextureCoordinates(const Rectangle& area, const IntPoint& size, const IntPoint& sampling);
176  };
177 }
A very basic class for any image.
Handles a collection of common rendering programs of predefined types and shared operations among the...
const VertexShader & getDefaultVertexShader(const GraphicPipeline *gpu) const
GLSL vertex shader.
Definition: program.h:93
Internal low-level GPU control API.
Definition: pipeline.h:33
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...
Definition: pipeline.cpp:976
void switchMode(Mode mode)
Switches GPU mode.
Definition: pipeline.cpp:941
const GL::VertexShader & getDefaultVertexShader() const
Definition: pipeline.h:126
const char * getGpuRendererString() const
Definition: pipeline.cpp:951
const ImageResolution & getDisplayResolution() const
Definition: pipeline.cpp:916
int getLimit(Limit limit) const
Definition: pipeline.cpp:936
void pullPixels(AbstractBitmap &bitmap)
Transfers bitmap pixels from GPU to CPU.
Definition: pipeline.cpp:921
GraphicPipeline(const GraphicPipeline &)=delete
int getGlslVersion() const
Returns GLSL language version supported by the GPU context being used.
Definition: pipeline.cpp:956
const char * getGpuVendorString() const
Definition: pipeline.cpp:946
void pushPixels(AbstractBitmap &bitmap)
Transfers bitmap pixels from CPU to GPU.
Definition: pipeline.cpp:926
void bindOutput(AbstractBitmap &bitmap)
Binds a bitmap to the pipeline output.
Definition: pipeline.cpp:891
Mode
Graphic pipeline working mode setting some common OpenGL switches.
Definition: pipeline.h:47
@ RENDERING
Textures are images to be blended together to produce another image.
@ INFERENCE
Textures are feature maps computed in fragment shaders.
void setTextureCoordinates(const Rectangle &coords)
Specifies texture coordinates for the next rendering pass.
Definition: pipeline.cpp:966
void switchDisplay(void *data)
Definition: pipeline.cpp:871
Limit
GPU characteristics.
Definition: pipeline.h:55
@ TEXTURE_IMAGE_UNITS
maximum number of texture units per fragment shader
@ FRAGMENT_UNIFORM_VECTORS
maximum number of 4-dimensional uniform vectors per fragment shader
static const int ATTRIB_TEXTURE_COORD
texture coordinate attribute index in the VBO
Definition: pipeline.h:67
GL::RenderingPrograms * renderingPrograms
Definition: pipeline.h:39
void flush()
Waits until all operations submitted to GPU are finished.
Definition: pipeline.cpp:931
void bind(GL::TextureHandler &texture, size_t texUnit, const TextureParam param)
Definition: pipeline.cpp:881
void unbindOutput()
Unbinds a bitmap from output and switches to screen.
Definition: pipeline.cpp:911
GL::RenderingPrograms & getRenderingPrograms()
Definition: pipeline.h:125
static const int ATTRIB_VERTEX_COORD
vertex coordinate attribute index in the VBO
Definition: pipeline.h:66
void setTextureCoordinates(const Rectangle &area, const IntPoint &size, const IntPoint &sampling)
Specifies texture coordinates for the next rendering pass.
Definition: pipeline.h:163
bool isGlEsCompliant() const
Returns true if the GPU context is OpenGL ES-compliant.
Definition: pipeline.cpp:961
Represents image size in pixels.
void write(Bitmap &bitmap, Args &&... args)
Calls a Func< WriterClass >::process(access, params) that writes to a bitmap of any kind,...
Definition: processing.h:90
void read(Bitmap &bitmap, Args &&... args)
Calls a Func< ReaderClass >::process(access, params), where.
Definition: processing.h:49
unsigned int handle_t
A reference to a GPU resource.
Definition: basic_types.h:61
TextureParam
Parameters of binding a texture to a texture unit on GPU.
Beatmup::InternalBitmap * bitmap
JNIEnv jlong jint mode
jlong jobject size