Beatmup
texture_handler.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 "../basic_types.h"
21 #include "../exception.h"
22 #include "recycle_bin.h"
23 
24 namespace Beatmup {
25  class GraphicPipeline;
26 
27  /**
28  Parameters of binding a texture to a texture unit on GPU.
29  */
30  enum TextureParam {
31  INTERP_NEAREST = 0, //!< nearest neighbor pixel interpolation
32  INTERP_LINEAR = 1, //!< bilinear pixel interpolation
33  REPEAT = 2 //!< wrapping the texture by repeating instead of clamping to edge
34  };
35 
36  namespace GL {
38  friend class ::Beatmup::GraphicPipeline;
39 
40  public:
41  /**
42  Texture format, specifies how the texture should be interpreted on the shader side
43  */
45  Rx8,
51  OES_Ext //!< external EGL image
52  };
53 
54  static const int TEXTURE_FORMAT_BYTES_PER_PIXEL[]; //!< size of a texel in bytes for different texture formats
55 
56  static const char* textureFormatToString(const TextureFormat&);
57 
58  protected:
60 
62 
63  /**
64  Prepares (eventually uploads) texture data on GPU.
65  Called only by the context managing thread.
66  \param[in] gpu Graphic pipeline instance
67  */
68  virtual void prepare(GraphicPipeline& gpu);
69 
70  /**
71  Forces disposing the texture data, e.g. when it is not used any more
72  */
73  void invalidate(RecycleBin&);
74 
75  public:
77 
78  /**
79  Width of the texture in pixels
80  */
81  virtual const int getWidth() const = 0;
82 
83  /**
84  Height of the texture in pixels
85  */
86  virtual const int getHeight() const = 0;
87 
88  /**
89  Depth of the texture in pixels
90  */
91  virtual const int getDepth() const = 0;
92 
93  /**
94  Aspect ratio of the texture.
95  */
96  float getAspectRatio() const { return (float)getWidth() / getHeight(); }
97 
98  /**
99  Inverse of the aspect ratio of the texture.
100  */
101  float getInvAspectRatio() const { return (float)getHeight() / getWidth(); }
102 
103  /**
104  Returns the texture format specifying how the shader must interpret the data
105  */
106  virtual const TextureFormat getTextureFormat() const = 0;
107 
108  const bool isFloatingPoint() const {
110  return format == TextureFormat::Rx32f || format == TextureFormat::RGBx32f || format == TextureFormat::RGBAx32f;
111  }
112 
113  /**
114  Returns number of channels containing in the texture
115  */
116  const int getNumberOfChannels() const;
117 
118  /**
119  Returns `true` if the texture handle points to a valid texture
120  */
121  inline bool hasValidHandle() const { return textureHandle > 0; }
122  };
123  }
124 
125  /**
126  Exception thrown when texture format does not match any supported format
127  */
129  public:
131  Exception("Input texture format is not supported: %s", GL::TextureHandler::textureFormatToString(format))
132  {}
133  };
134 }
Base class for all exceptions.
Definition: exception.h:37
Stores references to GPU resources that will not be used anymore and needed to be recycled in a threa...
Definition: recycle_bin.h:34
virtual const int getDepth() const =0
Depth of the texture in pixels.
const bool isFloatingPoint() const
static const char * textureFormatToString(const TextureFormat &)
const int getNumberOfChannels() const
Returns number of channels containing in the texture.
bool hasValidHandle() const
Returns true if the texture handle points to a valid texture.
virtual const int getHeight() const =0
Height of the texture in pixels.
virtual void prepare(GraphicPipeline &gpu)
Prepares (eventually uploads) texture data on GPU.
TextureFormat
Texture format, specifies how the texture should be interpreted on the shader side.
@ OES_Ext
external EGL image
static const int TEXTURE_FORMAT_BYTES_PER_PIXEL[]
size of a texel in bytes for different texture formats
virtual const TextureFormat getTextureFormat() const =0
Returns the texture format specifying how the shader must interpret the data.
void invalidate(RecycleBin &)
Forces disposing the texture data, e.g.
float getInvAspectRatio() const
Inverse of the aspect ratio of the texture.
float getAspectRatio() const
Aspect ratio of the texture.
virtual const int getWidth() const =0
Width of the texture in pixels.
Internal low-level GPU control API.
Definition: pipeline.h:33
Beatmup object base class
Definition: basic_types.h:67
Exception thrown when texture format does not match any supported format.
UnsupportedTextureFormat(const GL::TextureHandler::TextureFormat &format)
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.
@ INTERP_LINEAR
bilinear pixel interpolation
@ INTERP_NEAREST
nearest neighbor pixel interpolation
@ REPEAT
wrapping the texture by repeating instead of clamping to edge
JNIEnv jobject jint format