何时释放()ComputeBuffer

时间:2019-07-16 18:04:14

标签: c# unity3d graphics shader

我正在使用ComputeBuffers在我的应用程序中进行渲染。

我使用了两组:实时和持久。

当用户执行某项操作时,我会每帧更新一次实时缓冲区集;每当用户完成某项操作时,我都会更新持久性缓冲区集。

这是我“重置”实时缓冲区集的方式:

public void UpdatePersistentBuffers()
    {
        Vector4[] t = new Vector4[_realTimeData.InstanceCount];
        Vector4[] c = new Vector4[_realTimeData.InstanceCount];

        _realTimeData.TransformBuffer.GetData(t);
        _realTimeData.ColorBuffer.GetData(c);

        for (int i = 0; i < t.Length; i++)
        {
            _persistentData.AddElement(t[i], c[i]);
        }

        _persistentData.UpdateArgsBuffer();
        _persistentData.UpdateDataBuffers();

        _realTimeData.InstanceCount = 0;
        _realTimeData.CachedInstanceCount = 0;
    }

这是RendererData类:

public class RendererData
{
    public readonly int Capacity;
    public readonly Mesh InstanceMesh;
    public readonly Material InstanceMaterial;
    public int InstanceCount;
    public int CachedInstanceCount;
    public readonly ComputeBuffer ArgsBuffer;
    public readonly ComputeBuffer TransformBuffer;
    public readonly ComputeBuffer ColorBuffer;

    private readonly uint[] _args;
    private readonly Vector4[] _transforms;
    private readonly Vector4[] _colors;

    public RendererData(int capacity, Mesh instanceMesh, Material instanceMaterial, string transformBufferName,
        string colorBufferName)
    {
        Capacity = capacity;
        InstanceMesh = instanceMesh;
        InstanceMaterial = instanceMaterial;
        InstanceCount = 0;
        CachedInstanceCount = 0;

        uint numIndices = (InstanceMesh != null) ? InstanceMesh.GetIndexCount(0) : 0;
        _args = new uint[] { 0, 0, 0, 0, 0 };
        _args[0] = numIndices;
        _args[1] = (uint)InstanceCount;
        ArgsBuffer = new ComputeBuffer(1, _args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
        ArgsBuffer.SetData(_args);

        _transforms = new Vector4[Capacity];
        TransformBuffer = new ComputeBuffer(Capacity, sizeof(float) * 4);
        InstanceMaterial.SetBuffer(transformBufferName, TransformBuffer);

        _colors = new Vector4[Capacity];
        ColorBuffer = new ComputeBuffer(Capacity, sizeof(float) * 4);
        InstanceMaterial.SetBuffer(colorBufferName, ColorBuffer);
    }

    public void AddElement(Vector4 transform, Vector4 color)
    {
        _transforms[InstanceCount] = transform;
        _colors[InstanceCount] = color;
        InstanceCount++;
    }

    public void UpdateArgsBuffer()
    {
        _args[1] = (uint)InstanceCount;
        ArgsBuffer.SetData(_args);
    }

    public void UpdateDataBuffers()
    {
        TransformBuffer.SetData(_transforms, 0, 0, InstanceCount);
        ColorBuffer.SetData(_colors, 0, 0, InstanceCount);
        CachedInstanceCount = InstanceCount;
    }

    public void Release()
    {
        TransformBuffer.Release();
        ColorBuffer.Release();
        ArgsBuffer.Release();
    }

一旦调用UpdatePersistentBuffers(),我应该释放实时缓冲区并在其位置创建新缓冲区吗?

0 个答案:

没有答案