Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 7664721

Browse files
committed
release compute buffer when reference is none
1 parent 152d6c3 commit 7664721

File tree

3 files changed

+64
-32
lines changed

3 files changed

+64
-32
lines changed

Runtime/editor/surface.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public void Dispose() {
224224

225225
if (this._canvas != null) {
226226
this._canvas.reset();
227+
this._canvas.dispose();
227228
this._canvas = null;
228229
}
229230
}

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_computebuffer_utils.cs

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,71 +11,101 @@ struct uiVertex
1111
public Vector2 uv;
1212
}
1313

14-
static ComputeBuffer computeBuffer;
15-
static List<uiVertex> vertexes;
14+
static ComputeBuffer _computeBuffer;
15+
static List<uiVertex> _vertices;
1616

17-
static ComputeBuffer indexBuffer;
18-
static List<int> indexes;
17+
static ComputeBuffer _indexBuffer;
18+
static List<int> _indices;
1919

20-
static int startVertex;
21-
static int startIndex;
20+
static int _startVertex;
21+
static int _startIndex;
22+
23+
static int _instanceNum;
24+
25+
public static bool enableComputeBuffer = true;
26+
27+
public const int COMPUTE_BUFFER_MAX_ITEM_NUM = 1024 * 1024; // maxsize = 1M vertex/index
2228

2329
static bool supportComputeBuffer {
24-
get { return CanvasShader.supportComputeBuffer; }
30+
get { return SystemInfo.supportsComputeShaders && CanvasShader.supportComputeBuffer && enableComputeBuffer; }
31+
}
32+
33+
static void tryReleaseComputeBuffer() {
34+
_instanceNum--;
35+
36+
if (!supportComputeBuffer) {
37+
return;
38+
}
39+
40+
if (_computeBuffer == null) {
41+
return;
42+
}
43+
44+
if (_instanceNum != 0) {
45+
return;
46+
}
47+
48+
_computeBuffer.Dispose();
49+
_indexBuffer.Dispose();
50+
_vertices = null;
51+
_indices = null;
52+
_computeBuffer = null;
53+
_indexBuffer = null;
2554
}
2655

2756
void initComputeBuffer() {
2857
var stride = Marshal.SizeOf(typeof(uiVertex));
29-
computeBuffer = new ComputeBuffer(1024 * 1024, stride);
30-
vertexes = new List<uiVertex>();
58+
var strideIndex = Marshal.SizeOf(typeof(int));
59+
_computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
60+
_vertices = new List<uiVertex>();
3161

32-
indexBuffer = new ComputeBuffer(1024 * 1024, Marshal.SizeOf(typeof(int)));
33-
indexes = new List<int>();
62+
_indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
63+
_indices = new List<int>();
3464
}
3565

3666
void resetComputeBuffer() {
3767
if (!supportComputeBuffer) return;
3868

39-
if (computeBuffer == null) {
69+
if (_computeBuffer == null) {
4070
this.initComputeBuffer();
4171
}
4272

43-
vertexes.Clear();
44-
indexes.Clear();
45-
startVertex = 0;
46-
startIndex = 0;
73+
_vertices.Clear();
74+
_indices.Clear();
75+
_startVertex = 0;
76+
_startIndex = 0;
4777
}
4878

4979
void bindComputeBuffer() {
5080
if (!supportComputeBuffer) return;
5181

52-
computeBuffer.SetData(vertexes);
53-
indexBuffer.SetData(indexes);
82+
_computeBuffer.SetData(_vertices);
83+
_indexBuffer.SetData(_indices);
5484
}
5585

5686
void addMeshToComputeBuffer(List<Vector3> vertex, List<Vector2> uv, List<int> triangles) {
5787
if (!supportComputeBuffer) return;
5888

59-
startVertex = vertexes.Count;
60-
startIndex = indexes.Count;
89+
_startVertex = _vertices.Count;
90+
_startIndex = _indices.Count;
6191

6292
var hasUv = uv != null;
6393

6494
for (int i = 0; i < vertex.Count; i++) {
65-
vertexes.Add(new uiVertex {
95+
_vertices.Add(new uiVertex {
6696
position = new Vector2(vertex[i].x, vertex[i].y),
6797
uv = hasUv ? uv[i] : Vector2.zero
6898
});
6999
}
70100

71101
foreach (var triangleId in triangles) {
72-
indexes.Add(triangleId + startVertex);
102+
_indices.Add(triangleId + _startVertex);
73103
}
74104
}
75105

76106
/*public void DrawBuffer(CommandBuffer cmdBuf)
77107
{
78-
if (this.computeBuffer == null)
108+
if (this._computeBuffer == null)
79109
{
80110
this.initComputeBuffer();
81111
}
@@ -116,9 +146,9 @@ void addMeshToComputeBuffer(List<Vector3> vertex, List<Vector2> uv, List<int> tr
116146
this.addMeshToComputeBuffer(vert, null, index);
117147
118148
var mpb = new MaterialPropertyBlock();
119-
mpb.SetBuffer("databuffer", this.computeBuffer);
120-
mpb.SetBuffer("indexbuffer", this.indexBuffer);
121-
mpb.SetInt("_startVertex", this.startIndex);
149+
mpb.SetBuffer("databuffer", this._computeBuffer);
150+
mpb.SetBuffer("indexbuffer", this._indexBuffer);
151+
mpb.SetInt("_startVertex", this._startIndex);
122152
cmdBuf.DrawProcedural(Matrix4x4.identity, this.material, 0, MeshTopology.Triangles, 6, 1, mpb);
123153
}
124154
}

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public void dispose() {
4242
this._lastScissor = null;
4343
this._layers.Clear();
4444
}
45+
46+
tryReleaseComputeBuffer();
4547
}
4648

4749
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool) {
@@ -57,6 +59,8 @@ public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshP
5759
this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
5860
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
5961
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
62+
63+
_instanceNum++;
6064
}
6165

6266
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;
@@ -1045,9 +1049,6 @@ public void flush(uiPicture picture) {
10451049

10461050
this._lastRtID = -1;
10471051
this._drawLayer(layer, cmdBuf);
1048-
//cmdBuf.SetRenderTarget(this._renderTexture);
1049-
//cmdBuf.ClearRenderTarget(true, true, UnityEngine.Color.grey);
1050-
//this.DrawBuffer(cmdBuf);
10511052

10521053
// this is necessary for webgl2. not sure why... just to be safe to disable the scissor.
10531054
cmdBuf.DisableScissorRect();
@@ -1163,9 +1164,9 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
11631164
D.assert(mesh.vertices.Count > 0);
11641165
if (supportComputeBuffer) {
11651166
this.addMeshToComputeBuffer(mesh.vertices?.data, mesh.uv?.data, mesh.triangles?.data);
1166-
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, computeBuffer);
1167-
cmd.properties.SetBuffer(CmdDraw.indexBufferId, indexBuffer);
1168-
cmd.properties.SetInt(CmdDraw.startIndexId, startIndex);
1167+
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, _computeBuffer);
1168+
cmd.properties.SetBuffer(CmdDraw.indexBufferId, _indexBuffer);
1169+
cmd.properties.SetInt(CmdDraw.startIndexId, _startIndex);
11691170
cmdBuf.DrawProcedural(Matrix4x4.identity, cmd.material, cmd.pass, MeshTopology.Triangles, mesh.triangles.Count, 1, cmd.properties.mpb);
11701171
}
11711172
else {

0 commit comments

Comments
 (0)