Renders meshes using cascaded shadow mapping
| 550 | |
| 551 | // Renders meshes using cascaded shadow mapping |
| 552 | void MeshRenderer::RenderShadowMap(ID3D11DeviceContext* context, const Camera& camera, |
| 553 | const Float4x4& world) |
| 554 | { |
| 555 | PIXEvent event(L"Mesh Shadow Map Rendering"); |
| 556 | |
| 557 | // Get the current render targets + viewport |
| 558 | ID3D11RenderTargetView* renderTargets[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT] = { NULL }; |
| 559 | ID3D11DepthStencilView* depthStencil = NULL; |
| 560 | context->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, renderTargets, &depthStencil); |
| 561 | |
| 562 | uint32 numViewports = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; |
| 563 | D3D11_VIEWPORT oldViewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; |
| 564 | context->RSGetViewports(&numViewports, oldViewports); |
| 565 | |
| 566 | const float sMapSize = static_cast<float>(ShadowMapSize); |
| 567 | |
| 568 | const float MinDistance = shadowDepthBounds.x; |
| 569 | const float MaxDistance = shadowDepthBounds.y; |
| 570 | |
| 571 | // Compute the split distances based on the partitioning mode |
| 572 | float CascadeSplits[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 573 | |
| 574 | { |
| 575 | float lambda = 1.0f; |
| 576 | |
| 577 | float nearClip = camera.NearClip(); |
| 578 | float farClip = camera.FarClip(); |
| 579 | float clipRange = farClip - nearClip; |
| 580 | |
| 581 | float minZ = nearClip + MinDistance * clipRange; |
| 582 | float maxZ = nearClip + MaxDistance * clipRange; |
| 583 | |
| 584 | float range = maxZ - minZ; |
| 585 | float ratio = maxZ / minZ; |
| 586 | |
| 587 | for(uint32 i = 0; i < NumCascades; ++i) |
| 588 | { |
| 589 | float p = (i + 1) / static_cast<float>(NumCascades); |
| 590 | float log = minZ * std::pow(ratio, p); |
| 591 | float uniform = minZ + range * p; |
| 592 | float d = lambda * (log - uniform) + uniform; |
| 593 | CascadeSplits[i] = (d - nearClip) / clipRange; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | Float3 c0Extents; |
| 598 | Float4x4 c0Matrix; |
| 599 | |
| 600 | // Render the meshes to each cascade |
| 601 | for(uint32 cascadeIdx = 0; cascadeIdx < NumCascades; ++cascadeIdx) |
| 602 | { |
| 603 | PIXEvent cascadeEvent((L"Rendering Shadow Map Cascade " + ToString(cascadeIdx)).c_str()); |
| 604 | |
| 605 | // Set the viewport |
| 606 | D3D11_VIEWPORT viewport; |
| 607 | viewport.TopLeftX = 0.0f; |
| 608 | viewport.TopLeftY = 0.0f; |
| 609 | viewport.Width = sMapSize; |