forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometryRenderer.cpp
276 lines (261 loc) · 8.66 KB
/
GeometryRenderer.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
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2017 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/GeometryRenderer.h"
#include "opengl/OpenGLHelper.h"
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#define QGLF(f) QOpenGLContext::currentContext()->functions()->f
#else
#define QGLF(f) QGLFunctions(NULL).f
#endif
namespace QtAV {
GeometryRenderer::GeometryRenderer()
: g(NULL)
, features_(kVBO|kIBO|kVAO|kMapBuffer)
, vbo_size(0)
, ibo_size(0)
, ibo(QOpenGLBuffer::IndexBuffer)
, stride(0)
{
static bool disable_ibo = qgetenv("QTAV_NO_IBO").toInt() > 0;
setFeature(kIBO, !disable_ibo);
static bool disable_vbo = qgetenv("QTAV_NO_VBO").toInt() > 0;
setFeature(kVBO, !disable_vbo);
static bool disable_vao = qgetenv("QTAV_NO_VAO").toInt() > 0;
setFeature(kVAO, !disable_vao);
}
void GeometryRenderer::setFeature(int f, bool on)
{
if (on)
features_ |= f;
else
features_ ^= f;
}
void GeometryRenderer::setFeatures(int value)
{
features_ = value;
}
int GeometryRenderer::features() const
{
return features_;
}
int GeometryRenderer::actualFeatures() const
{
int f = 0;
if (vbo.isCreated())
f |= kVBO;
if (ibo.isCreated())
f |= kIBO;
#if QT_VAO
if (vao.isCreated())
f |= kVAO;
#endif
return f;
}
bool GeometryRenderer::testFeatures(int value) const
{
return !!(features() & value);
}
void GeometryRenderer::updateGeometry(Geometry *geo)
{
g = geo;
if (!g) {
ibo.destroy();
vbo.destroy();
#if QT_VAO
vao.destroy();
#endif
vbo_size = 0;
ibo_size = 0;
return;
}
static int support_map = -1;
if (support_map < 0) {
static const char* ext[] = { "GL_OES_mapbuffer", NULL};
if (OpenGLHelper::isOpenGLES()) {
support_map = QOpenGLContext::currentContext()->format().majorVersion() > 2 ||
OpenGLHelper::hasExtension(ext);
} else {
support_map = 1;
}
}
if (testFeatures(kIBO) && !ibo.isCreated()) {
if (g->indexCount() > 0) {
qDebug("creating IBO...");
if (!ibo.create())
qDebug("IBO create error");
}
}
if (ibo.isCreated()) {
ibo.bind();
const int bs = g->indexDataSize();
if (bs == ibo_size) {
void * p = NULL;
if (support_map && testFeatures(kMapBuffer))
p = ibo.map(QOpenGLBuffer::WriteOnly);
if (p) {
memcpy(p, g->constIndexData(), bs);
ibo.unmap();
} else {
ibo.write(0, g->constIndexData(), bs);
}
} else {
ibo.allocate(g->indexData(), bs); // TODO: allocate NULL and then map or BufferSubData?
ibo_size = bs;
}
ibo.release();
}
if (testFeatures(kVBO) && !vbo.isCreated()) {
qDebug("creating VBO...");
if (!vbo.create())
qWarning("VBO create error");
}
if (vbo.isCreated()) {
vbo.bind();
const int bs = g->vertexCount()*g->stride();
/* Notes from https://www.opengl.org/sdk/docs/man/html/glBufferSubData.xhtml
When replacing the entire data store, consider using glBufferSubData rather than completely recreating the data store with glBufferData. This avoids the cost of reallocating the data store.
*/
if (bs == vbo_size) { // vbo.size() error 0x501 on rpi, and query gl value can be slow
void* p = NULL;
if (support_map && testFeatures(kMapBuffer))
p = vbo.map(QOpenGLBuffer::WriteOnly);
if (p) {
memcpy(p, g->constVertexData(), bs);
vbo.unmap();
} else {
vbo.write(0, g->constVertexData(), bs);
vbo_size = bs;
}
} else {
vbo.allocate(g->vertexData(), bs);
}
vbo.release();
}
#if QT_VAO
if (stride == g->stride() && attrib == g->attributes())
return;
stride = g->stride();
attrib = g->attributes();
if (testFeatures(kVAO) && !vao.isCreated()) {
qDebug("creating VAO...");
if (!vao.create())
qDebug("VAO create error");
}
qDebug("vao updated");
if (vao.isCreated()) // can not use vao binder because it will create a vao if necessary
vao.bind();
// can set data before vao bind
if (!vao.isCreated())
return;
qDebug("geometry attributes changed, rebind vao...");
// call once is enough if no feature and no geometry attribute is changed
if (vbo.isCreated()) {
vbo.bind();
for (int an = 0; an < g->attributes().size(); ++an) {
// FIXME: assume bind order is 0,1,2...
const Attribute& a = g->attributes().at(an);
QGLF(glVertexAttribPointer(an, a.tupleSize(), a.type(), a.normalize(), g->stride(), reinterpret_cast<const void *>(qptrdiff(a.offset())))); //TODO: in setActiveShader
QGLF(glEnableVertexAttribArray(an));
}
vbo.release(); // unbind after vao unbind? http://www.zwqxin.com/archives/opengl/vao-and-vbo-stuff.html
} // TODO: bind pointers if vbo is disabled
// bind ibo to vao thus no bind is required later
if (ibo.isCreated())// if not bind here, glDrawElements(...,NULL) crashes and must use ibo data ptr, why?
ibo.bind();
vao.release();
if (ibo.isCreated())
ibo.release();
#endif
qDebug("geometry updated");
}
void GeometryRenderer::bindBuffers()
{
bool bind_vbo = vbo.isCreated();
bool bind_ibo = ibo.isCreated();
bool setv_skip = false;
#if QT_VAO
if (vao.isCreated()) {
vao.bind(); // vbo, ibo is ok now
setv_skip = bind_vbo;
bind_vbo = false;
bind_ibo = false;
}
#endif
//qDebug("bind ibo: %d vbo: %d; set v: %d", bind_ibo, bind_vbo, !setv_skip);
if (bind_ibo)
ibo.bind();
// no vbo: set vertex attributes
// has vbo, no vao: bind vbo & set vertex attributes
// has vbo, has vao: skip
if (setv_skip)
return;
if (!g)
return;
const char* vdata = static_cast<const char*>(g->vertexData());
if (bind_vbo) {
vbo.bind();
vdata = NULL;
}
for (int an = 0; an < g->attributes().size(); ++an) {
const Attribute& a = g->attributes().at(an);
QGLF(glVertexAttribPointer(an, a.tupleSize(), a.type(), a.normalize(), g->stride(), vdata + a.offset()));
QGLF(glEnableVertexAttribArray(an)); //TODO: in setActiveShader
}
}
void GeometryRenderer::unbindBuffers()
{
bool unbind_vbo = vbo.isCreated();
bool unbind_ibo = ibo.isCreated();
bool unsetv_skip = false;
#if QT_VAO
if (vao.isCreated()) {
vao.release();
unsetv_skip = unbind_vbo;
unbind_vbo = false;
unbind_ibo = false;
}
#endif //QT_VAO
//qDebug("unbind ibo: %d vbo: %d; unset v: %d", unbind_ibo, unbind_vbo, !unsetv_skip);
if (unbind_ibo)
ibo.release();
// release vbo. qpainter is affected if vbo is bound
if (unbind_vbo)
vbo.release();
// no vbo: disable vertex attributes
// has vbo, no vao: unbind vbo & set vertex attributes
// has vbo, has vao: skip
if (unsetv_skip)
return;
if (!g)
return;
for (int an = 0; an < g->attributes().size(); ++an) {
QGLF(glDisableVertexAttribArray(an));
}
}
void GeometryRenderer::render()
{
if (!g)
return;
bindBuffers();
if (g->indexCount() > 0) {
DYGL(glDrawElements(g->primitive(), g->indexCount(), g->indexType(), ibo.isCreated() ? NULL : g->indexData())); // null: data in vao or ibo. not null: data in memory
} else {
DYGL(glDrawArrays(g->primitive(), 0, g->vertexCount()));
}
unbindBuffers();
}
} //namespace QtAV