219void QSSGRenderShadowMap::addShadowMaps(
const QSSGShaderLightList &renderableLights)
221 QRhi *rhi = m_context.rhiContext()->rhi();
227 if (!m_shadowMapBlueNoiseTexture) {
235 QImage blueNoiseImage(QStringLiteral(
":/res/textures/blue_noise.png"));
236 if (blueNoiseImage.isNull()) {
237 qWarning(
"Failed to load blue noise texture!");
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");
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);
256 const quint32 numLights = renderableLights.size();
257 qsizetype numShadows = 0;
258 const bool supports32BitTextures = rhi->isTextureFormatSupported(QRhiTexture::R32F);
259 QRhiTexture::Format format = QRhiTexture::R16F;
261 QHash<quint32, QVector<QSSGShadowMapEntry::AtlasEntry>> lightIndexToAtlasEntries;
264 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
265 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
266 if (!shaderLight.shadows)
270 if (shaderLight.light->m_use32BitShadowmap && supports32BitTextures)
271 format = QRhiTexture::R32F;
274 if (mapSize < shaderLight.light->m_shadowMapRes)
275 mapSize = shaderLight.light->m_shadowMapRes;
280 auto atlasPacker = AtlasHelpers::ShelfPacker(mapSize, mapSize);
285 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
286 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
287 if (!shaderLight.shadows)
290 quint8 mapsNeeded = 0;
292 if (shaderLight.light->type == QSSGRenderLight::Type::DirectionalLight)
293 mapsNeeded = shaderLight.light->m_csmNumSplits + 1;
294 else if (shaderLight.light->type == QSSGRenderLight::Type::SpotLight)
296 else if (shaderLight.light->type == QSSGRenderLight::Type::PointLight)
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);
304 atlasPlacements.push_back(result);
306 lightIndexToAtlasEntries.insert(lightIndex, createAtlasEntries(atlasPlacements, shaderLight.light->m_shadowMapRes, mapSize));
308 const QSize texSize = QSize(mapSize, mapSize);
309 const quint32 layersNeeded = atlasPacker.pagesNeeded();
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) {
319 if (m_shadowMapAtlasTexture) {
320 m_shadowMapAtlasTexture.reset();
323 m_shadowMapAtlasTexture.reset(allocateRhiShadowTexture(rhi, format, texSize, layersNeeded, QRhiTexture::RenderTarget | QRhiTexture::TextureArray));
325 qDeleteAll(m_layerDepthStencilBuffers);
326 m_layerDepthStencilBuffers.clear();
327 qDeleteAll(m_layerRenderTargets);
328 m_layerRenderTargets.clear();
329 qDeleteAll(m_layerRenderPassDescriptors);
330 m_layerRenderPassDescriptors.clear();
332 for (quint32 i = 0; i < layersNeeded; ++i) {
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);
343 QRhiRenderPassDescriptor *rpDesc = rt->newCompatibleRenderPassDescriptor();
344 rt->setRenderPassDescriptor(rpDesc);
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);
357 if (!m_sharedFrontCubeToAtlasUniformBuffer || !m_sharedBackCubeToAtlasUniformBuffer) {
358 const quint32 uniformValueFront = 0;
359 const quint32 uniformValueBack = 1;
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);
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);
373 QRhiCommandBuffer *cb = m_context.rhiContext()->commandBuffer();
374 cb->resourceUpdate(rub);
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();
380 if (!m_shadowClearSrb) {
381 m_shadowClearSrb.reset(rhi->newShaderResourceBindings());
382 m_shadowClearSrb->create();
388 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
389 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
390 if (!shaderLight.shadows)
392 QSSGShadowMapEntry *pEntry = shadowMapEntry(lightIndex);
398 const auto &atlasEntires = lightIndexToAtlasEntries.value(lightIndex);
399 if (!checkCompatibility(pEntry, atlasEntires)) {
410 for (QSSGShadowMapEntry &entry : m_shadowMapList)
411 entry.destroyRhiResources();
412 m_shadowMapList.clear();
414 for (quint32 lightIndex = 0; lightIndex < numLights; ++lightIndex) {
415 const QSSGShaderLight &shaderLight = renderableLights.at(lightIndex);
416 if (!shaderLight.shadows)
418 addShadowMap(lightIndex,
420 lightIndexToAtlasEntries.value(lightIndex),
421 shaderLight.light->m_csmNumSplits,
423 shaderLight.light->type == QSSGRenderLight::Type::PointLight,
424 shaderLight.light->debugObjectName);