Commit 5e71ad40 authored by Laszlo Agocs's avatar Laszlo Agocs
Browse files

Add plainqwindow_* example replacement

parent 22b94c3e
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** 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.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QGuiApplication>
#include <QRhiD3D11InitParams>
#include "examplewindow.h"
#define PROFILE
#ifdef PROFILE
#include <QRhiProfiler>
#include <QFile>
QFile profOut;
#endif
class D3D11Window : public ExampleWindow
{
public:
D3D11Window() { setSurfaceType(OpenGLSurface); }
~D3D11Window() { releaseResources(); }
private:
void init() override;
};
void D3D11Window::init()
{
QRhiD3D11InitParams params;
params.enableDebugLayer = true;
params.importExistingDevice = false;
QRhi::Flags flags = QRhi::EnableDebugMarkers;
#ifdef PROFILE
flags |= QRhi::EnableProfiling;
#endif
m_r = QRhi::create(QRhi::D3D11, &params, flags);
#ifdef PROFILE
m_r->profiler()->setDevice(&profOut);
#endif
//setSampleCount(4); // enable 4x MSAA (except for the render-to-texture pass)
ExampleWindow::init();
}
int main(int argc, char **argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
#ifdef PROFILE
profOut.setFileName("rhiprof.cbor");
profOut.open(QIODevice::WriteOnly);
#endif
D3D11Window w;
w.resize(1280, 720);
w.setTitle(QLatin1String("D3D11"));
w.show();
return app.exec();
}
TEMPLATE = app
QT += shadertools rhi
SOURCES = \
main.cpp \
../shared/examplewindow.cpp \
../shared/trianglerenderer.cpp \
../shared/quadrenderer.cpp \
../shared/texturedcuberenderer.cpp \
../shared/triangleoncuberenderer.cpp
HEADERS = \
../shared/examplewindow.h \
../shared/trianglerenderer.h \
../shared/quadrenderer.h \
../shared/texturedcuberenderer.h \
../shared/triangleoncuberenderer.h
INCLUDEPATH += ../shared
RESOURCES = plainqwindow_d3d11.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/plainqwindow_d3d11
INSTALLS += target
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** 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.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QGuiApplication>
#include <QRhiGles2InitParams>
#include <QOpenGLContext>
#include <QOffscreenSurface>
#include "examplewindow.h"
class GlWindow : public ExampleWindow
{
public:
GlWindow() { setSurfaceType(OpenGLSurface); }
~GlWindow() { releaseResources(); }
private:
void init() override;
void releaseResources() override;
QOpenGLContext *ctx = nullptr;
QOffscreenSurface *fallbackSurface = nullptr;
};
void GlWindow::init()
{
ctx = new QOpenGLContext;
if (!ctx->create())
qFatal("Failed to get OpenGL context");
fallbackSurface = new QOffscreenSurface;
fallbackSurface->setFormat(ctx->format());
fallbackSurface->create();
QRhiGles2InitParams params;
params.context = ctx;
params.window = this;
params.fallbackSurface = fallbackSurface;
m_r = QRhi::create(QRhi::OpenGLES2, &params);
ExampleWindow::init();
}
void GlWindow::releaseResources()
{
ExampleWindow::releaseResources();
delete ctx;
ctx = nullptr;
delete fallbackSurface;
fallbackSurface = nullptr;
}
int main(int argc, char **argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QSurfaceFormat fmt;
fmt.setDepthBufferSize(24);
fmt.setStencilBufferSize(8);
//fmt.setSamples(4); // enable 4x MSAA (except for the render-to-texture pass)
QSurfaceFormat::setDefaultFormat(fmt);
GlWindow w;
w.resize(1280, 720);
w.setTitle(QLatin1String("OpenGL"));
w.show();
return app.exec();
}
TEMPLATE = app
QT += shadertools rhi
SOURCES = \
main.cpp \
../shared/examplewindow.cpp \
../shared/trianglerenderer.cpp \
../shared/quadrenderer.cpp \
../shared/texturedcuberenderer.cpp \
../shared/triangleoncuberenderer.cpp
HEADERS = \
../shared/examplewindow.h \
../shared/trianglerenderer.h \
../shared/quadrenderer.h \
../shared/texturedcuberenderer.h \
../shared/triangleoncuberenderer.h
INCLUDEPATH += ../shared
RESOURCES = plainqwindow_gles2.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/plainqwindow_gles2
INSTALLS += target
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file alias="color.vert.qsb">../shared/color.vert.qsb</file>
<file alias="color.frag.qsb">../shared/color.frag.qsb</file>
<file alias="texture.vert.qsb">../shared/texture.vert.qsb</file>
<file alias="texture.frag.qsb">../shared/texture.frag.qsb</file>
<file alias="qt256.png">../shared/qt256.png</file>
</qresource>
</RCC>
TEMPLATE = app
QT += shadertools rhi
SOURCES = \
main.cpp \
../shared/examplewindow.cpp \
../shared/trianglerenderer.cpp \
../shared/quadrenderer.cpp \
../shared/texturedcuberenderer.cpp \
../shared/triangleoncuberenderer.cpp
HEADERS = \
../shared/examplewindow.h \
../shared/trianglerenderer.h \
../shared/quadrenderer.h \
../shared/texturedcuberenderer.h \
../shared/triangleoncuberenderer.h
INCLUDEPATH += ../shared
RESOURCES = plainqwindow_metal.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/plainqwindow_metal
INSTALLS += target
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file alias="color.vert.qsb">../shared/color.vert.qsb</file>
<file alias="color.frag.qsb">../shared/color.frag.qsb</file>
<file alias="texture.vert.qsb">../shared/texture.vert.qsb</file>
<file alias="texture.frag.qsb">../shared/texture.frag.qsb</file>
<file alias="qt256.png">../shared/qt256.png</file>
</qresource>
</RCC>
TEMPLATE = app
QT += shadertools rhi
SOURCES = \
main.cpp \
../shared/examplewindow.cpp \
../shared/trianglerenderer.cpp \
../shared/quadrenderer.cpp \
../shared/texturedcuberenderer.cpp \
../shared/triangleoncuberenderer.cpp
HEADERS = \
../shared/examplewindow.h \
../shared/trianglerenderer.h \
../shared/quadrenderer.h \
../shared/texturedcuberenderer.h \
../shared/triangleoncuberenderer.h
INCLUDEPATH += ../shared
RESOURCES = plainqwindow_vulkan.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/plainqwindow_vulkan
INSTALLS += target
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file alias="color.vert.qsb">../shared/color.vert.qsb</file>
<file alias="color.frag.qsb">../shared/color.frag.qsb</file>
<file alias="texture.vert.qsb">../shared/texture.vert.qsb</file>
<file alias="texture.frag.qsb">../shared/texture.frag.qsb</file>
<file alias="qt256.png">../shared/qt256.png</file>
</qresource>
</RCC>
......@@ -11,24 +11,21 @@ SUBDIRS += \
multiwindow \
multiwindow_threaded \
imguidemo \
plainqwindow_gles2 \
triquadcube \
offscreen_gles2
qtConfig(vulkan) {
SUBDIRS += \
vulkanwindow \
plainqwindow_vulkan \
offscreen_vulkan
}
win32 {
SUBDIRS += \
plainqwindow_d3d11 \
offscreen_d3d11
}
mac {
SUBDIRS += \
plainqwindow_metal \
offscreen_metal
}
......@@ -116,6 +116,11 @@ QString graphicsApiName()
return QString();
}
QRhi::Flags rhiFlags = 0;
int sampleCount = 1;
QRhiSwapChain::Flags scFlags = 0;
void (*preInitFunc)() = nullptr;
class Window : public QWindow
{
public:
......@@ -244,7 +249,7 @@ void Window::init()
params.context = m_context;
params.window = this;
params.fallbackSurface = m_fallbackSurface;
m_r = QRhi::create(QRhi::OpenGLES2, &params);
m_r = QRhi::create(QRhi::OpenGLES2, &params, rhiFlags);
}
#endif
......@@ -253,7 +258,7 @@ void Window::init()
QRhiVulkanInitParams params;
params.inst = vulkanInstance();
params.window = this;
m_r = QRhi::create(QRhi::Vulkan, &params);
m_r = QRhi::create(QRhi::Vulkan, &params, rhiFlags);
}
#endif
......@@ -261,14 +266,14 @@ void Window::init()
if (graphicsApi == D3D11) {
QRhiD3D11InitParams params;
params.enableDebugLayer = true;
m_r = QRhi::create(QRhi::D3D11, &params);
m_r = QRhi::create(QRhi::D3D11, &params, rhiFlags);
}
#endif
#ifdef Q_OS_DARWIN
if (graphicsApi == Metal) {
QRhiMetalInitParams params;
m_r = QRhi::create(QRhi::Metal, &params);
m_r = QRhi::create(QRhi::Metal, &params, rhiFlags);
}
#endif
......@@ -281,10 +286,12 @@ void Window::init()
// allow depth-stencil, although we do not actually enable depth test/write for the triangle
m_ds = m_r->newRenderBuffer(QRhiRenderBuffer::DepthStencil,
QSize(), // no need to set the size yet
1,
sampleCount,
QRhiRenderBuffer::UsedWithSwapChainOnly);
m_sc->setWindow(this);
m_sc->setDepthStencil(m_ds);
m_sc->setSampleCount(sampleCount);
m_sc->setFlags(scFlags);
m_rp = m_sc->newCompatibleRenderPassDescriptor();
m_sc->setRenderPassDescriptor(m_rp);
......@@ -461,6 +468,11 @@ int main(int argc, char **argv)
}
#endif
#ifdef EXAMPLEFW_PREINIT
void preInit();
preInit();
#endif
// Create and show the window.
Window w;
#if QT_CONFIG(vulkan)
......
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** 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.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef EXAMPLEWINDOW_H
#define EXAMPLEWINDOW_H
#include <QRhi>
#include <QWindow>
#include <QPlatformSurfaceEvent>
#include "trianglerenderer.h"
#include "quadrenderer.h"
#include "texturedcuberenderer.h"
#include "triangleoncuberenderer.h"
class ExampleWindow : public QWindow
{
public:
virtual void init();
virtual void releaseResources();
virtual void recreateSwapChain();
virtual void releaseSwapChain();
void setSampleCount(int sampleCount) { m_sampleCount = sampleCount; }
void setOnScreenOnly(bool v) { m_onScreenOnly = v; }
void setTriangleOnly(bool v) { m_triangleOnly = v; }
protected:
void render();
void exposeEvent(QExposeEvent *) override;
bool event(QEvent *) override;
bool m_running = false;
bool m_notExposed = false;
bool m_newlyExposed = false;
int m_frameCount = 0;
QRhi *m_r = nullptr;
bool m_hasSwapChain = false;
bool m_resizedSwapChain = false;
QRhiSwapChain *m_sc = nullptr;
QRhiRenderPassDescriptor *m_scrp = nullptr;
QRhiRenderBuffer *m_ds = nullptr;
TriangleRenderer m_triRenderer;
QuadRenderer m_quadRenderer;
TexturedCubeRenderer m_cubeRenderer;
TriangleOnCubeRenderer m_liveTexCubeRenderer;
int m_sampleCount = 1;
bool m_onScreenOnly = false;
bool m_triangleOnly = false;
};
#endif
......@@ -52,7 +52,7 @@
#include <QFile>
#include <QBakedShader>
#include "cube.h"
#include "../shared/cube.h"
const bool MIPMAP = true;
const bool AUTOGENMIPMAP = true;
......@@ -69,10 +69,12 @@ static QBakedShader getShader(const QString &name)
void TexturedCubeRenderer::initResources(QRhiRenderPassDescriptor *rp)
{
m_vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(cube));
m_vbuf->setName(QByteArrayLiteral("Cube vbuf (textured)"));
m_vbuf->build();
m_vbufReady = false;
m_ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4);
m_ubuf->setName(QByteArrayLiteral("Cube ubuf (textured)"));
m_ubuf->build();
m_image = QImage(QLatin1String(":/qt256.png")).convertToFormat(QImage::Format_RGBA8888);
......@@ -82,6 +84,7 @@ void TexturedCubeRenderer::initResources(QRhiRenderPassDescriptor *rp)
if (AUTOGENMIPMAP)
texFlags |= QRhiTexture::UsedWithGenerateMips;
m_tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(m_image.width(), m_image.height()), 1, texFlags);
m_tex->setName(QByteArrayLiteral("Qt texture"));
m_tex->build();
m_sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, MIPMAP ? QRhiSampler::Linear : QRhiSampler::None,
......
......@@ -61,7 +61,7 @@ const bool DS_ATT = false; // have a depth-stencil attachment for the offscreen
const bool DEPTH_TEXTURE = false; // offscreen pass uses a depth texture (verify with renderdoc etc., ignore valid.layer about ps slot 0)
const bool MRT = false; // two textures, the second is just cleared as the shader does not write anything (valid.layer may warn but for testing that's ok)
#include "cube.h"
#include "../shared/cube.h"
static QBakedShader getShader(const QString &name)
{
......@@ -77,10 +77,12 @@ static const QSize OFFSCREEN_SIZE(512, 512);
void TriangleOnCubeRenderer::initResources(QRhiRenderPassDescriptor *rp)
{