Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgrendershadowmap.cpp
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6
7#include <QtQuick3DRuntimeRender/private/qssgrenderlayer_p.h>
8#include <QtQuick3DRuntimeRender/private/qssgrendershadowmap_p.h>
9#include <QtQuick3DRuntimeRender/private/qssglayerrenderdata_p.h>
11
12#include <algorithm>
13#include <iterator>
14
15QT_BEGIN_NAMESPACE
16
17namespace AtlasHelpers {
18
20 struct Shelf {
21 int y;
22 int height;
23 int curX;
24 };
25
26 struct ShelfPage {
28 int width;
29 int height;
30 int curY;
32 };
33
35 bool success;
37 int x;
38 int y;
39 };
40 const int pageWidth;
41 const int pageHeight;
43
44 ShelfPacker(int pageWidth, int pageHeight)
45 : pageWidth(pageWidth)
46 , pageHeight(pageHeight)
47 {
48 }
49
50 AtlasPlacement addRectangle(int sizeNeeded) {
51 // Try each page in turn
52 for (auto &page : pages) {
53 AtlasPlacement placement = placeOnPage(page, sizeNeeded);
54 if (placement.success)
55 return placement;
56 }
57
58 // If we get here, we need a new page
59 ShelfPage newPage;
60 newPage.pageIndex = int(pages.size());
61 newPage.width = pageWidth;
62 newPage.height = pageHeight;
63 newPage.curY = 0;
64 newPage.shelves.clear();
65 pages.push_back(newPage);
66
67 // now place in the new page
68 return placeOnPage(pages.back(), sizeNeeded);
69 }
70
71 AtlasPlacement placeOnPage(ShelfPage &page, int sizeNeeded)
72 {
73 AtlasPlacement result;
74 result.success = false;
75
76 // Iterate over shelves to see if we can place the rect
77 for (auto &shelf : page.shelves) {
78 // check if it fits horizontally
79 if (shelf.curX + sizeNeeded <= page.width) {
80 // also check if the shelf's height is enough
81 // but here we store the shelf's "height" as the max of the rectangles' heights
82 // If each rect is the same "sizeNeeded," we just need to see if there's room in page.height
83
84 // not enough vertical space
85 if (shelf.y + shelf.height >= page.height)
86 continue;
87
88 // place here
89 result.success = true;
90 result.pageIndex = page.pageIndex;
91 result.x = shelf.curX;
92 result.y = shelf.y;
93 // update shelf
94 shelf.curX += sizeNeeded;
95 if (sizeNeeded > shelf.height)
96 shelf.height = sizeNeeded;
97
98 return result;
99 }
100 }
101
102 // No existing shelf had space, so start a new shelf
103 int newShelfY = 0;
104 if (!page.shelves.empty()) {
105 const Shelf &lastShelf = page.shelves.back();
106 newShelfY = lastShelf.y + lastShelf.height;
107 }
108
109 // check if there's enough space left in the page vertically
110 if (newShelfY + sizeNeeded > page.height) {
111 // no space in this page
112 return result; // success = false
113 }
114
115 // create a new shelf
116 Shelf newShelf;
117 newShelf.y = newShelfY;
118 newShelf.height = sizeNeeded;
119 newShelf.curX = 0;
120 page.shelves.push_back(newShelf);
121
122 // place rect at top-left of this shelf
123 Shelf &shelfRef = page.shelves.back();
124 result.success = true;
125 result.pageIndex = page.pageIndex;
126 result.x = shelfRef.curX;
127 result.y = shelfRef.y;
128 shelfRef.curX += sizeNeeded;
129
130 return result;
131 }
132
133 int pagesNeeded() const { return int(pages.size()); }
134};
135
136}
137
138static QRhiTexture *allocateRhiShadowTexture(QRhi *rhi, QRhiTexture::Format format, const QSize &size, quint32 numLayers, QRhiTexture::Flags flags)
139{
140 auto texture = rhi->newTexture(format, size, 1, flags);
141 if (flags & QRhiTexture::TextureArray)
142 texture->setArraySize(numLayers);
143 if (!texture->create())
144 qWarning("Failed to create shadow map texture of size %dx%d", size.width(), size.height());
145 return texture;
146}
147
148static QRhiRenderBuffer *allocateRhiShadowRenderBuffer(QRhi *rhi, QRhiRenderBuffer::Type type, const QSize &size)
149{
150 auto renderBuffer = rhi->newRenderBuffer(type, size, 1);
151 if (!renderBuffer->create())
152 qWarning("Failed to build depth-stencil buffer of size %dx%d", size.width(), size.height());
153 return renderBuffer;
154}
155
156
157static bool checkCompatibility(QSSGShadowMapEntry *entry, const QVector<QSSGShadowMapEntry::AtlasEntry> &atlasEntries) {
158 if (!entry)
159 return false;
160
161 for (int i = 0; i < atlasEntries.size(); ++i) {
162 const auto &atlasEntry = atlasEntries.at(i);
163
164 if (!(entry->m_atlasInfo[i].layerIndex == atlasEntry.layerIndex &&
165 qFuzzyCompare(entry->m_atlasInfo[i].uOffset, atlasEntry.uOffset) &&
166 qFuzzyCompare(entry->m_atlasInfo[i].vOffset, atlasEntry.vOffset) &&
167 qFuzzyCompare(entry->m_atlasInfo[i].uvScale, atlasEntry.uvScale)))
168 return false;
169 }
170
171 return true;
172}
173
174static QVector<QSSGShadowMapEntry::AtlasEntry> createAtlasEntries(const QVarLengthArray<AtlasHelpers::ShelfPacker::AtlasPlacement, 4> &atlasPlacements, int entrySize, int atlasPageSize) {
175 QVector<QSSGShadowMapEntry::AtlasEntry> atlasEntries;
176 atlasEntries.reserve(atlasPlacements.size());
177 for (int i = 0; i < atlasPlacements.size(); ++i) {
178 const auto &placement = atlasPlacements.at(i);
179 const float x = float(placement.x) / float(atlasPageSize);
180 const float y = float(placement.y) / float(atlasPageSize);
181 const float uvScale = float(entrySize) / float(atlasPageSize);
182 atlasEntries.append({placement.pageIndex, x, y, uvScale});
183 }
184 return atlasEntries;
185}
186
187
188QSSGRenderShadowMap::QSSGRenderShadowMap(const QSSGRenderContextInterface &inContext)
189 : m_context(inContext)
190{
191}
192
193QSSGRenderShadowMap::~QSSGRenderShadowMap()
194{
195 releaseCachedResources();
196}
197
198void QSSGRenderShadowMap::releaseCachedResources()
199{
200 for (QSSGShadowMapEntry &entry : m_shadowMapList)
201 entry.destroyRhiResources();
202
203 if (m_shadowMapAtlasTexture)
204 m_shadowMapAtlasTexture.reset();
205
206 if (m_shadowMapBlueNoiseTexture)
207 m_shadowMapBlueNoiseTexture.reset();
208
209 qDeleteAll(m_layerDepthStencilBuffers);
210 m_layerDepthStencilBuffers.clear();
211 qDeleteAll(m_layerRenderTargets);
212 m_layerRenderTargets.clear();
213 qDeleteAll(m_layerRenderPassDescriptors);
214 m_layerRenderPassDescriptors.clear();
215
216 m_shadowMapList.clear();
217}
218
219void QSSGRenderShadowMap::addShadowMaps(const QSSGShaderLightList &renderableLights)
220{
221 QRhi *rhi = m_context.rhiContext()->rhi();
222 // Bail out if there is no QRhi, since we can't add entries without it
223 if (!rhi)
224 return;
225
226 // Handle blue noise texture
227 if (!m_shadowMapBlueNoiseTexture) {
228 // The blue noise image is based on two random blue noise images with values v0 and v1.
229 // Then the cos and sin of these are stored to avoid trigonmoetric functions
230 // in the shadow mapping shader.
231 // R = cos(v0)
232 // G = sin(v0)
233 // B = cos(v1)
234 // A = sin(v1)
235 QImage blueNoiseImage(QStringLiteral(":/res/textures/blue_noise.png"));
236 if (blueNoiseImage.isNull()) {
237 qWarning("Failed to load blue noise texture!");
238 } else {
239 Q_ASSERT(blueNoiseImage.size() == QSize(64, 64));
240 Q_ASSERT(blueNoiseImage.format() == QImage::Format_ARGB32);
241 blueNoiseImage = blueNoiseImage.convertToFormat(QImage::Format_RGBA8888);
242 m_shadowMapBlueNoiseTexture.reset(allocateRhiShadowTexture(rhi, QRhiTexture::RGBA8, QSize(64, 64), 0, {}));
243 if (!m_shadowMapBlueNoiseTexture->create()) {
244 qWarning("Failed to create blue noise texture");
245 } else {
246 QRhiResourceUpdateBatch *rub = rhi->nextResourceUpdateBatch();
247 QRhiTextureSubresourceUploadDescription subresDesc(blueNoiseImage);
248 rub->uploadTexture(m_shadowMapBlueNoiseTexture.get(),
249 QRhiTextureUploadDescription({ QRhiTextureUploadEntry(0, 0, subresDesc) }));
250 QRhiCommandBuffer *cb = m_context.rhiContext()->commandBuffer();
251 cb->resourceUpdate(rub);
252 }
253 }
254 }
255
256 const quint32 numLights = renderableLights.size();
257 qsizetype numShadows = 0;
258 const bool supports32BitTextures = rhi->isTextureFormatSupported(QRhiTexture::R32F);
259 QRhiTexture::Format format = QRhiTexture::R16F;
260 quint32 mapSize = 0;
261 QHash<quint32, QVector<QSSGShadowMapEntry::AtlasEntry>> lightIndexToAtlasEntries;
262
263 // Get format and maximum texture size needed
264 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
265 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
266 if (!shaderLight.shadows)
267 continue;
268
269 // Force R32F format if any light requests it
270 if (shaderLight.light->m_use32BitShadowmap && supports32BitTextures)
271 format = QRhiTexture::R32F;
272
273 // Find the largest shadow map size needed
274 if (mapSize < shaderLight.light->m_shadowMapRes)
275 mapSize = shaderLight.light->m_shadowMapRes;
276
277 numShadows += 1;
278 }
279
280 auto atlasPacker = AtlasHelpers::ShelfPacker(mapSize, mapSize);
281
282
283 // Figure out the number of layers needed for the atlas
284 // including where everthing will go in the atlas
285 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
286 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
287 if (!shaderLight.shadows)
288 continue;
289
290 quint8 mapsNeeded = 0;
291
292 if (shaderLight.light->type == QSSGRenderLight::Type::DirectionalLight)
293 mapsNeeded = shaderLight.light->m_csmNumSplits + 1;
294 else if (shaderLight.light->type == QSSGRenderLight::Type::SpotLight)
295 mapsNeeded = 1;
296 else if (shaderLight.light->type == QSSGRenderLight::Type::PointLight)
297 mapsNeeded = 2;
298
299 QVarLengthArray<AtlasHelpers::ShelfPacker::AtlasPlacement, 4> atlasPlacements;
300 for (quint8 i = 0; i < mapsNeeded; ++i) {
301 const int size = shaderLight.light->m_shadowMapRes;
302 auto result = atlasPacker.addRectangle(size);
303 if (result.success)
304 atlasPlacements.push_back(result);
305 }
306 lightIndexToAtlasEntries.insert(lightIndex, createAtlasEntries(atlasPlacements, shaderLight.light->m_shadowMapRes, mapSize));
307 }
308 const QSize texSize = QSize(mapSize, mapSize);
309 const quint32 layersNeeded = atlasPacker.pagesNeeded();
310
311 // Check if we need a rebuild
312 bool needsRebuild = numShadows != shadowMapEntryCount();
313 if (!m_shadowMapAtlasTexture ||
314 m_shadowMapAtlasTexture->pixelSize() != texSize ||
315 m_shadowMapAtlasTexture->arraySize() != int(layersNeeded) ||
316 m_shadowMapAtlasTexture->format() != format) {
317
318 // If we need a new texture as well
319 if (m_shadowMapAtlasTexture) {
320 m_shadowMapAtlasTexture.reset();
321 }
322
323 m_shadowMapAtlasTexture.reset(allocateRhiShadowTexture(rhi, format, texSize, layersNeeded, QRhiTexture::RenderTarget | QRhiTexture::TextureArray));
324
325 qDeleteAll(m_layerDepthStencilBuffers);
326 m_layerDepthStencilBuffers.clear();
327 qDeleteAll(m_layerRenderTargets);
328 m_layerRenderTargets.clear();
329 qDeleteAll(m_layerRenderPassDescriptors);
330 m_layerRenderPassDescriptors.clear();
331
332 for (quint32 i = 0; i < layersNeeded; ++i) {
333 // Recreate per layer RenderBuffers, TextureRenderTarget, and RenderPassDescriptors
334 QRhiRenderBuffer *depthStencilBuffer = allocateRhiShadowRenderBuffer(rhi, QRhiRenderBuffer::DepthStencil, texSize);
335 QRhiTextureRenderTargetDescription rtDesc;
336 QRhiColorAttachment attachment(m_shadowMapAtlasTexture.get());
337 attachment.setLayer(i);
338 rtDesc.setColorAttachments({ attachment });
339 rtDesc.setDepthStencilBuffer(depthStencilBuffer);
340 QRhiTextureRenderTarget *rt = rhi->newTextureRenderTarget(rtDesc);
341 rt->setDescription(rtDesc);
342 rt->setFlags(QRhiTextureRenderTarget::PreserveColorContents); // Don't clear between passes since this is an atlas
343 QRhiRenderPassDescriptor *rpDesc = rt->newCompatibleRenderPassDescriptor();
344 rt->setRenderPassDescriptor(rpDesc);
345 if (!rt->create())
346 qWarning("Failed to build shadow map render target");
347 rt->setName(QByteArrayLiteral("shadow map atlas layer") + QByteArray::number(i));
348 m_layerDepthStencilBuffers.append(depthStencilBuffer);
349 m_layerRenderTargets.append(rt);
350 m_layerRenderPassDescriptors.append(rpDesc);
351 }
352
353 needsRebuild = true;
354 }
355
356 // If we need to allocate the RHI resources
357 if (!m_sharedFrontCubeToAtlasUniformBuffer || !m_sharedBackCubeToAtlasUniformBuffer) {
358 const quint32 uniformValueFront = 0;
359 const quint32 uniformValueBack = 1;
360
361 QRhiResourceUpdateBatch *rub = rhi->nextResourceUpdateBatch();
362 if (!m_sharedFrontCubeToAtlasUniformBuffer) {
363 m_sharedFrontCubeToAtlasUniformBuffer.reset(rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, sizeof(uint32_t)));
364 m_sharedFrontCubeToAtlasUniformBuffer->create();
365 rub->updateDynamicBuffer(m_sharedFrontCubeToAtlasUniformBuffer.get(), 0, sizeof(quint32), &uniformValueFront);
366 }
367
368 if (!m_sharedBackCubeToAtlasUniformBuffer) {
369 m_sharedBackCubeToAtlasUniformBuffer.reset(rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, sizeof(uint32_t)));
370 m_sharedBackCubeToAtlasUniformBuffer->create();
371 rub->updateDynamicBuffer(m_sharedBackCubeToAtlasUniformBuffer.get(), 0, sizeof(quint32), &uniformValueBack);
372 }
373 QRhiCommandBuffer *cb = m_context.rhiContext()->commandBuffer();
374 cb->resourceUpdate(rub);
375 }
376 if (!m_sharedCubeToAtlasSampler) {
377 m_sharedCubeToAtlasSampler.reset(rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
378 m_sharedCubeToAtlasSampler->create();
379 }
380 if (!m_shadowClearSrb) {
381 m_shadowClearSrb.reset(rhi->newShaderResourceBindings());
382 m_shadowClearSrb->create();
383 }
384
385
386 if (!needsRebuild) {
387 // Check if relevant shadow properties has changed
388 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
389 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
390 if (!shaderLight.shadows)
391 continue;
392 QSSGShadowMapEntry *pEntry = shadowMapEntry(lightIndex);
393 if (!pEntry) {
394 needsRebuild = true;
395 break;
396 }
397
398 const auto &atlasEntires = lightIndexToAtlasEntries.value(lightIndex);
399 if (!checkCompatibility(pEntry, atlasEntires)) {
400 needsRebuild = true;
401 break;
402 }
403 }
404 }
405
406 if (!needsRebuild)
407 return;
408
409 // Rebuild then
410 for (QSSGShadowMapEntry &entry : m_shadowMapList)
411 entry.destroyRhiResources();
412 m_shadowMapList.clear();
413
414 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
415 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
416 if (!shaderLight.shadows)
417 continue;
418 addShadowMap(lightIndex,
419 texSize,
420 lightIndexToAtlasEntries.value(lightIndex),
421 shaderLight.light->m_csmNumSplits,
422 format,
423 shaderLight.light->type == QSSGRenderLight::Type::PointLight,
424 shaderLight.light->debugObjectName);
425 }
426
427}
428
429QSSGShadowMapEntry *QSSGRenderShadowMap::addShadowMap(quint32 lightIdx,
430 QSize size,
431 QVector<QSSGShadowMapEntry::AtlasEntry> atlasEntries,
432 quint32 csmNumSplits,
433 QRhiTexture::Format rhiFormat,
434 bool isPointLight,
435 const QString &renderNodeObjName)
436{
437 QRhi *rhi = m_context.rhiContext()->rhi();
438 QSSGShadowMapEntry *pEntry = shadowMapEntry(lightIdx);
439
440 Q_ASSERT(rhi);
441 Q_ASSERT(!pEntry);
442 Q_ASSERT(!atlasEntries.isEmpty());
443
444 m_shadowMapList.push_back(QSSGShadowMapEntry::withAtlas(lightIdx));
445
446 pEntry = &m_shadowMapList.back();
447 pEntry->m_csmNumSplits = csmNumSplits;
448
449 if (isPointLight) {
450 const QSize localSize = size * atlasEntries.first().uvScale;
451 // First pass renders to depth cube map
452 const QByteArray rtName = renderNodeObjName.toLatin1();
453
454 pEntry->m_rhiDepthCube = allocateRhiShadowTexture(rhi, rhiFormat, localSize, 0, QRhiTexture::RenderTarget | QRhiTexture::CubeMap);
455 pEntry->m_rhiDepthStencilCube = allocateRhiShadowRenderBuffer(rhi, QRhiRenderBuffer::DepthStencil, localSize);
456
457
458 for (const auto face : QSSGRenderTextureCubeFaces) {
459 QRhiTextureRenderTarget *&rt(pEntry->m_rhiRenderTargetCube[quint8(face)]);
460 Q_ASSERT(!rt);
461 QRhiColorAttachment att(pEntry->m_rhiDepthCube);
462 att.setLayer(quint8(face)); // 6 render targets, each referencing one face of the cubemap
463 QRhiTextureRenderTargetDescription rtDesc;
464 rtDesc.setColorAttachments({ att });
465 rtDesc.setDepthStencilBuffer(pEntry->m_rhiDepthStencilCube);
466 rt = rhi->newTextureRenderTarget(rtDesc);
467 rt->setDescription(rtDesc);
468 if (!pEntry->m_rhiRenderPassDescCube)
469 pEntry->m_rhiRenderPassDescCube = rt->newCompatibleRenderPassDescriptor();
470 rt->setRenderPassDescriptor(pEntry->m_rhiRenderPassDescCube);
471 if (!rt->create())
472 qWarning("Failed to build shadow map render target");
473 rt->setName(rtName + QByteArrayLiteral(" shadow cube face: ") + QSSGBaseTypeHelpers::displayName(face));
474 }
475
476 if (!pEntry->m_cubeToAtlasFrontSrb) {
477 pEntry->m_cubeToAtlasFrontSrb = rhi->newShaderResourceBindings();
478 pEntry->m_cubeToAtlasFrontSrb->setBindings({
479 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::FragmentStage, m_sharedFrontCubeToAtlasUniformBuffer.get()),
480 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, pEntry->m_rhiDepthCube, m_sharedCubeToAtlasSampler.get())
481 });
482 pEntry->m_cubeToAtlasFrontSrb->create();
483 }
484
485 if (!pEntry->m_cubeToAtlasBackSrb) {
486 pEntry->m_cubeToAtlasBackSrb = rhi->newShaderResourceBindings();
487 pEntry->m_cubeToAtlasBackSrb->setBindings({
488 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::FragmentStage, m_sharedBackCubeToAtlasUniformBuffer.get()),
489 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, pEntry->m_rhiDepthCube, m_sharedCubeToAtlasSampler.get())
490 });
491 pEntry->m_cubeToAtlasBackSrb->create();
492 }
493 }
494
495
496 // Additional graphics resources: samplers, render targets.
497 const quint32 entriesCount = atlasEntries.size();
498 for (quint32 splitIndex = 0; splitIndex < entriesCount; splitIndex++) {
499 pEntry->m_atlasInfo[splitIndex].layerIndex = atlasEntries.at(splitIndex).layerIndex;
500 pEntry->m_atlasInfo[splitIndex].uOffset = atlasEntries.at(splitIndex).uOffset;
501 pEntry->m_atlasInfo[splitIndex].vOffset = atlasEntries.at(splitIndex).vOffset;
502 pEntry->m_atlasInfo[splitIndex].uvScale = atlasEntries.at(splitIndex).uvScale;
503
504 quint32 layerId = atlasEntries.at(splitIndex).layerIndex;
505 pEntry->m_rhiRenderTargets[splitIndex] = m_layerRenderTargets[layerId];
506 pEntry->m_rhiRenderPassDesc[splitIndex] = m_layerRenderPassDescriptors[layerId];
507 }
508 pEntry->m_lightIndex = lightIdx;
509
510 return pEntry;
511}
512
513QSSGShadowMapEntry *QSSGRenderShadowMap::shadowMapEntry(int lightIdx)
514{
515 Q_ASSERT(lightIdx >= 0);
516
517 for (int i = 0; i < m_shadowMapList.size(); i++) {
518 QSSGShadowMapEntry *pEntry = &m_shadowMapList[i];
519 if (pEntry->m_lightIndex == quint32(lightIdx))
520 return pEntry;
521 }
522
523 return nullptr;
524}
525
526QRhiTextureRenderTarget *QSSGRenderShadowMap::layerRenderTarget(int layerIndex)
527{
528 if (layerIndex < 0 || layerIndex >= m_layerRenderTargets.size())
529 return nullptr;
530 return m_layerRenderTargets.at(layerIndex);
531}
532
533QRhiRenderPassDescriptor *QSSGRenderShadowMap::layerRenderPassDescriptor(int layerIndex)
534{
535 if (layerIndex < 0 || layerIndex >= m_layerRenderPassDescriptors.size())
536 return nullptr;
537 return m_layerRenderPassDescriptors.at(layerIndex);
538}
539
540QRhiTexture *QSSGRenderShadowMap::shadowMapAtlasTexture() const
541{
542 return m_shadowMapAtlasTexture.get();
543}
544
545QRhiTexture *QSSGRenderShadowMap::shadowMapBlueNoiseTexture() const
546{
547 return m_shadowMapBlueNoiseTexture.get();
548}
549
550QSSGShadowMapEntry::QSSGShadowMapEntry()
551 : m_lightIndex(std::numeric_limits<quint32>::max())
552{
553}
554
555QSSGShadowMapEntry QSSGShadowMapEntry::withAtlas(quint32 lightIdx)
556{
557 QSSGShadowMapEntry e;
558 e.m_lightIndex = lightIdx;
559 return e;
560}
561
562bool QSSGShadowMapEntry::hasActiveCascades() const
563{
564 const auto isActive = [](float cascade) { return cascade != 0.f; };
565 return std::any_of(std::begin(m_csmActive), std::end(m_csmActive), isActive);
566}
567
568void QSSGShadowMapEntry::destroyRhiResources()
569{
570 delete m_rhiDepthCube;
571 m_rhiDepthCube = nullptr;
572 delete m_rhiDepthStencilCube;
573 m_rhiDepthStencilCube = nullptr;
574 qDeleteAll(m_rhiRenderTargetCube);
575 m_rhiRenderTargetCube.fill(nullptr);
576 delete m_rhiRenderPassDescCube;
577 m_rhiRenderPassDescCube = nullptr;
578 delete m_cubeToAtlasFrontSrb;
579 m_cubeToAtlasFrontSrb = nullptr;
580 delete m_cubeToAtlasBackSrb;
581 m_cubeToAtlasBackSrb = nullptr;
582
583 // un-owned references
584 m_rhiRenderTargets.fill(nullptr);
585 m_rhiRenderPassDesc.fill(nullptr);
586}
587
588QT_END_NAMESPACE
static QVector< QSSGShadowMapEntry::AtlasEntry > createAtlasEntries(const QVarLengthArray< AtlasHelpers::ShelfPacker::AtlasPlacement, 4 > &atlasPlacements, int entrySize, int atlasPageSize)
static bool checkCompatibility(QSSGShadowMapEntry *entry, const QVector< QSSGShadowMapEntry::AtlasEntry > &atlasEntries)
static QRhiTexture * allocateRhiShadowTexture(QRhi *rhi, QRhiTexture::Format format, const QSize &size, quint32 numLayers, QRhiTexture::Flags flags)
static QRhiRenderBuffer * allocateRhiShadowRenderBuffer(QRhi *rhi, QRhiRenderBuffer::Type type, const QSize &size)
AtlasPlacement addRectangle(int sizeNeeded)
AtlasPlacement placeOnPage(ShelfPage &page, int sizeNeeded)
std::vector< ShelfPage > pages
ShelfPacker(int pageWidth, int pageHeight)