Нема описа
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.

BLASPositionsPool.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using Unity.Mathematics;
  3. using UnityEngine.Assertions;
  4. using UnityEngine.Rendering.RadeonRays;
  5. namespace UnityEngine.Rendering.UnifiedRayTracing
  6. {
  7. internal class BLASPositionsPool : IDisposable
  8. {
  9. public BLASPositionsPool(ComputeShader copyPositionsShader, ComputeShader copyShader)
  10. {
  11. m_VerticesBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, intialVertexCount*3, 4);
  12. m_VerticesAllocator = new BlockAllocator();
  13. m_VerticesAllocator.Initialize(intialVertexCount);
  14. m_IndicesBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, initialIndexCount, 4);
  15. m_IndicesAllocator = new BlockAllocator();
  16. m_IndicesAllocator.Initialize(initialIndexCount);
  17. m_CopyPositionsShader = copyPositionsShader;
  18. m_CopyIndicesKernel = m_CopyPositionsShader.FindKernel("CopyIndexBuffer");
  19. m_CopyIndices16Kernel = m_CopyPositionsShader.FindKernel("CopyIndexBuffer16");
  20. m_CopyVerticesKernel = m_CopyPositionsShader.FindKernel("CopyVertexBuffer");
  21. m_CopyShader = copyShader;
  22. }
  23. public void Dispose()
  24. {
  25. m_VerticesBuffer.Dispose();
  26. m_IndicesBuffer.Dispose();
  27. m_VerticesAllocator.Dispose();
  28. m_IndicesAllocator.Dispose();
  29. }
  30. public GraphicsBuffer IndexBuffer { get { return m_IndicesBuffer; } }
  31. public GraphicsBuffer VertexBuffer { get { return m_VerticesBuffer; } }
  32. public void Clear()
  33. {
  34. m_IndicesBuffer.Dispose();
  35. m_IndicesBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, initialIndexCount, 4);
  36. m_IndicesAllocator.Dispose();
  37. m_IndicesAllocator = new BlockAllocator();
  38. m_IndicesAllocator.Initialize(initialIndexCount);
  39. m_VerticesBuffer.Dispose();
  40. m_VerticesBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, intialVertexCount * 3, 4);
  41. m_VerticesAllocator.Dispose();
  42. m_VerticesAllocator = new BlockAllocator();
  43. m_VerticesAllocator.Initialize(intialVertexCount*3);
  44. }
  45. const int initialIndexCount = 1000;
  46. const int intialVertexCount = 1000;
  47. GraphicsBuffer m_VerticesBuffer;
  48. BlockAllocator m_VerticesAllocator;
  49. GraphicsBuffer m_IndicesBuffer;
  50. BlockAllocator m_IndicesAllocator;
  51. ComputeShader m_CopyPositionsShader;
  52. int m_CopyIndicesKernel;
  53. int m_CopyIndices16Kernel;
  54. int m_CopyVerticesKernel;
  55. ComputeShader m_CopyShader;
  56. const uint kItemsPerWorkgroup = 48u * 128u;
  57. public void Add(RadeonRays.MeshBuildInfo info, out BlockAllocator.Allocation indicesAllocation, out BlockAllocator.Allocation verticesAllocation)
  58. {
  59. indicesAllocation = m_IndicesAllocator.Allocate((int)info.triangleCount*3);
  60. if (!indicesAllocation.valid)
  61. {
  62. indicesAllocation = m_IndicesAllocator.GrowAndAllocate((int)info.triangleCount * 3, out int oldCapacity, out int newCapacity);
  63. var newIndexBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, newCapacity, 4);
  64. GraphicsHelpers.CopyBuffer(m_CopyShader, m_IndicesBuffer, 0, newIndexBuffer, 0, oldCapacity);
  65. m_IndicesBuffer.Dispose();
  66. m_IndicesBuffer = newIndexBuffer;
  67. }
  68. verticesAllocation = m_VerticesAllocator.Allocate((int)info.vertexCount*3);
  69. if (!verticesAllocation.valid)
  70. {
  71. verticesAllocation = m_VerticesAllocator.GrowAndAllocate((int)info.vertexCount * 3, out int oldCapacity, out int newCapacity);
  72. var newVertexBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, newCapacity, 4);
  73. GraphicsHelpers.CopyBuffer(m_CopyShader, m_VerticesBuffer, 0, newVertexBuffer, 0, oldCapacity);
  74. m_VerticesBuffer.Dispose();
  75. m_VerticesBuffer = newVertexBuffer;
  76. }
  77. var cmd = new CommandBuffer();
  78. var copyIndicesKernel = info.indexFormat == RadeonRays.IndexFormat.Int32 ? m_CopyIndicesKernel : m_CopyIndices16Kernel;
  79. uint indexCount = info.triangleCount * 3;
  80. cmd.SetComputeIntParam(m_CopyPositionsShader, "_InputIndexCount", (int)indexCount);
  81. cmd.SetComputeIntParam(m_CopyPositionsShader, "_InputIndexBufferOffset", info.indicesStartOffset);
  82. cmd.SetComputeIntParam(m_CopyPositionsShader, "_InputBaseIndex", info.baseIndex);
  83. cmd.SetComputeIntParam(m_CopyPositionsShader, "_OutputIndexBufferOffset", indicesAllocation.block.offset);
  84. cmd.SetComputeBufferParam(m_CopyPositionsShader, copyIndicesKernel, "_InputIndexBuffer", info.triangleIndices);
  85. cmd.SetComputeBufferParam(m_CopyPositionsShader, copyIndicesKernel, "_OutputIndexBuffer", m_IndicesBuffer);
  86. cmd.DispatchCompute(m_CopyPositionsShader, copyIndicesKernel, (int)Common.CeilDivide(indexCount, kItemsPerWorkgroup), 1, 1);
  87. cmd.SetComputeIntParam(m_CopyPositionsShader, "_InputPosBufferCount", (int)info.vertexCount);
  88. cmd.SetComputeIntParam(m_CopyPositionsShader, "_InputPosBufferOffset", info.verticesStartOffset);
  89. cmd.SetComputeIntParam(m_CopyPositionsShader, "_InputBaseVertex", info.baseVertex);
  90. cmd.SetComputeIntParam(m_CopyPositionsShader, "_InputPosBufferStride", (int)info.vertexStride);
  91. cmd.SetComputeIntParam(m_CopyPositionsShader, "_OutputPosBufferOffset", verticesAllocation.block.offset);
  92. cmd.SetComputeBufferParam(m_CopyPositionsShader, m_CopyVerticesKernel, "_InputPosBuffer", info.vertices);
  93. cmd.SetComputeBufferParam(m_CopyPositionsShader, m_CopyVerticesKernel, "_OutputPosBuffer", m_VerticesBuffer);
  94. cmd.DispatchCompute(m_CopyPositionsShader, m_CopyVerticesKernel, (int)Common.CeilDivide(info.vertexCount, kItemsPerWorkgroup), 1, 1);
  95. Graphics.ExecuteCommandBuffer(cmd);
  96. }
  97. public void Remove(ref BlockAllocator.Allocation indicesAllocation, ref BlockAllocator.Allocation verticesAllocation)
  98. {
  99. m_IndicesAllocator.FreeAllocation(indicesAllocation);
  100. m_VerticesAllocator.FreeAllocation(verticesAllocation);
  101. indicesAllocation = BlockAllocator.Allocation.Invalid;
  102. verticesAllocation = BlockAllocator.Allocation.Invalid;
  103. }
  104. }
  105. }