Skip to content

Instantly share code, notes, and snippets.

@tomspilman
Created July 6, 2016 17:00
Show Gist options
  • Select an option

  • Save tomspilman/ccf63763c93c481259c896827833aeae to your computer and use it in GitHub Desktop.

Select an option

Save tomspilman/ccf63763c93c481259c896827833aeae to your computer and use it in GitHub Desktop.
public static class GraphicsDeviceHelpers
{
struct TargetInfo
{
public RenderTargetBinding[] Targets;
public int TargetCount;
}
private static int _current;
private static readonly TargetInfo[] _lastTargets = new TargetInfo[10];
static public void ResetRenderTargetStack(this GraphicsDevice device)
{
for (var i = 0; i < _current; i++)
{
#if MONOGAME
Array.Clear(_lastTargets[i].Targets, 0, 4);
#else
_lastTargets[i].Targets = null;
#endif
}
_current = 0;
}
/// <summary>
/// Save the currently set render targets.
/// </summary>
static public void PushRenderTargets(this GraphicsDevice device)
{
#if MONOGAME
if (_lastTargets[_current].Targets == null)
_lastTargets[_current].Targets = new RenderTargetBinding[4];
device.GetRenderTargets(_lastTargets[_current].Targets, out _lastTargets[_current].TargetCount);
#else
_lastTargets[_current].Targets = device.GetRenderTargets();
#endif
++_current;
}
/// <summary>
/// Restore the last stored render targets.
/// </summary>
static public void PopRenderTargets(this GraphicsDevice device)
{
--_current;
if (_current < 0)
throw new Exception("The render target stack was unbalanced!");
#if MONOGAME
device.SetRenderTargets(_lastTargets[_current].Targets, _lastTargets[_current].TargetCount);
Array.Clear(_lastTargets[_current].Targets, 0, 4);
#else
device.SetRenderTargets(_lastTargets[_current].Targets);
_lastTargets[_current].Targets = null;
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment