From b231d7f2111c270fc8d5a6441fd837897fd5036e Mon Sep 17 00:00:00 2001 From: Mr Faber SanZ Date: Tue, 30 Jul 2024 18:26:47 -0500 Subject: [PATCH] add AllowTearing and VSync --- Src/Games/FPSTest.cs | 7 ++- Src/Xultaik.Graphics/CommandList.cs | 14 ++++- Src/Xultaik.Graphics/Settings.cs | 2 + Src/Xultaik.Graphics/SwapChain.cs | 85 ++++++++++++++++++++++------- 4 files changed, 84 insertions(+), 24 deletions(-) diff --git a/Src/Games/FPSTest.cs b/Src/Games/FPSTest.cs index 383c83d..4eb949e 100644 --- a/Src/Games/FPSTest.cs +++ b/Src/Games/FPSTest.cs @@ -42,6 +42,7 @@ public FPSTest() { Fullscreen = false, VSync = false, + AllowTearing = true, }, }; } @@ -101,9 +102,9 @@ public void Draw() { CommandList.Reset(); - CommandList.SetViewport(0, 0, 800, 600); - CommandList.SetScissor(0, 0, 800, 600); - CommandList.ClearTargetColor(SwapChain.BackBuffer, 0.0f, 0.2f, 0.4f, 1.0f); + //CommandList.SetViewport(0, 0, 800, 600); + //CommandList.SetScissor(0, 0, 800, 600); + CommandList.ClearTargetColor(SwapChain, 0.0f, 0.2f, 0.4f, 1.0f); CommandList.ExecuteCommandList(); diff --git a/Src/Xultaik.Graphics/CommandList.cs b/Src/Xultaik.Graphics/CommandList.cs index 0fcb0a5..46e26b9 100644 --- a/Src/Xultaik.Graphics/CommandList.cs +++ b/Src/Xultaik.Graphics/CommandList.cs @@ -103,7 +103,7 @@ public void SetRenderTargets(Texture depthStencilBuffer, Texture[] renderTargets public void ClearTargetColor(Texture texture, float r, float g, float b, float a) { - ResourceTransition(texture, ResourceStates.RenderTarget, ResourceStates.Present); + //ResourceTransition(texture, ResourceStates.RenderTarget, ResourceStates.Present); nativeCommandList.ClearRenderTargetView(texture.NativeRenderTargetView, new Vortice.Mathematics.Color4(r, g, b, a)); nativeCommandList.OMSetRenderTargets(texture.NativeRenderTargetView); @@ -112,6 +112,18 @@ public void ClearTargetColor(Texture texture, float r, float g, float b, float a + public void ClearTargetColor(SwapChain swapChain, float r, float g, float b, float a) + { + var texture = swapChain.BackBuffer ; + //var texture = swapChain.BackBuffers[swapChain.BackBufferIndex]; + ResourceTransition(texture, ResourceStates.RenderTarget, ResourceStates.Present); + + nativeCommandList.ClearRenderTargetView(texture.NativeRenderTargetView, new Vortice.Mathematics.Color4(r, g, b, a)); + nativeCommandList.OMSetRenderTargets(texture.NativeRenderTargetView); + } + + + public void ClearDepth(Texture texture, float depth) { ResourceTransition(texture, ResourceStates.DepthRead, ResourceStates.Present); diff --git a/Src/Xultaik.Graphics/Settings.cs b/Src/Xultaik.Graphics/Settings.cs index c55588d..533afae 100644 --- a/Src/Xultaik.Graphics/Settings.cs +++ b/Src/Xultaik.Graphics/Settings.cs @@ -15,5 +15,7 @@ public class Settings public bool VSync { get; set; } + public bool AllowTearing { get; set; } + } } \ No newline at end of file diff --git a/Src/Xultaik.Graphics/SwapChain.cs b/Src/Xultaik.Graphics/SwapChain.cs index 1bc089b..4ec5548 100644 --- a/Src/Xultaik.Graphics/SwapChain.cs +++ b/Src/Xultaik.Graphics/SwapChain.cs @@ -20,7 +20,9 @@ public class SwapChain { public RenderDescriptor Description { get; private set; } - public Texture BackBuffer { get; set; } + //public Texture BackBuffer { get; set; } + public Texture[] BackBuffers { get; set; } + public Texture BackBuffer => BackBuffers[BackBufferIndex]; public int BackBufferIndex { get; private set; } = 0; @@ -29,7 +31,7 @@ public class SwapChain internal IDXGISwapChain3 NativeSwapChain; internal GraphicsDevice GraphicsDevice; - private int bufferCount = 3; + private int bufferCount = 2; public SwapChain(GraphicsDevice graphicsDevice) { @@ -38,11 +40,21 @@ public SwapChain(GraphicsDevice graphicsDevice) CreateSwapChain(); - BackBufferIndex = NativeSwapChain.CurrentBackBufferIndex; + //BackBufferIndex = NativeSwapChain.CurrentBackBufferIndex; + + + BackBuffers = new Texture[bufferCount]; - BackBuffer = new Texture(GraphicsDevice); - BackBuffer.InitializeFromImpl(NativeSwapChain.GetBuffer(BackBufferIndex)); + for (int i = 0; i < bufferCount; i++) + { + BackBuffers[i] = new Texture(GraphicsDevice); + BackBuffers[i].InitializeFromImpl(NativeSwapChain.GetBuffer(i)); + } + + + //BackBuffer = new Texture(GraphicsDevice); + //BackBuffer.InitializeFromImpl(NativeSwapChain.GetBuffer(BackBufferIndex)); } @@ -68,11 +80,26 @@ private void CreateSwapChain() public void Present() { - NativeSwapChain.Present(1, PresentFlags.None); - BackBufferIndex = NativeSwapChain.CurrentBackBufferIndex; - BackBuffer.Resource.Dispose(); - BackBuffer.InitializeFromImpl(NativeSwapChain.GetBuffer(BackBufferIndex)); + if (!Description.Settings.AllowTearing && !Description.Settings.VSync) + { + NativeSwapChain.Present(0, PresentFlags.None); // TODO: Defaul Full Screen ? + } + + + if (Description.Settings.AllowTearing && !Description.Settings.VSync) + { + NativeSwapChain.Present(0, PresentFlags.AllowTearing); + } + + + if (Description.Settings.VSync && !Description.Settings.AllowTearing) + { + NativeSwapChain.Present(1, PresentFlags.None); + } + + + BackBufferIndex = NativeSwapChain.CurrentBackBufferIndex; } @@ -80,6 +107,25 @@ public void Present() private void CreateSwapChainForDesktop() { + SwapChainFlags Flags = SwapChainFlags.None; // TODO: Defaul Full Screen ? + + + if (Description.Settings.AllowTearing) + { + Flags = SwapChainFlags.AllowTearing; + + Description.Settings.VSync = false; + } + + + + if (Description.Settings.VSync) + { + Flags = SwapChainFlags.None; + + Description.Settings.AllowTearing = false; + } + ModeDescription BackBufferDesc = new ModeDescription() { @@ -101,20 +147,19 @@ private void CreateSwapChainForDesktop() Quality = 0 }; - SwapChainFlags Flags = Description.Settings.Fullscreen ? SwapChainFlags.None : SwapChainFlags.AllowModeSwitch; - SwapChainDescription swapChainDesc = new SwapChainDescription() // Initialize the swap chain description. + SwapChainDescription swapChainDesc = new SwapChainDescription() { - BufferCount = bufferCount, // Set to a single back buffer. - BufferDescription = BackBufferDesc, // Set the width and height of the back buffer. - BufferUsage = Usage.Backbuffer | Usage.RenderTargetOutput, // Set the usage of the back buffer. - OutputWindow = Description.DeviceHandle, // Set the handle for the window to render to. - SampleDescription = sampleDescription, // Turn multisampling off. - Windowed = true, // Set to full screen or windowed mode. - Flags = Flags, // Don't set the advanced flags. - SwapEffect = SwapEffect.FlipDiscard, // Discard the back buffer content after presenting. + BufferCount = bufferCount, // Set to a single back buffer. + BufferDescription = BackBufferDesc, // Set the width and height of the back buffer. + BufferUsage = Usage.RenderTargetOutput, // Set the usage of the back buffer. + OutputWindow = Description.DeviceHandle, // Set the handle for the window to render to. + SampleDescription = sampleDescription, // Turn multisampling off. + Windowed = true, // Set to full screen or windowed mode. + Flags = Flags, // Don't set the advanced flags. + SwapEffect = SwapEffect.FlipDiscard, // Discard the back buffer content after presenting. }; @@ -137,7 +182,7 @@ private void CreateSwapChainForDesktop() swapChain.SetFullscreenState(true, default); // This is really important to call ResizeBuffers AFTER switching to IsFullScreen - swapChain.ResizeBuffers(3, Description.BackBufferWidth, Description.BackBufferHeight, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch); + swapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, Format.R8G8B8A8_UNorm, Flags); }