BatchShaderBind.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public 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(int index, float4 data)
  37. {
  38. _f4Buffer[index] = data;
  39. }
  40. public void SetData(int index, float4x4 data)
  41. {
  42. _f4x4Buffer[index] = data;
  43. }
  44. public void SetData(int index, float3x4 data)
  45. {
  46. _f3x4Buffer[index] = data;
  47. }
  48. public void SetData(int index, float data)
  49. {
  50. _f1Buffer[index] = data;
  51. }
  52. public void SetData(GraphicsBuffer graphicsBuffer)
  53. {
  54. // IitBuffer(graphicsBuffer.count);
  55. switch (shaderValue.ValueType)
  56. {
  57. case BRGShaderValueType.F4X4:
  58. graphicsBuffer.SetData(_f4x4Buffer, 0, offset / _size, _count);
  59. break;
  60. case BRGShaderValueType.F3X4:
  61. graphicsBuffer.SetData(_f3x4Buffer, 0, offset / _size, _count);
  62. break;
  63. case BRGShaderValueType.F4:
  64. graphicsBuffer.SetData(_f4Buffer, 0, offset / _size, _count);
  65. break;
  66. case BRGShaderValueType.F1:
  67. graphicsBuffer.SetData(_f1Buffer, 0, offset / _size, graphicsBuffer.count);
  68. break;
  69. }
  70. }
  71. }
  72. }