| 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 | #ifndef QOPENGLFRAMEBUFFEROBJECT_H |
| 5 | #define QOPENGLFRAMEBUFFEROBJECT_H |
| 6 | |
| 7 | #include <QtOpenGL/qtopenglglobal.h> |
| 8 | |
| 9 | #include <QtGui/qopengl.h> |
| 10 | #include <QtGui/qpaintdevice.h> |
| 11 | |
| 12 | #include <QtCore/qscopedpointer.h> |
| 13 | |
| 14 | #if defined(Q_QDOC) |
| 15 | #undef GLuint |
| 16 | typedef unsigned int GLuint; |
| 17 | #undef GLenum |
| 18 | typedef unsigned int GLenum; |
| 19 | #undef GL_TEXTURE_2D |
| 20 | #define GL_TEXTURE_2D 0x0DE1 |
| 21 | #undef GLbitfield |
| 22 | typedef unsigned int GLbitfield; |
| 23 | #endif |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QOpenGLFramebufferObjectPrivate; |
| 28 | class QOpenGLFramebufferObjectFormat; |
| 29 | |
| 30 | class Q_OPENGL_EXPORT QOpenGLFramebufferObject |
| 31 | { |
| 32 | Q_DECLARE_PRIVATE(QOpenGLFramebufferObject) |
| 33 | public: |
| 34 | enum Attachment { |
| 35 | NoAttachment, |
| 36 | CombinedDepthStencil, |
| 37 | Depth |
| 38 | }; |
| 39 | |
| 40 | explicit QOpenGLFramebufferObject(const QSize &size, GLenum target = GL_TEXTURE_2D); |
| 41 | QOpenGLFramebufferObject(int width, int height, GLenum target = GL_TEXTURE_2D); |
| 42 | |
| 43 | QOpenGLFramebufferObject(const QSize &size, Attachment attachment, |
| 44 | GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0); |
| 45 | QOpenGLFramebufferObject(int width, int height, Attachment attachment, |
| 46 | GLenum target = GL_TEXTURE_2D, GLenum internalFormat = 0); |
| 47 | |
| 48 | QOpenGLFramebufferObject(const QSize &size, const QOpenGLFramebufferObjectFormat &format); |
| 49 | QOpenGLFramebufferObject(int width, int height, const QOpenGLFramebufferObjectFormat &format); |
| 50 | |
| 51 | virtual ~QOpenGLFramebufferObject(); |
| 52 | |
| 53 | void addColorAttachment(const QSize &size, GLenum internalFormat = 0); |
| 54 | void addColorAttachment(int width, int height, GLenum internalFormat = 0); |
| 55 | |
| 56 | QOpenGLFramebufferObjectFormat format() const; |
| 57 | |
| 58 | bool isValid() const; |
| 59 | bool isBound() const; |
| 60 | bool bind(); |
| 61 | bool release(); |
| 62 | |
| 63 | int width() const { return size().width(); } |
| 64 | int height() const { return size().height(); } |
| 65 | |
| 66 | GLuint texture() const; |
| 67 | QList<GLuint> textures() const; |
| 68 | |
| 69 | GLuint takeTexture(); |
| 70 | GLuint takeTexture(int colorAttachmentIndex); |
| 71 | |
| 72 | QSize size() const; |
| 73 | QList<QSize> sizes() const; |
| 74 | |
| 75 | QImage toImage(bool flipped = true) const; |
| 76 | QImage toImage(bool flipped, int colorAttachmentIndex) const; |
| 77 | |
| 78 | Attachment attachment() const; |
| 79 | void setAttachment(Attachment attachment); |
| 80 | |
| 81 | GLuint handle() const; |
| 82 | |
| 83 | static bool bindDefault(); |
| 84 | |
| 85 | static bool hasOpenGLFramebufferObjects(); |
| 86 | |
| 87 | static bool hasOpenGLFramebufferBlit(); |
| 88 | |
| 89 | enum FramebufferRestorePolicy { |
| 90 | DontRestoreFramebufferBinding, |
| 91 | RestoreFramebufferBindingToDefault, |
| 92 | RestoreFrameBufferBinding |
| 93 | }; |
| 94 | |
| 95 | static void blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, |
| 96 | QOpenGLFramebufferObject *source, const QRect &sourceRect, |
| 97 | GLbitfield buffers, |
| 98 | GLenum filter, |
| 99 | int readColorAttachmentIndex, |
| 100 | int drawColorAttachmentIndex, |
| 101 | FramebufferRestorePolicy restorePolicy); |
| 102 | static void blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, |
| 103 | QOpenGLFramebufferObject *source, const QRect &sourceRect, |
| 104 | GLbitfield buffers, |
| 105 | GLenum filter, |
| 106 | int readColorAttachmentIndex, |
| 107 | int drawColorAttachmentIndex); |
| 108 | static void blitFramebuffer(QOpenGLFramebufferObject *target, const QRect &targetRect, |
| 109 | QOpenGLFramebufferObject *source, const QRect &sourceRect, |
| 110 | GLbitfield buffers = GL_COLOR_BUFFER_BIT, |
| 111 | GLenum filter = GL_NEAREST); |
| 112 | static void blitFramebuffer(QOpenGLFramebufferObject *target, |
| 113 | QOpenGLFramebufferObject *source, |
| 114 | GLbitfield buffers = GL_COLOR_BUFFER_BIT, |
| 115 | GLenum filter = GL_NEAREST); |
| 116 | |
| 117 | private: |
| 118 | Q_DISABLE_COPY(QOpenGLFramebufferObject) |
| 119 | QScopedPointer<QOpenGLFramebufferObjectPrivate> d_ptr; |
| 120 | friend class QOpenGLPaintDevice; |
| 121 | friend class QOpenGLFBOGLPaintDevice; |
| 122 | }; |
| 123 | |
| 124 | class QOpenGLFramebufferObjectFormatPrivate; |
| 125 | class Q_OPENGL_EXPORT QOpenGLFramebufferObjectFormat |
| 126 | { |
| 127 | public: |
| 128 | QOpenGLFramebufferObjectFormat(); |
| 129 | QOpenGLFramebufferObjectFormat(const QOpenGLFramebufferObjectFormat &other); |
| 130 | QOpenGLFramebufferObjectFormat &operator=(const QOpenGLFramebufferObjectFormat &other); |
| 131 | ~QOpenGLFramebufferObjectFormat(); |
| 132 | |
| 133 | void setSamples(int samples); |
| 134 | int samples() const; |
| 135 | |
| 136 | void setMipmap(bool enabled); |
| 137 | bool mipmap() const; |
| 138 | |
| 139 | void setAttachment(QOpenGLFramebufferObject::Attachment attachment); |
| 140 | QOpenGLFramebufferObject::Attachment attachment() const; |
| 141 | |
| 142 | void setTextureTarget(GLenum target); |
| 143 | GLenum textureTarget() const; |
| 144 | |
| 145 | void setInternalTextureFormat(GLenum internalTextureFormat); |
| 146 | GLenum internalTextureFormat() const; |
| 147 | |
| 148 | bool operator==(const QOpenGLFramebufferObjectFormat& other) const; |
| 149 | bool operator!=(const QOpenGLFramebufferObjectFormat& other) const; |
| 150 | |
| 151 | private: |
| 152 | QOpenGLFramebufferObjectFormatPrivate *d; |
| 153 | |
| 154 | void detach(); |
| 155 | }; |
| 156 | |
| 157 | QT_END_NAMESPACE |
| 158 | |
| 159 | #endif // QOPENGLFRAMEBUFFEROBJECT_H |
| 160 | |