Minor core refactoring

このコミットが含まれているのは:
Sergio Pedri committed 2021-11-20 00:27:57 +01:00
1 親 863e5abaf6
コミット 39765788cc
2ファイル変更
+86 -58

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

+78 -7
ファイルの表示
@@ -1,8 +1,11 @@
using System; using System;
using System.Diagnostics;
using System.Threading;
using ComputeSharp.Core.Extensions; using ComputeSharp.Core.Extensions;
using ComputeSharp.Interop; using ComputeSharp.Interop;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using TerraFX.Interop; using TerraFX.Interop;
using WinRT;
using FX = TerraFX.Interop.Windows; using FX = TerraFX.Interop.Windows;
namespace ComputeSharp.WinUI namespace ComputeSharp.WinUI
@@ -10,16 +13,16 @@ namespace ComputeSharp.WinUI
/// <inheritdoc cref="ComputeShaderPanel"/> /// <inheritdoc cref="ComputeShaderPanel"/>
public sealed partial class ComputeShaderPanel : SwapChainPanel public sealed partial class ComputeShaderPanel : SwapChainPanel
{ {
/// <summary>
/// The render thread in use, if any.
/// </summary>
private Thread? renderThread;
/// <summary> /// <summary>
/// The <see cref="IShaderRunner"/> instance used to create shaders to run. /// The <see cref="IShaderRunner"/> instance used to create shaders to run.
/// </summary> /// </summary>
private IShaderRunner? shaderRunner; private IShaderRunner? shaderRunner;
/// <summary>
/// The resolution scale used to render frames.
/// </summary>
private double resolutionScale;
/// <summary> /// <summary>
/// The <see cref="ID3D12Device"/> pointer for the device currently in use. /// The <see cref="ID3D12Device"/> pointer for the device currently in use.
/// </summary> /// </summary>
@@ -100,6 +103,16 @@ namespace ComputeSharp.WinUI
/// </summary> /// </summary>
private float compositionScaleY; private float compositionScaleY;
/// <summary>
/// The resolution scale used to render frames.
/// </summary>
private double resolutionScale;
/// <summary>
/// Indicates whether or not the rendering has been canceled.
/// </summary>
private bool isCancellationRequested;
/// <summary> /// <summary>
/// Initializes the current application. /// Initializes the current application.
/// </summary> /// </summary>
@@ -120,7 +133,7 @@ namespace ComputeSharp.WinUI
d3D12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE; d3D12CommandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAGS.D3D12_COMMAND_QUEUE_FLAG_NONE;
d3D12CommandQueueDesc.NodeMask = 0; d3D12CommandQueueDesc.NodeMask = 0;
d3D12Device.Get()->CreateCommandQueue( this.d3D12Device.Get()->CreateCommandQueue(
&d3D12CommandQueueDesc, &d3D12CommandQueueDesc,
FX.__uuidof<ID3D12CommandQueue>(), FX.__uuidof<ID3D12CommandQueue>(),
(void**)d3D12CommandQueue).Assert(); (void**)d3D12CommandQueue).Assert();
@@ -178,7 +191,7 @@ namespace ComputeSharp.WinUI
this.d3D12Device.Get()->CreateCommandList( this.d3D12Device.Get()->CreateCommandList(
0, 0,
D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT,
d3D12CommandAllocator, this.d3D12CommandAllocator,
null, null,
FX.__uuidof<ID3D12GraphicsCommandList>(), FX.__uuidof<ID3D12GraphicsCommandList>(),
(void**)d3D12GraphicsCommandList).Assert(); (void**)d3D12GraphicsCommandList).Assert();
@@ -186,6 +199,24 @@ namespace ComputeSharp.WinUI
// Close the command list to prepare it for future use // Close the command list to prepare it for future use
this.d3D12GraphicsCommandList.Get()->Close().Assert(); this.d3D12GraphicsCommandList.Get()->Close().Assert();
// Extract the ISwapChainPanelNative reference from the current panel, then query the
// IDXGISwapChain reference just created and set that as the swap chain panel to use.
using (ComPtr<ISwapChainPanelNative> swapChainPanelNative = default)
{
IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)this).NativeObject.ThisPtr;
Guid iSwapChainPanelNativeUuid = Guid.Parse("63AAD0B8-7C24-40FF-85A8-640D944CC325");
swapChainPanel->QueryInterface(
&iSwapChainPanelNativeUuid,
(void**)&swapChainPanelNative).Assert();
using ComPtr<IDXGISwapChain> idxgiSwapChain = default;
this.dxgiSwapChain1.CopyTo(&idxgiSwapChain).Assert();
swapChainPanelNative.Get()->SetSwapChain(idxgiSwapChain.Get()).Assert();
}
} }
/// <summary> /// <summary>
@@ -334,5 +365,45 @@ namespace ComputeSharp.WinUI
this.nextD3D12FenceValue++; this.nextD3D12FenceValue++;
} }
/// <summary>
/// Executes the render loop for the current panel.
/// </summary>
/// <remarks>This method assumes to be invoked from a background thread.</remarks>
private void OnStartRenderLoop()
{
static void ExecuteRenderLoop(ComputeShaderPanel @this)
{
Stopwatch
startStopwatch = Stopwatch.StartNew(),
frameStopwatch = Stopwatch.StartNew();
const long targetFrameTimeInTicksFor60fps = 166666;
@this.OnUpdate(TimeSpan.Zero);
while (!@this.isCancellationRequested)
{
if (frameStopwatch.ElapsedTicks >= targetFrameTimeInTicksFor60fps)
{
frameStopwatch.Restart();
@this.OnUpdate(startStopwatch.Elapsed);
}
}
}
this.isCancellationRequested = false;
this.renderThread = new Thread(static args => ExecuteRenderLoop((ComputeShaderPanel)args!));
this.renderThread.Start(this);
}
/// <summary>
/// Stops the current render loop, if one is running.
/// </summary>
private void OnStopRenderLoop()
{
this.isCancellationRequested = true;
}
} }
} }
+8 -51
ファイルの表示
@@ -1,11 +1,5 @@
using System; using Microsoft.UI.Xaml;
using System.Diagnostics;
using System.Threading;
using ComputeSharp.Core.Extensions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using TerraFX.Interop;
using WinRT;
namespace ComputeSharp.WinUI namespace ComputeSharp.WinUI
{ {
@@ -15,14 +9,13 @@ namespace ComputeSharp.WinUI
/// </summary> /// </summary>
public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel
{ {
private Thread? renderThread;
/// <summary> /// <summary>
/// Creates a new <see cref="ComputeShaderPanel"/> instance. /// Creates a new <see cref="ComputeShaderPanel"/> instance.
/// </summary> /// </summary>
public ComputeShaderPanel() public ComputeShaderPanel()
{ {
this.Loaded += ComputeShaderPanel_Loaded; this.Loaded += ComputeShaderPanel_Loaded;
this.Unloaded += ComputeShaderPanel_Unloaded;
this.SizeChanged += ComputeShaderPanel_SizeChanged; this.SizeChanged += ComputeShaderPanel_SizeChanged;
this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged; this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged;
} }
@@ -37,49 +30,13 @@ namespace ComputeSharp.WinUI
this.resolutionScale = ResolutionScale; this.resolutionScale = ResolutionScale;
OnInitialize(); OnInitialize();
OnStartRenderLoop();
}
// Extract the ISwapChainPanelNative reference from the current panel, then query the // Requests a cancellation for the render thread
// IDXGISwapChain reference just created and set that as the swap chain panel to use. private void ComputeShaderPanel_Unloaded(object sender, RoutedEventArgs e)
using (ComPtr<ISwapChainPanelNative> swapChainPanelNative = default) {
{ OnStopRenderLoop();
IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)this).NativeObject.ThisPtr;
Guid iSwapChainPanelNativeUuid = Guid.Parse("63AAD0B8-7C24-40FF-85A8-640D944CC325");
swapChainPanel->QueryInterface(
&iSwapChainPanelNativeUuid,
(void**)&swapChainPanelNative).Assert();
using ComPtr<IDXGISwapChain> idxgiSwapChain = default;
this.dxgiSwapChain1.CopyTo(&idxgiSwapChain).Assert();
swapChainPanelNative.Get()->SetSwapChain(idxgiSwapChain.Get()).Assert();
}
this.renderThread = new Thread(static args =>
{
var panel = (ComputeShaderPanel)args!;
Stopwatch
startStopwatch = Stopwatch.StartNew(),
frameStopwatch = Stopwatch.StartNew();
const long targetFrameTimeInTicksFor60fps = 166666;
panel.OnUpdate(TimeSpan.Zero);
while (true)
{
if (frameStopwatch.ElapsedTicks >= targetFrameTimeInTicksFor60fps)
{
frameStopwatch.Restart();
panel.OnUpdate(startStopwatch.Elapsed);
}
}
});
this.renderThread.Start(this);
} }
// Updates the background store for the frame size factors used by the render thread // Updates the background store for the frame size factors used by the render thread