Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qopenglbuffer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtGui/qopengl.h>
5#include <QtGui/private/qopenglcontext_p.h>
6#include <QtCore/qatomic.h>
7#include "qopenglbuffer.h"
8#include <private/qopenglextensions_p.h>
9
10#ifndef GL_CONTEXT_LOST
11#define GL_CONTEXT_LOST 0x0507
12#endif
13
15
124
134 : d_ptr(new QOpenGLBufferPrivate(QOpenGLBuffer::VertexBuffer))
135{
136}
137
150
158 : d_ptr(other.d_ptr)
159{
160 d_ptr->ref.ref();
161}
162
168{
169 if (d_ptr && !d_ptr->ref.deref()) {
170 destroy();
171 delete d_ptr;
172 }
173}
174
182{
183 if (d_ptr != other.d_ptr) {
184 other.d_ptr->ref.ref();
185 if (d_ptr && !d_ptr->ref.deref()) {
186 destroy();
187 delete d_ptr;
188 }
189 d_ptr = other.d_ptr;
190 }
191 return *this;
192}
193
228{
229 Q_D(const QOpenGLBuffer);
230 return d->type;
231}
232
240{
241 Q_D(const QOpenGLBuffer);
242 return d->usagePattern;
243}
244
252{
253 Q_D(QOpenGLBuffer);
254 d->usagePattern = d->actualUsagePattern = value;
255}
256
257namespace {
258 void freeBufferFunc(QOpenGLFunctions *funcs, GLuint id)
259 {
260 funcs->glDeleteBuffers(1, &id);
261 }
262}
263
278{
279 Q_D(QOpenGLBuffer);
280 if (d->guard && d->guard->id())
281 return true;
283 if (ctx) {
284 delete d->funcs;
285 d->funcs = new QOpenGLExtensions(ctx);
286 GLuint bufferId = 0;
287 d->funcs->glGenBuffers(1, &bufferId);
288 if (bufferId) {
289 if (d->guard)
290 d->guard->free();
291
292 d->guard = new QOpenGLSharedResourceGuard(ctx, bufferId, freeBufferFunc);
293 return true;
294 }
295 }
296 return false;
297}
298
305{
306 Q_D(const QOpenGLBuffer);
307 return d->guard && d->guard->id();
308}
309
316{
317 Q_D(QOpenGLBuffer);
318 if (d->guard) {
319 d->guard->free();
320 d->guard = nullptr;
321 }
322 delete d->funcs;
323 d->funcs = nullptr;
324}
325
337{
338#if !QT_CONFIG(opengles2)
339 Q_D(QOpenGLBuffer);
340 if (!d->funcs->hasOpenGLFeature(QOpenGLFunctions::Buffers) || !d->guard->id())
341 return false;
342
343 while (true) { // Clear error state.
344 GLenum error = d->funcs->glGetError();
345 if (error == GL_NO_ERROR)
346 break;
347 if (error == GL_CONTEXT_LOST)
348 return false;
349 };
350 d->funcs->glGetBufferSubData(d->type, offset, count, data);
351 return d->funcs->glGetError() == GL_NO_ERROR;
352#else
354 Q_UNUSED(data);
356 return false;
357#endif
358}
359
370void QOpenGLBuffer::write(int offset, const void *data, int count)
371{
372#ifndef QT_NO_DEBUG
373 if (!isCreated())
374 qWarning("QOpenGLBuffer::write(): buffer not created");
375#endif
376 Q_D(QOpenGLBuffer);
377 if (d->guard && d->guard->id())
378 d->funcs->glBufferSubData(d->type, offset, count, data);
379}
380
390void QOpenGLBuffer::allocate(const void *data, int count)
391{
392#ifndef QT_NO_DEBUG
393 if (!isCreated())
394 qWarning("QOpenGLBuffer::allocate(): buffer not created");
395#endif
396 Q_D(QOpenGLBuffer);
397 if (d->guard && d->guard->id())
398 d->funcs->glBufferData(d->type, count, data, d->actualUsagePattern);
399}
400
426{
427#ifndef QT_NO_DEBUG
428 if (!isCreated())
429 qWarning("QOpenGLBuffer::bind(): buffer not created");
430#endif
431 Q_D(const QOpenGLBuffer);
432 GLuint bufferId = d->guard ? d->guard->id() : 0;
433 if (bufferId) {
434 if (d->guard->group() != QOpenGLContextGroup::currentContextGroup()) {
435#ifndef QT_NO_DEBUG
436 qWarning("QOpenGLBuffer::bind: buffer is not valid in the current context");
437#endif
438 return false;
439 }
440 d->funcs->glBindBuffer(d->type, bufferId);
441 return true;
442 } else {
443 return false;
444 }
445}
446
457{
458#ifndef QT_NO_DEBUG
459 if (!isCreated())
460 qWarning("QOpenGLBuffer::release(): buffer not created");
461#endif
462 Q_D(const QOpenGLBuffer);
463 if (d->guard && d->guard->id())
464 d->funcs->glBindBuffer(d->type, 0);
465}
466
479{
481 if (ctx)
482 ctx->functions()->glBindBuffer(GLenum(type), 0);
483}
484
492{
493 Q_D(const QOpenGLBuffer);
494 return d->guard ? d->guard->id() : 0;
495}
496
507{
508 Q_D(const QOpenGLBuffer);
509 if (!d->guard || !d->guard->id())
510 return -1;
511 GLint value = -1;
512 d->funcs->glGetBufferParameteriv(d->type, GL_BUFFER_SIZE, &value);
513 return value;
514}
515
535{
536 Q_D(QOpenGLBuffer);
537#ifndef QT_NO_DEBUG
538 if (!isCreated())
539 qWarning("QOpenGLBuffer::map(): buffer not created");
540#endif
541 if (!d->guard || !d->guard->id())
542 return nullptr;
543 if (d->funcs->hasOpenGLExtension(QOpenGLExtensions::MapBufferRange)) {
544 QOpenGLBuffer::RangeAccessFlags rangeAccess;
545 switch (access) {
547 rangeAccess = QOpenGLBuffer::RangeRead;
548 break;
550 rangeAccess = QOpenGLBuffer::RangeWrite;
551 break;
554 break;
555 }
556 return d->funcs->glMapBufferRange(d->type, 0, size(), rangeAccess);
557 } else {
558 return d->funcs->glMapBuffer(d->type, access);
559 }
560}
561
575void *QOpenGLBuffer::mapRange(int offset, int count, QOpenGLBuffer::RangeAccessFlags access)
576{
577 Q_D(QOpenGLBuffer);
578#ifndef QT_NO_DEBUG
579 if (!isCreated())
580 qWarning("QOpenGLBuffer::mapRange(): buffer not created");
581#endif
582 if (!d->guard || !d->guard->id())
583 return nullptr;
584 return d->funcs->glMapBufferRange(d->type, offset, count, access);
585}
586
601{
602 Q_D(QOpenGLBuffer);
603#ifndef QT_NO_DEBUG
604 if (!isCreated())
605 qWarning("QOpenGLBuffer::unmap(): buffer not created");
606#endif
607 if (!d->guard || !d->guard->id())
608 return false;
609 return d->funcs->glUnmapBuffer(d->type) == GL_TRUE;
610}
611
\inmodule QtCore
Definition qatomic.h:112
bool ref() noexcept
bool deref() noexcept
QOpenGLBuffer::UsagePattern usagePattern
QOpenGLBuffer::UsagePattern actualUsagePattern
QOpenGLBuffer::Type type
QOpenGLSharedResourceGuard * guard
QOpenGLBufferPrivate(QOpenGLBuffer::Type t)
QOpenGLExtensions * funcs
The QOpenGLBuffer class provides functions for creating and managing OpenGL buffer objects.
bool bind()
Binds the buffer associated with this object to the current OpenGL context.
void write(int offset, const void *data, int count)
Replaces the count bytes of this buffer starting at offset with the contents of data.
bool unmap()
Unmaps the buffer after it was mapped into the application's memory space with a previous call to map...
bool read(int offset, void *data, int count)
Reads the count bytes in this buffer starting at offset into data.
GLuint bufferId() const
Returns the OpenGL identifier associated with this buffer; zero if the buffer has not been created.
bool create()
Creates the buffer object in the OpenGL server.
void * mapRange(int offset, int count, QOpenGLBuffer::RangeAccessFlags access)
Maps the range specified by offset and count of the contents of this buffer into the application's me...
Access
This enum defines the access mode for QOpenGLBuffer::map().
QOpenGLBuffer()
Constructs a new buffer object of type QOpenGLBuffer::VertexBuffer.
~QOpenGLBuffer()
Destroys this buffer object, including the storage being used in the OpenGL server.
bool isCreated() const
Returns true if this buffer has been created; false otherwise.
void allocate(const void *data, int count)
Allocates count bytes of space to the buffer, initialized to the contents of data.
void destroy()
Destroys this buffer object, including the storage being used in the OpenGL server.
void setUsagePattern(QOpenGLBuffer::UsagePattern value)
Sets the usage pattern for this buffer object to value.
QOpenGLBuffer::Type type() const
Returns the type of buffer represented by this object.
int size() const
Returns the size of the data in this buffer, for reading operations.
UsagePattern
This enum defines the usage pattern of a QOpenGLBuffer object.
QOpenGLBuffer & operator=(const QOpenGLBuffer &other)
Assigns a shallow copy of other to this object.
Type
This enum defines the type of OpenGL buffer object to create with QOpenGLBuffer.
void release()
Releases the buffer associated with this object from the current OpenGL context.
QOpenGLBuffer::UsagePattern usagePattern() const
Returns the usage pattern for this buffer object.
void * map(QOpenGLBuffer::Access access)
Maps the contents of this buffer into the application's memory space and returns a pointer to it.
static QOpenGLContextGroup * currentContextGroup()
Returns the QOpenGLContextGroup corresponding to the current context.
\inmodule QtGui
static QOpenGLContext * currentContext()
Returns the last context which called makeCurrent in the current thread, or \nullptr,...
The QOpenGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API.
The QOpenGLSharedResourceGuard class is a convenience sub-class of QOpenGLSharedResource to be used t...
EGLContext ctx
static VulkanServerBufferGlFunctions * funcs
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:166
#define GL_CONTEXT_LOST
Definition qopengl.cpp:30
typedef GLint(GL_APIENTRYP PFNGLGETPROGRAMRESOURCELOCATIONINDEXEXTPROC)(GLuint program
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLenum access
GLenum GLuint GLintptr offset
GLint ref
GLdouble GLdouble t
Definition qopenglext.h:243
#define GL_BUFFER_SIZE
Definition qopenglext.h:481
#define GLuint
#define Q_UNUSED(x)
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]