private void BuildDescriptors() { // Create SRV to resource so we can sample the shadow map in a shader program. var srvDesc = new ShaderResourceViewDescription { Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping, Format = Format.R24_UNorm_X8_Typeless, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = new ShaderResourceViewDescription.Texture2DResource { MostDetailedMip = 0, MipLevels = 1, ResourceMinLODClamp = 0.0f, PlaneSlice = 0 } }; _device.CreateShaderResourceView(Resource, srvDesc, _cpuSrv); // Create DSV to resource so we can render to the shadow map. var dsvDesc = new DepthStencilViewDescription { Flags = DepthStencilViewFlags.None, Dimension = DepthStencilViewDimension.Texture2D, Format = Format.D24_UNorm_S8_UInt, Texture2D = new DepthStencilViewDescription.Texture2DResource { MipSlice = 0 } }; _device.CreateDepthStencilView(Resource, dsvDesc, _cpuDsv); }
/// <summary> /// Render scene /// </summary> public void Render() { // record all the commands we need to render the scene into the command list PopulateCommandLists(); // execute the command list commandQueue.ExecuteCommandList(commandList); // swap the back and front buffers swapChain.Present(1, 0); // wait and reset EVERYTHING WaitForPrevFrame(); RemoveAndDispose(ref renderTarget); if (width != newWidth || height != newHeight) { width = newWidth; height = newHeight; swapChain.ResizeBuffers(SwapBufferCount, width, height, Format.Unknown, SwapChainFlags.None); #if USE_DEPTH RemoveAndDispose(ref depthBuffer); depthBuffer = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 1, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil), ResourceStates.Common, new ClearValue { Format = Format.D32_Float, DepthStencil = new DepthStencilValue { Depth = 1, Stencil = 0, } })); device.CreateDepthStencilView(depthBuffer, null, descriptorHeapDS.CPUDescriptorHandleForHeapStart); #endif // Create the viewport viewPort = new ViewportF(0, 0, width, height); // Create the scissor scissorRectangle = new Rectangle(0, 0, width, height); } renderTarget = Collect(swapChain.GetBackBuffer <Resource>(swapChain.CurrentBackBufferIndex)); device.CreateRenderTargetView(renderTarget, null, descriptorHeapRT.CPUDescriptorHandleForHeapStart); }
protected virtual void OnResize() { Debug.Assert(Device != null); Debug.Assert(SwapChain != null); Debug.Assert(DirectCmdListAlloc != null); // Flush before changing any resources. FlushCommandQueue(); CommandList.Reset(DirectCmdListAlloc, null); // Release the previous resources we will be recreating. foreach (Resource buffer in _swapChainBuffers) { buffer?.Dispose(); } DepthStencilBuffer?.Dispose(); // Resize the swap chain. SwapChain.ResizeBuffers( SwapChainBufferCount, ClientWidth, ClientHeight, BackBufferFormat, SwapChainFlags.AllowModeSwitch); CpuDescriptorHandle rtvHeapHandle = RtvHeap.CPUDescriptorHandleForHeapStart; for (int i = 0; i < SwapChainBufferCount; i++) { Resource backBuffer = SwapChain.GetBackBuffer <Resource>(i); _swapChainBuffers[i] = backBuffer; Device.CreateRenderTargetView(backBuffer, null, rtvHeapHandle); rtvHeapHandle += RtvDescriptorSize; } // Create the depth/stencil buffer and view. var depthStencilDesc = new ResourceDescription { Dimension = ResourceDimension.Texture2D, Alignment = 0, Width = ClientWidth, Height = ClientHeight, DepthOrArraySize = 1, MipLevels = 1, Format = Format.R24G8_Typeless, SampleDescription = new SampleDescription { Count = MsaaCount, Quality = MsaaQuality }, Layout = TextureLayout.Unknown, Flags = ResourceFlags.AllowDepthStencil }; var optClear = new ClearValue { Format = DepthStencilFormat, DepthStencil = new DepthStencilValue { Depth = 1.0f, Stencil = 0 } }; DepthStencilBuffer = Device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, depthStencilDesc, ResourceStates.Common, optClear); var depthStencilViewDesc = new DepthStencilViewDescription { Dimension = DepthStencilViewDimension.Texture2D, Format = DepthStencilFormat }; // Create descriptor to mip level 0 of entire resource using a depth stencil format. CpuDescriptorHandle dsvHeapHandle = DsvHeap.CPUDescriptorHandleForHeapStart; Device.CreateDepthStencilView(DepthStencilBuffer, depthStencilViewDesc, dsvHeapHandle); // Transition the resource from its initial state to be used as a depth buffer. CommandList.ResourceBarrierTransition(DepthStencilBuffer, ResourceStates.Common, ResourceStates.DepthWrite); // Execute the resize commands. CommandList.Close(); CommandQueue.ExecuteCommandList(CommandList); // Wait until resize is complete. FlushCommandQueue(); Viewport = new ViewportF(0, 0, ClientWidth, ClientHeight, 0.0f, 1.0f); ScissorRectangle = new RectangleF(0, 0, ClientWidth, ClientHeight); }
void CreateBuffers() { SharpDX.Direct3D12.CpuDescriptorHandle rtv = _heapRTV.CPUDescriptorHandleForHeapStart; SharpDX.Direct3D12.HeapFlags flags = SharpDX.Direct3D12.HeapFlags.None; _frameIndex = _swapChain.CurrentBackBufferIndex; SharpDX.DXGI.SwapChainDescription1 swapChainDesc = _swapChain.Description1; // TODO: Check ID3D12Device8 to enable D3D12_HEAP_FLAG_CREATE_NOT_ZEROED SharpDX.Direct3D12.ResourceDescription desc = new SharpDX.Direct3D12.ResourceDescription(); desc.Dimension = SharpDX.Direct3D12.ResourceDimension.Texture2D; desc.Alignment = 0; desc.Width = swapChainDesc.Width; desc.Height = swapChainDesc.Height; desc.DepthOrArraySize = 1; desc.MipLevels = 1; desc.SampleDescription = _sampleDesc; desc.Layout = SharpDX.Direct3D12.TextureLayout.Unknown; SharpDX.Direct3D12.HeapProperties heap = new SharpDX.Direct3D12.HeapProperties(); heap.Type = SharpDX.Direct3D12.HeapType.Default; heap.CPUPageProperty = SharpDX.Direct3D12.CpuPageProperty.Unknown; heap.MemoryPoolPreference = SharpDX.Direct3D12.MemoryPool.Unknown; heap.CreationNodeMask = 0; heap.VisibleNodeMask = 0; SharpDX.Direct3D12.ClearValue clearValue = new SharpDX.Direct3D12.ClearValue(); clearValue.Format = _format; clearValue.Color = new SharpDX.Mathematics.Interop.RawVector4(0.0f, 0.0f, 0.0f, 0.0f); for (int i = 0; i < FrameCount; i++) { desc.Format = _format; desc.Flags = SharpDX.Direct3D12.ResourceFlags.AllowRenderTarget; _backBuffers[i] = _swapChain.GetBackBuffer <SharpDX.Direct3D12.Resource>(i); if (_sampleDesc.Count != 1) { _backBuffersAA[i] = _dev.CreateCommittedResource(heap, flags, desc, SharpDX.Direct3D12.ResourceStates.ResolveSource, clearValue); _dev.CreateRenderTargetView(_backBuffersAA[i], null, rtv); } else { SharpDX.Direct3D12.RenderTargetViewDescription view = new SharpDX.Direct3D12.RenderTargetViewDescription(); view.Format = _format; view.Dimension = SharpDX.Direct3D12.RenderTargetViewDimension.Texture2D; view.Texture2D.MipSlice = 0; view.Texture2D.PlaneSlice = 0; _dev.CreateRenderTargetView(_backBuffers[i], view, rtv); } rtv.Ptr += _sizeRTV; } // Stencil buffer clearValue.Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt; clearValue.DepthStencil.Depth = 0.0f; clearValue.DepthStencil.Stencil = 0; desc.Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt; desc.Flags = SharpDX.Direct3D12.ResourceFlags.AllowDepthStencil; _stencilBuffer = _dev.CreateCommittedResource(heap, flags, desc, SharpDX.Direct3D12.ResourceStates.DepthWrite, clearValue); SharpDX.Direct3D12.CpuDescriptorHandle dsv = _heapDSV.CPUDescriptorHandleForHeapStart; _dev.CreateDepthStencilView(_stencilBuffer, null, dsv); // Viewport _viewport[0].TopLeftX = 0.0f; _viewport[0].TopLeftY = 0.0f; _viewport[0].Width = desc.Width; _viewport[0].Height = desc.Height; _viewport[0].MinDepth = 0.0f; _viewport[0].MaxDepth = 1.0f; }