Commit 195978d3 authored by Laszlo Agocs's avatar Laszlo Agocs

gl: implement rsh

parent 76638c61
......@@ -105,6 +105,7 @@ static QString graphicsApiName()
#if QT_CONFIG(vulkan)
QVulkanInstance *vkinst = nullptr;
int activeRhiCount = 0;
QRhiResourceSharingHost *rsh = nullptr;
QRhiTexture *tex = nullptr;
#endif
......@@ -196,7 +197,6 @@ protected:
QRhiSampler *sampler = nullptr;
QRhiShaderResourceBindings *srb = nullptr;
QRhiGraphicsPipeline *ps = nullptr;
bool ownsTex = false;
};
Window::Window(const QString &title, const QColor &bgColor)
......@@ -299,6 +299,7 @@ QBakedShader getShader(const QString &name)
void Window::init()
{
createRhi(this, &m_rhi, &m_fallbackSurface);
++activeRhiCount;
m_sc = m_rhi->newSwapChain();
m_ds = m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
......@@ -325,13 +326,12 @@ void Window::init()
m_releasePool << ubuf;
QImage image;
bool newTex = false;
if (!tex) {
ownsTex = true;
newTex = true;
image.load(QLatin1String(":/qt256.png"));
tex = m_rhi->newTexture(QRhiTexture::RGBA8, image.size());
tex->build();
} else {
ownsTex = false;
}
sampler = m_rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
......@@ -372,7 +372,7 @@ void Window::init()
quint32 flip = 0;
initialUpdates->updateDynamicBuffer(ubuf, 64, 4, &flip);
if (ownsTex)
if (newTex)
initialUpdates->uploadTexture(tex, image);
}
......@@ -383,19 +383,18 @@ void Window::releaseResources()
m_releasePool.clear();
if (ownsTex) {
delete tex;
tex = nullptr;
ownsTex = false;
}
if (m_sc) {
m_sc->releaseAndDestroy();
m_sc = nullptr;
}
if (activeRhiCount == 1) {
delete tex;
tex = nullptr;
}
delete m_rhi;
m_rhi = nullptr;
--activeRhiCount;
delete m_fallbackSurface;
m_fallbackSurface = nullptr;
......
......@@ -35,6 +35,7 @@
****************************************************************************/
#include "qrhi_p.h"
#include "qrhirsh_p.h"
#include <qmath.h>
#include "qrhinull_p.h"
......@@ -2523,9 +2524,8 @@ quint32 QRhiImplementation::approxByteSizeForTexture(QRhiTexture::Format format,
Sharing the same device means that when two or more QRhi instances have the
same QhiResourceSharingHost set, there will only be one native graphics
device object, such as, \c VkDevice, \c MTLDevice, or \c ID3D11Device,
created, and that device will be available as long as the
QRhiResourceSharingHost is alive, thus avoiding lifetime and ownership
issues.
created, and that device will be available as long as any of the associated
QRhi instances are alive, thus avoiding lifetime and ownership issues.
This makes the underlying graphics resources of QRhiResource subclasses
such as QRhiTexture available to all the QRhi instances that use the same
......@@ -2539,10 +2539,9 @@ quint32 QRhiImplementation::approxByteSizeForTexture(QRhiTexture::Format format,
\note The QRhiResourceSharingHost can be created on a thread that is
different than the threads on which the associated QRhi instances will be
created. It is however up to the application to organize those threads in a
way that the construction and destruction of the QRhiResourceSharingHost is
performed (and completed) before and after all associated QRhi instances
and created and destroyed, respectively.
*/
way that the QRhiResourceSharingHost is only destroyed after all associated
QRhi instances have been fully destroyed.
*/
/*!
Constructs a new QRhiResourceSharingHost.
......
......@@ -1120,6 +1120,7 @@ public:
private:
Q_DISABLE_COPY(QRhiResourceSharingHost)
QRhiResourceSharingHostPrivate *d;
friend class QRhiResourceSharingHostPrivate;
};
struct Q_RHI_EXPORT QRhiInitParams
......
......@@ -362,11 +362,6 @@ private:
int curBinding = -1;
};
class Q_RHI_PRIVATE_EXPORT QRhiResourceSharingHostPrivate
{
public:
};
QT_END_NAMESPACE
#endif
......@@ -35,6 +35,7 @@
****************************************************************************/
#include "qrhigles2_p.h"
#include "qrhirsh_p.h"
#include <QWindow>
#include <QOffscreenSurface>
#include <QOpenGLContext>
......@@ -190,6 +191,9 @@ QOffscreenSurface *QRhiGles2InitParams::newFallbackSurface()
QRhiGles2::QRhiGles2(QRhiGles2InitParams *params, QRhiGles2NativeHandles *importDevice)
: ofr(this)
{
if (params->resourceSharingHost)
rsh = QRhiResourceSharingHostPrivate::get(params->resourceSharingHost);
fallbackSurface = params->fallbackSurface;
maybeWindow = params->window; // may be null
......@@ -232,9 +236,24 @@ bool QRhiGles2::create(QRhi::Flags flags)
Q_UNUSED(flags);
Q_ASSERT(fallbackSurface);
QMutexLocker lock(rsh ? &rsh->mtx : nullptr);
QOpenGLContext *shareContext = nullptr;
bool rshWantsContext = false;
// Now we either need to share with the rsh's context, or, if there is
// context in the rsh yet, store the new context there as well.
if (rsh) {
if (rsh->d_gles2.context)
shareContext = rsh->d_gles2.context;
else
rshWantsContext = true;
}
if (!importedContext) {
ctx = new QOpenGLContext;
ctx->setFormat(qrhigles2_effectiveFormat());
if (shareContext)
ctx->setShareContext(shareContext);
if (!ctx->create()) {
qWarning("QRhiGles2: Failed to create context");
delete ctx;
......@@ -267,6 +286,12 @@ bool QRhiGles2::create(QRhi::Flags flags)
nativeHandlesStruct.context = ctx;
if (rsh) {
rsh->rhiCount += 1;
if (rshWantsContext)
rsh->d_gles2.context = ctx;
}
return true;
}
......@@ -280,10 +305,17 @@ void QRhiGles2::destroy()
f = nullptr;
QMutexLocker lock(rsh ? &rsh->mtx : nullptr);
if (!importedContext) {
delete ctx;
ctx = nullptr;
if (!rsh || rsh->rhiCount == 1) {
delete ctx;
ctx = nullptr;
}
}
if (rsh)
rsh->rhiCount -= 1;
}
// Strictly speaking this is not necessary since we could do the deletes in
......
......@@ -50,6 +50,7 @@
QT_BEGIN_NAMESPACE
class QOpenGLExtensions;
class QRhiResourceSharingHostPrivate;
struct QGles2Buffer : public QRhiBuffer
{
......@@ -522,6 +523,7 @@ public:
void executeBindGraphicsPipeline(QRhiGraphicsPipeline *ps, QRhiShaderResourceBindings *srb);
void setChangedUniforms(QGles2GraphicsPipeline *psD, QRhiShaderResourceBindings *srb, bool changedOnly);
QRhiResourceSharingHostPrivate *rsh = nullptr;
QOpenGLContext *ctx = nullptr;
bool importedContext = false;
QWindow *maybeWindow = nullptr;
......
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt RHI module
**
** $QT_BEGIN_LICENSE:GPL$
** 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
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) 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.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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QRHIRSH_P_H
#define QRHIRSH_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include "qtrhiglobal_p.h"
#include "qrhi_p.h"
#include "qrhinull.h"
#ifndef QT_NO_OPENGL
#include "qrhigles2.h"
#endif
#if QT_CONFIG(vulkan)
#include "qrhivulkan.h"
#endif
#ifdef Q_OS_WIN
#include "qrhid3d11.h"
#endif
#ifdef Q_OS_DARWIN
#include "qrhimetal.h"
#endif
#include <QMutex>
QT_BEGIN_NAMESPACE
class Q_RHI_PRIVATE_EXPORT QRhiResourceSharingHostPrivate
{
public:
static QRhiResourceSharingHostPrivate *get(QRhiResourceSharingHost *h) { return h->d; }
QMutex mtx;
int rhiCount = 0;
QRhiNullNativeHandles d_null;
#ifndef QT_NO_OPENGL
QRhiGles2NativeHandles d_gles2;
#endif
#if QT_CONFIG(vulkan)
QRhiVulkanNativeHandles d_vulkan;
#endif
#ifdef Q_OS_WIN
QRhiD3D11NativeHandles d_d3d11;
#endif
#ifdef Q_OS_DARWIN
QRhiMetalNativeHandles d_metal;
#endif
};
QT_END_NAMESPACE
#endif
......@@ -9,6 +9,7 @@ HEADERS += \
qtrhiglobal_p.h \
qrhi.h \
qrhi_p.h \
qrhirsh_p.h \
qrhiprofiler.h \
qrhiprofiler_p.h \
qrhinull.h \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment