Exemplo n.º 1
0
DescriptorHeapHandle RenderPassDescriptorHeap::GetHeapHandleBlock(uint32 count)
{
    uint32 newHandleID = 0;
    uint32 blockEnd = mCurrentDescriptorIndex + count;

    if (blockEnd < mMaxDescriptors)
    {
        newHandleID = mCurrentDescriptorIndex;
        mCurrentDescriptorIndex = blockEnd;
    }
    else
    {
        Direct3DUtils::ThrowRuntimeError("Ran out of render pass descriptor heap handles, need to increase heap size.");
    }

    DescriptorHeapHandle newHandle;
    D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle = mDescriptorHeapCPUStart;
    cpuHandle.ptr += newHandleID * mDescriptorSize;
    newHandle.SetCPUHandle(cpuHandle);

    D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = mDescriptorHeapGPUStart;
    gpuHandle.ptr += newHandleID * mDescriptorSize;
    newHandle.SetGPUHandle(gpuHandle);

    newHandle.SetHeapIndex(newHandleID);

    return newHandle;
}
Exemplo n.º 2
0
DescriptorHeapHandle StaticDescriptorHeap::GetNewHeapHandle()
{
    uint32 newHandleID = 0;

    if (mCurrentDescriptorIndex < mMaxDescriptors)
    {
        newHandleID = mCurrentDescriptorIndex;
        mCurrentDescriptorIndex++;
    }
    else if (mFreeDescriptors.CurrentSize() > 0)
    {
        newHandleID = mFreeDescriptors.RemoveLast();
    }
    else
    {
        Direct3DUtils::ThrowRuntimeError("Ran out of dynamic descriptor heap handles, need to increase heap size.");
    }

    DescriptorHeapHandle newHandle;
    D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle = mDescriptorHeapCPUStart;
    cpuHandle.ptr += newHandleID * mDescriptorSize;
    newHandle.SetCPUHandle(cpuHandle);
    newHandle.SetHeapIndex(newHandleID);
    mActiveHandleCount++;

    return newHandle;
}
Exemplo n.º 3
0
void Direct3DManager::BuildSwapChainDependentResources()
{
	D3D12_DESCRIPTOR_HEAP_DESC desc = {};
	desc.NumDescriptors = BACK_BUFFER_COUNT;
	desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
	desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
	
	for (uint32 bufferIndex = 0; bufferIndex < BACK_BUFFER_COUNT; bufferIndex++)
	{
		ID3D12Resource *backBufferResource = NULL;
		DescriptorHeapHandle backBufferHeapHandle = mContextManager->GetHeapManager()->GetNewRTVDescriptorHeapHandle();
		D3D12_RENDER_TARGET_VIEW_DESC rtvDesc;
		rtvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
		rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
		rtvDesc.Texture2D.MipSlice = 0;
		rtvDesc.Texture2D.PlaneSlice = 0;

		Direct3DUtils::ThrowIfHRESULTFailed(mSwapChain->GetBuffer(bufferIndex, IID_PPV_ARGS(&backBufferResource)));
		mDevice->CreateRenderTargetView(backBufferResource, &rtvDesc, backBufferHeapHandle.GetCPUHandle());

		mBackBuffers.Add(new BackBufferTarget(backBufferResource, D3D12_RESOURCE_STATE_PRESENT, backBufferHeapHandle));

		WCHAR name[25];
		swprintf_s(name, L"Backbuffer Target %d", bufferIndex);
		mBackBuffers[bufferIndex]->GetResource()->SetName(name);
	}
}
Exemplo n.º 4
0
void TextureManager::ProcessGeneration(GenerateTextureInfo &generationInfo, RenderPassDescriptorHeap *srvHeap)
{
    ComputeContext *computeContext = mDirect3DManager->GetContextManager()->GetComputeContext();
    computeContext->SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, srvHeap->GetHeap());

    computeContext->SetPipelineState(generationInfo.GenerationShader);
    computeContext->SetRootSignature(generationInfo.GenerationShader->GetRootSignature());

    DescriptorHeapHandle generateTargetHandle = srvHeap->GetHeapHandleBlock(1);
    mDirect3DManager->GetDevice()->CopyDescriptorsSimple(1, generateTargetHandle.GetCPUHandle(), generationInfo.GenerateTarget->GetUnorderedAccessViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    computeContext->SetDescriptorTable(0, generateTargetHandle.GetGPUHandle());
    computeContext->Dispatch(generationInfo.GenerateTarget->GetWidth() / 8, generationInfo.GenerateTarget->GetHeight() / 8, 1);

    generationInfo.GenerationFence = computeContext->Flush(mDirect3DManager->GetContextManager()->GetQueueManager());
}
Exemplo n.º 5
0
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();
}
Exemplo n.º 6
0
void SDSMShadowManager::GenerateMipsForShadowMap(uint32 partitionIndex, RenderPassContext *renderPassContext)
{
    Direct3DManager *direct3DManager = mGraphicsManager->GetDirect3DManager();
    GraphicsContext *graphicsContext = direct3DManager->GetContextManager()->GetGraphicsContext();

    graphicsContext->TransitionResourceExplicit(mShadowEVSMTextures[partitionIndex], D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, 0, false, true);

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

    DescriptorHeapHandle evsmTargetHandle = renderPassContext->GetCBVSRVHeap()->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, evsmTargetHandle.GetCPUHandle(), mShadowEVSMTextures[partitionIndex]->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    Sampler *mipSampler = mGraphicsManager->GetSamplerManager()->GetSampler(SAMPLER_DEFAULT_LINEAR_CLAMP);
    DescriptorHeapHandle samplerHandle = renderPassContext->GetSamplerHeap()->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, samplerHandle.GetCPUHandle(), mipSampler->GetSamplerHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);

    graphicsContext->SetComputeDescriptorTable(2, samplerHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(3, evsmTargetHandle.GetGPUHandle());

    uint32 mipTextureSize = mShadowPreferences.ShadowTextureSize;

    for (uint32 i = 1; i < mShadowPreferences.ShadowTextureMipLevels; i++)
    {
        graphicsContext->TransitionResourceExplicit(mShadowEVSMTextures[partitionIndex], D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, i, false, true);

        DescriptorHeapHandle mipTargetHandle = renderPassContext->GetCBVSRVHeap()->GetHeapHandleBlock(1);
        direct3DManager->GetDevice()->CopyDescriptorsSimple(1, mipTargetHandle.GetCPUHandle(), mShadowEVSMTextures[partitionIndex]->GetUnorderedAccessViewHandle(i).GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
    
        mipTextureSize = mipTextureSize >> 1;
        float mipTexelSize = 1.0f / (float)mipTextureSize;
        Vector3 mipConstants = Vector3(mipTexelSize, mipTexelSize, (float)(i - 1));

        graphicsContext->SetComputeDescriptorTable(0, mipTargetHandle.GetGPUHandle());
        graphicsContext->SetComputeConstants(1, 3, &mipConstants);
        graphicsContext->Dispatch2D(mipTextureSize, mipTextureSize, 8, 8);

        graphicsContext->InsertUAVBarrier(mShadowEVSMTextures[partitionIndex], false);

        graphicsContext->TransitionResourceExplicit(mShadowEVSMTextures[partitionIndex], D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, i, false, true);
    }

    for (uint32 i = 1; i < mShadowPreferences.ShadowTextureMipLevels; i++)
    {
        graphicsContext->TransitionResourceExplicit(mShadowEVSMTextures[partitionIndex], D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, i, false, false);
    }

    graphicsContext->TransitionResourceExplicit(mShadowEVSMTextures[partitionIndex], D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, 0, true, true);
}
Exemplo n.º 7
0
void StaticDescriptorHeap::FreeHeapHandle(DescriptorHeapHandle handle)
{
    mFreeDescriptors.Add(handle.GetHeapIndex());

    if (mActiveHandleCount == 0)
    {
        Direct3DUtils::ThrowRuntimeError("Freeing heap handles when there should be none left");
    }
    mActiveHandleCount--;
}
Exemplo n.º 8
0
void TextureManager::ProcessCubeMapFiltering(CubeMapFilterInfo &cubeMapFilterInfo, RenderPassDescriptorHeap *srvHeap, RenderPassDescriptorHeap *samplerHeap)
{
    Application::Assert(cubeMapFilterInfo.CubeMapToFilter->GetIsReady());

    Vector4 targets[6] =
    {
        Vector4(1.0f,  0.0f,  0.0f, 0.0f),  // +X
        Vector4(-1.0f, 0.0f,  0.0f, 0.0f),  // -X
        Vector4(0.0f,  1.0f,  0.0f, 0.0f),  // +Y
        Vector4(0.0f, -1.0f,  0.0f, 0.0f),  // -Y
        Vector4(0.0f,  0.0f,  1.0f, 0.0f),  // +Z
        Vector4(0.0f,  0.0f, -1.0f, 0.0f)   // -Z
    };

    Vector4 ups[6] =
    {
        Vector4(0.0f, 1.0f,  0.0f, 0.0f),  // +X
        Vector4(0.0f, 1.0f,  0.0f, 0.0f),  // -X
        Vector4(0.0f, 0.0f, -1.0f, 0.0f),  // +Y
        Vector4(0.0f, 0.0f,  1.0f, 0.0f),  // -Y
        Vector4(0.0f, 1.0f,  0.0f, 0.0f),  // +Z
        Vector4(0.0f, 1.0f,  0.0f, 0.0f)   // -Z
    };
    
    ComputeContext *computeContext = mDirect3DManager->GetContextManager()->GetComputeContext();
    computeContext->SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, srvHeap->GetHeap());
    computeContext->SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, samplerHeap->GetHeap());

    computeContext->SetPipelineState(cubeMapFilterInfo.EnvironmentFilterShader);
    computeContext->SetRootSignature(cubeMapFilterInfo.EnvironmentFilterShader->GetRootSignature());

    DescriptorHeapHandle envMapHandle = srvHeap->GetHeapHandleBlock(1);
    DescriptorHeapHandle envMapSamplerHandle = samplerHeap->GetHeapHandleBlock(1);
    mDirect3DManager->GetDevice()->CopyDescriptorsSimple(1, envMapHandle.GetCPUHandle(), cubeMapFilterInfo.CubeMapToFilter->GetTextureResource()->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
    mDirect3DManager->GetDevice()->CopyDescriptorsSimple(1, envMapSamplerHandle.GetCPUHandle(), cubeMapFilterInfo.EnvironmentSampler->GetSamplerHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);

    computeContext->SetDescriptorTable(0, envMapHandle.GetGPUHandle());
    computeContext->SetDescriptorTable(1, envMapSamplerHandle.GetGPUHandle());

    EnvironmentMapFilterTextureBuffer filterInfo;
    filterInfo.mipCount = (float)cubeMapFilterInfo.NumMips;

    for (uint32 cubeSideIndex = 0; cubeSideIndex < 6; cubeSideIndex++)
    {
        filterInfo.forwardDir = targets[cubeSideIndex];
        filterInfo.upDir = ups[cubeSideIndex];
        filterInfo.sourceDimensions = Vector2((float)cubeMapFilterInfo.DimensionSize, (float)cubeMapFilterInfo.DimensionSize);

        for (uint32 mipIndex = 0; mipIndex < cubeMapFilterInfo.NumMips; mipIndex++)
        {
            filterInfo.mipLevel = (float)(cubeMapFilterInfo.NumMips - mipIndex);
            DescriptorHeapHandle filterTargetHandle = srvHeap->GetHeapHandleBlock(1);
            mDirect3DManager->GetDevice()->CopyDescriptorsSimple(1, filterTargetHandle.GetCPUHandle(), cubeMapFilterInfo.FilterTarget->GetUnorderedAccessViewHandle(mipIndex, cubeSideIndex).GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

            computeContext->SetDescriptorTable(2, filterTargetHandle.GetGPUHandle());
            computeContext->SetConstants(3, 12, &filterInfo);
            computeContext->Dispatch((uint32)filterInfo.sourceDimensions.X / 8, (uint32)filterInfo.sourceDimensions.Y / 8, 1);

            filterInfo.sourceDimensions = Vector2(filterInfo.sourceDimensions.X / 2, filterInfo.sourceDimensions.Y / 2);
        }
    }

    cubeMapFilterInfo.FilterTarget->SetComputeFence(computeContext->Flush(mDirect3DManager->GetContextManager()->GetQueueManager()));
}
Exemplo n.º 9
0
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();
}
Exemplo n.º 10
0
void SDSMShadowManager::ComputeShadowPartitions(Camera *camera, D3DXMATRIX &lightViewMatrix, D3DXMATRIX &lightProjectionMatrix, DepthStencilTarget *depthStencil)
{
    Direct3DManager *direct3DManager = mGraphicsManager->GetDirect3DManager();

    Camera::CameraScreenSettings cameraSettings = camera->GetScreenSettings();
    const float maxFloat = FLT_MAX;
    Vector3 maxPartitionScale(maxFloat, maxFloat, maxFloat);

    mBlurSizeInLightSpace = Vector2(0.0f, 0.0f);

    if (mShadowPreferences.UseSoftShadows)
    {
        mBlurSizeInLightSpace.X = mShadowPreferences.SofteningAmount * 0.5f * lightProjectionMatrix._11;
        mBlurSizeInLightSpace.Y = mShadowPreferences.SofteningAmount * 0.5f * lightProjectionMatrix._22;

        float maxBlurLightSpace = mShadowPreferences.MaxSofteningFilter / ((float)mShadowPreferences.ShadowTextureSize);
        maxPartitionScale.X = maxBlurLightSpace / mBlurSizeInLightSpace.X;
        maxPartitionScale.Y = maxBlurLightSpace / mBlurSizeInLightSpace.Y;
    }

    Vector3 partitionBorderLightSpace(mBlurSizeInLightSpace.X, mBlurSizeInLightSpace.Y, 1.0f);
    partitionBorderLightSpace.Z *= lightProjectionMatrix._33;

    D3DXMATRIX cameraViewInvMatrix;
    D3DXMATRIX cameraViewMatrix = camera->GetViewMatrix();
    D3DXMatrixInverse(&cameraViewInvMatrix, NULL, &cameraViewMatrix);
    D3DXMATRIX lightViewProjMatrix = lightViewMatrix * lightProjectionMatrix;

    mCurrentSDSMBuffer.cameraProjMatrix = camera->GetReverseProjectionMatrix();
    mCurrentSDSMBuffer.cameraViewToLightProjMatrix = cameraViewInvMatrix * lightViewProjMatrix;
    mCurrentSDSMBuffer.lightSpaceBorder = Vector4(partitionBorderLightSpace.X, partitionBorderLightSpace.Y, partitionBorderLightSpace.Z, 0.0f);
    mCurrentSDSMBuffer.maxScale = Vector4(maxPartitionScale.X, maxPartitionScale.Y, maxPartitionScale.Z, 0.0f);
    mCurrentSDSMBuffer.bufferDimensions = direct3DManager->GetScreenSize();
    mCurrentSDSMBuffer.cameraNearFar = Vector2(cameraSettings.Near, cameraSettings.Far);
    mCurrentSDSMBuffer.dilationFactor = 0.01f;
    mCurrentSDSMBuffer.reduceTileDim = 64;

    D3DXMatrixTranspose(&mCurrentSDSMBuffer.cameraProjMatrix, &mCurrentSDSMBuffer.cameraProjMatrix);
    D3DXMatrixTranspose(&mCurrentSDSMBuffer.cameraViewToLightProjMatrix, &mCurrentSDSMBuffer.cameraViewToLightProjMatrix);

    mCurrentAccumulationBuffer.projectionMatrix = mCurrentSDSMBuffer.cameraProjMatrix;
    mCurrentAccumulationBuffer.viewToLightProjMatrix = mCurrentSDSMBuffer.cameraViewToLightProjMatrix;
    mCurrentAccumulationBuffer.bufferDimensions = direct3DManager->GetScreenSize();

    mSDSMBuffers[direct3DManager->GetFrameIndex()]->SetConstantBufferData(&mCurrentSDSMBuffer, sizeof(SDSMBuffer));
    mSDSMAccumulationBuffers[direct3DManager->GetFrameIndex()]->SetConstantBufferData(&mCurrentAccumulationBuffer, sizeof(SDSMAccumulationBuffer));

    uint32 shadowNumSRVDescs = 1;
    Direct3DHeapManager *heapManager = direct3DManager->GetContextManager()->GetHeapManager();
    RenderPassDescriptorHeap *shadowSRVDescHeap = heapManager->GetRenderPassDescriptorHeapFor(RenderPassDescriptorHeapType_ShadowCompute, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, direct3DManager->GetFrameIndex(), shadowNumSRVDescs);

    GraphicsContext *graphicsContext = direct3DManager->GetContextManager()->GetGraphicsContext();

    graphicsContext->InsertPixBeginEvent(0xFF00FF00, "Shadow Compute");

    graphicsContext->SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, shadowSRVDescHeap->GetHeap());

    ////// Clear partitions
    graphicsContext->TransitionResource(mShadowPartitionBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, false);
    graphicsContext->TransitionResource(mShadowPartitionBoundsBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, true);

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

    graphicsContext->SetPipelineState(mClearShadowPartitionsShader);
    graphicsContext->SetRootSignature(NULL, mClearShadowPartitionsShader->GetRootSignature());
    graphicsContext->SetComputeDescriptorTable(0, shadowPartitionUAVHandle.GetGPUHandle());

    graphicsContext->Dispatch(1, 1, 1);

    ////// Clear Partition Bounds
    DescriptorHeapHandle shadowPartitionBoundsUAVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionBoundsUAVHandle.GetCPUHandle(), mShadowPartitionBoundsBuffer->GetUnorderedAccessViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    graphicsContext->SetPipelineState(mClearShadowPartitionBoundsShader);
    graphicsContext->SetRootSignature(NULL, mClearShadowPartitionBoundsShader->GetRootSignature());
    graphicsContext->SetComputeDescriptorTable(0, shadowPartitionBoundsUAVHandle.GetGPUHandle());

    graphicsContext->Dispatch(1, 1, 1);

    graphicsContext->TransitionResource(mShadowPartitionBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, false);          //force UAV barrier 
    graphicsContext->TransitionResource(mShadowPartitionBoundsBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, true);

    /////// Calculate depth buffer bounds

    shadowPartitionUAVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionUAVHandle.GetCPUHandle(), mShadowPartitionBuffer->GetUnorderedAccessViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

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

    DescriptorHeapHandle shadowPartitionCBVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionCBVHandle.GetCPUHandle(), mSDSMBuffers[direct3DManager->GetFrameIndex()]->GetConstantBufferViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    graphicsContext->SetPipelineState(mCalculateDepthBufferBoundsShader);
    graphicsContext->SetRootSignature(NULL, mCalculateDepthBufferBoundsShader->GetRootSignature());
    graphicsContext->SetComputeDescriptorTable(0, shadowPartitionUAVHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(1, shadowPartitionSRVHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(2, shadowPartitionCBVHandle.GetGPUHandle());

    graphicsContext->Dispatch2D((uint32)direct3DManager->GetScreenSize().X, (uint32)direct3DManager->GetScreenSize().Y, mCurrentSDSMBuffer.reduceTileDim, mCurrentSDSMBuffer.reduceTileDim);

    ////// Get Partitions From Depth Bounds
    graphicsContext->TransitionResource(mShadowPartitionBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, true);

    shadowPartitionUAVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionUAVHandle.GetCPUHandle(), mShadowPartitionBuffer->GetUnorderedAccessViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    shadowPartitionCBVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionCBVHandle.GetCPUHandle(), mSDSMBuffers[direct3DManager->GetFrameIndex()]->GetConstantBufferViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    graphicsContext->SetPipelineState(mCalculateLogPartitionsFromDepthBoundsShader);
    graphicsContext->SetRootSignature(NULL, mCalculateLogPartitionsFromDepthBoundsShader->GetRootSignature());
    graphicsContext->SetComputeDescriptorTable(0, shadowPartitionUAVHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(1, shadowPartitionCBVHandle.GetGPUHandle());

    graphicsContext->Dispatch(1, 1, 1);

    ////// Get Partition Bounds Range From Partitions
    graphicsContext->TransitionResource(mShadowPartitionBuffer, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, true);

    shadowPartitionBoundsUAVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionBoundsUAVHandle.GetCPUHandle(), mShadowPartitionBoundsBuffer->GetUnorderedAccessViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    shadowPartitionSRVHandle = shadowSRVDescHeap->GetHeapHandleBlock(2);
    D3D12_CPU_DESCRIPTOR_HANDLE currentTextureHandle = shadowPartitionSRVHandle.GetCPUHandle();

    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, currentTextureHandle, depthStencil->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
    currentTextureHandle.ptr += shadowSRVDescHeap->GetDescriptorSize();
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, currentTextureHandle, mShadowPartitionBuffer->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    shadowPartitionCBVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionCBVHandle.GetCPUHandle(), mSDSMBuffers[direct3DManager->GetFrameIndex()]->GetConstantBufferViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    graphicsContext->SetPipelineState(mCalculatePartitionBoundsShader);
    graphicsContext->SetRootSignature(NULL, mCalculatePartitionBoundsShader->GetRootSignature());
    graphicsContext->SetComputeDescriptorTable(0, shadowPartitionBoundsUAVHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(1, shadowPartitionSRVHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(2, shadowPartitionCBVHandle.GetGPUHandle());

    graphicsContext->Dispatch2D((uint32)direct3DManager->GetScreenSize().X, (uint32)direct3DManager->GetScreenSize().Y, mCurrentSDSMBuffer.reduceTileDim, mCurrentSDSMBuffer.reduceTileDim);

    ////// Finalize Partition Bounds
    graphicsContext->TransitionResource(mShadowPartitionBuffer, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, false);
    graphicsContext->TransitionResource(mShadowPartitionBoundsBuffer, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, true);

    shadowPartitionUAVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionUAVHandle.GetCPUHandle(), mShadowPartitionBuffer->GetUnorderedAccessViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    shadowPartitionSRVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionSRVHandle.GetCPUHandle(), mShadowPartitionBoundsBuffer->GetShaderResourceViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    shadowPartitionCBVHandle = shadowSRVDescHeap->GetHeapHandleBlock(1);
    direct3DManager->GetDevice()->CopyDescriptorsSimple(1, shadowPartitionCBVHandle.GetCPUHandle(), mSDSMBuffers[direct3DManager->GetFrameIndex()]->GetConstantBufferViewHandle().GetCPUHandle(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

    graphicsContext->SetPipelineState(mFinalizePartitionsShader);
    graphicsContext->SetRootSignature(NULL, mFinalizePartitionsShader->GetRootSignature());
    graphicsContext->SetComputeDescriptorTable(0, shadowPartitionUAVHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(1, shadowPartitionSRVHandle.GetGPUHandle());
    graphicsContext->SetComputeDescriptorTable(2, shadowPartitionCBVHandle.GetGPUHandle());

    graphicsContext->Dispatch(1, 1, 1);

    graphicsContext->InsertPixEndEvent();
}