-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathFramebuffer.cpp
More file actions
378 lines (335 loc) · 13.8 KB
/
Framebuffer.cpp
File metadata and controls
378 lines (335 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*****************************************************************************
* Alpine Renderer
* Copyright (C) 2022 Adam Celarek
* Copyright (C) 2023 Jakob Lindner
* Copyright (C) 2023 Gerald Kimmersdorfer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "Framebuffer.h"
#include <QOpenGLExtraFunctions>
#include <QOpenGLFunctions>
#include <QOpenGLTexture>
#ifdef ANDROID
#include <GLES3/gl3.h>
#endif
namespace gl_engine {
// https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml
QOpenGLTexture::TextureFormat internal_format_qt(Framebuffer::ColourFormat f)
{
switch (f) {
case Framebuffer::ColourFormat::R8:
return QOpenGLTexture::TextureFormat::R8_UNorm;
case Framebuffer::ColourFormat::RGB8:
return QOpenGLTexture::TextureFormat::RGB8_UNorm;
case Framebuffer::ColourFormat::RGBA8:
return QOpenGLTexture::TextureFormat::RGBA8_UNorm;
// case Framebuffer::ColourFormat::SRGBA8:
// return QOpenGLTexture::TextureFormat::SRGB8_Alpha8;
case Framebuffer::ColourFormat::RG16UI:
return QOpenGLTexture::TextureFormat::RG16U;
// case Framebuffer::ColourFormat::Float32:
// return QOpenGLTexture::TextureFormat::R32F;
// case Framebuffer::ColourFormat::RGB16F:
// return QOpenGLTexture::TextureFormat::RGB16F;
// case Framebuffer::ColourFormat::RGBA16F:
// return QOpenGLTexture::TextureFormat::RGBA16F;
case Framebuffer::ColourFormat::R32UI:
return QOpenGLTexture::TextureFormat::R32U;
case Framebuffer::ColourFormat::RGBA32F:
return QOpenGLTexture::TextureFormat::RGBA32F;
}
assert(false);
return QOpenGLTexture::TextureFormat::NoFormat;
}
GLenum format(Framebuffer::ColourFormat f)
{
switch (f) {
case Framebuffer::ColourFormat::R8:
return GL_RED;
case Framebuffer::ColourFormat::RGB8:
return GL_RGB;
case Framebuffer::ColourFormat::RGBA8:
return GL_RGBA;
// case Framebuffer::ColourFormat::SRGBA8:
// return GL_RGBA;
case Framebuffer::ColourFormat::RG16UI:
return QOpenGLTexture::PixelFormat::RG_Integer;
// case Framebuffer::ColourFormat::Float32: // reading Float32 is inefficient, see read_colour_attachment() for details.
// return GL_RED;
// case Framebuffer::ColourFormat::RGB16F:
// return GL_RGB;
// case Framebuffer::ColourFormat::RGBA16F:
// return GL_RGBA;
case Framebuffer::ColourFormat::R32UI:
return GL_RED_INTEGER;
case Framebuffer::ColourFormat::RGBA32F:
return GL_RGBA;
}
assert(false);
return GLenum(-1);
}
GLenum format(Framebuffer::DepthFormat f)
{
if (f != Framebuffer::DepthFormat::None) return GL_DEPTH_COMPONENT;
return GLenum(-1);
}
QOpenGLTexture::TextureFormat internal_format_qt(Framebuffer::DepthFormat f)
{
switch (f) {
case Framebuffer::DepthFormat::Int16:
return QOpenGLTexture::TextureFormat::D16;
case Framebuffer::DepthFormat::Int24:
return QOpenGLTexture::TextureFormat::D24;
case Framebuffer::DepthFormat::Float32:
return QOpenGLTexture::TextureFormat::D32F;
case Framebuffer::DepthFormat::None: // prevent compiler warning
assert(false); // extra assert, so we can from the line number which issue it is
return QOpenGLTexture::TextureFormat::NoFormat;
}
assert(false);
return QOpenGLTexture::TextureFormat::NoFormat;
}
GLenum type(Framebuffer::ColourFormat f)
{
switch (f) {
case Framebuffer::ColourFormat::R8:
case Framebuffer::ColourFormat::RGBA8:
// case Framebuffer::ColourFormat::SRGBA8:
case Framebuffer::ColourFormat::RGB8:
return GL_UNSIGNED_BYTE;
case Framebuffer::ColourFormat::RG16UI:
return QOpenGLTexture::PixelType::UInt16;
// case Framebuffer::ColourFormat::Float32:
case Framebuffer::ColourFormat::RGBA32F:
return GL_FLOAT;
// case Framebuffer::ColourFormat::RGB16F:
// case Framebuffer::ColourFormat::RGBA16F:
// return GL_HALF_FLOAT;
case Framebuffer::ColourFormat::R32UI:
return GL_UNSIGNED_INT;
}
assert(false);
return GLenum(-1);
}
GLenum type(Framebuffer::DepthFormat f)
{
switch (f) {
case Framebuffer::DepthFormat::Int16:
return GL_UNSIGNED_SHORT;
case Framebuffer::DepthFormat::Int24:
return GL_UNSIGNED_INT;
case Framebuffer::DepthFormat::Float32:
return GL_FLOAT;
case Framebuffer::DepthFormat::None: // prevent compiler warning
assert(false); // extra assert, so we can from the line number which issue it is
return GLenum(-1);
}
assert(false);
return GLenum(-1);
}
// https://doc.qt.io/qt-6/qimage.html#Format-enum
QImage::Format qimage_format(Framebuffer::ColourFormat f)
{
switch (f) {
case Framebuffer::ColourFormat::R8:
return QImage::Format_Grayscale8;
case Framebuffer::ColourFormat::RGBA8:
return QImage::Format_RGBA8888;
case Framebuffer::ColourFormat::RGB8:
return QImage::Format_RGB888;
// case Framebuffer::ColourFormat::RGB16F:
// return QImage::Format_RGB16;
default:
throw std::logic_error("unsupported, QImage does not support the color format of the texture");
}
assert(false);
return QImage::Format_Invalid;
}
void Framebuffer::recreate_texture(size_t index)
{
if (index == size_t(-1)) {
if (m_depth_format != DepthFormat::None) {
m_depth_texture->destroy();
m_depth_texture->setFormat(internal_format_qt(m_depth_format));
m_depth_texture->setSize(int(m_size.x), int(m_size.y));
m_depth_texture->setAutoMipMapGenerationEnabled(false);
// No filtering of depth buffer possible in OpenGLES and WebGL!!!!
m_depth_texture->setMinMagFilters(QOpenGLTexture::Filter::Nearest, QOpenGLTexture::Filter::Nearest);
#if (defined(__linux) && !defined(__ANDROID__)) || defined(_WIN32) || defined(_WIN64)
// No support on WebGL and OpenGL ES for Border (Warning: On those platforms we just ignore the wrap mode)
// Feel free to make this code better!!
m_depth_texture->setWrapMode(QOpenGLTexture::WrapMode::ClampToBorder);
#endif
m_depth_texture->allocateStorage();
}
} else {
m_colour_textures[index]->destroy();
m_colour_textures[index]->setFormat(internal_format_qt(m_colour_definitions[index]));
m_colour_textures[index]->setSize(int(m_size.x), int(m_size.y));
m_colour_textures[index]->setAutoMipMapGenerationEnabled(false);
m_colour_textures[index]->setMinMagFilters(QOpenGLTexture::Filter::Nearest, QOpenGLTexture::Filter::Nearest);
m_colour_textures[index]->setWrapMode(QOpenGLTexture::WrapMode::ClampToEdge);
// WARNING: If format and type not specifically defined in the following function it will crash for uint-textures
// on OpenGL ES (Android). Might be a bug with the default of QOpenGLTexture on that platform.
m_colour_textures[index]->allocateStorage(
(QOpenGLTexture::PixelFormat)format(m_colour_definitions[index]), (QOpenGLTexture::PixelType)type(m_colour_definitions[index]));
}
}
void Framebuffer::recreate_all_textures() {
recreate_texture(size_t(-1));
for (size_t i = 0; i < m_colour_textures.size(); i++)
recreate_texture(i);
}
Framebuffer::Framebuffer(DepthFormat depth_format, std::vector<Framebuffer::ColourFormat> colour_definitions, glm::uvec2 init_size)
: m_depth_format(depth_format)
, m_colour_definitions(std::move(colour_definitions))
, m_size(init_size)
{
for (size_t i = 0; i < m_colour_definitions.size(); i++) {
auto colorTexture = std::make_unique<QOpenGLTexture>(QOpenGLTexture::Target::Target2D);
m_colour_textures.push_back(std::move(colorTexture));
}
if (m_depth_format != DepthFormat::None) {
m_depth_texture = std::make_unique<QOpenGLTexture>(QOpenGLTexture::Target::Target2D);
}
recreate_all_textures();
QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions();
f->glBindTexture(GL_TEXTURE_2D, 0);
f->glGenFramebuffers(1, &m_frame_buffer);
reset_fbo();
}
Framebuffer::~Framebuffer()
{
QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions();
f->glDeleteFramebuffers(1, &m_frame_buffer);
}
void Framebuffer::resize(const glm::uvec2& new_size)
{
m_size = new_size;
recreate_all_textures();
reset_fbo();
}
void Framebuffer::bind()
{
QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions();
f->glViewport(0, 0, int(m_size.x), int(m_size.y));
f->glBindFramebuffer(GL_FRAMEBUFFER, m_frame_buffer);
}
void Framebuffer::bind_colour_texture(unsigned index, unsigned location)
{
assert(index < m_colour_textures.size());
m_colour_textures[index]->bind(location);
}
void Framebuffer::bind_depth_texture(unsigned location)
{
assert(m_depth_format != DepthFormat::None);
m_depth_texture->bind(location);
}
QOpenGLTexture* Framebuffer::depth_texture()
{
return m_depth_texture.get();
}
QImage Framebuffer::read_colour_attachment(unsigned index)
{
assert(index < m_colour_textures.size());
auto texFormat = m_colour_definitions[index];
// Float framebuffers can not be read efficiently on all environments.
// that is, reading float red channel crashes on linux webassembly, but reading it as rgba is inefficient on linux native (4x slower, yes I measured).
// i'm removing the old reading code altogether in order not to be tempted to use it in production (or by accident).
// if you need to read a float32 buffer, pack the float into rgba8 values!
if (texFormat != ColourFormat::RGBA8)
qWarning() << "Reading back a different texture format than RGBA8 is not recommended.";
bind();
QOpenGLExtraFunctions* f = QOpenGLContext::currentContext()->extraFunctions();
f->glReadBuffer(GL_COLOR_ATTACHMENT0 + index);
QImage image({ static_cast<int>(m_size.x), static_cast<int>(m_size.y) }, qimage_format(texFormat));
assert(!image.isNull());
f->glReadPixels(0, 0, int(m_size.x), int(m_size.y), format(texFormat), type(texFormat), image.bits());
#if QT_VERSION > QT_VERSION_CHECK(6, 9, 0)
image.flip(Qt::Orientation::Vertical);
#else
image.mirror();
#endif
return image;
}
template <typename T>
T Framebuffer::read_colour_attachment_pixel(unsigned int index, const glm::dvec2& normalised_device_coordinates)
{
assert(index < m_colour_textures.size());
auto texFormat = m_colour_definitions[index];
switch (texFormat) {
case Framebuffer::ColourFormat::R8:
case Framebuffer::ColourFormat::RGB8:
case Framebuffer::ColourFormat::RG16UI: // unsupported on android emulator (and webassembly linux firefox?)
// case Framebuffer::ColourFormat::Float32:
// case Framebuffer::ColourFormat::RGB16F:
// case Framebuffer::ColourFormat::RGBA16F:
case Framebuffer::ColourFormat::R32UI: // fails on linux firefox
// unsupported or untested.
// you really should add a unit test if you move something down to the supported section
// as the support accross platforms (webassembly, android, ios?) is patchy
assert(false);
return {};
case Framebuffer::ColourFormat::RGBA8:
// case Framebuffer::ColourFormat::SRGBA8:
assert(sizeof(T) == 4);
if (sizeof(T) != 4)
return {};
break;
case Framebuffer::ColourFormat::RGBA32F:
assert(sizeof(T) == 16);
if (sizeof(T) != 16)
return {};
break;
}
QOpenGLExtraFunctions* f = QOpenGLContext::currentContext()->extraFunctions();
bind();
f->glReadBuffer(GL_COLOR_ATTACHMENT0 + index);
T pixel;
f->glReadPixels(
int((normalised_device_coordinates.x + 1) / 2 * m_size.x),
int((normalised_device_coordinates.y + 1) / 2 * m_size.y),
1, 1, format(texFormat), type(texFormat), reinterpret_cast<void*>(&pixel));
unbind();
return pixel;
}
template glm::vec4 Framebuffer::read_colour_attachment_pixel<glm::vec4>(unsigned index, const glm::dvec2& normalised_device_coordinates);
template glm::u8vec4 Framebuffer::read_colour_attachment_pixel<glm::u8vec4>(unsigned index, const glm::dvec2& normalised_device_coordinates);
void Framebuffer::unbind()
{
QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions();
f->glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::reset_fbo()
{
QOpenGLExtraFunctions* f = QOpenGLContext::currentContext()->extraFunctions();
f->glBindFramebuffer(GL_FRAMEBUFFER, m_frame_buffer);
// unsigned int draw_attachments[m_colour_textures.size()];
std::vector<unsigned> draw_attachments;
for (unsigned i = 0; i < m_colour_textures.size(); i++) {
draw_attachments.push_back(GL_COLOR_ATTACHMENT0 + i);
f->glFramebufferTexture2D(GL_FRAMEBUFFER, draw_attachments.back(), GL_TEXTURE_2D, m_colour_textures[i]->textureId(), 0);
}
if (m_depth_format != DepthFormat::None)
f->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depth_texture->textureId(), 0);
// Tell OpenGL how many attachments to use
f->glDrawBuffers(m_colour_textures.size(), draw_attachments.data());
// assert(f->glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
}
glm::uvec2 Framebuffer::size() const
{
return m_size;
}
}