Merge pull request #537 from Sergio0694/dev/remove-reified-generics-cli

Remove reified generic instantiations in CLI sample
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-06-26 10:49:26 +02:00
コミット 8b44e727bb
6ファイル変更
+384 -377

検索条件に一致するファイルはありません

-1
ファイルの表示
@@ -58,7 +58,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\ComputeSharp.SwapChain\Backend\SwapChainApplication{T}.cs" Link="Backend\SwapChainApplication{T}.cs" />
<Compile Include="..\ComputeSharp.SwapChain\Backend\Win32Application.cs" Link="Backend\Win32Application.cs" />
<Compile Include="..\ComputeSharp.SwapChain\Backend\Win32ApplicationRunner.cs" Link="Backend\Win32ApplicationRunner.cs" />
</ItemGroup>
+36 -30
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using ComputeSharp;
using ComputeSharp.SwapChain.Backend;
using ComputeSharp.SwapChain.Shaders;
using TerraFX.Interop.Windows;
@@ -14,44 +15,49 @@ if (args is [] or [_])
shaderName = nameof(ColorfulInfinity);
}
static HelloWorld HelloWorld(TimeSpan time) => new((float)time.TotalSeconds);
static ColorfulInfinity ColorfulInfinity(TimeSpan time) => new((float)time.TotalSeconds);
static FractalTiling FractalTiling(TimeSpan time) => new((float)time.TotalSeconds);
static TwoTiledTruchet TwoTiledTruchet(TimeSpan time) => new((float)time.TotalSeconds);
static MengerJourney MengerJourney(TimeSpan time) => new((float)time.TotalSeconds);
static Octagrams Octagrams(TimeSpan time) => new((float)time.TotalSeconds);
static ProteanClouds ProteanClouds(TimeSpan time) => new((float)time.TotalSeconds);
static ExtrudedTruchetPattern ExtrudedTruchetPattern(TimeSpan time) => new((float)time.TotalSeconds);
static PyramidPattern PyramidPattern(TimeSpan time) => new((float)time.TotalSeconds);
static TriangleGridContouring TriangleGridContouring(TimeSpan time) => new((float)time.TotalSeconds);
static TerracedHills TerracedHills(TimeSpan time) => new((float)time.TotalSeconds);
Win32Application? application;
// This CLI shader is heavily tuned for the smallest binary size possible when doing NativeAOT publish builds.
// As a result, the code is intentionally not really standard, and pulling all sorts of tricks to minimize the
// final binary size. These are callback functions to draw a shader, which are passed to the shared Win32 runner
// as function pointers. This is done to avoid the additional binary size increase due to delegate instantiations.
// Similarly, there is a single type hosting all rendering code to avoid per-shader reified generic instantiations.
static void _0(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new HelloWorld((float)time.TotalSeconds));
static void _1(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new ColorfulInfinity((float)time.TotalSeconds));
static void _2(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new FractalTiling((float)time.TotalSeconds));
static void _3(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new TwoTiledTruchet((float)time.TotalSeconds));
static void _4(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new MengerJourney((float)time.TotalSeconds));
static void _5(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new Octagrams((float)time.TotalSeconds));
static void _6(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new ProteanClouds((float)time.TotalSeconds));
static void _7(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new ExtrudedTruchetPattern((float)time.TotalSeconds));
static void _8(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new PyramidPattern((float)time.TotalSeconds));
static void _9(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new TriangleGridContouring((float)time.TotalSeconds));
static void _A(IReadWriteNormalizedTexture2D<float4> texture, TimeSpan time) => texture.GraphicsDevice.ForEach(texture, new TerracedHills((float)time.TotalSeconds));
unsafe
{
// Get the requested shader, if possible
application = shaderName.ToLowerInvariant() switch
delegate*<IReadWriteNormalizedTexture2D<float4>, TimeSpan, void> shaderCallback = shaderName.ToLowerInvariant() switch
{
"helloworld" => new SwapChainApplication<HelloWorld>(&HelloWorld),
"colorfulinfinity" => new SwapChainApplication<ColorfulInfinity>(&ColorfulInfinity),
"fractaltiling" => new SwapChainApplication<FractalTiling>(&FractalTiling),
"twotiledtruchet" => new SwapChainApplication<TwoTiledTruchet>(&TwoTiledTruchet),
"mengerjourney" => new SwapChainApplication<MengerJourney>(&MengerJourney),
"octagrams" => new SwapChainApplication<Octagrams>(&Octagrams),
"proteanclouds" => new SwapChainApplication<ProteanClouds>(&ProteanClouds),
"extrudedtruchetpattern" => new SwapChainApplication<ExtrudedTruchetPattern>(&ExtrudedTruchetPattern),
"pyramidpattern" => new SwapChainApplication<PyramidPattern>(&PyramidPattern),
"trianglegridcontouring" => new SwapChainApplication<TriangleGridContouring>(&TriangleGridContouring),
"terracedhills" => new SwapChainApplication<TerracedHills>(&TerracedHills),
"helloworld" => &_0,
"colorfulinfinity" => &_1,
"fractaltiling" => &_2,
"twotiledtruchet" => &_3,
"mengerjourney" => &_4,
"octagrams" => &_5,
"proteanclouds" => &_6,
"extrudedtruchetpattern" => &_7,
"pyramidpattern" => &_8,
"trianglegridcontouring" => &_9,
"terracedhills" => &_A,
_ => null
};
}
// If a shader is found, run it
if (application is not null)
{
return Win32ApplicationRunner.Run(application, "ComputeSharp", "ComputeSharp");
// Initialize the application instance and run it
if (shaderCallback is not null)
{
Win32Application? application = new(shaderCallback);
return Win32ApplicationRunner.Run(application, "ComputeSharp", "ComputeSharp");
}
}
}
-307
ファイルの表示
@@ -1,307 +0,0 @@
using System;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.SwapChain.Backend;
/// <summary>
/// A <see cref="Win32Application"/> 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>
internal sealed unsafe class SwapChainApplication<T> : Win32Application
where T : struct, IPixelShader<float4>
{
/// <summary>
/// The function pointer used to create shaders to run.
/// </summary>
private readonly delegate*<TimeSpan, T> shaderFactory;
/// <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>
/// Creates a new <see cref="SwapChainApplication{T}"/> instance with the specified parameters.
/// </summary>
/// <param name="shaderFactory">The function pointer used to create shaders to run.</param>
public SwapChainApplication(delegate*<TimeSpan, T> shaderFactory)
{
this.shaderFactory = shaderFactory;
}
/// <inheritdoc/>
public override unsafe 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();
}
/// <inheritdoc/>
public override unsafe void OnResize()
{
this.isResizePending = true;
}
/// <summary>
/// Applies the actual resize logic that was scheduled from <see cref="OnResize"/>.
/// </summary>
private unsafe 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);
}
/// <inheritdoc/>
public override unsafe void OnUpdate(TimeSpan time)
{
if (this.isResizePending)
{
ApplyResize();
this.isResizePending = false;
}
// Generate the new frame
GraphicsDevice.GetDefault().ForEach(this.texture!, this.shaderFactory(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++;
}
}
+290 -4
ファイルの表示
@@ -1,4 +1,6 @@
using System;
using ComputeSharp.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.SwapChain.Backend;
@@ -6,22 +8,306 @@ namespace ComputeSharp.SwapChain.Backend;
/// <summary>
/// A class representing a simple Win32 application.
/// </summary>
internal abstract class Win32Application
internal sealed unsafe class Win32Application
{
/// <summary>
/// The function pointer used to run a shader on a target texture.
/// </summary>
private readonly 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>
/// Creates a new <see cref="Win32Application"/> instance with the specified parameters.
/// </summary>
/// <param name="shaderCallback">The function pointer used to run a shader on a target texture.</param>
public Win32Application(delegate*<IReadWriteNormalizedTexture2D<float4>, TimeSpan, void> shaderCallback)
{
this.shaderCallback = shaderCallback;
}
/// <summary>
/// Initializes the current application.
/// </summary>
/// <param name="hwnd">The handle for the window.</param>
public abstract void OnInitialize(HWND hwnd);
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 abstract void OnResize();
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 abstract void OnUpdate(TimeSpan time);
public void OnUpdate(TimeSpan time)
{
if (this.isResizePending)
{
ApplyResize();
this.isResizePending = false;
}
// Generate the new frame by invoking the callback
this.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++;
}
}
+42
ファイルの表示
@@ -0,0 +1,42 @@
using System;
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, IPixelShader<float4>
{
/// <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));
}
}
+16 -35
ファイルの表示
@@ -15,42 +15,23 @@ class Program
static void Main()
{
static HelloWorld HelloWorld(TimeSpan time) => new((float)time.TotalSeconds);
static FourColorGradient FourColorGradient(TimeSpan time) => new((float)time.TotalSeconds);
static ColorfulInfinity ColorfulInfinity(TimeSpan time) => new((float)time.TotalSeconds);
static FractalTiling FractalTiling(TimeSpan time) => new((float)time.TotalSeconds);
static TwoTiledTruchet TwoTiledTruchet(TimeSpan time) => new((float)time.TotalSeconds);
static MengerJourney MengerJourney(TimeSpan time) => new((float)time.TotalSeconds);
static Octagrams Octagrams(TimeSpan time) => new((float)time.TotalSeconds);
static ProteanClouds ProteanClouds(TimeSpan time) => new((float)time.TotalSeconds);
static ExtrudedTruchetPattern ExtrudedTruchetPattern(TimeSpan time) => new((float)time.TotalSeconds);
static PyramidPattern PyramidPattern(TimeSpan time) => new((float)time.TotalSeconds);
static TriangleGridContouring TriangleGridContouring(TimeSpan time) => new((float)time.TotalSeconds);
static ContouredLayers ContouredLayers(TimeSpan time) => new((float)time.TotalSeconds, RustyMetal.Value);
static TerracedHills TerracedHills(TimeSpan time) => new((float)time.TotalSeconds);
(string ShaderName, Win32Application Application)[] samples;
unsafe
// Prepare the mapping of available samples to choose from
(string ShaderName, Win32Application Application)[] samples = new[]
{
// Prepare the mapping of available samples to choose from
samples = new (string, Win32Application)[]
{
(nameof(HelloWorld), new SwapChainApplication<HelloWorld>(&HelloWorld)),
(nameof(FourColorGradient), new SwapChainApplication<FourColorGradient>(&FourColorGradient)),
(nameof(ColorfulInfinity), new SwapChainApplication<ColorfulInfinity>(&ColorfulInfinity)),
(nameof(FractalTiling), new SwapChainApplication<FractalTiling>(&FractalTiling)),
(nameof(TwoTiledTruchet), new SwapChainApplication<TwoTiledTruchet>(&TwoTiledTruchet)),
(nameof(MengerJourney), new SwapChainApplication<MengerJourney>(&MengerJourney)),
(nameof(Octagrams), new SwapChainApplication<Octagrams>(&Octagrams)),
(nameof(ProteanClouds), new SwapChainApplication<ProteanClouds>(&ProteanClouds)),
(nameof(ExtrudedTruchetPattern), new SwapChainApplication<ExtrudedTruchetPattern>(&ExtrudedTruchetPattern)),
(nameof(PyramidPattern), new SwapChainApplication<PyramidPattern>(&PyramidPattern)),
(nameof(TriangleGridContouring), new SwapChainApplication<TriangleGridContouring>(&TriangleGridContouring)),
(nameof(ContouredLayers), new SwapChainApplication<ContouredLayers>(&ContouredLayers)),
(nameof(TerracedHills), new SwapChainApplication<TerracedHills>(&TerracedHills))
};
}
(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;