Merge pull request #189 from Sergio0694/feature/shader-panel-improvements

ShaderPanel improvements
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-01-06 02:17:29 +01:00
コミット 3ac903e241
5ファイル変更
+94 -72

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

+3 -1
ファイルの表示
@@ -23,7 +23,7 @@ public sealed class ContouredLayersRunner : IShaderRunner
private ReadOnlyTexture2D<Rgba32, Float4>? texture;
/// <inheritdoc/>
public void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan, object? parameter)
public bool TryExecute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan, object? parameter)
{
if (this.texture is null)
{
@@ -33,5 +33,7 @@ public sealed class ContouredLayersRunner : IShaderRunner
}
GraphicsDevice.Default.ForEach(texture, new ContouredLayers((float)timespan.TotalSeconds, this.texture));
return true;
}
}
+34 -22
ファイルの表示
@@ -185,11 +185,14 @@ partial class SwapChainManager<TOwner>
return;
}
// Start the initial frame separately, before the timer starts. This ensures that
// resuming after a pause correctly renders the first frame at the right time.
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
// Start the initial frame separately, before the timer starts. This ensures that
// resuming after a pause correctly renders the first frame at the right time.
if (OnUpdate(renderStopwatch.Elapsed, parameter))
{
OnPresent();
}
renderStopwatch.Start();
@@ -202,8 +205,11 @@ partial class SwapChainManager<TOwner>
}
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
if (OnUpdate(renderStopwatch.Elapsed, parameter))
{
OnPresent();
}
}
renderStopwatch.Stop();
@@ -215,41 +221,46 @@ partial class SwapChainManager<TOwner>
private void RenderLoopWithDynamicResolution()
{
Stopwatch renderStopwatch = this.renderStopwatch ??= new();
Stopwatch frameStopwatch = new();
CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token;
DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher);
if (!OnFrameRequest(out object? parameter, cancellationToken))
{
return;
}
Stopwatch frameStopwatch = Stopwatch.StartNew();
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
if (OnUpdate(renderStopwatch.Elapsed, parameter))
{
OnPresent();
}
renderStopwatch.Start();
while (!cancellationToken.IsCancellationRequested)
{
// Evaluate the dynamic resolution frame time step, if the mode is enabled
if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale))
{
this.isResizePending = true;
}
if (!OnFrameRequest(out parameter, cancellationToken))
{
return;
}
OnResize();
frameStopwatch.Restart();
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
if (OnUpdate(renderStopwatch.Elapsed, parameter))
{
OnPresent();
// Evaluate the dynamic resolution frame time step
if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale))
{
this.isResizePending = true;
}
}
}
renderStopwatch.Stop();
@@ -309,9 +320,10 @@ partial class SwapChainManager<TOwner>
/// </summary>
/// <param name="time">The current time since the start of the application.</param>
/// <param name="parameter">The input parameter for the frame being rendered.</param>
private void OnUpdate(TimeSpan time, object? parameter)
/// <returns>Whether or not to present the resulting frame.</returns>
private bool OnUpdate(TimeSpan time, object? parameter)
{
this.shaderRunner!.Execute(this.texture!, time, parameter);
return this.shaderRunner!.TryExecute(this.texture!, time, parameter);
}
/// <summary>
+49 -45
ファイルの表示
@@ -263,8 +263,8 @@ internal sealed unsafe partial class SwapChainManager<TOwner> : NativeObject
dxgiSwapChainDesc1.BufferCount = 2;
dxgiSwapChainDesc1.Flags = (uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM;
dxgiSwapChainDesc1.Width = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0);
dxgiSwapChainDesc1.Height = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0);
dxgiSwapChainDesc1.Width = 1;
dxgiSwapChainDesc1.Height = 1;
dxgiSwapChainDesc1.SampleDesc = new DXGI_SAMPLE_DESC(count: 1, quality: 0);
dxgiSwapChainDesc1.Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH;
dxgiSwapChainDesc1.Stereo = 0;
@@ -323,55 +323,59 @@ internal sealed unsafe partial class SwapChainManager<TOwner> : NativeObject
}
/// <summary>
/// Applies the actual resize logic that was scheduled from <see cref="OnResize"/>.
/// Applies the actual resize logic that was scheduled from <see cref="OnResize"/>, if needed.
/// </summary>
private unsafe void ApplyResize()
{
ulong updatedFenceValue = ++this.nextD3D12FenceValue;
uint resizedWidth = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0);
uint resizeHeight = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0);
this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), updatedFenceValue).Assert();
// Wait for the fence again to ensure there are no pending operations
this.d3D12Fence.Get()->SetEventOnCompletion(updatedFenceValue, default).Assert();
// Dispose the old buffers before resizing the buffer
this.d3D12Resource0.Dispose();
this.d3D12Resource1.Dispose();
// Resize the swap chain buffers
this.dxgiSwapChain3.Get()->ResizeBuffers(
2,
(uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0),
(uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0),
DXGI_FORMAT.DXGI_FORMAT_UNKNOWN,
(uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT).Assert();
// Apply the necessary scale transform
DXGI_MATRIX_3X2_F transformMatrix = default;
transformMatrix._11 = (1 / this.compositionScaleX) * (1 / this.targetResolutionScale);
transformMatrix._22 = (1 / this.compositionScaleY) * (1 / this.targetResolutionScale);
this.dxgiSwapChain3.Get()->SetMatrixTransform(&transformMatrix).Assert();
// Retrieve the back buffers for the swap chain
fixed (ID3D12Resource** d3D12Resource0 = this.d3D12Resource0)
fixed (ID3D12Resource** d3D12Resource1 = this.d3D12Resource1)
if (this.texture is null ||
this.texture.Width != resizedWidth ||
this.texture.Height != resizeHeight)
{
this.dxgiSwapChain3.Get()->GetBuffer(0, Win32.__uuidof<ID3D12Resource>(), (void**)d3D12Resource0).Assert();
this.dxgiSwapChain3.Get()->GetBuffer(1, Win32.__uuidof<ID3D12Resource>(), (void**)d3D12Resource1).Assert();
ulong updatedFenceValue = ++this.nextD3D12FenceValue;
this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), updatedFenceValue).Assert();
// Wait for the fence again to ensure there are no pending operations
this.d3D12Fence.Get()->SetEventOnCompletion(updatedFenceValue, default).Assert();
// Dispose the old buffers before resizing the buffer
this.d3D12Resource0.Dispose();
this.d3D12Resource1.Dispose();
// Resize the swap chain buffers
this.dxgiSwapChain3.Get()->ResizeBuffers(
2,
resizedWidth,
resizeHeight,
DXGI_FORMAT.DXGI_FORMAT_UNKNOWN,
(uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT).Assert();
// Apply the necessary scale transform
DXGI_MATRIX_3X2_F transformMatrix = default;
transformMatrix._11 = (1 / this.compositionScaleX) * (1 / this.targetResolutionScale);
transformMatrix._22 = (1 / this.compositionScaleY) * (1 / this.targetResolutionScale);
this.dxgiSwapChain3.Get()->SetMatrixTransform(&transformMatrix).Assert();
// Retrieve the back buffers for the swap chain
fixed (ID3D12Resource** d3D12Resource0 = this.d3D12Resource0)
fixed (ID3D12Resource** d3D12Resource1 = this.d3D12Resource1)
{
this.dxgiSwapChain3.Get()->GetBuffer(0, Win32.__uuidof<ID3D12Resource>(), (void**)d3D12Resource0).Assert();
this.dxgiSwapChain3.Get()->GetBuffer(1, Win32.__uuidof<ID3D12Resource>(), (void**)d3D12Resource1).Assert();
}
// Get the index of the initial back buffer
this.currentBufferIndex = this.dxgiSwapChain3.Get()->GetCurrentBackBufferIndex();
// Dispose the previous texture, if present, and create the new 2D texture to use to generate frames to display
this.texture?.Dispose();
this.texture = GraphicsDevice.Default.AllocateReadWriteTexture2D<Rgba32, Float4>((int)resizedWidth, (int)resizeHeight);
}
// Get the index of the initial back buffer
this.currentBufferIndex = this.dxgiSwapChain3.Get()->GetCurrentBackBufferIndex();
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.Default.AllocateReadWriteTexture2D<Rgba32, Float4>(
(int)d3D12Resource0Description.Width,
(int)d3D12Resource0Description.Height);
}
/// <inheritdoc/>
+3 -2
ファイルの表示
@@ -12,10 +12,11 @@ namespace ComputeSharp.WinUI;
public interface IShaderRunner
{
/// <summary>
/// Renders a single frame to a texture.
/// Tries to render a single frame to a texture, optionally skipping the frame if needed.
/// </summary>
/// <param name="texture">The target texture to render the frame to.</param>
/// <param name="timespan">The timespan for the current frame.</param>
/// <param name="parameter">The input parameter for the frame being rendered.</param>
void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan, object? parameter);
/// <returns>Whether or not to present the current frame. If <see langword="false"/>, the frame will be skipped.</returns>
bool TryExecute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan, object? parameter);
}
+5 -2
ファイルの表示
@@ -8,7 +8,8 @@ namespace ComputeSharp.WinUI;
#endif
/// <summary>
/// An <see cref="IShaderRunner"/> implementation powered by a supplied shader type.
/// A simple <see cref="IShaderRunner"/> implementation powered by a supplied shader type.
/// All rendered frames produced by this type will always be presented.
/// </summary>
/// <typeparam name="T">The type of shader to use to render frames.</typeparam>
public sealed class ShaderRunner<T> : IShaderRunner
@@ -56,7 +57,7 @@ public sealed class ShaderRunner<T> : IShaderRunner
}
/// <inheritdoc/>
public void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan time, object? parameter)
public bool TryExecute(IReadWriteTexture2D<Float4> texture, TimeSpan time, object? parameter)
{
if (this.shaderFactory is Func<TimeSpan, T> shaderFactory)
{
@@ -66,5 +67,7 @@ public sealed class ShaderRunner<T> : IShaderRunner
{
GraphicsDevice.Default.ForEach(texture, this.statefulShaderFactory!(time, parameter));
}
return true;
}
}