Commit c4e5c4db authored by Laszlo Agocs's avatar Laszlo Agocs

d3d: implement begin/endOffscreenFrame

parent 514b98f6
/****************************************************************************
**
** 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 <QImage>
#include <QFileInfo>
#include <QFile>
#include <QBakedShader>
#include <QRhiD3D11InitParams>
static float vertexData[] = { // Y up (note m_proj), CCW
0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
};
static QBakedShader getShader(const QString &name)
{
QFile f(name);
if (f.open(QIODevice::ReadOnly))
return QBakedShader::fromSerialized(f.readAll());
return QBakedShader();
}
int main(int argc, char **argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QRhiD3D11InitParams params;
params.enableDebugLayer = true;
QRhi *r = QRhi::create(QRhi::D3D11, &params);
if (!r) {
qWarning("Failed to initialize RHI");
return 1;
}
QRhiTexture *tex = r->newTexture(QRhiTexture::RGBA8, QSize(1280, 720), QRhiTexture::RenderTarget | QRhiTexture::ReadBack);
tex->build();
QRhiTextureRenderTarget *rt = r->newTextureRenderTarget({ tex });
QRhiRenderPassDescriptor *rp = rt->newCompatibleRenderPassDescriptor();
rt->setRenderPassDescriptor(rp);
rt->build();
QMatrix4x4 proj = r->clipSpaceCorrMatrix();
proj.perspective(45.0f, 1280 / 720.f, 0.01f, 1000.0f);
proj.translate(0, 0, -4);
QRhiBuffer *vbuf = r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData));
vbuf->build();
QRhiBuffer *ubuf = r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68);
ubuf->build();
QRhiShaderResourceBindings *srb = r->newShaderResourceBindings();
srb->setBindings({
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, ubuf)
});
srb->build();
QRhiGraphicsPipeline *ps = r->newGraphicsPipeline();
QRhiGraphicsPipeline::TargetBlend premulAlphaBlend;
premulAlphaBlend.enable = true;
ps->setTargetBlends({ premulAlphaBlend });
const QBakedShader vs = getShader(QLatin1String(":/color.vert.qsb"));
if (!vs.isValid())
qFatal("Failed to load shader pack (vertex)");
const QBakedShader fs = getShader(QLatin1String(":/color.frag.qsb"));
if (!fs.isValid())
qFatal("Failed to load shader pack (fragment)");
ps->setShaderStages({
{ QRhiGraphicsShaderStage::Vertex, vs },
{ QRhiGraphicsShaderStage::Fragment, fs }
});
QRhiVertexInputLayout inputLayout;
inputLayout.bindings = {
{ 5 * sizeof(float) }
};
inputLayout.attributes = {
{ 0, 0, QRhiVertexInputLayout::Attribute::Float2, 0 },
{ 0, 1, QRhiVertexInputLayout::Attribute::Float3, 2 * sizeof(float) }
};
ps->setVertexInputLayout(inputLayout);
ps->setShaderResourceBindings(srb);
ps->setRenderPassDescriptor(rp);
ps->build();
for (int frame = 0; frame < 20; ++frame) {
QRhiCommandBuffer *cb;
if (r->beginOffscreenFrame(&cb) != QRhi::FrameOpSuccess)
break;
qDebug("Generating offscreen frame %d", frame);
QRhiResourceUpdateBatch *u = r->nextResourceUpdateBatch();
if (frame == 0)
u->uploadStaticBuffer(vbuf, vertexData);
static float rotation = 0.0f;
QMatrix4x4 mvp = proj;
mvp.rotate(rotation, 0, 1, 0);
u->updateDynamicBuffer(ubuf, 0, 64, mvp.constData());
rotation += 5.0f;
static float opacity = 1.0f;
static int opacityDir= 1;
u->updateDynamicBuffer(ubuf, 64, 4, &opacity);
opacity += opacityDir * 0.005f;
if (opacity < 0.0f || opacity > 1.0f) {
opacityDir *= -1;
opacity = qBound(0.0f, opacity, 1.0f);
}
r->beginPass(rt, cb, { 0, 1, 0, 1 }, { 1, 0 }, u);
r->setGraphicsPipeline(cb, ps);
r->setViewport(cb, { 0, 0, 1280, 720 });
r->setVertexInput(cb, 0, { { vbuf, 0 } });
r->draw(cb, 3);
r->endPass(cb);
QRhiReadbackDescription rb(tex);
QRhiReadbackResult rbResult;
rbResult.completed = [frame] { qDebug(" - readback %d completed", frame); };
r->readback(cb, rb, &rbResult);
qDebug("Submit and wait");
r->endOffscreenFrame();
// No finish() or waiting for the completed callback is needed here
// since the endOffscreenFrame() implies a wait for completion.
if (!rbResult.data.isEmpty()) {
const uchar *p = reinterpret_cast<const uchar *>(rbResult.data.constData());
QImage image(p, rbResult.pixelSize.width(), rbResult.pixelSize.height(), QImage::Format_RGBA8888);
QString fn = QString::asprintf("frame%d.png", frame);
fn = QFileInfo(fn).absoluteFilePath();
qDebug("Saving into %s", qPrintable(fn));
image.save(fn);
} else {
qWarning("Readback failed!");
}
}
ps->releaseAndDestroy();
srb->releaseAndDestroy();
ubuf->releaseAndDestroy();
vbuf->releaseAndDestroy();
rt->releaseAndDestroy();
rp->releaseAndDestroy();
tex->releaseAndDestroy();
delete r;
return 0;
}
TEMPLATE = app
CONFIG += console
QT += shadertools rhi
SOURCES = \
main.cpp
RESOURCES = offscreen_d3d11.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/rhi/offscreen_d3d11
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>
</qresource>
</RCC>
...@@ -14,7 +14,8 @@ qtConfig(vulkan) { ...@@ -14,7 +14,8 @@ qtConfig(vulkan) {
win32 { win32 {
SUBDIRS += \ SUBDIRS += \
plainqwindow_d3d11 plainqwindow_d3d11 \
offscreen_d3d11
} }
mac { mac {
......
...@@ -1008,17 +1008,38 @@ public: ...@@ -1008,17 +1008,38 @@ public:
FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb); FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb);
FrameOpResult endOffscreenFrame(); FrameOpResult endOffscreenFrame();
// Cannot be inside a pass. /*
Readbacks cannot be inside a pass. When used in a begin-endFrame (not
offscreen), the data may only be available in a future frame. Hence the
completed callback:
m_r->beginFrame(sc);
...
QRhiReadbackResult *rbResult = new QRhiReadbackResult;
rbResult->completed = [rbResult] {
{
QImage::Format fmt = rbResult->format == QRhiTexture::BGRA8 ? QImage::Format_ARGB32_Premultiplied
: QImage::Format_RGBA8888_Premultiplied;
const uchar *p = reinterpret_cast<const uchar *>(rbResult->data.constData());
QImage image(p, rbResult->pixelSize.width(), rbResult->pixelSize.height(), fmt);
...
}
delete rbResult;
};
QRhiReadbackDescription rb; // no texture -> backbuffer
m_r->readback(cb, rb, rbResult);
m_r->endFrame(sc);
*/
bool readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &rb, QRhiReadbackResult *result); bool readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &rb, QRhiReadbackResult *result);
// Waits for any work on the graphics queue (where applicable) to complete, // Waits for any work on the graphics queue (where applicable) to complete,
// then forcibly executes all deferred operations, like completing // then forcibly executes all deferred operations, like completing
// readbacks and resource releases. This should _not_ be used in practice, // readbacks and resource releases. This should _not_ be used in practice,
// except in infrequent special cases, like when the results of a readback // except in infrequent special cases, like when the results of a readback
// are needed right away. Can be called inside and outside of a frame, but // are needed asap and no new frames are going to be generated for some
// not inside a pass. Inside a frame it implies submitting any work on the // time. Can be called inside and outside of a frame, but not inside a
// command buffer. Unnecessary in combination with begin/endOffscreenFrame // pass. Inside a frame it implies submitting any work on the command
// because ending an offscreen frame implies waiting for completion. // buffer. Unnecessary in combination with begin/endOffscreenFrame because
// ending an offscreen frame implies waiting for completion.
QRhi::FrameOpResult finish(); QRhi::FrameOpResult finish();
// Returns an instance to which updates can be queued. Batch instances are // Returns an instance to which updates can be queued. Batch instances are
......
...@@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE ...@@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE
*/ */
QRhiD3D11::QRhiD3D11(QRhiInitParams *params) QRhiD3D11::QRhiD3D11(QRhiInitParams *params)
: ofr(this)
{ {
QRhiD3D11InitParams *d3dparams = static_cast<QRhiD3D11InitParams *>(params); QRhiD3D11InitParams *d3dparams = static_cast<QRhiD3D11InitParams *>(params);
debugLayer = d3dparams->enableDebugLayer; debugLayer = d3dparams->enableDebugLayer;
...@@ -461,11 +462,9 @@ QRhi::FrameOpResult QRhiD3D11::beginFrame(QRhiSwapChain *swapChain) ...@@ -461,11 +462,9 @@ QRhi::FrameOpResult QRhiD3D11::beginFrame(QRhiSwapChain *swapChain)
QD3D11SwapChain *swapChainD = QRHI_RES(QD3D11SwapChain, swapChain); QD3D11SwapChain *swapChainD = QRHI_RES(QD3D11SwapChain, swapChain);
swapChainD->cb.resetState(); swapChainD->cb.resetState();
currentFrameSlot = swapChainD->currentFrame;
swapChainD->rt.d.pixelSize = swapChainD->pixelSize; swapChainD->rt.d.pixelSize = swapChainD->pixelSize;
swapChainD->rt.d.rtv[0] = swapChainD->sampleDesc.Count > 1 ? swapChainD->rt.d.rtv[0] = swapChainD->sampleDesc.Count > 1 ?
swapChainD->msaaRtv[currentFrameSlot] : swapChainD->rtv[currentFrameSlot]; swapChainD->msaaRtv[swapChainD->currentFrame] : swapChainD->rtv[swapChainD->currentFrame];
swapChainD->rt.d.dsv = swapChainD->ds ? swapChainD->ds->dsv : nullptr; swapChainD->rt.d.dsv = swapChainD->ds ? swapChainD->ds->dsv : nullptr;
return QRhi::FrameOpSuccess; return QRhi::FrameOpSuccess;
...@@ -480,8 +479,8 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain) ...@@ -480,8 +479,8 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain)
executeCommandBuffer(&swapChainD->cb); executeCommandBuffer(&swapChainD->cb);
if (swapChainD->sampleDesc.Count > 1) { if (swapChainD->sampleDesc.Count > 1) {
context->ResolveSubresource(swapChainD->tex[currentFrameSlot], 0, context->ResolveSubresource(swapChainD->tex[swapChainD->currentFrame], 0,
swapChainD->msaaTex[currentFrameSlot], 0, swapChainD->msaaTex[swapChainD->currentFrame], 0,
swapChainD->colorFormat); swapChainD->colorFormat);
} }
...@@ -491,7 +490,7 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain) ...@@ -491,7 +490,7 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain)
if (FAILED(hr)) if (FAILED(hr))
qWarning("Failed to present: %s", qPrintable(comErrorMessage(hr))); qWarning("Failed to present: %s", qPrintable(comErrorMessage(hr)));
swapChainD->currentFrame = (swapChainD->currentFrame + 1) % FRAMES_IN_FLIGHT; swapChainD->currentFrame = (swapChainD->currentFrame + 1) % QD3D11SwapChain::BUFFER_COUNT;
++finishedFrameCount; ++finishedFrameCount;
return QRhi::FrameOpSuccess; return QRhi::FrameOpSuccess;
...@@ -499,13 +498,27 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain) ...@@ -499,13 +498,27 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain)
QRhi::FrameOpResult QRhiD3D11::beginOffscreenFrame(QRhiCommandBuffer **cb) QRhi::FrameOpResult QRhiD3D11::beginOffscreenFrame(QRhiCommandBuffer **cb)
{ {
Q_UNUSED(cb); Q_ASSERT(!inFrame);
return QRhi::FrameOpError; inFrame = true;
ofr.active = true;
ofr.cbWrapper.resetState();
*cb = &ofr.cbWrapper;
return QRhi::FrameOpSuccess;
} }
QRhi::FrameOpResult QRhiD3D11::endOffscreenFrame() QRhi::FrameOpResult QRhiD3D11::endOffscreenFrame()
{ {
return QRhi::FrameOpError; Q_ASSERT(inFrame && ofr.active);
inFrame = false;
ofr.active = false;
executeCommandBuffer(&ofr.cbWrapper);
context->Flush();
++finishedFrameCount;
return QRhi::FrameOpSuccess;;
} }
bool QRhiD3D11::readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &rb, QRhiReadbackResult *result) bool QRhiD3D11::readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &rb, QRhiReadbackResult *result)
...@@ -522,7 +535,7 @@ bool QRhiD3D11::readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &r ...@@ -522,7 +535,7 @@ bool QRhiD3D11::readback(QRhiCommandBuffer *cb, const QRhiReadbackDescription &r
QRhi::FrameOpResult QRhiD3D11::finish() QRhi::FrameOpResult QRhiD3D11::finish()
{ {
Q_ASSERT(!inPass); Q_ASSERT(!inPass);
context->Flush();
return QRhi::FrameOpSuccess; return QRhi::FrameOpSuccess;
} }
......
...@@ -429,8 +429,6 @@ public: ...@@ -429,8 +429,6 @@ public:
D3D_FEATURE_LEVEL featureLevel; D3D_FEATURE_LEVEL featureLevel;
IDXGIFactory2 *dxgiFactory = nullptr; IDXGIFactory2 *dxgiFactory = nullptr;
static const int FRAMES_IN_FLIGHT = QD3D11SwapChain::BUFFER_COUNT;
int currentFrameSlot = 0; // 0..FRAMES_IN_FLIGHT-1
bool inFrame = false; bool inFrame = false;
int finishedFrameCount = 0; int finishedFrameCount = 0;
bool inPass = false; bool inPass = false;
...@@ -438,7 +436,14 @@ public: ...@@ -438,7 +436,14 @@ public:
struct { struct {
int vsLastActiveSrvBinding = 0; int vsLastActiveSrvBinding = 0;
int fsLastActiveSrvBinding = 0; int fsLastActiveSrvBinding = 0;
QD3D11SwapChain *currentSwapChain = nullptr;
} contextState; } contextState;
struct OffscreenFrame {
OffscreenFrame(QRhiImplementation *rhi) : cbWrapper(rhi) { }
bool active = false;
QD3D11CommandBuffer cbWrapper;
} ofr;
}; };
QT_END_NAMESPACE QT_END_NAMESPACE
......
...@@ -1403,6 +1403,8 @@ QRhi::FrameOpResult QRhiVulkan::beginOffscreenFrame(QRhiCommandBuffer **cb) ...@@ -1403,6 +1403,8 @@ QRhi::FrameOpResult QRhiVulkan::beginOffscreenFrame(QRhiCommandBuffer **cb)
QRhi::FrameOpResult QRhiVulkan::endOffscreenFrame() QRhi::FrameOpResult QRhiVulkan::endOffscreenFrame()
{ {
Q_ASSERT(ofr.active);
ofr.active = false; ofr.active = false;
prepareFrameEnd(); prepareFrameEnd();
......
mtl, d3d, gl: rhi without a window
mtl, d3d, gl: implement finish()
mtl, d3d, gl: readback (tex, backbuffer) mtl, d3d, gl: readback (tex, backbuffer)
mtl, d3d, gl: implement finish()
mtl, gl: rhi without a window
gl, mtl: compressed textures gl, mtl: compressed textures
gl, mtl: srgb (tex, swapchain buf) gl, mtl: srgb (tex, swapchain buf)
multi window? (multi swapchain) -> trouble multi window? (multi swapchain) -> trouble
...@@ -50,6 +50,7 @@ dxc for d3d as an alternative to fxc? ...@@ -50,6 +50,7 @@ dxc for d3d as an alternative to fxc?
hlsl -> dxc -> spirv -> spirv-cross hmmm... hlsl -> dxc -> spirv -> spirv-cross hmmm...
+++ done +++ done
d3d: offscreen frame
vk: read back the backbuffer vk: read back the backbuffer
vk: readback size/formats vk: readback size/formats
vk: readback vk: readback
......
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