参照されないプロジェクトを削除しビルド構成を整理
このコミットが含まれているのは:
1 親
902d1a50a3
コミット
67af7b667b
588ファイル変更
+3
-56403
検索条件に一致するファイルはありません
@@ -1,300 +0,0 @@
|
||||
using System;
|
||||
using ComputeSharp.Interop;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.SwapChain.Backend;
|
||||
|
||||
/// <summary>
|
||||
/// A class representing a simple Win32 application.
|
||||
/// </summary>
|
||||
/// <param name="shaderCallback">The function pointer used to run a shader on a target texture.</param>
|
||||
internal sealed unsafe class Win32Application(delegate*<IReadWriteNormalizedTexture2D<float4>, TimeSpan, void> shaderCallback)
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Device"/> pointer for the device currently in use.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12Device> d3D12Device;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12CommandQueue"/> instance to use for graphics operations.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12CommandQueue> d3D12CommandQueue;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Fence"/> instance used for graphics operations.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12Fence> d3D12Fence;
|
||||
|
||||
/// <summary>
|
||||
/// The next fence value for graphics operations using <see cref="d3D12CommandQueue"/>.
|
||||
/// </summary>
|
||||
private ulong nextD3D12FenceValue = 1;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12CommandAllocator"/> object to create command lists.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12CommandAllocator> d3D12CommandAllocator;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12GraphicsCommandList"/> instance used to copy data to the back buffers.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12GraphicsCommandList> d3D12GraphicsCommandList;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IDXGISwapChain1"/> instance used to display content onto the target window.
|
||||
/// </summary>
|
||||
private ComPtr<IDXGISwapChain1> dxgiSwapChain1;
|
||||
|
||||
/// <summary>
|
||||
/// The first buffer within <see cref="dxgiSwapChain1"/>.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12Resource> d3D12Resource0;
|
||||
|
||||
/// <summary>
|
||||
/// The second buffer within <see cref="dxgiSwapChain1"/>.
|
||||
/// </summary>
|
||||
private ComPtr<ID3D12Resource> d3D12Resource1;
|
||||
|
||||
/// <summary>
|
||||
/// The index of the next buffer that can be used to present content.
|
||||
/// </summary>
|
||||
private uint currentBufferIndex;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ReadWriteTexture2D{T, TPixel}"/> instance used to prepare frames to display.
|
||||
/// </summary>
|
||||
private ReadWriteTexture2D<Rgba32, float4>? texture;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not the window has been resized and requires the buffers to be updated.
|
||||
/// </summary>
|
||||
private volatile bool isResizePending;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the current application.
|
||||
/// </summary>
|
||||
/// <param name="hwnd">The handle for the window.</param>
|
||||
public void OnInitialize(HWND hwnd)
|
||||
{
|
||||
// Get the underlying ID3D12Device in use
|
||||
fixed (ID3D12Device** d3D12Device = this.d3D12Device)
|
||||
{
|
||||
_ = InteropServices.TryGetID3D12Device(GraphicsDevice.GetDefault(), Windows.__uuidof<ID3D12Device>(), (void**)d3D12Device);
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
_ = this.d3D12Device.Get()->CreateCommandQueue(
|
||||
&d3D12CommandQueueDesc,
|
||||
Windows.__uuidof<ID3D12CommandQueue>(),
|
||||
(void**)d3D12CommandQueue);
|
||||
}
|
||||
|
||||
// Create the direct fence
|
||||
fixed (ID3D12Fence** d3D12Fence = this.d3D12Fence)
|
||||
{
|
||||
_ = this.d3D12Device.Get()->CreateFence(
|
||||
0,
|
||||
D3D12_FENCE_FLAGS.D3D12_FENCE_FLAG_NONE,
|
||||
Windows.__uuidof<ID3D12Fence>(),
|
||||
(void**)d3D12Fence);
|
||||
}
|
||||
|
||||
// Create the swap chain to display frames
|
||||
fixed (IDXGISwapChain1** dxgiSwapChain1 = this.dxgiSwapChain1)
|
||||
{
|
||||
using ComPtr<IDXGIFactory2> dxgiFactory2 = default;
|
||||
|
||||
_ = DirectX.CreateDXGIFactory2(DXGI.DXGI_CREATE_FACTORY_DEBUG, Windows.__uuidof<IDXGIFactory2>(), (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_R8G8B8A8_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*)this.d3D12CommandQueue.Get(),
|
||||
hwnd,
|
||||
&dxgiSwapChainDesc1,
|
||||
null,
|
||||
null,
|
||||
dxgiSwapChain1);
|
||||
}
|
||||
|
||||
// Create the command allocator to use
|
||||
fixed (ID3D12CommandAllocator** d3D12CommandAllocator = this.d3D12CommandAllocator)
|
||||
{
|
||||
_ = this.d3D12Device.Get()->CreateCommandAllocator(
|
||||
D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
Windows.__uuidof<ID3D12CommandAllocator>(),
|
||||
(void**)d3D12CommandAllocator);
|
||||
}
|
||||
|
||||
// Create the reusable command list to copy data to the back buffers
|
||||
fixed (ID3D12GraphicsCommandList** d3D12GraphicsCommandList = this.d3D12GraphicsCommandList)
|
||||
{
|
||||
_ = this.d3D12Device.Get()->CreateCommandList(
|
||||
0,
|
||||
D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
this.d3D12CommandAllocator,
|
||||
null,
|
||||
Windows.__uuidof<ID3D12GraphicsCommandList>(),
|
||||
(void**)d3D12GraphicsCommandList);
|
||||
}
|
||||
|
||||
// Close the command list to prepare it for future use
|
||||
_ = this.d3D12GraphicsCommandList.Get()->Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resizes the current application.
|
||||
/// </summary>
|
||||
public void OnResize()
|
||||
{
|
||||
this.isResizePending = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the actual resize logic that was scheduled from <see cref="OnResize"/>.
|
||||
/// </summary>
|
||||
private void ApplyResize()
|
||||
{
|
||||
_ = this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue);
|
||||
|
||||
// Wait for the fence again to ensure there are no pending operations
|
||||
_ = this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default);
|
||||
|
||||
this.nextD3D12FenceValue++;
|
||||
|
||||
// Dispose the old buffers before resizing the buffer
|
||||
this.d3D12Resource0.Dispose();
|
||||
this.d3D12Resource1.Dispose();
|
||||
|
||||
// Resize the swap chain buffers
|
||||
_ = this.dxgiSwapChain1.Get()->ResizeBuffers(0, 0, 0, DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, 0);
|
||||
|
||||
// Get the index of the initial back buffer
|
||||
using (ComPtr<IDXGISwapChain3> dxgiSwapChain3 = default)
|
||||
{
|
||||
_ = this.dxgiSwapChain1.CopyTo(dxgiSwapChain3.GetAddressOf());
|
||||
|
||||
this.currentBufferIndex = dxgiSwapChain3.Get()->GetCurrentBackBufferIndex();
|
||||
}
|
||||
|
||||
// Retrieve the back buffers for the swap chain
|
||||
fixed (ID3D12Resource** d3D12Resource0 = this.d3D12Resource0)
|
||||
fixed (ID3D12Resource** d3D12Resource1 = this.d3D12Resource1)
|
||||
{
|
||||
_ = this.dxgiSwapChain1.Get()->GetBuffer(0, Windows.__uuidof<ID3D12Resource>(), (void**)d3D12Resource0);
|
||||
_ = this.dxgiSwapChain1.Get()->GetBuffer(1, Windows.__uuidof<ID3D12Resource>(), (void**)d3D12Resource1);
|
||||
}
|
||||
|
||||
this.texture?.Dispose();
|
||||
|
||||
D3D12_RESOURCE_DESC d3D12Resource0Description = this.d3D12Resource0.Get()->GetDesc();
|
||||
|
||||
// Create the 2D texture to use to generate frames to display
|
||||
this.texture = GraphicsDevice.GetDefault().AllocateReadWriteTexture2D<Rgba32, float4>(
|
||||
(int)d3D12Resource0Description.Width,
|
||||
(int)d3D12Resource0Description.Height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the current application.
|
||||
/// </summary>
|
||||
/// <param name="time">The current time since the start of the application.</param>
|
||||
public void OnUpdate(TimeSpan time)
|
||||
{
|
||||
if (this.isResizePending)
|
||||
{
|
||||
ApplyResize();
|
||||
|
||||
this.isResizePending = false;
|
||||
}
|
||||
|
||||
// Generate the new frame by invoking the callback
|
||||
shaderCallback(this.texture!, time);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
// Get the underlying ID3D12Resource pointer for the texture
|
||||
_ = InteropServices.TryGetID3D12Resource(this.texture!, Windows.__uuidof<ID3D12Resource>(), (void**)d3D12Resource.GetAddressOf());
|
||||
|
||||
// Get the target back buffer to update
|
||||
ID3D12Resource* d3D12ResourceBackBuffer = this.currentBufferIndex switch
|
||||
{
|
||||
0 => this.d3D12Resource0.Get(),
|
||||
1 => this.d3D12Resource1.Get(),
|
||||
_ => null
|
||||
};
|
||||
|
||||
this.currentBufferIndex ^= 1;
|
||||
|
||||
// Reset the command list and command allocator
|
||||
_ = this.d3D12CommandAllocator.Get()->Reset();
|
||||
_ = this.d3D12GraphicsCommandList.Get()->Reset(this.d3D12CommandAllocator.Get(), null);
|
||||
|
||||
D3D12_RESOURCE_BARRIER* d3D12ResourceBarriers = stackalloc D3D12_RESOURCE_BARRIER[]
|
||||
{
|
||||
D3D12_RESOURCE_BARRIER.InitTransition(
|
||||
d3D12Resource.Get(),
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE),
|
||||
D3D12_RESOURCE_BARRIER.InitTransition(
|
||||
d3D12ResourceBackBuffer,
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON,
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST)
|
||||
};
|
||||
|
||||
// Transition the resources to COPY_DEST and COPY_SOURCE respectively
|
||||
this.d3D12GraphicsCommandList.Get()->ResourceBarrier(2, d3D12ResourceBarriers);
|
||||
|
||||
// Copy the generated frame to the target back buffer
|
||||
this.d3D12GraphicsCommandList.Get()->CopyResource(d3D12ResourceBackBuffer, d3D12Resource.Get());
|
||||
|
||||
d3D12ResourceBarriers[0] = D3D12_RESOURCE_BARRIER.InitTransition(
|
||||
d3D12Resource.Get(),
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
|
||||
|
||||
d3D12ResourceBarriers[1] = D3D12_RESOURCE_BARRIER.InitTransition(
|
||||
d3D12ResourceBackBuffer,
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST,
|
||||
D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON);
|
||||
|
||||
// Transition the resources back to COMMON and UNORDERED_ACCESS respectively
|
||||
this.d3D12GraphicsCommandList.Get()->ResourceBarrier(2, d3D12ResourceBarriers);
|
||||
|
||||
_ = this.d3D12GraphicsCommandList.Get()->Close();
|
||||
|
||||
// Execute the command list to perform the copy
|
||||
this.d3D12CommandQueue.Get()->ExecuteCommandLists(1, (ID3D12CommandList**)this.d3D12GraphicsCommandList.GetAddressOf());
|
||||
|
||||
_ = this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue);
|
||||
|
||||
// Present the new frame
|
||||
_ = this.dxgiSwapChain1.Get()->Present(0, 0);
|
||||
|
||||
if (this.nextD3D12FenceValue > this.d3D12Fence.Get()->GetCompletedValue())
|
||||
{
|
||||
_ = this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default);
|
||||
}
|
||||
|
||||
this.nextD3D12FenceValue++;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using System;
|
||||
using ComputeSharp.Descriptors;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.SwapChain.Backend;
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Win32Application"/> factory type that renders an animated shader on a swapchain on a target <see cref="HWND"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The shader type to render.</typeparam>
|
||||
/// <remarks>
|
||||
/// The main point of this type is to hide the complexity of <see cref="Win32Application"/> from the non-NativeAOT sample.
|
||||
/// </remarks>
|
||||
internal static class Win32ApplicationFactory<T>
|
||||
where T : struct, IComputeShader<float4>, IComputeShaderDescriptor<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="Func{T1, TResult}"/> instance used to create shaders to run.
|
||||
/// </summary>
|
||||
private static Func<TimeSpan, T>? shaderFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Win32Application"/> from the specified factory.
|
||||
/// </summary>
|
||||
/// <param name="shaderFactory">The <see cref="Func{T1, TResult}"/> instance used to create shaders to run.</param>
|
||||
/// <returns>A <see cref="Win32Application"/> instance to use.</returns>
|
||||
public static unsafe Win32Application Create(Func<TimeSpan, T> shaderFactory)
|
||||
{
|
||||
Win32ApplicationFactory<T>.shaderFactory = shaderFactory;
|
||||
|
||||
return new(&ShaderCallback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The callback function to draw a single frame.
|
||||
/// </summary>
|
||||
/// <param name="texture">The input <see cref="IReadOnlyNormalizedTexture2D{TPixel}"/> instance to draw on.</param>
|
||||
/// <param name="time">The time for the current frame.</param>
|
||||
private static void ShaderCallback(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time)
|
||||
{
|
||||
texture.GraphicsDevice.ForEach(texture, shaderFactory!(time));
|
||||
}
|
||||
}
|
||||
@@ -1,350 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.SwapChain.Backend;
|
||||
|
||||
/// <summary>
|
||||
/// A helper class to manage the creation and execution of Win32 applications.
|
||||
/// </summary>
|
||||
internal static unsafe class Win32ApplicationRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether or not a resize operation is in progress.
|
||||
/// </summary>
|
||||
private static bool isResizing = false;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not the application is currently paused.
|
||||
/// </summary>
|
||||
private static bool isPaused = false;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="HWND"/> for the application window.
|
||||
/// </summary>
|
||||
private static HWND hwnd;
|
||||
|
||||
/// <summary>
|
||||
/// The application being run.
|
||||
/// </summary>
|
||||
private static Win32Application application = null!;
|
||||
|
||||
/// <summary>
|
||||
/// The thread running the render loop.
|
||||
/// </summary>
|
||||
private static Thread renderThread = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Runs a specified application and starts the main loop to update its state. This is the entry point for a given application,
|
||||
/// and it should be called as soon as the process is launched, excluding any other additional initialization needed.
|
||||
/// </summary>
|
||||
/// <param name="application">The input application instance to run.</param>
|
||||
/// <param name="applicationName">Name of the input application instance to run.</param>
|
||||
/// <param name="windowTitle">The title to use for the window being opened.</param>
|
||||
/// <returns>The exit code for the application.</returns>
|
||||
public static int Run(Win32Application application, string applicationName, string windowTitle)
|
||||
{
|
||||
Win32ApplicationRunner.application = application;
|
||||
|
||||
HMODULE hInstance = Windows.GetModuleHandleW(null);
|
||||
|
||||
fixed (char* name = applicationName)
|
||||
fixed (char* title = windowTitle)
|
||||
{
|
||||
// Initialize the window class
|
||||
WNDCLASSEXW windowClassEx = new()
|
||||
{
|
||||
cbSize = (uint)sizeof(WNDCLASSEXW),
|
||||
style = CS.CS_HREDRAW | CS.CS_VREDRAW,
|
||||
lpfnWndProc = &WindowProc,
|
||||
hInstance = hInstance,
|
||||
hCursor = Windows.LoadCursorW(HINSTANCE.NULL, IDC.IDC_ARROW),
|
||||
lpszClassName = name
|
||||
};
|
||||
|
||||
// Register the window class
|
||||
_ = Windows.RegisterClassExW(&windowClassEx);
|
||||
|
||||
Rectangle windowRect = new(0, 0, 1280, 720);
|
||||
|
||||
// Set the target window size
|
||||
_ = Windows.AdjustWindowRect((RECT*)&windowRect, WS.WS_OVERLAPPEDWINDOW, Windows.FALSE);
|
||||
|
||||
uint height = (uint)(windowRect.Bottom - windowRect.Top);
|
||||
uint width = (uint)(windowRect.Right - windowRect.Left);
|
||||
|
||||
// Create the window and store a handle to it
|
||||
hwnd = Windows.CreateWindowExW(
|
||||
0,
|
||||
windowClassEx.lpszClassName,
|
||||
title,
|
||||
WS.WS_OVERLAPPEDWINDOW,
|
||||
Windows.CW_USEDEFAULT,
|
||||
Windows.CW_USEDEFAULT,
|
||||
(int)width,
|
||||
(int)height,
|
||||
HWND.NULL,
|
||||
HMENU.NULL,
|
||||
hInstance,
|
||||
(void*)GCHandle.ToIntPtr(GCHandle.Alloc(application))
|
||||
);
|
||||
|
||||
MARGINS margins = default;
|
||||
margins.cxLeftWidth = -1;
|
||||
margins.cxRightWidth = -1;
|
||||
margins.cyTopHeight = -1;
|
||||
margins.cyBottomHeight = -1;
|
||||
|
||||
_ = Windows.DwmExtendFrameIntoClientArea(hwnd, &margins);
|
||||
}
|
||||
|
||||
// Initialize the application
|
||||
application.OnInitialize(hwnd);
|
||||
|
||||
// Display the window
|
||||
_ = Windows.ShowWindow(hwnd, SW.SW_SHOWDEFAULT);
|
||||
|
||||
MSG msg = default;
|
||||
|
||||
// Setup the render thread that enables smooth resizing of the window
|
||||
renderThread = new Thread(static args =>
|
||||
{
|
||||
(Win32Application application, CancellationToken token) = ((Win32Application, CancellationToken))args!;
|
||||
|
||||
Stopwatch startStopwatch = Stopwatch.StartNew();
|
||||
Stopwatch frameStopwatch = Stopwatch.StartNew();
|
||||
|
||||
const long targetFrameTimeInTicksFor60fps = 166666;
|
||||
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
if (frameStopwatch.ElapsedTicks >= targetFrameTimeInTicksFor60fps)
|
||||
{
|
||||
frameStopwatch.Restart();
|
||||
|
||||
application.OnUpdate(startStopwatch.Elapsed);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
CancellationTokenSource tokenSource = new();
|
||||
|
||||
renderThread.Start((application, tokenSource.Token));
|
||||
|
||||
// Process any messages in the queue
|
||||
while (msg.message != WM.WM_QUIT)
|
||||
{
|
||||
if (Windows.PeekMessageW(&msg, HWND.NULL, 0, 0, PM.PM_REMOVE) != 0)
|
||||
{
|
||||
_ = Windows.TranslateMessage(&msg);
|
||||
_ = Windows.DispatchMessageW(&msg);
|
||||
}
|
||||
else if (isPaused)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
|
||||
// Also listen to Escape and 'Q' to quit
|
||||
if (msg.message == WM.WM_KEYUP && (msg.wParam == (WPARAM)VK.VK_ESCAPE || msg.wParam == 'Q'))
|
||||
{
|
||||
_ = Windows.DestroyWindow(hwnd);
|
||||
}
|
||||
}
|
||||
|
||||
tokenSource.Cancel();
|
||||
|
||||
renderThread.Join();
|
||||
|
||||
// Return this part of the WM_QUIT message to Windows
|
||||
return (int)msg.wParam;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes incoming messages for a window.
|
||||
/// </summary>
|
||||
/// <param name="hwnd">A handle to the window.</param>
|
||||
/// <param name="uMsg">The message.</param>
|
||||
/// <param name="wParam">Additional message information (the contents depend on the value of <paramref name="uMsg"/>).</param>
|
||||
/// <param name="lParam">Additional message information (the contents depend on the value of <paramref name="uMsg"/>).</param>
|
||||
/// <returns>The result of the message processing for the input message.</returns>
|
||||
[UnmanagedCallersOnly]
|
||||
private static LRESULT WindowProc(HWND hwnd, uint uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
switch (uMsg)
|
||||
{
|
||||
// Change the paused state on window activation
|
||||
case WM.WM_ACTIVATE:
|
||||
{
|
||||
if (Windows.LOWORD(wParam) == Windows.WA_INACTIVE)
|
||||
{
|
||||
isPaused = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
isPaused = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Change the paused state when ESC is pressed
|
||||
case WM.WM_KEYDOWN:
|
||||
{
|
||||
if (wParam == (byte)ConsoleKey.Escape)
|
||||
{
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
_ = Windows.SetCapture(hwnd);
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = Windows.ReleaseCapture();
|
||||
}
|
||||
|
||||
isPaused = !isPaused;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Window resize started
|
||||
case WM.WM_ENTERSIZEMOVE:
|
||||
{
|
||||
isResizing = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Window resize completed
|
||||
case WM.WM_EXITSIZEMOVE:
|
||||
{
|
||||
isResizing = false;
|
||||
application.OnResize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Size update
|
||||
case WM.WM_SIZE:
|
||||
{
|
||||
if (!isResizing && wParam != Windows.SIZE_MINIMIZED)
|
||||
{
|
||||
application.OnResize();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Size and position of the window (needed to enable the borderless mode)
|
||||
case WM.WM_NCCALCSIZE:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Enable dragging the window
|
||||
case WM.WM_NCHITTEST:
|
||||
{
|
||||
POINT point;
|
||||
RECT rect;
|
||||
|
||||
_ = Windows.GetCursorPos(&point);
|
||||
_ = Windows.GetWindowRect(hwnd, &rect);
|
||||
|
||||
bool isAtTop = Math.Abs(point.y - rect.top) < 12;
|
||||
bool isAtRight = Math.Abs(point.x - rect.right) < 12;
|
||||
bool isAtBottom = Math.Abs(point.y - rect.bottom) < 12;
|
||||
bool isAtLeft = Math.Abs(point.x - rect.left) < 12;
|
||||
|
||||
if (isAtTop)
|
||||
{
|
||||
if (isAtRight)
|
||||
{
|
||||
return Windows.HTTOPRIGHT;
|
||||
}
|
||||
|
||||
if (isAtLeft)
|
||||
{
|
||||
return Windows.HTTOPLEFT;
|
||||
}
|
||||
|
||||
return Windows.HTTOP;
|
||||
}
|
||||
|
||||
if (isAtRight)
|
||||
{
|
||||
if (isAtTop)
|
||||
{
|
||||
return Windows.HTTOPRIGHT;
|
||||
}
|
||||
|
||||
if (isAtBottom)
|
||||
{
|
||||
return Windows.HTBOTTOMRIGHT;
|
||||
}
|
||||
|
||||
return Windows.HTRIGHT;
|
||||
}
|
||||
|
||||
if (isAtBottom)
|
||||
{
|
||||
if (isAtRight)
|
||||
{
|
||||
return Windows.HTBOTTOMRIGHT;
|
||||
}
|
||||
|
||||
if (isAtLeft)
|
||||
{
|
||||
return Windows.HTBOTTOMLEFT;
|
||||
}
|
||||
|
||||
return Windows.HTBOTTOM;
|
||||
}
|
||||
|
||||
if (isAtLeft)
|
||||
{
|
||||
if (isAtTop)
|
||||
{
|
||||
return Windows.HTTOPLEFT;
|
||||
}
|
||||
|
||||
if (isAtBottom)
|
||||
{
|
||||
return Windows.HTBOTTOMLEFT;
|
||||
}
|
||||
|
||||
return Windows.HTLEFT;
|
||||
}
|
||||
|
||||
return Windows.HTCAPTION;
|
||||
}
|
||||
|
||||
// Restore the drop shadow
|
||||
case WM.WM_DWMCOMPOSITIONCHANGED:
|
||||
{
|
||||
MARGINS margins = default;
|
||||
margins.cxLeftWidth = -1;
|
||||
margins.cxRightWidth = -1;
|
||||
margins.cyTopHeight = -1;
|
||||
margins.cyBottomHeight = -1;
|
||||
|
||||
_ = Windows.DwmExtendFrameIntoClientArea(hwnd, &margins);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Shutdown
|
||||
case WM.WM_DESTROY:
|
||||
{
|
||||
Windows.PostQuitMessage(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return Windows.DefWindowProcW(hwnd, uMsg, wParam, lParam);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Platforms>x64;ARM64</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.Core\ComputeSharp.Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
|
||||
<ProjectReference Include="..\..\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="contentfiles;build" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Textures\RustyMetal.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="..\ComputeSharp.SwapChain.Shaders\ComputeSharp.SwapChain.Shaders.projitems" Label="Shared" />
|
||||
</Project>
|
||||
@@ -1,80 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.SwapChain.Backend;
|
||||
using ComputeSharp.SwapChain.Shaders;
|
||||
|
||||
namespace ComputeSharp.SwapChain;
|
||||
|
||||
class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// A texture for <c>\Textures\RustyMetal.png</c>.
|
||||
/// </summary>
|
||||
private static readonly Lazy<IReadOnlyNormalizedTexture2D<float4>> RustyMetal = new(static () => LoadTexture());
|
||||
|
||||
static void Main()
|
||||
{
|
||||
// Prepare the mapping of available samples to choose from
|
||||
(string ShaderName, Win32Application Application)[] samples =
|
||||
[
|
||||
(nameof(HelloWorld), Win32ApplicationFactory<HelloWorld>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(FourColorGradient), Win32ApplicationFactory<FourColorGradient>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(ColorfulInfinity), Win32ApplicationFactory<ColorfulInfinity>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(FractalTiling), Win32ApplicationFactory<FractalTiling>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(TwoTiledTruchet), Win32ApplicationFactory<TwoTiledTruchet>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(MengerJourney), Win32ApplicationFactory<MengerJourney>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(Octagrams), Win32ApplicationFactory<Octagrams>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(ProteanClouds), Win32ApplicationFactory<ProteanClouds>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(ExtrudedTruchetPattern), Win32ApplicationFactory<ExtrudedTruchetPattern>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(PyramidPattern), Win32ApplicationFactory<PyramidPattern>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(TriangleGridContouring), Win32ApplicationFactory<TriangleGridContouring>.Create(static time => new((float)time.TotalSeconds))),
|
||||
(nameof(ContouredLayers), Win32ApplicationFactory<ContouredLayers>.Create(static time => new((float)time.TotalSeconds, RustyMetal.Value))),
|
||||
(nameof(TerracedHills), Win32ApplicationFactory<TerracedHills>.Create(static time => new((float)time.TotalSeconds))),
|
||||
];
|
||||
|
||||
int index;
|
||||
|
||||
do
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine("Available samples:");
|
||||
Console.WriteLine();
|
||||
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
Console.WriteLine($"{i}: {samples[i].ShaderName}");
|
||||
}
|
||||
|
||||
Console.WriteLine($"{samples.Length}+: Exit (Use Escape, 'Q', or Alt + F4 to exit a sample once chosen)");
|
||||
Console.WriteLine();
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write("Enter the index of the sample to run: ");
|
||||
}
|
||||
while (!int.TryParse(Console.ReadLine(), out index));
|
||||
|
||||
if (index >= 0 && index < samples.Length)
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Starting {samples[index].ShaderName}...");
|
||||
|
||||
_ = Win32ApplicationRunner.Run(samples[index].Application, "ComputeSharp.SwapChain", samples[index].ShaderName);
|
||||
}
|
||||
}
|
||||
while (index >= 0 && index < samples.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the specified texture data.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the texture to load.</param>
|
||||
/// <returns>A texture with the data from the image at the specified name.</returns>
|
||||
private static ReadOnlyTexture2D<Rgba32, float4> LoadTexture([CallerMemberName] string name = null!)
|
||||
{
|
||||
string filename = Path.Combine(AppContext.BaseDirectory, "Textures", $"{name}.png");
|
||||
|
||||
return GraphicsDevice.GetDefault().LoadReadOnlyTexture2D<Rgba32, float4>(filename);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする