Skip to content

Commit

Permalink
Restore VertexBuffer/IndexBuffer/EffectPass after SpriteBatch.End
Browse files Browse the repository at this point in the history
  • Loading branch information
jvbsl committed Mar 12, 2023
1 parent b38a93c commit 478c042
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
10 changes: 9 additions & 1 deletion Graphics/Effect/EffectPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public class EffectPass : GraphicsResource, IDisposable
private readonly GraphicsDevice _graphicsDevice;
private readonly EffectPass? _oldValue;

/// <summary>
/// Restores the active <see cref="GraphicsDevice.EffectPass"/> to the given value on dispose.
/// </summary>
/// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> to restore on.</param>
public PassRestorer(GraphicsDevice graphicsDevice) : this(graphicsDevice, graphicsDevice.EffectPass)
{
}

/// <summary>
/// Restores the active <see cref="GraphicsDevice.EffectPass"/> to the given value on dispose.
/// </summary>
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions Graphics/IndexBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,41 @@ namespace engenious.Graphics
/// </summary>
public class IndexBuffer : GraphicsResource
{
/// <summary>
/// Class that restores the active <see cref="GraphicsDevice.IndexBuffer"/> on dispose.
/// </summary>
public readonly struct IndexBufferRestorer : IDisposable
{
private readonly GraphicsDevice _graphicsDevice;
private readonly IndexBuffer? _oldValue;

/// <summary>
/// Restores the active <see cref="GraphicsDevice.IndexBuffer"/> to the given value on dispose.
/// </summary>
/// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> to restore on.</param>
public IndexBufferRestorer(GraphicsDevice graphicsDevice) : this(graphicsDevice,
graphicsDevice.IndexBuffer)
{

}

/// <summary>
/// Restores the active <see cref="GraphicsDevice.IndexBuffer"/> to the given value on dispose.
/// </summary>
/// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> to restore on.</param>
/// <param name="oldValue">The <see cref="IndexBuffer"/> to restore to.</param>
public IndexBufferRestorer(GraphicsDevice graphicsDevice, IndexBuffer? oldValue)
{
_graphicsDevice = graphicsDevice;
_oldValue = oldValue;
}

/// <inheritdoc />
public void Dispose()
{
_graphicsDevice.IndexBuffer = _oldValue;
}
}
private int _ibo;
//private byte[] buffer;
private readonly int _elementSize;
Expand Down
26 changes: 14 additions & 12 deletions Graphics/SpriteBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions Graphics/VertexBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,41 @@ namespace engenious.Graphics
/// </summary>
public class VertexBuffer : GraphicsResource
{
/// <summary>
/// Class that restores the active <see cref="GraphicsDevice.VertexBuffer"/> on dispose.
/// </summary>
public readonly struct VertexBufferRestorer : IDisposable
{
private readonly GraphicsDevice _graphicsDevice;
private readonly VertexBuffer? _oldValue;

/// <summary>
/// Restores the active <see cref="GraphicsDevice.VertexBuffer"/> to the given value on dispose.
/// </summary>
/// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> to restore on.</param>
public VertexBufferRestorer(GraphicsDevice graphicsDevice) : this(graphicsDevice,
graphicsDevice.VertexBuffer)
{

}

/// <summary>
/// Restores the active <see cref="GraphicsDevice.VertexBuffer"/> to the given value on dispose.
/// </summary>
/// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> to restore on.</param>
/// <param name="oldValue">The <see cref="VertexBuffer"/> to restore to.</param>
public VertexBufferRestorer(GraphicsDevice graphicsDevice, VertexBuffer? oldValue)
{
_graphicsDevice = graphicsDevice;
_oldValue = oldValue;
}

/// <inheritdoc />
public void Dispose()
{
_graphicsDevice.VertexBuffer = _oldValue;
}
}
internal int Vbo, NextVbo = -1;
internal long NextVertexCount;
internal VertexAttributes? Vao;
Expand Down

0 comments on commit 478c042

Please sign in to comment.