2011-08-22 08:49:28 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-08-22 08:49:28 +00:00
|
|
|
**
|
2020-01-22 15:33:23 +00:00
|
|
|
** This file is part of the QtOpenGL module of the Qt Toolkit.
|
2011-08-22 08:49:28 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-08-22 08:49:28 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
2011-08-22 08:49:28 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-08-22 08:49:28 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtGui/qopengl.h>
|
|
|
|
#include <QtGui/private/qopenglcontext_p.h>
|
|
|
|
#include <QtCore/qatomic.h>
|
|
|
|
#include "qopenglbuffer.h"
|
|
|
|
#include <private/qopenglextensions_p.h>
|
|
|
|
|
2018-09-18 11:38:14 +00:00
|
|
|
#ifndef GL_CONTEXT_LOST
|
|
|
|
#define GL_CONTEXT_LOST 0x0507
|
|
|
|
#endif
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QOpenGLBuffer
|
2012-02-21 12:24:39 +00:00
|
|
|
\brief The QOpenGLBuffer class provides functions for creating and managing OpenGL buffer objects.
|
2011-09-02 10:32:20 +00:00
|
|
|
\since 5.0
|
2011-08-22 08:49:28 +00:00
|
|
|
\ingroup painting-3D
|
2020-01-22 15:33:23 +00:00
|
|
|
\inmodule QtOpenGL
|
2011-08-22 08:49:28 +00:00
|
|
|
|
2012-02-21 12:24:39 +00:00
|
|
|
Buffer objects are created in the OpenGL server so that the
|
2011-08-22 08:49:28 +00:00
|
|
|
client application can avoid uploading vertices, indices,
|
|
|
|
texture image data, etc every time they are needed.
|
|
|
|
|
|
|
|
QOpenGLBuffer objects can be copied around as a reference to the
|
2012-02-21 12:24:39 +00:00
|
|
|
underlying OpenGL buffer object:
|
2011-08-22 08:49:28 +00:00
|
|
|
|
2018-09-03 12:02:13 +00:00
|
|
|
\snippet code/src_gui_opengl_qopenglbuffer.cpp 0
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
QOpenGLBuffer performs a shallow copy when objects are copied in this
|
|
|
|
manner, but does not implement copy-on-write semantics. The original
|
|
|
|
object will be affected whenever the copy is modified.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\enum QOpenGLBuffer::Type
|
2012-02-21 12:24:39 +00:00
|
|
|
This enum defines the type of OpenGL buffer object to create with QOpenGLBuffer.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
\value VertexBuffer Vertex buffer object for use when specifying
|
|
|
|
vertex arrays.
|
|
|
|
\value IndexBuffer Index buffer object for use with \c{glDrawElements()}.
|
|
|
|
\value PixelPackBuffer Pixel pack buffer object for reading pixel
|
2012-02-21 12:24:39 +00:00
|
|
|
data from the OpenGL server (for example, with \c{glReadPixels()}).
|
2011-08-22 08:49:28 +00:00
|
|
|
Not supported under OpenGL/ES.
|
|
|
|
\value PixelUnpackBuffer Pixel unpack buffer object for writing pixel
|
2012-02-21 12:24:39 +00:00
|
|
|
data to the OpenGL server (for example, with \c{glTexImage2D()}).
|
2011-08-22 08:49:28 +00:00
|
|
|
Not supported under OpenGL/ES.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\enum QOpenGLBuffer::UsagePattern
|
|
|
|
This enum defines the usage pattern of a QOpenGLBuffer object.
|
|
|
|
|
|
|
|
\value StreamDraw The data will be set once and used a few times
|
|
|
|
for drawing operations. Under OpenGL/ES 1.1 this is identical
|
|
|
|
to StaticDraw.
|
|
|
|
\value StreamRead The data will be set once and used a few times
|
2012-02-21 12:24:39 +00:00
|
|
|
for reading data back from the OpenGL server. Not supported
|
2011-08-22 08:49:28 +00:00
|
|
|
under OpenGL/ES.
|
|
|
|
\value StreamCopy The data will be set once and used a few times
|
2012-02-21 12:24:39 +00:00
|
|
|
for reading data back from the OpenGL server for use in further
|
2011-08-22 08:49:28 +00:00
|
|
|
drawing operations. Not supported under OpenGL/ES.
|
|
|
|
\value StaticDraw The data will be set once and used many times
|
|
|
|
for drawing operations.
|
|
|
|
\value StaticRead The data will be set once and used many times
|
2012-02-21 12:24:39 +00:00
|
|
|
for reading data back from the OpenGL server. Not supported
|
2011-08-22 08:49:28 +00:00
|
|
|
under OpenGL/ES.
|
|
|
|
\value StaticCopy The data will be set once and used many times
|
2012-02-21 12:24:39 +00:00
|
|
|
for reading data back from the OpenGL server for use in further
|
2011-08-22 08:49:28 +00:00
|
|
|
drawing operations. Not supported under OpenGL/ES.
|
|
|
|
\value DynamicDraw The data will be modified repeatedly and used
|
|
|
|
many times for drawing operations.
|
|
|
|
\value DynamicRead The data will be modified repeatedly and used
|
2012-02-21 12:24:39 +00:00
|
|
|
many times for reading data back from the OpenGL server.
|
2011-08-22 08:49:28 +00:00
|
|
|
Not supported under OpenGL/ES.
|
|
|
|
\value DynamicCopy The data will be modified repeatedly and used
|
2012-02-21 12:24:39 +00:00
|
|
|
many times for reading data back from the OpenGL server for
|
2011-08-22 08:49:28 +00:00
|
|
|
use in further drawing operations. Not supported under OpenGL/ES.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\enum QOpenGLBuffer::Access
|
|
|
|
This enum defines the access mode for QOpenGLBuffer::map().
|
|
|
|
|
|
|
|
\value ReadOnly The buffer will be mapped for reading only.
|
|
|
|
\value WriteOnly The buffer will be mapped for writing only.
|
|
|
|
\value ReadWrite The buffer will be mapped for reading and writing.
|
|
|
|
*/
|
|
|
|
|
2014-06-13 14:36:27 +00:00
|
|
|
/*!
|
|
|
|
\enum QOpenGLBuffer::RangeAccessFlag
|
|
|
|
This enum defines the access mode bits for QOpenGLBuffer::mapRange().
|
|
|
|
|
|
|
|
\value RangeRead The buffer will be mapped for reading.
|
|
|
|
\value RangeWrite The buffer will be mapped for writing.
|
|
|
|
\value RangeInvalidate Discard the previous contents of the specified range.
|
|
|
|
\value RangeInvalidateBuffer Discard the previous contents of the entire buffer.
|
|
|
|
\value RangeFlushExplicit Indicates that modifications are to be flushed explicitly via \c glFlushMappedBufferRange.
|
|
|
|
\value RangeUnsynchronized Indicates that pending operations should not be synchronized before returning from mapRange().
|
|
|
|
*/
|
|
|
|
|
2011-08-22 08:49:28 +00:00
|
|
|
class QOpenGLBufferPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QOpenGLBufferPrivate(QOpenGLBuffer::Type t)
|
|
|
|
: ref(1),
|
|
|
|
type(t),
|
2019-11-22 13:46:58 +00:00
|
|
|
guard(nullptr),
|
2011-08-22 08:49:28 +00:00
|
|
|
usagePattern(QOpenGLBuffer::StaticDraw),
|
|
|
|
actualUsagePattern(QOpenGLBuffer::StaticDraw),
|
2019-11-22 13:46:58 +00:00
|
|
|
funcs(nullptr)
|
2011-08-22 08:49:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QAtomicInt ref;
|
|
|
|
QOpenGLBuffer::Type type;
|
|
|
|
QOpenGLSharedResourceGuard *guard;
|
|
|
|
QOpenGLBuffer::UsagePattern usagePattern;
|
|
|
|
QOpenGLBuffer::UsagePattern actualUsagePattern;
|
|
|
|
QOpenGLExtensions *funcs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a new buffer object of type QOpenGLBuffer::VertexBuffer.
|
|
|
|
|
|
|
|
Note: this constructor just creates the QOpenGLBuffer instance. The actual
|
2012-02-21 12:24:39 +00:00
|
|
|
buffer object in the OpenGL server is not created until create() is called.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
\sa create()
|
|
|
|
*/
|
|
|
|
QOpenGLBuffer::QOpenGLBuffer()
|
|
|
|
: d_ptr(new QOpenGLBufferPrivate(QOpenGLBuffer::VertexBuffer))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a new buffer object of \a type.
|
|
|
|
|
|
|
|
Note: this constructor just creates the QOpenGLBuffer instance. The actual
|
2012-02-21 12:24:39 +00:00
|
|
|
buffer object in the OpenGL server is not created until create() is called.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
\sa create()
|
|
|
|
*/
|
|
|
|
QOpenGLBuffer::QOpenGLBuffer(QOpenGLBuffer::Type type)
|
|
|
|
: d_ptr(new QOpenGLBufferPrivate(type))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a shallow copy of \a other.
|
|
|
|
|
|
|
|
Note: QOpenGLBuffer does not implement copy-on-write semantics,
|
|
|
|
so \a other will be affected whenever the copy is modified.
|
|
|
|
*/
|
|
|
|
QOpenGLBuffer::QOpenGLBuffer(const QOpenGLBuffer &other)
|
|
|
|
: d_ptr(other.d_ptr)
|
|
|
|
{
|
|
|
|
d_ptr->ref.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Destroys this buffer object, including the storage being
|
2012-02-21 12:24:39 +00:00
|
|
|
used in the OpenGL server.
|
2011-08-22 08:49:28 +00:00
|
|
|
*/
|
|
|
|
QOpenGLBuffer::~QOpenGLBuffer()
|
|
|
|
{
|
|
|
|
if (!d_ptr->ref.deref()) {
|
|
|
|
destroy();
|
|
|
|
delete d_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Assigns a shallow copy of \a other to this object.
|
|
|
|
|
|
|
|
Note: QOpenGLBuffer does not implement copy-on-write semantics,
|
|
|
|
so \a other will be affected whenever the copy is modified.
|
|
|
|
*/
|
|
|
|
QOpenGLBuffer &QOpenGLBuffer::operator=(const QOpenGLBuffer &other)
|
|
|
|
{
|
|
|
|
if (d_ptr != other.d_ptr) {
|
|
|
|
other.d_ptr->ref.ref();
|
2012-11-17 03:10:51 +00:00
|
|
|
if (!d_ptr->ref.deref()) {
|
2011-08-22 08:49:28 +00:00
|
|
|
destroy();
|
2012-11-17 03:10:51 +00:00
|
|
|
delete d_ptr;
|
|
|
|
}
|
2011-08-22 08:49:28 +00:00
|
|
|
d_ptr = other.d_ptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the type of buffer represented by this object.
|
|
|
|
*/
|
|
|
|
QOpenGLBuffer::Type QOpenGLBuffer::type() const
|
|
|
|
{
|
|
|
|
Q_D(const QOpenGLBuffer);
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the usage pattern for this buffer object.
|
|
|
|
The default value is StaticDraw.
|
|
|
|
|
|
|
|
\sa setUsagePattern()
|
|
|
|
*/
|
|
|
|
QOpenGLBuffer::UsagePattern QOpenGLBuffer::usagePattern() const
|
|
|
|
{
|
|
|
|
Q_D(const QOpenGLBuffer);
|
|
|
|
return d->usagePattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets the usage pattern for this buffer object to \a value.
|
|
|
|
This function must be called before allocate() or write().
|
|
|
|
|
|
|
|
\sa usagePattern(), allocate(), write()
|
|
|
|
*/
|
|
|
|
void QOpenGLBuffer::setUsagePattern(QOpenGLBuffer::UsagePattern value)
|
|
|
|
{
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
d->usagePattern = d->actualUsagePattern = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void freeBufferFunc(QOpenGLFunctions *funcs, GLuint id)
|
|
|
|
{
|
|
|
|
funcs->glDeleteBuffers(1, &id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Creates the buffer object in the OpenGL server. Returns \c true if
|
2011-08-22 08:49:28 +00:00
|
|
|
the object was created; false otherwise.
|
|
|
|
|
|
|
|
This function must be called with a current QOpenGLContext.
|
|
|
|
The buffer will be bound to and can only be used in
|
|
|
|
that context (or any other context that is shared with it).
|
|
|
|
|
2012-02-21 12:24:39 +00:00
|
|
|
This function will return false if the OpenGL implementation
|
2011-08-22 08:49:28 +00:00
|
|
|
does not support buffers, or there is no current QOpenGLContext.
|
|
|
|
|
|
|
|
\sa isCreated(), allocate(), write(), destroy()
|
|
|
|
*/
|
|
|
|
bool QOpenGLBuffer::create()
|
|
|
|
{
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
if (d->guard && d->guard->id())
|
|
|
|
return true;
|
|
|
|
QOpenGLContext *ctx = QOpenGLContext::currentContext();
|
|
|
|
if (ctx) {
|
|
|
|
delete d->funcs;
|
|
|
|
d->funcs = new QOpenGLExtensions(ctx);
|
|
|
|
GLuint bufferId = 0;
|
|
|
|
d->funcs->glGenBuffers(1, &bufferId);
|
|
|
|
if (bufferId) {
|
|
|
|
if (d->guard)
|
|
|
|
d->guard->free();
|
|
|
|
|
|
|
|
d->guard = new QOpenGLSharedResourceGuard(ctx, bufferId, freeBufferFunc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this buffer has been created; false otherwise.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
\sa create(), destroy()
|
|
|
|
*/
|
|
|
|
bool QOpenGLBuffer::isCreated() const
|
|
|
|
{
|
|
|
|
Q_D(const QOpenGLBuffer);
|
|
|
|
return d->guard && d->guard->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Destroys this buffer object, including the storage being
|
2012-02-21 12:24:39 +00:00
|
|
|
used in the OpenGL server. All references to the buffer will
|
2011-08-22 08:49:28 +00:00
|
|
|
become invalid.
|
|
|
|
*/
|
|
|
|
void QOpenGLBuffer::destroy()
|
|
|
|
{
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
if (d->guard) {
|
|
|
|
d->guard->free();
|
2019-11-22 13:46:58 +00:00
|
|
|
d->guard = nullptr;
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
2013-04-12 19:27:16 +00:00
|
|
|
delete d->funcs;
|
2019-11-22 13:46:58 +00:00
|
|
|
d->funcs = nullptr;
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Reads the \a count bytes in this buffer starting at \a offset
|
2013-10-02 14:51:05 +00:00
|
|
|
into \a data. Returns \c true on success; false if reading from
|
2011-08-22 08:49:28 +00:00
|
|
|
the buffer is not supported. Buffer reading is not supported
|
|
|
|
under OpenGL/ES.
|
|
|
|
|
|
|
|
It is assumed that this buffer has been bound to the current context.
|
|
|
|
|
|
|
|
\sa write(), bind()
|
|
|
|
*/
|
|
|
|
bool QOpenGLBuffer::read(int offset, void *data, int count)
|
|
|
|
{
|
2020-04-14 11:48:28 +00:00
|
|
|
#if !QT_CONFIG(opengles2)
|
2014-03-03 20:20:30 +00:00
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
if (!d->funcs->hasOpenGLFeature(QOpenGLFunctions::Buffers) || !d->guard->id())
|
|
|
|
return false;
|
2018-09-18 11:38:14 +00:00
|
|
|
|
|
|
|
while (true) { // Clear error state.
|
|
|
|
GLenum error = d->funcs->glGetError();
|
|
|
|
if (error == GL_NO_ERROR)
|
|
|
|
break;
|
|
|
|
if (error == GL_CONTEXT_LOST)
|
|
|
|
return false;
|
|
|
|
};
|
2014-03-03 20:20:30 +00:00
|
|
|
d->funcs->glGetBufferSubData(d->type, offset, count, data);
|
|
|
|
return d->funcs->glGetError() == GL_NO_ERROR;
|
2011-08-22 08:49:28 +00:00
|
|
|
#else
|
|
|
|
Q_UNUSED(offset);
|
|
|
|
Q_UNUSED(data);
|
|
|
|
Q_UNUSED(count);
|
Dynamic GL switch on Windows
The patch introduces a new build configuration on Windows which
can be requested by passing -opengl dynamic to configure.
Platforms other than Windows (including WinRT) are not affected.
The existing Angle and desktop configurations are not affected.
These continue to function as before and Angle remains the default.
In the future, when all modules have added support for the dynamic
path, as described below, the default configuration could be changed
to be the dynamic one. This would allow providing a single set of
binaries in the official builds instead of the current two.
When requesting dynamic GL, Angle is built but QT_OPENGL_ES[_2] are
never defined. Instead, the code path that has traditionally been
desktop GL only becomes the dynamic path that has to do runtime
checks. Qt modules and applications are not linked to opengl32.dll or
libegl/glesv2.dll in this case. Instead, QtGui exports all necessary
egl/egl/gl functions which will, under the hood, forward all requests
to a dynamically loaded EGL/WGL/GL implementation.
Porting guide (better said, changes needed to prepare your code to
work with dynamic GL builds when the fallback to Angle is utilized):
1. In !QT_OPENGL_ES[_2] code branches use QOpenGLFunctions::isES() to
differentiate between desktop and ES where needed. Keep in mind that
it is the desktop GL header (plus qopenglext.h) that is included,
not the GLES one.
QtGui's proxy will handle some differences, for example calling
glClearDepth will route to glClearDepthf when needed. The built-in
eglGetProcAddress is able to retrieve pointers for standard GLES2
functions too so code resolving OpenGL 2 functions will function
in any case.
2. QT_CONFIG will contain "opengl" and "dynamicgl" in dynamic builds,
but never "angle" or "opengles2".
3. The preprocessor define QT_OPENGL_DYNAMIC is also available in
dynamic builds. The usage of this is strongly discouraged and should
not be needed anywhere except for QtGui and the platform plugin.
4. Code in need of the library handle can use
QOpenGLFunctions::platformGLHandle().
The decision on which library to load is currently based on a simple
test that creates a dummy window/context and tries to resolve an
OpenGL 2 function. If this fails, it goes for Angle. This seems to work
well on Win7 PCs for example that do not have proper graphics drivers
providing OpenGL installed but are D3D9 capable using the default drivers.
Setting QT_OPENGL to desktop or angle skips the test and forces
usage of the given GL. There are also two new application attributes
that could be used for the same purpose.
If Angle is requested but the libraries are not present, desktop is
tried. If desktop is requested, or if angle is requested but nothing
works, the EGL/WGL functions will still be callable but will return 0.
This conveniently means that eglInitialize() and such will report a failure.
Debug messages can be enabled by setting QT_OPENGLPROXY_DEBUG. This will
tell which implementation is chosen.
The textures example application is ported to OpenGL 2, the GL 1
code path is removed.
[ChangeLog][QtGui] Qt builds on Windows can now be configured for
dynamic loading of the OpenGL implementation. This can be requested
by passing -opengl dynamic to configure. In this mode no modules will
link to opengl32.dll or Angle's libegl/libglesv2. Instead, QtGui will
dynamically choose between desktop and Angle during the first GL/EGL/WGL
call. This allows deploying applications with a single set of Qt libraries
with the ability of transparently falling back to Angle in case the
opengl32.dll is not suitable, due to missing graphics drivers for example.
Task-number: QTBUG-36483
Change-Id: I716fdebbf60b355b7d9ef57d1e069eef366b4ab9
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Jørgen Lind <jorgen.lind@digia.com>
2014-01-27 13:45:11 +00:00
|
|
|
return false;
|
2014-03-03 20:20:30 +00:00
|
|
|
#endif
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Replaces the \a count bytes of this buffer starting at \a offset
|
|
|
|
with the contents of \a data. Any other bytes in the buffer
|
|
|
|
will be left unmodified.
|
|
|
|
|
|
|
|
It is assumed that create() has been called on this buffer and that
|
|
|
|
it has been bound to the current context.
|
|
|
|
|
|
|
|
\sa create(), read(), allocate()
|
|
|
|
*/
|
|
|
|
void QOpenGLBuffer::write(int offset, const void *data, int count)
|
|
|
|
{
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
if (!isCreated())
|
2018-07-12 05:32:28 +00:00
|
|
|
qWarning("QOpenGLBuffer::write(): buffer not created");
|
2011-08-22 08:49:28 +00:00
|
|
|
#endif
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
if (d->guard && d->guard->id())
|
|
|
|
d->funcs->glBufferSubData(d->type, offset, count, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Allocates \a count bytes of space to the buffer, initialized to
|
|
|
|
the contents of \a data. Any previous contents will be removed.
|
|
|
|
|
|
|
|
It is assumed that create() has been called on this buffer and that
|
|
|
|
it has been bound to the current context.
|
|
|
|
|
|
|
|
\sa create(), read(), write()
|
|
|
|
*/
|
|
|
|
void QOpenGLBuffer::allocate(const void *data, int count)
|
|
|
|
{
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
if (!isCreated())
|
|
|
|
qWarning("QOpenGLBuffer::allocate(): buffer not created");
|
|
|
|
#endif
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
if (d->guard && d->guard->id())
|
|
|
|
d->funcs->glBufferData(d->type, count, data, d->actualUsagePattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn void QOpenGLBuffer::allocate(int count)
|
|
|
|
\overload
|
|
|
|
|
|
|
|
Allocates \a count bytes of space to the buffer. Any previous
|
|
|
|
contents will be removed.
|
|
|
|
|
|
|
|
It is assumed that create() has been called on this buffer and that
|
|
|
|
it has been bound to the current context.
|
|
|
|
|
|
|
|
\sa create(), write()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Binds the buffer associated with this object to the current
|
2013-10-02 14:51:05 +00:00
|
|
|
OpenGL context. Returns \c false if binding was not possible, usually because
|
2012-02-21 12:24:39 +00:00
|
|
|
type() is not supported on this OpenGL implementation.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
The buffer must be bound to the same QOpenGLContext current when create()
|
|
|
|
was called, or to another QOpenGLContext that is sharing with it.
|
|
|
|
Otherwise, false will be returned from this function.
|
|
|
|
|
|
|
|
\sa release(), create()
|
|
|
|
*/
|
|
|
|
bool QOpenGLBuffer::bind()
|
|
|
|
{
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
if (!isCreated())
|
|
|
|
qWarning("QOpenGLBuffer::bind(): buffer not created");
|
|
|
|
#endif
|
|
|
|
Q_D(const QOpenGLBuffer);
|
|
|
|
GLuint bufferId = d->guard ? d->guard->id() : 0;
|
|
|
|
if (bufferId) {
|
|
|
|
if (d->guard->group() != QOpenGLContextGroup::currentContextGroup()) {
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
qWarning("QOpenGLBuffer::bind: buffer is not valid in the current context");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
d->funcs->glBindBuffer(d->type, bufferId);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Releases the buffer associated with this object from the
|
2012-02-21 12:24:39 +00:00
|
|
|
current OpenGL context.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
This function must be called with the same QOpenGLContext current
|
|
|
|
as when bind() was called on the buffer.
|
|
|
|
|
|
|
|
\sa bind()
|
|
|
|
*/
|
|
|
|
void QOpenGLBuffer::release()
|
|
|
|
{
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
if (!isCreated())
|
|
|
|
qWarning("QOpenGLBuffer::release(): buffer not created");
|
|
|
|
#endif
|
|
|
|
Q_D(const QOpenGLBuffer);
|
|
|
|
if (d->guard && d->guard->id())
|
|
|
|
d->funcs->glBindBuffer(d->type, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Releases the buffer associated with \a type in the current
|
|
|
|
QOpenGLContext.
|
|
|
|
|
|
|
|
This function is a direct call to \c{glBindBuffer(type, 0)}
|
|
|
|
for use when the caller does not know which QOpenGLBuffer has
|
|
|
|
been bound to the context but wants to make sure that it
|
|
|
|
is released.
|
|
|
|
|
2018-09-03 12:02:13 +00:00
|
|
|
\snippet code/src_gui_opengl_qopenglbuffer.cpp 1
|
2011-08-22 08:49:28 +00:00
|
|
|
*/
|
|
|
|
void QOpenGLBuffer::release(QOpenGLBuffer::Type type)
|
|
|
|
{
|
|
|
|
QOpenGLContext *ctx = QOpenGLContext::currentContext();
|
2011-08-29 08:22:28 +00:00
|
|
|
if (ctx)
|
|
|
|
ctx->functions()->glBindBuffer(GLenum(type), 0);
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2012-02-21 12:24:39 +00:00
|
|
|
Returns the OpenGL identifier associated with this buffer; zero if
|
2011-08-22 08:49:28 +00:00
|
|
|
the buffer has not been created.
|
|
|
|
|
|
|
|
\sa isCreated()
|
|
|
|
*/
|
|
|
|
GLuint QOpenGLBuffer::bufferId() const
|
|
|
|
{
|
|
|
|
Q_D(const QOpenGLBuffer);
|
|
|
|
return d->guard ? d->guard->id() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the size of the data in this buffer, for reading operations.
|
|
|
|
Returns -1 if fetching the buffer size is not supported, or the
|
|
|
|
buffer has not been created.
|
|
|
|
|
|
|
|
It is assumed that this buffer has been bound to the current context.
|
|
|
|
|
|
|
|
\sa isCreated(), bind()
|
|
|
|
*/
|
|
|
|
int QOpenGLBuffer::size() const
|
|
|
|
{
|
|
|
|
Q_D(const QOpenGLBuffer);
|
|
|
|
if (!d->guard || !d->guard->id())
|
|
|
|
return -1;
|
|
|
|
GLint value = -1;
|
|
|
|
d->funcs->glGetBufferParameteriv(d->type, GL_BUFFER_SIZE, &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Maps the contents of this buffer into the application's memory
|
|
|
|
space and returns a pointer to it. Returns null if memory
|
|
|
|
mapping is not possible. The \a access parameter indicates the
|
|
|
|
type of access to be performed.
|
|
|
|
|
|
|
|
It is assumed that create() has been called on this buffer and that
|
|
|
|
it has been bound to the current context.
|
|
|
|
|
2014-06-13 14:36:27 +00:00
|
|
|
\note This function is only supported under OpenGL ES 2.0 or
|
|
|
|
earlier if the \c GL_OES_mapbuffer extension is present.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
2014-06-13 14:36:27 +00:00
|
|
|
\note On OpenGL ES 3.0 and newer, or, in case if desktop OpenGL,
|
|
|
|
if \c GL_ARB_map_buffer_range is supported, this function uses
|
|
|
|
\c glMapBufferRange instead of \c glMapBuffer.
|
|
|
|
|
|
|
|
\sa unmap(), create(), bind(), mapRange()
|
2011-08-22 08:49:28 +00:00
|
|
|
*/
|
|
|
|
void *QOpenGLBuffer::map(QOpenGLBuffer::Access access)
|
|
|
|
{
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
if (!isCreated())
|
|
|
|
qWarning("QOpenGLBuffer::map(): buffer not created");
|
|
|
|
#endif
|
|
|
|
if (!d->guard || !d->guard->id())
|
2019-11-18 16:01:26 +00:00
|
|
|
return nullptr;
|
2014-06-13 14:36:27 +00:00
|
|
|
if (d->funcs->hasOpenGLExtension(QOpenGLExtensions::MapBufferRange)) {
|
2019-11-18 16:01:26 +00:00
|
|
|
QOpenGLBuffer::RangeAccessFlags rangeAccess;
|
2014-06-13 14:36:27 +00:00
|
|
|
switch (access) {
|
|
|
|
case QOpenGLBuffer::ReadOnly:
|
|
|
|
rangeAccess = QOpenGLBuffer::RangeRead;
|
|
|
|
break;
|
|
|
|
case QOpenGLBuffer::WriteOnly:
|
|
|
|
rangeAccess = QOpenGLBuffer::RangeWrite;
|
|
|
|
break;
|
|
|
|
case QOpenGLBuffer::ReadWrite:
|
|
|
|
rangeAccess = QOpenGLBuffer::RangeRead | QOpenGLBuffer::RangeWrite;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return d->funcs->glMapBufferRange(d->type, 0, size(), rangeAccess);
|
|
|
|
} else {
|
|
|
|
return d->funcs->glMapBuffer(d->type, access);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Maps the range specified by \a offset and \a count of the contents
|
|
|
|
of this buffer into the application's memory space and returns a
|
|
|
|
pointer to it. Returns null if memory mapping is not possible.
|
|
|
|
The \a access parameter specifies a combination of access flags.
|
|
|
|
|
|
|
|
It is assumed that create() has been called on this buffer and that
|
|
|
|
it has been bound to the current context.
|
|
|
|
|
|
|
|
\note This function is not available on OpenGL ES 2.0 and earlier.
|
|
|
|
|
|
|
|
\sa unmap(), create(), bind()
|
|
|
|
*/
|
|
|
|
void *QOpenGLBuffer::mapRange(int offset, int count, QOpenGLBuffer::RangeAccessFlags access)
|
|
|
|
{
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
if (!isCreated())
|
|
|
|
qWarning("QOpenGLBuffer::mapRange(): buffer not created");
|
|
|
|
#endif
|
|
|
|
if (!d->guard || !d->guard->id())
|
2019-11-22 13:46:58 +00:00
|
|
|
return nullptr;
|
2014-06-13 14:36:27 +00:00
|
|
|
return d->funcs->glMapBufferRange(d->type, offset, count, access);
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Unmaps the buffer after it was mapped into the application's
|
2013-10-02 14:51:05 +00:00
|
|
|
memory space with a previous call to map(). Returns \c true if
|
2011-08-22 08:49:28 +00:00
|
|
|
the unmap succeeded; false otherwise.
|
|
|
|
|
|
|
|
It is assumed that this buffer has been bound to the current context,
|
|
|
|
and that it was previously mapped with map().
|
|
|
|
|
2014-06-13 14:36:27 +00:00
|
|
|
\note This function is only supported under OpenGL ES 2.0 and
|
|
|
|
earlier if the \c{GL_OES_mapbuffer} extension is present.
|
2011-08-22 08:49:28 +00:00
|
|
|
|
|
|
|
\sa map()
|
|
|
|
*/
|
|
|
|
bool QOpenGLBuffer::unmap()
|
|
|
|
{
|
|
|
|
Q_D(QOpenGLBuffer);
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
if (!isCreated())
|
|
|
|
qWarning("QOpenGLBuffer::unmap(): buffer not created");
|
|
|
|
#endif
|
|
|
|
if (!d->guard || !d->guard->id())
|
|
|
|
return false;
|
2012-05-05 18:03:04 +00:00
|
|
|
return d->funcs->glUnmapBuffer(d->type) == GL_TRUE;
|
2011-08-22 08:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|