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

source code of qt3d/src/plugins/renderers/opengl/graphicshelpers/submissioncontext_p.h