fix memory leaks

This commit is contained in:
DCFApixels 2025-03-05 17:15:45 +08:00
parent af2a55e2c8
commit 75d512a05e
4 changed files with 88 additions and 34 deletions

View File

@ -4,6 +4,7 @@ using DCFApixels.DebugXCore.Internal;
using System.Runtime.InteropServices;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
@ -277,6 +278,16 @@ namespace DCFApixels
_material = material;
_matrices = PinnedArray<Matrix4x4>.Pin(DummyArray<Matrix4x4>.Get());
_colors = PinnedArray<Vector4>.Pin(DummyArray<Vector4>.Get());
AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload;
}
private void AssemblyReloadEvents_beforeAssemblyReload()
{
AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload;
_materialPropertyBlock.Clear();
_matrices.Dispose();
_colors.Dispose();
_gizmos.Dispose();
}
public void Prepare(GizmosList rawList)
{
@ -380,7 +391,19 @@ namespace DCFApixels
_materialPropertyBlock = new MaterialPropertyBlock();
_drawDatas = PinnedArray<DrawData>.Pin(DummyArray<DrawData>.Get());
_enableInstancing = IsSupportsComputeShaders && _material.GetMaterial().enableInstancing;
AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload;
}
private void AssemblyReloadEvents_beforeAssemblyReload()
{
AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload;
_graphicsBuffer?.Release();
_graphicsBuffer?.Dispose();
_materialPropertyBlock.Clear();
_drawDatas.Dispose();
_gizmos.Dispose();
}
public virtual int ExecuteOrder => _material.GetExecuteOrder();
public virtual bool IsStaticRender => true;
protected void Prepare(GizmosList rawList)
@ -399,11 +422,8 @@ namespace DCFApixels
_buffersLength = count;
}
if (ReferenceEquals(_gizmos.Array, items) == false)
{
if (_gizmos.Array != null)
{
_gizmos.Dispose();
}
_gizmos = PinnedArray<Gizmo<GizmoData>>.Pin(items);
}
@ -438,9 +458,11 @@ namespace DCFApixels
private readonly static int _BufferPropertyID = Shader.PropertyToID("_DataBuffer");
private void AllocateGraphicsBuffer(int capacity)
{
_graphicsBuffer?.Release();
_graphicsBuffer?.Dispose();
_materialPropertyBlock.Clear();
_graphicsBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, capacity, Marshal.SizeOf<DrawData>());
_materialPropertyBlock.Clear();
_materialPropertyBlock.SetBuffer(_BufferPropertyID, _graphicsBuffer);
}
}
@ -498,6 +520,17 @@ namespace DCFApixels
_materialPropertyBlock = new MaterialPropertyBlock();
_drawDatas = PinnedArray<DrawData>.Pin(DummyArray<DrawData>.Get());
_enableInstancing = IsSupportsComputeShaders && _material.GetMaterial().enableInstancing;
AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload;
}
private void AssemblyReloadEvents_beforeAssemblyReload()
{
AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload;
_graphicsBuffer?.Release();
_graphicsBuffer?.Dispose();
_materialPropertyBlock.Clear();
_drawDatas.Dispose();
_gizmos.Dispose();
}
public virtual int ExecuteOrder => _material.GetExecuteOrder() - 1;
public virtual bool IsStaticRender => true;
@ -553,9 +586,11 @@ namespace DCFApixels
private readonly static int _BufferPropertyID = Shader.PropertyToID("_DataBuffer");
private void AllocateGraphicsBuffer(int capacity)
{
_graphicsBuffer?.Release();
_graphicsBuffer?.Dispose();
_materialPropertyBlock.Clear();
_graphicsBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, capacity, Marshal.SizeOf<DrawData>());
_materialPropertyBlock.Clear();
_materialPropertyBlock.SetBuffer(_BufferPropertyID, _graphicsBuffer);
}
}

View File

@ -0,0 +1,44 @@
//#undef DEBUG
using DCFApixels.DebugXCore;
using System;
using UnityEngine;
using UnityEngine.Rendering;
namespace DCFApixels
{
using static DebugXConsts;
using IN = System.Runtime.CompilerServices.MethodImplAttribute;
public unsafe static partial class DebugX
{
public readonly partial struct DrawHandler
{
#region DotCross
[IN(LINE)] public DrawHandler DotCross(Vector3 position) => Mesh<DotCrossMesh, DotMat>(position, Quaternion.identity, new Vector3(0.06f, 0.06f, 1f));
#endregion
#region Dot
[IN(LINE)] public DrawHandler Dot(Vector3 position) => Mesh<DotMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE, DOT_SIZE, 1f));
#endregion
#region WireDot
[IN(LINE)] public DrawHandler WireDot(Vector3 position) => Mesh<WireCircleMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE * 0.5f, DOT_SIZE * 0.5f, 1f));
#endregion
#region DotQuad
[IN(LINE)] public DrawHandler DotQuad(Vector3 position) => Mesh<DotQuadMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE, DOT_SIZE, 1f));
#endregion
#region WireDotQuad
[IN(LINE)] public DrawHandler WireDotQuad(Vector3 position) => Mesh<WireCubeMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE, DOT_SIZE, 0f));
#endregion
#region DotDiamond
[IN(LINE)] public DrawHandler DotDiamond(Vector3 position) => Mesh<DotDiamondMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE * 1.16f, DOT_SIZE * 1.16f, 1f));
#endregion
#region WireDotDiamond
[IN(LINE)] public DrawHandler WireDotDiamond(Vector3 position) => Mesh<WireDotDiamondMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE * 1.16f, DOT_SIZE * 1.16f, 1f));
#endregion
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d5cceb4c23bcb634ca4a5fd4668ed2e1

View File

@ -20,10 +20,6 @@ namespace DCFApixels
[IN(LINE)] public DrawHandler Cross(Vector3 position, float size) => Mesh<DotCrossMesh, BillboardMat>(position, Quaternion.identity, new Vector3(size, size, size));
#endregion
#region DotCross
[IN(LINE)] public DrawHandler DotCross(Vector3 position) => Mesh<DotCrossMesh, DotMat>(position, Quaternion.identity, new Vector3(0.06f, 0.06f, 1f));
#endregion
#region Sphere
[IN(LINE)] public DrawHandler Sphere(Vector3 position, float radius) => Mesh<SphereMesh, LitMat>(position, Quaternion.identity, new Vector3(radius, radius, radius));
@ -122,14 +118,6 @@ namespace DCFApixels
[IN(LINE)] public DrawHandler WireCircle(Vector3 position, Quaternion rotation, float radius) => Mesh<WireCircleMesh, GeometryUnlitMat>(position, rotation, new Vector3(radius, radius, radius));
#endregion
#region Dot
[IN(LINE)] public DrawHandler Dot(Vector3 position) => Mesh<DotMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE, DOT_SIZE, 1f));
#endregion
#region WireDot
[IN(LINE)] public DrawHandler WireDot(Vector3 position) => Mesh<WireCircleMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE * 0.5f, DOT_SIZE * 0.5f, 1f));
#endregion
#region Cylinder
[IN(LINE)]
@ -246,6 +234,7 @@ namespace DCFApixels
}
#endregion
#region Capsule
[IN(LINE)]
public DrawHandler Capsule(Vector3 position, Quaternion rotation, float radius, float height)
@ -539,22 +528,6 @@ namespace DCFApixels
return this;
}
#endregion
#region DotQuad
[IN(LINE)] public DrawHandler DotQuad(Vector3 position) => Mesh<DotQuadMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE, DOT_SIZE, 1f));
#endregion
#region WireDotQuad
[IN(LINE)] public DrawHandler WireDotQuad(Vector3 position) => Mesh<WireCubeMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE, DOT_SIZE, 0f));
#endregion
#region DotDiamond
[IN(LINE)] public DrawHandler DotDiamond(Vector3 position) => Mesh<DotDiamondMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE * 1.16f, DOT_SIZE * 1.16f, 1f));
#endregion
#region WireDotDiamond
[IN(LINE)] public DrawHandler WireDotDiamond(Vector3 position) => Mesh<WireDotDiamondMesh, DotMat>(position, Quaternion.identity, new Vector3(DOT_SIZE * 1.16f, DOT_SIZE * 1.16f, 1f));
#endregion
}
}
}