Skip to content

Commit

Permalink
Add dispose on context element
Browse files Browse the repository at this point in the history
Fix shader nodes disposal (no need to destroy cache if not in forced mode)
  • Loading branch information
mrvux committed Nov 22, 2016
1 parent b9898f1 commit 81fd333
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
20 changes: 19 additions & 1 deletion Core/VVVV.DX11.Core/Resources/DX11ContextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace VVVV.DX11
/// Main resource holder for per data device, to be used by any pin which holds a dx11 resource
/// </summary>
/// <typeparam name="T">Resource Type</typeparam>
public class DX11ContextElement<T>
public class DX11ContextElement<T> : IDisposable
{
private Dictionary<DX11RenderContext, T> resources = new Dictionary<DX11RenderContext, T>();

Expand Down Expand Up @@ -55,6 +55,24 @@ public void Dispose(DX11RenderContext context)
}
}

public void Dispose()
{
lock (syncRoot)
{
//Dispose resource for all devices
foreach (DX11RenderContext context in this.resources.Keys)
{
if (resources[context] is IDisposable)
{
IDisposable d = resources[context] as IDisposable;
d.Dispose();
}
//resources[dev].Dispose();
}
resources.Clear();
}
}

public void Clear()
{
this.resources.Clear();
Expand Down
21 changes: 8 additions & 13 deletions Core/VVVV.DX11.Lib/Effects/DX11ImageShaderNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public unsafe class DX11ImageShaderNode : DX11BaseShaderNode,IPluginBase, IPlugi
private DX11ContextElement<DX11ShaderVariableCache> shaderVariableCache = new DX11ContextElement<DX11ShaderVariableCache>();

private DX11ImageShaderVariableManager varmanager;
private Dictionary<DX11RenderContext, DX11ShaderData> deviceshaderdata = new Dictionary<DX11RenderContext, DX11ShaderData>();
private DX11ContextElement<DX11ShaderData> deviceshaderdata = new DX11ContextElement<DX11ShaderData>();
private bool shaderupdated;

private int spmax = 0;
Expand Down Expand Up @@ -279,9 +279,9 @@ public void Update(DX11RenderContext context)
Device device = context.Device;
DeviceContext ctx = context.CurrentDeviceContext;

if (!this.deviceshaderdata.ContainsKey(context))
if (!this.deviceshaderdata.Contains(context))
{
this.deviceshaderdata.Add(context, new DX11ShaderData(context));
this.deviceshaderdata[context] = new DX11ShaderData(context);
this.deviceshaderdata[context].SetEffect(this.FShader);
}
if (!this.shaderVariableCache.Contains(context))
Expand Down Expand Up @@ -668,14 +668,11 @@ private void BindSemanticSRV(Effect effect, string semantic, ShaderResourceView
#region Destroy
public void Destroy(DX11RenderContext context, bool force)
{
//this.FOutLayer[0].Dispose(OnDevice.Device);

if (this.deviceshaderdata.ContainsKey(context))
if (force)
{
this.deviceshaderdata[context].Dispose();
this.deviceshaderdata.Remove(context);
this.deviceshaderdata.Dispose(context);
this.shaderVariableCache.Dispose(context);
}

foreach (DX11ResourcePoolEntry<DX11RenderTarget2D> entry in this.lastframetargets)
{
entry.UnLock();
Expand All @@ -687,10 +684,8 @@ public void Destroy(DX11RenderContext context, bool force)
#region Dispose
public void Dispose()
{
foreach (DX11ShaderData sd in this.deviceshaderdata.Values)
{
sd.Dispose();
}
this.deviceshaderdata.Dispose();
this.shaderVariableCache.Dispose();

foreach (DX11ResourcePoolEntry<DX11RenderTarget2D> entry in this.lastframetargets)
{
Expand Down
16 changes: 10 additions & 6 deletions Core/VVVV.DX11.Lib/Effects/DX11ShaderNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,14 @@ public void Update(DX11RenderContext context)
#region Destroy
public void Destroy(DX11RenderContext context, bool force)
{
this.FOutLayer.SafeDisposeAll(context);

if (this.deviceshaderdata.Contains(context))
if (force)
{
this.FOutLayer.SafeDisposeAll(context);
this.deviceshaderdata.Dispose(context);
this.shaderVariableCache.Dispose(context);
this.objectSettings.Dispose(context);
this.orderedObjectSettings.Dispose(context);
}
this.shaderVariableCache.Dispose(context);
this.objectSettings.Dispose(context);
this.orderedObjectSettings.Dispose(context);
}
#endregion

Expand Down Expand Up @@ -626,6 +625,11 @@ public void Render(DX11RenderContext context, DX11RenderSettings settings)
public void Dispose()
{
this.deviceshaderdata.Dispose();
this.shaderVariableCache.Dispose();
this.FOutLayer.SafeDisposeAll();
this.objectSettings.Dispose();
this.orderedObjectSettings.Dispose();

}
#endregion

Expand Down
15 changes: 8 additions & 7 deletions Core/VVVV.DX11.Lib/Effects/DX11StreamOutShaderNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public unsafe class DX11StreamOutShaderNode : DX11BaseShaderNode, IPluginBase, I
private DX11ObjectRenderSettings objectsettings = new DX11ObjectRenderSettings();

private DX11ShaderVariableManager varmanager;
private Dictionary<DX11RenderContext, DX11ShaderData> deviceshaderdata = new Dictionary<DX11RenderContext, DX11ShaderData>();
private DX11ContextElement<DX11ShaderData> deviceshaderdata = new DX11ContextElement<DX11ShaderData>();
private DX11ContextElement<DX11ShaderVariableCache> shaderVariableCache = new DX11ContextElement<DX11ShaderVariableCache>();

private DX11RenderSettings settings = new DX11RenderSettings();
Expand Down Expand Up @@ -277,9 +277,9 @@ public void Update(DX11RenderContext context)
Device device = context.Device;
DeviceContext ctx = context.CurrentDeviceContext;

if (!this.deviceshaderdata.ContainsKey(context))
if (!this.deviceshaderdata.Contains(context))
{
this.deviceshaderdata.Add(context, new DX11ShaderData(context));
this.deviceshaderdata[context] = new DX11ShaderData(context);
this.deviceshaderdata[context].SetEffect(this.FShader);
}
if (!this.shaderVariableCache.Contains(context))
Expand Down Expand Up @@ -549,18 +549,19 @@ public void Update(DX11RenderContext context)
#region Destroy
public void Destroy(DX11RenderContext context, bool force)
{
if (this.deviceshaderdata.ContainsKey(context))
if (force)
{
this.deviceshaderdata[context].Dispose();
this.deviceshaderdata.Remove(context);
this.deviceshaderdata.Dispose(context);
this.shaderVariableCache.Dispose(context);
}
}
#endregion

#region Dispose
public void Dispose()
{
//if (this.effect != null) { this.effect.Dispose(); }
this.deviceshaderdata.Dispose();
this.shaderVariableCache.Dispose();
}
#endregion

Expand Down

0 comments on commit 81fd333

Please sign in to comment.