Skip to content

Commit

Permalink
[2D] - Cleaned up internal workings for the compositor system to be m…
Browse files Browse the repository at this point in the history
…ore efficient.
  • Loading branch information
Tape-Worm committed Aug 6, 2021
1 parent 3622bd5 commit 78a717e
Showing 1 changed file with 26 additions and 39 deletions.
65 changes: 26 additions & 39 deletions Gorgon/Gorgon.Renderers/Gorgon2D/Effects/Gorgon2DCompositor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class Gorgon2DCompositor
: IDisposable, IGorgonGraphicsObject, IReadOnlyList<IGorgon2DCompositorPass>
{
#region Variables.
// The final output for the scene.
private GorgonRenderTargetView _final;
// The ping render target in the ping-pong target scheme.
private GorgonRenderTarget2DView _pingTarget;
// The pong render target in the ping-pong target scheme.
Expand Down Expand Up @@ -119,30 +117,25 @@ public IGorgon2DCompositorPass this[string name]
#endregion

#region Methods.
/// <summary>
/// Function to determine if the resources need updating or not.
/// </summary>
/// <param name="outputTarget">The final render output target.</param>
/// <returns><b>true</b> if the resources need updating, or <b>false</b> if not.</returns>
private bool NeedsResourceUpdate(GorgonRenderTargetView outputTarget) => (_final is null)
|| (_pingTarget.Width != outputTarget.Width)
|| (_pingTarget.Height != outputTarget.Height)
|| (_pingTarget.Format != outputTarget.Format);

/// <summary>
/// Function to free any resources allocated by the compositor.
/// </summary>
private void FreeResources()
{
GorgonTexture2DView pingTexture = Interlocked.Exchange(ref _pingTexture, null);
GorgonTexture2DView pongTexture = Interlocked.Exchange(ref _pongTexture, null);
Interlocked.Exchange(ref _pingTexture, null);
Interlocked.Exchange(ref _pongTexture, null);
GorgonRenderTarget2DView pingTarget = Interlocked.Exchange(ref _pingTarget, null);
GorgonRenderTarget2DView pongTarget = Interlocked.Exchange(ref _pongTarget, null);

pingTexture?.Dispose();
pongTexture?.Dispose();
pingTarget?.Dispose();
pongTarget?.Dispose();
if (pingTarget is not null)
{
Graphics.TemporaryTargets.Return(pingTarget);
}

if (pongTarget is not null)
{
Graphics.TemporaryTargets.Return(pongTarget);
}
}

/// <summary>
Expand All @@ -152,34 +145,33 @@ private void FreeResources()
private void CreateResources(GorgonRenderTargetView outputTarget)
{
FreeResources();

_final = outputTarget;

_pingTarget = GorgonRenderTarget2DView.CreateRenderTarget(Graphics, new GorgonTexture2DInfo(outputTarget.Width, outputTarget.Height, outputTarget.Format)

_pingTarget = Graphics.TemporaryTargets.Rent(new GorgonTexture2DInfo(outputTarget.Width, outputTarget.Height, outputTarget.Format)
{
Name = "Gorgon 2D Post Process Ping Render Target",
Binding = TextureBinding.ShaderResource
});
}, "Gorgon 2D Post Process Ping Render Target", true);
_pingTexture = _pingTarget.GetShaderResourceView();

_pongTarget = GorgonRenderTarget2DView.CreateRenderTarget(Graphics, new GorgonTexture2DInfo(_pingTarget)
_pongTarget = Graphics.TemporaryTargets.Rent(new GorgonTexture2DInfo(_pingTarget)
{
Name = "Gorgon 2D Post Process Pong Render Target"
});
}, "Gorgon 2D Post Process Pong Render Target", true);
_pongTexture = _pongTarget.GetShaderResourceView();
}

/// <summary>
/// Function to render the initial scene to the initial render target.
/// </summary>
/// <param name="lastTargetTexture">The last texture used as a target.</param>
private void CopyToFinal(GorgonTexture2DView lastTargetTexture)
/// <param name="final">The final output render target.</param>
private void CopyToFinal(GorgonTexture2DView lastTargetTexture, GorgonRenderTargetView final)
{
Graphics.SetRenderTarget(_final);
Graphics.SetRenderTarget(final);

if (_finalClear is not null)
{
_final.Clear(_finalClear.Value);
final.Clear(_finalClear.Value);
}

// Copy the composited output into the final render target specified by the user.
Expand Down Expand Up @@ -239,11 +231,7 @@ private Gorgon2DCompositor Pass(CompositionPass pass)
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Interlocked.Exchange(ref _final, null);
FreeResources();
}
public void Dispose() => FreeResources();

/// <summary>
/// Function to assign the color to use when clearing the initial render target when rendering begins.
Expand Down Expand Up @@ -471,15 +459,13 @@ public Gorgon2DCompositor Render(GorgonTexture2DView source, GorgonRenderTargetV
output.ValidateObject(nameof(output));

// Create or update our resources
if ((_final != output) || (NeedsResourceUpdate(output)))
{
CreateResources(output);
}
CreateResources(output);

// If we have no effects, then, just output the scene to the render target as-is.
if (_passLookup.Count == 0)
{
CopyToFinal(source);
CopyToFinal(source, output);
FreeResources();
return this;
}

Expand Down Expand Up @@ -523,8 +509,9 @@ public Gorgon2DCompositor Render(GorgonTexture2DView source, GorgonRenderTargetV
current = (nextTarget, nextTexture);
}

CopyToFinal(current.texture);
CopyToFinal(current.texture, output);

FreeResources();
return this;
}

Expand Down

0 comments on commit 78a717e

Please sign in to comment.