| 1 |
// Copyright (C) 2018 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "../shared/examplefw.h" |
| 5 |
|
| 6 |
// Exercises MRT, by attaching 4 textures to a single QRhiTextureRenderTarget. |
| 7 |
// The fragment shader outputs to all four targets. |
| 8 |
// The textures are then used to render 4 quads, so their contents can be confirmed. |
| 9 |
|
| 10 |
const int ATTCOUNT = 4; |
| 11 |
|
| 12 |
static float quadVertexData[] = |
| 13 |
{ // Y up, CCW |
| 14 |
-0.5f, 0.5f, 0.0f, 0.0f, |
| 15 |
-0.5f, -0.5f, 0.0f, 1.0f, |
| 16 |
0.5f, -0.5f, 1.0f, 1.0f, |
| 17 |
0.5f, 0.5f, 1.0f, 0.0f |
| 18 |
}; |
| 19 |
|
| 20 |
static quint16 quadIndexData[] = |
| 21 |
{ |
| 22 |
0, 1, 2, 0, 2, 3 |
| 23 |
}; |
| 24 |
|
| 25 |
static float triangleData[] = |
| 26 |
{ // Y up, CCW |
| 27 |
0.0f, 0.5f, 1.0f, 0.0f, 0.0f, |
| 28 |
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, |
| 29 |
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, |
| 30 |
}; |
| 31 |
|
| 32 |
struct { |
| 33 |
QList<QRhiResource *> releasePool; |
| 34 |
QRhiBuffer *vbuf = nullptr; |
| 35 |
QRhiBuffer *ibuf = nullptr; |
| 36 |
QRhiBuffer *ubuf = nullptr; |
| 37 |
QRhiTextureRenderTarget *rt = nullptr; |
| 38 |
QRhiRenderPassDescriptor *rtRp = nullptr; |
| 39 |
QRhiSampler *sampler = nullptr; |
| 40 |
QRhiGraphicsPipeline *ps = nullptr; |
| 41 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 42 |
QMatrix4x4 winProj; |
| 43 |
struct PerColorBuffer { |
| 44 |
QRhiTexture *tex = nullptr; |
| 45 |
QRhiShaderResourceBindings *srb = nullptr; |
| 46 |
}; |
| 47 |
PerColorBuffer colData[ATTCOUNT]; |
| 48 |
|
| 49 |
QRhiBuffer *triUbuf = nullptr; |
| 50 |
QRhiShaderResourceBindings *triSrb = nullptr; |
| 51 |
QRhiGraphicsPipeline *triPs = nullptr; |
| 52 |
float triRot = 0; |
| 53 |
QMatrix4x4 triBaseMvp; |
| 54 |
} d; |
| 55 |
|
| 56 |
void Window::customInit() |
| 57 |
{ |
| 58 |
qDebug("Max color attachments: %d", m_r->resourceLimit(QRhi::MaxColorAttachments)); |
| 59 |
if (m_r->resourceLimit(QRhi::MaxColorAttachments) < 4) |
| 60 |
qWarning("MRT is not supported"); |
| 61 |
|
| 62 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData) + sizeof(triangleData)); |
| 63 |
d.vbuf->create(); |
| 64 |
d.releasePool << d.vbuf; |
| 65 |
|
| 66 |
d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData)); |
| 67 |
d.ibuf->create(); |
| 68 |
d.releasePool << d.ibuf; |
| 69 |
|
| 70 |
const int oneRoundedUniformBlockSize = m_r->ubufAligned(68); |
| 71 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, oneRoundedUniformBlockSize * ATTCOUNT); |
| 72 |
d.ubuf->create(); |
| 73 |
d.releasePool << d.ubuf; |
| 74 |
|
| 75 |
d.sampler = m_r->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 76 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 77 |
d.releasePool << d.sampler; |
| 78 |
d.sampler->create(); |
| 79 |
|
| 80 |
for (int i = 0; i < ATTCOUNT; ++i) { |
| 81 |
QRhiTexture *tex = m_r->newTexture(QRhiTexture::RGBA8, QSize(512, 512), 1, QRhiTexture::RenderTarget); |
| 82 |
d.releasePool << tex; |
| 83 |
tex->create(); |
| 84 |
|
| 85 |
QRhiShaderResourceBindings *srb = m_r->newShaderResourceBindings(); |
| 86 |
d.releasePool << srb; |
| 87 |
srb->setBindings({ |
| 88 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, |
| 89 |
d.ubuf, i * oneRoundedUniformBlockSize, 68), |
| 90 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, tex, d.sampler) |
| 91 |
}); |
| 92 |
srb->create(); |
| 93 |
|
| 94 |
d.colData[i].tex = tex; |
| 95 |
d.colData[i].srb = srb; |
| 96 |
} |
| 97 |
|
| 98 |
QRhiTextureRenderTargetDescription rtDesc; |
| 99 |
QRhiColorAttachment att[ATTCOUNT]; |
| 100 |
for (int i = 0; i < ATTCOUNT; ++i) |
| 101 |
att[i] = QRhiColorAttachment(d.colData[i].tex); |
| 102 |
rtDesc.setColorAttachments(att, att + ATTCOUNT); |
| 103 |
d.rt = m_r->newTextureRenderTarget(rtDesc); |
| 104 |
d.releasePool << d.rt; |
| 105 |
d.rtRp = d.rt->newCompatibleRenderPassDescriptor(); |
| 106 |
d.releasePool << d.rtRp; |
| 107 |
d.rt->setRenderPassDescriptor(d.rtRp); |
| 108 |
d.rt->create(); |
| 109 |
|
| 110 |
d.ps = m_r->newGraphicsPipeline(); |
| 111 |
d.releasePool << d.ps; |
| 112 |
d.ps->setShaderStages({ |
| 113 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture.vert.qsb")) }, |
| 114 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture.frag.qsb")) } |
| 115 |
}); |
| 116 |
QRhiVertexInputLayout inputLayout; |
| 117 |
inputLayout.setBindings({ |
| 118 |
{ 4 * sizeof(float) } |
| 119 |
}); |
| 120 |
inputLayout.setAttributes({ |
| 121 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 122 |
{ 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) } |
| 123 |
}); |
| 124 |
d.ps->setVertexInputLayout(inputLayout); |
| 125 |
d.ps->setShaderResourceBindings(d.colData[0].srb); // all of them are layout-compatible |
| 126 |
d.ps->setRenderPassDescriptor(m_rp); |
| 127 |
d.ps->create(); |
| 128 |
|
| 129 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 130 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(quadVertexData), quadVertexData); |
| 131 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(quadVertexData), sizeof(triangleData), triangleData); |
| 132 |
d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData); |
| 133 |
|
| 134 |
qint32 flip = m_r->isYUpInFramebuffer() ? 1 : 0; |
| 135 |
for (int i = 0; i < ATTCOUNT; ++i) |
| 136 |
d.initialUpdates->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize + 64, 4, &flip); |
| 137 |
|
| 138 |
// triangle |
| 139 |
d.triUbuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 68); |
| 140 |
d.releasePool << d.triUbuf; |
| 141 |
d.triUbuf->create(); |
| 142 |
|
| 143 |
d.triSrb = m_r->newShaderResourceBindings(); |
| 144 |
d.releasePool << d.triSrb; |
| 145 |
d.triSrb->setBindings({ |
| 146 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.triUbuf) |
| 147 |
}); |
| 148 |
d.triSrb->create(); |
| 149 |
|
| 150 |
d.triPs = m_r->newGraphicsPipeline(); |
| 151 |
d.releasePool << d.triPs; |
| 152 |
d.triPs->setShaderStages({ |
| 153 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/mrt.vert.qsb")) }, |
| 154 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/mrt.frag.qsb")) } |
| 155 |
}); |
| 156 |
|
| 157 |
QRhiGraphicsPipeline::TargetBlend blends[ATTCOUNT]; // defaults to blending == false |
| 158 |
d.triPs->setTargetBlends(blends, blends + ATTCOUNT); |
| 159 |
|
| 160 |
inputLayout.setBindings({ |
| 161 |
{ 5 * sizeof(float) } |
| 162 |
}); |
| 163 |
inputLayout.setAttributes({ |
| 164 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 165 |
{ 0, 1, QRhiVertexInputAttribute::Float3, quint32(2 * sizeof(float)) } |
| 166 |
}); |
| 167 |
d.triPs->setVertexInputLayout(inputLayout); |
| 168 |
d.triPs->setShaderResourceBindings(d.triSrb); |
| 169 |
d.triPs->setRenderPassDescriptor(d.rtRp); |
| 170 |
d.triPs->create(); |
| 171 |
|
| 172 |
d.triBaseMvp = m_r->clipSpaceCorrMatrix(); |
| 173 |
d.triBaseMvp.perspective(45.0f, d.rt->pixelSize().width() / float(d.rt->pixelSize().height()), 0.01f, 1000.0f); |
| 174 |
d.triBaseMvp.translate(0, 0, -2); |
| 175 |
float opacity = 1.0f; |
| 176 |
d.initialUpdates->updateDynamicBuffer(d.triUbuf, 64, 4, &opacity); |
| 177 |
} |
| 178 |
|
| 179 |
void Window::customRelease() |
| 180 |
{ |
| 181 |
qDeleteAll(d.releasePool); |
| 182 |
d.releasePool.clear(); |
| 183 |
} |
| 184 |
|
| 185 |
void Window::customRender() |
| 186 |
{ |
| 187 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 188 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 189 |
if (d.initialUpdates) { |
| 190 |
u->merge(d.initialUpdates); |
| 191 |
d.initialUpdates->release(); |
| 192 |
d.initialUpdates = nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
QMatrix4x4 triMvp = d.triBaseMvp; |
| 196 |
triMvp.rotate(d.triRot, 0, 1, 0); |
| 197 |
d.triRot += 1; |
| 198 |
u->updateDynamicBuffer(d.triUbuf, 0, 64, triMvp.constData()); |
| 199 |
|
| 200 |
cb->beginPass(d.rt, QColor::fromRgbF(0.5f, 0.2f, 0.0f, 1.0f), { 1.0f, 0 }, u); |
| 201 |
cb->setGraphicsPipeline(d.triPs); |
| 202 |
cb->setViewport({ 0, 0, float(d.rt->pixelSize().width()), float(d.rt->pixelSize().height()) }); |
| 203 |
cb->setShaderResources(); |
| 204 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, quint32(sizeof(quadVertexData))); |
| 205 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 206 |
cb->draw(3); |
| 207 |
cb->endPass(); |
| 208 |
|
| 209 |
u = m_r->nextResourceUpdateBatch(); |
| 210 |
if (d.winProj != m_proj) { |
| 211 |
d.winProj = m_proj; |
| 212 |
const int oneRoundedUniformBlockSize = m_r->ubufAligned(68); |
| 213 |
for (int i = 0; i < ATTCOUNT; ++i) { |
| 214 |
QMatrix4x4 mvp = m_proj; |
| 215 |
switch (i) { |
| 216 |
case 0: |
| 217 |
mvp.translate(-2.0f, 0, 0); |
| 218 |
break; |
| 219 |
case 1: |
| 220 |
mvp.translate(-0.8f, 0, 0); |
| 221 |
break; |
| 222 |
case 2: |
| 223 |
mvp.translate(0.4f, 0, 0); |
| 224 |
break; |
| 225 |
case 3: |
| 226 |
mvp.translate(1.6f, 0, 0); |
| 227 |
break; |
| 228 |
default: |
| 229 |
Q_UNREACHABLE(); |
| 230 |
break; |
| 231 |
} |
| 232 |
u->updateDynamicBuffer(d.ubuf, i * oneRoundedUniformBlockSize, 64, mvp.constData()); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 237 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 238 |
cb->setGraphicsPipeline(d.ps); |
| 239 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 240 |
vbufBinding.second = 0; |
| 241 |
cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); |
| 242 |
for (int i = 0; i < ATTCOUNT; ++i) { |
| 243 |
cb->setShaderResources(d.colData[i].srb); |
| 244 |
cb->drawIndexed(6); |
| 245 |
} |
| 246 |
cb->endPass(); |
| 247 |
} |