Skip to content

Commit

Permalink
[Nodes] Fix resource leak on stream out renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvux committed Jun 21, 2017
1 parent d1e6758 commit f391e56
Showing 1 changed file with 45 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
using FeralTic.DX11.Resources;
using FeralTic.DX11;
using FeralTic.DX11.Utils;
using FeralTic.DX11.Resources.Misc;

namespace VVVV.DX11.Nodes
{
[PluginInfo(Name = "Renderer", Category = "DX11", Version = "StreamOut", Author = "vux", AutoEvaluate = false)]
public class DX11SORendererNode : IPluginEvaluate, IDX11RendererHost, IDisposable
public class DX11SORendererNode : IPluginEvaluate, IDX11RendererHost, IDisposable, IDX11Queryable
{
protected IPluginHost FHost;

Expand All @@ -33,9 +34,6 @@ public class DX11SORendererNode : IPluginEvaluate, IDX11RendererHost, IDisposabl
[Input("Output Layout", Order = 10005, CheckIfChanged = true)]
protected Pin<InputElement> FInLayout;

[Input("Reset Counter Value")]
protected IDiffSpread<int> FInResetCounterValue;

[Input("Enabled", DefaultValue = 1, Order = 15)]
protected ISpread<bool> FInEnabled;

Expand All @@ -48,27 +46,24 @@ public class DX11SORendererNode : IPluginEvaluate, IDX11RendererHost, IDisposabl
[Input("Keep In Memory", Order = 18, Visibility = PinVisibility.OnlyInspector, IsSingle=true)]
protected ISpread<bool> FInKeepInMemory;

[Output("Geometry Out", IsSingle = true)]
[Output("Geometry Out")]
protected ISpread<DX11Resource<IDX11Geometry>> FOutGeom;

[Output("Buffer Out", IsSingle = true)]
[Output("Buffer Out")]
protected ISpread<DX11Resource<DX11RawBuffer>> FOutBuffer;

protected int vsize;
protected int cnt;
[Output("Query", Order = 200, IsSingle = true)]
protected ISpread<IDX11Queryable> FOutQueryable;

protected List<DX11RenderContext> updateddevices = new List<DX11RenderContext>();
protected List<DX11RenderContext> rendereddevices = new List<DX11RenderContext>();

private bool reset = false;


public event DX11QueryableDelegate BeginQuery;

public event DX11QueryableDelegate EndQuery;

private DX11RenderSettings settings = new DX11RenderSettings();
private SlimDX.Direct3D11.Buffer buffer;
private StreamOutputBufferWithRawSupport buffer;
private bool invalidate = false;

[ImportingConstructor()]
public DX11SORendererNode(IPluginHost FHost)
Expand All @@ -78,21 +73,28 @@ public DX11SORendererNode(IPluginHost FHost)

public void Evaluate(int SpreadMax)
{
if (this.FOutQueryable[0] == null) { this.FOutQueryable[0] = this; }


this.rendereddevices.Clear();
this.updateddevices.Clear();

reset = this.FInVSize.IsChanged || this.FInElemCount.IsChanged;
invalidate = this.FInVSize.IsChanged || this.FInElemCount.IsChanged || this.FInLayout.IsChanged;

if (this.FOutGeom[0] == null)
if (SpreadMax == 0)
{
this.FOutGeom[0] = new DX11Resource<IDX11Geometry>();
this.FOutBuffer[0] = new DX11Resource<DX11RawBuffer>();
if (this.buffer != null)
{
this.buffer.Dispose();
this.buffer = null;
}
return;
}

if (reset)
if (this.FOutBuffer[0] == null)
{
this.cnt = this.FInElemCount[0];
this.vsize = this.FInVSize[0];
this.FOutGeom[0] = new DX11Resource<IDX11Geometry>();
this.FOutBuffer[0] = new DX11Resource<DX11RawBuffer>();
}
}

Expand All @@ -103,6 +105,9 @@ public bool IsEnabled

public void Render(DX11RenderContext context)
{
if (this.FOutBuffer.SliceCount == 0)
return;

Device device = context.Device;
DeviceContext ctx = context.CurrentDeviceContext;

Expand All @@ -112,9 +117,7 @@ public void Render(DX11RenderContext context)
this.Update(context);
}



if (!this.FInLayer.PluginIO.IsConnected) { return; }
if (!this.FInLayer.IsConnected) { return; }

if (this.rendereddevices.Contains(context)) { return; }

Expand All @@ -127,7 +130,7 @@ public void Render(DX11RenderContext context)

context.CurrentDeviceContext.OutputMerger.SetTargets(new RenderTargetView[0]);

ctx.StreamOutput.SetTargets(new StreamOutputBufferBinding(this.buffer, 0));
ctx.StreamOutput.SetTargets(new StreamOutputBufferBinding(this.buffer.D3DBuffer, 0));

int rtmax = Math.Max(this.FInProjection.SliceCount, this.FInView.SliceCount);

Expand All @@ -138,9 +141,9 @@ public void Render(DX11RenderContext context)
settings.View = this.FInView[i];
settings.Projection = this.FInProjection[i];
settings.ViewProjection = settings.View * settings.Projection;
settings.RenderWidth = this.cnt;
settings.RenderHeight = this.cnt;
settings.RenderDepth = this.cnt;
settings.RenderWidth = 1;
settings.RenderHeight = 1;
settings.RenderDepth = 1;
settings.BackBuffer = null;

this.FInLayer.RenderAll(context, settings);
Expand All @@ -158,31 +161,21 @@ public void Render(DX11RenderContext context)

public void Update(DX11RenderContext context)
{
if (this.FOutBuffer.SliceCount == 0)
return;

if (this.updateddevices.Contains(context)) { return; }
if (reset || !this.FOutGeom[0].Contains(context))
if (this.invalidate || this.buffer == null)
{
this.DisposeBuffers(context);

// int vsize = customlayout ? size : ig.VertexSize;
SlimDX.Direct3D11.Buffer vbo = BufferHelper.CreateStreamOutBuffer(context, vsize, this.cnt);

//Copy a new Vertex buffer with stream out
DX11VertexGeometry vg = new DX11VertexGeometry(context);
vg.AssignDrawer(new DX11VertexAutoDrawer());
vg.HasBoundingBox = false;
vg.InputLayout = this.FInLayout.ToArray();
vg.Topology = PrimitiveTopology.TriangleList;
vg.VertexBuffer = vbo;
vg.VertexSize = vsize;
vg.VerticesCount = this.cnt;

this.buffer = vbo;
this.buffer = new StreamOutputBufferWithRawSupport(context, this.FInVSize[0], this.FInElemCount[0], this.FInLayout.ToArray());

this.FOutGeom[0][context] = vg;
this.FOutGeom[0][context] = this.buffer.VertexGeometry;

if (context.ComputeShaderSupport)
{
this.FOutBuffer[0][context] = new DX11RawBuffer(context, vbo);
this.FOutBuffer[0][context] = this.buffer.RawBuffer;
}
else
{
Expand All @@ -204,16 +197,21 @@ public void Destroy(DX11RenderContext context, bool force)
#region Dispose Buffers
private void DisposeBuffers(DX11RenderContext context)
{
for (int i = 0; i < this.FOutGeom.SliceCount; i++)
if (this.buffer != null)
{
this.FOutGeom[i].Dispose(context);
this.buffer.Dispose();
this.buffer = null;
}
}
#endregion

public void Dispose()
{
this.FOutGeom.SafeDisposeAll();
if (this.buffer != null)
{
this.buffer.Dispose();
this.buffer = null;
}
}
}

Expand Down

0 comments on commit f391e56

Please sign in to comment.