You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

50 lines
1.2 KiB

using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
namespace FidelityFX
{
/// <summary>
/// Convenience class for handling a constants buffer containing a single struct item.
/// This wraps the compute buffer and the value array, as well as providing easy access to both.
/// </summary>
public class ConstantsBuffer<TConst>
where TConst: struct
{
private ComputeBuffer _computeBuffer;
private readonly TConst[] _constArray = { new TConst() };
public ref TConst Value => ref _constArray[0];
public static ConstantsBuffer<TConst> Create()
{
ConstantsBuffer<TConst> buffer = new();
buffer.Init();
return buffer;
}
public void Init()
{
_computeBuffer = new ComputeBuffer(1, Marshal.SizeOf<TConst>(), ComputeBufferType.Constant);
}
public void UpdateBufferData(CommandBuffer commandBuffer)
{
commandBuffer.SetBufferData(_computeBuffer, _constArray);
}
public void Destroy()
{
if (_computeBuffer == null)
return;
_computeBuffer.Release();
_computeBuffer = null;
}
public static implicit operator ComputeBuffer(ConstantsBuffer<TConst> constants)
{
return constants._computeBuffer;
}
}
}