Added GeometryBinding refcounting

This commit is contained in:
Simon Lübeß
2021-02-20 14:52:51 +01:00
parent 9ae376f572
commit 78af9e736e
2 changed files with 33 additions and 19 deletions
+15 -1
View File
@@ -3,7 +3,7 @@ using System.Collections;
namespace GlitchyEngine.Renderer namespace GlitchyEngine.Renderer
{ {
public class GeometryBinding public class GeometryBinding : RefCounted
{ {
internal GraphicsContext _context ~ _?.ReleaseRef(); internal GraphicsContext _context ~ _?.ReleaseRef();
@@ -19,6 +19,16 @@ namespace GlitchyEngine.Renderer
_context = context..AddRef(); _context = context..AddRef();
} }
public ~this()
{
for(let binding in _vertexBuffers)
{
binding.Buffer?.ReleaseRef();
}
_indexBuffer?.ReleaseRef();
}
public VertexBufferBinding GetVertexBuffer(uint32 slot) public VertexBufferBinding GetVertexBuffer(uint32 slot)
{ {
return _vertexBuffers[slot]; return _vertexBuffers[slot];
@@ -46,6 +56,7 @@ namespace GlitchyEngine.Renderer
public void SetVertexBufferSlot(VertexBufferBinding vertexBufferBinding, uint32 slot) public void SetVertexBufferSlot(VertexBufferBinding vertexBufferBinding, uint32 slot)
{ {
_vertexBuffers.Add(vertexBufferBinding); _vertexBuffers.Add(vertexBufferBinding);
vertexBufferBinding.Buffer.AddRef();
PlatformSetVertexBuffer(vertexBufferBinding, slot); PlatformSetVertexBuffer(vertexBufferBinding, slot);
} }
@@ -70,7 +81,10 @@ namespace GlitchyEngine.Renderer
public void SetIndexBuffer(IndexBuffer indexBuffer, uint32 byteOffset = 0, uint32 indexCount = (.)-1) public void SetIndexBuffer(IndexBuffer indexBuffer, uint32 byteOffset = 0, uint32 indexCount = (.)-1)
{ {
_indexBuffer?.ReleaseRef();
_indexBuffer = indexBuffer; _indexBuffer = indexBuffer;
_indexBuffer.AddRef();
_indexByteOffset = byteOffset; _indexByteOffset = byteOffset;
if(indexCount == (.)-1) if(indexCount == (.)-1)
+18 -18
View File
@@ -53,13 +53,9 @@ namespace Sandbox
VertexLayout _vertexLayout ~ delete _; VertexLayout _vertexLayout ~ delete _;
GeometryBinding _geometryBinding ~ delete _; GeometryBinding _geometryBinding ~ _?.ReleaseRef();
VertexBuffer _vertexBuffer ~ _?.ReleaseRef();
IndexBuffer _indexBuffer ~ _?.ReleaseRef();
GeometryBinding _quadGeometryBinding ~ delete _; GeometryBinding _quadGeometryBinding ~ _?.ReleaseRef();
VertexBuffer _quadVertexBuffer ~ _?.ReleaseRef();
IndexBuffer _quadIndexBuffer ~ _?.ReleaseRef();
RasterizerState _rasterizerState ~ delete _; RasterizerState _rasterizerState ~ delete _;
@@ -109,9 +105,10 @@ namespace Sandbox
VertexColorTexture(CircleCoord(-pO3), Color(255, 0,255)), VertexColorTexture(CircleCoord(-pO3), Color(255, 0,255)),
); );
_vertexBuffer = new VertexBuffer(_context, typeof(VertexColorTexture), (.)vertices.Count, .Immutable); let vb = new VertexBuffer(_context, typeof(VertexColorTexture), (.)vertices.Count, .Immutable);
_vertexBuffer.SetData(vertices); vb.SetData(vertices);
_geometryBinding.SetVertexBufferSlot(_vertexBuffer, 0); _geometryBinding.SetVertexBufferSlot(vb, 0);
vb.ReleaseRef();
uint16[?] indices = .( uint16[?] indices = .(
0, 1, 2, 0, 1, 2,
@@ -121,9 +118,10 @@ namespace Sandbox
0, 5, 6, 0, 5, 6,
0, 6, 1); 0, 6, 1);
_indexBuffer = new IndexBuffer(_context, (.)indices.Count, .Immutable); let ib = new IndexBuffer(_context, (.)indices.Count, .Immutable);
_indexBuffer.SetData(indices); ib.SetData(indices);
_geometryBinding.SetIndexBuffer(_indexBuffer); _geometryBinding.SetIndexBuffer(ib);
ib.ReleaseRef();
} }
// Create Quad // Create Quad
@@ -139,17 +137,19 @@ namespace Sandbox
VertexColorTexture(Vector3(0.75f, 0.75f, 0), Color.White, .(1, 0)), VertexColorTexture(Vector3(0.75f, 0.75f, 0), Color.White, .(1, 0)),
); );
_quadVertexBuffer = new VertexBuffer(_context, typeof(VertexColorTexture), (.)vertices.Count, .Immutable); let qvb = new VertexBuffer(_context, typeof(VertexColorTexture), (.)vertices.Count, .Immutable);
_quadVertexBuffer.SetData(vertices); qvb.SetData(vertices);
_quadGeometryBinding.SetVertexBufferSlot(_quadVertexBuffer, 0); _quadGeometryBinding.SetVertexBufferSlot(qvb, 0);
qvb.ReleaseRef();
uint16[?] indices = .( uint16[?] indices = .(
0, 1, 2, 0, 1, 2,
2, 3, 0); 2, 3, 0);
_quadIndexBuffer = new IndexBuffer(_context, (.)indices.Count, .Immutable); let qib = new IndexBuffer(_context, (.)indices.Count, .Immutable);
_quadIndexBuffer.SetData(indices); qib.SetData(indices);
_quadGeometryBinding.SetIndexBuffer(_quadIndexBuffer); _quadGeometryBinding.SetIndexBuffer(qib);
qib.ReleaseRef();
} }
// Create rasterizer state // Create rasterizer state