1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
2 | // Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #ifndef QT3DRENDER_RENDER_OPENGL_SUBMISSIONCONTEXT_H |
6 | #define QT3DRENDER_RENDER_OPENGL_SUBMISSIONCONTEXT_H |
7 | |
8 | // |
9 | // W A R N I N G |
10 | // ------------- |
11 | // |
12 | // This file is not part of the Qt API. It exists for the convenience |
13 | // of other Qt classes. This header file may change from version to |
14 | // version without notice, or even be removed. |
15 | // |
16 | // We mean it. |
17 | // |
18 | |
19 | |
20 | #include <glbuffer_p.h> |
21 | #include <glfence_p.h> |
22 | #include <graphicscontext_p.h> |
23 | #include <texturesubmissioncontext_p.h> |
24 | #include <imagesubmissioncontext_p.h> |
25 | #include <Qt3DRender/qclearbuffers.h> |
26 | #include <Qt3DRender/private/handle_types_p.h> |
27 | #include <Qt3DRender/private/attachmentpack_p.h> |
28 | |
29 | QT_BEGIN_NAMESPACE |
30 | |
31 | class QAbstractOpenGLFunctions; |
32 | |
33 | namespace Qt3DRender { |
34 | |
35 | namespace Render { |
36 | |
37 | class Material; |
38 | class AttachmentPack; |
39 | class Attribute; |
40 | class Buffer; |
41 | class ShaderManager; |
42 | struct StateVariant; |
43 | class RenderTarget; |
44 | class RenderStateSet; |
45 | |
46 | namespace OpenGL { |
47 | |
48 | class Renderer; |
49 | class GraphicsHelperInterface; |
50 | class GLTexture; |
51 | class RenderCommand; |
52 | |
53 | typedef QPair<QString, int> NamedUniformLocation; |
54 | |
55 | class Q_AUTOTEST_EXPORT SubmissionContext : public GraphicsContext |
56 | { |
57 | public: |
58 | SubmissionContext(); |
59 | ~SubmissionContext(); |
60 | |
61 | int id() const; // unique, small integer ID of this context |
62 | void setRenderer(Renderer *renderer) { m_renderer = renderer; } |
63 | |
64 | bool beginDrawing(QSurface *surface); |
65 | void endDrawing(bool swapBuffers); |
66 | void releaseOpenGL(); |
67 | void setOpenGLContext(QOpenGLContext* ctx); |
68 | |
69 | // Viewport |
70 | void setViewport(const QRectF &viewport, const QSize &surfaceSize); |
71 | QRectF viewport() const { return m_viewport; } |
72 | |
73 | // Shaders |
74 | bool activateShader(GLShader *shader); |
75 | QOpenGLShaderProgram *activeShader() const { return m_activeShader; } |
76 | |
77 | // FBO |
78 | GLuint activeFBO() const { return m_activeFBO; } |
79 | void activateRenderTarget(const Qt3DCore::QNodeId id, const AttachmentPack &attachments, GLuint defaultFboId); |
80 | void releaseRenderTarget(const Qt3DCore::QNodeId id); |
81 | void releaseRenderTargets(); |
82 | QSize renderTargetSize(const QSize &surfaceSize) const; |
83 | QImage readFramebuffer(const QRect &rect); |
84 | void blitFramebuffer(Qt3DCore::QNodeId outputRenderTargetId, Qt3DCore::QNodeId inputRenderTargetId, |
85 | QRect inputRect, |
86 | QRect outputRect, uint defaultFboId, |
87 | QRenderTargetOutput::AttachmentPoint inputAttachmentPoint, |
88 | QRenderTargetOutput::AttachmentPoint outputAttachmentPoint, |
89 | QBlitFramebuffer::InterpolationMethod interpolationMethod); |
90 | |
91 | // Attributes |
92 | void specifyAttribute(const Attribute *attribute, |
93 | Buffer *buffer, |
94 | const ShaderAttribute *attributeDescription); |
95 | void specifyIndices(Buffer *buffer); |
96 | |
97 | // Buffer |
98 | void updateBuffer(Buffer *buffer); |
99 | QByteArray downloadBufferContent(Buffer *buffer); |
100 | void releaseBuffer(Qt3DCore::QNodeId bufferId); |
101 | bool hasGLBufferForBuffer(Buffer *buffer); |
102 | GLBuffer *glBufferForRenderBuffer(Buffer *buf); |
103 | |
104 | // Parameters |
105 | bool setParameters(ShaderParameterPack ¶meterPack, GLShader *shader); |
106 | |
107 | // RenderState |
108 | void setCurrentStateSet(RenderStateSet* ss); |
109 | RenderStateSet *currentStateSet() const; |
110 | void applyState(const StateVariant &state); |
111 | void resetState(); |
112 | |
113 | void resetMasked(qint64 maskOfStatesToReset); |
114 | void applyStateSet(RenderStateSet *ss); |
115 | |
116 | // Wrappers |
117 | void clearColor(const QColor &color); |
118 | void clearDepthValue(float depth); |
119 | void clearStencilValue(int stencil); |
120 | |
121 | |
122 | // Fences |
123 | GLFence fenceSync(); |
124 | void clientWaitSync(GLFence sync, GLuint64 nanoSecTimeout); |
125 | void waitSync(GLFence sync); |
126 | bool wasSyncSignaled(GLFence sync); |
127 | void deleteSync(GLFence sync); |
128 | |
129 | // Textures |
130 | void setUpdatedTexture(const Qt3DCore::QNodeIdVector &updatedTextureIds); |
131 | |
132 | private: |
133 | struct RenderTargetInfo { |
134 | GLuint fboId; |
135 | QSize size; |
136 | AttachmentPack attachments; |
137 | }; |
138 | |
139 | void initialize(); |
140 | |
141 | // Material |
142 | Material* activeMaterial() const { return m_material; } |
143 | void setActiveMaterial(Material* rmat); |
144 | |
145 | // FBO |
146 | RenderTargetInfo bindFrameBufferAttachmentHelper(GLuint fboId, const AttachmentPack &attachments); |
147 | void activateDrawBuffers(const AttachmentPack &attachments); |
148 | void resolveRenderTargetFormat(); |
149 | GLuint createRenderTarget(Qt3DCore::QNodeId renderTargetNodeId, const AttachmentPack &attachments); |
150 | GLuint updateRenderTarget(Qt3DCore::QNodeId renderTargetNodeId, const AttachmentPack &attachments, bool isActiveRenderTarget); |
151 | |
152 | // Buffers |
153 | HGLBuffer createGLBufferFor(Buffer *buffer); |
154 | void uploadDataToGLBuffer(Buffer *buffer, GLBuffer *b, bool releaseBuffer = false); |
155 | QByteArray downloadDataFromGLBuffer(Buffer *buffer, GLBuffer *b); |
156 | bool bindGLBuffer(GLBuffer *buffer, GLBuffer::Type type); |
157 | |
158 | bool m_ownCurrent; |
159 | const unsigned int m_id; |
160 | QSurface *m_surface; |
161 | QSize m_surfaceSize; |
162 | |
163 | QOpenGLShaderProgram *m_activeShader; |
164 | |
165 | QHash<Qt3DCore::QNodeId, HGLBuffer> m_renderBufferHash; |
166 | |
167 | |
168 | QHash<Qt3DCore::QNodeId, RenderTargetInfo> m_renderTargets; |
169 | QAbstractTexture::TextureFormat m_renderTargetFormat; |
170 | |
171 | // cache some current state, to make sure we don't issue unnecessary GL calls |
172 | int m_currClearStencilValue; |
173 | float m_currClearDepthValue; |
174 | QColor m_currClearColorValue; |
175 | |
176 | Material* m_material; |
177 | QRectF m_viewport; |
178 | GLuint m_activeFBO; |
179 | Qt3DCore::QNodeId m_activeFBONodeId; |
180 | |
181 | GLBuffer *m_boundArrayBuffer; |
182 | RenderStateSet* m_stateSet; |
183 | Renderer *m_renderer; |
184 | QByteArray m_uboTempArray; |
185 | |
186 | TextureSubmissionContext m_textureContext; |
187 | ImageSubmissionContext m_imageContext; |
188 | |
189 | // Attributes |
190 | friend class OpenGLVertexArrayObject; |
191 | |
192 | struct VAOVertexAttribute |
193 | { |
194 | HGLBuffer bufferHandle; |
195 | GLBuffer::Type attributeType; |
196 | int location; |
197 | GLint dataType; |
198 | uint byteOffset; |
199 | uint vertexSize; |
200 | uint byteStride; |
201 | uint divisor; |
202 | GLenum shaderDataType; |
203 | }; |
204 | |
205 | using VAOIndexAttribute = HGLBuffer; |
206 | void enableAttribute(const VAOVertexAttribute &attr); |
207 | void disableAttribute(const VAOVertexAttribute &attr); |
208 | |
209 | Qt3DCore::QNodeIdVector m_updateTextureIds; |
210 | }; |
211 | |
212 | } // namespace OpenGL |
213 | } // namespace Render |
214 | } // namespace Qt3DRender |
215 | |
216 | QT_END_NAMESPACE |
217 | |
218 | #endif // QT3DRENDER_RENDER_OPENGL_SUBMISSIONCONTEXT_H |
219 | |