From 478c042f335e7d590797acf3dcb028d8a278ecc7 Mon Sep 17 00:00:00 2001 From: jvbsl Date: Sun, 12 Mar 2023 18:23:08 +0100 Subject: [PATCH] Restore VertexBuffer/IndexBuffer/EffectPass after SpriteBatch.End --- Graphics/Effect/EffectPass.cs | 10 +++++++++- Graphics/IndexBuffer.cs | 35 +++++++++++++++++++++++++++++++++++ Graphics/SpriteBatcher.cs | 26 ++++++++++++++------------ Graphics/VertexBuffer.cs | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 13 deletions(-) diff --git a/Graphics/Effect/EffectPass.cs b/Graphics/Effect/EffectPass.cs index 6433bc3..8d71c0a 100644 --- a/Graphics/Effect/EffectPass.cs +++ b/Graphics/Effect/EffectPass.cs @@ -20,6 +20,14 @@ public class EffectPass : GraphicsResource, IDisposable private readonly GraphicsDevice _graphicsDevice; private readonly EffectPass? _oldValue; + /// + /// Restores the active to the given value on dispose. + /// + /// The to restore on. + public PassRestorer(GraphicsDevice graphicsDevice) : this(graphicsDevice, graphicsDevice.EffectPass) + { + } + /// /// Restores the active to the given value on dispose. /// @@ -215,7 +223,7 @@ internal void Link() public PassRestorer Apply() { GraphicsDevice.ValidateUiGraphicsThread(); - var passRestorer = new PassRestorer(GraphicsDevice, GraphicsDevice.EffectPass); + var passRestorer = new PassRestorer(GraphicsDevice); GraphicsDevice.EffectPass = this; return passRestorer; diff --git a/Graphics/IndexBuffer.cs b/Graphics/IndexBuffer.cs index e1f34a8..2056c3a 100644 --- a/Graphics/IndexBuffer.cs +++ b/Graphics/IndexBuffer.cs @@ -10,6 +10,41 @@ namespace engenious.Graphics /// public class IndexBuffer : GraphicsResource { + /// + /// Class that restores the active on dispose. + /// + public readonly struct IndexBufferRestorer : IDisposable + { + private readonly GraphicsDevice _graphicsDevice; + private readonly IndexBuffer? _oldValue; + + /// + /// Restores the active to the given value on dispose. + /// + /// The to restore on. + public IndexBufferRestorer(GraphicsDevice graphicsDevice) : this(graphicsDevice, + graphicsDevice.IndexBuffer) + { + + } + + /// + /// Restores the active to the given value on dispose. + /// + /// The to restore on. + /// The to restore to. + public IndexBufferRestorer(GraphicsDevice graphicsDevice, IndexBuffer? oldValue) + { + _graphicsDevice = graphicsDevice; + _oldValue = oldValue; + } + + /// + public void Dispose() + { + _graphicsDevice.IndexBuffer = _oldValue; + } + } private int _ibo; //private byte[] buffer; private readonly int _elementSize; diff --git a/Graphics/SpriteBatcher.cs b/Graphics/SpriteBatcher.cs index c52091b..9844bb3 100644 --- a/Graphics/SpriteBatcher.cs +++ b/Graphics/SpriteBatcher.cs @@ -191,20 +191,22 @@ public void Flush(IModelTechnique effectTechnique, Texture texture, int batch, i _indexBuffer.SetData(sizeof(short) * 6 * batch, _indexData, 0, batchCount * 6); - _graphicsDevice.VertexBuffer = _vertexBuffer; - _graphicsDevice.IndexBuffer = _indexBuffer; - effectTechnique.Texture = texture; - //graphicsDevice.SamplerStates[0] = samplerState; - if (effectTechnique == null) - throw new Exception("No valid effect technique set"); - foreach (var pass in effectTechnique.Passes) + using (new VertexBuffer.VertexBufferRestorer(_graphicsDevice)) + using (new IndexBuffer.IndexBufferRestorer(_graphicsDevice)) + using (new EffectPass.PassRestorer(_graphicsDevice)) { - pass.Apply(); - _graphicsDevice.DrawIndexedPrimitives(PrimitiveType.Triangles, 0, 0, batchCount * 4, 0, batchCount * 2); + _graphicsDevice.VertexBuffer = _vertexBuffer; + _graphicsDevice.IndexBuffer = _indexBuffer; + effectTechnique.Texture = texture; + //graphicsDevice.SamplerStates[0] = samplerState; + if (effectTechnique == null) + throw new Exception("No valid effect technique set"); + foreach (var pass in effectTechnique.Passes) + { + pass.Apply(); + _graphicsDevice.DrawIndexedPrimitives(PrimitiveType.Triangles, 0, 0, batchCount * 4, 0, batchCount * 2); + } } - - _graphicsDevice.IndexBuffer = null; - _graphicsDevice.VertexBuffer = null; } public void End(Matrix world, Matrix projection) diff --git a/Graphics/VertexBuffer.cs b/Graphics/VertexBuffer.cs index 19cab07..a779ad4 100644 --- a/Graphics/VertexBuffer.cs +++ b/Graphics/VertexBuffer.cs @@ -12,6 +12,41 @@ namespace engenious.Graphics /// public class VertexBuffer : GraphicsResource { + /// + /// Class that restores the active on dispose. + /// + public readonly struct VertexBufferRestorer : IDisposable + { + private readonly GraphicsDevice _graphicsDevice; + private readonly VertexBuffer? _oldValue; + + /// + /// Restores the active to the given value on dispose. + /// + /// The to restore on. + public VertexBufferRestorer(GraphicsDevice graphicsDevice) : this(graphicsDevice, + graphicsDevice.VertexBuffer) + { + + } + + /// + /// Restores the active to the given value on dispose. + /// + /// The to restore on. + /// The to restore to. + public VertexBufferRestorer(GraphicsDevice graphicsDevice, VertexBuffer? oldValue) + { + _graphicsDevice = graphicsDevice; + _oldValue = oldValue; + } + + /// + public void Dispose() + { + _graphicsDevice.VertexBuffer = _oldValue; + } + } internal int Vbo, NextVbo = -1; internal long NextVertexCount; internal VertexAttributes? Vao;