diff --git a/Backgrounds/SunkenSeaBurrowsBG.cs b/Backgrounds/SunkenSeaBurrowsBG.cs index 8a55b31db4..b8de0ce754 100644 --- a/Backgrounds/SunkenSeaBurrowsBG.cs +++ b/Backgrounds/SunkenSeaBurrowsBG.cs @@ -151,10 +151,9 @@ namespace CalamityMod.Backgrounds DrawBurrowsBG(); Main.spriteBatch.End(); - - Main.graphics.GraphicsDevice.SetRenderTarget(null); - CurrentlyRendering = false; } + + CurrentlyRendering = false; } private void On_Main_DrawBackgroundBlackFill(On_Main.orig_DrawBackgroundBlackFill orig, Main self) diff --git a/FluidSimulation/FluidField.cs b/FluidSimulation/FluidField.cs index a4b01c9237..d9079427e5 100644 --- a/FluidSimulation/FluidField.cs +++ b/FluidSimulation/FluidField.cs @@ -96,14 +96,14 @@ namespace CalamityMod.FluidSimulation internal void ApplyThingToTarget(RenderTarget2D currentField, Action shaderPreparationsAction) { - Main.instance.GraphicsDevice.SetRenderTarget(TemporaryAuxilaryTarget); - Main.instance.GraphicsDevice.Clear(Color.Transparent); + using (TemporaryAuxilaryTarget.Scope(clearColor: Color.Transparent)) + { + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Matrix.Identity); - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Matrix.Identity); - - shaderPreparationsAction(); - Main.spriteBatch.Draw(currentField, currentField.Bounds, Color.White); - Main.spriteBatch.End(); + shaderPreparationsAction(); + Main.spriteBatch.Draw(currentField, currentField.Bounds, Color.White); + Main.spriteBatch.End(); + } currentField.CopyContentsFrom(TemporaryAuxilaryTarget); } @@ -202,8 +202,7 @@ namespace CalamityMod.FluidSimulation }); // Clear the Poisson values and then calculate them, for use in the divergence clearance pass. - Main.instance.GraphicsDevice.SetRenderTarget(DivergencePoissonField); - Main.instance.GraphicsDevice.Clear(Color.Transparent); + using (DivergencePoissonField.Scope(clearColor: Color.Transparent)) { } for (int i = 0; i < GaussSeidelIterations; i++) { @@ -275,16 +274,14 @@ namespace CalamityMod.FluidSimulation public void UpdateOutputTarget() { - Main.instance.GraphicsDevice.SetRenderTarget(OutputTarget); - Main.instance.GraphicsDevice.Clear(Color.Transparent); - - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, Main.Rasterizer, null, Matrix.Identity); - Main.instance.GraphicsDevice.Textures[5] = ColorField.NextState.Target; - CalamityShaders.FluidShaders.Value.CurrentTechnique.Passes["DrawFluidPass"].Apply(); - Main.spriteBatch.Draw(DensityField.NextState.Target, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 1f, 0, 0f); - Main.spriteBatch.End(); - - Main.instance.GraphicsDevice.SetRenderTarget(null); + using (OutputTarget.Scope(clearColor: Color.Transparent)) + { + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, Main.Rasterizer, null, Matrix.Identity); + Main.instance.GraphicsDevice.Textures[5] = ColorField.NextState.Target; + CalamityShaders.FluidShaders.Value.CurrentTechnique.Passes["DrawFluidPass"].Apply(); + Main.spriteBatch.Draw(DensityField.NextState.Target, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, 1f, 0, 0f); + Main.spriteBatch.End(); + } } public void Dispose() diff --git a/Graphics/Metaballs/MetaballManager.cs b/Graphics/Metaballs/MetaballManager.cs index 107f2dbeba..a051f064c8 100644 --- a/Graphics/Metaballs/MetaballManager.cs +++ b/Graphics/Metaballs/MetaballManager.cs @@ -2,6 +2,7 @@ using System.Linq; using CalamityMod.Enums; using CalamityMod.Systems.Graphic; +using CalamityMod.Utilities.Daybreak.Buffers; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -72,18 +73,17 @@ namespace CalamityMod.Graphics.Metaballs // Draw the raw contents of the metaball to each of its render targets. foreach (var target in metaball.LayerTargets) { - gd.SetRenderTarget(target); - gd.Clear(Color.Transparent); - metaball.DrawInstances(); + using (target.Scope(clearColor: Color.Transparent)) + { + metaball.DrawInstances(); - // Flush metaball contents to its render target and reset the sprite batch for the next iteration. - Main.spriteBatch.End(); - Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, Main.Rasterizer, null, Main.Transform); + // Flush metaball contents to its render target and reset the sprite batch for the next iteration. + Main.spriteBatch.End(); + Main.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, Main.Rasterizer, null, Main.Transform); + } } } - // Return to the backbuffer and end the sprite batch. - gd.SetRenderTarget(null); Main.spriteBatch.End(); } diff --git a/Graphics/Renderers/RendererManager.cs b/Graphics/Renderers/RendererManager.cs index 9665bab61b..225e743a61 100644 --- a/Graphics/Renderers/RendererManager.cs +++ b/Graphics/Renderers/RendererManager.cs @@ -3,6 +3,7 @@ using System.Linq; using CalamityMod.Enums; using CalamityMod.Systems.Graphic; using CalamityMod.Utilities.Daybreak; +using CalamityMod.Utilities.Daybreak.Buffers; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -93,9 +94,8 @@ namespace CalamityMod.Graphics.Renderers continue; using (Main.spriteBatch.Scope()) + using (renderer.MainTarget.Scope(clearColor: Color.Transparent)) { - Main.instance.GraphicsDevice.SetRenderTarget(renderer.MainTarget); - Main.instance.GraphicsDevice.Clear(Color.Transparent); Main.spriteBatch.Begin( SpriteSortMode.Deferred, BlendState.AlphaBlend, @@ -109,8 +109,6 @@ namespace CalamityMod.Graphics.Renderers Main.spriteBatch.End(); } } - - Main.instance.GraphicsDevice.SetRenderTarget(null); } #endregion } diff --git a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_RT.cs b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_RT.cs index 167bbece0d..4fdf3ee22e 100644 --- a/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_RT.cs +++ b/Systems/Graphic/LiquidSystem/ModLavaStyleSystem_RT.cs @@ -1,4 +1,5 @@ -using Microsoft.Xna.Framework; +using CalamityMod.Utilities.Daybreak.Buffers; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; @@ -58,32 +59,33 @@ namespace CalamityMod.Systems.Graphic.LiquidSystem if (!Initialized || !TextureArrayReady || Main.gameMenu) return; - var graphicsDevice = Main.instance.GraphicsDevice; - graphicsDevice.SetRenderTarget(LavaRT); - graphicsDevice.Clear(Color.Transparent); - Begin(); - DrawTextures(Textures); - End(); + using (LavaRT.Scope(clearColor: Color.Transparent)) + { + Begin(); + DrawTextures(Textures); + End(); + } - graphicsDevice.SetRenderTarget(LavaBlockRT); - graphicsDevice.Clear(Color.Transparent); - Begin(); - DrawTextures(BlockTextures); - End(); + using (LavaBlockRT.Scope(clearColor: Color.Transparent)) + { + Begin(); + DrawTextures(BlockTextures); + End(); + } - graphicsDevice.SetRenderTarget(LavaSlopeRT); - graphicsDevice.Clear(Color.Transparent); - Begin(); - DrawTextures(SlopeTextures); - End(); + using (LavaSlopeRT.Scope(clearColor: Color.Transparent)) + { + Begin(); + DrawTextures(SlopeTextures); + End(); + } - graphicsDevice.SetRenderTarget(LavaWaterfallRT); - graphicsDevice.Clear(Color.Transparent); - Begin(); - DrawTextures(WaterfallTextures); - End(); - - graphicsDevice.SetRenderTarget(null); + using (LavaWaterfallRT.Scope(clearColor: Color.Transparent)) + { + Begin(); + DrawTextures(WaterfallTextures); + End(); + } } private static readonly BlendState ActualAdditive = new() diff --git a/Systems/TileMerging/TileBlendTexture_Baking.cs b/Systems/TileMerging/TileBlendTexture_Baking.cs index de51103d63..a40a39dc52 100644 --- a/Systems/TileMerging/TileBlendTexture_Baking.cs +++ b/Systems/TileMerging/TileBlendTexture_Baking.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using CalamityMod.Utilities.Daybreak.Buffers; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Terraria; @@ -62,20 +63,17 @@ namespace CalamityMod.Systems var renderTarget = BakedBlendTexture; if (renderTarget != null && !renderTarget.IsDisposed && !renderTarget.IsContentLost) { - graphicsDevice.SetRenderTarget(renderTarget); - if (_ShouldClearRT) + using (renderTarget.Scope(clearColor: _ShouldClearRT ? Color.Transparent : null)) { - graphicsDevice.Clear(Color.Transparent); _ShouldClearRT = false; + + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullNone); + + BakeBlendTextureCache(v); + + Main.spriteBatch.End(); } - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullNone); - - BakeBlendTextureCache(v); - - Main.spriteBatch.End(); - graphicsDevice.SetRenderTarget(null); - _RequestedVariants[variant] = false; _IsBaked[variant] = true; } diff --git a/Utilities/DrawingUtils.cs b/Utilities/DrawingUtils.cs index 6941342c23..0fc90e184c 100644 --- a/Utilities/DrawingUtils.cs +++ b/Utilities/DrawingUtils.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text; +using CalamityMod.Utilities.Daybreak.Buffers; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; @@ -356,16 +357,14 @@ namespace CalamityMod /// public static void CopyContentsFrom(this RenderTarget2D to, RenderTarget2D from) { - Main.instance.GraphicsDevice.SetRenderTarget(to); - Main.instance.GraphicsDevice.Clear(Color.Transparent); + using (to.Scope(clearColor: Color.Transparent)) + { + Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Matrix.Identity); + Main.spriteBatch.Draw(from, Vector2.Zero, null, Color.White); + Main.spriteBatch.End(); + } - Main.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, Main.DefaultSamplerState, DepthStencilState.None, Main.Rasterizer, null, Matrix.Identity); - Main.spriteBatch.Draw(from, Vector2.Zero, null, Color.White); - Main.spriteBatch.End(); - - Main.instance.GraphicsDevice.SetRenderTarget(from); - Main.instance.GraphicsDevice.Clear(Color.Transparent); - Main.instance.GraphicsDevice.SetRenderTarget(null); + using (from.Scope(clearColor: Color.Transparent)) { } } ///