diff --git a/samples/ComputeSharp.Sample.SwapChain/Program.cs b/samples/ComputeSharp.Sample.SwapChain/Program.cs
index bb263015..66f488b5 100644
--- a/samples/ComputeSharp.Sample.SwapChain/Program.cs
+++ b/samples/ComputeSharp.Sample.SwapChain/Program.cs
@@ -1,6 +1,8 @@
using System;
using System.Drawing;
+using ComputeSharp.Interop;
using TerraFX.Interop;
+using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.Sample.SwapChain
{
@@ -14,14 +16,123 @@ namespace ComputeSharp.Sample.SwapChain
internal sealed class FractalTilesApplication : Win32Application
{
+ ///
+ /// The pointer for the device currently in use.
+ ///
+ private ComPtr d3D12Device;
+
+ ///
+ /// The instance to use for graphics operations.
+ ///
+ private ComPtr d3D12CommandQueue;
+
+ ///
+ /// The instance used for graphics operations.
+ ///
+ private ComPtr d3D12Fence;
+
+ ///
+ /// The next fence value for graphics operations using .
+ ///
+ //private ulong nextD3D12FenceValue = 1;
+
+ ///
+ /// The instance used to display content onto the target window.
+ ///
+ private ComPtr dxgiSwapChain1;
+
+ ///
+ /// The first buffer within .
+ ///
+ private ComPtr d3D12Resource0;
+
+ ///
+ /// The second buffer within .
+ ///
+ private ComPtr d3D12Resource1;
+
+ ///
+ /// The index of the next buffer that can be used to present content.
+ ///
+ private uint currentBufferIndex;
+
public override string Title => "Fractal tiles";
- public override void Dispose()
+ public override unsafe void OnInitialize(Size size, HWND hwnd)
{
- }
+ // Get the underlying ID3D12Device in use
+ fixed (ID3D12Device** d3D12Device = this.d3D12Device)
+ {
+ _ = InteropServices.TryGetID3D12Device(Gpu.Default, FX.__uuidof(), (void**)d3D12Device);
+ }
- public override void OnInitialize(Size size, HWND hwnd)
- {
+ // Create the direct command queue to use
+ fixed (ID3D12CommandQueue** d3D12CommandQueue = this.d3D12CommandQueue)
+ {
+ D3D12_COMMAND_QUEUE_DESC d3D12CommandQueueDesc;
+ d3D12CommandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT;
+ d3D12CommandQueueDesc.Priority = (int)D3D12_COMMAND_QUEUE_PRIORITY.D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
+ d3D12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE;
+ d3D12CommandQueueDesc.NodeMask = 0;
+
+ _ = d3D12Device.Get()->CreateCommandQueue(
+ &d3D12CommandQueueDesc,
+ FX.__uuidof(),
+ (void**)d3D12CommandQueue);
+ }
+
+ // Create the direct fence
+ fixed (ID3D12Fence** d3D12Fence = this.d3D12Fence)
+ {
+ _ = this.d3D12Device.Get()->CreateFence(
+ 0,
+ D3D12_FENCE_FLAGS.D3D12_FENCE_FLAG_NONE,
+ FX.__uuidof(),
+ (void**)d3D12Fence);
+ }
+
+ // Create the swap chain to display frames
+ fixed (IDXGISwapChain1** dxgiSwapChain1 = this.dxgiSwapChain1)
+ {
+ using ComPtr dxgiFactory2 = default;
+
+ _ = FX.CreateDXGIFactory2(FX.DXGI_CREATE_FACTORY_DEBUG, FX.__uuidof(), (void**)dxgiFactory2.GetAddressOf());
+
+ DXGI_SWAP_CHAIN_DESC1 dxgiSwapChainDesc1 = default;
+ dxgiSwapChainDesc1.AlphaMode = DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_IGNORE;
+ dxgiSwapChainDesc1.BufferCount = 2;
+ dxgiSwapChainDesc1.Flags = 0;
+ dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
+ dxgiSwapChainDesc1.Width = 0;
+ dxgiSwapChainDesc1.Height = 0;
+ dxgiSwapChainDesc1.SampleDesc = new DXGI_SAMPLE_DESC(count: 1, quality: 0);
+ dxgiSwapChainDesc1.Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH;
+ dxgiSwapChainDesc1.Stereo = 0;
+ dxgiSwapChainDesc1.SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
+
+ _ = dxgiFactory2.Get()->CreateSwapChainForHwnd(
+ (IUnknown*)d3D12CommandQueue.Get(),
+ hwnd,
+ &dxgiSwapChainDesc1,
+ null,
+ null,
+ dxgiSwapChain1);
+ }
+
+ // Retrieve the back buffers for the swap chain
+ fixed (ID3D12Resource** d3D12Resource0 = this.d3D12Resource0)
+ fixed (ID3D12Resource** d3D12Resource1 = this.d3D12Resource1)
+ {
+ _ = dxgiSwapChain1.Get()->GetBuffer(0, FX.__uuidof(), (void**)d3D12Resource0);
+ _ = dxgiSwapChain1.Get()->GetBuffer(1, FX.__uuidof(), (void**)d3D12Resource1);
+ }
+
+ // Get the index of the current buffer
+ using ComPtr dxgiSwapChain3 = default;
+
+ _ = this.dxgiSwapChain1.CopyTo(dxgiSwapChain3.GetAddressOf());
+
+ this.currentBufferIndex = dxgiSwapChain3.Get()->GetCurrentBackBufferIndex();
}
public override void OnResize(Size size)
@@ -31,5 +142,9 @@ namespace ComputeSharp.Sample.SwapChain
public override void OnUpdate(TimeSpan time)
{
}
+
+ public override void Dispose()
+ {
+ }
}
}
diff --git a/samples/ComputeSharp.Sample.SwapChain/Win32ApplicationRunner.cs b/samples/ComputeSharp.Sample.SwapChain/Win32ApplicationRunner.cs
index b33dac6f..a0ec1e0e 100644
--- a/samples/ComputeSharp.Sample.SwapChain/Win32ApplicationRunner.cs
+++ b/samples/ComputeSharp.Sample.SwapChain/Win32ApplicationRunner.cs
@@ -27,15 +27,55 @@ namespace ComputeSharp.Sample.SwapChain
///
internal unsafe static class Win32ApplicationRunner
{
+ ///
+ /// Whether or not a resize operation is in progress.
+ ///
private static bool isResizing = false;
+
+ ///
+ /// Whether or not the application is currently paused.
+ ///
private static bool isPaused = false;
+
+ ///
+ /// The for the application window.
+ ///
private static HWND hwnd;
+
+ ///
+ /// The current size of the application window.
+ ///
private static Size windowSize;
+
+ ///
+ /// A instance used to track the elapsed time for the application.
+ ///
private static Stopwatch stopwatch = null!;
+
+ ///
+ /// The application being run.
+ ///
private static Win32Application application = null!;
+
+ ///
+ /// The number of elapsed milliseconds since the window title was last updated.
+ ///
private static long elapsedMillisecondsForLastTitleUpdate;
+
+ ///
+ /// The number of elapsed frames since the window title was last updated.
+ ///
private static int elapsedFramesSinceLastTitleUpdate;
+ ///
+ /// Runs a specified application and starts the main loop to update its state.
+ /// This is the entry point for a given application of type , and it should be
+ /// called as soon as the process is launched, excluding any other additional initialization needed.
+ /// To launch an application, simply add this line to the project being used:
+ /// Win32ApplicationRunner.Run<MyApplication>();
+ ///
+ /// The type of application being launched.
+ /// The exit code for the application.
public static int Run()
where T : Win32Application, new()
{
@@ -149,6 +189,8 @@ namespace ComputeSharp.Sample.SwapChain
// Update the application state
application.OnUpdate(stopwatch.Elapsed);
+
+ elapsedFramesSinceLastTitleUpdate++;
}
///
@@ -186,7 +228,7 @@ namespace ComputeSharp.Sample.SwapChain
if (isPaused)
{
- FX.SetCapture(Win32ApplicationRunner.hwnd);
+ FX.SetCapture(hwnd);
}
else
{