| 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 |
#include "../shared/cube.h" |
| 6 |
|
| 7 |
// Depth texture / shadow sampler / shadow map example. |
| 8 |
// Not available on GLES 2.0. |
| 9 |
|
| 10 |
static float quadVertexData[] = |
| 11 |
{ // Y up, CCW, x-y-z |
| 12 |
-0.5f, 0.5f, 0.0f, |
| 13 |
-0.5f, -0.5f, 0.0f, |
| 14 |
0.5f, -0.5f, 0.0f, |
| 15 |
0.5f, 0.5f, 0.0f, |
| 16 |
}; |
| 17 |
|
| 18 |
static quint16 quadIndexData[] = |
| 19 |
{ |
| 20 |
0, 1, 2, 0, 2, 3 |
| 21 |
}; |
| 22 |
|
| 23 |
struct { |
| 24 |
QList<QRhiResource *> releasePool; |
| 25 |
QRhiBuffer *vbuf = nullptr; |
| 26 |
QRhiBuffer *ibuf = nullptr; |
| 27 |
QRhiBuffer *ubuf = nullptr; |
| 28 |
QRhiSampler *shadowSampler = nullptr; |
| 29 |
QRhiShaderResourceBindings *srb = nullptr; |
| 30 |
QRhiGraphicsPipeline *ps = nullptr; |
| 31 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 32 |
QMatrix4x4 winProj; |
| 33 |
float cubeRot = 0; |
| 34 |
|
| 35 |
QRhiTextureRenderTarget *rt = nullptr; |
| 36 |
QRhiRenderPassDescriptor *rtRp = nullptr; |
| 37 |
QRhiTexture *shadowMap = nullptr; |
| 38 |
QRhiShaderResourceBindings *shadowSrb = nullptr; |
| 39 |
QRhiGraphicsPipeline *shadowPs = nullptr; |
| 40 |
} d; |
| 41 |
|
| 42 |
const int UBLOCK_SIZE = 64 * 3 + 4; |
| 43 |
const int SHADOW_UBLOCK_SIZE = 64 * 1; |
| 44 |
const int UBUF_SLOTS = 4; // 2 objects * 2 passes with different cameras |
| 45 |
|
| 46 |
void Window::customInit() |
| 47 |
{ |
| 48 |
if (!m_r->isTextureFormatSupported(QRhiTexture::D32F)) |
| 49 |
qFatal("Depth texture is not supported"); |
| 50 |
|
| 51 |
d.vbuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData) + sizeof(cube)); |
| 52 |
d.vbuf->create(); |
| 53 |
d.releasePool << d.vbuf; |
| 54 |
|
| 55 |
d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData)); |
| 56 |
d.ibuf->create(); |
| 57 |
d.releasePool << d.ibuf; |
| 58 |
|
| 59 |
const int oneRoundedUniformBlockSize = m_r->ubufAligned(UBLOCK_SIZE); |
| 60 |
d.ubuf = m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, UBUF_SLOTS * oneRoundedUniformBlockSize); |
| 61 |
d.ubuf->create(); |
| 62 |
d.releasePool << d.ubuf; |
| 63 |
|
| 64 |
d.shadowMap = m_r->newTexture(QRhiTexture::D32F, QSize(1024, 1024), 1, QRhiTexture::RenderTarget); |
| 65 |
d.releasePool << d.shadowMap; |
| 66 |
d.shadowMap->create(); |
| 67 |
|
| 68 |
d.shadowSampler = m_r->newSampler(QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None, |
| 69 |
QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge); |
| 70 |
d.releasePool << d.shadowSampler; |
| 71 |
d.shadowSampler->setTextureCompareOp(QRhiSampler::Less); |
| 72 |
d.shadowSampler->create(); |
| 73 |
|
| 74 |
d.srb = m_r->newShaderResourceBindings(); |
| 75 |
d.releasePool << d.srb; |
| 76 |
const QRhiShaderResourceBinding::StageFlags stages = QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage; |
| 77 |
d.srb->setBindings({ QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, stages, d.ubuf, UBLOCK_SIZE), |
| 78 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, d.shadowMap, d.shadowSampler) }); |
| 79 |
d.srb->create(); |
| 80 |
|
| 81 |
d.ps = m_r->newGraphicsPipeline(); |
| 82 |
d.releasePool << d.ps; |
| 83 |
d.ps->setShaderStages({ |
| 84 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/main.vert.qsb")) }, |
| 85 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/main.frag.qsb")) } |
| 86 |
}); |
| 87 |
d.ps->setDepthTest(true); |
| 88 |
d.ps->setDepthWrite(true); |
| 89 |
// fits both the quad and cube vertex data |
| 90 |
QRhiVertexInputLayout inputLayout; |
| 91 |
inputLayout.setBindings({ |
| 92 |
{ 3 * sizeof(float) } |
| 93 |
}); |
| 94 |
inputLayout.setAttributes({ |
| 95 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 }, |
| 96 |
}); |
| 97 |
d.ps->setVertexInputLayout(inputLayout); |
| 98 |
d.ps->setShaderResourceBindings(d.srb); |
| 99 |
d.ps->setRenderPassDescriptor(m_rp); |
| 100 |
d.ps->create(); |
| 101 |
|
| 102 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 103 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, 0, sizeof(quadVertexData), quadVertexData); |
| 104 |
d.initialUpdates->uploadStaticBuffer(d.vbuf, sizeof(quadVertexData), sizeof(cube), cube); |
| 105 |
d.initialUpdates->uploadStaticBuffer(d.ibuf, quadIndexData); |
| 106 |
|
| 107 |
QRhiTextureRenderTargetDescription rtDesc; |
| 108 |
rtDesc.setDepthTexture(d.shadowMap); |
| 109 |
d.rt = m_r->newTextureRenderTarget(rtDesc); |
| 110 |
d.releasePool << d.rt; |
| 111 |
d.rtRp = d.rt->newCompatibleRenderPassDescriptor(); |
| 112 |
d.releasePool << d.rtRp; |
| 113 |
d.rt->setRenderPassDescriptor(d.rtRp); |
| 114 |
d.rt->create(); |
| 115 |
|
| 116 |
d.shadowSrb = m_r->newShaderResourceBindings(); |
| 117 |
d.releasePool << d.shadowSrb; |
| 118 |
d.shadowSrb->setBindings({ QRhiShaderResourceBinding::uniformBufferWithDynamicOffset(0, stages, d.ubuf, SHADOW_UBLOCK_SIZE) }); |
| 119 |
d.shadowSrb->create(); |
| 120 |
|
| 121 |
d.shadowPs = m_r->newGraphicsPipeline(); |
| 122 |
d.releasePool << d.shadowPs; |
| 123 |
d.shadowPs->setShaderStages({ |
| 124 |
{ QRhiShaderStage::Vertex, getShader(QLatin1String(":/shadowmap.vert.qsb")) }, |
| 125 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/shadowmap.frag.qsb")) } |
| 126 |
}); |
| 127 |
d.shadowPs->setDepthTest(true); |
| 128 |
d.shadowPs->setDepthWrite(true); |
| 129 |
inputLayout.setBindings({ |
| 130 |
{ 3 * sizeof(float) } |
| 131 |
}); |
| 132 |
inputLayout.setAttributes({ |
| 133 |
{ 0, 0, QRhiVertexInputAttribute::Float3, 0 } |
| 134 |
}); |
| 135 |
d.shadowPs->setVertexInputLayout(inputLayout); |
| 136 |
d.shadowPs->setShaderResourceBindings(d.shadowSrb); |
| 137 |
d.shadowPs->setRenderPassDescriptor(d.rtRp); |
| 138 |
d.shadowPs->create(); |
| 139 |
} |
| 140 |
|
| 141 |
void Window::customRelease() |
| 142 |
{ |
| 143 |
qDeleteAll(d.releasePool); |
| 144 |
d.releasePool.clear(); |
| 145 |
} |
| 146 |
|
| 147 |
static void enqueueScene(QRhiCommandBuffer *cb, QRhiShaderResourceBindings *srb, int oneRoundedUniformBlockSize, int firstUbufSlot) |
| 148 |
{ |
| 149 |
QRhiCommandBuffer::DynamicOffset ubufOffset(0, quint32(firstUbufSlot * oneRoundedUniformBlockSize)); |
| 150 |
// draw the ground (the quad) |
| 151 |
cb->setShaderResources(srb, 1, &ubufOffset); |
| 152 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vbuf, 0); |
| 153 |
cb->setVertexInput(0, 1, &vbufBinding, d.ibuf, 0, QRhiCommandBuffer::IndexUInt16); |
| 154 |
cb->drawIndexed(6); |
| 155 |
|
| 156 |
// Draw the object (the cube). Both vertex and uniform data are in the same |
| 157 |
// buffer, right after the quad's. |
| 158 |
ubufOffset.second += oneRoundedUniformBlockSize; |
| 159 |
cb->setShaderResources(srb, 1, &ubufOffset); |
| 160 |
vbufBinding.second = sizeof(quadVertexData); |
| 161 |
cb->setVertexInput(0, 1, &vbufBinding); |
| 162 |
cb->draw(36); |
| 163 |
} |
| 164 |
|
| 165 |
void Window::customRender() |
| 166 |
{ |
| 167 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 168 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 169 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 170 |
if (d.initialUpdates) { |
| 171 |
u->merge(d.initialUpdates); |
| 172 |
d.initialUpdates->release(); |
| 173 |
d.initialUpdates = nullptr; |
| 174 |
} |
| 175 |
|
| 176 |
const int oneRoundedUniformBlockSize = m_r->ubufAligned(UBLOCK_SIZE); |
| 177 |
|
| 178 |
QMatrix4x4 shadowBias; |
| 179 |
// fill it in column-major order to keep our sanity (ctor would take row-major) |
| 180 |
float *sbp = shadowBias.data(); |
| 181 |
if (m_r->isClipDepthZeroToOne()) { |
| 182 |
// convert x, y [-1, 1] -> [0, 1] |
| 183 |
*sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 0.0f; |
| 184 |
*sbp++ = 0.0f; *sbp++ = m_r->isYUpInNDC() ? -0.5f : 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f; |
| 185 |
*sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 1.0f; *sbp++ = 0.0f; |
| 186 |
*sbp++ = 0.5f; *sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 1.0f; |
| 187 |
} else { |
| 188 |
// convert x, y, z [-1, 1] -> [0, 1] |
| 189 |
*sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 0.0f; |
| 190 |
*sbp++ = 0.0f; *sbp++ = 0.5f; *sbp++ = 0.0f; *sbp++ = 0.0f; |
| 191 |
*sbp++ = 0.0f; *sbp++ = 0.0f; *sbp++ = 0.5f; *sbp++ = 0.0f; |
| 192 |
*sbp++ = 0.5f; *sbp++ = 0.5f; *sbp++ = 0.5f; *sbp++ = 1.0f; |
| 193 |
} |
| 194 |
|
| 195 |
const QVector3D lightPos(5, 10, 10); |
| 196 |
QMatrix4x4 lightViewProj = m_r->clipSpaceCorrMatrix(); |
| 197 |
lightViewProj.perspective(45.0f, 1, 0.1f, 100.0f); |
| 198 |
lightViewProj.lookAt(lightPos, QVector3D(0, 0, 0), QVector3D(0, 1, 0)); |
| 199 |
|
| 200 |
// uniform data for the ground |
| 201 |
if (d.winProj != m_proj) { |
| 202 |
d.winProj = m_proj; |
| 203 |
|
| 204 |
QMatrix4x4 m; |
| 205 |
m.scale(4.0f); |
| 206 |
m.rotate(-60, 1, 0, 0); |
| 207 |
|
| 208 |
// for the main pass |
| 209 |
const QMatrix4x4 mvp = m_proj * m; // m_proj is in fact projection * view |
| 210 |
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData()); |
| 211 |
const QMatrix4x4 shadowMvp = lightViewProj * m; |
| 212 |
u->updateDynamicBuffer(d.ubuf, 64, 64, shadowMvp.constData()); |
| 213 |
u->updateDynamicBuffer(d.ubuf, 128, 64, shadowBias.constData()); |
| 214 |
qint32 useShadows = 1; |
| 215 |
u->updateDynamicBuffer(d.ubuf, 192, 4, &useShadows); |
| 216 |
|
| 217 |
// for the shadow pass |
| 218 |
u->updateDynamicBuffer(d.ubuf, 2 * oneRoundedUniformBlockSize, 64, shadowMvp.constData()); |
| 219 |
} |
| 220 |
|
| 221 |
// uniform data for the rotating cube |
| 222 |
QMatrix4x4 m; |
| 223 |
m.translate(0, 0.5f, 2); |
| 224 |
m.scale(0.2f); |
| 225 |
m.rotate(d.cubeRot, 0, 1, 0); |
| 226 |
m.rotate(45, 1, 0, 0); |
| 227 |
d.cubeRot += 1; |
| 228 |
|
| 229 |
// for the main pass |
| 230 |
const QMatrix4x4 mvp = m_proj * m; |
| 231 |
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize, 64, mvp.constData()); |
| 232 |
const QMatrix4x4 shadowMvp = lightViewProj * m; |
| 233 |
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 64, 64, shadowMvp.constData()); |
| 234 |
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 128, 64, shadowBias.constData()); |
| 235 |
qint32 useShadows = 0; |
| 236 |
u->updateDynamicBuffer(d.ubuf, oneRoundedUniformBlockSize + 192, 4, &useShadows); |
| 237 |
|
| 238 |
// for the shadow pass |
| 239 |
u->updateDynamicBuffer(d.ubuf, 3 * oneRoundedUniformBlockSize, 64, shadowMvp.constData()); |
| 240 |
|
| 241 |
cb->resourceUpdate(u); |
| 242 |
|
| 243 |
// shadow pass |
| 244 |
const QSize shadowMapSize = d.shadowMap->pixelSize(); |
| 245 |
cb->beginPass(d.rt, QColor(), { 1.0f, 0 }); |
| 246 |
cb->setGraphicsPipeline(d.shadowPs); |
| 247 |
cb->setViewport({ 0, 0, float(shadowMapSize.width()), float(shadowMapSize.height()) }); |
| 248 |
enqueueScene(cb, d.shadowSrb, oneRoundedUniformBlockSize, 2); |
| 249 |
cb->endPass(); |
| 250 |
|
| 251 |
// main pass |
| 252 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }); |
| 253 |
cb->setGraphicsPipeline(d.ps); |
| 254 |
cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 255 |
enqueueScene(cb, d.srb, oneRoundedUniformBlockSize, 0); |
| 256 |
cb->endPass(); |
| 257 |
} |