| 1 |
// Copyright (C) 2024 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "../shared/examplefw.h" |
| 5 |
|
| 6 |
// Uses a multisample texture both for color and depth-stencil, renders into |
| 7 |
// those, and then resolves into non-multisample textures. Also the |
| 8 |
// depth-stencil, in order to exercise that rarely used path. If that is |
| 9 |
// functional, will not be visible on-screen. Frame captures with tools such as |
| 10 |
// RenderDoc can be used to verify that there is indeed a non-multisample depth |
| 11 |
// texture generated. |
| 12 |
|
| 13 |
static float vertexData[] = |
| 14 |
{ // Y up, CCW |
| 15 |
-0.5f, 0.5f, 0.0f, 0.0f, |
| 16 |
-0.5f, -0.5f, 0.0f, 1.0f, |
| 17 |
0.5f, -0.5f, 1.0f, 1.0f, |
| 18 |
0.5f, 0.5f, 1.0f, 0.0f |
| 19 |
}; |
| 20 |
|
| 21 |
static quint16 indexData[] = |
| 22 |
{ |
| 23 |
0, 1, 2, 0, 2, 3 |
| 24 |
}; |
| 25 |
|
| 26 |
static float triangleData[] = |
| 27 |
{ // Y up, CCW |
| 28 |
0.0f, 0.5f, 1.0f, 0.0f, 0.0f, |
| 29 |
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, |
| 30 |
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, |
| 31 |
}; |
| 32 |
|
| 33 |
struct { |
| 34 |
QList<QRhiResource *> releasePool; |
| 35 |
QRhiBuffer *vbuf = nullptr; |
| 36 |
QRhiBuffer *ibuf = nullptr; |
| 37 |
QRhiBuffer *ubuf = nullptr; |
| 38 |
QRhiTexture *msaaColorTexture = nullptr; |
| 39 |
QRhiTexture *msaaDepthTexture = nullptr; |
| 40 |
QRhiTextureRenderTarget *rt = nullptr; |
| 41 |
QRhiRenderPassDescriptor *rtRp = nullptr; |
| 42 |
QRhiTexture *tex = nullptr; |
| 43 |
QRhiTexture *depthTex = nullptr; |
| 44 |
QRhiSampler *sampler = nullptr; |
| 45 |
QRhiBuffer *triUbuf = nullptr; |
| 46 |
QRhiShaderResourceBindings *triSrb = nullptr; |
| 47 |
QRhiGraphicsPipeline *triPs = nullptr; |
| 48 |
QRhiShaderResourceBindings *srb = nullptr; |
| 49 |
QRhiGraphicsPipeline *ps = nullptr; |
| 50 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 51 |
QMatrix4x4 triBaseMvp; |
| 52 |
float triRot = 0; |
| 53 |
QMatrix4x4 winProj; |
| 54 |
} d; |
| 55 |
|
| 56 |
void Window::customInit() |
| 57 |
{ |
| 58 |
if (!m_r->isFeatureSupported(QRhi::MultisampleTexture)) |
| 59 |
qFatal("Multisample textures not supported by this backend"); |
| 60 |
|
| 61 |
// Skip the check for ResolveDepthStencil, and let it run regardless. When |
| 62 |
// not supported, it won't be functional, but should be handled somewhat |
| 63 |
// gracefully. |
| 64 |
|
| 65 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData) + sizeof(triangleData)); |
| 66 |
d.vbuf->create(); |
| 67 |
d.releasePool << d.vbuf; |
| 68 |
|
| 69 |
d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(indexData)); |
| 70 |
d.ibuf->create(); |
| 71 |
d.releasePool << d.ibuf; |
| 72 |
|
| 73 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 74 |
d.ubuf->create(); |
| 75 |
d.releasePool << d.ubuf; |
| 76 |
|
| 77 |
d.msaaColorTexture = m_r->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 4, QRhiTexture::RenderTarget); // 4x MSAA |
| 78 |
d.msaaColorTexture->create(); |
| 79 |
d.releasePool << d.msaaColorTexture; |
| 80 |
|
| 81 |
d.msaaDepthTexture = m_r->newTexture(QRhiTexture::D24S8, QSize(512, 512), 4, QRhiTexture::RenderTarget); // 4x MSAA |
| 82 |
d.msaaDepthTexture->create(); |
| 83 |
d.releasePool << d.msaaDepthTexture; |
| 84 |
|
| 85 |
// the non-msaa texture that will be the destination in the resolve |
| 86 |
d.tex = m_r->newTexture(QRhiTexture::RGBA8, d.msaaColorTexture->pixelSize(), 1, QRhiTexture::RenderTarget); |
| 87 |
d.releasePool << d.tex; |
| 88 |
d.tex->create(); |
| 89 |
|
| 90 |
d.depthTex = m_r->newTexture(QRhiTexture::D24S8, d.msaaDepthTexture->pixelSize(), 1, QRhiTexture::RenderTarget); |
| 91 |
d.releasePool << d.depthTex; |
| 92 |
d.depthTex->create(); |
| 93 |
|
| 94 |
QRhiTextureRenderTargetDescription rtDesc; |
| 95 |
QRhiColorAttachment rtAtt(d.msaaColorTexture); |
| 96 |
rtAtt.setResolveTexture(d.tex); |
| 97 |
rtDesc.setColorAttachments({ rtAtt }); |
| 98 |
rtDesc.setDepthTexture(d.msaaDepthTexture); |
| 99 |
rtDesc.setDepthResolveTexture(d.depthTex); |
| 100 |
|
| 101 |
d.rt = m_r->newTextureRenderTarget(rtDesc); |
| 102 |
d.releasePool << d.rt; |
| 103 |
d.rtRp = d.rt->newCompatibleRenderPassDescriptor(); |
| 104 |
d.releasePool << d.rtRp; |
| 105 |
d.rt->setRenderPassDescriptor(d.rtRp); |
| 106 |
d.rt->create(); |
| 107 |
|
| 108 |
d.triUbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 109 |
d.releasePool << d.triUbuf; |
| 110 |
d.triUbuf->create(); |
| 111 |
|
| 112 |
d.triSrb = m_r->newShaderResourceBindings(); |
| 113 |
d.releasePool << d.triSrb; |
| 114 |
d.triSrb->setBindings({ |
| 115 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.triUbuf) |
| 116 |
}); |
| 117 |
d.triSrb->create(); |
| 118 |
|
| 119 |
d.triPs = m_r->newGraphicsPipeline(); |
| 120 |
d.releasePool << d.triPs; |
| 121 |
d.triPs->setDepthTest(true); |
| 122 |
d.triPs->setDepthWrite(true); |
| 123 |
d.triPs->setSampleCount(4); // must match the render target |
| 124 |
d.triPs->setShaderStages({ |
| 125 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/color.vert.qsb")) }, |
| 126 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/color.frag.qsb")) } |
| 127 |
}); |
| 128 |
QRhiVertexInputLayout inputLayout; |
| 129 |
inputLayout.setBindings({ |
| 130 |
{ 5 * sizeof(float) } |
| 131 |
}); |
| 132 |
inputLayout.setAttributes({ |
| 133 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 134 |
{ 0, 1, QRhiVertexInputAttribute::Float3, quint32(2 * sizeof(float)) } |
| 135 |
}); |
| 136 |
d.triPs->setVertexInputLayout(inputLayout); |
| 137 |
d.triPs->setShaderResourceBindings(d.triSrb); |
| 138 |
d.triPs->setRenderPassDescriptor(d.rtRp); |
| 139 |
d.triPs->create(); |
| 140 |
|
| 141 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 142 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 143 |
d.releasePool << d.sampler; |
| 144 |
d.sampler->create(); |
| 145 |
|
| 146 |
d.srb = m_r->newShaderResourceBindings(); |
| 147 |
d.releasePool << d.srb; |
| 148 |
d.srb->setBindings({ |
| 149 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf), |
| 150 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.tex, d.sampler) |
| 151 |
}); |
| 152 |
d.srb->create(); |
| 153 |
|
| 154 |
d.ps = m_r->newGraphicsPipeline(); |
| 155 |
d.releasePool << d.ps; |
| 156 |
d.ps->setShaderStages({ |
| 157 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) }, |
| 158 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) } |
| 159 |
}); |
| 160 |
inputLayout.setBindings({ |
| 161 |
{ 4 * sizeof(float) } |
| 162 |
}); |
| 163 |
inputLayout.setAttributes({ |
| 164 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 165 |
{ 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) } |
| 166 |
}); |
| 167 |
d.ps->setVertexInputLayout(inputLayout); |
| 168 |
d.ps->setShaderResourceBindings(d.srb); |
| 169 |
d.ps->setRenderPassDescriptor(m_rp); |
| 170 |
d.ps->create(); |
| 171 |
|
| 172 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 173 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(vertexData), vertexData); |
| 174 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(vertexData), sizeof(triangleData), triangleData); |
| 175 |
d.initialUpdates->uploadStaticBuffer(d.ibuf, indexData); |
| 176 |
|
| 177 |
d.triBaseMvp = m_r->clipSpaceCorrMatrix(); |
| 178 |
d.triBaseMvp.perspective(45.0f, d.msaaColorTexture->pixelSize().width() / float(d.msaaColorTexture->pixelSize().height()), 0.01f, 1000.0f); |
| 179 |
d.triBaseMvp.translate(0, 0, -2); |
| 180 |
float opacity = 1.0f; |
| 181 |
d.initialUpdates->updateDynamicBuffer(d.triUbuf, 64, 4, &opacity); |
| 182 |
|
| 183 |
qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0; |
| 184 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, 64, 4, &flip); |
| 185 |
} |
| 186 |
|
| 187 |
void Window::customRelease() |
| 188 |
{ |
| 189 |
qDeleteAll(d.releasePool); |
| 190 |
d.releasePool.clear(); |
| 191 |
} |
| 192 |
|
| 193 |
void Window::customRender() |
| 194 |
{ |
| 195 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 196 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 197 |
if (d.initialUpdates) { |
| 198 |
u->merge(d.initialUpdates); |
| 199 |
d.initialUpdates->release(); |
| 200 |
d.initialUpdates = nullptr; |
| 201 |
} |
| 202 |
|
| 203 |
QMatrix4x4 triMvp = d.triBaseMvp; |
| 204 |
triMvp.rotate(d.triRot, 0, 1, 0); |
| 205 |
d.triRot += 1; |
| 206 |
u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData()); |
| 207 |
|
| 208 |
if (d.winProj != m_proj) { |
| 209 |
d.winProj = m_proj; |
| 210 |
QMatrix4x4 mvp = m_proj; |
| 211 |
mvp.scale(2.5f); |
| 212 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 213 |
} |
| 214 |
|
| 215 |
// offscreen (triangle, msaa) |
| 216 |
cb->beginPass(d.rt, QColor::fromRgbF(0.5f, 0.2f, 0.0f, 1.0f), { 1.0f, 0 }, u); |
| 217 |
cb->setGraphicsPipeline(d.triPs); |
| 218 |
cb->setViewport({ 0, 0, float(d.msaaColorTexture->pixelSize().width()), float(d.msaaColorTexture->pixelSize().height()) }); |
| 219 |
cb->setShaderResources(); |
| 220 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, quint32(sizeof(vertexData))); |
| 221 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 222 |
cb->draw(3); |
| 223 |
cb->endPass(); |
| 224 |
|
| 225 |
// onscreen (quad) |
| 226 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 227 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }); |
| 228 |
cb->setGraphicsPipeline(d.ps); |
| 229 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 230 |
cb->setShaderResources(); |
| 231 |
vbufBinding.second = 0; |
| 232 |
cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); |
| 233 |
cb->drawIndexed(6); |
| 234 |
cb->endPass(); |
| 235 |
} |