BatchShaderBind.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Unity.Collections;
  2. using Unity.Mathematics;
  3. using UnityEngine;
  4. namespace Core.BRG
  5. {
  6. public class BatchShaderBind
  7. {
  8. public BRGShaderValue shaderValue;
  9. public int offset;
  10. private NativeArray<float4x4> _f4x4Buffer;
  11. private NativeArray<float3x4> _f3x4Buffer;
  12. private NativeArray<float4> _f4Buffer;
  13. private NativeArray<float> _f1Buffer;
  14. private int _count;
  15. private int _size;
  16. private void InitBuffer(int count)
  17. {
  18. this._count = count;
  19. _size = shaderValue.GetSize();
  20. switch (shaderValue.ValueType)
  21. {
  22. case BRGShaderValueType.F4X4:
  23. _f4x4Buffer = new NativeArray<float4x4>(count, Allocator.Persistent);
  24. break;
  25. case BRGShaderValueType.F3X4:
  26. _f3x4Buffer = new NativeArray<float3x4>(count, Allocator.Persistent);
  27. break;
  28. case BRGShaderValueType.F4:
  29. _f4Buffer = new NativeArray<float4>(count, Allocator.Persistent);
  30. break;
  31. case BRGShaderValueType.F1:
  32. _f1Buffer = new NativeArray<float>(count, Allocator.Persistent);
  33. break;
  34. }
  35. }
  36. public void SetData(GraphicsBuffer graphicsBuffer)
  37. {
  38. InitBuffer(graphicsBuffer.count);
  39. switch (shaderValue.ValueType)
  40. {
  41. case BRGShaderValueType.F4X4:
  42. graphicsBuffer.SetData(_f4x4Buffer, 0, offset / _size, _count);
  43. break;
  44. case BRGShaderValueType.F3X4:
  45. graphicsBuffer.SetData(_f3x4Buffer, 0, offset / _size, _count);
  46. break;
  47. case BRGShaderValueType.F4:
  48. graphicsBuffer.SetData(_f4Buffer, 0, offset / _size, _count);
  49. break;
  50. case BRGShaderValueType.F1:
  51. graphicsBuffer.SetData(_f1Buffer, 0, offset / _size, graphicsBuffer.count);
  52. break;
  53. }
  54. }
  55. }
  56. }