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_GRAPHICSCONTEXT_H
42#define QT3DRENDER_RENDER_GRAPHICSCONTEXT_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#include <QOpenGLContext>
56#include <QOpenGLFunctions>
57#include <QOpenGLVertexArrayObject>
58#include <QHash>
59#include <QColor>
60#include <QMatrix4x4>
61#include <QBitArray>
62#include <QImage>
63#include <Qt3DRender/private/shaderparameterpack_p.h>
64#include <Qt3DRender/qclearbuffers.h>
65#include <Qt3DRender/private/shader_p.h>
66#include <Qt3DRender/private/glbuffer_p.h>
67#include <Qt3DRender/qattribute.h>
68#include <Qt3DRender/qmemorybarrier.h>
69#include <Qt3DRender/private/handle_types_p.h>
70#include <Qt3DRender/private/qgraphicsapifilter_p.h>
71#include <Qt3DRender/private/shadercache_p.h>
72#include <Qt3DRender/private/uniform_p.h>
73#include <Qt3DRender/private/graphicshelperinterface_p.h>
74#include <Qt3DRender/private/qblitframebuffer_p.h>
75#include <qmath.h>
76
77QT_BEGIN_NAMESPACE
78
79class QOpenGLShaderProgram;
80class QAbstractOpenGLFunctions;
81class QOpenGLDebugLogger;
82
83namespace Qt3DRender {
84
85namespace Render {
86
87class GraphicsHelperInterface;
88class RenderTarget;
89class AttachmentPack;
90class ShaderManager;
91
92typedef QPair<QString, int> NamedUniformLocation;
93
94class Q_AUTOTEST_EXPORT GraphicsContext
95{
96public:
97 GraphicsContext();
98 ~GraphicsContext();
99
100 void setShaderCache(ShaderCache *shaderCache) { m_shaderCache = shaderCache; }
101 ShaderCache *shaderCache() const { return m_shaderCache; }
102
103 void setOpenGLContext(QOpenGLContext* ctx);
104 QOpenGLContext *openGLContext() { return m_gl; }
105 bool makeCurrent(QSurface *surface);
106 void doneCurrent();
107 bool hasValidGLHelper() const;
108 bool isInitialized() const;
109
110 // Shaders
111 QOpenGLShaderProgram *createShaderProgram(Shader *shaderNode);
112 void introspectShaderInterface(Shader *shader, QOpenGLShaderProgram *shaderProgram);
113 void loadShader(Shader* shader, ShaderManager *manager);
114 void removeShaderProgramReference(Shader *shaderNode);
115
116 GLuint defaultFBO() const { return m_defaultFBO; }
117
118 const GraphicsApiFilterData *contextInfo() const;
119
120 // Wrapper methods
121 void clearBackBuffer(QClearBuffers::BufferTypeFlags buffers);
122 void alphaTest(GLenum mode1, GLenum mode2);
123 void bindFramebuffer(GLuint fbo, GraphicsHelperInterface::FBOBindMode mode);
124 void bindBufferBase(GLenum target, GLuint bindingIndex, GLuint buffer);
125 void bindFragOutputs(GLuint shader, const QHash<QString, int> &outputs);
126 void bindImageTexture(GLuint imageUnit, GLuint texture, GLint mipLevel, GLboolean layered, GLint layer, GLenum access, GLenum format);
127 void bindUniformBlock(GLuint programId, GLuint uniformBlockIndex, GLuint uniformBlockBinding);
128 void bindShaderStorageBlock(GLuint programId, GLuint shaderStorageBlockIndex, GLuint shaderStorageBlockBinding);
129 void blendEquation(GLenum mode);
130 void blendFunci(GLuint buf, GLenum sfactor, GLenum dfactor);
131 void blendFuncSeparatei(GLuint buf, GLenum sRGB, GLenum dRGB, GLenum sAlpha, GLenum dAlpha);
132 GLuint boundFrameBufferObject();
133 void buildUniformBuffer(const QVariant &v, const ShaderUniform &description, QByteArray &buffer);
134 void clearBufferf(GLint drawbuffer, const QVector4D &values);
135 void clearColor(const QColor &color);
136 void clearDepthValue(float depth);
137 void clearStencilValue(int stencil);
138 void depthRange(GLdouble nearValue, GLdouble farValue);
139 void depthMask(GLenum mode);
140 void depthTest(GLenum mode);
141 void disableClipPlane(int clipPlane);
142 void disablei(GLenum cap, GLuint index);
143 void disablePrimitiveRestart();
144 void dispatchCompute(int x, int y, int z);
145 char * mapBuffer(GLenum target, GLsizeiptr size);
146 GLboolean unmapBuffer(GLenum target);
147 void drawArrays(GLenum primitiveType, GLint first, GLsizei count);
148 void drawArraysIndirect(GLenum mode,void *indirect);
149 void drawArraysInstanced(GLenum primitiveType, GLint first, GLsizei count, GLsizei instances);
150 void drawArraysInstancedBaseInstance(GLenum primitiveType, GLint first, GLsizei count, GLsizei instances, GLsizei baseinstance);
151 void drawElements(GLenum primitiveType, GLsizei primitiveCount, GLint indexType, void * indices, GLint baseVertex);
152 void drawElementsIndirect(GLenum mode, GLenum type, void *indirect);
153 void drawElementsInstancedBaseVertexBaseInstance(GLenum primitiveType, GLsizei primitiveCount, GLint indexType, void * indices, GLsizei instances, GLint baseVertex, GLint baseInstance);
154 void enableClipPlane(int clipPlane);
155 void enablei(GLenum cap, GLuint index);
156 void enablePrimitiveRestart(int restartIndex);
157 void frontFace(GLenum mode);
158 GLint maxClipPlaneCount();
159 GLint maxTextureUnitsCount() const;
160 GLint maxImageUnitsCount() const;
161 void pointSize(bool programmable, GLfloat value);
162 void readBuffer(GLenum mode);
163 void drawBuffer(GLenum mode);
164 void drawBuffers(GLsizei n, const int *bufs);
165 void setMSAAEnabled(bool enabled);
166 void setAlphaCoverageEnabled(bool enabled);
167 void setClipPlane(int clipPlane, const QVector3D &normal, float distance);
168 void setSeamlessCubemap(bool enable);
169 void setVerticesPerPatch(GLint verticesPerPatch);
170 void memoryBarrier(QMemoryBarrier::Operations barriers);
171 void activateDrawBuffers(const AttachmentPack &attachments);
172 void rasterMode(GLenum faceMode, GLenum rasterMode);
173
174 // Helper methods
175 static GLint elementType(GLint type);
176 static GLint tupleSizeFromType(GLint type);
177 static GLuint byteSizeFromType(GLint type);
178 static GLint glDataTypeFromAttributeDataType(QAttribute::VertexBaseType dataType);
179
180 bool supportsDrawBuffersBlend() const;
181 bool supportsVAO() const { return m_supportsVAO; }
182
183 void initialize();
184 void initializeHelpers(QSurface *surface);
185 GraphicsHelperInterface *resolveHighestOpenGLFunctions();
186
187 bool m_initialized;
188 bool m_supportsVAO;
189 GLint m_maxTextureUnits;
190 GLint m_maxImageUnits;
191 GLuint m_defaultFBO;
192 QOpenGLContext *m_gl;
193 GraphicsHelperInterface *m_glHelper;
194
195 QHash<QSurface *, GraphicsHelperInterface*> m_glHelpers;
196 GraphicsApiFilterData m_contextInfo;
197 ShaderCache *m_shaderCache;
198 QScopedPointer<QOpenGLDebugLogger> m_debugLogger;
199
200 friend class OpenGLVertexArrayObject;
201 OpenGLVertexArrayObject *m_currentVAO;
202
203 void applyUniform(const ShaderUniform &description, const UniformValue &v);
204
205 template<UniformType>
206 void applyUniformHelper(const ShaderUniform &, const UniformValue &) const
207 {
208 Q_ASSERT_X(false, Q_FUNC_INFO, "Uniform: Didn't provide specialized apply() implementation");
209 }
210};
211
212#define QT3D_UNIFORM_TYPE_PROTO(UniformTypeEnum, BaseType, Func) \
213template<> \
214void GraphicsContext::applyUniformHelper<UniformTypeEnum>(const ShaderUniform &description, const UniformValue &value) const;
215
216#define QT3D_UNIFORM_TYPE_IMPL(UniformTypeEnum, BaseType, Func) \
217 template<> \
218 void GraphicsContext::applyUniformHelper<UniformTypeEnum>(const ShaderUniform &description, const UniformValue &value) const \
219{ \
220 const int count = qMin(description.m_size, int(value.byteSize() / description.m_rawByteSize)); \
221 m_glHelper->Func(description.m_location, count, value.constData<BaseType>()); \
222}
223
224
225QT3D_UNIFORM_TYPE_PROTO(UniformType::Float, float, glUniform1fv)
226QT3D_UNIFORM_TYPE_PROTO(UniformType::Vec2, float, glUniform2fv)
227QT3D_UNIFORM_TYPE_PROTO(UniformType::Vec3, float, glUniform3fv)
228QT3D_UNIFORM_TYPE_PROTO(UniformType::Vec4, float, glUniform4fv)
229
230// OpenGL expects int* as values for booleans
231QT3D_UNIFORM_TYPE_PROTO(UniformType::Bool, int, glUniform1iv)
232QT3D_UNIFORM_TYPE_PROTO(UniformType::BVec2, int, glUniform2iv)
233QT3D_UNIFORM_TYPE_PROTO(UniformType::BVec3, int, glUniform3iv)
234QT3D_UNIFORM_TYPE_PROTO(UniformType::BVec4, int, glUniform4iv)
235
236QT3D_UNIFORM_TYPE_PROTO(UniformType::Int, int, glUniform1iv)
237QT3D_UNIFORM_TYPE_PROTO(UniformType::IVec2, int, glUniform2iv)
238QT3D_UNIFORM_TYPE_PROTO(UniformType::IVec3, int, glUniform3iv)
239QT3D_UNIFORM_TYPE_PROTO(UniformType::IVec4, int, glUniform4iv)
240
241QT3D_UNIFORM_TYPE_PROTO(UniformType::UInt, uint, glUniform1uiv)
242QT3D_UNIFORM_TYPE_PROTO(UniformType::UIVec2, uint, glUniform2uiv)
243QT3D_UNIFORM_TYPE_PROTO(UniformType::UIVec3, uint, glUniform3uiv)
244QT3D_UNIFORM_TYPE_PROTO(UniformType::UIVec4, uint, glUniform4uiv)
245
246QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat2, float, glUniformMatrix2fv)
247QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat3, float, glUniformMatrix3fv)
248QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat4, float, glUniformMatrix4fv)
249QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat2x3, float, glUniformMatrix2x3fv)
250QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat3x2, float, glUniformMatrix3x2fv)
251QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat2x4, float, glUniformMatrix2x4fv)
252QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat4x2, float, glUniformMatrix4x2fv)
253QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat3x4, float, glUniformMatrix3x4fv)
254QT3D_UNIFORM_TYPE_PROTO(UniformType::Mat4x3, float, glUniformMatrix4x3fv)
255
256} // namespace Render
257} // namespace Qt3DRender
258
259QT_END_NAMESPACE
260
261#endif // QT3DRENDER_RENDER_GRAPHICSCONTEXT_H
262