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_SUBMISSIONCONTEXT_H
42#define QT3DRENDER_RENDER_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 <Qt3DRender/private/graphicscontext_p.h>
57#include <Qt3DRender/private/texturesubmissioncontext_p.h>
58#include <Qt3DRender/private/imagesubmissioncontext_p.h>
59#include <Qt3DRender/qclearbuffers.h>
60#include <Qt3DRender/private/glbuffer_p.h>
61#include <Qt3DRender/qattribute.h>
62#include <Qt3DRender/private/handle_types_p.h>
63#include <Qt3DRender/private/shadercache_p.h>
64#include <Qt3DRender/private/glfence_p.h>
65
66QT_BEGIN_NAMESPACE
67
68class QAbstractOpenGLFunctions;
69
70namespace Qt3DRender {
71
72namespace Render {
73
74class Renderer;
75class GraphicsHelperInterface;
76class RenderStateSet;
77class Material;
78class GLTexture;
79class RenderCommand;
80class RenderTarget;
81class AttachmentPack;
82class Attribute;
83class Buffer;
84class ShaderManager;
85struct StateVariant;
86
87typedef QPair<QString, int> NamedUniformLocation;
88
89class Q_AUTOTEST_EXPORT SubmissionContext : public GraphicsContext
90{
91public:
92 SubmissionContext();
93 ~SubmissionContext();
94
95 int id() const; // unique, small integer ID of this context
96 void setRenderer(Renderer *renderer) { m_renderer = renderer; }
97
98 bool beginDrawing(QSurface *surface);
99 void endDrawing(bool swapBuffers);
100 void releaseOpenGL();
101 void setOpenGLContext(QOpenGLContext* ctx);
102
103 // Viewport
104 void setViewport(const QRectF &viewport, const QSize &surfaceSize);
105 QRectF viewport() const { return m_viewport; }
106
107 // Shaders
108 bool activateShader(ProgramDNA shaderDNA);
109 QOpenGLShaderProgram *activeShader() const { return m_activeShader; }
110
111 // FBO
112 GLuint activeFBO() const { return m_activeFBO; }
113 void activateRenderTarget(const Qt3DCore::QNodeId id, const AttachmentPack &attachments, GLuint defaultFboId);
114 QSize renderTargetSize(const QSize &surfaceSize) const;
115 QImage readFramebuffer(const QRect &rect);
116 void blitFramebuffer(Qt3DCore::QNodeId outputRenderTargetId, Qt3DCore::QNodeId inputRenderTargetId,
117 QRect inputRect,
118 QRect outputRect, uint defaultFboId,
119 QRenderTargetOutput::AttachmentPoint inputAttachmentPoint,
120 QRenderTargetOutput::AttachmentPoint outputAttachmentPoint,
121 QBlitFramebuffer::InterpolationMethod interpolationMethod);
122
123 // Attributes
124 void specifyAttribute(const Attribute *attribute,
125 Buffer *buffer,
126 const ShaderAttribute *attributeDescription);
127 void specifyIndices(Buffer *buffer);
128
129 // Buffer
130 void updateBuffer(Buffer *buffer);
131 QByteArray downloadBufferContent(Buffer *buffer);
132 void releaseBuffer(Qt3DCore::QNodeId bufferId);
133 bool hasGLBufferForBuffer(Buffer *buffer);
134 GLBuffer *glBufferForRenderBuffer(Buffer *buf, GLBuffer::Type type);
135
136 // Parameters
137 bool setParameters(ShaderParameterPack &parameterPack);
138
139 // RenderState
140 void setCurrentStateSet(RenderStateSet* ss);
141 RenderStateSet *currentStateSet() const;
142 void applyState(const StateVariant &state);
143
144 void resetMasked(qint64 maskOfStatesToReset);
145 void applyStateSet(RenderStateSet *ss);
146
147 // Wrappers
148 void clearColor(const QColor &color);
149 void clearDepthValue(float depth);
150 void clearStencilValue(int stencil);
151
152
153 // Fences
154 GLFence fenceSync();
155 void clientWaitSync(GLFence sync, GLuint64 nanoSecTimeout);
156 void waitSync(GLFence sync);
157 bool wasSyncSignaled(GLFence sync);
158 void deleteSync(GLFence sync);
159
160private:
161 void initialize();
162
163 // Material
164 Material* activeMaterial() const { return m_material; }
165 void setActiveMaterial(Material* rmat);
166
167 // FBO
168 void bindFrameBufferAttachmentHelper(GLuint fboId, const AttachmentPack &attachments);
169 void activateDrawBuffers(const AttachmentPack &attachments);
170 void resolveRenderTargetFormat();
171 GLuint createRenderTarget(Qt3DCore::QNodeId renderTargetNodeId, const AttachmentPack &attachments);
172 GLuint updateRenderTarget(Qt3DCore::QNodeId renderTargetNodeId, const AttachmentPack &attachments, bool isActiveRenderTarget);
173
174 // Buffers
175 HGLBuffer createGLBufferFor(Buffer *buffer, GLBuffer::Type type);
176 void uploadDataToGLBuffer(Buffer *buffer, GLBuffer *b, bool releaseBuffer = false);
177 QByteArray downloadDataFromGLBuffer(Buffer *buffer, GLBuffer *b);
178 bool bindGLBuffer(GLBuffer *buffer, GLBuffer::Type type);
179
180 bool m_ownCurrent;
181 const unsigned int m_id;
182 QSurface *m_surface;
183 QSize m_surfaceSize;
184
185 QOpenGLShaderProgram *m_activeShader;
186 ProgramDNA m_activeShaderDNA;
187
188 QHash<Qt3DCore::QNodeId, HGLBuffer> m_renderBufferHash;
189 QHash<Qt3DCore::QNodeId, GLuint> m_renderTargets;
190 QHash<GLuint, QSize> m_renderTargetsSize;
191 QAbstractTexture::TextureFormat m_renderTargetFormat;
192
193 // cache some current state, to make sure we don't issue unnecessary GL calls
194 int m_currClearStencilValue;
195 float m_currClearDepthValue;
196 QColor m_currClearColorValue;
197
198 Material* m_material;
199 QRectF m_viewport;
200 GLuint m_activeFBO;
201
202 GLBuffer *m_boundArrayBuffer;
203 RenderStateSet* m_stateSet;
204 Renderer *m_renderer;
205 QByteArray m_uboTempArray;
206
207 TextureSubmissionContext m_textureContext;
208 ImageSubmissionContext m_imageContext;
209
210 // Attributes
211 friend class OpenGLVertexArrayObject;
212
213 struct VAOVertexAttribute
214 {
215 HGLBuffer bufferHandle;
216 GLBuffer::Type attributeType;
217 int location;
218 GLint dataType;
219 uint byteOffset;
220 uint vertexSize;
221 uint byteStride;
222 uint divisor;
223 GLenum shaderDataType;
224 };
225
226 using VAOIndexAttribute = HGLBuffer;
227 void enableAttribute(const VAOVertexAttribute &attr);
228 void disableAttribute(const VAOVertexAttribute &attr);
229};
230
231} // namespace Render
232} // namespace Qt3DRender
233
234QT_END_NAMESPACE
235
236#endif // QT3DRENDER_RENDER_SUBMISSIONCONTEXT_H
237