ShaderData.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. class ShaderData : IDisposable
  6. {
  7. static ShaderData m_Instance = null;
  8. ComputeBuffer m_LightDataBuffer = null;
  9. ComputeBuffer m_LightIndicesBuffer = null;
  10. ComputeBuffer m_ShadowDataBuffer = null;
  11. ComputeBuffer m_ShadowIndicesBuffer = null;
  12. ShaderData()
  13. {
  14. }
  15. internal static ShaderData instance
  16. {
  17. get
  18. {
  19. if (m_Instance == null)
  20. m_Instance = new ShaderData();
  21. return m_Instance;
  22. }
  23. }
  24. public void Dispose()
  25. {
  26. DisposeBuffer(ref m_LightDataBuffer);
  27. DisposeBuffer(ref m_LightIndicesBuffer);
  28. DisposeBuffer(ref m_ShadowDataBuffer);
  29. DisposeBuffer(ref m_ShadowIndicesBuffer);
  30. }
  31. internal ComputeBuffer GetLightDataBuffer(int size)
  32. {
  33. return GetOrUpdateBuffer<ShaderInput.LightData>(ref m_LightDataBuffer, size);
  34. }
  35. internal ComputeBuffer GetLightIndicesBuffer(int size)
  36. {
  37. return GetOrUpdateBuffer<int>(ref m_LightIndicesBuffer, size);
  38. }
  39. internal ComputeBuffer GetShadowDataBuffer(int size)
  40. {
  41. return GetOrUpdateBuffer<ShaderInput.ShadowData>(ref m_ShadowDataBuffer, size);
  42. }
  43. internal ComputeBuffer GetShadowIndicesBuffer(int size)
  44. {
  45. return GetOrUpdateBuffer<int>(ref m_ShadowIndicesBuffer, size);
  46. }
  47. ComputeBuffer GetOrUpdateBuffer<T>(ref ComputeBuffer buffer, int size) where T : struct
  48. {
  49. if (buffer == null)
  50. {
  51. buffer = new ComputeBuffer(size, Marshal.SizeOf<T>());
  52. }
  53. else if (size > buffer.count)
  54. {
  55. buffer.Dispose();
  56. buffer = new ComputeBuffer(size, Marshal.SizeOf<T>());
  57. }
  58. return buffer;
  59. }
  60. void DisposeBuffer(ref ComputeBuffer buffer)
  61. {
  62. if (buffer != null)
  63. {
  64. buffer.Dispose();
  65. buffer = null;
  66. }
  67. }
  68. }
  69. }