| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qopenglframebufferobject.h" |
| 5 | #include "qopenglframebufferobject_p.h" |
| 6 | |
| 7 | #include <qdebug.h> |
| 8 | #include <private/qopengl_p.h> |
| 9 | #include <private/qopenglcontext_p.h> |
| 10 | #include <private/qopenglextensions_p.h> |
| 11 | #include <private/qfont_p.h> |
| 12 | |
| 13 | #include <qwindow.h> |
| 14 | #include <qimage.h> |
| 15 | #include <QtCore/qbytearray.h> |
| 16 | |
| 17 | #include <qtopengl_tracepoints_p.h> |
| 18 | |
| 19 | QT_BEGIN_NAMESPACE |
| 20 | |
| 21 | Q_TRACE_PREFIX(qtopengl, |
| 22 | "#include <private/qopengl2pexvertexarray_p.h>" \ |
| 23 | "#include <private/qopengltextureuploader_p.h>" \ |
| 24 | "#include <qopenglframebufferobject.h>" |
| 25 | ); |
| 26 | Q_TRACE_PARAM_REPLACE(GLenum, int); |
| 27 | Q_TRACE_PARAM_REPLACE(GLint, int); |
| 28 | Q_TRACE_METADATA(qtopengl, "ENUM { } QOpenGLFramebufferObject::Attachment; " ); |
| 29 | |
| 30 | #ifndef QT_NO_DEBUG |
| 31 | #define QT_RESET_GLERROR() \ |
| 32 | { \ |
| 33 | while (true) {\ |
| 34 | GLenum error = QOpenGLContext::currentContext()->functions()->glGetError(); \ |
| 35 | if (error == GL_NO_ERROR || error == GL_CONTEXT_LOST) \ |
| 36 | break; \ |
| 37 | } \ |
| 38 | } |
| 39 | #define QT_CHECK_GLERROR() \ |
| 40 | { \ |
| 41 | GLenum err = QOpenGLContext::currentContext()->functions()->glGetError(); \ |
| 42 | if (err != GL_NO_ERROR && err != GL_CONTEXT_LOST) { \ |
| 43 | qDebug("[%s line %d] OpenGL Error: %d", \ |
| 44 | __FILE__, __LINE__, (int)err); \ |
| 45 | } \ |
| 46 | } |
| 47 | #else |
| 48 | #define QT_RESET_GLERROR() {} |
| 49 | #define QT_CHECK_GLERROR() {} |
| 50 | #endif |
| 51 | |
| 52 | #ifndef GL_MAX_SAMPLES |
| 53 | #define GL_MAX_SAMPLES 0x8D57 |
| 54 | #endif |
| 55 | |
| 56 | #ifndef GL_RENDERBUFFER_SAMPLES |
| 57 | #define GL_RENDERBUFFER_SAMPLES 0x8CAB |
| 58 | #endif |
| 59 | |
| 60 | #ifndef GL_DEPTH24_STENCIL8 |
| 61 | #define GL_DEPTH24_STENCIL8 0x88F0 |
| 62 | #endif |
| 63 | |
| 64 | #ifndef GL_DEPTH_COMPONENT24 |
| 65 | #define GL_DEPTH_COMPONENT24 0x81A6 |
| 66 | #endif |
| 67 | |
| 68 | #ifndef GL_DEPTH_COMPONENT24_OES |
| 69 | #define GL_DEPTH_COMPONENT24_OES 0x81A6 |
| 70 | #endif |
| 71 | |
| 72 | #ifndef GL_READ_FRAMEBUFFER |
| 73 | #define GL_READ_FRAMEBUFFER 0x8CA8 |
| 74 | #endif |
| 75 | |
| 76 | #ifndef GL_DRAW_FRAMEBUFFER |
| 77 | #define GL_DRAW_FRAMEBUFFER 0x8CA9 |
| 78 | #endif |
| 79 | |
| 80 | #ifndef GL_RGB8 |
| 81 | #define GL_RGB8 0x8051 |
| 82 | #endif |
| 83 | |
| 84 | #ifndef GL_RGB10 |
| 85 | #define GL_RGB10 0x8052 |
| 86 | #endif |
| 87 | |
| 88 | #ifndef GL_RGB16 |
| 89 | #define GL_RGB16 0x8054 |
| 90 | #endif |
| 91 | |
| 92 | #ifndef GL_RGBA8 |
| 93 | #define GL_RGBA8 0x8058 |
| 94 | #endif |
| 95 | |
| 96 | #ifndef GL_RGB10_A2 |
| 97 | #define GL_RGB10_A2 0x8059 |
| 98 | #endif |
| 99 | |
| 100 | #ifndef GL_RGBA16 |
| 101 | #define GL_RGBA16 0x805B |
| 102 | #endif |
| 103 | |
| 104 | #ifndef GL_BGRA |
| 105 | #define GL_BGRA 0x80E1 |
| 106 | #endif |
| 107 | |
| 108 | #ifndef GL_UNSIGNED_INT_8_8_8_8_REV |
| 109 | #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367 |
| 110 | #endif |
| 111 | |
| 112 | #ifndef GL_UNSIGNED_INT_2_10_10_10_REV |
| 113 | #define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368 |
| 114 | #endif |
| 115 | |
| 116 | #ifndef GL_CONTEXT_LOST |
| 117 | #define GL_CONTEXT_LOST 0x0507 |
| 118 | #endif |
| 119 | |
| 120 | #ifndef GL_DEPTH_STENCIL_ATTACHMENT |
| 121 | #define GL_DEPTH_STENCIL_ATTACHMENT 0x821A |
| 122 | #endif |
| 123 | |
| 124 | #ifndef GL_DEPTH_STENCIL |
| 125 | #define GL_DEPTH_STENCIL 0x84F9 |
| 126 | #endif |
| 127 | |
| 128 | #ifndef GL_HALF_FLOAT |
| 129 | #define GL_HALF_FLOAT 0x140B |
| 130 | #endif |
| 131 | |
| 132 | #ifndef GL_RGBA32F |
| 133 | #define GL_RGBA32F 0x8814 |
| 134 | #endif |
| 135 | |
| 136 | #ifndef GL_RGB32F |
| 137 | #define GL_RGB32F 0x8815 |
| 138 | #endif |
| 139 | |
| 140 | #ifndef GL_RGBA16F |
| 141 | #define GL_RGBA16F 0x881A |
| 142 | #endif |
| 143 | |
| 144 | #ifndef GL_RGB16F |
| 145 | #define GL_RGB16F 0x881B |
| 146 | #endif |
| 147 | |
| 148 | |
| 149 | /*! |
| 150 | \class QOpenGLFramebufferObjectFormat |
| 151 | \brief The QOpenGLFramebufferObjectFormat class specifies the format of an OpenGL |
| 152 | framebuffer object. |
| 153 | \inmodule QtOpenGL |
| 154 | |
| 155 | \since 5.0 |
| 156 | |
| 157 | \ingroup painting-3D |
| 158 | |
| 159 | A framebuffer object has several characteristics: |
| 160 | \list |
| 161 | \li \l{setSamples()}{Number of samples per pixels.} |
| 162 | \li \l{setAttachment()}{Depth and/or stencil attachments.} |
| 163 | \li \l{setTextureTarget()}{Texture target.} |
| 164 | \li \l{setInternalTextureFormat()}{Internal texture format.} |
| 165 | \endlist |
| 166 | |
| 167 | Note that the desired attachments or number of samples per pixels might not |
| 168 | be supported by the hardware driver. Call QOpenGLFramebufferObject::format() |
| 169 | after creating a QOpenGLFramebufferObject to find the exact format that was |
| 170 | used to create the frame buffer object. |
| 171 | |
| 172 | \sa QOpenGLFramebufferObject |
| 173 | */ |
| 174 | |
| 175 | /*! |
| 176 | \internal |
| 177 | */ |
| 178 | void QOpenGLFramebufferObjectFormat::detach() |
| 179 | { |
| 180 | if (d->ref.loadRelaxed() != 1) { |
| 181 | QOpenGLFramebufferObjectFormatPrivate *newd |
| 182 | = new QOpenGLFramebufferObjectFormatPrivate(d); |
| 183 | if (!d->ref.deref()) |
| 184 | delete d; |
| 185 | d = newd; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /*! |
| 190 | Creates a QOpenGLFramebufferObjectFormat object for specifying |
| 191 | the format of an OpenGL framebuffer object. |
| 192 | |
| 193 | By default the format specifies a non-multisample framebuffer object with no |
| 194 | depth/stencil attachments, texture target \c GL_TEXTURE_2D, and internal format \c GL_RGBA8. |
| 195 | On OpenGL/ES systems, the default internal format is \c GL_RGBA. |
| 196 | |
| 197 | \sa samples(), attachment(), internalTextureFormat() |
| 198 | */ |
| 199 | |
| 200 | QOpenGLFramebufferObjectFormat::QOpenGLFramebufferObjectFormat() |
| 201 | { |
| 202 | d = new QOpenGLFramebufferObjectFormatPrivate; |
| 203 | } |
| 204 | |
| 205 | /*! |
| 206 | Constructs a copy of \a other. |
| 207 | */ |
| 208 | |
| 209 | QOpenGLFramebufferObjectFormat::QOpenGLFramebufferObjectFormat(const QOpenGLFramebufferObjectFormat &other) |
| 210 | { |
| 211 | d = other.d; |
| 212 | d->ref.ref(); |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | Assigns \a other to this object. |
| 217 | */ |
| 218 | |
| 219 | QOpenGLFramebufferObjectFormat &QOpenGLFramebufferObjectFormat::operator=(const QOpenGLFramebufferObjectFormat &other) |
| 220 | { |
| 221 | if (d != other.d) { |
| 222 | other.d->ref.ref(); |
| 223 | if (!d->ref.deref()) |
| 224 | delete d; |
| 225 | d = other.d; |
| 226 | } |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | /*! |
| 231 | Destroys the QOpenGLFramebufferObjectFormat. |
| 232 | */ |
| 233 | QOpenGLFramebufferObjectFormat::~QOpenGLFramebufferObjectFormat() |
| 234 | { |
| 235 | if (!d->ref.deref()) |
| 236 | delete d; |
| 237 | } |
| 238 | |
| 239 | /*! |
| 240 | Sets the number of samples per pixel for a multisample framebuffer object |
| 241 | to \a samples. The default sample count of 0 represents a regular |
| 242 | non-multisample framebuffer object. |
| 243 | |
| 244 | If the desired amount of samples per pixel is not supported by the hardware |
| 245 | then the maximum number of samples per pixel will be used. Note that |
| 246 | multisample framebuffer objects cannot be bound as textures. Also, the |
| 247 | \c{GL_EXT_framebuffer_multisample} extension is required to create a |
| 248 | framebuffer with more than one sample per pixel. |
| 249 | |
| 250 | \sa samples() |
| 251 | */ |
| 252 | void QOpenGLFramebufferObjectFormat::setSamples(int samples) |
| 253 | { |
| 254 | detach(); |
| 255 | d->samples = samples; |
| 256 | } |
| 257 | |
| 258 | /*! |
| 259 | Returns the number of samples per pixel if a framebuffer object |
| 260 | is a multisample framebuffer object. Otherwise, returns 0. |
| 261 | The default value is 0. |
| 262 | |
| 263 | \sa setSamples() |
| 264 | */ |
| 265 | int QOpenGLFramebufferObjectFormat::samples() const |
| 266 | { |
| 267 | return d->samples; |
| 268 | } |
| 269 | |
| 270 | /*! |
| 271 | Enables mipmapping if \a enabled is true; otherwise disables it. |
| 272 | |
| 273 | Mipmapping is disabled by default. |
| 274 | |
| 275 | If mipmapping is enabled, additional memory will be allocated for |
| 276 | the mipmap levels. The mipmap levels can be updated by binding the |
| 277 | texture and calling glGenerateMipmap(). Mipmapping cannot be enabled |
| 278 | for multisampled framebuffer objects. |
| 279 | |
| 280 | \sa mipmap(), QOpenGLFramebufferObject::texture() |
| 281 | */ |
| 282 | void QOpenGLFramebufferObjectFormat::setMipmap(bool enabled) |
| 283 | { |
| 284 | detach(); |
| 285 | d->mipmap = enabled; |
| 286 | } |
| 287 | |
| 288 | /*! |
| 289 | Returns \c true if mipmapping is enabled. |
| 290 | |
| 291 | \sa setMipmap() |
| 292 | */ |
| 293 | bool QOpenGLFramebufferObjectFormat::mipmap() const |
| 294 | { |
| 295 | return d->mipmap; |
| 296 | } |
| 297 | |
| 298 | /*! |
| 299 | Sets the attachment configuration of a framebuffer object to \a attachment. |
| 300 | |
| 301 | \sa attachment() |
| 302 | */ |
| 303 | void QOpenGLFramebufferObjectFormat::setAttachment(QOpenGLFramebufferObject::Attachment attachment) |
| 304 | { |
| 305 | detach(); |
| 306 | d->attachment = attachment; |
| 307 | } |
| 308 | |
| 309 | /*! |
| 310 | Returns the configuration of the depth and stencil buffers attached to |
| 311 | a framebuffer object. The default is QOpenGLFramebufferObject::NoAttachment. |
| 312 | |
| 313 | \sa setAttachment() |
| 314 | */ |
| 315 | QOpenGLFramebufferObject::Attachment QOpenGLFramebufferObjectFormat::attachment() const |
| 316 | { |
| 317 | return d->attachment; |
| 318 | } |
| 319 | |
| 320 | /*! |
| 321 | Sets the texture target of the texture attached to a framebuffer object to |
| 322 | \a target. Ignored for multisample framebuffer objects. |
| 323 | |
| 324 | \sa textureTarget(), samples() |
| 325 | */ |
| 326 | void QOpenGLFramebufferObjectFormat::setTextureTarget(GLenum target) |
| 327 | { |
| 328 | detach(); |
| 329 | d->target = target; |
| 330 | } |
| 331 | |
| 332 | /*! |
| 333 | Returns the texture target of the texture attached to a framebuffer object. |
| 334 | Ignored for multisample framebuffer objects. The default is |
| 335 | \c GL_TEXTURE_2D. |
| 336 | |
| 337 | \sa setTextureTarget(), samples() |
| 338 | */ |
| 339 | GLenum QOpenGLFramebufferObjectFormat::textureTarget() const |
| 340 | { |
| 341 | return d->target; |
| 342 | } |
| 343 | |
| 344 | /*! |
| 345 | Sets the internal format of a framebuffer object's texture or |
| 346 | multisample framebuffer object's color buffer to |
| 347 | \a internalTextureFormat. |
| 348 | |
| 349 | \sa internalTextureFormat() |
| 350 | */ |
| 351 | void QOpenGLFramebufferObjectFormat::setInternalTextureFormat(GLenum internalTextureFormat) |
| 352 | { |
| 353 | detach(); |
| 354 | d->internal_format = internalTextureFormat; |
| 355 | } |
| 356 | |
| 357 | /*! |
| 358 | Returns the internal format of a framebuffer object's texture or |
| 359 | multisample framebuffer object's color buffer. The default is |
| 360 | \c GL_RGBA8 on desktop OpenGL systems, and \c GL_RGBA on |
| 361 | OpenGL/ES systems. |
| 362 | |
| 363 | \sa setInternalTextureFormat() |
| 364 | */ |
| 365 | GLenum QOpenGLFramebufferObjectFormat::internalTextureFormat() const |
| 366 | { |
| 367 | return d->internal_format; |
| 368 | } |
| 369 | |
| 370 | /*! |
| 371 | Returns \c true if all the options of this framebuffer object format |
| 372 | are the same as \a other; otherwise returns \c false. |
| 373 | */ |
| 374 | bool QOpenGLFramebufferObjectFormat::operator==(const QOpenGLFramebufferObjectFormat& other) const |
| 375 | { |
| 376 | if (d == other.d) |
| 377 | return true; |
| 378 | else |
| 379 | return d->equals(other: other.d); |
| 380 | } |
| 381 | |
| 382 | /*! |
| 383 | Returns \c false if all the options of this framebuffer object format |
| 384 | are the same as \a other; otherwise returns \c true. |
| 385 | */ |
| 386 | bool QOpenGLFramebufferObjectFormat::operator!=(const QOpenGLFramebufferObjectFormat& other) const |
| 387 | { |
| 388 | return !(*this == other); |
| 389 | } |
| 390 | |
| 391 | bool QOpenGLFramebufferObjectPrivate::checkFramebufferStatus(QOpenGLContext *ctx) const |
| 392 | { |
| 393 | if (!ctx) |
| 394 | return false; // Context no longer exists. |
| 395 | GLenum status = ctx->functions()->glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 396 | switch(status) { |
| 397 | case GL_NO_ERROR: |
| 398 | case GL_FRAMEBUFFER_COMPLETE: |
| 399 | return true; |
| 400 | case GL_FRAMEBUFFER_UNSUPPORTED: |
| 401 | qDebug(msg: "QOpenGLFramebufferObject: Unsupported framebuffer format." ); |
| 402 | break; |
| 403 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: |
| 404 | qDebug(msg: "QOpenGLFramebufferObject: Framebuffer incomplete attachment." ); |
| 405 | break; |
| 406 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: |
| 407 | qDebug(msg: "QOpenGLFramebufferObject: Framebuffer incomplete, missing attachment." ); |
| 408 | break; |
| 409 | #ifdef GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT |
| 410 | case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT: |
| 411 | qDebug("QOpenGLFramebufferObject: Framebuffer incomplete, duplicate attachment." ); |
| 412 | break; |
| 413 | #endif |
| 414 | #ifdef GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS |
| 415 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: |
| 416 | qDebug("QOpenGLFramebufferObject: Framebuffer incomplete, attached images must have same dimensions." ); |
| 417 | break; |
| 418 | #endif |
| 419 | #ifdef GL_FRAMEBUFFER_INCOMPLETE_FORMATS |
| 420 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS: |
| 421 | qDebug("QOpenGLFramebufferObject: Framebuffer incomplete, attached images must have same format." ); |
| 422 | break; |
| 423 | #endif |
| 424 | #ifdef GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER |
| 425 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: |
| 426 | qDebug(msg: "QOpenGLFramebufferObject: Framebuffer incomplete, missing draw buffer." ); |
| 427 | break; |
| 428 | #endif |
| 429 | #ifdef GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER |
| 430 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: |
| 431 | qDebug(msg: "QOpenGLFramebufferObject: Framebuffer incomplete, missing read buffer." ); |
| 432 | break; |
| 433 | #endif |
| 434 | #ifdef GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE |
| 435 | case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: |
| 436 | qDebug(msg: "QOpenGLFramebufferObject: Framebuffer incomplete, attachments must have same number of samples per pixel." ); |
| 437 | break; |
| 438 | #endif |
| 439 | default: |
| 440 | qDebug() <<"QOpenGLFramebufferObject: An undefined error has occurred: " << status; |
| 441 | break; |
| 442 | } |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | namespace |
| 447 | { |
| 448 | void freeFramebufferFunc(QOpenGLFunctions *funcs, GLuint id) |
| 449 | { |
| 450 | funcs->glDeleteFramebuffers(n: 1, framebuffers: &id); |
| 451 | } |
| 452 | |
| 453 | void freeRenderbufferFunc(QOpenGLFunctions *funcs, GLuint id) |
| 454 | { |
| 455 | funcs->glDeleteRenderbuffers(n: 1, renderbuffers: &id); |
| 456 | } |
| 457 | |
| 458 | void freeTextureFunc(QOpenGLFunctions *funcs, GLuint id) |
| 459 | { |
| 460 | funcs->glDeleteTextures(n: 1, textures: &id); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | void Q_TRACE_INSTRUMENT(qtopengl) QOpenGLFramebufferObjectPrivate::init( |
| 465 | QOpenGLFramebufferObject *qfbo, const QSize &size, |
| 466 | QOpenGLFramebufferObject::Attachment attachment, |
| 467 | GLenum texture_target, GLenum internal_format, |
| 468 | GLint samples, bool mipmap) |
| 469 | { |
| 470 | Q_TRACE_SCOPE(QOpenGLFramebufferObjectPrivate_init, qfbo, size, attachment, texture_target, internal_format, samples, mipmap); |
| 471 | Q_UNUSED(qfbo); |
| 472 | |
| 473 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 474 | |
| 475 | funcs.initializeOpenGLFunctions(); |
| 476 | |
| 477 | if (!funcs.hasOpenGLFeature(feature: QOpenGLFunctions::Framebuffers)) |
| 478 | return; |
| 479 | |
| 480 | // Fall back to using a normal non-msaa FBO if we don't have support for MSAA |
| 481 | if (!funcs.hasOpenGLExtension(extension: QOpenGLExtensions::FramebufferMultisample) |
| 482 | || !funcs.hasOpenGLExtension(extension: QOpenGLExtensions::FramebufferBlit)) { |
| 483 | samples = 0; |
| 484 | } else if (!ctx->isOpenGLES() || ctx->format().majorVersion() >= 3) { |
| 485 | GLint maxSamples; |
| 486 | funcs.glGetIntegerv(GL_MAX_SAMPLES, params: &maxSamples); |
| 487 | samples = qBound(min: 0, val: int(samples), max: int(maxSamples)); |
| 488 | } |
| 489 | |
| 490 | colorAttachments.append(t: ColorAttachment(size, internal_format)); |
| 491 | |
| 492 | dsSize = size; |
| 493 | |
| 494 | samples = qMax(a: 0, b: samples); |
| 495 | requestedSamples = samples; |
| 496 | |
| 497 | target = texture_target; |
| 498 | |
| 499 | QT_RESET_GLERROR(); // reset error state |
| 500 | GLuint fbo = 0; |
| 501 | |
| 502 | funcs.glGenFramebuffers(n: 1, framebuffers: &fbo); |
| 503 | funcs.glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: fbo); |
| 504 | |
| 505 | QOpenGLContextPrivate::get(context: ctx)->qgl_current_fbo_invalid = true; |
| 506 | |
| 507 | QT_CHECK_GLERROR(); |
| 508 | |
| 509 | format.setTextureTarget(target); |
| 510 | format.setInternalTextureFormat(internal_format); |
| 511 | format.setMipmap(mipmap); |
| 512 | |
| 513 | if (samples == 0) |
| 514 | initTexture(idx: 0); |
| 515 | else |
| 516 | initColorBuffer(idx: 0, samples: &samples); |
| 517 | |
| 518 | format.setSamples(int(samples)); |
| 519 | |
| 520 | initDepthStencilAttachments(ctx, attachment); |
| 521 | |
| 522 | if (valid) |
| 523 | fbo_guard = new QOpenGLSharedResourceGuard(ctx, fbo, freeFramebufferFunc); |
| 524 | else |
| 525 | funcs.glDeleteFramebuffers(n: 1, framebuffers: &fbo); |
| 526 | |
| 527 | QT_CHECK_GLERROR(); |
| 528 | } |
| 529 | |
| 530 | void QOpenGLFramebufferObjectPrivate::initTexture(int idx) |
| 531 | { |
| 532 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 533 | GLuint texture = 0; |
| 534 | |
| 535 | funcs.glGenTextures(n: 1, textures: &texture); |
| 536 | funcs.glBindTexture(target, texture); |
| 537 | |
| 538 | funcs.glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 539 | funcs.glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 540 | funcs.glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 541 | funcs.glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 542 | |
| 543 | ColorAttachment &color(colorAttachments[idx]); |
| 544 | |
| 545 | GLuint pixelType = GL_UNSIGNED_BYTE; |
| 546 | if (color.internalFormat == GL_RGB10_A2 || color.internalFormat == GL_RGB10) |
| 547 | pixelType = GL_UNSIGNED_INT_2_10_10_10_REV; |
| 548 | else if (color.internalFormat == GL_RGB16 || color.internalFormat == GL_RGBA16) |
| 549 | pixelType = GL_UNSIGNED_SHORT; |
| 550 | else if (color.internalFormat == GL_RGB16F || color.internalFormat == GL_RGBA16F) |
| 551 | pixelType = GL_HALF_FLOAT; |
| 552 | |
| 553 | bool isOpaque = false; |
| 554 | switch (color.internalFormat) { |
| 555 | case GL_RGB8: |
| 556 | case GL_RGB16: |
| 557 | case GL_RGB16F: |
| 558 | case GL_RGB32F: |
| 559 | isOpaque = true; |
| 560 | break; |
| 561 | case GL_RGB10: |
| 562 | // opaque but the pixel type (INT_2_10_10_10) has alpha and so requires RGBA texture format |
| 563 | break; |
| 564 | } |
| 565 | const GLuint textureFormat = isOpaque ? GL_RGB : GL_RGBA; |
| 566 | |
| 567 | funcs.glTexImage2D(target, level: 0, internalformat: color.internalFormat, width: color.size.width(), height: color.size.height(), border: 0, |
| 568 | format: textureFormat, type: pixelType, pixels: nullptr); |
| 569 | if (format.mipmap()) { |
| 570 | int width = color.size.width(); |
| 571 | int height = color.size.height(); |
| 572 | int level = 0; |
| 573 | while (width > 1 || height > 1) { |
| 574 | width = qMax(a: 1, b: width >> 1); |
| 575 | height = qMax(a: 1, b: height >> 1); |
| 576 | ++level; |
| 577 | funcs.glTexImage2D(target, level, internalformat: color.internalFormat, width, height, border: 0, format: textureFormat, |
| 578 | type: pixelType, pixels: nullptr); |
| 579 | } |
| 580 | } |
| 581 | funcs.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + idx, |
| 582 | textarget: target, texture, level: 0); |
| 583 | |
| 584 | QT_CHECK_GLERROR(); |
| 585 | funcs.glBindTexture(target, texture: 0); |
| 586 | valid = checkFramebufferStatus(ctx); |
| 587 | if (valid) { |
| 588 | color.guard = new QOpenGLSharedResourceGuard(ctx, texture, freeTextureFunc); |
| 589 | } else { |
| 590 | funcs.glDeleteTextures(n: 1, textures: &texture); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void QOpenGLFramebufferObjectPrivate::initColorBuffer(int idx, GLint *samples) |
| 595 | { |
| 596 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 597 | GLuint color_buffer = 0; |
| 598 | |
| 599 | ColorAttachment &color(colorAttachments[idx]); |
| 600 | |
| 601 | GLenum storageFormat = color.internalFormat; |
| 602 | // ES requires a sized format. The older desktop extension does not. Correct the format on ES. |
| 603 | if (ctx->isOpenGLES()) { |
| 604 | if (color.internalFormat == GL_RGBA) { |
| 605 | if (funcs.hasOpenGLExtension(extension: QOpenGLExtensions::Sized8Formats)) |
| 606 | storageFormat = GL_RGBA8; |
| 607 | else |
| 608 | storageFormat = GL_RGBA4; |
| 609 | } else if (color.internalFormat == GL_RGB10) { |
| 610 | // GL_RGB10 is not allowed in ES for glRenderbufferStorage. |
| 611 | storageFormat = GL_RGB10_A2; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | funcs.glGenRenderbuffers(n: 1, renderbuffers: &color_buffer); |
| 616 | funcs.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: color_buffer); |
| 617 | funcs.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples: *samples, internalformat: storageFormat, width: color.size.width(), height: color.size.height()); |
| 618 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + idx, |
| 619 | GL_RENDERBUFFER, renderbuffer: color_buffer); |
| 620 | |
| 621 | QT_CHECK_GLERROR(); |
| 622 | valid = checkFramebufferStatus(ctx); |
| 623 | if (valid) { |
| 624 | // Query the actual number of samples. This can be greater than the requested |
| 625 | // value since the typically supported values are 0, 4, 8, ..., and the |
| 626 | // requests are mapped to the next supported value. |
| 627 | funcs.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, params: samples); |
| 628 | color.guard = new QOpenGLSharedResourceGuard(ctx, color_buffer, freeRenderbufferFunc); |
| 629 | } else { |
| 630 | funcs.glDeleteRenderbuffers(n: 1, renderbuffers: &color_buffer); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | void QOpenGLFramebufferObjectPrivate::initDepthStencilAttachments(QOpenGLContext *ctx, |
| 635 | QOpenGLFramebufferObject::Attachment attachment) |
| 636 | { |
| 637 | // Use the same sample count for all attachments. format.samples() already contains |
| 638 | // the actual number of samples for the color attachment and is not suitable. Use |
| 639 | // requestedSamples instead. |
| 640 | const int samples = requestedSamples; |
| 641 | |
| 642 | // free existing attachments |
| 643 | if (depth_buffer_guard) { |
| 644 | #ifdef Q_OS_WASM |
| 645 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0); |
| 646 | #else |
| 647 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer: 0); |
| 648 | #endif |
| 649 | depth_buffer_guard->free(); |
| 650 | } |
| 651 | if (stencil_buffer_guard) { |
| 652 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer: 0); |
| 653 | if (stencil_buffer_guard != depth_buffer_guard) |
| 654 | stencil_buffer_guard->free(); |
| 655 | } |
| 656 | |
| 657 | depth_buffer_guard = nullptr; |
| 658 | stencil_buffer_guard = nullptr; |
| 659 | |
| 660 | GLuint depth_buffer = 0; |
| 661 | GLuint stencil_buffer = 0; |
| 662 | |
| 663 | // In practice, a combined depth-stencil buffer is supported by all desktop platforms, while a |
| 664 | // separate stencil buffer is not. On embedded devices however, a combined depth-stencil buffer |
| 665 | // might not be supported while separate buffers are, according to QTBUG-12861. |
| 666 | #ifdef Q_OS_WASM |
| 667 | // WebGL doesn't allow separately attach buffers to |
| 668 | // STENCIL_ATTACHMENT and DEPTH_ATTACHMENT |
| 669 | // QTBUG-69913 |
| 670 | if (attachment == QOpenGLFramebufferObject::CombinedDepthStencil) { |
| 671 | funcs.glGenRenderbuffers(1, &depth_buffer); |
| 672 | funcs.glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer); |
| 673 | Q_ASSERT(funcs.glIsRenderbuffer(depth_buffer)); |
| 674 | |
| 675 | if (samples != 0 ) { |
| 676 | funcs.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, |
| 677 | GL_DEPTH24_STENCIL8, dsSize.width(), dsSize.height()); |
| 678 | } else { |
| 679 | funcs.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, |
| 680 | dsSize.width(), dsSize.height()); |
| 681 | } |
| 682 | |
| 683 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, |
| 684 | GL_RENDERBUFFER, depth_buffer); |
| 685 | |
| 686 | valid = checkFramebufferStatus(ctx); |
| 687 | if (!valid) { |
| 688 | funcs.glDeleteRenderbuffers(1, &depth_buffer); |
| 689 | depth_buffer = 0; |
| 690 | } |
| 691 | } |
| 692 | #else |
| 693 | if (attachment == QOpenGLFramebufferObject::CombinedDepthStencil |
| 694 | && funcs.hasOpenGLExtension(extension: QOpenGLExtensions::PackedDepthStencil)) |
| 695 | { |
| 696 | // depth and stencil buffer needs another extension |
| 697 | funcs.glGenRenderbuffers(n: 1, renderbuffers: &depth_buffer); |
| 698 | funcs.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: depth_buffer); |
| 699 | Q_ASSERT(funcs.glIsRenderbuffer(depth_buffer)); |
| 700 | if (samples != 0 && funcs.hasOpenGLExtension(extension: QOpenGLExtensions::FramebufferMultisample)) |
| 701 | funcs.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, |
| 702 | GL_DEPTH24_STENCIL8, width: dsSize.width(), height: dsSize.height()); |
| 703 | else |
| 704 | funcs.glRenderbufferStorage(GL_RENDERBUFFER, |
| 705 | GL_DEPTH24_STENCIL8, width: dsSize.width(), height: dsSize.height()); |
| 706 | |
| 707 | stencil_buffer = depth_buffer; |
| 708 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, |
| 709 | GL_RENDERBUFFER, renderbuffer: depth_buffer); |
| 710 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, |
| 711 | GL_RENDERBUFFER, renderbuffer: stencil_buffer); |
| 712 | |
| 713 | valid = checkFramebufferStatus(ctx); |
| 714 | if (!valid) { |
| 715 | funcs.glDeleteRenderbuffers(n: 1, renderbuffers: &depth_buffer); |
| 716 | stencil_buffer = depth_buffer = 0; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (depth_buffer == 0 && (attachment == QOpenGLFramebufferObject::CombinedDepthStencil |
| 721 | || (attachment == QOpenGLFramebufferObject::Depth))) |
| 722 | { |
| 723 | funcs.glGenRenderbuffers(n: 1, renderbuffers: &depth_buffer); |
| 724 | funcs.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: depth_buffer); |
| 725 | Q_ASSERT(funcs.glIsRenderbuffer(depth_buffer)); |
| 726 | if (samples != 0 && funcs.hasOpenGLExtension(extension: QOpenGLExtensions::FramebufferMultisample)) { |
| 727 | if (ctx->isOpenGLES()) { |
| 728 | if (funcs.hasOpenGLExtension(extension: QOpenGLExtensions::Depth24)) |
| 729 | funcs.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, |
| 730 | GL_DEPTH_COMPONENT24, width: dsSize.width(), height: dsSize.height()); |
| 731 | else |
| 732 | funcs.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, |
| 733 | GL_DEPTH_COMPONENT16, width: dsSize.width(), height: dsSize.height()); |
| 734 | } else { |
| 735 | funcs.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, |
| 736 | GL_DEPTH_COMPONENT, width: dsSize.width(), height: dsSize.height()); |
| 737 | } |
| 738 | } else { |
| 739 | if (ctx->isOpenGLES()) { |
| 740 | if (funcs.hasOpenGLExtension(extension: QOpenGLExtensions::Depth24)) { |
| 741 | funcs.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, |
| 742 | width: dsSize.width(), height: dsSize.height()); |
| 743 | } else { |
| 744 | funcs.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, |
| 745 | width: dsSize.width(), height: dsSize.height()); |
| 746 | } |
| 747 | } else { |
| 748 | funcs.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width: dsSize.width(), height: dsSize.height()); |
| 749 | } |
| 750 | } |
| 751 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, |
| 752 | GL_RENDERBUFFER, renderbuffer: depth_buffer); |
| 753 | valid = checkFramebufferStatus(ctx); |
| 754 | if (!valid) { |
| 755 | funcs.glDeleteRenderbuffers(n: 1, renderbuffers: &depth_buffer); |
| 756 | depth_buffer = 0; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (stencil_buffer == 0 && (attachment == QOpenGLFramebufferObject::CombinedDepthStencil)) { |
| 761 | funcs.glGenRenderbuffers(n: 1, renderbuffers: &stencil_buffer); |
| 762 | funcs.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: stencil_buffer); |
| 763 | Q_ASSERT(funcs.glIsRenderbuffer(stencil_buffer)); |
| 764 | |
| 765 | #if QT_CONFIG(opengles2) |
| 766 | GLenum storage = GL_STENCIL_INDEX8; |
| 767 | #else |
| 768 | GLenum storage = ctx->isOpenGLES() ? GL_STENCIL_INDEX8 : GL_STENCIL_INDEX; |
| 769 | #endif |
| 770 | |
| 771 | if (samples != 0 && funcs.hasOpenGLExtension(extension: QOpenGLExtensions::FramebufferMultisample)) |
| 772 | funcs.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, internalformat: storage, width: dsSize.width(), height: dsSize.height()); |
| 773 | else |
| 774 | funcs.glRenderbufferStorage(GL_RENDERBUFFER, internalformat: storage, width: dsSize.width(), height: dsSize.height()); |
| 775 | |
| 776 | funcs.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, |
| 777 | GL_RENDERBUFFER, renderbuffer: stencil_buffer); |
| 778 | valid = checkFramebufferStatus(ctx); |
| 779 | if (!valid) { |
| 780 | funcs.glDeleteRenderbuffers(n: 1, renderbuffers: &stencil_buffer); |
| 781 | stencil_buffer = 0; |
| 782 | } |
| 783 | } |
| 784 | #endif //Q_OS_WASM |
| 785 | |
| 786 | // The FBO might have become valid after removing the depth or stencil buffer. |
| 787 | valid = checkFramebufferStatus(ctx); |
| 788 | |
| 789 | #ifdef Q_OS_WASM |
| 790 | if (depth_buffer) { |
| 791 | #else |
| 792 | if (depth_buffer && stencil_buffer) { |
| 793 | #endif |
| 794 | fbo_attachment = QOpenGLFramebufferObject::CombinedDepthStencil; |
| 795 | } else if (depth_buffer) { |
| 796 | fbo_attachment = QOpenGLFramebufferObject::Depth; |
| 797 | } else { |
| 798 | fbo_attachment = QOpenGLFramebufferObject::NoAttachment; |
| 799 | } |
| 800 | |
| 801 | if (valid) { |
| 802 | if (depth_buffer) |
| 803 | depth_buffer_guard = new QOpenGLSharedResourceGuard(ctx, depth_buffer, freeRenderbufferFunc); |
| 804 | if (stencil_buffer) { |
| 805 | if (stencil_buffer == depth_buffer) |
| 806 | stencil_buffer_guard = depth_buffer_guard; |
| 807 | else |
| 808 | stencil_buffer_guard = new QOpenGLSharedResourceGuard(ctx, stencil_buffer, freeRenderbufferFunc); |
| 809 | } |
| 810 | } else { |
| 811 | if (depth_buffer) |
| 812 | funcs.glDeleteRenderbuffers(n: 1, renderbuffers: &depth_buffer); |
| 813 | if (stencil_buffer && depth_buffer != stencil_buffer) |
| 814 | funcs.glDeleteRenderbuffers(n: 1, renderbuffers: &stencil_buffer); |
| 815 | } |
| 816 | QT_CHECK_GLERROR(); |
| 817 | |
| 818 | format.setAttachment(fbo_attachment); |
| 819 | } |
| 820 | |
| 821 | /*! |
| 822 | \class QOpenGLFramebufferObject |
| 823 | \brief The QOpenGLFramebufferObject class encapsulates an OpenGL framebuffer object. |
| 824 | \since 5.0 |
| 825 | \inmodule QtOpenGL |
| 826 | |
| 827 | \ingroup painting-3D |
| 828 | |
| 829 | The QOpenGLFramebufferObject class encapsulates an OpenGL framebuffer |
| 830 | object, defined by the \c{GL_EXT_framebuffer_object} extension. It provides |
| 831 | a rendering surface that can be painted on with a QPainter with the help of |
| 832 | QOpenGLPaintDevice, or rendered to using native OpenGL calls. This surface |
| 833 | can be bound and used as a regular texture in your own OpenGL drawing code. |
| 834 | By default, the QOpenGLFramebufferObject class generates a 2D OpenGL |
| 835 | texture (using the \c{GL_TEXTURE_2D} target), which is used as the internal |
| 836 | rendering target. |
| 837 | |
| 838 | \b{It is important to have a current OpenGL context when creating a |
| 839 | QOpenGLFramebufferObject, otherwise initialization will fail.} |
| 840 | |
| 841 | Create the QOpenGLFrameBufferObject instance with the CombinedDepthStencil |
| 842 | attachment if you want QPainter to render correctly. Note that you need to |
| 843 | create a QOpenGLFramebufferObject with more than one sample per pixel for |
| 844 | primitives to be antialiased when drawing using a QPainter. To create a |
| 845 | multisample framebuffer object you should use one of the constructors that |
| 846 | take a QOpenGLFramebufferObjectFormat parameter, and set the |
| 847 | QOpenGLFramebufferObjectFormat::samples() property to a non-zero value. |
| 848 | |
| 849 | For multisample framebuffer objects a color render buffer is created, |
| 850 | otherwise a texture with the specified texture target is created. |
| 851 | The color render buffer or texture will have the specified internal |
| 852 | format, and will be bound to the \c GL_COLOR_ATTACHMENT0 |
| 853 | attachment in the framebuffer object. |
| 854 | |
| 855 | Multiple render targets are also supported, in case the OpenGL |
| 856 | implementation supports this. Here there will be multiple textures (or, in |
| 857 | case of multisampling, renderbuffers) present and each of them will get |
| 858 | attached to \c GL_COLOR_ATTACHMENT0, \c 1, \c 2, ... |
| 859 | |
| 860 | If you want to use a framebuffer object with multisampling enabled |
| 861 | as a texture, you first need to copy from it to a regular framebuffer |
| 862 | object using QOpenGLContext::blitFramebuffer(). |
| 863 | |
| 864 | It is possible to draw into a QOpenGLFramebufferObject using QPainter and |
| 865 | QOpenGLPaintDevice in a separate thread. |
| 866 | */ |
| 867 | |
| 868 | |
| 869 | /*! |
| 870 | \enum QOpenGLFramebufferObject::Attachment |
| 871 | |
| 872 | This enum type is used to configure the depth and stencil buffers |
| 873 | attached to the framebuffer object when it is created. |
| 874 | |
| 875 | \value NoAttachment No attachment is added to the framebuffer object. Note that the |
| 876 | OpenGL depth and stencil tests won't work when rendering to a |
| 877 | framebuffer object without any depth or stencil buffers. |
| 878 | This is the default value. |
| 879 | |
| 880 | \value CombinedDepthStencil If the \c GL_EXT_packed_depth_stencil extension is present, |
| 881 | a combined depth and stencil buffer is attached. |
| 882 | If the extension is not present, only a depth buffer is attached. |
| 883 | |
| 884 | \value Depth A depth buffer is attached to the framebuffer object. |
| 885 | |
| 886 | \sa attachment() |
| 887 | */ |
| 888 | |
| 889 | static inline GLenum effectiveInternalFormat(GLenum internalFormat) |
| 890 | { |
| 891 | if (!internalFormat) |
| 892 | #if QT_CONFIG(opengles2) |
| 893 | internalFormat = GL_RGBA; |
| 894 | #else |
| 895 | internalFormat = QOpenGLContext::currentContext()->isOpenGLES() ? GL_RGBA : GL_RGBA8; |
| 896 | #endif |
| 897 | return internalFormat; |
| 898 | } |
| 899 | |
| 900 | /*! |
| 901 | |
| 902 | Constructs an OpenGL framebuffer object and binds a 2D OpenGL texture |
| 903 | to the buffer of the size \a size. The texture is bound to the |
| 904 | \c GL_COLOR_ATTACHMENT0 target in the framebuffer object. |
| 905 | |
| 906 | The \a target parameter is used to specify the OpenGL texture |
| 907 | target. The default target is \c GL_TEXTURE_2D. Keep in mind that |
| 908 | \c GL_TEXTURE_2D textures must have a power of 2 width and height |
| 909 | (e.g. 256x512), unless you are using OpenGL 2.0 or higher. |
| 910 | |
| 911 | By default, no depth and stencil buffers are attached. This behavior |
| 912 | can be toggled using one of the overloaded constructors. |
| 913 | |
| 914 | The default internal texture format is \c GL_RGBA8 for desktop |
| 915 | OpenGL, and \c GL_RGBA for OpenGL/ES. |
| 916 | |
| 917 | It is important that you have a current OpenGL context set when |
| 918 | creating the QOpenGLFramebufferObject, otherwise the initialization |
| 919 | will fail. |
| 920 | |
| 921 | \sa size(), texture(), attachment() |
| 922 | */ |
| 923 | |
| 924 | QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, GLenum target) |
| 925 | : d_ptr(new QOpenGLFramebufferObjectPrivate) |
| 926 | { |
| 927 | Q_D(QOpenGLFramebufferObject); |
| 928 | d->init(qfbo: this, size, attachment: NoAttachment, texture_target: target, internal_format: effectiveInternalFormat(internalFormat: 0)); |
| 929 | } |
| 930 | |
| 931 | /*! |
| 932 | |
| 933 | Constructs an OpenGL framebuffer object and binds a 2D OpenGL texture |
| 934 | to the buffer of the given \a width and \a height. |
| 935 | |
| 936 | \sa size(), texture() |
| 937 | */ |
| 938 | QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, GLenum target) |
| 939 | : QOpenGLFramebufferObject(QSize(width, height), target) |
| 940 | { |
| 941 | } |
| 942 | |
| 943 | /*! |
| 944 | |
| 945 | Constructs an OpenGL framebuffer object of the given \a size based on the |
| 946 | supplied \a format. |
| 947 | */ |
| 948 | |
| 949 | QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, const QOpenGLFramebufferObjectFormat &format) |
| 950 | : d_ptr(new QOpenGLFramebufferObjectPrivate) |
| 951 | { |
| 952 | Q_D(QOpenGLFramebufferObject); |
| 953 | d->init(qfbo: this, size, attachment: format.attachment(), texture_target: format.textureTarget(), internal_format: format.internalTextureFormat(), |
| 954 | samples: format.samples(), mipmap: format.mipmap()); |
| 955 | } |
| 956 | |
| 957 | /*! |
| 958 | |
| 959 | Constructs an OpenGL framebuffer object of the given \a width and \a height |
| 960 | based on the supplied \a format. |
| 961 | */ |
| 962 | |
| 963 | QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, const QOpenGLFramebufferObjectFormat &format) |
| 964 | : QOpenGLFramebufferObject(QSize(width, height), format) |
| 965 | { |
| 966 | } |
| 967 | |
| 968 | /*! |
| 969 | |
| 970 | Constructs an OpenGL framebuffer object and binds a texture to the |
| 971 | buffer of the given \a width and \a height. |
| 972 | |
| 973 | The \a attachment parameter describes the depth/stencil buffer |
| 974 | configuration, \a target the texture target and \a internalFormat |
| 975 | the internal texture format. The default texture target is \c |
| 976 | GL_TEXTURE_2D, while the default internal format is \c GL_RGBA8 |
| 977 | for desktop OpenGL and \c GL_RGBA for OpenGL/ES. |
| 978 | |
| 979 | \sa size(), texture(), attachment() |
| 980 | */ |
| 981 | QOpenGLFramebufferObject::QOpenGLFramebufferObject(int width, int height, Attachment attachment, |
| 982 | GLenum target, GLenum internalFormat) |
| 983 | : d_ptr(new QOpenGLFramebufferObjectPrivate) |
| 984 | { |
| 985 | Q_D(QOpenGLFramebufferObject); |
| 986 | d->init(qfbo: this, size: QSize(width, height), attachment, texture_target: target, internal_format: effectiveInternalFormat(internalFormat)); |
| 987 | } |
| 988 | |
| 989 | /*! |
| 990 | |
| 991 | Constructs an OpenGL framebuffer object and binds a texture to the |
| 992 | buffer of the given \a size. |
| 993 | |
| 994 | The \a attachment parameter describes the depth/stencil buffer |
| 995 | configuration, \a target the texture target and \a internalFormat |
| 996 | the internal texture format. The default texture target is \c |
| 997 | GL_TEXTURE_2D, while the default internal format is \c GL_RGBA8 |
| 998 | for desktop OpenGL and \c GL_RGBA for OpenGL/ES. |
| 999 | |
| 1000 | \sa size(), texture(), attachment() |
| 1001 | */ |
| 1002 | QOpenGLFramebufferObject::QOpenGLFramebufferObject(const QSize &size, Attachment attachment, |
| 1003 | GLenum target, GLenum internalFormat) |
| 1004 | : d_ptr(new QOpenGLFramebufferObjectPrivate) |
| 1005 | { |
| 1006 | Q_D(QOpenGLFramebufferObject); |
| 1007 | d->init(qfbo: this, size, attachment, texture_target: target, internal_format: effectiveInternalFormat(internalFormat)); |
| 1008 | } |
| 1009 | |
| 1010 | /*! |
| 1011 | |
| 1012 | Destroys the framebuffer object and frees any allocated resources. |
| 1013 | */ |
| 1014 | QOpenGLFramebufferObject::~QOpenGLFramebufferObject() |
| 1015 | { |
| 1016 | Q_D(QOpenGLFramebufferObject); |
| 1017 | if (isBound()) |
| 1018 | release(); |
| 1019 | |
| 1020 | for (const auto &color : std::as_const(t&: d->colorAttachments)) { |
| 1021 | if (color.guard) |
| 1022 | color.guard->free(); |
| 1023 | } |
| 1024 | d->colorAttachments.clear(); |
| 1025 | |
| 1026 | if (d->depth_buffer_guard) |
| 1027 | d->depth_buffer_guard->free(); |
| 1028 | if (d->stencil_buffer_guard && d->stencil_buffer_guard != d->depth_buffer_guard) |
| 1029 | d->stencil_buffer_guard->free(); |
| 1030 | if (d->fbo_guard) |
| 1031 | d->fbo_guard->free(); |
| 1032 | |
| 1033 | QOpenGLContextPrivate *contextPrv = QOpenGLContextPrivate::get(context: QOpenGLContext::currentContext()); |
| 1034 | if (contextPrv && contextPrv->qgl_current_fbo == this) { |
| 1035 | contextPrv->qgl_current_fbo_invalid = true; |
| 1036 | contextPrv->qgl_current_fbo = nullptr; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /*! |
| 1041 | Creates and attaches an additional texture or renderbuffer of \a size width |
| 1042 | and height. |
| 1043 | |
| 1044 | There is always an attachment at GL_COLOR_ATTACHMENT0. Call this function |
| 1045 | to set up additional attachments at GL_COLOR_ATTACHMENT1, |
| 1046 | GL_COLOR_ATTACHMENT2, ... |
| 1047 | |
| 1048 | When \a internalFormat is not \c 0, it specifies the internal format of the |
| 1049 | texture or renderbuffer. Otherwise a default of GL_RGBA or GL_RGBA8 is |
| 1050 | used. |
| 1051 | |
| 1052 | \note This is only functional when multiple render targets are supported by |
| 1053 | the OpenGL implementation. When that is not the case, the function will not |
| 1054 | add any additional color attachments. Call |
| 1055 | QOpenGLFunctions::hasOpenGLFeature() with |
| 1056 | QOpenGLFunctions::MultipleRenderTargets at runtime to check if MRT is |
| 1057 | supported. |
| 1058 | |
| 1059 | \note The internal format of the color attachments may differ but there may |
| 1060 | be limitations on the supported combinations, depending on the drivers. |
| 1061 | |
| 1062 | \note The size of the color attachments may differ but rendering is limited |
| 1063 | to the area that fits all the attachments, according to the OpenGL |
| 1064 | specification. Some drivers may not be fully conformant in this respect, |
| 1065 | however. |
| 1066 | |
| 1067 | \since 5.6 |
| 1068 | */ |
| 1069 | void QOpenGLFramebufferObject::addColorAttachment(const QSize &size, GLenum internalFormat) |
| 1070 | { |
| 1071 | Q_D(QOpenGLFramebufferObject); |
| 1072 | |
| 1073 | if (!QOpenGLContext::currentContext()->functions()->hasOpenGLFeature(feature: QOpenGLFunctions::MultipleRenderTargets)) { |
| 1074 | qWarning(msg: "Multiple render targets not supported, ignoring extra color attachment request" ); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | QOpenGLFramebufferObjectPrivate::ColorAttachment color(size, effectiveInternalFormat(internalFormat)); |
| 1079 | d->colorAttachments.append(t: color); |
| 1080 | const int idx = d->colorAttachments.size() - 1; |
| 1081 | |
| 1082 | if (d->requestedSamples == 0) { |
| 1083 | d->initTexture(idx); |
| 1084 | } else { |
| 1085 | GLint samples = d->requestedSamples; |
| 1086 | d->initColorBuffer(idx, samples: &samples); |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /*! \overload |
| 1091 | |
| 1092 | Creates and attaches an additional texture or renderbuffer of size \a width and \a height. |
| 1093 | |
| 1094 | When \a internalFormat is not \c 0, it specifies the internal format of the texture or |
| 1095 | renderbuffer. Otherwise a default of GL_RGBA or GL_RGBA8 is used. |
| 1096 | |
| 1097 | \since 5.6 |
| 1098 | */ |
| 1099 | void QOpenGLFramebufferObject::addColorAttachment(int width, int height, GLenum internalFormat) |
| 1100 | { |
| 1101 | addColorAttachment(size: QSize(width, height), internalFormat); |
| 1102 | } |
| 1103 | |
| 1104 | /*! |
| 1105 | \fn bool QOpenGLFramebufferObject::isValid() const |
| 1106 | |
| 1107 | Returns \c true if the framebuffer object is valid. |
| 1108 | |
| 1109 | The framebuffer can become invalid if the initialization process |
| 1110 | fails, the user attaches an invalid buffer to the framebuffer |
| 1111 | object, or a non-power of two width/height is specified as the |
| 1112 | texture size if the texture target is \c{GL_TEXTURE_2D}. |
| 1113 | The non-power of two limitation does not apply if the OpenGL version |
| 1114 | is 2.0 or higher, or if the GL_ARB_texture_non_power_of_two extension |
| 1115 | is present. |
| 1116 | |
| 1117 | The framebuffer can also become invalid if the QOpenGLContext that |
| 1118 | the framebuffer was created within is destroyed and there are |
| 1119 | no other shared contexts that can take over ownership of the |
| 1120 | framebuffer. |
| 1121 | */ |
| 1122 | bool QOpenGLFramebufferObject::isValid() const |
| 1123 | { |
| 1124 | Q_D(const QOpenGLFramebufferObject); |
| 1125 | return d->valid && d->fbo_guard && d->fbo_guard->id(); |
| 1126 | } |
| 1127 | |
| 1128 | /*! |
| 1129 | \fn bool QOpenGLFramebufferObject::bind() |
| 1130 | |
| 1131 | Switches rendering from the default, windowing system provided |
| 1132 | framebuffer to this framebuffer object. |
| 1133 | Returns \c true upon success, false otherwise. |
| 1134 | |
| 1135 | \note If takeTexture() was called, a new texture is created and associated |
| 1136 | with the framebuffer object. This is potentially expensive and changes the |
| 1137 | context state (the currently bound texture). |
| 1138 | |
| 1139 | \sa release() |
| 1140 | */ |
| 1141 | bool QOpenGLFramebufferObject::bind() |
| 1142 | { |
| 1143 | if (!isValid()) |
| 1144 | return false; |
| 1145 | Q_D(QOpenGLFramebufferObject); |
| 1146 | QOpenGLContext *current = QOpenGLContext::currentContext(); |
| 1147 | if (!current) |
| 1148 | return false; |
| 1149 | #ifdef QT_DEBUG |
| 1150 | if (current->shareGroup() != d->fbo_guard->group()) |
| 1151 | qWarning(msg: "QOpenGLFramebufferObject::bind() called from incompatible context" ); |
| 1152 | #endif |
| 1153 | |
| 1154 | d->funcs.glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: d->fbo()); |
| 1155 | |
| 1156 | QOpenGLContextPrivate::get(context: current)->qgl_current_fbo_invalid = true; |
| 1157 | QOpenGLContextPrivate::get(context: current)->qgl_current_fbo = this; |
| 1158 | |
| 1159 | if (d->format.samples() == 0) { |
| 1160 | // Create new textures to replace the ones stolen via takeTexture(). |
| 1161 | for (int i = 0; i < d->colorAttachments.size(); ++i) { |
| 1162 | if (!d->colorAttachments.at(idx: i).guard) |
| 1163 | d->initTexture(idx: i); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return d->valid; |
| 1168 | } |
| 1169 | |
| 1170 | /*! |
| 1171 | \fn bool QOpenGLFramebufferObject::release() |
| 1172 | |
| 1173 | Switches rendering back to the default, windowing system provided |
| 1174 | framebuffer. |
| 1175 | Returns \c true upon success, false otherwise. |
| 1176 | |
| 1177 | \sa bind() |
| 1178 | */ |
| 1179 | bool QOpenGLFramebufferObject::release() |
| 1180 | { |
| 1181 | if (!isValid()) |
| 1182 | return false; |
| 1183 | |
| 1184 | QOpenGLContext *current = QOpenGLContext::currentContext(); |
| 1185 | if (!current) |
| 1186 | return false; |
| 1187 | |
| 1188 | Q_D(QOpenGLFramebufferObject); |
| 1189 | #ifdef QT_DEBUG |
| 1190 | if (current->shareGroup() != d->fbo_guard->group()) |
| 1191 | qWarning(msg: "QOpenGLFramebufferObject::release() called from incompatible context" ); |
| 1192 | #endif |
| 1193 | |
| 1194 | if (current) { |
| 1195 | d->funcs.glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: current->defaultFramebufferObject()); |
| 1196 | |
| 1197 | QOpenGLContextPrivate *contextPrv = QOpenGLContextPrivate::get(context: current); |
| 1198 | contextPrv->qgl_current_fbo_invalid = true; |
| 1199 | contextPrv->qgl_current_fbo = nullptr; |
| 1200 | } |
| 1201 | |
| 1202 | return true; |
| 1203 | } |
| 1204 | |
| 1205 | /*! |
| 1206 | \fn GLuint QOpenGLFramebufferObject::texture() const |
| 1207 | |
| 1208 | Returns the texture id for the texture attached as the default |
| 1209 | rendering target in this framebuffer object. This texture id can |
| 1210 | be bound as a normal texture in your own OpenGL code. |
| 1211 | |
| 1212 | If a multisample framebuffer object is used then the value returned |
| 1213 | from this function will be invalid. |
| 1214 | |
| 1215 | When multiple textures are attached, the return value is the ID of |
| 1216 | the first one. |
| 1217 | |
| 1218 | \sa takeTexture(), textures() |
| 1219 | */ |
| 1220 | GLuint QOpenGLFramebufferObject::texture() const |
| 1221 | { |
| 1222 | Q_D(const QOpenGLFramebufferObject); |
| 1223 | return d->colorAttachments[0].guard ? d->colorAttachments[0].guard->id() : 0; |
| 1224 | } |
| 1225 | |
| 1226 | /*! |
| 1227 | Returns the texture id for all attached textures. |
| 1228 | |
| 1229 | If a multisample framebuffer object is used, then an empty vector is returned. |
| 1230 | |
| 1231 | \since 5.6 |
| 1232 | |
| 1233 | \sa takeTexture(), texture() |
| 1234 | */ |
| 1235 | QList<GLuint> QOpenGLFramebufferObject::textures() const |
| 1236 | { |
| 1237 | Q_D(const QOpenGLFramebufferObject); |
| 1238 | QList<GLuint> ids; |
| 1239 | if (d->format.samples() != 0) |
| 1240 | return ids; |
| 1241 | ids.reserve(asize: d->colorAttachments.size()); |
| 1242 | for (const auto &color : d->colorAttachments) |
| 1243 | ids.append(t: color.guard ? color.guard->id() : 0); |
| 1244 | return ids; |
| 1245 | } |
| 1246 | |
| 1247 | /*! |
| 1248 | \fn GLuint QOpenGLFramebufferObject::takeTexture() |
| 1249 | |
| 1250 | Returns the texture id for the texture attached to this framebuffer |
| 1251 | object. The ownership of the texture is transferred to the caller. |
| 1252 | |
| 1253 | If the framebuffer object is currently bound, an implicit release() |
| 1254 | will be done. During the next call to bind() a new texture will be |
| 1255 | created. |
| 1256 | |
| 1257 | If a multisample framebuffer object is used, then there is no |
| 1258 | texture and the return value from this function will be invalid. |
| 1259 | Similarly, incomplete framebuffer objects will also return 0. |
| 1260 | |
| 1261 | \since 5.3 |
| 1262 | |
| 1263 | \sa texture(), bind(), release() |
| 1264 | */ |
| 1265 | GLuint QOpenGLFramebufferObject::takeTexture() |
| 1266 | { |
| 1267 | return takeTexture(colorAttachmentIndex: 0); |
| 1268 | } |
| 1269 | |
| 1270 | /*! \overload |
| 1271 | |
| 1272 | Returns the texture id for the texture attached to the color attachment of |
| 1273 | index \a colorAttachmentIndex of this framebuffer object. The ownership of |
| 1274 | the texture is transferred to the caller. |
| 1275 | |
| 1276 | When \a colorAttachmentIndex is \c 0, the behavior is identical to the |
| 1277 | parameter-less variant of this function. |
| 1278 | |
| 1279 | If the framebuffer object is currently bound, an implicit release() |
| 1280 | will be done. During the next call to bind() a new texture will be |
| 1281 | created. |
| 1282 | |
| 1283 | If a multisample framebuffer object is used, then there is no |
| 1284 | texture and the return value from this function will be invalid. |
| 1285 | Similarly, incomplete framebuffer objects will also return 0. |
| 1286 | |
| 1287 | \since 5.6 |
| 1288 | */ |
| 1289 | GLuint QOpenGLFramebufferObject::takeTexture(int colorAttachmentIndex) |
| 1290 | { |
| 1291 | Q_D(QOpenGLFramebufferObject); |
| 1292 | GLuint id = 0; |
| 1293 | if (isValid() && d->format.samples() == 0 && d->colorAttachments.size() > colorAttachmentIndex) { |
| 1294 | QOpenGLContext *current = QOpenGLContext::currentContext(); |
| 1295 | if (current && current->shareGroup() == d->fbo_guard->group() && isBound()) |
| 1296 | release(); |
| 1297 | auto &guard = d->colorAttachments[colorAttachmentIndex].guard; |
| 1298 | id = guard ? guard->id() : 0; |
| 1299 | // Do not call free() on texture_guard, just null it out. |
| 1300 | // This way the texture will not be deleted when the guard is destroyed. |
| 1301 | guard = nullptr; |
| 1302 | } |
| 1303 | return id; |
| 1304 | } |
| 1305 | |
| 1306 | /*! |
| 1307 | \return the size of the color and depth/stencil attachments attached to |
| 1308 | this framebuffer object. |
| 1309 | */ |
| 1310 | QSize QOpenGLFramebufferObject::size() const |
| 1311 | { |
| 1312 | Q_D(const QOpenGLFramebufferObject); |
| 1313 | return d->dsSize; |
| 1314 | } |
| 1315 | |
| 1316 | /*! |
| 1317 | \return the sizes of all color attachments attached to this framebuffer |
| 1318 | object. |
| 1319 | |
| 1320 | \since 5.6 |
| 1321 | */ |
| 1322 | QList<QSize> QOpenGLFramebufferObject::sizes() const |
| 1323 | { |
| 1324 | Q_D(const QOpenGLFramebufferObject); |
| 1325 | QList<QSize> sz; |
| 1326 | sz.reserve(asize: d->colorAttachments.size()); |
| 1327 | for (const auto &color : d->colorAttachments) |
| 1328 | sz.append(t: color.size); |
| 1329 | return sz; |
| 1330 | } |
| 1331 | |
| 1332 | /*! |
| 1333 | \fn int QOpenGLFramebufferObject::width() const |
| 1334 | |
| 1335 | Returns the width of the framebuffer object attachments. |
| 1336 | */ |
| 1337 | |
| 1338 | /*! |
| 1339 | \fn int QOpenGLFramebufferObject::height() const |
| 1340 | |
| 1341 | Returns the height of the framebuffer object attachments. |
| 1342 | */ |
| 1343 | |
| 1344 | /*! |
| 1345 | Returns the format of this framebuffer object. |
| 1346 | */ |
| 1347 | QOpenGLFramebufferObjectFormat QOpenGLFramebufferObject::format() const |
| 1348 | { |
| 1349 | Q_D(const QOpenGLFramebufferObject); |
| 1350 | return d->format; |
| 1351 | } |
| 1352 | |
| 1353 | static inline QImage qt_gl_read_framebuffer_rgba8(const QSize &size, bool include_alpha, QOpenGLContext *context) |
| 1354 | { |
| 1355 | QOpenGLFunctions *funcs = context->functions(); |
| 1356 | const int w = size.width(); |
| 1357 | const int h = size.height(); |
| 1358 | bool isOpenGL12orBetter = !context->isOpenGLES() && (context->format().majorVersion() >= 2 || context->format().minorVersion() >= 2); |
| 1359 | if (isOpenGL12orBetter) { |
| 1360 | QImage img(size, include_alpha ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32); |
| 1361 | if (!img.isNull()) |
| 1362 | funcs->glReadPixels(x: 0, y: 0, width: w, height: h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels: img.bits()); |
| 1363 | return img; |
| 1364 | } |
| 1365 | |
| 1366 | // For OpenGL ES stick with the byte ordered format / RGBA readback format |
| 1367 | // since that is the only spec mandated way. (also, skip the |
| 1368 | // GL_IMPLEMENTATION_COLOR_READ_FORMAT mess since there is nothing saying a |
| 1369 | // BGRA capable impl would return BGRA from there) |
| 1370 | |
| 1371 | QImage rgbaImage(size, include_alpha ? QImage::Format_RGBA8888_Premultiplied : QImage::Format_RGBX8888); |
| 1372 | if (!rgbaImage.isNull()) |
| 1373 | funcs->glReadPixels(x: 0, y: 0, width: w, height: h, GL_RGBA, GL_UNSIGNED_BYTE, pixels: rgbaImage.bits()); |
| 1374 | return rgbaImage; |
| 1375 | } |
| 1376 | |
| 1377 | static inline QImage qt_gl_read_framebuffer_rgb10a2(const QSize &size, bool include_alpha, QOpenGLContext *context) |
| 1378 | { |
| 1379 | // We assume OpenGL 1.2+ or ES 3.0+ here. |
| 1380 | QImage img(size, include_alpha ? QImage::Format_A2BGR30_Premultiplied : QImage::Format_BGR30); |
| 1381 | if (!img.isNull()) |
| 1382 | context->functions()->glReadPixels(x: 0, y: 0, width: size.width(), height: size.height(), GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, pixels: img.bits()); |
| 1383 | return img; |
| 1384 | } |
| 1385 | |
| 1386 | static inline QImage qt_gl_read_framebuffer_rgba16(const QSize &size, bool include_alpha, QOpenGLContext *context) |
| 1387 | { |
| 1388 | // We assume OpenGL 1.2+ or ES 3.0+ here. |
| 1389 | QImage img(size, include_alpha ? QImage::Format_RGBA64_Premultiplied : QImage::Format_RGBX64); |
| 1390 | if (!img.isNull()) |
| 1391 | context->functions()->glReadPixels(x: 0, y: 0, width: size.width(), height: size.height(), GL_RGBA, GL_UNSIGNED_SHORT, pixels: img.bits()); |
| 1392 | return img; |
| 1393 | } |
| 1394 | |
| 1395 | static inline QImage qt_gl_read_framebuffer_rgba16f(const QSize &size, bool include_alpha, QOpenGLContext *context) |
| 1396 | { |
| 1397 | // We assume OpenGL (ES) 3.0+ here. |
| 1398 | QImage img(size, include_alpha ? QImage::Format_RGBA16FPx4_Premultiplied : QImage::Format_RGBX16FPx4); |
| 1399 | if (!img.isNull()) |
| 1400 | context->functions()->glReadPixels(x: 0, y: 0, width: size.width(), height: size.height(), GL_RGBA, GL_HALF_FLOAT, pixels: img.bits()); |
| 1401 | return img; |
| 1402 | } |
| 1403 | |
| 1404 | static inline QImage qt_gl_read_framebuffer_rgba32f(const QSize &size, bool include_alpha, QOpenGLContext *context) |
| 1405 | { |
| 1406 | QImage img(size, include_alpha ? QImage::Format_RGBA32FPx4_Premultiplied : QImage::Format_RGBX32FPx4); |
| 1407 | if (!img.isNull()) |
| 1408 | context->functions()->glReadPixels(x: 0, y: 0, width: size.width(), height: size.height(), GL_RGBA, GL_FLOAT, pixels: img.bits()); |
| 1409 | return img; |
| 1410 | } |
| 1411 | |
| 1412 | static QImage qt_gl_read_framebuffer(const QSize &size, GLenum internal_format, bool include_alpha, bool flip) |
| 1413 | { |
| 1414 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 1415 | QOpenGLFunctions *funcs = ctx->functions(); |
| 1416 | while (true) { |
| 1417 | GLenum error = funcs->glGetError(); |
| 1418 | if (error == GL_NO_ERROR || error == GL_CONTEXT_LOST) |
| 1419 | break; |
| 1420 | } |
| 1421 | switch (internal_format) { |
| 1422 | case GL_RGB: |
| 1423 | case GL_RGB8: |
| 1424 | return qt_gl_read_framebuffer_rgba8(size, include_alpha: false, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1425 | case GL_RGB10: |
| 1426 | return qt_gl_read_framebuffer_rgb10a2(size, include_alpha: false, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1427 | case GL_RGB10_A2: |
| 1428 | return qt_gl_read_framebuffer_rgb10a2(size, include_alpha, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1429 | case GL_RGB16: |
| 1430 | return qt_gl_read_framebuffer_rgba16(size, include_alpha: false, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1431 | case GL_RGBA16: |
| 1432 | return qt_gl_read_framebuffer_rgba16(size, include_alpha, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1433 | case GL_RGB16F: |
| 1434 | return qt_gl_read_framebuffer_rgba16f(size, include_alpha: false, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1435 | case GL_RGBA16F: |
| 1436 | return qt_gl_read_framebuffer_rgba16f(size, include_alpha, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1437 | case GL_RGB32F: |
| 1438 | return qt_gl_read_framebuffer_rgba32f(size, include_alpha: false, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1439 | case GL_RGBA32F: |
| 1440 | return qt_gl_read_framebuffer_rgba32f(size, include_alpha, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1441 | case GL_RGBA: |
| 1442 | case GL_RGBA8: |
| 1443 | default: |
| 1444 | return qt_gl_read_framebuffer_rgba8(size, include_alpha, context: ctx).mirrored(horizontally: false, vertically: flip); |
| 1445 | } |
| 1446 | |
| 1447 | Q_UNREACHABLE_RETURN(QImage()); |
| 1448 | } |
| 1449 | |
| 1450 | Q_OPENGL_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha) |
| 1451 | { |
| 1452 | return qt_gl_read_framebuffer(size, internal_format: alpha_format ? GL_RGBA : GL_RGB, include_alpha, flip: true); |
| 1453 | } |
| 1454 | |
| 1455 | /*! |
| 1456 | \fn QImage QOpenGLFramebufferObject::toImage(bool flipped) const |
| 1457 | |
| 1458 | Returns the contents of this framebuffer object as a QImage. |
| 1459 | |
| 1460 | If \a flipped is true the image is flipped from OpenGL coordinates to raster coordinates. |
| 1461 | If used together with QOpenGLPaintDevice, \a flipped should be the opposite of the value |
| 1462 | of QOpenGLPaintDevice::paintFlipped(). |
| 1463 | |
| 1464 | The returned image has a format of premultiplied ARGB32 or RGB32. The latter |
| 1465 | is used only when internalTextureFormat() is set to \c GL_RGB. Since Qt 5.2 |
| 1466 | the function will fall back to premultiplied RGBA8888 or RGBx8888 when |
| 1467 | reading to (A)RGB32 is not supported, and this includes OpenGL ES. Since Qt |
| 1468 | 5.4 an A2BGR30 image is returned if the internal format is RGB10_A2, and since |
| 1469 | Qt 5.12 a RGBA64 image is return if the internal format is RGBA16. |
| 1470 | |
| 1471 | If the rendering in the framebuffer was not done with premultiplied alpha in mind, |
| 1472 | create a wrapper QImage with a non-premultiplied format. This is necessary before |
| 1473 | performing operations like QImage::save() because otherwise the image data would get |
| 1474 | unpremultiplied, even though it was not premultiplied in the first place. To create |
| 1475 | such a wrapper without performing a copy of the pixel data, do the following: |
| 1476 | |
| 1477 | \code |
| 1478 | QImage fboImage(fbo.toImage()); |
| 1479 | QImage image(fboImage.constBits(), fboImage.width(), fboImage.height(), QImage::Format_ARGB32); |
| 1480 | \endcode |
| 1481 | |
| 1482 | For multisampled framebuffer objects the samples are resolved using the |
| 1483 | \c{GL_EXT_framebuffer_blit} extension. If the extension is not available, the contents |
| 1484 | of the returned image is undefined. |
| 1485 | |
| 1486 | For singlesampled framebuffers the contents is retrieved via \c glReadPixels. This is |
| 1487 | a potentially expensive and inefficient operation. Therefore it is recommended that |
| 1488 | this function is used as seldom as possible. |
| 1489 | |
| 1490 | \sa QOpenGLPaintDevice::paintFlipped() |
| 1491 | */ |
| 1492 | |
| 1493 | QImage QOpenGLFramebufferObject::toImage(bool flipped) const |
| 1494 | { |
| 1495 | return toImage(flipped, colorAttachmentIndex: 0); |
| 1496 | } |
| 1497 | |
| 1498 | /*! \overload |
| 1499 | |
| 1500 | Returns the contents of the color attachment of index \a |
| 1501 | colorAttachmentIndex of this framebuffer object as a QImage. This method |
| 1502 | flips the image from OpenGL coordinates to raster coordinates when \a |
| 1503 | flipped is set to \c true. |
| 1504 | |
| 1505 | \note This overload is only fully functional when multiple render targets are |
| 1506 | supported by the OpenGL implementation. When that is not the case, only one |
| 1507 | color attachment will be set up. |
| 1508 | |
| 1509 | \since 5.6 |
| 1510 | */ |
| 1511 | QImage QOpenGLFramebufferObject::toImage(bool flipped, int colorAttachmentIndex) const |
| 1512 | { |
| 1513 | Q_D(const QOpenGLFramebufferObject); |
| 1514 | if (!d->valid) |
| 1515 | return QImage(); |
| 1516 | |
| 1517 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 1518 | if (!ctx) { |
| 1519 | qWarning(msg: "QOpenGLFramebufferObject::toImage() called without a current context" ); |
| 1520 | return QImage(); |
| 1521 | } |
| 1522 | |
| 1523 | if (d->colorAttachments.size() <= colorAttachmentIndex) { |
| 1524 | qWarning(msg: "QOpenGLFramebufferObject::toImage() called for missing color attachment" ); |
| 1525 | return QImage(); |
| 1526 | } |
| 1527 | |
| 1528 | GLuint prevFbo = 0; |
| 1529 | ctx->functions()->glGetIntegerv(GL_FRAMEBUFFER_BINDING, params: (GLint *) &prevFbo); |
| 1530 | |
| 1531 | if (prevFbo != d->fbo()) |
| 1532 | const_cast<QOpenGLFramebufferObject *>(this)->bind(); |
| 1533 | |
| 1534 | QImage image; |
| 1535 | QOpenGLExtraFunctions * = ctx->extraFunctions(); |
| 1536 | // qt_gl_read_framebuffer doesn't work on a multisample FBO |
| 1537 | if (format().samples() != 0) { |
| 1538 | QRect rect(QPoint(0, 0), size()); |
| 1539 | QOpenGLFramebufferObjectFormat fmt; |
| 1540 | if (extraFuncs->hasOpenGLFeature(feature: QOpenGLFunctions::MultipleRenderTargets)) { |
| 1541 | fmt.setInternalTextureFormat(d->colorAttachments[colorAttachmentIndex].internalFormat); |
| 1542 | QOpenGLFramebufferObject temp(d->colorAttachments[colorAttachmentIndex].size, fmt); |
| 1543 | blitFramebuffer(target: &temp, targetRect: rect, source: const_cast<QOpenGLFramebufferObject *>(this), sourceRect: rect, |
| 1544 | GL_COLOR_BUFFER_BIT, GL_NEAREST, |
| 1545 | readColorAttachmentIndex: colorAttachmentIndex, drawColorAttachmentIndex: 0); |
| 1546 | image = temp.toImage(flipped); |
| 1547 | } else { |
| 1548 | fmt.setInternalTextureFormat(d->colorAttachments[0].internalFormat); |
| 1549 | QOpenGLFramebufferObject temp(size(), fmt); |
| 1550 | blitFramebuffer(target: &temp, targetRect: rect, source: const_cast<QOpenGLFramebufferObject *>(this), sourceRect: rect); |
| 1551 | image = temp.toImage(flipped); |
| 1552 | } |
| 1553 | } else { |
| 1554 | if (extraFuncs->hasOpenGLFeature(feature: QOpenGLFunctions::MultipleRenderTargets)) { |
| 1555 | extraFuncs->glReadBuffer(GL_COLOR_ATTACHMENT0 + colorAttachmentIndex); |
| 1556 | image = qt_gl_read_framebuffer(size: d->colorAttachments[colorAttachmentIndex].size, |
| 1557 | internal_format: d->colorAttachments[colorAttachmentIndex].internalFormat, |
| 1558 | include_alpha: true, flip: flipped); |
| 1559 | extraFuncs->glReadBuffer(GL_COLOR_ATTACHMENT0); |
| 1560 | } else { |
| 1561 | image = qt_gl_read_framebuffer(size: d->colorAttachments[0].size, |
| 1562 | internal_format: d->colorAttachments[0].internalFormat, |
| 1563 | include_alpha: true, flip: flipped); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | if (prevFbo != d->fbo()) |
| 1568 | ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: prevFbo); |
| 1569 | |
| 1570 | return image; |
| 1571 | } |
| 1572 | |
| 1573 | /*! |
| 1574 | \fn bool QOpenGLFramebufferObject::bindDefault() |
| 1575 | |
| 1576 | Switches rendering back to the default, windowing system provided |
| 1577 | framebuffer. |
| 1578 | Returns \c true upon success, false otherwise. |
| 1579 | |
| 1580 | \sa bind(), release() |
| 1581 | */ |
| 1582 | bool QOpenGLFramebufferObject::bindDefault() |
| 1583 | { |
| 1584 | QOpenGLContext *ctx = const_cast<QOpenGLContext *>(QOpenGLContext::currentContext()); |
| 1585 | |
| 1586 | if (ctx) { |
| 1587 | ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: ctx->defaultFramebufferObject()); |
| 1588 | QOpenGLContextPrivate::get(context: ctx)->qgl_current_fbo_invalid = true; |
| 1589 | QOpenGLContextPrivate::get(context: ctx)->qgl_current_fbo = nullptr; |
| 1590 | } |
| 1591 | #ifdef QT_DEBUG |
| 1592 | else |
| 1593 | qWarning(msg: "QOpenGLFramebufferObject::bindDefault() called without current context." ); |
| 1594 | #endif |
| 1595 | |
| 1596 | return ctx != nullptr; |
| 1597 | } |
| 1598 | |
| 1599 | /*! |
| 1600 | \fn bool QOpenGLFramebufferObject::hasOpenGLFramebufferObjects() |
| 1601 | |
| 1602 | Returns \c true if the OpenGL \c{GL_EXT_framebuffer_object} extension |
| 1603 | is present on this system; otherwise returns \c false. |
| 1604 | */ |
| 1605 | bool QOpenGLFramebufferObject::hasOpenGLFramebufferObjects() |
| 1606 | { |
| 1607 | return QOpenGLContext::currentContext()->functions()->hasOpenGLFeature(feature: QOpenGLFunctions::Framebuffers); |
| 1608 | } |
| 1609 | |
| 1610 | /*! |
| 1611 | \fn GLuint QOpenGLFramebufferObject::handle() const |
| 1612 | |
| 1613 | Returns the OpenGL framebuffer object handle for this framebuffer |
| 1614 | object (returned by the \c{glGenFrameBuffersEXT()} function). This |
| 1615 | handle can be used to attach new images or buffers to the |
| 1616 | framebuffer. The user is responsible for cleaning up and |
| 1617 | destroying these objects. |
| 1618 | */ |
| 1619 | GLuint QOpenGLFramebufferObject::handle() const |
| 1620 | { |
| 1621 | Q_D(const QOpenGLFramebufferObject); |
| 1622 | return d->fbo(); |
| 1623 | } |
| 1624 | |
| 1625 | /*! |
| 1626 | Returns the status of the depth and stencil buffers attached to |
| 1627 | this framebuffer object. |
| 1628 | */ |
| 1629 | |
| 1630 | QOpenGLFramebufferObject::Attachment QOpenGLFramebufferObject::attachment() const |
| 1631 | { |
| 1632 | Q_D(const QOpenGLFramebufferObject); |
| 1633 | if (d->valid) |
| 1634 | return d->fbo_attachment; |
| 1635 | return NoAttachment; |
| 1636 | } |
| 1637 | |
| 1638 | /*! |
| 1639 | Sets the attachments of the framebuffer object to \a attachment. |
| 1640 | |
| 1641 | This can be used to free or reattach the depth and stencil buffer |
| 1642 | attachments as needed. |
| 1643 | |
| 1644 | \note This function alters the current framebuffer binding. |
| 1645 | */ |
| 1646 | void QOpenGLFramebufferObject::setAttachment(QOpenGLFramebufferObject::Attachment attachment) |
| 1647 | { |
| 1648 | Q_D(QOpenGLFramebufferObject); |
| 1649 | if (attachment == d->fbo_attachment || !isValid()) |
| 1650 | return; |
| 1651 | QOpenGLContext *current = QOpenGLContext::currentContext(); |
| 1652 | if (!current) |
| 1653 | return; |
| 1654 | #ifdef QT_DEBUG |
| 1655 | if (current->shareGroup() != d->fbo_guard->group()) |
| 1656 | qWarning(msg: "QOpenGLFramebufferObject::setAttachment() called from incompatible context" ); |
| 1657 | #endif |
| 1658 | d->funcs.glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: d->fbo()); |
| 1659 | QOpenGLContextPrivate::get(context: current)->qgl_current_fbo_invalid = true; |
| 1660 | d->initDepthStencilAttachments(ctx: current, attachment); |
| 1661 | } |
| 1662 | |
| 1663 | /*! |
| 1664 | Returns \c true if the framebuffer object is currently bound to the current context, |
| 1665 | otherwise false is returned. |
| 1666 | */ |
| 1667 | bool QOpenGLFramebufferObject::isBound() const |
| 1668 | { |
| 1669 | Q_D(const QOpenGLFramebufferObject); |
| 1670 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 1671 | if (!ctx) |
| 1672 | return false; |
| 1673 | GLint fbo = 0; |
| 1674 | ctx->functions()->glGetIntegerv(GL_FRAMEBUFFER_BINDING, params: &fbo); |
| 1675 | return GLuint(fbo) == d->fbo(); |
| 1676 | } |
| 1677 | |
| 1678 | /*! |
| 1679 | \fn bool QOpenGLFramebufferObject::hasOpenGLFramebufferBlit() |
| 1680 | |
| 1681 | Returns \c true if the OpenGL \c{GL_EXT_framebuffer_blit} extension |
| 1682 | is present on this system; otherwise returns \c false. |
| 1683 | |
| 1684 | \sa blitFramebuffer() |
| 1685 | */ |
| 1686 | bool QOpenGLFramebufferObject::hasOpenGLFramebufferBlit() |
| 1687 | { |
| 1688 | return QOpenGLExtensions(QOpenGLContext::currentContext()).hasOpenGLExtension(extension: QOpenGLExtensions::FramebufferBlit); |
| 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | /*! |
| 1693 | \overload |
| 1694 | |
| 1695 | Convenience overload to blit between two framebuffer objects. |
| 1696 | */ |
| 1697 | void QOpenGLFramebufferObject::blitFramebuffer(QOpenGLFramebufferObject *target, |
| 1698 | QOpenGLFramebufferObject *source, |
| 1699 | GLbitfield buffers, GLenum filter) |
| 1700 | { |
| 1701 | if (!target && !source) |
| 1702 | return; |
| 1703 | |
| 1704 | QSize targetSize; |
| 1705 | QSize sourceSize; |
| 1706 | |
| 1707 | if (target) |
| 1708 | targetSize = target->size(); |
| 1709 | if (source) |
| 1710 | sourceSize = source->size(); |
| 1711 | |
| 1712 | if (targetSize.isEmpty()) |
| 1713 | targetSize = sourceSize; |
| 1714 | else if (sourceSize.isEmpty()) |
| 1715 | sourceSize = targetSize; |
| 1716 | |
| 1717 | blitFramebuffer(target, targetRect: QRect(QPoint(0, 0), targetSize), |
| 1718 | source, sourceRect: QRect(QPoint(0, 0), sourceSize), |
| 1719 | buffers, filter); |
| 1720 | } |
| 1721 | |
| 1722 | /*! \overload |
| 1723 | * |
| 1724 | Convenience overload to blit between two framebuffer objects. |
| 1725 | */ |
| 1726 | void QOpenGLFramebufferObject::blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, |
| 1727 | QOpenGLFramebufferObject *source, const QRect &sourceRect, |
| 1728 | GLbitfield buffers, |
| 1729 | GLenum filter) |
| 1730 | { |
| 1731 | blitFramebuffer(target, targetRect, source, sourceRect, buffers, filter, readColorAttachmentIndex: 0, drawColorAttachmentIndex: 0); |
| 1732 | } |
| 1733 | |
| 1734 | /*! |
| 1735 | \enum QOpenGLFramebufferObject::FramebufferRestorePolicy |
| 1736 | \since 5.7 |
| 1737 | |
| 1738 | This enum type is used to configure the behavior related to restoring |
| 1739 | framebuffer bindings when calling blitFramebuffer(). |
| 1740 | |
| 1741 | \value DontRestoreFramebufferBinding Do not restore the previous framebuffer binding. |
| 1742 | The caller is responsible for tracking and setting |
| 1743 | the framebuffer binding as needed. |
| 1744 | |
| 1745 | \value RestoreFramebufferBindingToDefault After the blit operation, bind the default |
| 1746 | framebuffer. |
| 1747 | |
| 1748 | \value RestoreFrameBufferBinding Restore the previously bound framebuffer. This is |
| 1749 | potentially expensive because of the need to |
| 1750 | query the currently bound framebuffer. |
| 1751 | |
| 1752 | \sa blitFramebuffer() |
| 1753 | */ |
| 1754 | |
| 1755 | /*! |
| 1756 | \since 5.7 |
| 1757 | |
| 1758 | Blits from the \a sourceRect rectangle in the \a source framebuffer |
| 1759 | object to the \a targetRect rectangle in the \a target framebuffer object. |
| 1760 | |
| 1761 | If \a source or \a target is 0, the default framebuffer will be used |
| 1762 | instead of a framebuffer object as source or target respectively. |
| 1763 | |
| 1764 | This function will have no effect unless hasOpenGLFramebufferBlit() returns |
| 1765 | true. |
| 1766 | |
| 1767 | The \a buffers parameter should be a mask consisting of any combination of |
| 1768 | \c GL_COLOR_BUFFER_BIT, \c GL_DEPTH_BUFFER_BIT, and |
| 1769 | \c GL_STENCIL_BUFFER_BIT. Any buffer type that is not present both |
| 1770 | in the source and target buffers is ignored. |
| 1771 | |
| 1772 | The \a sourceRect and \a targetRect rectangles may have different sizes; |
| 1773 | in this case \a buffers should not contain \c GL_DEPTH_BUFFER_BIT or |
| 1774 | \c GL_STENCIL_BUFFER_BIT. The \a filter parameter should be set to |
| 1775 | \c GL_LINEAR or \c GL_NEAREST, and specifies whether linear or nearest |
| 1776 | interpolation should be used when scaling is performed. |
| 1777 | |
| 1778 | If \a source equals \a target a copy is performed within the same buffer. |
| 1779 | Results are undefined if the source and target rectangles overlap and |
| 1780 | have different sizes. The sizes must also be the same if any of the |
| 1781 | framebuffer objects are multisample framebuffers. |
| 1782 | |
| 1783 | \note The scissor test will restrict the blit area if enabled. |
| 1784 | |
| 1785 | When multiple render targets are in use, \a readColorAttachmentIndex and \a |
| 1786 | drawColorAttachmentIndex specify the index of the color attachments in the |
| 1787 | source and destination framebuffers. |
| 1788 | |
| 1789 | The \a restorePolicy determines if the framebuffer that was bound prior to |
| 1790 | calling this function should be restored, or if the default framebuffer |
| 1791 | should be bound before returning, of if the caller is responsible for |
| 1792 | tracking and setting the bound framebuffer. Restoring the previous |
| 1793 | framebuffer can be relatively expensive due to the call to \c{glGetIntegerv} |
| 1794 | which on some OpenGL drivers may imply a pipeline stall. |
| 1795 | |
| 1796 | \sa hasOpenGLFramebufferBlit() |
| 1797 | */ |
| 1798 | void QOpenGLFramebufferObject::blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, |
| 1799 | QOpenGLFramebufferObject *source, const QRect &sourceRect, |
| 1800 | GLbitfield buffers, |
| 1801 | GLenum filter, |
| 1802 | int readColorAttachmentIndex, |
| 1803 | int drawColorAttachmentIndex, |
| 1804 | QOpenGLFramebufferObject::FramebufferRestorePolicy restorePolicy) |
| 1805 | { |
| 1806 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 1807 | if (!ctx) |
| 1808 | return; |
| 1809 | |
| 1810 | QOpenGLExtensions extensions(ctx); |
| 1811 | if (!extensions.hasOpenGLExtension(extension: QOpenGLExtensions::FramebufferBlit)) |
| 1812 | return; |
| 1813 | |
| 1814 | GLuint prevFbo = 0; |
| 1815 | if (restorePolicy == RestoreFrameBufferBinding) |
| 1816 | ctx->functions()->glGetIntegerv(GL_FRAMEBUFFER_BINDING, params: (GLint *) &prevFbo); |
| 1817 | |
| 1818 | const int sx0 = sourceRect.left(); |
| 1819 | const int sx1 = sourceRect.left() + sourceRect.width(); |
| 1820 | const int sy0 = sourceRect.top(); |
| 1821 | const int sy1 = sourceRect.top() + sourceRect.height(); |
| 1822 | |
| 1823 | const int tx0 = targetRect.left(); |
| 1824 | const int tx1 = targetRect.left() + targetRect.width(); |
| 1825 | const int ty0 = targetRect.top(); |
| 1826 | const int ty1 = targetRect.top() + targetRect.height(); |
| 1827 | |
| 1828 | const GLuint defaultFboId = ctx->defaultFramebufferObject(); |
| 1829 | |
| 1830 | extensions.glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer: source ? source->handle() : defaultFboId); |
| 1831 | extensions.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer: target ? target->handle() : defaultFboId); |
| 1832 | |
| 1833 | const bool supportsMRT = extensions.hasOpenGLFeature(feature: QOpenGLFunctions::MultipleRenderTargets); |
| 1834 | if (supportsMRT) { |
| 1835 | extensions.glReadBuffer(GL_COLOR_ATTACHMENT0 + readColorAttachmentIndex); |
| 1836 | if (target) { |
| 1837 | GLenum drawBuf = GL_COLOR_ATTACHMENT0 + drawColorAttachmentIndex; |
| 1838 | extensions.glDrawBuffers(n: 1, bufs: &drawBuf); |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | extensions.glBlitFramebuffer(srcX0: sx0, srcY0: sy0, srcX1: sx1, srcY1: sy1, |
| 1843 | dstX0: tx0, dstY0: ty0, dstX1: tx1, dstY1: ty1, |
| 1844 | mask: buffers, filter); |
| 1845 | |
| 1846 | if (supportsMRT) |
| 1847 | extensions.glReadBuffer(GL_COLOR_ATTACHMENT0); |
| 1848 | |
| 1849 | switch (restorePolicy) { |
| 1850 | case RestoreFrameBufferBinding: |
| 1851 | ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: prevFbo); // sets both READ and DRAW |
| 1852 | break; |
| 1853 | |
| 1854 | case RestoreFramebufferBindingToDefault: |
| 1855 | ctx->functions()->glBindFramebuffer(GL_FRAMEBUFFER, framebuffer: ctx->defaultFramebufferObject()); // sets both READ and DRAW |
| 1856 | break; |
| 1857 | |
| 1858 | case DontRestoreFramebufferBinding: |
| 1859 | break; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | /*! |
| 1864 | \overload |
| 1865 | |
| 1866 | Convenience overload to blit between two framebuffer objects and |
| 1867 | to restore the previous framebuffer binding. Equivalent to calling |
| 1868 | blitFramebuffer(target, targetRect, source, sourceRect, buffers, filter, |
| 1869 | readColorAttachmentIndex, drawColorAttachmentIndex, |
| 1870 | RestoreFrameBufferBinding). |
| 1871 | */ |
| 1872 | void QOpenGLFramebufferObject::blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, |
| 1873 | QOpenGLFramebufferObject *source, const QRect &sourceRect, |
| 1874 | GLbitfield buffers, |
| 1875 | GLenum filter, |
| 1876 | int readColorAttachmentIndex, |
| 1877 | int drawColorAttachmentIndex) |
| 1878 | { |
| 1879 | blitFramebuffer(target, targetRect, source, sourceRect, |
| 1880 | buffers, filter, |
| 1881 | readColorAttachmentIndex, |
| 1882 | drawColorAttachmentIndex, |
| 1883 | restorePolicy: RestoreFrameBufferBinding); |
| 1884 | } |
| 1885 | |
| 1886 | QT_END_NAMESPACE |
| 1887 | |