Beatmup
bgl.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 
21 #ifdef BEATMUP_OPENGLVERSION_GLES
22  #include <EGL/egl.h>
23  #include <EGL/eglext.h>
24  #ifdef BEATMUP_OPENGLVERSION_GLES31
25  #include <GLES3/gl31.h>
26  #include <GLES3/gl3ext.h>
27  #include <GLES2/gl2ext.h>
28  #elif BEATMUP_OPENGLVERSION_GLES20
29  #include <GLES2/gl2.h>
30  #include <GLES2/gl2ext.h>
31  #else
32  #error OpenGL ES version is not specified
33  #endif
34  #ifdef BEATMUP_PLATFORM_ANDROID
35  #include <android/native_window_jni.h>
36  #endif
37 #elif BEATMUP_PLATFORM_WINDOWS
38  #include <windows.h>
39  #undef min
40  #undef max
41  #include <gl/glew.h>
42  #include <gl/wglew.h>
43 #else
44  #include <X11/Xlib.h>
45  #include <GL/glxew.h>
46 #endif
47 
48 #include "../exception.h"
49 
50 namespace Beatmup {
51  namespace GL {
52 
53  /**
54  GPU exception
55  */
57  public:
58  GLException(const char* info, int errCode) : Exception("GL error %x: %s", errCode, info) {}
59  GLException(const char* info) : Exception("GL error %x: %s", glGetError(), info) {}
60  GLException(const std::string& info) : GLException(info.c_str()) {}
61 
62  static inline void check(const std::string& info) {
63  GLuint err = glGetError();
64  if (err != GL_NO_ERROR)
65  throw GLException(info.c_str(), err);
66  }
67  };
68 
69  /**
70  Exception communicating the use of an unsupported feature on GPU
71  */
73  public:
74  Unsupported(const char* info) : Exception(info) {}
75  };
76 
77  /**
78  Mapping of bitmap pixel formats to GL pixel formats
79  */
80  const GLuint BITMAP_PIXELFORMATS[] = {
81 #ifdef BEATMUP_OPENGLVERSION_GLES20
82  GL_LUMINANCE, // SingleByte
83 #else
84  GL_RED, // SingleByte
85 #endif
86 
87 
88 #ifdef BEATMUP_CHANNEL_ORDER_BGRA
89  GL_BGR, // TripleByte
90 #else
91  GL_RGB, // TripleByte
92 #endif
93 
94 
95 #ifdef BEATMUP_CHANNEL_ORDER_BGRA
96  GL_BGRA, // QuadByte
97 #elif BEATMUP_CHANNEL_ORDER_ARGB
98  GL_BGRA, // QuadByte; an inversion is done in a modern OpenGL using the pixeltype trick
99 #else
100  GL_RGBA, // QuadByte
101 #endif
102 
103 
104 #ifdef BEATMUP_OPENGLVERSION_GLES20
105  GL_LUMINANCE, // SingleFloat
106 #else
107  GL_RED, // SingleFloat
108 #endif
109 
110 
111 #ifdef BEATMUP_CHANNEL_ORDER_BGRA
112  GL_BGR, // TripleFloat
113 #else
114  GL_RGB, // TripleFloat
115 #endif
116 
117 
118 #ifdef BEATMUP_CHANNEL_ORDER_BGRA
119  GL_BGRA // QuadFloat
120 #elif BEATMUP_CHANNEL_ORDER_ARGB
121  GL_BGRA // QuadFloat
122 #else
123  GL_RGBA // QuadFloat
124 #endif
125  };
126 
127 
129 #ifdef BEATMUP_OPENGLVERSION_GLES20
130  GL_LUMINANCE, // Rx8
131  GL_RGB, // RGBx8
132  GL_RGBA, // RGBAx8
133  GL_LUMINANCE, // Rx32f
134  GL_RGB, // RGBx32f
135  GL_RGBA, // RGBAx32f
136 #else
137  GL_R8, // Rx8
138  GL_RGB8, // RGBx8
139  GL_RGBA8, // RGBAx8
140  GL_R32F, // Rx32f
141  GL_RGB32F, // RGBx32f
142  GL_RGBA32F, // RGBAx32f
143 #endif
144  0
145  };
146 
147  const GLuint BITMAP_INTERNALFORMATS[] = {
154  };
155 
156 
157  /**
158  Mapping of bitmap pixel formats to GL pixel types
159  */
160  const GLuint BITMAP_PIXELTYPES[] = {
161  GL_UNSIGNED_BYTE, // SingleByte
162 
163  GL_UNSIGNED_BYTE, // TripleByte
164 
165 #if BEATMUP_CHANNEL_ORDER_ARGB // QuadByte, special case of ARGB first
166  #ifdef BEATMUP_OPENGLVERSION_GLES
167  GL_UNSIGNED_BYTE, // no way to do in OpenGL ES...
168  #else
169  // QuadByte, inverting BGRA by applying appropriate pixel type. The latter
170  // depends on the current platform endianness.
171  #ifdef BEATMUP_PLATFORM_BIGENDIAN
172  GL_UNSIGNED_INT_8_8_8_8_REV,
173  #else
174  GL_UNSIGNED_INT_8_8_8_8,
175  #endif
176  #endif
177 #else
178  GL_UNSIGNED_BYTE, // QuadByte, other pixel orders
179 #endif
180 
181  GL_FLOAT, // SingleFloat
182 
183  GL_FLOAT, // TripleFloat
184 
185  GL_FLOAT // QuadFloat
186  };
187  }
188 }
Base class for all exceptions.
Definition: exception.h:37
GPU exception.
Definition: bgl.h:56
GLException(const char *info)
Definition: bgl.h:59
GLException(const std::string &info)
Definition: bgl.h:60
static void check(const std::string &info)
Definition: bgl.h:62
GLException(const char *info, int errCode)
Definition: bgl.h:58
Exception communicating the use of an unsupported feature on GPU.
Definition: bgl.h:72
Unsupported(const char *info)
Definition: bgl.h:74
const GLuint TEXTUREHANDLER_INTERNALFORMATS[]
Definition: bgl.h:128
const GLuint BITMAP_INTERNALFORMATS[]
Definition: bgl.h:147
const GLuint BITMAP_PIXELTYPES[]
Mapping of bitmap pixel formats to GL pixel types.
Definition: bgl.h:160
const GLuint BITMAP_PIXELFORMATS[]
Mapping of bitmap pixel formats to GL pixel formats.
Definition: bgl.h:80