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

Commit ce61c6a

Browse files
committed
make compute buffer non-static
1 parent 5a0a169 commit ce61c6a

File tree

2 files changed

+42
-47
lines changed

2 files changed

+42
-47
lines changed

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

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,85 @@ struct uiVertex
1111
public Vector2 uv;
1212
}
1313

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

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

20-
static int _startVertex;
21-
static int _startIndex;
22-
23-
static int _instanceNum;
20+
int _startVertex;
21+
int _startIndex;
2422

2523
public const int COMPUTE_BUFFER_MAX_ITEM_NUM = 1024 * 1024; // maxsize = 1M vertex/index
2624

27-
static void _releaseComputeBuffer() {
28-
if (!CanvasShader.supportComputeBuffer) {
29-
return;
30-
}
25+
bool supportComputeBuffer {
26+
get { return CanvasShader.supportComputeBuffer; }
27+
}
3128

32-
if (_computeBuffer == null) {
29+
void _releaseComputeBuffer() {
30+
if (!this.supportComputeBuffer) {
3331
return;
3432
}
3533

36-
if (_instanceNum != 0) {
34+
if (this._computeBuffer == null) {
3735
return;
3836
}
3937

40-
_computeBuffer.Dispose();
41-
_indexBuffer.Dispose();
42-
_vertices = null;
43-
_indices = null;
44-
_computeBuffer = null;
45-
_indexBuffer = null;
38+
this._computeBuffer.Dispose();
39+
this._indexBuffer.Dispose();
40+
this._vertices = null;
41+
this._indices = null;
42+
this._computeBuffer = null;
43+
this._indexBuffer = null;
4644
}
4745

4846
void _initComputeBuffer() {
4947
var stride = Marshal.SizeOf(typeof(uiVertex));
5048
var strideIndex = Marshal.SizeOf(typeof(int));
51-
_computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
52-
_vertices = new List<uiVertex>();
49+
this._computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
50+
this._vertices = new List<uiVertex>();
5351

54-
_indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
55-
_indices = new List<int>();
52+
this._indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
53+
this._indices = new List<int>();
5654
}
5755

5856
void _resetComputeBuffer() {
59-
if (!CanvasShader.supportComputeBuffer) return;
57+
if (!this.supportComputeBuffer) return;
6058

61-
if (_computeBuffer == null) {
59+
if (this._computeBuffer == null) {
6260
this._initComputeBuffer();
6361
}
6462

65-
_vertices.Clear();
66-
_indices.Clear();
67-
_startVertex = 0;
68-
_startIndex = 0;
63+
this._vertices.Clear();
64+
this._indices.Clear();
65+
this._startVertex = 0;
66+
this._startIndex = 0;
6967
}
7068

7169
void _bindComputeBuffer() {
72-
if (!CanvasShader.supportComputeBuffer) return;
70+
if (!this.supportComputeBuffer) return;
7371

74-
_computeBuffer.SetData(_vertices);
75-
_indexBuffer.SetData(_indices);
72+
this._computeBuffer.SetData(this._vertices);
73+
this._indexBuffer.SetData(this._indices);
7674
}
7775

7876
void _addMeshToComputeBuffer(List<Vector3> vertex, List<Vector2> uv, List<int> triangles) {
79-
if (!CanvasShader.supportComputeBuffer) return;
77+
if (!this.supportComputeBuffer) return;
8078

81-
_startVertex = _vertices.Count;
82-
_startIndex = _indices.Count;
79+
this._startVertex = this._vertices.Count;
80+
this._startIndex = this._indices.Count;
8381

8482
var hasUv = uv != null;
8583

8684
for (int i = 0; i < vertex.Count; i++) {
87-
_vertices.Add(new uiVertex {
85+
this._vertices.Add(new uiVertex {
8886
position = new Vector2(vertex[i].x, vertex[i].y),
8987
uv = hasUv ? uv[i] : Vector2.zero
9088
});
9189
}
9290

9391
foreach (var triangleId in triangles) {
94-
_indices.Add(triangleId + _startVertex);
92+
this._indices.Add(triangleId + this._startVertex);
9593
}
9694
}
9795
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public void dispose() {
4242
this._lastScissor = null;
4343
this._layers.Clear();
4444
}
45-
46-
_instanceNum--;
47-
_releaseComputeBuffer();
45+
46+
this._releaseComputeBuffer();
4847
}
4948

5049
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool) {
@@ -60,8 +59,6 @@ public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshP
6059
this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
6160
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
6261
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
63-
64-
_instanceNum++;
6562
}
6663

6764
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;
@@ -1174,11 +1171,11 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
11741171
}
11751172

11761173
D.assert(mesh.vertices.Count > 0);
1177-
if (CanvasShader.supportComputeBuffer) {
1174+
if (this.supportComputeBuffer) {
11781175
this._addMeshToComputeBuffer(mesh.vertices?.data, mesh.uv?.data, mesh.triangles?.data);
1179-
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, _computeBuffer);
1180-
cmd.properties.SetBuffer(CmdDraw.indexBufferId, _indexBuffer);
1181-
cmd.properties.SetInt(CmdDraw.startIndexId, _startIndex);
1176+
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, this._computeBuffer);
1177+
cmd.properties.SetBuffer(CmdDraw.indexBufferId, this._indexBuffer);
1178+
cmd.properties.SetInt(CmdDraw.startIndexId, this._startIndex);
11821179
cmdBuf.DrawProcedural(Matrix4x4.identity, cmd.material, cmd.pass, MeshTopology.Triangles, mesh.triangles.Count, 1, cmd.properties.mpb);
11831180
}
11841181
else {

0 commit comments

Comments
 (0)