浏览代码

release compute buffer when reference is none

/main
xingwei.zhu 5 年前
当前提交
9ab5eb65
共有 3 个文件被更改,包括 64 次插入32 次删除
  1. 1
      Runtime/editor/surface.cs
  2. 82
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_computebuffer_utils.cs
  3. 13
      Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs

1
Runtime/editor/surface.cs


if (this._canvas != null) {
this._canvas.reset();
this._canvas.dispose();
this._canvas = null;
}
}

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


public Vector2 uv;
}
static ComputeBuffer computeBuffer;
static List<uiVertex> vertexes;
static ComputeBuffer _computeBuffer;
static List<uiVertex> _vertices;
static ComputeBuffer indexBuffer;
static List<int> indexes;
static ComputeBuffer _indexBuffer;
static List<int> _indices;
static int startVertex;
static int startIndex;
static int _startVertex;
static int _startIndex;
static int _instanceNum;
public static bool enableComputeBuffer = true;
public const int COMPUTE_BUFFER_MAX_ITEM_NUM = 1024 * 1024; // maxsize = 1M vertex/index
get { return CanvasShader.supportComputeBuffer; }
get { return SystemInfo.supportsComputeShaders && CanvasShader.supportComputeBuffer && enableComputeBuffer; }
}
static void tryReleaseComputeBuffer() {
_instanceNum--;
if (!supportComputeBuffer) {
return;
}
if (_computeBuffer == null) {
return;
}
if (_instanceNum != 0) {
return;
}
_computeBuffer.Dispose();
_indexBuffer.Dispose();
_vertices = null;
_indices = null;
_computeBuffer = null;
_indexBuffer = null;
computeBuffer = new ComputeBuffer(1024 * 1024, stride);
vertexes = new List<uiVertex>();
var strideIndex = Marshal.SizeOf(typeof(int));
_computeBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, stride);
_vertices = new List<uiVertex>();
indexBuffer = new ComputeBuffer(1024 * 1024, Marshal.SizeOf(typeof(int)));
indexes = new List<int>();
_indexBuffer = new ComputeBuffer(COMPUTE_BUFFER_MAX_ITEM_NUM, strideIndex);
_indices = new List<int>();
if (computeBuffer == null) {
if (_computeBuffer == null) {
vertexes.Clear();
indexes.Clear();
startVertex = 0;
startIndex = 0;
_vertices.Clear();
_indices.Clear();
_startVertex = 0;
_startIndex = 0;
computeBuffer.SetData(vertexes);
indexBuffer.SetData(indexes);
_computeBuffer.SetData(_vertices);
_indexBuffer.SetData(_indices);
startVertex = vertexes.Count;
startIndex = indexes.Count;
_startVertex = _vertices.Count;
_startIndex = _indices.Count;
vertexes.Add(new uiVertex {
_vertices.Add(new uiVertex {
position = new Vector2(vertex[i].x, vertex[i].y),
uv = hasUv ? uv[i] : Vector2.zero
});

indexes.Add(triangleId + startVertex);
_indices.Add(triangleId + _startVertex);
if (this.computeBuffer == null)
if (this._computeBuffer == null)
{
this.initComputeBuffer();
}

this.addMeshToComputeBuffer(vert, null, index);
var mpb = new MaterialPropertyBlock();
mpb.SetBuffer("databuffer", this.computeBuffer);
mpb.SetBuffer("indexbuffer", this.indexBuffer);
mpb.SetInt("_startVertex", this.startIndex);
mpb.SetBuffer("databuffer", this._computeBuffer);
mpb.SetBuffer("indexbuffer", this._indexBuffer);
mpb.SetInt("_startVertex", this._startIndex);
cmdBuf.DrawProcedural(Matrix4x4.identity, this.material, 0, MeshTopology.Triangles, 6, 1, mpb);
}
}

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


this._lastScissor = null;
this._layers.Clear();
}
tryReleaseComputeBuffer();
}
public PictureFlusher(RenderTexture renderTexture, float devicePixelRatio, MeshPool meshPool) {

this.___drawTextDrawMeshCallback = this._drawTextDrawMeshCallback;
this.___drawPathDrawMeshCallback2 = this._drawPathDrawMeshCallback2;
this.___drawPathDrawMeshCallback = this._drawPathDrawMeshCallback;
_instanceNum++;
}
readonly _drawPathDrawMeshCallbackDelegate ___drawTextDrawMeshCallback;

this._lastRtID = -1;
this._drawLayer(layer, cmdBuf);
//cmdBuf.SetRenderTarget(this._renderTexture);
//cmdBuf.ClearRenderTarget(true, true, UnityEngine.Color.grey);
//this.DrawBuffer(cmdBuf);
// this is necessary for webgl2. not sure why... just to be safe to disable the scissor.
cmdBuf.DisableScissorRect();

D.assert(mesh.vertices.Count > 0);
if (supportComputeBuffer) {
this.addMeshToComputeBuffer(mesh.vertices?.data, mesh.uv?.data, mesh.triangles?.data);
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, computeBuffer);
cmd.properties.SetBuffer(CmdDraw.indexBufferId, indexBuffer);
cmd.properties.SetInt(CmdDraw.startIndexId, startIndex);
cmd.properties.SetBuffer(CmdDraw.vertexBufferId, _computeBuffer);
cmd.properties.SetBuffer(CmdDraw.indexBufferId, _indexBuffer);
cmd.properties.SetInt(CmdDraw.startIndexId, _startIndex);
cmdBuf.DrawProcedural(Matrix4x4.identity, cmd.material, cmd.pass, MeshTopology.Triangles, mesh.triangles.Count, 1, cmd.properties.mpb);
}
else {

正在加载...
取消
保存