using System.Collections; using System.Collections.Generic; using UnityEngine; namespace UltraCombos { public class StructuredBuffer : MonoBehaviour { public string bufferName = "ssbo"; public int count { get { return (buffer != null) ? buffer.count : 0; } } public int stride { get { return (buffer != null) ? buffer.stride : 0; } } public ComputeBuffer obj { get { return buffer; } } ComputeBuffer buffer = null; public bool IsValid { get { return (buffer != null); } } System.IntPtr pointer; public System.IntPtr handle { get { if (pointer == System.IntPtr.Zero) pointer = obj.GetNativeBufferPtr(); return pointer; } } #if false [SerializeField] string debug; private void FixedUpdate() { debug = string.Format("count: {0}, stride: {1}", count, stride); } #endif private void OnDestroy() { Release(); } public void Allocate(int count, int stride) { Release(); buffer = new ComputeBuffer(count, stride, ComputeBufferType.Default); //Debug.LogFormat("ALLOCATE: count({0}), stride({1})", count, stride); } public void SetData(List data) { int data_count = data.Count; int data_stride = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)); if (buffer == null) { Allocate(data_count, data_stride); } else if (buffer.count * buffer.stride != data_count * data_stride) { Allocate(data_count, data_stride); } buffer.SetData(data); } public void Release() { if (buffer != null) { buffer.Release(); } } } }