diff --git a/src/DirectX12GameEngine.Graphics/CommandList.cs b/src/DirectX12GameEngine.Graphics/CommandList.cs index 843bbf88..bcc34d45 100644 --- a/src/DirectX12GameEngine.Graphics/CommandList.cs +++ b/src/DirectX12GameEngine.Graphics/CommandList.cs @@ -28,62 +28,14 @@ namespace DirectX12GameEngine.Graphics public CommandListType CommandListType { get; } - public Texture? DepthStencilBuffer { get; private set; } - public GraphicsDevice GraphicsDevice { get; } - public Texture[] RenderTargets { get; private set; } = Array.Empty(); - public RawRectangle[] ScissorRectangles { get; private set; } = Array.Empty(); public RawViewportF[] Viewports { get; private set; } = Array.Empty(); public PrimitiveTopology PrimitiveTopology { set => currentCommandList.NativeCommandList.PrimitiveTopology = value; } - public void BeginRenderPass() - { - BeginRenderPass(DepthStencilBuffer, RenderTargets); - } - - public void BeginRenderPass(Texture? depthStencilView, params Texture[] renderTargetViews) - { - RenderPassRenderTargetDescription[] renderPassRenderTargetDescriptions = new RenderPassRenderTargetDescription[renderTargetViews.Length]; - - RenderPassBeginningAccess renderPassBeginningAccessPreserve = new RenderPassBeginningAccess { Type = RenderPassBeginningAccessType.Preserve }; - RenderPassEndingAccess renderPassEndingAccessPreserve = new RenderPassEndingAccess { Type = RenderPassEndingAccessType.Preserve }; - - for (int i = 0; i < renderTargetViews.Length; i++) - { - RenderPassRenderTargetDescription renderPassRenderTargetDescription = new RenderPassRenderTargetDescription - { - BeginningAccess = renderPassBeginningAccessPreserve, - EndingAccess = renderPassEndingAccessPreserve, - CpuDescriptor = renderTargetViews[i].NativeCpuDescriptorHandle - }; - - renderPassRenderTargetDescriptions[i] = renderPassRenderTargetDescription; - } - - RenderPassBeginningAccess renderPassBeginningAccessNoAccess = new RenderPassBeginningAccess { Type = RenderPassBeginningAccessType.NoAccess }; - RenderPassEndingAccess renderPassEndingAccessNoAccess = new RenderPassEndingAccess { Type = RenderPassEndingAccessType.NoAccess }; - - RenderPassDepthStencilDescription? renderPassDepthStencilDescription = null; - - if (depthStencilView != null) - { - renderPassDepthStencilDescription = new RenderPassDepthStencilDescription - { - DepthBeginningAccess = renderPassBeginningAccessNoAccess, - DepthEndingAccess = renderPassEndingAccessNoAccess, - StencilBeginningAccess = renderPassBeginningAccessNoAccess, - StencilEndingAccess = renderPassEndingAccessNoAccess, - CpuDescriptor = depthStencilView.NativeCpuDescriptorHandle, - }; - } - - BeginRenderPass(renderTargetViews.Length, renderPassRenderTargetDescriptions, renderPassDepthStencilDescription, RenderPassFlags.None); - } - public void BeginRenderPass(int numRenderTargets, RenderPassRenderTargetDescription[] renderTargetsRef, RenderPassDepthStencilDescription? depthStencilRef, RenderPassFlags flags) { using GraphicsCommandList4 commandList = currentCommandList.NativeCommandList.QueryInterface(); @@ -96,42 +48,6 @@ namespace DirectX12GameEngine.Graphics commandList.EndRenderPass(); } - public void Clear(Texture depthStencilBuffer, ClearFlags clearFlags, float depth = 1, byte stencil = 0) - { - currentCommandList.NativeCommandList.ClearDepthStencilView(depthStencilBuffer.NativeCpuDescriptorHandle, (SharpDX.Direct3D12.ClearFlags)clearFlags, depth, stencil); - } - - public unsafe void Clear(Texture renderTarget, Vector4 color) - { - currentCommandList.NativeCommandList.ClearRenderTargetView(renderTarget.NativeCpuDescriptorHandle, *(RawColor4*)&color); - } - - public void ClearState() - { - Array.Clear(Viewports, 0, Viewports.Length); - Array.Clear(ScissorRectangles, 0, ScissorRectangles.Length); - - Texture? depthStencilBuffer = GraphicsDevice.Presenter?.DepthStencilBuffer; - Texture? backBuffer = GraphicsDevice.Presenter?.BackBuffer; - - if (backBuffer != null) - { - SetRenderTargets(depthStencilBuffer, backBuffer); - SetViewports(new RawViewportF { X = 0, Y = 0, Width = backBuffer.Width, Height = backBuffer.Height, MaxDepth = 1.0f }); - SetScissorRectangles(new RawRectangle { Right = backBuffer.Width, Bottom = backBuffer.Height }); - } - else if (depthStencilBuffer != null) - { - SetRenderTargets(depthStencilBuffer); - SetViewports(new RawViewportF { X = 0, Y = 0, Width = depthStencilBuffer.Width, Height = depthStencilBuffer.Height, MaxDepth = 1.0f }); - SetScissorRectangles(new RawRectangle { Right = depthStencilBuffer.Width, Bottom = depthStencilBuffer.Height }); - } - else - { - SetRenderTargets(null); - } - } - public CompiledCommandList Close() { //foreach (var renderTarget in RenderTargets) @@ -297,33 +213,6 @@ namespace DirectX12GameEngine.Graphics currentCommandList.NativeCommandList.PipelineState = pipelineState.NativePipelineState; } - public void SetRenderTargets(Texture? depthStencilView, params Texture[] 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 Texture[renderTargetViews.Length]; - } - - renderTargetViews.CopyTo(RenderTargets, 0); - - CpuDescriptorHandle[] renderTargetDescriptors = new CpuDescriptorHandle[renderTargetViews.Length]; - - for (int i = 0; i < renderTargetViews.Length; i++) - { - //ResourceBarrierTransition(renderTargetViews[i], ResourceStates.Present, ResourceStates.RenderTarget); - renderTargetDescriptors[i] = renderTargetViews[i].NativeCpuDescriptorHandle; - } - - currentCommandList.NativeCommandList.SetRenderTargets(renderTargetDescriptors, depthStencilView?.NativeCpuDescriptorHandle); - } - public void SetScissorRectangles(params RawRectangle[] scissorRectangles) { if (scissorRectangles.Length > MaxViewportAndScissorRectangleCount) diff --git a/src/DirectX12GameEngine.Graphics/GraphicsPresenter.cs b/src/DirectX12GameEngine.Graphics/GraphicsPresenter.cs index 3edf4fd1..8db786ce 100644 --- a/src/DirectX12GameEngine.Graphics/GraphicsPresenter.cs +++ b/src/DirectX12GameEngine.Graphics/GraphicsPresenter.cs @@ -8,27 +8,20 @@ namespace DirectX12GameEngine.Graphics { GraphicsDevice = device; PresentationParameters = presentationParameters; - - DepthStencilBuffer = CreateDepthStencilBuffer(); } - public abstract Texture BackBuffer { get; } - public GraphicsDevice GraphicsDevice { get; } public abstract object NativePresenter { get; } public PresentationParameters PresentationParameters { get; } - public Texture DepthStencilBuffer { get; protected set; } - public virtual void BeginDraw(CommandList commandList) { } public virtual void Dispose() { - DepthStencilBuffer.Dispose(); } public abstract void Present(); @@ -39,18 +32,8 @@ namespace DirectX12GameEngine.Graphics PresentationParameters.BackBufferHeight = height; ResizeBackBuffer(width, height); - ResizeDepthStencilBuffer(width, height); - } - - protected virtual Texture CreateDepthStencilBuffer() - { - return Texture.New2D(GraphicsDevice, - PresentationParameters.BackBufferWidth, PresentationParameters.BackBufferHeight, PresentationParameters.DepthStencilFormat, - TextureFlags.DepthStencil, 1, PresentationParameters.Stereo ? 2 : 1); } protected abstract void ResizeBackBuffer(int width, int height); - - protected abstract void ResizeDepthStencilBuffer(int width, int height); } } diff --git a/src/DirectX12GameEngine.Graphics/ImageDescription.cs b/src/DirectX12GameEngine.Graphics/ImageDescription.cs index 4575a9da..cf605e31 100644 --- a/src/DirectX12GameEngine.Graphics/ImageDescription.cs +++ b/src/DirectX12GameEngine.Graphics/ImageDescription.cs @@ -2,7 +2,6 @@ { public struct ImageDescription { - public TextureDimension Dimension; public int Width; @@ -20,7 +19,6 @@ { return new ImageDescription { - Dimension = TextureDimension.Texture2D, Width = width, Height = height, Depth = 1, diff --git a/src/DirectX12GameEngine.Graphics/PipelineState.cs b/src/DirectX12GameEngine.Graphics/PipelineState.cs index 692df0d9..79bcfd32 100644 --- a/src/DirectX12GameEngine.Graphics/PipelineState.cs +++ b/src/DirectX12GameEngine.Graphics/PipelineState.cs @@ -82,18 +82,6 @@ namespace DirectX12GameEngine.Graphics StreamOutput = new StreamOutputDescription() }; - Texture? depthStencilBuffer = device.CommandList.DepthStencilBuffer; - - if (depthStencilBuffer != null) - { - pipelineStateDescription.DepthStencilFormat = (Format)depthStencilBuffer.Description.Format; - } - - for (int i = 0; i < device.CommandList.RenderTargets.Length; i++) - { - pipelineStateDescription.RenderTargetFormats[i] = (Format)device.CommandList.RenderTargets[i].Description.Format; - } - return pipelineStateDescription; } } diff --git a/src/DirectX12GameEngine.Graphics/SwapChainGraphicsPresenter.cs b/src/DirectX12GameEngine.Graphics/SwapChainGraphicsPresenter.cs index 826f1387..8dc3198b 100644 --- a/src/DirectX12GameEngine.Graphics/SwapChainGraphicsPresenter.cs +++ b/src/DirectX12GameEngine.Graphics/SwapChainGraphicsPresenter.cs @@ -10,8 +10,6 @@ namespace DirectX12GameEngine.Graphics { protected const int BufferCount = 2; - private readonly Texture[] renderTargets = new Texture[BufferCount]; - public SwapChainGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters, SwapChain3 swapChain) : base(device, presentationParameters) { @@ -22,12 +20,8 @@ namespace DirectX12GameEngine.Graphics GraphicsDevice.RenderTargetViewAllocator.Dispose(); GraphicsDevice.RenderTargetViewAllocator = new DescriptorAllocator(GraphicsDevice, DescriptorHeapType.RenderTargetView, descriptorCount: BufferCount); } - - CreateRenderTargets(); } - public override Texture BackBuffer => renderTargets[SwapChain.CurrentBackBufferIndex]; - public Matrix3x2 MatrixTransform { get => SwapChain.MatrixTransform.ToMatrix3x2(); set => SwapChain.MatrixTransform = value.ToMatrix3x2(); } public override object NativePresenter => SwapChain; @@ -38,11 +32,6 @@ namespace DirectX12GameEngine.Graphics { SwapChain.Dispose(); - foreach (Texture renderTarget in renderTargets) - { - renderTarget.Dispose(); - } - base.Dispose(); } @@ -53,28 +42,9 @@ namespace DirectX12GameEngine.Graphics protected override void ResizeBackBuffer(int width, int height) { - for (int i = 0; i < BufferCount; i++) - { - renderTargets[i].Dispose(); - } SwapChain.ResizeBuffers(BufferCount, width, height, (Format)PresentationParameters.BackBufferFormat, SwapChainFlags.None); - CreateRenderTargets(); - } - - protected override void ResizeDepthStencilBuffer(int width, int height) - { - DepthStencilBuffer.Dispose(); - DepthStencilBuffer = CreateDepthStencilBuffer(); - } - - private void CreateRenderTargets() - { - for (int i = 0; i < BufferCount; i++) - { - renderTargets[i] = new Texture(GraphicsDevice).InitializeFrom(SwapChain.GetBackBuffer(i)); - } } } diff --git a/src/DirectX12GameEngine.Graphics/Texture.cs b/src/DirectX12GameEngine.Graphics/Texture.cs deleted file mode 100644 index 3879ce41..00000000 --- a/src/DirectX12GameEngine.Graphics/Texture.cs +++ /dev/null @@ -1,223 +0,0 @@ -using System; -using System.IO; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using SharpDX.Direct3D12; -using SharpDX.DXGI; - -using Resource = SharpDX.Direct3D12.Resource; - -namespace DirectX12GameEngine.Graphics -{ - public sealed class Texture : GraphicsResource - { - public Texture() - { - } - - internal Texture(GraphicsDevice device) : base(device) - { - } - - public TextureDescription Description { get; private set; } - - public int Width => Description.Width; - - public int Height => Description.Height; - - public static async Task LoadAsync(GraphicsDevice device, string filePath) - { - using FileStream stream = File.OpenRead(filePath); - return await LoadAsync(device, stream); - } - - public static async Task LoadAsync(GraphicsDevice device, Stream stream) - { - using Image image = await Image.LoadAsync(stream); - return New2D(device, image.Data.Span, image.Width, image.Height, image.Description.Format); ; - } - - public static Texture New(GraphicsDevice device, TextureDescription description) - { - return new Texture(device).InitializeFrom(description); - } - - public static Texture New2D(GraphicsDevice device, int width, int height, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int mipCount = 1, int arraySize = 1, int multisampleCount = 1, GraphicsHeapType heapType = GraphicsHeapType.Default) - { - return New(device, TextureDescription.New2D(width, height, format, textureFlags, mipCount, arraySize, multisampleCount, heapType)); - } - - public static Texture New2D(GraphicsDevice device, Span data, int width, int height, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int mipCount = 1, int arraySize = 1, int multisampleCount = 1, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged - { - Texture texture = New2D(device, width, height, format, textureFlags, mipCount, arraySize, multisampleCount, heapType); - texture.Recreate(data); - - return texture; - } - - public unsafe void Recreate(Span data) where T : unmanaged - { - Resource uploadResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None, NativeResource.Description, ResourceStates.CopyDestination); - using Texture textureUploadBuffer = new Texture(GraphicsDevice).InitializeFrom(uploadResource); - - fixed (T* pointer = data) - { - textureUploadBuffer.NativeResource.WriteToSubresource(0, null, (IntPtr)pointer, ((Format)Description.Format).SizeOfInBytes() * Width, data.Length * Unsafe.SizeOf()); - } - - using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy); - - copyCommandList.CopyResource(textureUploadBuffer, this); - copyCommandList.Flush(true); - } - - public Texture InitializeFrom(TextureDescription description) - { - ResourceStates resourceStates = ResourceStates.Common; - - if (description.HeapType == GraphicsHeapType.Upload) - { - resourceStates = ResourceStates.GenericRead; - } - else if (description.HeapType == GraphicsHeapType.Readback) - { - resourceStates = ResourceStates.CopyDestination; - } - - NativeResource ??= GraphicsDevice.NativeDevice.CreateCommittedResource( - new HeapProperties((HeapType)description.HeapType), HeapFlags.None, - ConvertToNativeDescription(description), resourceStates); - - Description = description; - - (NativeCpuDescriptorHandle, NativeGpuDescriptorHandle) = description.Flags switch - { - TextureFlags.DepthStencil => CreateDepthStencilView(), - TextureFlags.RenderTarget => CreateRenderTargetView(), - TextureFlags.ShaderResource => CreateShaderResourceView(), - TextureFlags.UnorderedAccess => CreateUnorderedAccessView(), - _ => default - }; - - return this; - } - - internal Texture InitializeFrom(Resource resource) - { - NativeResource = resource; - - TextureDescription description = ConvertFromNativeDescription(resource.Description); - - return InitializeFrom(description); - } - - internal static ResourceFlags GetBindFlagsFromTextureFlags(TextureFlags flags) - { - ResourceFlags result = ResourceFlags.None; - - if (flags.HasFlag(TextureFlags.RenderTarget)) - { - result |= ResourceFlags.AllowRenderTarget; - } - - if (flags.HasFlag(TextureFlags.UnorderedAccess)) - { - result |= ResourceFlags.AllowUnorderedAccess; - //result |= ResourceFlags.AllowSimultaneousAccess; - } - - if (flags.HasFlag(TextureFlags.DepthStencil)) - { - result |= ResourceFlags.AllowDepthStencil; - - if (!flags.HasFlag(TextureFlags.ShaderResource)) - { - result |= ResourceFlags.DenyShaderResource; - } - } - - return result; - } - - private static TextureDescription ConvertFromNativeDescription(ResourceDescription description, bool isShaderResource = false) - { - TextureDescription textureDescription = new TextureDescription() - { - Dimension = TextureDimension.Texture2D, - Width = (int)description.Width, - Height = description.Height, - Depth = 1, - MultisampleCount = description.SampleDescription.Count, - Format = (PixelFormat)description.Format, - MipLevels = description.MipLevels, - HeapType = GraphicsHeapType.Default, - ArraySize = description.DepthOrArraySize, - Flags = TextureFlags.None - }; - - if (description.Flags.HasFlag(ResourceFlags.AllowRenderTarget)) - { - textureDescription.Flags |= TextureFlags.RenderTarget; - } - - if (description.Flags.HasFlag(ResourceFlags.AllowUnorderedAccess)) - { - textureDescription.Flags |= TextureFlags.UnorderedAccess; - } - - if (description.Flags.HasFlag(ResourceFlags.AllowDepthStencil)) - { - textureDescription.Flags |= TextureFlags.DepthStencil; - } - - if (!description.Flags.HasFlag(ResourceFlags.DenyShaderResource) && isShaderResource) - { - textureDescription.Flags |= TextureFlags.ShaderResource; - } - - return textureDescription; - } - - private static ResourceDescription ConvertToNativeDescription(TextureDescription description) - { - return description.Dimension switch - { - TextureDimension.Texture2D => ResourceDescription.Texture2D((Format)description.Format, description.Width, description.Height, (short)description.ArraySize, (short)description.MipLevels, description.MultisampleCount, 0, GetBindFlagsFromTextureFlags(description.Flags)), - _ => throw new NotSupportedException() - }; - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateDepthStencilView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.DepthStencilViewAllocator.Allocate(1); - GraphicsDevice.NativeDevice.CreateDepthStencilView(NativeResource, null, cpuHandle); - - return (cpuHandle, gpuHandle); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateRenderTargetView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.RenderTargetViewAllocator.Allocate(1); - GraphicsDevice.NativeDevice.CreateRenderTargetView(NativeResource, null, cpuHandle); - - return (cpuHandle, gpuHandle); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateShaderResourceView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, null, cpuHandle); - - return (cpuHandle, gpuHandle); - } - - private (CpuDescriptorHandle, GpuDescriptorHandle) CreateUnorderedAccessView() - { - (CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle) = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1); - - GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, null, cpuHandle); - - return (cpuHandle, gpuHandle); - } - } -} diff --git a/src/DirectX12GameEngine.Graphics/TextureDescription.cs b/src/DirectX12GameEngine.Graphics/TextureDescription.cs deleted file mode 100644 index ba46f874..00000000 --- a/src/DirectX12GameEngine.Graphics/TextureDescription.cs +++ /dev/null @@ -1,73 +0,0 @@ -namespace DirectX12GameEngine.Graphics -{ - public struct TextureDescription - { - public TextureDimension Dimension; - - public int Width; - - public int Height; - - public int Depth; - - public int ArraySize; - - public int MipLevels; - - public PixelFormat Format; - - public int MultisampleCount; - - public GraphicsHeapType HeapType; - - public TextureFlags Flags; - - public static TextureDescription New2D(int width, int height, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int mipCount = 1, int arraySize = 1, int multisampleCount = 1, GraphicsHeapType heapType = GraphicsHeapType.Default) - { - return new TextureDescription - { - Dimension = TextureDimension.Texture2D, - Width = width, - Height = height, - Depth = 1, - ArraySize = arraySize, - MultisampleCount = multisampleCount, - Flags = textureFlags, - Format = format, - MipLevels = mipCount, - HeapType = heapType, - }; - } - - public static implicit operator TextureDescription(ImageDescription description) - { - return new TextureDescription() - { - Dimension = description.Dimension, - Width = description.Width, - Height = description.Height, - Depth = description.Depth, - ArraySize = description.ArraySize, - MipLevels = description.MipLevels, - Format = description.Format, - Flags = TextureFlags.ShaderResource, - MultisampleCount = 1, - HeapType = GraphicsHeapType.Default - }; - } - - public static implicit operator ImageDescription(TextureDescription description) - { - return new ImageDescription() - { - Dimension = description.Dimension, - Width = description.Width, - Height = description.Height, - Depth = description.Depth, - ArraySize = description.ArraySize, - MipLevels = description.MipLevels, - Format = description.Format, - }; - } - } -} diff --git a/src/DirectX12GameEngine.Graphics/TextureDimension.cs b/src/DirectX12GameEngine.Graphics/TextureDimension.cs deleted file mode 100644 index b6f9a580..00000000 --- a/src/DirectX12GameEngine.Graphics/TextureDimension.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace DirectX12GameEngine.Graphics -{ - public enum TextureDimension - { - Texture1D, - Texture2D, - Texture3D, - TextureCube - } -} diff --git a/src/DirectX12GameEngine.Graphics/TextureFlags.cs b/src/DirectX12GameEngine.Graphics/TextureFlags.cs deleted file mode 100644 index 6cfa0070..00000000 --- a/src/DirectX12GameEngine.Graphics/TextureFlags.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace DirectX12GameEngine.Graphics -{ - [Flags] - public enum TextureFlags - { - None = 0, - RenderTarget = 1, - DepthStencil = 2, - UnorderedAccess = 4, - ShaderResource = 8 - } -}