1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// Copyright (C) 2008-2012 NVIDIA Corporation.
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtQuick3DRuntimeRender/private/qssgrenderlayer_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendershadowmap_p.h>
#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
#include "qssgrendercontextcore.h"
QT_BEGIN_NAMESPACE
static constexpr quint32 NUM_TEXTURE_SIZES = 5;
static QRhiTexture *allocateRhiShadowTexture(QRhi *rhi, QRhiTexture::Format format, const QSize &size, quint32 numLayers, QRhiTexture::Flags flags)
{
auto texture = rhi->newTexture(format, size, 1, flags);
if (flags & QRhiTexture::TextureArray)
texture->setArraySize(numLayers);
if (!texture->create())
qWarning("Failed to create shadow map texture of size %dx%d", size.width(), size.height());
return texture;
}
static QRhiRenderBuffer *allocateRhiShadowRenderBuffer(QRhi *rhi, QRhiRenderBuffer::Type type, const QSize &size)
{
auto renderBuffer = rhi->newRenderBuffer(type, size, 1);
if (!renderBuffer->create())
qWarning("Failed to build depth-stencil buffer of size %dx%d", size.width(), size.height());
return renderBuffer;
}
static QRhiTexture::Format getShadowMapTextureFormat(QRhi *rhi, bool use32bit)
{
if (use32bit && rhi->isTextureFormatSupported(QRhiTexture::R32F))
return QRhiTexture::R32F;
if (rhi->isTextureFormatSupported(QRhiTexture::R16F))
return QRhiTexture::R16F;
return QRhiTexture::R16;
}
static quint8 mapSizeToIndex(quint32 mapSize)
{
Q_ASSERT(!(mapSize & (mapSize - 1)) && "shadow map resolution is power of 2");
Q_ASSERT(mapSize >= 256);
quint8 index = qCountTrailingZeroBits(mapSize) - 8;
Q_ASSERT(index < NUM_TEXTURE_SIZES);
return index;
}
static quint32 indexToMapSize(quint8 index)
{
Q_ASSERT(index < NUM_TEXTURE_SIZES);
return 1 << (index + 8);
}
QSSGRenderShadowMap::QSSGRenderShadowMap(const QSSGRenderContextInterface &inContext)
: m_context(inContext)
{
}
QSSGRenderShadowMap::~QSSGRenderShadowMap()
{
releaseCachedResources();
}
void QSSGRenderShadowMap::releaseCachedResources()
{
for (QSSGShadowMapEntry &entry : m_shadowMapList)
entry.destroyRhiResources();
for (auto &hash : m_depthTextureArrays) {
for (auto &textureArray : hash)
delete textureArray;
hash.clear();
}
m_shadowMapList.clear();
}
void QSSGRenderShadowMap::addShadowMaps(const QSSGShaderLightList &renderableLights)
{
QRhi *rhi = m_context.rhiContext()->rhi();
// Bail out if there is no QRhi, since we can't add entries without it
if (!rhi)
return;
constexpr quint32 MAX_SPLITS = 4;
const quint32 numLights = renderableLights.size();
qsizetype numShadows = 0;
std::array<std::array<quint8, NUM_TEXTURE_SIZES>, 2> textureSizeLayerCount = {}; // 0: 16 bit, 1: 32bit
QVarLengthArray<quint8, 16> lightIndexToLayerStartIndex;
lightIndexToLayerStartIndex.resize(numLights * MAX_SPLITS);
// NOTE: This is a quite ugly workaround. If 32bit is not supported then go through all lights and disable it.
const bool supports32BitTextures = rhi->isTextureFormatSupported(QRhiTexture::R32F);
if (!supports32BitTextures) {
bool any32bit = false;
for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
QSSGRenderLight *light = renderableLights.at(lightIndex).light;
any32bit = any32bit || light->m_use32BitShadowmap;
light->m_use32BitShadowmap = false;
}
static bool warned32bit = false;
if (!warned32bit && any32bit) {
qWarning() << "WARN: 32 bit shadow maps are unsupported, falling back to 16 bit.";
warned32bit = true;
}
}
for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
ShadowMapModes mapMode = (shaderLight.light->type == QSSGRenderLight::Type::PointLight) ? ShadowMapModes::CUBE
: ShadowMapModes::VSM;
if (shaderLight.shadows)
numShadows += 1;
if (!shaderLight.shadows || mapMode == ShadowMapModes::CUBE)
continue;
quint32 mapSize = shaderLight.light->m_shadowMapRes;
quint8 &layerCount = textureSizeLayerCount[shaderLight.light->m_use32BitShadowmap ? 1 : 0][mapSizeToIndex(mapSize)];
quint8 layerIndex = layerCount;
layerCount += shaderLight.light->m_csmNumSplits + 1;
lightIndexToLayerStartIndex[lightIndex] = layerIndex;
}
// Only recreate shadow assets if something has changed
bool needsRebuild = numShadows != shadowMapEntryCount();
if (!needsRebuild) {
// Check if relevant shadow properties has changed
for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
if (!shaderLight.shadows)
continue;
QSSGShadowMapEntry *pEntry = shadowMapEntry(lightIndex);
if (!pEntry) {
needsRebuild = true;
break;
}
QRhiTexture::Format textureFormat = getShadowMapTextureFormat(rhi, shaderLight.light->m_use32BitShadowmap);
ShadowMapModes mapMode = (shaderLight.light->type == QSSGRenderLight::Type::PointLight) ? ShadowMapModes::CUBE
: ShadowMapModes::VSM;
quint32 mapSize = shaderLight.light->m_shadowMapRes;
quint32 csmNumSplits = shaderLight.light->m_csmNumSplits;
quint32 layerIndex = mapMode == ShadowMapModes::VSM ? lightIndexToLayerStartIndex[lightIndex] : 0;
if (!pEntry->isCompatible(QSize(mapSize, mapSize), layerIndex, csmNumSplits, mapMode, textureFormat)) {
needsRebuild = true;
break;
}
}
}
if (!needsRebuild)
return;
releaseCachedResources();
// Create VSM texture arrays
for (quint32 hashI = 0; hashI < 2; ++hashI) {
const bool use32bit = hashI == 1;
QRhiTexture::Format rhiFormat = getShadowMapTextureFormat(rhi, use32bit);
for (quint32 sizeI = 0; sizeI < NUM_TEXTURE_SIZES; sizeI++) {
const quint32 numLayers = textureSizeLayerCount[hashI][sizeI];
if (numLayers == 0)
continue;
const quint32 mapSize = indexToMapSize(sizeI);
QSize texSize = QSize(mapSize, mapSize);
auto texture = allocateRhiShadowTexture(rhi, rhiFormat, texSize, numLayers, QRhiTexture::RenderTarget | QRhiTexture::TextureArray);
m_depthTextureArrays[hashI].insert(texSize, texture);
}
}
// Setup render targets
for (quint32 lightIdx = 0; lightIdx < numLights; ++lightIdx) {
const auto &shaderLight = renderableLights.at(lightIdx);
if (!shaderLight.shadows)
continue;
const bool use32bit = shaderLight.light->m_use32BitShadowmap;
QSize mapSize = QSize(shaderLight.light->m_shadowMapRes, shaderLight.light->m_shadowMapRes);
ShadowMapModes mapMode = (shaderLight.light->type == QSSGRenderLight::Type::PointLight) ? ShadowMapModes::CUBE
: ShadowMapModes::VSM;
switch (mapMode) {
case ShadowMapModes::VSM: {
quint32 layerStartIndex = lightIndexToLayerStartIndex.value(lightIdx);
quint32 csmNumSplits = shaderLight.light->m_csmNumSplits;
addDirectionalShadowMap(lightIdx, mapSize, use32bit, layerStartIndex, csmNumSplits, shaderLight.light->debugObjectName);
break;
}
case ShadowMapModes::CUBE: {
addCubeShadowMap(lightIdx, mapSize, use32bit, shaderLight.light->debugObjectName);
break;
}
default:
Q_UNREACHABLE();
break;
}
}
}
QSSGShadowMapEntry *QSSGRenderShadowMap::addDirectionalShadowMap(qint32 lightIdx,
QSize size,
bool use32bit,
quint32 layerStartIndex,
quint32 csmNumSplits,
const QString &renderNodeObjName)
{
QRhi *rhi = m_context.rhiContext()->rhi();
QSSGShadowMapEntry *pEntry = shadowMapEntry(lightIdx);
Q_ASSERT(rhi);
Q_ASSERT(!pEntry);
auto texture = m_depthTextureArrays[use32bit ? 1 : 0].value(size);
Q_ASSERT(texture);
m_shadowMapList.push_back(QSSGShadowMapEntry::withRhiDepthMap(lightIdx, ShadowMapModes::VSM, texture));
pEntry = &m_shadowMapList.back();
pEntry->m_csmNumSplits = csmNumSplits;
// Additional graphics resources: samplers, render targets.
for (quint32 splitIndex = 0; splitIndex < csmNumSplits + 1; splitIndex++) {
QRhiTextureRenderTarget *&rt(pEntry->m_rhiRenderTargets[splitIndex]);
Q_ASSERT(!rt);
pEntry->m_rhiDepthStencil[splitIndex] = allocateRhiShadowRenderBuffer(rhi, QRhiRenderBuffer::DepthStencil, size);
QRhiTextureRenderTargetDescription rtDesc;
QRhiColorAttachment attachment(pEntry->m_rhiDepthTextureArray);
attachment.setLayer(layerStartIndex + splitIndex);
rtDesc.setColorAttachments({ attachment });
rtDesc.setDepthStencilBuffer(pEntry->m_rhiDepthStencil[splitIndex]);
rt = rhi->newTextureRenderTarget(rtDesc);
rt->setDescription(rtDesc);
// The same renderpass descriptor can be reused since the
// format, load/store ops are the same regardless of the shadow mode.
if (!pEntry->m_rhiRenderPassDesc[splitIndex])
pEntry->m_rhiRenderPassDesc[splitIndex] = rt->newCompatibleRenderPassDescriptor();
rt->setRenderPassDescriptor(pEntry->m_rhiRenderPassDesc[splitIndex]);
if (!rt->create())
qWarning("Failed to build shadow map render target");
const QByteArray rtName = renderNodeObjName.toLatin1();
rt->setName(rtName + QByteArrayLiteral(" shadow map"));
}
pEntry->m_lightIndex = lightIdx;
pEntry->m_depthArrayIndex = layerStartIndex;
return pEntry;
}
QSSGShadowMapEntry *QSSGRenderShadowMap::addCubeShadowMap(qint32 lightIdx, QSize size, bool use32bit, const QString &renderNodeObjName)
{
QRhi *rhi = m_context.rhiContext()->rhi();
QSSGShadowMapEntry *pEntry = shadowMapEntry(lightIdx);
Q_ASSERT(rhi);
Q_ASSERT(!pEntry);
QRhiTexture::Format rhiFormat = getShadowMapTextureFormat(rhi, use32bit);
QRhiTexture *depthMap = allocateRhiShadowTexture(rhi, rhiFormat, size, 0, QRhiTexture::RenderTarget | QRhiTexture::CubeMap);
QRhiRenderBuffer *depthStencil = allocateRhiShadowRenderBuffer(rhi, QRhiRenderBuffer::DepthStencil, size);
m_shadowMapList.push_back(QSSGShadowMapEntry::withRhiDepthCubeMap(lightIdx, ShadowMapModes::CUBE, depthMap, depthStencil));
pEntry = &m_shadowMapList.back();
const QByteArray rtName = renderNodeObjName.toLatin1();
for (const auto face : QSSGRenderTextureCubeFaces) {
QRhiTextureRenderTarget *&rt(pEntry->m_rhiRenderTargets[quint8(face)]);
Q_ASSERT(!rt);
QRhiColorAttachment att(pEntry->m_rhiDepthCube);
att.setLayer(quint8(face)); // 6 render targets, each referencing one face of the cubemap
QRhiTextureRenderTargetDescription rtDesc;
rtDesc.setColorAttachments({ att });
rtDesc.setDepthStencilBuffer(pEntry->m_rhiDepthStencil[0]);
rt = rhi->newTextureRenderTarget(rtDesc);
rt->setDescription(rtDesc);
if (!pEntry->m_rhiRenderPassDesc[0])
pEntry->m_rhiRenderPassDesc[0] = rt->newCompatibleRenderPassDescriptor();
rt->setRenderPassDescriptor(pEntry->m_rhiRenderPassDesc[0]);
if (!rt->create())
qWarning("Failed to build shadow map render target");
rt->setName(rtName + QByteArrayLiteral(" shadow cube face: ") + QSSGBaseTypeHelpers::displayName(face));
}
return pEntry;
}
QSSGShadowMapEntry *QSSGRenderShadowMap::shadowMapEntry(int lightIdx)
{
Q_ASSERT(lightIdx >= 0);
for (int i = 0; i < m_shadowMapList.size(); i++) {
QSSGShadowMapEntry *pEntry = &m_shadowMapList[i];
if (pEntry->m_lightIndex == quint32(lightIdx))
return pEntry;
}
return nullptr;
}
QSSGShadowMapEntry::QSSGShadowMapEntry()
: m_lightIndex(std::numeric_limits<quint32>::max())
, m_shadowMapMode(ShadowMapModes::VSM)
{
}
QSSGShadowMapEntry QSSGShadowMapEntry::withRhiDepthMap(quint32 lightIdx, ShadowMapModes mode, QRhiTexture *textureArray)
{
QSSGShadowMapEntry e;
e.m_lightIndex = lightIdx;
e.m_shadowMapMode = mode;
e.m_rhiDepthTextureArray = textureArray;
return e;
}
QSSGShadowMapEntry QSSGShadowMapEntry::withRhiDepthCubeMap(quint32 lightIdx, ShadowMapModes mode, QRhiTexture *depthCube, QRhiRenderBuffer *depthStencil)
{
QSSGShadowMapEntry e;
e.m_lightIndex = lightIdx;
e.m_shadowMapMode = mode;
e.m_rhiDepthCube = depthCube;
e.m_rhiDepthStencil[0] = depthStencil;
return e;
}
bool QSSGShadowMapEntry::isCompatible(QSize mapSize, quint32 layerIndex, quint32 csmNumSplits, ShadowMapModes mapMode, QRhiTexture::Format textureFormat)
{
if (csmNumSplits != m_csmNumSplits)
return false;
if (mapMode != m_shadowMapMode)
return false;
switch (mapMode) {
case ShadowMapModes::CUBE: {
if (mapSize != m_rhiDepthCube->pixelSize()) {
return false;
}
break;
}
case ShadowMapModes::VSM: {
if (mapSize != m_rhiDepthTextureArray->pixelSize() || int(layerIndex) >= m_rhiDepthTextureArray->arraySize()
|| textureFormat != m_rhiDepthTextureArray->format()) {
return false;
}
break;
}
default:
Q_UNREACHABLE();
break;
}
return true;
}
void QSSGShadowMapEntry::destroyRhiResources()
{
m_rhiDepthTextureArray = nullptr;
delete m_rhiDepthCube;
m_rhiDepthCube = nullptr;
qDeleteAll(m_rhiDepthStencil);
m_rhiDepthStencil.fill(nullptr);
qDeleteAll(m_rhiRenderTargets);
m_rhiRenderTargets.fill(nullptr);
qDeleteAll(m_rhiRenderPassDesc);
m_rhiRenderPassDesc.fill(nullptr);
}
QT_END_NAMESPACE
|