private void BuildResources() { // Note, compressed formats cannot be used for UAV. We get error like: // ERROR: ID3D11Device::CreateTexture2D: The format (0x4d, BC3_UNORM) // cannot be bound as an UnorderedAccessView, or cast to a format that // could be bound as an UnorderedAccessView. Therefore this format // does not support D3D11_BIND_UNORDERED_ACCESS. var texDesc = new ResourceDescription { Dimension = ResourceDimension.Texture2D, Alignment = 0, Width = _width, Height = _height, DepthOrArraySize = 1, MipLevels = 1, Format = _format, SampleDescription = new SampleDescription(1, 0), Layout = TextureLayout.Unknown, Flags = ResourceFlags.AllowUnorderedAccess }; _blurMap0 = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.GenericRead); _blurMap1 = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.UnorderedAccess); }
private void BuildResources() { var texDesc = new ResourceDescription { Dimension = ResourceDimension.Texture2D, Alignment = 0, Width = _renderTargetWidth, Height = _renderTargetHeight, DepthOrArraySize = 1, MipLevels = 1, Format = NormalMapFormat, SampleDescription = new SampleDescription(1, 0), Layout = TextureLayout.Unknown, Flags = ResourceFlags.AllowRenderTarget }; var optClear = new ClearValue { Format = NormalMapFormat, Color = Vector4.UnitZ }; _normalMap = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.GenericRead, optClear); // Ambient occlusion maps are at half resolution. texDesc.Width = SsaoMapWidth; texDesc.Height = SsaoMapHeight; texDesc.Format = AmbientMapFormat; optClear = new ClearValue { Format = AmbientMapFormat, Color = Vector4.One }; _ambientMap0 = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.GenericRead, optClear); _ambientMap1 = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.GenericRead, optClear); }
private void BuildResource() { var texDesc = new ResourceDescription { Dimension = ResourceDimension.Texture2D, Alignment = 0, Width = Width, Height = Height, DepthOrArraySize = 1, MipLevels = 1, Format = Format, SampleDescription = new SampleDescription(1, 0), Layout = TextureLayout.Unknown, Flags = ResourceFlags.AllowDepthStencil }; var optClear = new ClearValue { Format = Format.D24_UNorm_S8_UInt, DepthStencil = new DepthStencilValue { Depth = 1.0f, Stencil = 0 } }; Resource = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.GenericRead, optClear); }
/// <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); }
public static Resource New(Device device, long clientWidth, int clientHeight, int msaaCount, int msaaQuality, Format depthStencilFormat) { var depthStencilDesc = BuildResourceDescription(clientWidth, clientHeight, msaaCount, msaaQuality); var optClear = BuildClearValue(depthStencilFormat); return(device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, depthStencilDesc, ResourceStates.Common, optClear)); }
public static Resource CreateDefaultBuffer <T>( Device device, GraphicsCommandList cmdList, T[] initData, long byteSize, out Resource uploadBuffer) where T : struct { // Create the actual default buffer resource. Resource defaultBuffer = device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, ResourceDescription.Buffer(byteSize), ResourceStates.Common); // In order to copy CPU memory data into our default buffer, we need to create // an intermediate upload heap. uploadBuffer = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(byteSize), ResourceStates.GenericRead); // Copy the data to the upload buffer. IntPtr ptr = uploadBuffer.Map(0); Utilities.Write(ptr, initData, 0, initData.Length); uploadBuffer.Unmap(0); // Schedule to copy the data to the default buffer resource. cmdList.ResourceBarrierTransition(defaultBuffer, ResourceStates.Common, ResourceStates.CopyDestination); cmdList.CopyResource(defaultBuffer, uploadBuffer); cmdList.ResourceBarrierTransition(defaultBuffer, ResourceStates.CopyDestination, ResourceStates.GenericRead); // Note: uploadBuffer has to be kept alive after the above function calls because // the command list has not been executed yet that performs the actual copy. // The caller can Release the uploadBuffer after it knows the copy has been executed. return(defaultBuffer); }
/// <summary> /// Create texture from bmp /// </summary> /// <param name="device">Device</param> /// <param name="filename">Filename</param> /// <returns></returns> public static Resource CreateTextureFromBitmap(Device device, string filename) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(filename); int width = bitmap.Width; int height = bitmap.Height; // Describe and create a Texture2D. ResourceDescription textureDesc = new ResourceDescription() { MipLevels = 1, Format = Format.B8G8R8A8_UNorm, Width = width, Height = height, Flags = ResourceFlags.None, DepthOrArraySize = 1, SampleDescription = new SampleDescription(1, 0), Dimension = ResourceDimension.Texture2D, }; var buffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, textureDesc, ResourceStates.GenericRead); System.Drawing.Imaging.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); buffer.WriteToSubresource(0, new ResourceRegion() { Back = 1, Bottom = height, Right = width }, data.Scan0, 4 * width, 4 * width * height); int bufferSize = data.Height * data.Stride; bitmap.UnlockBits(data); return(buffer); }
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); }
public void BuildResources(GraphicsCommandList cmdList) { // All the textures for the wave simulation will be bound as a shader resource and // unordered access view at some point since we ping-pong the buffers. var texDesc = new ResourceDescription { Dimension = ResourceDimension.Texture2D, Alignment = 0, Width = ColumnCount, Height = RowCount, DepthOrArraySize = 1, MipLevels = 1, Format = Format.R32_Float, SampleDescription = new SampleDescription(1, 0), Layout = TextureLayout.Unknown, Flags = ResourceFlags.AllowUnorderedAccess }; _prevSol = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.Common); _currSol = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.Common); _nextSol = _device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, texDesc, ResourceStates.Common); // // In order to copy CPU memory data into our default buffer, we need to create // an intermediate upload heap. // int num2DSubresources = texDesc.DepthOrArraySize * texDesc.MipLevels; _device.GetCopyableFootprints(ref texDesc, 0, num2DSubresources, 0, null, null, null, out long uploadBufferSize); _prevUploadBuffer = _device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(uploadBufferSize), ResourceStates.GenericRead); _currUploadBuffer = _device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, ResourceDescription.Buffer(uploadBufferSize), ResourceStates.GenericRead); // // Schedule to copy the data to the default resource, and change states. // Note that mCurrSol is put in the GENERIC_READ state so it can be // read by a shader. // cmdList.ResourceBarrierTransition(_prevSol, ResourceStates.Common, ResourceStates.CopyDestination); cmdList.CopyResource(_prevSol, _prevUploadBuffer); cmdList.ResourceBarrierTransition(_prevSol, ResourceStates.CopyDestination, ResourceStates.UnorderedAccess); cmdList.ResourceBarrierTransition(_currSol, ResourceStates.Common, ResourceStates.CopyDestination); cmdList.CopyResource(_currSol, _currUploadBuffer); cmdList.ResourceBarrierTransition(_currSol, ResourceStates.CopyDestination, ResourceStates.GenericRead); cmdList.ResourceBarrierTransition(_nextSol, ResourceStates.Common, ResourceStates.UnorderedAccess); }
private void LoadPipeline(RenderForm form) { int width = form.ClientSize.Width; int height = form.ClientSize.Height; viewport.Width = width; viewport.Height = height; viewport.MaxDepth = 1.0f; scissorRect.Right = width; scissorRect.Bottom = height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = form.Handle, //Flags = SwapChainFlags.None, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface<SwapChain3>(); tempSwapChain.Dispose(); frameIndex = swapChain.CurrentBackBufferIndex; } // Create descriptor heaps. // Describe and create a render target view (RTV) descriptor heap. DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); // Create frame resources. CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; } //create depth buffer; DescriptorHeapDescription dsvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.DepthStencilView }; depthStencilViewHeap = device.CreateDescriptorHeap(dsvHeapDesc); CpuDescriptorHandle dsvHandle = depthStencilViewHeap.CPUDescriptorHandleForHeapStart; ClearValue depthOptimizedClearValue = new ClearValue() { Format = Format.D32_Float, DepthStencil = new DepthStencilValue() { Depth = 1.0F, Stencil = 0 }, }; depthTarget = device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 0, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil), ResourceStates.DepthWrite, depthOptimizedClearValue); var depthView = new DepthStencilViewDescription() { Format = Format.D32_Float, Dimension = DepthStencilViewDimension.Texture2D, Flags = DepthStencilViewFlags.None, }; //bind depth buffer device.CreateDepthStencilView(depthTarget, null, dsvHandle); commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); bundleAllocator = device.CreateCommandAllocator(CommandListType.Bundle); }
static Resource CreateTextureFromDDS(Device d3dDevice, DDS_HEADER header, DDS_HEADER_DXT10?header10, byte[] bitData, int offset, int maxsize, out bool isCubeMap) { int width = header.width; int height = header.height; int depth = header.depth; ResourceDimension resDim = ResourceDimension.Unknown; int arraySize = 1; Format format = Format.Unknown; isCubeMap = false; int mipCount = header.mipMapCount; if (0 == mipCount) { mipCount = 1; } if (((header.ddspf.flags & DDS_FOURCC) > 0) && (MAKEFOURCC('D', 'X', '1', '0') == header.ddspf.fourCC)) { DDS_HEADER_DXT10 d3d10ext = header10.Value; arraySize = d3d10ext.arraySize; if (arraySize == 0) { throw new Exception(); } if (BitsPerPixel(d3d10ext.dxgiFormat) == 0) { throw new Exception(); } format = d3d10ext.dxgiFormat; switch ((ResourceDimension)d3d10ext.resourceDimension) { case ResourceDimension.Texture1D: // D3DX writes 1D textures with a fixed Height of 1 if ((header.flags & DDS_HEIGHT) > 0 && height != 1) { throw new Exception(); } height = depth = 1; break; case ResourceDimension.Texture2D: //D3D11_RESOURCE_MISC_TEXTURECUBE if ((d3d10ext.miscFlag & 0x4) > 0) { arraySize *= 6; isCubeMap = true; } depth = 1; break; case ResourceDimension.Texture3D: if (!((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0)) { throw new Exception(); } if (arraySize > 1) { throw new Exception(); } break; default: throw new Exception(); } resDim = (ResourceDimension)d3d10ext.resourceDimension; } else { format = GetDXGIFormat(header.ddspf); if (format == Format.Unknown) { throw new Exception(); } if ((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0) { resDim = ResourceDimension.Texture3D; } else { if ((header.caps2 & DDS_CUBEMAP) > 0) { // We require all six faces to be defined if ((header.caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES) { throw new Exception(); } arraySize = 6; isCubeMap = true; } depth = 1; resDim = ResourceDimension.Texture2D; } } var resource = d3dDevice.CreateCommittedResource(new HeapProperties(CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None, new ResourceDescription() { //Alignment = -1, Dimension = resDim, DepthOrArraySize = (short)arraySize, Flags = ResourceFlags.None, Format = format, Height = height, Layout = TextureLayout.Unknown, MipLevels = (short)mipCount, SampleDescription = new SampleDescription(1, 0), Width = width, }, ResourceStates.GenericRead); FillInitData(resource, width, height, depth, mipCount, arraySize, format, 0, 0, bitData, offset); return(resource); }
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; }