internal Texture InitializeFrom(ID3D12Resource resource, TextureDescription description) { NativeResource = resource; Description = description; if (description.Flags.HasFlag(TextureFlags.DepthStencil)) { NativeDepthStencilView = CreateDepthStencilView(); } if (description.Flags.HasFlag(TextureFlags.RenderTarget)) { NativeRenderTargetView = CreateRenderTargetView(); } if (description.Flags.HasFlag(TextureFlags.ShaderResource)) { NativeShaderResourceView = CreateShaderResourceView(); } if (description.Flags.HasFlag(TextureFlags.UnorderedAccess)) { NativeUnorderedAccessView = CreateUnorderedAccessView(); } return(this); }
internal CpuDescriptorHandle CreateRenderTargetView() { CpuDescriptorHandle cpuHandle = GraphicsDevice.RenderTargetViewAllocator.Allocate(1); RenderTargetViewDescription?rtvDescription = null; if (Description.Dimension == TextureDimension.Texture2D) { RenderTargetViewDescription rtvTexture2DDescription = new RenderTargetViewDescription { Format = (Format)Description.Format, ViewDimension = Description.DepthOrArraySize > 1 ? RenderTargetViewDimension.Texture2DArray : RenderTargetViewDimension.Texture2D, }; if (Description.DepthOrArraySize > 1) { rtvTexture2DDescription.Texture2DArray.ArraySize = Description.DepthOrArraySize; } rtvDescription = rtvTexture2DDescription; } GraphicsDevice.NativeDevice.CreateRenderTargetView(NativeResource, rtvDescription, cpuHandle); return(cpuHandle); }
public void BuildDescriptors( Resource depthStencilBuffer, CpuDescriptorHandle cpuSrv, GpuDescriptorHandle gpuSrv, CpuDescriptorHandle cpuRtv, int cbvSrvUavDescriptorSize, int rtvDescriptorSize) { // Save references to the descriptors. The Ssao reserves heap space // for 5 contiguous Srvs. _ambientMap0CpuSrv = cpuSrv; _ambientMap1CpuSrv = cpuSrv + cbvSrvUavDescriptorSize; _normalMapCpuSrv = cpuSrv + 2 * cbvSrvUavDescriptorSize; _depthMapCpuSrv = cpuSrv + 3 * cbvSrvUavDescriptorSize; _randomVectorMapCpuSrv = cpuSrv + 4 * cbvSrvUavDescriptorSize; _ambientMap0GpuSrv = gpuSrv; _ambientMap1GpuSrv = gpuSrv + cbvSrvUavDescriptorSize; _normalMapGpuSrv = gpuSrv + 2 * cbvSrvUavDescriptorSize; // We skip a depth map gpu srv. _randomVectorMapGpuSrv = gpuSrv + 4 * cbvSrvUavDescriptorSize; _normalMapCpuRtv = cpuRtv; _ambientMap0CpuRtv = cpuRtv + rtvDescriptorSize; _ambientMap1CpuRtv = cpuRtv + 2 * rtvDescriptorSize; // Create the descriptors RebuildDescriptors(depthStencilBuffer); }
public CpuDescriptorHandle Allocate(int count) { Requires.IsNotDisposed(this); if (heap == null || remaining < count) { if (heap != null) { heaps.Add(heap); } heap = Device.NativeDevice.CreateDescriptorHeap(new DescriptorHeapDescription { Flags = DescriptorHeapFlags.None, Type = HeapType, DescriptorCount = DescriptorsPerHeap, NodeMask = 1, }); remaining = DescriptorsPerHeap; handle = heap.CPUDescriptorHandleForHeapStart; } var result = handle; handle.Ptr += Stride * count; remaining -= count; return(result); }
internal CpuDescriptorHandle GetUnorderedAccessView(PixelFormat viewFormat) { var uav = new CpuDescriptorHandle(); if ((ViewFlags & BufferFlags.UnorderedAccess) != 0) { var description = new UnorderedAccessViewDescription { Format = (SharpDX.DXGI.Format)viewFormat, Dimension = SharpDX.Direct3D12.UnorderedAccessViewDimension.Buffer, Buffer = { ElementCount = this.ElementCount, FirstElement = 0, Flags = BufferUnorderedAccessViewFlags.None, StructureByteStride = StructureByteStride, CounterOffsetInBytes = 0, } }; if ((ViewFlags & BufferFlags.RawBuffer) == BufferFlags.RawBuffer) { description.Buffer.Flags |= BufferUnorderedAccessViewFlags.Raw; description.Format = Format.R32_Typeless; } uav = GraphicsDevice.UnorderedAccessViewAllocator.Allocate(1); // TODO: manage counter value here if buffer has 'Counter' or 'Append' flag // if (Flags == BufferFlags.StructuredAppendBuffer || Flags == BufferFlags.StructuredCounterBuffer)) NativeDevice.CreateUnorderedAccessView(NativeResource, null, description, uav); } return(uav); }
private CpuDescriptorHandle GetRenderTargetView() { RenderTargetViewDescription RTVDescription = new RenderTargetViewDescription() { Buffer = new BufferRenderTargetView() { FirstElement = 0, NumElements = 0 }, //Texture1D, Format = Vortice.DXGI.Format.R8G8B8A8_UNorm, //Texture1DArray Texture2D = new Texture2DRenderTargetView() { MipSlice = 1, PlaneSlice = 0 }, //Texture2DArray Texture2DMS = new Texture2DMultisampledRenderTargetView() { // UnusedFieldNothingToDefine = 0x001, }, //Texture2DMSArray //Texture3D ViewDimension = RenderTargetViewDimension.Texture2D }; CpuDescriptorHandle descriptorHandle = GraphicsDevice.RenderTargetViewAllocator.Allocate(1); GraphicsDevice.NativeDevice.CreateRenderTargetView(Resource, /*null*/ RTVDescription, descriptorHandle); return(descriptorHandle); }
public DescriptorHandle Allocate(int count) { if (_currentHeap == null || _remainingFreeHandles < count) { _currentHeap = Device.RequestNewHeap(Type, DescriptorsPerHeap); _currentCpuHandle = _currentHeap.GetCPUDescriptorHandleForHeapStart(); if (IsShaderVisible) { _currentGpuHandle = _currentHeap.GetGPUDescriptorHandleForHeapStart(); } _remainingFreeHandles = DescriptorsPerHeap; if (_descriptorSize == 0) { _descriptorSize = Device.D3D12Device.GetDescriptorHandleIncrementSize(Type); } } var cpuHandle = _currentCpuHandle; _currentCpuHandle.Ptr += count * _descriptorSize; _remainingFreeHandles -= count; if (IsShaderVisible) { var gpuHandle = _currentGpuHandle; _currentGpuHandle.Ptr += (long)(count * _descriptorSize); return(new DescriptorHandle(_currentHeap, _descriptorSize, cpuHandle, gpuHandle)); } return(new DescriptorHandle(_currentHeap, _descriptorSize, cpuHandle)); }
private void PopulateCommandList() { // Command list allocators can only be reset when the associated // command lists have finished execution on the GPU; apps should use // fences to determine GPU execution progress. commandAllocator.Reset(); // However, when ExecuteCommandList() is called on a particular command // list, that command list can then be reset at any time and must be before // re-recording. commandList.Reset(commandAllocator, null); // Indicate that the back buffer will be used as a render target. commandList.ResourceBarrierTransition(renderTargets[frameIndex], ResourceStates.Present, ResourceStates.RenderTarget); CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; rtvHandle += frameIndex * rtvDescriptorSize; // Record commands. commandList.ClearRenderTargetView(rtvHandle, new Color4(0, 0.2F, 0.4f, 1), 0, null); // Indicate that the back buffer will now be used to present. commandList.ResourceBarrierTransition(renderTargets[frameIndex], ResourceStates.RenderTarget, ResourceStates.Present); commandList.Close(); }
public DescriptorHandle(ID3D12DescriptorHeap heap, int sizeIncrement, CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) { Heap = heap; SizeIncrement = sizeIncrement; CpuHandle = cpuHandle; GpuHandle = gpuHandle; }
private DescriptorSet(GraphicsDevice graphicsDevice, DescriptorPool pool, DescriptorSetLayout desc) { if (pool.SrvOffset + desc.SrvCount > pool.SrvCount || pool.SamplerOffset + desc.SamplerCount > pool.SamplerCount) { // Eearly exit if OOM, IsValid should return false (TODO: different mechanism?) Device = null; BindingOffsets = null; Description = null; SrvStart = new CpuDescriptorHandle(); SamplerStart = new CpuDescriptorHandle(); return; } Device = graphicsDevice; BindingOffsets = desc.BindingOffsets; Description = desc; // Store start CpuDescriptorHandle SrvStart = desc.SrvCount > 0 ? (pool.SrvStart + graphicsDevice.SrvHandleIncrementSize * pool.SrvOffset) : new CpuDescriptorHandle(); SamplerStart = desc.SamplerCount > 0 ? (pool.SamplerStart + graphicsDevice.SamplerHandleIncrementSize * pool.SamplerOffset) : new CpuDescriptorHandle(); // Allocation is done, bump offsets // TODO D3D12 thread safety? pool.SrvOffset += desc.SrvCount; pool.SamplerOffset += desc.SamplerCount; }
public CpuDescriptorHandle GetRenderTargetScreen() { CpuDescriptorHandle handle = rtvHeap.GetCPUDescriptorHandleForHeapStart(); handle.Ptr += swapChain.GetCurrentBackBufferIndex() * rtvHeapIncrementSize; return(handle); }
public void SetRenderTargets(Texture?depthStencilView, params GraphicsResource[] renderTargetViews) { DepthStencilBuffer = depthStencilView; if (renderTargetViews.Length > MaxRenderTargetCount) { throw new ArgumentOutOfRangeException(nameof(renderTargetViews), renderTargetViews.Length, $"The maximum number of render targets is {MaxRenderTargetCount}."); } if (RenderTargets.Length != renderTargetViews.Length) { RenderTargets = new GraphicsResource[renderTargetViews.Length]; } renderTargetViews.CopyTo(RenderTargets, 0); CpuDescriptorHandle[] renderTargetDescriptors = new CpuDescriptorHandle[renderTargetViews.Length]; for (int i = 0; i < renderTargetViews.Length; i++) { renderTargetDescriptors[i] = renderTargetViews[i].NativeRenderTargetView; } currentCommandList.NativeCommandList.OMSetRenderTargets(renderTargetDescriptors, depthStencilView?.NativeDepthStencilView); }
/// <summary> /// Constructs a <see cref="DescriptorSet"/>. /// </summary> /// <param name="device"></param> /// <param name="layout"></param> public DescriptorSet(GraphicsDevice device, DescriptorLayout layout) : base(device) { Layout = layout; ShaderResourceViewHandle = device.ShaderResourceViewAllocator.Allocate(layout.ShaderResourceViewCount); SamplerHandle = device.SamplerAllocator.Allocate(layout.SamplerCount); }
/// <summary> /// Creates a depth texture. /// </summary> /// <param name="width"></param> /// <param name="height"></param> public void CreateDepth(int width, int height) { Width = width; Height = height; ClearValue depthOptimizedClearValue = new ClearValue() { Format = Format.D32_Float, DepthStencil = new DepthStencilValue() { Depth = 1.0F, Stencil = 0 }, }; Resource = Device.NativeDevice.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, ResourceDescription.Texture2D( Format.D32_Float, Width, Height, mipLevels: MipLevels, flags: ResourceFlags.AllowDepthStencil), ResourceStates.DepthWrite, depthOptimizedClearValue); NativeDepthStencilView = Device.DepthStencilViewAllocator.Allocate(1); Device.NativeDevice.CreateDepthStencilView(Resource, null, NativeDepthStencilView); }
public CpuDescriptorHandle GetAvailableAllocCpuAddress(Int32 allocCounts = 1) { CpuDescriptorHandle allocStartCpu = m_HeapStartCPU + Convert.ToInt32(m_Cursor * m_DescSize); m_Cursor += Convert.ToUInt32(allocCounts); // update cursor return(allocStartCpu); }
/// <summary> /// Gets a <see cref="ShaderResourceView"/> for a particular <see cref="PixelFormat"/>. /// </summary> /// <param name="viewFormat">The view format.</param> /// <returns>A <see cref="ShaderResourceView"/> for the particular view format.</returns> /// <remarks> /// The buffer must have been declared with <see cref="Graphics.BufferFlags.ShaderResource"/>. /// The ShaderResourceView instance is kept by this buffer and will be disposed when this buffer is disposed. /// </remarks> internal CpuDescriptorHandle GetShaderResourceView(PixelFormat viewFormat) { var srv = new CpuDescriptorHandle(); if ((ViewFlags & BufferFlags.ShaderResource) != 0) { var description = new ShaderResourceViewDescription { Shader4ComponentMapping = 0x00001688, Format = (SharpDX.DXGI.Format)viewFormat, Dimension = SharpDX.Direct3D12.ShaderResourceViewDimension.Buffer, Buffer = { ElementCount = this.ElementCount, FirstElement = 0, Flags = BufferShaderResourceViewFlags.None, StructureByteStride = StructureByteStride, } }; if (((ViewFlags & BufferFlags.RawBuffer) == BufferFlags.RawBuffer)) { description.Buffer.Flags |= BufferShaderResourceViewFlags.Raw; } srv = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); NativeDevice.CreateShaderResourceView(NativeResource, description, srv); } return(srv); }
public void SetDSVRTV(Texture2D dsv, Texture2D[] rtvs, bool clearDSV, bool clearRTV) { dsv?.StateChange(commandList, ResourceStates.DepthWrite); CpuDescriptorHandle[] rtvHandles = null; if (rtvs != null) { rtvHandles = new CpuDescriptorHandle[rtvs.Length]; for (int i = 0; i < rtvs.Length; i++) { Texture2D rtv = rtvs[i]; rtv.StateChange(commandList, ResourceStates.RenderTarget); rtvHandles[i] = rtv.renderTargetView.GetCPUDescriptorHandleForHeapStart(); } } if (clearDSV && dsv != null) { commandList.ClearDepthStencilView(dsv.depthStencilView.GetCPUDescriptorHandleForHeapStart(), ClearFlags.Depth | ClearFlags.Stencil, 1.0f, 0); } if (clearRTV && rtvs != null) { foreach (var rtv in rtvs) { commandList.ClearRenderTargetView(rtv.renderTargetView.GetCPUDescriptorHandleForHeapStart(), new Color4()); } } commandList.OMSetRenderTargets(rtvHandles, dsv.depthStencilView.GetCPUDescriptorHandleForHeapStart()); }
private void BuildDescriptorHeaps() { const int textureDescriptorCount = 3; const int blurDescriptorCount = 4; // // Create the descriptor heap. // var heapDesc = new DescriptorHeapDescription { DescriptorCount = textureDescriptorCount + blurDescriptorCount, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, Flags = DescriptorHeapFlags.ShaderVisible }; _cbvSrvUavDescriptorHeap = Device.CreateDescriptorHeap(heapDesc); _descriptorHeaps = new[] { _cbvSrvUavDescriptorHeap }; // // Fill out the heap with texture descriptors. // CpuDescriptorHandle hDescriptor = _cbvSrvUavDescriptorHeap.CPUDescriptorHandleForHeapStart; Resource[] tex2DList = { _textures["grassTex"].Resource, _textures["waterTex"].Resource, _textures["fenceTex"].Resource }; var srvDesc = new ShaderResourceViewDescription { Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = new ShaderResourceViewDescription.Texture2DResource { MostDetailedMip = 0, MipLevels = -1, } }; foreach (Resource tex2D in tex2DList) { srvDesc.Format = tex2D.Description.Format; Device.CreateShaderResourceView(tex2D, srvDesc, hDescriptor); // Next descriptor. hDescriptor += CbvSrvUavDescriptorSize; } // // Fill out the heap with the descriptors to the BlurFilter resources. // _blurFilter.BuildDescriptors( _cbvSrvUavDescriptorHeap.CPUDescriptorHandleForHeapStart + textureDescriptorCount * CbvSrvUavDescriptorSize, _cbvSrvUavDescriptorHeap.GPUDescriptorHandleForHeapStart + textureDescriptorCount * CbvSrvUavDescriptorSize, CbvSrvUavDescriptorSize); }
internal CpuDescriptorHandle CreateDepthStencilView() { CpuDescriptorHandle cpuHandle = GraphicsDevice.DepthStencilViewAllocator.Allocate(1); GraphicsDevice.NativeDevice.CreateDepthStencilView(NativeResource, null, cpuHandle); return(cpuHandle); }
internal CpuDescriptorHandle CreateUnorderedAccessView() { CpuDescriptorHandle cpuHandle = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, null, cpuHandle); return(cpuHandle); }
internal CpuDescriptorHandle CreateRenderTargetView() { CpuDescriptorHandle cpuHandle = GraphicsDevice.RenderTargetViewAllocator.Allocate(1); GraphicsDevice.NativeDevice.CreateRenderTargetView(NativeResource, null, cpuHandle); return(cpuHandle); }
internal void Initialize(Resource resource) { Resource = resource; NativeRenderTargetView = Device.RenderTargetViewAllocator.Allocate(1); Device.NativeDevice.CreateRenderTargetView(Resource, null, NativeRenderTargetView); // TODO: Recycle render target view }
private GpuDescriptorHandle CopyDescriptors(DescriptorAllocator descriptorAllocator, CpuDescriptorHandle baseDescriptor, int descriptorCount) { CpuDescriptorHandle destinationDescriptor = descriptorAllocator.Allocate(descriptorCount); GraphicsDevice.NativeDevice.CopyDescriptorsSimple(descriptorCount, destinationDescriptor, baseDescriptor, descriptorAllocator.DescriptorHeap.Description.Type); return(descriptorAllocator.GetGpuDescriptorHandle(destinationDescriptor)); }
private CpuDescriptorHandle CreateSampler() { CpuDescriptorHandle cpuHandle = GraphicsDevice.SamplerAllocator.Allocate(1); GraphicsDevice.NativeDevice.CreateSampler(Description, cpuHandle); return(cpuHandle); }
/// <summary> /// Initialize new instance of <see cref="RenderPassRenderTargetDescription"/> struct. /// </summary> /// <param name="cpuDescriptor">The CPU <see cref="CpuDescriptorHandle"/> handle corresponding to the render target view(s) (RTVs).</param> /// <param name="beginningAccess">The access to the RTV(s) requested at the transition into a render pass.</param> /// <param name="endingAccess">The access to the RTV(s) requested at the transition out of a render pass.</param> public RenderPassRenderTargetDescription( CpuDescriptorHandle cpuDescriptor, RenderPassBeginningAccess beginningAccess, RenderPassEndingAccess endingAccess) { CpuDescriptor = cpuDescriptor; BeginningAccess = beginningAccess; EndingAccess = endingAccess; }
public CpuDescriptorHandle GetTempCpuHandle() { CpuDescriptorHandle cpuHandle1 = heap.GetCPUDescriptorHandleForHeapStart(); cpuHandle1.Ptr += (nuint)(allocatedCount * IncrementSize); allocatedCount = (allocatedCount + 1) % descriptorCount; return(cpuHandle1); }
public GpuDescriptorHandle GetGpuDescriptorHandle(CpuDescriptorHandle descriptor) { if (!DescriptorHeap.Description.Flags.HasFlag(DescriptorHeapFlags.ShaderVisible)) { throw new InvalidOperationException(); } return(DescriptorHeap.GetGPUDescriptorHandleForHeapStart() + (descriptor.Ptr - DescriptorHeap.GetCPUDescriptorHandleForHeapStart().Ptr)); }
public void BuildDescriptors(CpuDescriptorHandle cpuSrv, GpuDescriptorHandle gpuSrv, CpuDescriptorHandle cpuRtv) { // Save references to the descriptors. _cpuSrv = cpuSrv; _gpuSrv = gpuSrv; _cpuRtv = cpuRtv; BuildDescriptors(); }
public void UpdateTransferFunctionTexture() { // TODO: This code is identical with TF loading in the LoadAssets method. // Could be extracted to common helper class, perhaps also including // volume texture loading. We call nearly-identical code three times in this file. // TODO: I suspect the might be another, minor, memory leak here. // Load transfer function var transferFunctionTextureDesc = ResourceDescription.Texture1D(Format.R8G8B8A8_UNorm, TransferFunctionWidth, 1); transferFunctionTexture = device.CreateCommittedResource(new HeapProperties( HeapType.Default), HeapFlags.None, transferFunctionTextureDesc, ResourceStates.CopyDestination); long transferFunctionUploadBufferSize = GetRequiredIntermediateSize(this.transferFunctionTexture, 0, 1); // Create the GPU upload buffer. transferFunctionTextureUploadHeap = device.CreateCommittedResource(new HeapProperties( CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None, ResourceDescription.Texture1D(Format.R8G8B8A8_UNorm, TransferFunctionWidth, 1), ResourceStates.GenericRead); // Copy data to the intermediate upload heap and then schedule a copy // from the upload heap to the Texture1D. byte[] transferFunctionTextureData = GenerateTransferFunctionTextureData(); var transferFunctionHandle = GCHandle.Alloc(transferFunctionTextureData, GCHandleType.Pinned); var transferFunctionPtr = Marshal.UnsafeAddrOfPinnedArrayElement(transferFunctionTextureData, 0); transferFunctionTextureUploadHeap.WriteToSubresource(0, null, transferFunctionPtr, TransferFunctionWidth, TransferFunctionWidth); transferFunctionHandle.Free(); commandList.CopyTextureRegion(new TextureCopyLocation(transferFunctionTexture, 0), 0, 0, 0, new TextureCopyLocation(transferFunctionTextureUploadHeap, 0), null); commandList.ResourceBarrierTransition(this.transferFunctionTexture, ResourceStates.CopyDestination, ResourceStates.PixelShaderResource); // Describe and create a SRV for the transfer function texture. var transferFunctionSrvDesc = new ShaderResourceViewDescription { Shader4ComponentMapping = D3DXUtilities.DefaultComponentMapping(), Format = transferFunctionTextureDesc.Format, Dimension = ShaderResourceViewDimension.Texture1D, Texture1D = { MipLevels = 1 }, }; var handleIncrement = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); CpuDescriptorHandle locationDesctiptor = shaderRenderViewHeap.CPUDescriptorHandleForHeapStart + handleIncrement; device.CreateShaderResourceView(this.transferFunctionTexture, transferFunctionSrvDesc, locationDesctiptor); // End load transfer function data }
public DescriptorHandle(ID3D12DescriptorHeap heap, int sizeIncrement, CpuDescriptorHandle cpuHandle) { Heap = heap; SizeIncrement = sizeIncrement; CpuHandle = cpuHandle; GpuHandle = new GpuDescriptorHandle { Ptr = InvalidAddress }; }