diff --git a/ComputeSharp.sln b/ComputeSharp.sln index cccc08d6..7e4f1344 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -47,6 +47,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internal EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.NativeLibrariesResolver", "tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj", "{BF52326F-E415-49E6-8595-62762216762B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Sample.SwapChain", "samples\ComputeSharp.Sample.SwapChain\ComputeSharp.Sample.SwapChain.csproj", "{E7C9247D-016B-4819-AF9F-0E9B3591E04B}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution samples\ComputeSharp.Sample.Shared\ComputeSharp.Sample.Shared.projitems*{7048deea-0a6f-46a0-b951-b682a8f1cb75}*SharedItemsImports = 5 @@ -172,6 +174,14 @@ Global {BF52326F-E415-49E6-8595-62762216762B}.Release|Any CPU.Build.0 = Release|Any CPU {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.ActiveCfg = Release|Any CPU {BF52326F-E415-49E6-8595-62762216762B}.Release|x64.Build.0 = Release|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Debug|x64.ActiveCfg = Debug|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Debug|x64.Build.0 = Debug|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|Any CPU.Build.0 = Release|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.ActiveCfg = Release|Any CPU + {E7C9247D-016B-4819-AF9F-0E9B3591E04B}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -187,6 +197,7 @@ Global {8CEFC324-CA6C-4845-9AD9-70B92BD87057} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} {BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} {BF52326F-E415-49E6-8595-62762216762B} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7} + {E7C9247D-016B-4819-AF9F-0E9B3591E04B} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC} diff --git a/samples/ComputeSharp.Sample.SwapChain/ComputeSharp.Sample.SwapChain.csproj b/samples/ComputeSharp.Sample.SwapChain/ComputeSharp.Sample.SwapChain.csproj new file mode 100644 index 00000000..44b2ad45 --- /dev/null +++ b/samples/ComputeSharp.Sample.SwapChain/ComputeSharp.Sample.SwapChain.csproj @@ -0,0 +1,16 @@ + + + + Exe + net5.0 + + + + + + + + diff --git a/samples/ComputeSharp.Sample.SwapChain/Program.cs b/samples/ComputeSharp.Sample.SwapChain/Program.cs new file mode 100644 index 00000000..bb263015 --- /dev/null +++ b/samples/ComputeSharp.Sample.SwapChain/Program.cs @@ -0,0 +1,35 @@ +using System; +using System.Drawing; +using TerraFX.Interop; + +namespace ComputeSharp.Sample.SwapChain +{ + class Program + { + static void Main(string[] args) + { + Win32ApplicationRunner.Run(); + } + } + + internal sealed class FractalTilesApplication : Win32Application + { + public override string Title => "Fractal tiles"; + + public override void Dispose() + { + } + + public override void OnInitialize(Size size, HWND hwnd) + { + } + + public override void OnResize(Size size) + { + } + + public override void OnUpdate(TimeSpan time) + { + } + } +} diff --git a/samples/ComputeSharp.Sample.SwapChain/Win32ApplicationRunner.cs b/samples/ComputeSharp.Sample.SwapChain/Win32ApplicationRunner.cs new file mode 100644 index 00000000..b33dac6f --- /dev/null +++ b/samples/ComputeSharp.Sample.SwapChain/Win32ApplicationRunner.cs @@ -0,0 +1,243 @@ +using System; +using System.Diagnostics; +using System.Drawing; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Threading; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Sample.SwapChain +{ + internal abstract class Win32Application : IDisposable + { + public abstract string Title { get; } + + public abstract void OnInitialize(Size size, HWND hwnd); + + public abstract void OnResize(Size size); + + public abstract void OnUpdate(TimeSpan time); + + public abstract void Dispose(); + } + + /// + /// A helper class to manage the creation and execution of Win32 applications. + /// + internal unsafe static class Win32ApplicationRunner + { + private static bool isResizing = false; + private static bool isPaused = false; + private static HWND hwnd; + private static Size windowSize; + private static Stopwatch stopwatch = null!; + private static Win32Application application = null!; + private static long elapsedMillisecondsForLastTitleUpdate; + private static int elapsedFramesSinceLastTitleUpdate; + + public static int Run() + where T : Win32Application, new() + { + application = new T(); + + IntPtr hInstance = FX.GetModuleHandleW(null); + uint width = 1280; + uint height = 720; + + fixed (char* name = Assembly.GetExecutingAssembly().FullName) + fixed (char* windowTitle = application.Title) + { + // Initialize the window class + WNDCLASSEXW windowClassEx = new() + { + cbSize = (uint)sizeof(WNDCLASSEXW), + style = FX.CS_HREDRAW | FX.CS_VREDRAW, + lpfnWndProc = &WindowProc, + hInstance = hInstance, + hCursor = FX.LoadCursorW(IntPtr.Zero, FX.MAKEINTRESOURCE(32512)), + lpszClassName = (ushort*)name + }; + + // Register the window class + _ = FX.RegisterClassExW(&windowClassEx); + + Rectangle windowRect = new(0, 0, (int)width, (int)height); + + // Set the target window size + _ = FX.AdjustWindowRect((RECT*)&windowRect, FX.WS_OVERLAPPEDWINDOW, FX.FALSE); + + height = (uint)(windowRect.Bottom - windowRect.Top); + width = (uint)(windowRect.Right - windowRect.Left); + + // Create the window and store a handle to it + hwnd = FX.CreateWindowExW( + 0, + windowClassEx.lpszClassName, + (ushort*)windowTitle, + FX.WS_OVERLAPPEDWINDOW, + FX.CW_USEDEFAULT, + FX.CW_USEDEFAULT, + (int)width, + (int)height, + HWND.NULL, + HMENU.NULL, + hInstance, + (void*)GCHandle.ToIntPtr(GCHandle.Alloc(application)) + ); + } + + windowSize = new Size((int)height, (int)width); + + // Initialize the application + application.OnInitialize(windowSize, hwnd); + + // Display the window + _ = FX.ShowWindow(hwnd, FX.SW_SHOWDEFAULT); + + // Start the timer + stopwatch = Stopwatch.StartNew(); + + MSG msg = default; + + // Process any messages in the queue + while (msg.message != FX.WM_QUIT) + { + if (FX.PeekMessageW(&msg, HWND.NULL, 0, 0, FX.PM_REMOVE) != 0) + { + _ = FX.TranslateMessage(&msg); + _ = FX.DispatchMessageW(&msg); + } + else if (!isPaused || true) + { + RunApp(); + } + else + { + Thread.Sleep(100); + } + } + + application.Dispose(); + + // Return this part of the WM_QUIT message to Windows + return (int)msg.wParam; + } + + /// + /// The application state update method that is invoked within the main application loop. + /// + private static void RunApp() + { + // Update window title approx every second + if (stopwatch.ElapsedMilliseconds - elapsedMillisecondsForLastTitleUpdate >= 1000) + { + string title = isPaused switch + { + true => $"{application.Title} - Paused", + false => $"{application.Title} - FPS: {elapsedFramesSinceLastTitleUpdate}" + }; + + fixed (char* pTitle = title) + { + _ = FX.SetWindowTextW(hwnd, (ushort*)pTitle); + } + + elapsedMillisecondsForLastTitleUpdate = stopwatch.ElapsedMilliseconds; + elapsedFramesSinceLastTitleUpdate = 0; + } + + // Update the application state + application.OnUpdate(stopwatch.Elapsed); + } + + /// + /// Processes incoming messages for a window. + /// + /// A handle to the window. + /// The message. + /// Additional message information (the contents depend on the value of ). + /// Additional message information (the contents depend on the value of ). + /// The result of the message processing for the input message. + [UnmanagedCallersOnly] + private static nint WindowProc(IntPtr hwnd, uint uMsg, nuint wParam, nint lParam) + { + switch (uMsg) + { + // Change the paused state on window activation + case FX.WM_ACTIVATE: + { + if (FX.LOWORD(wParam) == FX.WA_INACTIVE) + { + isPaused = true; + } + else + { + isPaused = false; + } + return 0; + } + + // Change the paused state when ESC is pressed + case FX.WM_KEYDOWN: + { + if ((ConsoleKey)wParam == ConsoleKey.Escape) + { + + if (isPaused) + { + FX.SetCapture(Win32ApplicationRunner.hwnd); + } + else + { + FX.ReleaseCapture(); + } + + isPaused = !isPaused; + } + + return 0; + } + + // Window resize started + case FX.WM_ENTERSIZEMOVE: + { + isResizing = true; + + return 0; + } + + // Window resize completed + case FX.WM_EXITSIZEMOVE: + { + isResizing = false; + application.OnResize(windowSize); + + return 0; + } + case FX.WM_SIZE: + { + uint size = (uint)lParam; + + windowSize = new Size(FX.LOWORD(size), FX.HIWORD(size)); + + if (!isResizing && wParam != FX.SIZE_MINIMIZED) + { + application.OnResize(windowSize); + } + + return 0; + } + + // Shutdown + case FX.WM_DESTROY: + { + FX.PostQuitMessage(0); + return 0; + } + } + + return FX.DefWindowProcW(hwnd, uMsg, wParam, lParam); + } + } +} \ No newline at end of file