summaryrefslogtreecommitdiffstats
path: root/src/render/backend/qgraphicscontext.h
blob: f79695d7bf22347b2aedd172c5df7318a823927e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QGRAPHICSCONTEXT_H
#define QGRAPHICSCONTEXT_H

#include <QOpenGLContext>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QHash>
#include <QMatrix4x4>
#include <QBitArray>

#include <rendercamera.h>
#include <meshdata.h>

class QOpenGLShaderProgram;
class QOpenGLFunctions_3_3_Core;

namespace Qt3D
{

class RenderShader;
class RenderCamera;
class RenderMaterial;
class RenderTexture;
class DrawStateSet;

enum TextureScope
{
    TextureScopeMaterial = 0,
    TextureScopeTechnique
    // per-pass for deferred rendering?
};

class QGraphicsContext
{
public:
    QGraphicsContext();
    ~QGraphicsContext();

    void setSurface(QSurface* s);

    int id() const; // unique, small integer ID of this context

    void beginDrawing();

    void endDrawing();

    void setCamera(RenderCamera* rcam);
    RenderCamera* camera() const;

    QMatrix4x4 projectionMatrix() const;
    QMatrix4x4 viewMatrix() const;

    /**
     * @brief releaseGL - release all OpenGL objects associated with
     * this context
     */
    void releaseOpenGL();
    void setOpenGLContext(QOpenGLContext* ctx);
    QOpenGLContext *openGLContext() { return m_gl; }

    void activateShader(RenderShader* shader);

    RenderMaterial* activeMaterial() const
    { return m_material; }

    void setActiveMaterial(RenderMaterial* rmat);

    /**
     * @brief activeShader
     * @return
     */
    QOpenGLShaderProgram* activeShader();

    void specifyAttribute(QString nm, AttributePtr attr);

    void specifyIndices(AttributePtr attr);

    /**
     * @brief glBufferFor - given a client-side (CPU) buffer, provide the
     * context-specific object. Initial call will create the buffer.
     * @param buf
     * @return
     */
    QOpenGLBuffer glBufferFor(BufferPtr buf);

    void setModelMatrix(const QMatrix4x4 &modelMat);

    /**
     * @brief activateTexture - make a texture active on a hardware unit
     * @param tex - the texture to activate
     * @param onUnit - option, specify the explicit unit to activate on
     * @return - the unit the texture was activated on
     */
    int activateTexture(TextureScope scope, RenderTexture* tex, int onUnit = -1);

    void deactivateTexture(RenderTexture *tex);

    void setCurrentStateSet(DrawStateSet* ss);
    DrawStateSet* currentStateSet() const;
private:
    void initialize();

    void decayTextureScores();

    GLint assignUnitForTexture(RenderTexture* tex);
    void deactivateTexturesWithScope(TextureScope ts);


    bool m_initialized;
    const unsigned int m_id;
    QOpenGLContext* m_gl;
    QSurface* m_surface;

    QOpenGLFunctions_3_3_Core *m_funcs;
    RenderShader* m_activeShader;
    QHash<RenderShader*, QOpenGLShaderProgram*> m_shaderHash;
    QHash<BufferPtr,QOpenGLBuffer> m_bufferHash;

    // active textures, indexed by texture unit
    QVector<RenderTexture*> m_activeTextures;
    QBitArray m_pinnedTextureUnits;
    QVector<TextureScope> m_textureScopes;

    // recency score for all render-textures we've seen. Higher scores
    // mean more recently used.
    QHash<RenderTexture*, int> m_textureScores;

    RenderCamera* m_camera;
    RenderMaterial* m_material;

    DrawStateSet* m_stateSet;
};

}

#endif // QGRAPHICSCONTEXT_H