Skip to content

Commit

Permalink
add AllowTearing and VSync
Browse files Browse the repository at this point in the history
  • Loading branch information
FaberSanZ committed Jul 30, 2024
1 parent 1199a92 commit b231d7f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
7 changes: 4 additions & 3 deletions Src/Games/FPSTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public FPSTest()
{
Fullscreen = false,
VSync = false,
AllowTearing = true,
},
};
}
Expand Down Expand Up @@ -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();

Expand Down
14 changes: 13 additions & 1 deletion Src/Xultaik.Graphics/CommandList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions Src/Xultaik.Graphics/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class Settings

public bool VSync { get; set; }

public bool AllowTearing { get; set; }

}
}
85 changes: 65 additions & 20 deletions Src/Xultaik.Graphics/SwapChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand All @@ -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<ID3D12Resource>(BackBufferIndex));
for (int i = 0; i < bufferCount; i++)
{
BackBuffers[i] = new Texture(GraphicsDevice);
BackBuffers[i].InitializeFromImpl(NativeSwapChain.GetBuffer<ID3D12Resource>(i));
}


//BackBuffer = new Texture(GraphicsDevice);
//BackBuffer.InitializeFromImpl(NativeSwapChain.GetBuffer<ID3D12Resource>(BackBufferIndex));
}


Expand All @@ -68,18 +80,52 @@ private void CreateSwapChain()

public void Present()
{
NativeSwapChain.Present(1, PresentFlags.None);
BackBufferIndex = NativeSwapChain.CurrentBackBufferIndex;

BackBuffer.Resource.Dispose();
BackBuffer.InitializeFromImpl(NativeSwapChain.GetBuffer<ID3D12Resource>(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;

}



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()
{
Expand All @@ -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.


};
Expand All @@ -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);
}


Expand Down

0 comments on commit b231d7f

Please sign in to comment.