forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoShaderObject.cpp
202 lines (175 loc) · 6.12 KB
/
VideoShaderObject.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2016)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/VideoShaderObject.h"
#include "QtAV/private/VideoShader_p.h"
#include <QtCore/QEvent>
#include <QtCore/QMetaProperty>
#include <QtCore/QSignalMapper>
namespace QtAV {
class VideoShaderObjectPrivate : public VideoShaderPrivate
{
public:
~VideoShaderObjectPrivate() {
qDeleteAll(sigMap[VertexShader]);
qDeleteAll(sigMap[FragmentShader]);
sigMap[VertexShader].clear();
sigMap[FragmentShader].clear();
}
QVector<QSignalMapper*> sigMap[ShaderTypeCount];
};
VideoShaderObject::VideoShaderObject(QObject *parent)
: QObject(parent)
, VideoShader(*new VideoShaderObjectPrivate())
{}
VideoShaderObject::VideoShaderObject(VideoShaderObjectPrivate &d, QObject *parent)
: QObject(parent)
, VideoShader(d)
{}
bool VideoShaderObject::event(QEvent *event)
{
DPTR_D(VideoShaderObject);
if (event->type() != QEvent::DynamicPropertyChange)
return QObject::event(event);
QDynamicPropertyChangeEvent *e = static_cast<QDynamicPropertyChangeEvent*>(event);
for (int shaderType = VertexShader; shaderType < ShaderTypeCount; ++shaderType) {
QVector<Uniform> &uniforms = d.user_uniforms[shaderType];
for (int i = 0; i < uniforms.size(); ++i) {
if (uniforms.at(i).name == e->propertyName()) {
propertyChanged(i|(shaderType<<16));
}
}
}
return QObject::event(event);
}
void VideoShaderObject::propertyChanged(int id)
{
DPTR_D(VideoShaderObject);
const int st = id>>16;
const int idx = id&0xffff;
Uniform &u = d.user_uniforms[st][idx];
const QVariant v = property(u.name.constData());
u.set(v);
//if (u.dirty) update();
}
void VideoShaderObject::programReady()
{
DPTR_D(VideoShaderObject);
// find property name. if has property, bind to property
for (int st = VertexShader; st < ShaderTypeCount; ++st) {
qDeleteAll(d.sigMap[st]);
d.sigMap[st].clear();
const QVector<Uniform> &uniforms = d.user_uniforms[st];
for (int i = 0; i < uniforms.size(); ++i) {
const Uniform& u = uniforms[i];
const int idx = metaObject()->indexOfProperty(u.name.constData());
if (idx < 0) {
qDebug("VideoShaderObject has no meta property '%s'. Setting initial value from dynamic property", u.name.constData());
const_cast<Uniform&>(u).set(property(u.name.constData()));
continue;
}
QMetaProperty mp = metaObject()->property(idx);
if (!mp.hasNotifySignal()) {
qWarning("VideoShaderObject property '%s' has no signal", mp.name());
continue;
}
QMetaMethod mm = mp.notifySignal();
QSignalMapper *mapper = new QSignalMapper();
mapper->setMapping(this, i|(st<<16));
#if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
connect(this, mm, mapper, mapper->metaObject()->method(mapper->metaObject()->indexOfSlot("map()")));
#else
#endif
connect(mapper, SIGNAL(mapped(int)), this, SLOT(propertyChanged(int)));
d.sigMap[st].append(mapper);
qDebug() << "set uniform property: " << u.name << property(u.name.constData());
propertyChanged(i|(st<<16)); // set the initial value
}
}
//ready();
}
class DynamicShaderObjectPrivate : public VideoShaderObjectPrivate {
public:
QString header;
QString sampleFunc;
QString pp;
};
DynamicShaderObject::DynamicShaderObject(QObject *parent)
: VideoShaderObject(*new DynamicShaderObjectPrivate(), parent)
{}
DynamicShaderObject::DynamicShaderObject(DynamicShaderObjectPrivate &d, QObject *parent)
: VideoShaderObject(d, parent)
{}
QString DynamicShaderObject::header() const
{
return d_func().header;
}
void DynamicShaderObject::setHeader(const QString &text)
{
DPTR_D(DynamicShaderObject);
if (d.header == text)
return;
d.header = text;
Q_EMIT headerChanged();
rebuildLater();
}
QString DynamicShaderObject::sample() const
{
return d_func().sampleFunc;
}
void DynamicShaderObject::setSample(const QString &text)
{
DPTR_D(DynamicShaderObject);
if (d.sampleFunc == text)
return;
d.sampleFunc = text;
Q_EMIT sampleChanged();
rebuildLater();
}
QString DynamicShaderObject::postProcess() const
{
return d_func().pp;
}
void DynamicShaderObject::setPostProcess(const QString &text)
{
DPTR_D(DynamicShaderObject);
if (d.pp == text)
return;
d.pp = text;
Q_EMIT postProcessChanged();
rebuildLater();
}
const char* DynamicShaderObject::userShaderHeader(QOpenGLShader::ShaderType st) const
{
if (st == QOpenGLShader::Vertex)
return 0;
if (d_func().header.isEmpty())
return 0;
return d_func().header.toUtf8().constData();
}
const char* DynamicShaderObject::userSample() const
{
if (d_func().sampleFunc.isEmpty())
return 0;
return d_func().sampleFunc.toUtf8().constData();
}
const char* DynamicShaderObject::userPostProcess() const
{
if (d_func().pp.isEmpty())
return 0;
return d_func().pp.toUtf8().constData();
}
} //namespace QtAV