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