void SDSMShadowManager::RenderAccumulatedShadowMap(RenderPassContext *renderPassContext, DepthStencilTarget *gbufferDepth, RenderTarget *gbufferNormals)
{
    Direct3DManager *direct3DManager = mGraphicsManager->GetDirect3DManager();
    GraphicsContext *graphicsContext = direct3DManager->GetContextManager()->GetGraphicsContext();
    Direct3DHeapManager *heapManager = direct3DManager->GetContextManager()->GetHeapManager();
    Vector2 screenSize = direct3DManager->GetScreenSize();

    graphicsContext->TransitionResource(gbufferNormals, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, false);
    graphicsContext->TransitionResource(gbufferNormals, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, false);
    graphicsContext->TransitionResource(mShadowAccumulationTexture, D3D12_RESOURCE_STATE_RENDER_TARGET, true);

    graphicsContext->SetViewport(direct3DManager->GetScreenViewport());
    graphicsContext->SetScissorRect(0, 0, (uint32)screenSize.X, (uint32)screenSize.Y);

    DescriptorHeapHandle shadowSRVHandle = renderPassContext->GetCBVSRVHeap()->GetHeapHandleBlock(7);
    DescriptorHeapHandle shadowSamplersHandle = renderPassContext->GetSamplerHeap()->GetHeapHandleBlock(1);
    DescriptorHeapHandle shadowCBVHandle = renderPassContext->GetCBVSRVHeap()->GetHeapHandleBlock(1);

    D3D12_CPU_DESCRIPTOR_HANDLE shadowSRVHandleOffset = shadowSRVHandle.GetCPUHandle();
    uint32 srvHandleOffsetIncrement = renderPassContext->GetCBVSRVHeap()->GetDescriptorSize();

    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowSRVHandleOffset, gbufferNormals->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
    shadowSRVHandleOffset.ptr += srvHandleOffsetIncrement;
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowSRVHandleOffset, gbufferDepth->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
    shadowSRVHandleOffset.ptr += srvHandleOffsetIncrement;
    
    for (uint32 i = 0; i < SDSM_SHADOW_PARTITION_COUNT; i++)
    {
        direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowSRVHandleOffset, mShadowEVSMTextures[i]->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
        shadowSRVHandleOffset.ptr += srvHandleOffsetIncrement;
    }

    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowSRVHandleOffset, mShadowPartitionBuffer->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
    
    Sampler *shadowSampler = mGraphicsManager->GetSamplerManager()->GetSampler(SAMPLER_DEFAULT_ANISO_16_CLAMP);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowSamplersHandle.GetCPUHandle(), shadowSampler->GetSamplerHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);

    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowCBVHandle.GetCPUHandle(), mSDSMAccumulationBuffers[direct3DManager->GetFrameIndex()]->GetConstantBufferViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    graphicsContext->SetRenderTarget(mShadowAccumulationTexture->GetRenderTargetViewHandle().GetCPUHandle());

    graphicsContext->SetPipelineState(mSDSMAccumulationShader);
    graphicsContext->SetRootSignature(mSDSMAccumulationShader->GetRootSignature(), NULL);

    graphicsContext->SetGraphicsDescriptorTable(0, shadowSRVHandle.GetGPUHandle());
    graphicsContext->SetGraphicsDescriptorTable(1, shadowSamplersHandle.GetGPUHandle());
    graphicsContext->SetGraphicsDescriptorTable(2, shadowCBVHandle.GetGPUHandle());

    graphicsContext->DrawFullScreenTriangle();
}
void SDSMShadowManager::RenderShadowMapPartitions(const D3DXMATRIX &lightViewProjMatrix, DynamicArray<SceneEntity*> &shadowEntities, DepthStencilTarget *gbufferDepth, RenderTarget *gbufferNormals)
{
    Direct3DManager *direct3DManager = mGraphicsManager->GetDirect3DManager();
    GraphicsContext *graphicsContext = direct3DManager->GetContextManager()->GetGraphicsContext();
    Direct3DHeapManager *heapManager = direct3DManager->GetContextManager()->GetHeapManager();
    Direct3DQueueManager *queueManager = direct3DManager->GetContextManager()->GetQueueManager();

    //TDA: * mShadowPreferences.PartitionCount <--- is only for testing. Can just fill those cbv descs on the first partition and then reuse them
    uint32 numCBVSRVDescsShadowMap = shadowEntities.CurrentSize() * mShadowPreferences.PartitionCount + mShadowPreferences.PartitionCount + 1; //1 cbv per entity + X partition cbvs + 1 partition srv
    uint32 numCBVSRVDescsEVSM = 1;
    uint32 numCBVSRVDescsMips = 1 + mShadowPreferences.ShadowTextureMipLevels;
    RenderPassDescriptorHeap *shadowSRVDescHeap = heapManager->GetRenderPassDescriptorHeapFor(RenderPassDescriptorHeapType_ShadowRender, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, direct3DManager->GetFrameIndex(), numCBVSRVDescsShadowMap + numCBVSRVDescsEVSM + numCBVSRVDescsMips);
    RenderPassDescriptorHeap *shadowSamplerDescHeap = heapManager->GetRenderPassDescriptorHeapFor(RenderPassDescriptorHeapType_ShadowRender, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, direct3DManager->GetFrameIndex(), 1);
    
    graphicsContext->InsertPixBeginEvent(0xFF00FF00, "Shadow Render");

    graphicsContext->TransitionResource(mShadowPartitionBuffer, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, true);

    graphicsContext->SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, shadowSRVDescHeap->GetHeap());
    graphicsContext->SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, shadowSamplerDescHeap->GetHeap());
    graphicsContext->SetViewport(mShadowMapViewport);
    graphicsContext->SetScissorRect(0, 0, mShadowPreferences.ShadowTextureSize, mShadowPreferences.ShadowTextureSize);

    DescriptorHeapHandle shadowParitionReadBuffer = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowParitionReadBuffer.GetCPUHandle(), mShadowPartitionBuffer->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    DescriptorHeapHandle shadowMapDepthHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowMapDepthHandle.GetCPUHandle(), mShadowDepthTarget->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    DynamicArray<MaterialTextureType> noTexturesForThisPass;
    RenderPassContext shadowPassContext(graphicsContext, shadowSRVDescHeap, shadowSamplerDescHeap, noTexturesForThisPass, direct3DManager->GetFrameIndex());

    for (uint32 i = 0; i < SDSM_SHADOW_PARTITION_COUNT; i++)
    {
        DescriptorHeapHandle perPassParitionBuffer = shadowSRVDescHeap->GetHeapHandleBlock(1);
        direct3DManager->GetDevice()->CopyDescriptorsSimple(1, perPassParitionBuffer.GetCPUHandle(), mPartitionIndexBuffers[i]->GetConstantBufferViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

        graphicsContext->SetPipelineState(mShadowMapShader);
        graphicsContext->SetRootSignature(mShadowMapShader->GetRootSignature(), NULL);
        graphicsContext->SetGraphicsDescriptorTable(1, perPassParitionBuffer.GetGPUHandle());
        graphicsContext->SetGraphicsDescriptorTable(2, shadowParitionReadBuffer.GetGPUHandle());

        RenderShadowDepth(i, &shadowPassContext, lightViewProjMatrix, shadowEntities);

        graphicsContext->SetPipelineState(mShadowMapEVSMShader);
        graphicsContext->SetRootSignature(mShadowMapEVSMShader->GetRootSignature(), NULL);
        graphicsContext->SetGraphicsDescriptorTable(0, perPassParitionBuffer.GetGPUHandle());
        graphicsContext->SetGraphicsDescriptorTable(1, shadowMapDepthHandle.GetGPUHandle());
        graphicsContext->SetGraphicsDescriptorTable(2, shadowParitionReadBuffer.GetGPUHandle());

        ConvertToEVSM(i);

        if (mShadowPreferences.UseSoftShadows)
        {
            ApplyBlur();
        }

        GenerateMipsForShadowMap(i, &shadowPassContext);
        graphicsContext->TransitionResource(mShadowEVSMTextures[i], D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, true);
    }

    RenderAccumulatedShadowMap(&shadowPassContext, gbufferDepth, gbufferNormals);

    graphicsContext->InsertPixEndEvent();
}