1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCanvas3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "program3d_p.h"
41
42QT_BEGIN_NAMESPACE
43QT_CANVAS3D_BEGIN_NAMESPACE
44
45/*!
46 * \qmltype Canvas3DProgram
47 * \since QtCanvas3D 1.0
48 * \inqmlmodule QtCanvas3D
49 * \inherits Canvas3DAbstractObject
50 * \brief Contains a shader program.
51 * \deprecated
52 *
53 * \b{Deprecated in Qt 5.12.}
54 * An uncreatable QML type that contains a compiled shader program. You can get it by calling
55 * the \l{Context3D::createProgram()}{Context3D.createProgram()} method.
56 */
57
58CanvasProgram::CanvasProgram(CanvasGlCommandQueue *queue, QObject *parent) :
59 CanvasAbstractObject(queue, parent),
60 m_programId(queue->createResourceId()),
61 m_linked(false)
62{
63 queueCommand(id: CanvasGlCommandQueue::glCreateProgram, p1: m_programId);
64}
65
66CanvasProgram::~CanvasProgram()
67{
68 del();
69}
70
71bool CanvasProgram::isAlive()
72{
73 return bool(m_programId);
74}
75
76void CanvasProgram::attach(CanvasShader *shader)
77{
78 if (m_programId) {
79 if (m_attachedShaders.count(t: shader) == 0) {
80 m_attachedShaders.append(t: shader);
81 queueCommand(id: CanvasGlCommandQueue::glAttachShader, p1: m_programId, p2: shader->id());
82 }
83 }
84}
85
86void CanvasProgram::detach(CanvasShader *shader)
87{
88 if (m_programId) {
89 if (m_attachedShaders.count(t: shader) > 0) {
90 m_attachedShaders.removeOne(t: shader);
91 queueCommand(id: CanvasGlCommandQueue::glDetachShader, p1: m_programId, p2: shader->id());
92 }
93 }
94}
95
96const QList<CanvasShader *> &CanvasProgram::attachedShaders() const
97{
98 return m_attachedShaders;
99}
100
101void CanvasProgram::link()
102{
103 if (m_programId) {
104 queueCommand(id: CanvasGlCommandQueue::glLinkProgram, p1: m_programId);
105 m_linked = true;
106 }
107}
108
109bool CanvasProgram::isLinked()
110{
111 // This method reports true if linking has been attempted for this program.
112 // We don't know if linking will be successful.
113 return m_linked;
114}
115
116void CanvasProgram::useProgram()
117{
118 if (m_programId)
119 queueCommand(id: CanvasGlCommandQueue::glUseProgram, p1: m_programId);
120}
121
122void CanvasProgram::bindAttributeLocation(int index, const QString &name)
123{
124 if (m_programId) {
125 queueCommand(id: CanvasGlCommandQueue::glBindAttribLocation, data: new QByteArray(name.toLatin1()),
126 p1: m_programId, p2: GLint(index));
127 }
128}
129
130void CanvasProgram::del()
131{
132 if (m_programId) {
133 queueCommand(id: CanvasGlCommandQueue::glDeleteProgram, p1: m_programId);
134 m_programId = 0;
135 }
136 m_attachedShaders.clear();
137}
138
139void CanvasProgram::validateProgram()
140{
141 if (m_programId)
142 queueCommand(id: CanvasGlCommandQueue::glValidateProgram, p1: m_programId);
143}
144
145GLint CanvasProgram::id()
146{
147 return m_programId;
148}
149
150QDebug operator<<(QDebug dbg, const CanvasProgram *program)
151{
152 if (program)
153 dbg.nospace() << "Canvas3DProgram("<< program->name() << ", id:" << program->m_programId << ")";
154 else
155 dbg.nospace() << "Canvas3DProgram("<< ((void*) program) <<")";
156 return dbg.maybeSpace();
157}
158
159QT_CANVAS3D_END_NAMESPACE
160QT_END_NAMESPACE
161

source code of qtcanvas3d/src/imports/qtcanvas3d/program3d.cpp