QtBase  v6.3.1
shadowmap.cpp
Go to the documentation of this file.
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 {
72  QRhiBuffer *vbuf = nullptr;
73  QRhiBuffer *ibuf = nullptr;
74  QRhiBuffer *ubuf = nullptr;
80  float cubeRot = 0;
81 
84  QRhiTexture *shadowMap = 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 {
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->create();
100  d.releasePool << d.vbuf;
101 
102  d.ibuf = m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData));
103  d.ibuf->create();
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->create();
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->create();
114 
117  d.releasePool << d.shadowSampler;
118  d.shadowSampler->setTextureCompareOp(QRhiSampler::Less);
119  d.shadowSampler->create();
120 
122  d.releasePool << d.srb;
123  const QRhiShaderResourceBinding::StageFlags stages = QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage;
126  d.srb->create();
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({
143  });
144  d.ps->setVertexInputLayout(inputLayout);
145  d.ps->setShaderResourceBindings(d.srb);
146  d.ps->setRenderPassDescriptor(m_rp);
147  d.ps->create();
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 
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->create();
162 
163  d.shadowSrb = m_r->newShaderResourceBindings();
164  d.releasePool << d.shadowSrb;
166  d.shadowSrb->create();
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({
181  });
182  d.shadowPs->setVertexInputLayout(inputLayout);
183  d.shadowPs->setShaderResourceBindings(d.shadowSrb);
184  d.shadowPs->setRenderPassDescriptor(d.rtRp);
185  d.shadowPs->create();
186 }
187 
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 
213 {
214  const QSize outputSizeInPixels = m_sc->currentPixelSize();
215  QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
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 }
small capitals from c petite p scientific f u
Definition: afcover.h:88
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:61
void lookAt(const QVector3D &eye, const QVector3D &center, const QVector3D &up)
float * data()
Definition: qmatrix4x4.h:1080
void perspective(float verticalAngle, float aspectRatio, float nearPlane, float farPlane)
const float * constData() const
Definition: qmatrix4x4.h:183
@ Immutable
Definition: qrhi_p.h:717
@ Dynamic
Definition: qrhi_p.h:719
@ IndexBuffer
Definition: qrhi_p.h:724
@ VertexBuffer
Definition: qrhi_p.h:723
@ UniformBuffer
Definition: qrhi_p.h:725
QPair< QRhiBuffer *, quint32 > VertexInput
Definition: qrhi_p.h:1426
QPair< int, quint32 > DynamicOffset
Definition: qrhi_p.h:1422
bool isClipDepthZeroToOne() const
Definition: qrhi.cpp:6277
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags={}) const
Definition: qrhi.cpp:6310
int ubufAligned(int v) const
Definition: qrhi.cpp:6207
QMatrix4x4 clipSpaceCorrMatrix() const
Definition: qrhi.cpp:6299
bool isYUpInNDC() const
Definition: qrhi.cpp:6252
QRhiShaderResourceBindings * newShaderResourceBindings()
Definition: qrhi.cpp:6543
QRhiBuffer * newBuffer(QRhiBuffer::Type type, QRhiBuffer::UsageFlags usage, int size)
Definition: qrhi.cpp:6562
QRhiSampler * newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter, QRhiSampler::Filter mipmapMode, QRhiSampler::AddressMode addressU, QRhiSampler::AddressMode addressV, QRhiSampler::AddressMode addressW=QRhiSampler::Repeat)
Definition: qrhi.cpp:6679
QRhiTextureRenderTarget * newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc, QRhiTextureRenderTarget::Flags flags={})
Definition: qrhi.cpp:6696
QRhiGraphicsPipeline * newGraphicsPipeline()
Definition: qrhi.cpp:6520
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition: qrhi.cpp:6609
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition: qrhi.cpp:5568
@ ClampToEdge
Definition: qrhi_p.h:884
static QRhiShaderResourceBinding uniformBufferWithDynamicOffset(int binding, StageFlags stage, QRhiBuffer *buf, int size)
Definition: qrhi.cpp:3342
static QRhiShaderResourceBinding sampledTexture(int binding, StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler)
Definition: qrhi.cpp:3374
@ RenderTarget
Definition: qrhi_p.h:766
void setDepthTexture(QRhiTexture *texture)
Definition: qrhi_p.h:519
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Definition: qrhi_p.h:257
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Definition: qrhi_p.h:268
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
constexpr int height() const noexcept
Definition: qsize.h:160
constexpr int width() const noexcept
Definition: qsize.h:157
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvectornd.h:205
std::unique_ptr< QRhiSwapChain > m_sc
Definition: window.h:89
std::unique_ptr< QRhiRenderPassDescriptor > m_rp
Definition: window.h:91
virtual void customRender()
QColor m_clearColor
Definition: examplefw.h:176
QStandardItemModel m
QMatrix4x4 m_proj
Definition: window.h:94
virtual void customInit()
QRhi * m_r
Definition: examplefw.h:161
qDeleteAll(list.begin(), list.end())
QShader getShader(const QString &name)
Definition: examplefw.h:86
unsigned int quint32
Definition: qglobal.h:288
unsigned short quint16
Definition: qglobal.h:286
int qint32
Definition: qglobal.h:287
#define qFatal
Definition: qlogging.h:181
GLbitfield stages
const GLfloat * m
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
QRhiTexture * shadowMap
Definition: shadowmap.cpp:84
QRhiSampler * shadowSampler
Definition: shadowmap.cpp:75
QRhiBuffer * ibuf
Definition: shadowmap.cpp:73
QRhiShaderResourceBindings * shadowSrb
Definition: shadowmap.cpp:85
QRhiBuffer * ubuf
Definition: shadowmap.cpp:74
QList< QRhiResource * > releasePool
Definition: shadowmap.cpp:71
QRhiTextureRenderTarget * rt
Definition: shadowmap.cpp:82
QRhiRenderPassDescriptor * rtRp
Definition: shadowmap.cpp:83
QRhiGraphicsPipeline * shadowPs
Definition: shadowmap.cpp:86
const int UBUF_SLOTS
Definition: shadowmap.cpp:91
const int SHADOW_UBLOCK_SIZE
Definition: shadowmap.cpp:90
float cubeRot
Definition: shadowmap.cpp:80
QRhiShaderResourceBindings * srb
Definition: shadowmap.cpp:76
QRhiBuffer * vbuf
Definition: shadowmap.cpp:72
QMatrix4x4 winProj
Definition: shadowmap.cpp:79
struct @923 d
QRhiGraphicsPipeline * ps
Definition: shadowmap.cpp:77
QRhiResourceUpdateBatch * initialUpdates
Definition: shadowmap.cpp:78
const int UBLOCK_SIZE
Definition: shadowmap.cpp:89