forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenGLTypes.cpp
398 lines (361 loc) · 11.7 KB
/
OpenGLTypes.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/******************************************************************************
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/OpenGLTypes.h"
#include "opengl/OpenGLHelper.h"
#include <QtCore/QRegExp>
#include <QtCore/QStringList>
#include "utils/Logger.h"
namespace QtAV {
struct uniform_type_name {
QByteArray name;
Uniform::Type type;
} uniform_type_names[] ={
{"sample2D", Uniform::Sampler},
{"bool", Uniform::Bool},
{"int", Uniform::Int},
{"uint", Uniform::Int},
{"float", Uniform::Float},
{"vec2", Uniform::Vec2},
{"vec3", Uniform::Vec3},
{"vec4", Uniform::Vec4},
{"mat2", Uniform::Mat2},
{"mat3", Uniform::Mat3},
{"mat4", Uniform::Mat4},
{"bvec2", Uniform::BVec2},
{"bvec3", Uniform::BVec3},
{"bvec4", Uniform::BVec4},
{"ivec2", Uniform::IVec2},
{"ivec3", Uniform::IVec3},
{"ivec4", Uniform::IVec4},
{"uvec2", Uniform::UVec2},
{"uvec3", Uniform::UVec3},
{"uvec4", Uniform::UVec4},
{"mat2x2", Uniform::Mat2},
{"mat3x3", Uniform::Mat3},
{"mat4x4", Uniform::Mat4},
{"dmat2", Uniform::DMat2},
{"dmat3", Uniform::DMat3},
{"dmat4", Uniform::DMat4},
};
static Uniform::Type UniformTypeFromName(const QByteArray& name)
{
for (const uniform_type_name* un = uniform_type_names; un < uniform_type_names + sizeof(uniform_type_names)/sizeof(uniform_type_names[0]); ++un) {
if (un->name == name)
return un->type;
}
return Uniform::Unknown;
}
static QByteArray UniformTypeToName(Uniform::Type ut)
{
for (const uniform_type_name* un = uniform_type_names; un < uniform_type_names + sizeof(uniform_type_names)/sizeof(uniform_type_names[0]); ++un) {
if (un->type == ut)
return un->name;
}
return "unknown";
}
Uniform::Uniform(Type tp, int count)
: dirty(true)
, location(-1)
, tuple_size(1)
, array_size(1)
, t(tp)
{
setType(tp, count);
}
Uniform& Uniform::setType(Type tp, int count)
{
t = tp;
array_size = count;
if (isVec()) {
tuple_size = (t >> (V+1)) & ((1<<3) - 1);
} else if (isMat()) {
tuple_size = (t >> (M+1)) & ((1<<3) - 1);
tuple_size *= tuple_size;
}
int element_size = sizeof(float);
if (isInt() || isUInt() || isBool()) {
element_size = sizeof(int);
}
data = QVector<int>(element_size/sizeof(int)*tupleSize()*arraySize());
return *this;
}
template<typename T> bool set_uniform_value(QVector<int>& dst, const T* v, int count)
{
Q_ASSERT(sizeof(T)*count <= sizeof(int)*dst.size() && "set_uniform_value: Bad type or array size");
// why not dst.constData()?
const QVector<int> old(dst);
memcpy((char*)dst.data(), (const char*)v, count*sizeof(T));
return old != dst;
}
template<> bool set_uniform_value<bool>(QVector<int>& dst, const bool* v, int count)
{
const QVector<int> old(dst);
for (int i = 0; i < count; ++i) {
dst[i] = *(v + i);
}
return old != dst;
}
void Uniform::set(const float &v, int count)
{
if (count <= 0)
count = tupleSize()*arraySize();
dirty = set_uniform_value(data, &v, count);
}
void Uniform::set(const int &v, int count)
{
if (count <= 0)
count = tupleSize()*arraySize();
dirty = set_uniform_value(data, &v, count);
}
void Uniform::set(const unsigned &v, int count)
{
if (count <= 0)
count = tupleSize()*arraySize();
dirty = set_uniform_value(data, &v, count);
}
void Uniform::set(const float *v, int count)
{
if (count <= 0)
count = tupleSize()*arraySize();
dirty = set_uniform_value(data, v, count);
}
void Uniform::set(const int *v, int count)
{
if (count <= 0)
count = tupleSize()*arraySize();
dirty = set_uniform_value(data, v, count);
}
void Uniform::set(const unsigned *v, int count)
{
if (count <= 0)
count = tupleSize()*arraySize();
dirty = set_uniform_value(data, v, count);
}
void Uniform::set(const QVariant &v)
{
if (tupleSize() > 1 || arraySize() > 1) {
if (isFloat()) { //TODO: what if QVector<qreal> but uniform is float?
set(v.value<QVector<float> >().data());
} else if (isInt() || isBool()) {
set(v.value<QVector<int> >().data());
} else if (isUInt()) {
set(v.value<QVector<unsigned> >().data());
} else if (type() == Uniform::Sampler) {
}
} else {
if (isFloat()) {
set(v.toFloat());
} else if (isInt() || isBool()) {
set(v.toInt());
} else if (isUInt()) {
set(v.toUInt());
} else if (type() == Uniform::Sampler) {
}
}
}
bool Uniform::setGL()
{
if (location < 0) {
return false;
}
switch (type()) {
case Uniform::Bool:
case Uniform::Int:
gl().Uniform1iv(location, arraySize(), address<int>());
break;
case Uniform::Float:
gl().Uniform1fv(location, arraySize(), address<float>());
break;
case Uniform::Vec2:
gl().Uniform2fv(location, arraySize(), address<float>());
break;
case Uniform::Vec3:
gl().Uniform3fv(location, arraySize(), address<float>());
break;
case Uniform::Vec4:
gl().Uniform4fv(location, arraySize(), address<float>());
break;
case Uniform::Mat2:
gl().UniformMatrix2fv(location, arraySize(), GL_FALSE, address<float>());
break;
case Uniform::Mat3:
gl().UniformMatrix3fv(location, arraySize(), GL_FALSE, address<float>());
break;
case Uniform::Mat4:
gl().UniformMatrix4fv(location, arraySize(), GL_FALSE, address<float>());
break;
case Uniform::IVec2:
gl().Uniform2iv(location, arraySize(), address<int>());
break;
case Uniform::IVec3:
gl().Uniform3iv(location, arraySize(), address<int>());
break;
case Uniform::IVec4:
gl().Uniform4iv(location, arraySize(), address<int>());
break;
default:
qDebug() << *this;
qWarning("Unsupported uniform type in Qt. You should use 'VideoShader::setUserUniformValues()' to call glUniformXXX or directly call glUniformXXX instead");
return false;
}
dirty = false;
return true;
}
#ifndef QT_NO_DEBUG_STREAM
Q_AV_EXPORT QDebug operator<<(QDebug dbg, const Uniform &u)
{
dbg.nospace() << "uniform " << UniformTypeToName(u.type()) << " " << u.name.constData();
if (u.arraySize() > 1) {
dbg.nospace() << "[" << u.arraySize() << "]";
}
dbg.nospace() << ", dirty: " << u.dirty;
dbg.nospace() << ", location: " << u.location << ", " << "tupleSize: " << u.tupleSize() << ", ";
if (u.isBool() || u.isInt()) {
dbg.nospace() << "value: " << u.value<int>();
} else if (u.isUInt()) {
dbg.nospace() << "value: " << u.value<unsigned>();
} else if (u.isDouble()) {
dbg.nospace() << "value: " << u.value<double>();
} else {
dbg.nospace() << "value: " << u.value<float>();
}
return dbg.space();
}
Q_AV_EXPORT QDebug operator<<(QDebug dbg, Uniform::Type ut);
#endif
QVector<Uniform> ParseUniforms(const QByteArray &text, GLuint programId = 0)
{
QVector<Uniform> uniforms;
const QString code = OpenGLHelper::removeComments(QString(text));
const QStringList lines = code.split(';');
// TODO: highp lowp etc.
const QString exp(QStringLiteral("\\s*uniform\\s+([\\w\\d]+)\\s+([\\w\\d]+)\\s*"));
const QString exp_array = exp + QStringLiteral("\\[(\\d+)\\]\\s*");
foreach (QString line, lines) {
line = line.trimmed();
if (!line.startsWith(QStringLiteral("uniform ")))
continue;
QRegExp rx(exp_array);
if (rx.indexIn(line) < 0) {
rx = QRegExp(exp);
if (rx.indexIn(line) < 0)
continue;
}
Uniform u;
const QStringList x = rx.capturedTexts();
//qDebug() << x;
u.name = x.at(2).toUtf8();
int array_size = 1;
if (x.size() > 3)
array_size = x[3].toInt();
const QByteArray t(x[1].toLatin1());
u.setType(UniformTypeFromName(t), array_size);
if (programId > 0)
u.location = gl().GetUniformLocation(programId, u.name.constData());
uniforms.append(u);
}
qDebug() << uniforms;
return uniforms;
}
TexturedGeometry::TexturedGeometry(int texCount, int count, Triangle t)
: tri(t)
, points_per_tex(count)
, nb_tex(texCount)
{
if (texCount < 1)
texCount = 1;
v.resize(nb_tex*points_per_tex);
}
void TexturedGeometry::setTextureCount(int value)
{
if (value < 1)
value = 1;
if (value == nb_tex)
return;
nb_tex = value;
v.resize(nb_tex*points_per_tex);
}
int TexturedGeometry::textureCount() const
{
return nb_tex;
}
int TexturedGeometry::size() const
{
return nb_tex * textureSize();
}
int TexturedGeometry::textureSize() const
{
return textureVertexCount() * stride();
}
int TexturedGeometry::mode() const
{
if (tri == Strip)
return GL_TRIANGLE_STRIP;
return GL_TRIANGLE_FAN;
}
void TexturedGeometry::setPoint(int index, const QPointF &p, const QPointF &tp, int texIndex)
{
setGeometryPoint(index, p, texIndex);
setTexturePoint(index, tp, texIndex);
}
void TexturedGeometry::setGeometryPoint(int index, const QPointF &p, int texIndex)
{
v[texIndex*points_per_tex + index].x = p.x();
v[texIndex*points_per_tex + index].y = p.y();
}
void TexturedGeometry::setTexturePoint(int index, const QPointF &tp, int texIndex)
{
v[texIndex*points_per_tex + index].tx = tp.x();
v[texIndex*points_per_tex + index].ty = tp.y();
}
void TexturedGeometry::setRect(const QRectF &r, const QRectF &tr, int texIndex)
{
setPoint(0, r.topLeft(), tr.topLeft(), texIndex);
setPoint(1, r.bottomLeft(), tr.bottomLeft(), texIndex);
if (tri == Strip) {
setPoint(2, r.topRight(), tr.topRight(), texIndex);
setPoint(3, r.bottomRight(), tr.bottomRight(), texIndex);
} else {
setPoint(3, r.topRight(), tr.topRight(), texIndex);
setPoint(2, r.bottomRight(), tr.bottomRight(), texIndex);
}
}
void TexturedGeometry::setGeometryRect(const QRectF &r, int texIndex)
{
setGeometryPoint(0, r.topLeft(), texIndex);
setGeometryPoint(1, r.bottomLeft(), texIndex);
if (tri == Strip) {
setGeometryPoint(2, r.topRight(), texIndex);
setGeometryPoint(3, r.bottomRight(), texIndex);
} else {
setGeometryPoint(3, r.topRight(), texIndex);
setGeometryPoint(2, r.bottomRight(), texIndex);
}
}
void TexturedGeometry::setTextureRect(const QRectF &tr, int texIndex)
{
setTexturePoint(0, tr.topLeft(), texIndex);
setTexturePoint(1, tr.bottomLeft(), texIndex);
if (tri == Strip) {
setTexturePoint(2, tr.topRight(), texIndex);
setTexturePoint(3, tr.bottomRight(), texIndex);
} else {
setTexturePoint(3, tr.topRight(), texIndex);
setTexturePoint(2, tr.bottomRight(), texIndex);
}
}
} //namespace QtAV