|
@@ -12,325 +12,126 @@
|
|
|
|
|
|
|
|
namespace anki {
|
|
namespace anki {
|
|
|
|
|
|
|
|
-class ShadowMapping::Scratch::WorkItem
|
|
|
|
|
|
|
+class ShadowMapping::LightToRenderTempInfo
|
|
|
{
|
|
{
|
|
|
public:
|
|
public:
|
|
|
UVec4 m_viewport;
|
|
UVec4 m_viewport;
|
|
|
RenderQueue* m_renderQueue;
|
|
RenderQueue* m_renderQueue;
|
|
|
- U32 m_firstRenderableElement;
|
|
|
|
|
- U32 m_renderableElementCount;
|
|
|
|
|
- U32 m_threadPoolTaskIdx;
|
|
|
|
|
|
|
+ U32 m_drawcallCount;
|
|
|
U32 m_renderQueueElementsLod;
|
|
U32 m_renderQueueElementsLod;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-class ShadowMapping::Scratch::LightToRenderToScratchInfo
|
|
|
|
|
|
|
+class ShadowMapping::ThreadWorkItem
|
|
|
{
|
|
{
|
|
|
public:
|
|
public:
|
|
|
UVec4 m_viewport;
|
|
UVec4 m_viewport;
|
|
|
RenderQueue* m_renderQueue;
|
|
RenderQueue* m_renderQueue;
|
|
|
- U32 m_drawcallCount;
|
|
|
|
|
|
|
+ U32 m_firstRenderableElement;
|
|
|
|
|
+ U32 m_renderableElementCount;
|
|
|
|
|
+ U32 m_threadPoolTaskIdx;
|
|
|
U32 m_renderQueueElementsLod;
|
|
U32 m_renderQueueElementsLod;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-class ShadowMapping::Atlas::ResolveWorkItem
|
|
|
|
|
-{
|
|
|
|
|
-public:
|
|
|
|
|
- Vec4 m_uvInBounds; ///< Bounds used to avoid blurring neighbour tiles.
|
|
|
|
|
- Vec4 m_uvIn; ///< UV + size that point to the scratch buffer.
|
|
|
|
|
- UVec4 m_viewportOut; ///< Viewport in the atlas RT.
|
|
|
|
|
- Bool m_blur;
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
ShadowMapping::~ShadowMapping()
|
|
ShadowMapping::~ShadowMapping()
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Error ShadowMapping::init()
|
|
Error ShadowMapping::init()
|
|
|
{
|
|
{
|
|
|
- ANKI_R_LOGV("Initializing shadowmapping")
|
|
|
|
|
-
|
|
|
|
|
const Error err = initInternal();
|
|
const Error err = initInternal();
|
|
|
if(err)
|
|
if(err)
|
|
|
{
|
|
{
|
|
|
ANKI_R_LOGE("Failed to initialize shadowmapping");
|
|
ANKI_R_LOGE("Failed to initialize shadowmapping");
|
|
|
}
|
|
}
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- ANKI_R_LOGV("Shadowmapping initialized. Scratch size %ux%u, atlas size %ux%u",
|
|
|
|
|
- m_scratch.m_tileCountX * m_scratch.m_tileResolution,
|
|
|
|
|
- m_scratch.m_tileCountY * m_scratch.m_tileResolution,
|
|
|
|
|
- m_atlas.m_tileCountBothAxis * m_atlas.m_tileResolution,
|
|
|
|
|
- m_atlas.m_tileCountBothAxis * m_atlas.m_tileResolution);
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
return err;
|
|
return err;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-Error ShadowMapping::initScratch()
|
|
|
|
|
-{
|
|
|
|
|
- // Init the shadowmaps and FBs
|
|
|
|
|
- {
|
|
|
|
|
- m_scratch.m_tileCountX = getConfig().getRShadowMappingScratchTileCountX();
|
|
|
|
|
- m_scratch.m_tileCountY = getConfig().getRShadowMappingScratchTileCountY();
|
|
|
|
|
- m_scratch.m_tileResolution = getConfig().getRShadowMappingTileResolution();
|
|
|
|
|
-
|
|
|
|
|
- // RT
|
|
|
|
|
- m_scratch.m_rtDescr = m_r->create2DRenderTargetDescription(m_scratch.m_tileResolution * m_scratch.m_tileCountX,
|
|
|
|
|
- m_scratch.m_tileResolution * m_scratch.m_tileCountY,
|
|
|
|
|
- m_r->getDepthNoStencilFormat(), "SM scratch");
|
|
|
|
|
- m_scratch.m_rtDescr.bake();
|
|
|
|
|
-
|
|
|
|
|
- // FB
|
|
|
|
|
- m_scratch.m_fbDescr.m_depthStencilAttachment.m_loadOperation = AttachmentLoadOperation::kClear;
|
|
|
|
|
- m_scratch.m_fbDescr.m_depthStencilAttachment.m_clearValue.m_depthStencil.m_depth = 1.0f;
|
|
|
|
|
- m_scratch.m_fbDescr.m_depthStencilAttachment.m_aspect = DepthStencilAspectBit::kDepth;
|
|
|
|
|
- m_scratch.m_fbDescr.bake();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- m_scratch.m_tileAlloc.init(&getMemoryPool(), m_scratch.m_tileCountX, m_scratch.m_tileCountY, kMaxLodCount, false);
|
|
|
|
|
-
|
|
|
|
|
- return Error::kNone;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-Error ShadowMapping::initAtlas()
|
|
|
|
|
|
|
+Error ShadowMapping::initInternal()
|
|
|
{
|
|
{
|
|
|
- const Bool preferCompute = getConfig().getRPreferCompute();
|
|
|
|
|
-
|
|
|
|
|
// Init RT
|
|
// Init RT
|
|
|
{
|
|
{
|
|
|
- m_atlas.m_tileResolution = getConfig().getRShadowMappingTileResolution();
|
|
|
|
|
- m_atlas.m_tileCountBothAxis = getConfig().getRShadowMappingTileCountPerRowOrColumn();
|
|
|
|
|
|
|
+ m_tileResolution = getConfig().getRShadowMappingTileResolution();
|
|
|
|
|
+ m_tileCountBothAxis = getConfig().getRShadowMappingTileCountPerRowOrColumn();
|
|
|
|
|
+
|
|
|
|
|
+ ANKI_R_LOGV("Initializing shadowmapping. Atlas resolution %ux%u", m_tileResolution * m_tileCountBothAxis,
|
|
|
|
|
+ m_tileResolution * m_tileCountBothAxis);
|
|
|
|
|
|
|
|
// RT
|
|
// RT
|
|
|
- const Format texFormat = (ANKI_EVSM4) ? Format::kR32G32B32A32_Sfloat : Format::kR32G32_Sfloat;
|
|
|
|
|
- TextureUsageBit usage = TextureUsageBit::kSampledFragment | TextureUsageBit::kSampledCompute;
|
|
|
|
|
- usage |= (preferCompute) ? TextureUsageBit::kImageComputeWrite : TextureUsageBit::kAllFramebuffer;
|
|
|
|
|
- TextureInitInfo texinit = m_r->create2DRenderTargetInitInfo(
|
|
|
|
|
- m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis,
|
|
|
|
|
- m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis, texFormat, usage, "SM atlas");
|
|
|
|
|
|
|
+ const TextureUsageBit usage =
|
|
|
|
|
+ TextureUsageBit::kSampledFragment | TextureUsageBit::kSampledCompute | TextureUsageBit::kAllFramebuffer;
|
|
|
|
|
+ TextureInitInfo texinit = m_r->create2DRenderTargetInitInfo(m_tileResolution * m_tileCountBothAxis,
|
|
|
|
|
+ m_tileResolution * m_tileCountBothAxis,
|
|
|
|
|
+ Format::kD16_Unorm, usage, "ShadowAtlas");
|
|
|
ClearValue clearVal;
|
|
ClearValue clearVal;
|
|
|
clearVal.m_colorf[0] = 1.0f;
|
|
clearVal.m_colorf[0] = 1.0f;
|
|
|
- m_atlas.m_tex = m_r->createAndClearRenderTarget(texinit, TextureUsageBit::kSampledFragment, clearVal);
|
|
|
|
|
|
|
+ m_atlasTex = m_r->createAndClearRenderTarget(texinit, TextureUsageBit::kSampledFragment, clearVal);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Tiles
|
|
// Tiles
|
|
|
- m_atlas.m_tileAlloc.init(&getMemoryPool(), m_atlas.m_tileCountBothAxis, m_atlas.m_tileCountBothAxis, kMaxLodCount,
|
|
|
|
|
- true);
|
|
|
|
|
|
|
+ m_tileAlloc.init(&getMemoryPool(), m_tileCountBothAxis, m_tileCountBothAxis, kTileAllocHierarchyCount, true);
|
|
|
|
|
|
|
|
- // Programs and shaders
|
|
|
|
|
- {
|
|
|
|
|
- ANKI_CHECK(getResourceManager().loadResource((preferCompute) ? "ShaderBinaries/EvsmCompute.ankiprogbin"
|
|
|
|
|
- : "ShaderBinaries/EvsmRaster.ankiprogbin",
|
|
|
|
|
- m_atlas.m_resolveProg));
|
|
|
|
|
-
|
|
|
|
|
- ShaderProgramResourceVariantInitInfo variantInitInfo(m_atlas.m_resolveProg);
|
|
|
|
|
- variantInitInfo.addConstant("kInputTextureSize", UVec2(m_scratch.m_tileCountX * m_scratch.m_tileResolution,
|
|
|
|
|
- m_scratch.m_tileCountY * m_scratch.m_tileResolution));
|
|
|
|
|
-
|
|
|
|
|
- if(!preferCompute)
|
|
|
|
|
- {
|
|
|
|
|
- variantInitInfo.addConstant("kFramebufferSize",
|
|
|
|
|
- UVec2(m_atlas.m_tileCountBothAxis * m_atlas.m_tileResolution));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ m_fbDescr.m_depthStencilAttachment.m_aspect = DepthStencilAspectBit::kDepth;
|
|
|
|
|
+ m_fbDescr.m_depthStencilAttachment.m_loadOperation = AttachmentLoadOperation::kLoad;
|
|
|
|
|
+ m_fbDescr.bake();
|
|
|
|
|
|
|
|
- const ShaderProgramResourceVariant* variant;
|
|
|
|
|
- m_atlas.m_resolveProg->getOrCreateVariant(variantInitInfo, variant);
|
|
|
|
|
- m_atlas.m_resolveGrProg = variant->getProgram();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ ANKI_CHECK(
|
|
|
|
|
+ getResourceManager().loadResource("ShaderBinaries/ShadowmappingClearDepth.ankiprogbin", m_clearDepthProg));
|
|
|
|
|
+ const ShaderProgramResourceVariant* variant;
|
|
|
|
|
+ m_clearDepthProg->getOrCreateVariant(variant);
|
|
|
|
|
+ m_clearDepthGrProg = variant->getProgram();
|
|
|
|
|
|
|
|
- m_atlas.m_fbDescr.m_colorAttachmentCount = 1;
|
|
|
|
|
- m_atlas.m_fbDescr.m_colorAttachments[0].m_loadOperation = AttachmentLoadOperation::kLoad;
|
|
|
|
|
- m_atlas.m_fbDescr.bake();
|
|
|
|
|
-
|
|
|
|
|
- return Error::kNone;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-Error ShadowMapping::initInternal()
|
|
|
|
|
-{
|
|
|
|
|
- ANKI_CHECK(initScratch());
|
|
|
|
|
- ANKI_CHECK(initAtlas());
|
|
|
|
|
return Error::kNone;
|
|
return Error::kNone;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void ShadowMapping::runAtlas(RenderPassWorkContext& rgraphCtx)
|
|
|
|
|
|
|
+void ShadowMapping::populateRenderGraph(RenderingContext& ctx)
|
|
|
{
|
|
{
|
|
|
- ANKI_ASSERT(m_atlas.m_resolveWorkItems.getSize());
|
|
|
|
|
ANKI_TRACE_SCOPED_EVENT(R_SM);
|
|
ANKI_TRACE_SCOPED_EVENT(R_SM);
|
|
|
|
|
|
|
|
- CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
|
|
|
|
|
-
|
|
|
|
|
- // Allocate and populate uniforms
|
|
|
|
|
- EvsmResolveUniforms* uniforms = allocateAndBindStorage<EvsmResolveUniforms*>(
|
|
|
|
|
- m_atlas.m_resolveWorkItems.getSize() * sizeof(EvsmResolveUniforms), cmdb, 0, 0);
|
|
|
|
|
- for(U32 i = 0; i < m_atlas.m_resolveWorkItems.getSize(); ++i)
|
|
|
|
|
- {
|
|
|
|
|
- EvsmResolveUniforms& uni = uniforms[i];
|
|
|
|
|
- const Atlas::ResolveWorkItem& workItem = m_atlas.m_resolveWorkItems[i];
|
|
|
|
|
-
|
|
|
|
|
- uni.m_viewportXY = IVec2(workItem.m_viewportOut.xy());
|
|
|
|
|
- uni.m_viewportZW = Vec2(workItem.m_viewportOut.zw());
|
|
|
|
|
-
|
|
|
|
|
- uni.m_uvScale = workItem.m_uvIn.zw();
|
|
|
|
|
- uni.m_uvTranslation = workItem.m_uvIn.xy();
|
|
|
|
|
-
|
|
|
|
|
- uni.m_uvMin = workItem.m_uvInBounds.xy();
|
|
|
|
|
- uni.m_uvMax = workItem.m_uvInBounds.xy() + workItem.m_uvInBounds.zw();
|
|
|
|
|
-
|
|
|
|
|
- uni.m_blur = workItem.m_blur;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- cmdb->bindShaderProgram(m_atlas.m_resolveGrProg);
|
|
|
|
|
-
|
|
|
|
|
- // Continue
|
|
|
|
|
- cmdb->bindSampler(0, 1, m_r->getSamplers().m_trilinearClamp);
|
|
|
|
|
- rgraphCtx.bindTexture(0, 2, m_scratch.m_rt, TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
|
|
|
|
|
|
|
+ RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
|
|
|
|
|
|
|
|
- if(getConfig().getRPreferCompute())
|
|
|
|
|
|
|
+ // Import
|
|
|
|
|
+ if(ANKI_LIKELY(m_rtImportedOnce))
|
|
|
{
|
|
{
|
|
|
- rgraphCtx.bindImage(0, 3, m_atlas.m_rt);
|
|
|
|
|
-
|
|
|
|
|
- constexpr U32 workgroupSize = 8;
|
|
|
|
|
- ANKI_ASSERT(m_atlas.m_tileResolution >= workgroupSize && (m_atlas.m_tileResolution % workgroupSize) == 0);
|
|
|
|
|
-
|
|
|
|
|
- cmdb->dispatchCompute(m_atlas.m_tileResolution / workgroupSize, m_atlas.m_tileResolution / workgroupSize,
|
|
|
|
|
- m_atlas.m_resolveWorkItems.getSize());
|
|
|
|
|
|
|
+ m_runCtx.m_rt = rgraph.importRenderTarget(m_atlasTex);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- cmdb->setViewport(0, 0, m_atlas.m_tex->getWidth(), m_atlas.m_tex->getHeight());
|
|
|
|
|
-
|
|
|
|
|
- cmdb->drawArrays(PrimitiveTopology::kTriangles, 6, m_atlas.m_resolveWorkItems.getSize());
|
|
|
|
|
|
|
+ m_runCtx.m_rt = rgraph.importRenderTarget(m_atlasTex, TextureUsageBit::kSampledFragment);
|
|
|
|
|
+ m_rtImportedOnce = true;
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void ShadowMapping::runShadowMapping(RenderPassWorkContext& rgraphCtx)
|
|
|
|
|
-{
|
|
|
|
|
- ANKI_ASSERT(m_scratch.m_workItems.getSize());
|
|
|
|
|
- ANKI_TRACE_SCOPED_EVENT(R_SM);
|
|
|
|
|
-
|
|
|
|
|
- CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
|
|
|
|
|
- const U threadIdx = rgraphCtx.m_currentSecondLevelCommandBufferIndex;
|
|
|
|
|
-
|
|
|
|
|
- for(Scratch::WorkItem& work : m_scratch.m_workItems)
|
|
|
|
|
- {
|
|
|
|
|
- if(work.m_threadPoolTaskIdx != threadIdx)
|
|
|
|
|
- {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Set state
|
|
|
|
|
- cmdb->setViewport(work.m_viewport[0], work.m_viewport[1], work.m_viewport[2], work.m_viewport[3]);
|
|
|
|
|
- cmdb->setScissor(work.m_viewport[0], work.m_viewport[1], work.m_viewport[2], work.m_viewport[3]);
|
|
|
|
|
-
|
|
|
|
|
- RenderableDrawerArguments args;
|
|
|
|
|
- args.m_viewMatrix = work.m_renderQueue->m_viewMatrix;
|
|
|
|
|
- args.m_cameraTransform = Mat3x4::getIdentity(); // Don't care
|
|
|
|
|
- args.m_viewProjectionMatrix = work.m_renderQueue->m_viewProjectionMatrix;
|
|
|
|
|
- args.m_previousViewProjectionMatrix = Mat4::getIdentity(); // Don't care
|
|
|
|
|
- args.m_sampler = m_r->getSamplers().m_trilinearRepeatAniso;
|
|
|
|
|
- args.m_minLod = args.m_maxLod = work.m_renderQueueElementsLod;
|
|
|
|
|
-
|
|
|
|
|
- m_r->getSceneDrawer().drawRange(RenderingTechnique::kShadow, args,
|
|
|
|
|
- work.m_renderQueue->m_renderables.getBegin() + work.m_firstRenderableElement,
|
|
|
|
|
- work.m_renderQueue->m_renderables.getBegin() + work.m_firstRenderableElement
|
|
|
|
|
- + work.m_renderableElementCount,
|
|
|
|
|
- cmdb);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void ShadowMapping::populateRenderGraph(RenderingContext& ctx)
|
|
|
|
|
-{
|
|
|
|
|
- ANKI_TRACE_SCOPED_EVENT(R_SM);
|
|
|
|
|
|
|
|
|
|
// First process the lights
|
|
// First process the lights
|
|
|
- U32 threadCountForScratchPass = 0;
|
|
|
|
|
- processLights(ctx, threadCountForScratchPass);
|
|
|
|
|
|
|
+ U32 threadCountForPass = 0;
|
|
|
|
|
+ processLights(ctx, threadCountForPass);
|
|
|
|
|
|
|
|
// Build the render graph
|
|
// Build the render graph
|
|
|
- RenderGraphDescription& rgraph = ctx.m_renderGraphDescr;
|
|
|
|
|
- if(m_scratch.m_workItems.getSize())
|
|
|
|
|
|
|
+ if(m_runCtx.m_workItems.getSize())
|
|
|
{
|
|
{
|
|
|
// Will have to create render passes
|
|
// Will have to create render passes
|
|
|
|
|
|
|
|
- // Scratch pass
|
|
|
|
|
- {
|
|
|
|
|
- // Compute render area
|
|
|
|
|
- const U32 minx = 0, miny = 0;
|
|
|
|
|
- const U32 height = m_scratch.m_maxViewportHeight;
|
|
|
|
|
- const U32 width = m_scratch.m_maxViewportWidth;
|
|
|
|
|
-
|
|
|
|
|
- GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass("SM scratch");
|
|
|
|
|
-
|
|
|
|
|
- m_scratch.m_rt = rgraph.newRenderTarget(m_scratch.m_rtDescr);
|
|
|
|
|
- pass.setFramebufferInfo(m_scratch.m_fbDescr, {}, m_scratch.m_rt, {}, minx, miny, width, height);
|
|
|
|
|
- ANKI_ASSERT(threadCountForScratchPass
|
|
|
|
|
- && threadCountForScratchPass <= m_r->getThreadHive().getThreadCount());
|
|
|
|
|
- pass.setWork(threadCountForScratchPass, [this](RenderPassWorkContext& rgraphCtx) {
|
|
|
|
|
- runShadowMapping(rgraphCtx);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- TextureSubresourceInfo subresource = TextureSubresourceInfo(DepthStencilAspectBit::kDepth);
|
|
|
|
|
- pass.newTextureDependency(m_scratch.m_rt, TextureUsageBit::kAllFramebuffer, subresource);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Compute render area
|
|
|
|
|
+ const U32 minx = m_runCtx.m_fullViewport[0];
|
|
|
|
|
+ const U32 miny = m_runCtx.m_fullViewport[1];
|
|
|
|
|
+ const U32 width = m_runCtx.m_fullViewport[2] - m_runCtx.m_fullViewport[0];
|
|
|
|
|
+ const U32 height = m_runCtx.m_fullViewport[3] - m_runCtx.m_fullViewport[1];
|
|
|
|
|
|
|
|
- // Atlas pass
|
|
|
|
|
- {
|
|
|
|
|
- if(ANKI_LIKELY(m_atlas.m_rtImportedOnce))
|
|
|
|
|
- {
|
|
|
|
|
- m_atlas.m_rt = rgraph.importRenderTarget(m_atlas.m_tex);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- m_atlas.m_rt = rgraph.importRenderTarget(m_atlas.m_tex, TextureUsageBit::kSampledFragment);
|
|
|
|
|
- m_atlas.m_rtImportedOnce = true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass("ShadowMapping");
|
|
|
|
|
|
|
|
- if(getConfig().getRPreferCompute())
|
|
|
|
|
- {
|
|
|
|
|
- ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("EVSM resolve");
|
|
|
|
|
-
|
|
|
|
|
- pass.setWork([this](RenderPassWorkContext& rgraphCtx) {
|
|
|
|
|
- runAtlas(rgraphCtx);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- pass.newTextureDependency(m_scratch.m_rt, TextureUsageBit::kSampledCompute,
|
|
|
|
|
- TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
|
|
|
|
|
- pass.newTextureDependency(m_atlas.m_rt, TextureUsageBit::kImageComputeWrite);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- GraphicsRenderPassDescription& pass = rgraph.newGraphicsRenderPass("EVSM resolve");
|
|
|
|
|
- pass.setFramebufferInfo(m_atlas.m_fbDescr, {m_atlas.m_rt});
|
|
|
|
|
|
|
+ pass.setFramebufferInfo(m_fbDescr, {}, m_runCtx.m_rt, {}, minx, miny, width, height);
|
|
|
|
|
+ ANKI_ASSERT(threadCountForPass && threadCountForPass <= m_r->getThreadHive().getThreadCount());
|
|
|
|
|
+ pass.setWork(threadCountForPass, [this](RenderPassWorkContext& rgraphCtx) {
|
|
|
|
|
+ runShadowMapping(rgraphCtx);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- pass.setWork([this](RenderPassWorkContext& rgraphCtx) {
|
|
|
|
|
- runAtlas(rgraphCtx);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- pass.newTextureDependency(m_scratch.m_rt, TextureUsageBit::kSampledFragment,
|
|
|
|
|
- TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
|
|
|
|
|
- pass.newTextureDependency(m_atlas.m_rt,
|
|
|
|
|
- TextureUsageBit::kFramebufferRead | TextureUsageBit::kFramebufferWrite);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- // No need for shadowmapping passes, just import the atlas
|
|
|
|
|
- if(ANKI_LIKELY(m_atlas.m_rtImportedOnce))
|
|
|
|
|
- {
|
|
|
|
|
- m_atlas.m_rt = rgraph.importRenderTarget(m_atlas.m_tex);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- m_atlas.m_rt = rgraph.importRenderTarget(m_atlas.m_tex, TextureUsageBit::kSampledFragment);
|
|
|
|
|
- m_atlas.m_rtImportedOnce = true;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ TextureSubresourceInfo subresource = TextureSubresourceInfo(DepthStencilAspectBit::kDepth);
|
|
|
|
|
+ pass.newTextureDependency(m_runCtx.m_rt, TextureUsageBit::kAllFramebuffer, subresource);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Mat4 ShadowMapping::createSpotLightTextureMatrix(const UVec4& viewport) const
|
|
Mat4 ShadowMapping::createSpotLightTextureMatrix(const UVec4& viewport) const
|
|
|
{
|
|
{
|
|
|
- const F32 atlasSize = F32(m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis);
|
|
|
|
|
|
|
+ const F32 atlasSize = F32(m_tileResolution * m_tileCountBothAxis);
|
|
|
#if ANKI_COMPILER_GCC_COMPATIBLE
|
|
#if ANKI_COMPILER_GCC_COMPATIBLE
|
|
|
# pragma GCC diagnostic push
|
|
# pragma GCC diagnostic push
|
|
|
# pragma GCC diagnostic ignored "-Wpedantic" // Because GCC and clang throw an incorrect warning
|
|
# pragma GCC diagnostic ignored "-Wpedantic" // Because GCC and clang throw an incorrect warning
|
|
@@ -348,27 +149,24 @@ Mat4 ShadowMapping::createSpotLightTextureMatrix(const UVec4& viewport) const
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void ShadowMapping::chooseLod(const Vec4& cameraOrigin, const PointLightQueueElement& light, Bool& blurAtlas,
|
|
|
|
|
- U32& tileBufferLod, U32& renderQueueElementsLod) const
|
|
|
|
|
|
|
+void ShadowMapping::chooseDetail(const Vec4& cameraOrigin, const PointLightQueueElement& light,
|
|
|
|
|
+ U32& tileAllocatorHierarchy, U32& renderQueueElementsLod) const
|
|
|
{
|
|
{
|
|
|
const F32 distFromTheCamera = (cameraOrigin - light.m_worldPosition.xyz0()).getLength() - light.m_radius;
|
|
const F32 distFromTheCamera = (cameraOrigin - light.m_worldPosition.xyz0()).getLength() - light.m_radius;
|
|
|
if(distFromTheCamera < getConfig().getLod0MaxDistance())
|
|
if(distFromTheCamera < getConfig().getLod0MaxDistance())
|
|
|
{
|
|
{
|
|
|
- ANKI_ASSERT(m_pointLightsMaxLod == 1);
|
|
|
|
|
- blurAtlas = true;
|
|
|
|
|
- tileBufferLod = 1;
|
|
|
|
|
|
|
+ tileAllocatorHierarchy = kPointLightMaxTileAllocHierarchy;
|
|
|
renderQueueElementsLod = 0;
|
|
renderQueueElementsLod = 0;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- blurAtlas = false;
|
|
|
|
|
- tileBufferLod = 0;
|
|
|
|
|
|
|
+ tileAllocatorHierarchy = max(kPointLightMaxTileAllocHierarchy, 1u) - 1;
|
|
|
renderQueueElementsLod = kMaxLodCount - 1;
|
|
renderQueueElementsLod = kMaxLodCount - 1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void ShadowMapping::chooseLod(const Vec4& cameraOrigin, const SpotLightQueueElement& light, Bool& blurAtlas,
|
|
|
|
|
- U32& tileBufferLod, U32& renderQueueElementsLod) const
|
|
|
|
|
|
|
+void ShadowMapping::chooseDetail(const Vec4& cameraOrigin, const SpotLightQueueElement& light,
|
|
|
|
|
+ U32& tileAllocatorHierarchy, U32& renderQueueElementsLod) const
|
|
|
{
|
|
{
|
|
|
// Get some data
|
|
// Get some data
|
|
|
const Vec4 coneOrigin = light.m_worldTransform.getTranslationPart().xyz0();
|
|
const Vec4 coneOrigin = light.m_worldTransform.getTranslationPart().xyz0();
|
|
@@ -383,47 +181,39 @@ void ShadowMapping::chooseLod(const Vec4& cameraOrigin, const SpotLightQueueElem
|
|
|
|
|
|
|
|
if(distFromTheCamera < getConfig().getLod0MaxDistance())
|
|
if(distFromTheCamera < getConfig().getLod0MaxDistance())
|
|
|
{
|
|
{
|
|
|
- blurAtlas = true;
|
|
|
|
|
- tileBufferLod = 2;
|
|
|
|
|
|
|
+ tileAllocatorHierarchy = kSpotLightMaxTileAllocHierarchy;
|
|
|
renderQueueElementsLod = 0;
|
|
renderQueueElementsLod = 0;
|
|
|
}
|
|
}
|
|
|
else if(distFromTheCamera < getConfig().getLod1MaxDistance())
|
|
else if(distFromTheCamera < getConfig().getLod1MaxDistance())
|
|
|
{
|
|
{
|
|
|
- blurAtlas = false;
|
|
|
|
|
- tileBufferLod = 1;
|
|
|
|
|
|
|
+ tileAllocatorHierarchy = max(kSpotLightMaxTileAllocHierarchy, 1u) - 1;
|
|
|
renderQueueElementsLod = kMaxLodCount - 1;
|
|
renderQueueElementsLod = kMaxLodCount - 1;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- blurAtlas = false;
|
|
|
|
|
- tileBufferLod = 0;
|
|
|
|
|
|
|
+ tileAllocatorHierarchy = max(kSpotLightMaxTileAllocHierarchy, 2u) - 2;
|
|
|
renderQueueElementsLod = kMaxLodCount - 1;
|
|
renderQueueElementsLod = kMaxLodCount - 1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-TileAllocatorResult ShadowMapping::allocateTilesAndScratchTiles(U64 lightUuid, U32 faceCount, const U64* faceTimestamps,
|
|
|
|
|
- const U32* faceIndices, const U32* drawcallsCount,
|
|
|
|
|
- const U32* lods, UVec4* atlasTileViewports,
|
|
|
|
|
- UVec4* scratchTileViewports,
|
|
|
|
|
- TileAllocatorResult* subResults)
|
|
|
|
|
|
|
+Bool ShadowMapping::allocateAtlasTiles(U64 lightUuid, U32 faceCount, const U64* faceTimestamps, const U32* faceIndices,
|
|
|
|
|
+ const U32* drawcallsCount, const U32* hierarchies, UVec4* atlasTileViewports,
|
|
|
|
|
+ TileAllocatorResult* subResults)
|
|
|
{
|
|
{
|
|
|
ANKI_ASSERT(lightUuid > 0);
|
|
ANKI_ASSERT(lightUuid > 0);
|
|
|
ANKI_ASSERT(faceCount > 0);
|
|
ANKI_ASSERT(faceCount > 0);
|
|
|
ANKI_ASSERT(faceTimestamps);
|
|
ANKI_ASSERT(faceTimestamps);
|
|
|
ANKI_ASSERT(faceIndices);
|
|
ANKI_ASSERT(faceIndices);
|
|
|
ANKI_ASSERT(drawcallsCount);
|
|
ANKI_ASSERT(drawcallsCount);
|
|
|
- ANKI_ASSERT(lods);
|
|
|
|
|
|
|
+ ANKI_ASSERT(hierarchies);
|
|
|
|
|
|
|
|
- TileAllocatorResult res = TileAllocatorResult::kAllocationFailed;
|
|
|
|
|
-
|
|
|
|
|
- // Allocate atlas tiles first. They may be cached and that will affect how many scratch tiles we'll need
|
|
|
|
|
for(U i = 0; i < faceCount; ++i)
|
|
for(U i = 0; i < faceCount; ++i)
|
|
|
{
|
|
{
|
|
|
- Array<U32, 4> tileRanges;
|
|
|
|
|
- res = m_atlas.m_tileAlloc.allocate(m_r->getGlobalTimestamp(), faceTimestamps[i], lightUuid, faceIndices[i],
|
|
|
|
|
- drawcallsCount[i], lods[i], tileRanges);
|
|
|
|
|
|
|
+ Array<U32, 4> tileViewport;
|
|
|
|
|
+ subResults[i] = m_tileAlloc.allocate(m_r->getGlobalTimestamp(), faceTimestamps[i], lightUuid, faceIndices[i],
|
|
|
|
|
+ drawcallsCount[i], hierarchies[i], tileViewport);
|
|
|
|
|
|
|
|
- if(res == TileAllocatorResult::kAllocationFailed)
|
|
|
|
|
|
|
+ if(subResults[i] == TileAllocatorResult::kAllocationFailed)
|
|
|
{
|
|
{
|
|
|
ANKI_R_LOGW("There is not enough space in the shadow atlas for more shadow maps. "
|
|
ANKI_R_LOGW("There is not enough space in the shadow atlas for more shadow maps. "
|
|
|
"Increase the RShadowMappingTileCountPerRowOrColumn or decrease the scene's shadow casters");
|
|
"Increase the RShadowMappingTileCountPerRowOrColumn or decrease the scene's shadow casters");
|
|
@@ -431,79 +221,55 @@ TileAllocatorResult ShadowMapping::allocateTilesAndScratchTiles(U64 lightUuid, U
|
|
|
// Invalidate cache entries for what we already allocated
|
|
// Invalidate cache entries for what we already allocated
|
|
|
for(U j = 0; j < i; ++j)
|
|
for(U j = 0; j < i; ++j)
|
|
|
{
|
|
{
|
|
|
- m_atlas.m_tileAlloc.invalidateCache(lightUuid, faceIndices[j]);
|
|
|
|
|
|
|
+ m_tileAlloc.invalidateCache(lightUuid, faceIndices[j]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return res;
|
|
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- subResults[i] = res;
|
|
|
|
|
-
|
|
|
|
|
// Set viewport
|
|
// Set viewport
|
|
|
- atlasTileViewports[i] = UVec4(tileRanges) * m_atlas.m_tileResolution;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Allocate scratch tiles
|
|
|
|
|
- for(U i = 0; i < faceCount; ++i)
|
|
|
|
|
- {
|
|
|
|
|
- if(subResults[i] == TileAllocatorResult::kCached)
|
|
|
|
|
- {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- ANKI_ASSERT(subResults[i] == TileAllocatorResult::kAllocationSucceded);
|
|
|
|
|
-
|
|
|
|
|
- Array<U32, 4> tileRanges;
|
|
|
|
|
- res = m_scratch.m_tileAlloc.allocate(m_r->getGlobalTimestamp(), faceTimestamps[i], lightUuid, faceIndices[i],
|
|
|
|
|
- drawcallsCount[i], lods[i], tileRanges);
|
|
|
|
|
-
|
|
|
|
|
- if(res == TileAllocatorResult::kAllocationFailed)
|
|
|
|
|
- {
|
|
|
|
|
- ANKI_R_LOGW("Don't have enough space in the scratch shadow mapping buffer. "
|
|
|
|
|
- "If you see this message too often increase RShadowMappingScratchTileCountX/Y");
|
|
|
|
|
-
|
|
|
|
|
- // Invalidate atlas tiles
|
|
|
|
|
- for(U j = 0; j < faceCount; ++j)
|
|
|
|
|
- {
|
|
|
|
|
- m_atlas.m_tileAlloc.invalidateCache(lightUuid, faceIndices[j]);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const UVec4 viewport = UVec4(tileViewport) * m_tileResolution;
|
|
|
|
|
+ atlasTileViewports[i] = viewport;
|
|
|
|
|
|
|
|
- return res;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Fix viewport
|
|
|
|
|
- scratchTileViewports[i] = UVec4(tileRanges) * m_scratch.m_tileResolution;
|
|
|
|
|
-
|
|
|
|
|
- // Update the max view width
|
|
|
|
|
- m_scratch.m_maxViewportWidth =
|
|
|
|
|
- max(m_scratch.m_maxViewportWidth, scratchTileViewports[i][0] + scratchTileViewports[i][2]);
|
|
|
|
|
- m_scratch.m_maxViewportHeight =
|
|
|
|
|
- max(m_scratch.m_maxViewportHeight, scratchTileViewports[i][1] + scratchTileViewports[i][3]);
|
|
|
|
|
|
|
+ m_runCtx.m_fullViewport[0] = min(m_runCtx.m_fullViewport[0], viewport[0]);
|
|
|
|
|
+ m_runCtx.m_fullViewport[1] = min(m_runCtx.m_fullViewport[1], viewport[1]);
|
|
|
|
|
+ m_runCtx.m_fullViewport[2] = max(m_runCtx.m_fullViewport[2], viewport[0] + viewport[2]);
|
|
|
|
|
+ m_runCtx.m_fullViewport[3] = max(m_runCtx.m_fullViewport[3], viewport[1] + viewport[3]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return res;
|
|
|
|
|
|
|
+ return true;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void ShadowMapping::newWorkItems(const UVec4& atlasViewport, RenderQueue* lightRenderQueue, U32 renderQueueElementsLod,
|
|
|
|
|
+ DynamicArrayRaii<LightToRenderTempInfo>& workItems, U32& drawcallCount) const
|
|
|
|
|
+{
|
|
|
|
|
+ LightToRenderTempInfo toRender;
|
|
|
|
|
+ toRender.m_renderQueue = lightRenderQueue;
|
|
|
|
|
+ toRender.m_viewport = atlasViewport;
|
|
|
|
|
+ toRender.m_drawcallCount = lightRenderQueue->m_renderables.getSize();
|
|
|
|
|
+ toRender.m_renderQueueElementsLod = renderQueueElementsLod;
|
|
|
|
|
+
|
|
|
|
|
+ workItems.emplaceBack(toRender);
|
|
|
|
|
+ drawcallCount += toRender.m_drawcallCount;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScratchPass)
|
|
|
|
|
|
|
+void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForPass)
|
|
|
{
|
|
{
|
|
|
- // Reset the scratch viewport width
|
|
|
|
|
- m_scratch.m_maxViewportWidth = 0;
|
|
|
|
|
- m_scratch.m_maxViewportHeight = 0;
|
|
|
|
|
|
|
+ m_runCtx.m_fullViewport = UVec4(kMaxU32, kMaxU32, kMinU32, kMinU32);
|
|
|
|
|
|
|
|
// Vars
|
|
// Vars
|
|
|
const Vec4 cameraOrigin = ctx.m_renderQueue->m_cameraTransform.getTranslationPart().xyz0();
|
|
const Vec4 cameraOrigin = ctx.m_renderQueue->m_cameraTransform.getTranslationPart().xyz0();
|
|
|
- DynamicArrayRaii<Scratch::LightToRenderToScratchInfo> lightsToRender(ctx.m_tempPool);
|
|
|
|
|
|
|
+ DynamicArrayRaii<LightToRenderTempInfo> lightsToRender(ctx.m_tempPool);
|
|
|
U32 drawcallCount = 0;
|
|
U32 drawcallCount = 0;
|
|
|
- DynamicArrayRaii<Atlas::ResolveWorkItem> atlasWorkItems(ctx.m_tempPool);
|
|
|
|
|
|
|
|
|
|
// First thing, allocate an empty tile for empty faces of point lights
|
|
// First thing, allocate an empty tile for empty faces of point lights
|
|
|
UVec4 emptyTileViewport;
|
|
UVec4 emptyTileViewport;
|
|
|
{
|
|
{
|
|
|
- Array<U32, 4> tileRange;
|
|
|
|
|
- [[maybe_unused]] const TileAllocatorResult res =
|
|
|
|
|
- m_atlas.m_tileAlloc.allocate(m_r->getGlobalTimestamp(), 1, kMaxU64, 0, 1, m_pointLightsMaxLod, tileRange);
|
|
|
|
|
|
|
+ Array<U32, 4> tileViewport;
|
|
|
|
|
+ [[maybe_unused]] const TileAllocatorResult res = m_tileAlloc.allocate(
|
|
|
|
|
+ m_r->getGlobalTimestamp(), 1, kMaxU64, 0, 1, kPointLightMaxTileAllocHierarchy, tileViewport);
|
|
|
|
|
|
|
|
- emptyTileViewport = UVec4(tileRange);
|
|
|
|
|
|
|
+ emptyTileViewport = UVec4(tileViewport);
|
|
|
|
|
|
|
|
#if ANKI_ENABLE_ASSERTIONS
|
|
#if ANKI_ENABLE_ASSERTIONS
|
|
|
static Bool firstRun = true;
|
|
static Bool firstRun = true;
|
|
@@ -528,11 +294,9 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
Array<U32, kMaxShadowCascades> cascadeIndices;
|
|
Array<U32, kMaxShadowCascades> cascadeIndices;
|
|
|
Array<U32, kMaxShadowCascades> drawcallCounts;
|
|
Array<U32, kMaxShadowCascades> drawcallCounts;
|
|
|
Array<UVec4, kMaxShadowCascades> atlasViewports;
|
|
Array<UVec4, kMaxShadowCascades> atlasViewports;
|
|
|
- Array<UVec4, kMaxShadowCascades> scratchViewports;
|
|
|
|
|
Array<TileAllocatorResult, kMaxShadowCascades> subResults;
|
|
Array<TileAllocatorResult, kMaxShadowCascades> subResults;
|
|
|
- Array<U32, kMaxShadowCascades> lods;
|
|
|
|
|
|
|
+ Array<U32, kMaxShadowCascades> hierarchies;
|
|
|
Array<U32, kMaxShadowCascades> renderQueueElementsLods;
|
|
Array<U32, kMaxShadowCascades> renderQueueElementsLods;
|
|
|
- Array<Bool, kMaxShadowCascades> blurAtlass;
|
|
|
|
|
|
|
|
|
|
U32 activeCascades = 0;
|
|
U32 activeCascades = 0;
|
|
|
|
|
|
|
@@ -548,8 +312,8 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
drawcallCounts[activeCascades] = 1; // Doesn't matter
|
|
drawcallCounts[activeCascades] = 1; // Doesn't matter
|
|
|
|
|
|
|
|
// Change the quality per cascade
|
|
// Change the quality per cascade
|
|
|
- blurAtlass[activeCascades] = (cascade <= 1);
|
|
|
|
|
- lods[activeCascades] = (cascade <= 1) ? (kMaxLodCount - 1) : (lods[0] - 1);
|
|
|
|
|
|
|
+ hierarchies[activeCascades] =
|
|
|
|
|
+ (cascade <= 1) ? (kTileAllocHierarchyCount - 1) : (kTileAllocHierarchyCount - 2);
|
|
|
renderQueueElementsLods[activeCascades] = (cascade == 0) ? 0 : (kMaxLodCount - 1);
|
|
renderQueueElementsLods[activeCascades] = (cascade == 0) ? 0 : (kMaxLodCount - 1);
|
|
|
|
|
|
|
|
++activeCascades;
|
|
++activeCascades;
|
|
@@ -558,10 +322,8 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
|
|
|
|
|
const Bool allocationFailed =
|
|
const Bool allocationFailed =
|
|
|
activeCascades == 0
|
|
activeCascades == 0
|
|
|
- || allocateTilesAndScratchTiles(light.m_uuid, activeCascades, ×tamps[0], &cascadeIndices[0],
|
|
|
|
|
- &drawcallCounts[0], &lods[0], &atlasViewports[0], &scratchViewports[0],
|
|
|
|
|
- &subResults[0])
|
|
|
|
|
- == TileAllocatorResult::kAllocationFailed;
|
|
|
|
|
|
|
+ || !allocateAtlasTiles(light.m_uuid, activeCascades, ×tamps[0], &cascadeIndices[0], &drawcallCounts[0],
|
|
|
|
|
+ &hierarchies[0], &atlasViewports[0], &subResults[0]);
|
|
|
|
|
|
|
|
if(!allocationFailed)
|
|
if(!allocationFailed)
|
|
|
{
|
|
{
|
|
@@ -578,10 +340,8 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
createSpotLightTextureMatrix(atlasViewports[activeCascades]) * light.m_textureMatrices[cascade];
|
|
createSpotLightTextureMatrix(atlasViewports[activeCascades]) * light.m_textureMatrices[cascade];
|
|
|
|
|
|
|
|
// Push work
|
|
// Push work
|
|
|
- newScratchAndAtlasResloveRenderWorkItems(
|
|
|
|
|
- atlasViewports[activeCascades], scratchViewports[activeCascades], blurAtlass[activeCascades],
|
|
|
|
|
- light.m_shadowRenderQueues[cascade], renderQueueElementsLods[activeCascades], lightsToRender,
|
|
|
|
|
- atlasWorkItems, drawcallCount);
|
|
|
|
|
|
|
+ newWorkItems(atlasViewports[activeCascades], light.m_shadowRenderQueues[cascade],
|
|
|
|
|
+ renderQueueElementsLods[activeCascades], lightsToRender, drawcallCount);
|
|
|
|
|
|
|
|
++activeCascades;
|
|
++activeCascades;
|
|
|
}
|
|
}
|
|
@@ -615,14 +375,12 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
Array<U32, 6> faceIndices;
|
|
Array<U32, 6> faceIndices;
|
|
|
Array<U32, 6> drawcallCounts;
|
|
Array<U32, 6> drawcallCounts;
|
|
|
Array<UVec4, 6> atlasViewports;
|
|
Array<UVec4, 6> atlasViewports;
|
|
|
- Array<UVec4, 6> scratchViewports;
|
|
|
|
|
Array<TileAllocatorResult, 6> subResults;
|
|
Array<TileAllocatorResult, 6> subResults;
|
|
|
- Array<U32, 6> lods;
|
|
|
|
|
|
|
+ Array<U32, 6> hierarchies;
|
|
|
U32 numOfFacesThatHaveDrawcalls = 0;
|
|
U32 numOfFacesThatHaveDrawcalls = 0;
|
|
|
|
|
|
|
|
- Bool blurAtlas;
|
|
|
|
|
- U32 lod, renderQueueElementsLod;
|
|
|
|
|
- chooseLod(cameraOrigin, light, blurAtlas, lod, renderQueueElementsLod);
|
|
|
|
|
|
|
+ U32 hierarchy, renderQueueElementsLod;
|
|
|
|
|
+ chooseDetail(cameraOrigin, light, hierarchy, renderQueueElementsLod);
|
|
|
|
|
|
|
|
for(U32 face = 0; face < 6; ++face)
|
|
for(U32 face = 0; face < 6; ++face)
|
|
|
{
|
|
{
|
|
@@ -637,7 +395,7 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
|
|
|
|
|
drawcallCounts[numOfFacesThatHaveDrawcalls] = light.m_shadowRenderQueues[face]->m_renderables.getSize();
|
|
drawcallCounts[numOfFacesThatHaveDrawcalls] = light.m_shadowRenderQueues[face]->m_renderables.getSize();
|
|
|
|
|
|
|
|
- lods[numOfFacesThatHaveDrawcalls] = lod;
|
|
|
|
|
|
|
+ hierarchies[numOfFacesThatHaveDrawcalls] = hierarchy;
|
|
|
|
|
|
|
|
++numOfFacesThatHaveDrawcalls;
|
|
++numOfFacesThatHaveDrawcalls;
|
|
|
}
|
|
}
|
|
@@ -645,18 +403,27 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
|
|
|
|
|
const Bool allocationFailed =
|
|
const Bool allocationFailed =
|
|
|
numOfFacesThatHaveDrawcalls == 0
|
|
numOfFacesThatHaveDrawcalls == 0
|
|
|
- || allocateTilesAndScratchTiles(light.m_uuid, numOfFacesThatHaveDrawcalls, ×tamps[0], &faceIndices[0],
|
|
|
|
|
- &drawcallCounts[0], &lods[0], &atlasViewports[0], &scratchViewports[0],
|
|
|
|
|
- &subResults[0])
|
|
|
|
|
- == TileAllocatorResult::kAllocationFailed;
|
|
|
|
|
|
|
+ || !allocateAtlasTiles(light.m_uuid, numOfFacesThatHaveDrawcalls, ×tamps[0], &faceIndices[0],
|
|
|
|
|
+ &drawcallCounts[0], &hierarchies[0], &atlasViewports[0], &subResults[0]);
|
|
|
|
|
|
|
|
if(!allocationFailed)
|
|
if(!allocationFailed)
|
|
|
{
|
|
{
|
|
|
// All good, update the lights
|
|
// All good, update the lights
|
|
|
|
|
|
|
|
- const F32 atlasResolution = F32(m_atlas.m_tileResolution * m_atlas.m_tileCountBothAxis);
|
|
|
|
|
|
|
+ // Remove a few texels to avoid bilinear filtering bleeding
|
|
|
|
|
+ F32 texelsBorder;
|
|
|
|
|
+ if(getConfig().getRShadowMappingPcf())
|
|
|
|
|
+ {
|
|
|
|
|
+ texelsBorder = 2.0f; // 2 texels
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ texelsBorder = 0.5f; // Half texel
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const F32 atlasResolution = F32(m_tileResolution * m_tileCountBothAxis);
|
|
|
F32 superTileSize = F32(atlasViewports[0][2]); // Should be the same for all tiles and faces
|
|
F32 superTileSize = F32(atlasViewports[0][2]); // Should be the same for all tiles and faces
|
|
|
- superTileSize -= 1.0f; // Remove 2 half texels to avoid bilinear filtering bleeding
|
|
|
|
|
|
|
+ superTileSize -= texelsBorder * 2.0f; // Remove from both sides
|
|
|
|
|
|
|
|
light.m_shadowAtlasTileSize = superTileSize / atlasResolution;
|
|
light.m_shadowAtlasTileSize = superTileSize / atlasResolution;
|
|
|
|
|
|
|
@@ -668,17 +435,15 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
// Has drawcalls, asigned it to a tile
|
|
// Has drawcalls, asigned it to a tile
|
|
|
|
|
|
|
|
const UVec4& atlasViewport = atlasViewports[numOfFacesThatHaveDrawcalls];
|
|
const UVec4& atlasViewport = atlasViewports[numOfFacesThatHaveDrawcalls];
|
|
|
- const UVec4& scratchViewport = scratchViewports[numOfFacesThatHaveDrawcalls];
|
|
|
|
|
|
|
|
|
|
// Add a half texel to the viewport's start to avoid bilinear filtering bleeding
|
|
// Add a half texel to the viewport's start to avoid bilinear filtering bleeding
|
|
|
- light.m_shadowAtlasTileOffsets[face].x() = (F32(atlasViewport[0]) + 0.5f) / atlasResolution;
|
|
|
|
|
- light.m_shadowAtlasTileOffsets[face].y() = (F32(atlasViewport[1]) + 0.5f) / atlasResolution;
|
|
|
|
|
|
|
+ light.m_shadowAtlasTileOffsets[face].x() = (F32(atlasViewport[0]) + texelsBorder) / atlasResolution;
|
|
|
|
|
+ light.m_shadowAtlasTileOffsets[face].y() = (F32(atlasViewport[1]) + texelsBorder) / atlasResolution;
|
|
|
|
|
|
|
|
if(subResults[numOfFacesThatHaveDrawcalls] != TileAllocatorResult::kCached)
|
|
if(subResults[numOfFacesThatHaveDrawcalls] != TileAllocatorResult::kCached)
|
|
|
{
|
|
{
|
|
|
- newScratchAndAtlasResloveRenderWorkItems(
|
|
|
|
|
- atlasViewport, scratchViewport, blurAtlas, light.m_shadowRenderQueues[face],
|
|
|
|
|
- renderQueueElementsLod, lightsToRender, atlasWorkItems, drawcallCount);
|
|
|
|
|
|
|
+ newWorkItems(atlasViewport, light.m_shadowRenderQueues[face], renderQueueElementsLod,
|
|
|
|
|
+ lightsToRender, drawcallCount);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
++numOfFacesThatHaveDrawcalls;
|
|
++numOfFacesThatHaveDrawcalls;
|
|
@@ -691,8 +456,8 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
atlasViewport[2] = U32(superTileSize);
|
|
atlasViewport[2] = U32(superTileSize);
|
|
|
atlasViewport[3] = U32(superTileSize);
|
|
atlasViewport[3] = U32(superTileSize);
|
|
|
|
|
|
|
|
- light.m_shadowAtlasTileOffsets[face].x() = (F32(atlasViewport[0]) + 0.5f) / atlasResolution;
|
|
|
|
|
- light.m_shadowAtlasTileOffsets[face].y() = (F32(atlasViewport[1]) + 0.5f) / atlasResolution;
|
|
|
|
|
|
|
+ light.m_shadowAtlasTileOffsets[face].x() = (F32(atlasViewport[0]) + texelsBorder) / atlasResolution;
|
|
|
|
|
+ light.m_shadowAtlasTileOffsets[face].y() = (F32(atlasViewport[1]) + texelsBorder) / atlasResolution;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -718,16 +483,13 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
UVec4 scratchViewport;
|
|
UVec4 scratchViewport;
|
|
|
const U32 localDrawcallCount = light.m_shadowRenderQueue->m_renderables.getSize();
|
|
const U32 localDrawcallCount = light.m_shadowRenderQueue->m_renderables.getSize();
|
|
|
|
|
|
|
|
- Bool blurAtlas;
|
|
|
|
|
- U32 lod, renderQueueElementsLod;
|
|
|
|
|
- chooseLod(cameraOrigin, light, blurAtlas, lod, renderQueueElementsLod);
|
|
|
|
|
|
|
+ U32 hierarchy, renderQueueElementsLod;
|
|
|
|
|
+ chooseDetail(cameraOrigin, light, hierarchy, renderQueueElementsLod);
|
|
|
|
|
|
|
|
const Bool allocationFailed =
|
|
const Bool allocationFailed =
|
|
|
localDrawcallCount == 0
|
|
localDrawcallCount == 0
|
|
|
- || allocateTilesAndScratchTiles(
|
|
|
|
|
- light.m_uuid, 1, &light.m_shadowRenderQueue->m_shadowRenderablesLastUpdateTimestamp, &faceIdx,
|
|
|
|
|
- &localDrawcallCount, &lod, &atlasViewport, &scratchViewport, &subResult)
|
|
|
|
|
- == TileAllocatorResult::kAllocationFailed;
|
|
|
|
|
|
|
+ || !allocateAtlasTiles(light.m_uuid, 1, &light.m_shadowRenderQueue->m_shadowRenderablesLastUpdateTimestamp,
|
|
|
|
|
+ &faceIdx, &localDrawcallCount, &hierarchy, &atlasViewport, &subResult);
|
|
|
|
|
|
|
|
if(!allocationFailed)
|
|
if(!allocationFailed)
|
|
|
{
|
|
{
|
|
@@ -738,9 +500,8 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
|
|
|
|
|
if(subResult != TileAllocatorResult::kCached)
|
|
if(subResult != TileAllocatorResult::kCached)
|
|
|
{
|
|
{
|
|
|
- newScratchAndAtlasResloveRenderWorkItems(atlasViewport, scratchViewport, blurAtlas,
|
|
|
|
|
- light.m_shadowRenderQueue, renderQueueElementsLod,
|
|
|
|
|
- lightsToRender, atlasWorkItems, drawcallCount);
|
|
|
|
|
|
|
+ newWorkItems(atlasViewport, light.m_shadowRenderQueue, renderQueueElementsLod, lightsToRender,
|
|
|
|
|
+ drawcallCount);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
@@ -753,13 +514,13 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
// Split the work that will happen in the scratch buffer
|
|
// Split the work that will happen in the scratch buffer
|
|
|
if(lightsToRender.getSize())
|
|
if(lightsToRender.getSize())
|
|
|
{
|
|
{
|
|
|
- DynamicArrayRaii<Scratch::WorkItem> workItems(ctx.m_tempPool);
|
|
|
|
|
- Scratch::LightToRenderToScratchInfo* lightToRender = lightsToRender.getBegin();
|
|
|
|
|
|
|
+ DynamicArrayRaii<ThreadWorkItem> workItems(ctx.m_tempPool);
|
|
|
|
|
+ LightToRenderTempInfo* lightToRender = lightsToRender.getBegin();
|
|
|
U32 lightToRenderDrawcallCount = lightToRender->m_drawcallCount;
|
|
U32 lightToRenderDrawcallCount = lightToRender->m_drawcallCount;
|
|
|
- const Scratch::LightToRenderToScratchInfo* lightToRenderEnd = lightsToRender.getEnd();
|
|
|
|
|
|
|
+ const LightToRenderTempInfo* lightToRenderEnd = lightsToRender.getEnd();
|
|
|
|
|
|
|
|
const U32 threadCount = computeNumberOfSecondLevelCommandBuffers(drawcallCount);
|
|
const U32 threadCount = computeNumberOfSecondLevelCommandBuffers(drawcallCount);
|
|
|
- threadCountForScratchPass = threadCount;
|
|
|
|
|
|
|
+ threadCountForPass = threadCount;
|
|
|
for(U32 taskId = 0; taskId < threadCount; ++taskId)
|
|
for(U32 taskId = 0; taskId < threadCount; ++taskId)
|
|
|
{
|
|
{
|
|
|
U32 start, end;
|
|
U32 start, end;
|
|
@@ -774,7 +535,7 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
ANKI_ASSERT(lightToRender != lightToRenderEnd);
|
|
ANKI_ASSERT(lightToRender != lightToRenderEnd);
|
|
|
const U32 workItemDrawcallCount = min(lightToRenderDrawcallCount, taskDrawcallCount);
|
|
const U32 workItemDrawcallCount = min(lightToRenderDrawcallCount, taskDrawcallCount);
|
|
|
|
|
|
|
|
- Scratch::WorkItem workItem;
|
|
|
|
|
|
|
+ ThreadWorkItem workItem;
|
|
|
workItem.m_viewport = lightToRender->m_viewport;
|
|
workItem.m_viewport = lightToRender->m_viewport;
|
|
|
workItem.m_renderQueue = lightToRender->m_renderQueue;
|
|
workItem.m_renderQueue = lightToRender->m_renderQueue;
|
|
|
workItem.m_firstRenderableElement = lightToRender->m_drawcallCount - lightToRenderDrawcallCount;
|
|
workItem.m_firstRenderableElement = lightToRender->m_drawcallCount - lightToRenderDrawcallCount;
|
|
@@ -803,76 +564,61 @@ void ShadowMapping::processLights(RenderingContext& ctx, U32& threadCountForScra
|
|
|
ANKI_ASSERT(lightsToRender.getSize() <= workItems.getSize());
|
|
ANKI_ASSERT(lightsToRender.getSize() <= workItems.getSize());
|
|
|
|
|
|
|
|
// All good, store the work items for the threads to pick up
|
|
// All good, store the work items for the threads to pick up
|
|
|
- {
|
|
|
|
|
- Scratch::WorkItem* items;
|
|
|
|
|
- U32 itemSize;
|
|
|
|
|
- U32 itemStorageSize;
|
|
|
|
|
- workItems.moveAndReset(items, itemSize, itemStorageSize);
|
|
|
|
|
-
|
|
|
|
|
- ANKI_ASSERT(items && itemSize && itemStorageSize);
|
|
|
|
|
- m_scratch.m_workItems = WeakArray<Scratch::WorkItem>(items, itemSize);
|
|
|
|
|
-
|
|
|
|
|
- Atlas::ResolveWorkItem* atlasItems;
|
|
|
|
|
- atlasWorkItems.moveAndReset(atlasItems, itemSize, itemStorageSize);
|
|
|
|
|
- ANKI_ASSERT(atlasItems && itemSize && itemStorageSize);
|
|
|
|
|
- m_atlas.m_resolveWorkItems = WeakArray<Atlas::ResolveWorkItem>(atlasItems, itemSize);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ workItems.moveAndReset(m_runCtx.m_workItems);
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- m_scratch.m_workItems = WeakArray<Scratch::WorkItem>();
|
|
|
|
|
- m_atlas.m_resolveWorkItems = WeakArray<Atlas::ResolveWorkItem>();
|
|
|
|
|
|
|
+ m_runCtx.m_workItems = {};
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void ShadowMapping::newScratchAndAtlasResloveRenderWorkItems(
|
|
|
|
|
- const UVec4& atlasViewport, const UVec4& scratchVewport, Bool blurAtlas, RenderQueue* lightRenderQueue,
|
|
|
|
|
- U32 renderQueueElementsLod, DynamicArrayRaii<Scratch::LightToRenderToScratchInfo>& scratchWorkItem,
|
|
|
|
|
- DynamicArrayRaii<Atlas::ResolveWorkItem>& atlasResolveWorkItem, U32& drawcallCount) const
|
|
|
|
|
|
|
+void ShadowMapping::runShadowMapping(RenderPassWorkContext& rgraphCtx)
|
|
|
{
|
|
{
|
|
|
- // Scratch work item
|
|
|
|
|
- {
|
|
|
|
|
- Scratch::LightToRenderToScratchInfo toRender;
|
|
|
|
|
- toRender.m_renderQueue = lightRenderQueue;
|
|
|
|
|
- toRender.m_viewport = scratchVewport;
|
|
|
|
|
- toRender.m_drawcallCount = lightRenderQueue->m_renderables.getSize();
|
|
|
|
|
- toRender.m_renderQueueElementsLod = renderQueueElementsLod;
|
|
|
|
|
-
|
|
|
|
|
- scratchWorkItem.emplaceBack(toRender);
|
|
|
|
|
- drawcallCount += lightRenderQueue->m_renderables.getSize();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ ANKI_ASSERT(m_runCtx.m_workItems.getSize());
|
|
|
|
|
+ ANKI_TRACE_SCOPED_EVENT(R_SM);
|
|
|
|
|
|
|
|
- // Atlas resolve work items
|
|
|
|
|
- const U32 tilesX = scratchVewport[2] / m_scratch.m_tileResolution;
|
|
|
|
|
- const U32 tilesY = scratchVewport[3] / m_scratch.m_tileResolution;
|
|
|
|
|
- for(U32 x = 0; x < tilesX; ++x)
|
|
|
|
|
- {
|
|
|
|
|
- for(U32 y = 0; y < tilesY; ++y)
|
|
|
|
|
- {
|
|
|
|
|
- const F32 scratchAtlasWidth = F32(m_scratch.m_tileCountX * m_scratch.m_tileResolution);
|
|
|
|
|
- const F32 scratchAtlasHeight = F32(m_scratch.m_tileCountY * m_scratch.m_tileResolution);
|
|
|
|
|
|
|
+ CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
|
|
|
|
|
+ const U threadIdx = rgraphCtx.m_currentSecondLevelCommandBufferIndex;
|
|
|
|
|
|
|
|
- Atlas::ResolveWorkItem atlasItem;
|
|
|
|
|
|
|
+ cmdb->setPolygonOffset(kShadowsPolygonOffsetFactor, kShadowsPolygonOffsetUnits);
|
|
|
|
|
|
|
|
- atlasItem.m_uvInBounds[0] = F32(scratchVewport[0]) / scratchAtlasWidth;
|
|
|
|
|
- atlasItem.m_uvInBounds[1] = F32(scratchVewport[1]) / scratchAtlasHeight;
|
|
|
|
|
- atlasItem.m_uvInBounds[2] = F32(scratchVewport[2]) / scratchAtlasWidth;
|
|
|
|
|
- atlasItem.m_uvInBounds[3] = F32(scratchVewport[3]) / scratchAtlasHeight;
|
|
|
|
|
|
|
+ for(ThreadWorkItem& work : m_runCtx.m_workItems)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(work.m_threadPoolTaskIdx != threadIdx)
|
|
|
|
|
+ {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- atlasItem.m_uvIn[0] = F32(scratchVewport[0] + scratchVewport[2] / tilesX * x) / scratchAtlasWidth;
|
|
|
|
|
- atlasItem.m_uvIn[1] = F32(scratchVewport[1] + scratchVewport[3] / tilesY * y) / scratchAtlasHeight;
|
|
|
|
|
- atlasItem.m_uvIn[2] = F32(scratchVewport[2] / tilesX) / scratchAtlasWidth;
|
|
|
|
|
- atlasItem.m_uvIn[3] = F32(scratchVewport[3] / tilesY) / scratchAtlasHeight;
|
|
|
|
|
|
|
+ // Set state
|
|
|
|
|
+ cmdb->setViewport(work.m_viewport[0], work.m_viewport[1], work.m_viewport[2], work.m_viewport[3]);
|
|
|
|
|
+ cmdb->setScissor(work.m_viewport[0], work.m_viewport[1], work.m_viewport[2], work.m_viewport[3]);
|
|
|
|
|
|
|
|
- atlasItem.m_viewportOut[0] = atlasViewport[0] + atlasViewport[2] / tilesX * x;
|
|
|
|
|
- atlasItem.m_viewportOut[1] = atlasViewport[1] + atlasViewport[3] / tilesY * y;
|
|
|
|
|
- atlasItem.m_viewportOut[2] = atlasViewport[2] / tilesX;
|
|
|
|
|
- atlasItem.m_viewportOut[3] = atlasViewport[3] / tilesY;
|
|
|
|
|
|
|
+ // The 1st drawcall will clear the depth buffer
|
|
|
|
|
+ if(work.m_firstRenderableElement == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ cmdb->bindShaderProgram(m_clearDepthGrProg);
|
|
|
|
|
+ cmdb->setDepthCompareOperation(CompareOperation::kAlways);
|
|
|
|
|
+ cmdb->setPolygonOffset(0.0f, 0.0f);
|
|
|
|
|
+ cmdb->drawArrays(PrimitiveTopology::kTriangles, 3, 1);
|
|
|
|
|
+
|
|
|
|
|
+ // Restore state
|
|
|
|
|
+ cmdb->setDepthCompareOperation(CompareOperation::kLess);
|
|
|
|
|
+ cmdb->setPolygonOffset(kShadowsPolygonOffsetFactor, kShadowsPolygonOffsetUnits);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- atlasItem.m_blur = blurAtlas;
|
|
|
|
|
|
|
+ RenderableDrawerArguments args;
|
|
|
|
|
+ args.m_viewMatrix = work.m_renderQueue->m_viewMatrix;
|
|
|
|
|
+ args.m_cameraTransform = Mat3x4::getIdentity(); // Don't care
|
|
|
|
|
+ args.m_viewProjectionMatrix = work.m_renderQueue->m_viewProjectionMatrix;
|
|
|
|
|
+ args.m_previousViewProjectionMatrix = Mat4::getIdentity(); // Don't care
|
|
|
|
|
+ args.m_sampler = m_r->getSamplers().m_trilinearRepeatAniso;
|
|
|
|
|
+ args.m_minLod = args.m_maxLod = work.m_renderQueueElementsLod;
|
|
|
|
|
|
|
|
- atlasResolveWorkItem.emplaceBack(atlasItem);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ m_r->getSceneDrawer().drawRange(RenderingTechnique::kShadow, args,
|
|
|
|
|
+ work.m_renderQueue->m_renderables.getBegin() + work.m_firstRenderableElement,
|
|
|
|
|
+ work.m_renderQueue->m_renderables.getBegin() + work.m_firstRenderableElement
|
|
|
|
|
+ + work.m_renderableElementCount,
|
|
|
|
|
+ cmdb);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|