| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the examples of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** BSD License Usage |
| 18 |
** Alternatively, you may use this file under the terms of the BSD license |
| 19 |
** as follows: |
| 20 |
** |
| 21 |
** "Redistribution and use in source and binary forms, with or without |
| 22 |
** modification, are permitted provided that the following conditions are |
| 23 |
** met: |
| 24 |
** * Redistributions of source code must retain the above copyright |
| 25 |
** notice, this list of conditions and the following disclaimer. |
| 26 |
** * Redistributions in binary form must reproduce the above copyright |
| 27 |
** notice, this list of conditions and the following disclaimer in |
| 28 |
** the documentation and/or other materials provided with the |
| 29 |
** distribution. |
| 30 |
** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 |
** contributors may be used to endorse or promote products derived |
| 32 |
** from this software without specific prior written permission. |
| 33 |
** |
| 34 |
** |
| 35 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 |
** |
| 47 |
** $QT_END_LICENSE$ |
| 48 |
** |
| 49 |
****************************************************************************/ |
| 50 |
|
| 51 |
// An example exercising more than a single feature. Enables profiling |
| 52 |
// (resource logging to a file) and inserts debug markers and sets some |
| 53 |
// object names. Can also be used to test MSAA swapchains, swapchain image |
| 54 |
// readback, requesting an sRGB swapchain, and some texture features. |
| 55 |
|
| 56 |
#define EXAMPLEFW_PREINIT |
| 57 |
#include "../shared/examplefw.h" |
| 58 |
#include "trianglerenderer.h" |
| 59 |
#include "quadrenderer.h" |
| 60 |
#include "texturedcuberenderer.h" |
| 61 |
#include "triangleoncuberenderer.h" |
| 62 |
|
| 63 |
#include <QFileInfo> |
| 64 |
#include <QFile> |
| 65 |
#include <QtGui/private/qrhiprofiler_p.h> |
| 66 |
|
| 67 |
#define PROFILE_TO_FILE |
| 68 |
//#define SKIP_PRESENT |
| 69 |
//#define USE_MSAA |
| 70 |
//#define USE_SRGB_SWAPCHAIN |
| 71 |
//#define READBACK_SWAPCHAIN |
| 72 |
//#define NO_VSYNC |
| 73 |
//#define USE_MIN_SWAPCHAIN_BUFFERS |
| 74 |
//#define DECLARE_EXT_CONTENTS |
| 75 |
|
| 76 |
struct { |
| 77 |
TriangleRenderer triRenderer; |
| 78 |
QuadRenderer quadRenderer; |
| 79 |
TexturedCubeRenderer cubeRenderer; |
| 80 |
TriangleOnCubeRenderer liveTexCubeRenderer; |
| 81 |
bool onScreenOnly = false; |
| 82 |
bool triangleOnly = false; |
| 83 |
QSize lastOutputSize; |
| 84 |
int frameCount = 0; |
| 85 |
QFile profOut; |
| 86 |
} d; |
| 87 |
|
| 88 |
void preInit() |
| 89 |
{ |
| 90 |
#ifdef PROFILE_TO_FILE |
| 91 |
rhiFlags |= QRhi::EnableProfiling; |
| 92 |
const QString profFn = QFileInfo(QLatin1String("rhiprof.txt")).absoluteFilePath(); |
| 93 |
qDebug("Writing profiling output to %s", qPrintable(profFn)); |
| 94 |
d.profOut.setFileName(profFn); |
| 95 |
d.profOut.open(QIODevice::WriteOnly); |
| 96 |
#endif |
| 97 |
|
| 98 |
#ifdef SKIP_PRESENT |
| 99 |
endFrameFlags |= QRhi::SkipPresent; |
| 100 |
#endif |
| 101 |
|
| 102 |
#ifdef USE_MSAA |
| 103 |
sampleCount = 4; // enable 4x MSAA (except for the render-to-texture pass) |
| 104 |
#endif |
| 105 |
|
| 106 |
#ifdef READBACK_SWAPCHAIN |
| 107 |
scFlags |= QRhiSwapChain::UsedAsTransferSource; |
| 108 |
#endif |
| 109 |
|
| 110 |
#ifdef USE_SRGB_SWAPCHAIN |
| 111 |
scFlags |= QRhiSwapChain::sRGB; |
| 112 |
#endif |
| 113 |
|
| 114 |
#ifdef NO_VSYNC |
| 115 |
scFlags |= QRhiSwapChain::NoVSync; |
| 116 |
#endif |
| 117 |
|
| 118 |
#ifdef USE_MIN_SWAPCHAIN_BUFFERS |
| 119 |
scFlags |= QRhiSwapChain::MinimalBufferCount; |
| 120 |
#endif |
| 121 |
|
| 122 |
#ifdef DECLARE_EXT_CONTENTS |
| 123 |
beginFrameFlags |= QRhi::ExternalContentsInPass; |
| 124 |
#endif |
| 125 |
|
| 126 |
// For OpenGL some of these are incorporated into the QSurfaceFormat by |
| 127 |
// examplefw.h after returning from here as that is out of the RHI's control. |
| 128 |
} |
| 129 |
|
| 130 |
void Window::customInit() |
| 131 |
{ |
| 132 |
#ifdef PROFILE_TO_FILE |
| 133 |
m_r->profiler()->setDevice(&d.profOut); |
| 134 |
#endif |
| 135 |
|
| 136 |
d.triRenderer.setRhi(m_r); |
| 137 |
d.triRenderer.setSampleCount(sampleCount); |
| 138 |
d.triRenderer.initResources(m_rp); |
| 139 |
|
| 140 |
if (!d.triangleOnly) { |
| 141 |
d.triRenderer.setTranslation(QVector3D(0, 0.5f, 0)); |
| 142 |
|
| 143 |
d.quadRenderer.setRhi(m_r); |
| 144 |
d.quadRenderer.setSampleCount(sampleCount); |
| 145 |
d.quadRenderer.setPipeline(d.triRenderer.pipeline()); |
| 146 |
d.quadRenderer.initResources(m_rp); |
| 147 |
d.quadRenderer.setTranslation(QVector3D(1.5f, -0.5f, 0)); |
| 148 |
|
| 149 |
d.cubeRenderer.setRhi(m_r); |
| 150 |
d.cubeRenderer.setSampleCount(sampleCount); |
| 151 |
d.cubeRenderer.initResources(m_rp); |
| 152 |
d.cubeRenderer.setTranslation(QVector3D(0, -0.5f, 0)); |
| 153 |
} |
| 154 |
|
| 155 |
if (!d.onScreenOnly) { |
| 156 |
d.liveTexCubeRenderer.setRhi(m_r); |
| 157 |
d.liveTexCubeRenderer.setSampleCount(sampleCount); |
| 158 |
d.liveTexCubeRenderer.initResources(m_rp); |
| 159 |
d.liveTexCubeRenderer.setTranslation(QVector3D(-2.0f, 0, 0)); |
| 160 |
} |
| 161 |
|
| 162 |
// Put the gpu mem allocator statistics to the profiling stream after doing |
| 163 |
// all the init. (where applicable) |
| 164 |
m_r->profiler()->addVMemAllocatorStats(); |
| 165 |
|
| 166 |
// Check some features/limits. |
| 167 |
qDebug("isFeatureSupported(MultisampleTexture): %d", m_r->isFeatureSupported(QRhi::MultisampleTexture)); |
| 168 |
qDebug("isFeatureSupported(MultisampleRenderBuffer): %d", m_r->isFeatureSupported(QRhi::MultisampleRenderBuffer)); |
| 169 |
qDebug("isFeatureSupported(DebugMarkers): %d", m_r->isFeatureSupported(QRhi::DebugMarkers)); |
| 170 |
qDebug("isFeatureSupported(Timestamps): %d", m_r->isFeatureSupported(QRhi::Timestamps)); |
| 171 |
qDebug("isFeatureSupported(Instancing): %d", m_r->isFeatureSupported(QRhi::Instancing)); |
| 172 |
qDebug("isFeatureSupported(CustomInstanceStepRate): %d", m_r->isFeatureSupported(QRhi::CustomInstanceStepRate)); |
| 173 |
qDebug("isFeatureSupported(PrimitiveRestart): %d", m_r->isFeatureSupported(QRhi::PrimitiveRestart)); |
| 174 |
qDebug("isFeatureSupported(NonDynamicUniformBuffers): %d", m_r->isFeatureSupported(QRhi::NonDynamicUniformBuffers)); |
| 175 |
qDebug("isFeatureSupported(NonFourAlignedEffectiveIndexBufferOffset): %d", m_r->isFeatureSupported(QRhi::NonFourAlignedEffectiveIndexBufferOffset)); |
| 176 |
qDebug("isFeatureSupported(NPOTTextureRepeat): %d", m_r->isFeatureSupported(QRhi::NPOTTextureRepeat)); |
| 177 |
qDebug("isFeatureSupported(RedOrAlpha8IsRed): %d", m_r->isFeatureSupported(QRhi::RedOrAlpha8IsRed)); |
| 178 |
qDebug("isFeatureSupported(ElementIndexUint): %d", m_r->isFeatureSupported(QRhi::ElementIndexUint)); |
| 179 |
qDebug("isFeatureSupported(Compute): %d", m_r->isFeatureSupported(QRhi::Compute)); |
| 180 |
qDebug("isFeatureSupported(WideLines): %d", m_r->isFeatureSupported(QRhi::WideLines)); |
| 181 |
qDebug("isFeatureSupported(VertexShaderPointSize): %d", m_r->isFeatureSupported(QRhi::VertexShaderPointSize)); |
| 182 |
qDebug("isFeatureSupported(BaseVertex): %d", m_r->isFeatureSupported(QRhi::BaseVertex)); |
| 183 |
qDebug("isFeatureSupported(BaseInstance): %d", m_r->isFeatureSupported(QRhi::BaseInstance)); |
| 184 |
qDebug("Min 2D texture width/height: %d", m_r->resourceLimit(QRhi::TextureSizeMin)); |
| 185 |
qDebug("Max 2D texture width/height: %d", m_r->resourceLimit(QRhi::TextureSizeMax)); |
| 186 |
qDebug("Max color attachment count: %d", m_r->resourceLimit(QRhi::MaxColorAttachments)); |
| 187 |
} |
| 188 |
|
| 189 |
void Window::customRelease() |
| 190 |
{ |
| 191 |
d.triRenderer.releaseResources(); |
| 192 |
|
| 193 |
if (!d.triangleOnly) { |
| 194 |
d.quadRenderer.releaseResources(); |
| 195 |
d.cubeRenderer.releaseResources(); |
| 196 |
} |
| 197 |
|
| 198 |
if (!d.onScreenOnly) |
| 199 |
d.liveTexCubeRenderer.releaseResources(); |
| 200 |
} |
| 201 |
|
| 202 |
void Window::customRender() |
| 203 |
{ |
| 204 |
const QSize outputSize = m_sc->currentPixelSize(); |
| 205 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 206 |
|
| 207 |
if (outputSize != d.lastOutputSize) { |
| 208 |
d.triRenderer.resize(outputSize); |
| 209 |
if (!d.triangleOnly) { |
| 210 |
d.quadRenderer.resize(outputSize); |
| 211 |
d.cubeRenderer.resize(outputSize); |
| 212 |
} |
| 213 |
if (!d.onScreenOnly) |
| 214 |
d.liveTexCubeRenderer.resize(outputSize); |
| 215 |
d.lastOutputSize = outputSize; |
| 216 |
} |
| 217 |
|
| 218 |
if (!d.onScreenOnly) { |
| 219 |
cb->debugMarkBegin("Offscreen triangle pass"); |
| 220 |
d.liveTexCubeRenderer.queueOffscreenPass(cb); |
| 221 |
cb->debugMarkEnd(); |
| 222 |
} |
| 223 |
|
| 224 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 225 |
d.triRenderer.queueResourceUpdates(u); |
| 226 |
if (!d.triangleOnly) { |
| 227 |
d.quadRenderer.queueResourceUpdates(u); |
| 228 |
d.cubeRenderer.queueResourceUpdates(u); |
| 229 |
} |
| 230 |
if (!d.onScreenOnly) |
| 231 |
d.liveTexCubeRenderer.queueResourceUpdates(u); |
| 232 |
|
| 233 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 234 |
cb->debugMarkBegin(QByteArrayLiteral("Triangle")); |
| 235 |
d.triRenderer.queueDraw(cb, outputSize); |
| 236 |
cb->debugMarkEnd(); |
| 237 |
if (!d.triangleOnly) { |
| 238 |
cb->debugMarkMsg(QByteArrayLiteral("More stuff")); |
| 239 |
cb->debugMarkBegin(QByteArrayLiteral("Quad")); |
| 240 |
d.quadRenderer.queueDraw(cb, outputSize); |
| 241 |
cb->debugMarkEnd(); |
| 242 |
cb->debugMarkBegin(QByteArrayLiteral("Cube")); |
| 243 |
d.cubeRenderer.queueDraw(cb, outputSize); |
| 244 |
cb->debugMarkEnd(); |
| 245 |
} |
| 246 |
if (!d.onScreenOnly) { |
| 247 |
cb->debugMarkMsg(QByteArrayLiteral("Even more stuff")); |
| 248 |
cb->debugMarkBegin(QByteArrayLiteral("Cube with offscreen triangle")); |
| 249 |
d.liveTexCubeRenderer.queueDraw(cb, outputSize); |
| 250 |
cb->debugMarkEnd(); |
| 251 |
} |
| 252 |
|
| 253 |
QRhiResourceUpdateBatch *passEndUpdates = nullptr; |
| 254 |
#ifdef READBACK_SWAPCHAIN |
| 255 |
passEndUpdates = m_r->nextResourceUpdateBatch(); |
| 256 |
QRhiReadbackDescription rb; // no texture given -> backbuffer |
| 257 |
QRhiReadbackResult *rbResult = new QRhiReadbackResult; |
| 258 |
int frameNo = d.frameCount; |
| 259 |
rbResult->completed = [this, rbResult, frameNo] { |
| 260 |
{ |
| 261 |
QImage::Format fmt = rbResult->format == QRhiTexture::BGRA8 ? QImage::Format_ARGB32_Premultiplied |
| 262 |
: QImage::Format_RGBA8888_Premultiplied; |
| 263 |
const uchar *p = reinterpret_cast<const uchar *>(rbResult->data.constData()); |
| 264 |
QImage image(p, rbResult->pixelSize.width(), rbResult->pixelSize.height(), fmt); |
| 265 |
QString fn = QString::asprintf("frame%d.png", frameNo); |
| 266 |
fn = QFileInfo(fn).absoluteFilePath(); |
| 267 |
qDebug("Saving into %s", qPrintable(fn)); |
| 268 |
if (m_r->isYUpInFramebuffer()) |
| 269 |
image.mirrored().save(fn); |
| 270 |
else |
| 271 |
image.save(fn); |
| 272 |
} |
| 273 |
delete rbResult; |
| 274 |
}; |
| 275 |
passEndUpdates->readBackTexture(rb, rbResult); |
| 276 |
#endif |
| 277 |
|
| 278 |
cb->endPass(passEndUpdates); |
| 279 |
|
| 280 |
d.frameCount += 1; |
| 281 |
} |