No Description
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.

GraphicsHelpers.cs 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Unity.Mathematics;
  2. namespace UnityEngine.Rendering.UnifiedRayTracing
  3. {
  4. internal class GraphicsHelpers
  5. {
  6. static public void CopyBuffer(ComputeShader copyShader, CommandBuffer cmd, GraphicsBuffer src, int srcOffsetInDWords, GraphicsBuffer dst, int dstOffsetInDwords, int sizeInDWords)
  7. {
  8. const int groupSize = 256;
  9. const int elementsPerThread = 8;
  10. const int maxThreadGroups = 65535; // gfx device limitation
  11. const int maxBatchSizeInDWords = groupSize * elementsPerThread * maxThreadGroups;
  12. int remainingDWords = sizeInDWords;
  13. cmd.SetComputeBufferParam(copyShader, 0, "_SrcBuffer", src);
  14. cmd.SetComputeBufferParam(copyShader, 0, "_DstBuffer", dst);
  15. while (remainingDWords > 0)
  16. {
  17. int batchSize = math.min(remainingDWords, maxBatchSizeInDWords);
  18. cmd.SetComputeIntParam(copyShader, "_SrcOffset", srcOffsetInDWords);
  19. cmd.SetComputeIntParam(copyShader, "_DstOffset", dstOffsetInDwords);
  20. cmd.SetComputeIntParam(copyShader, "_Size", batchSize);
  21. cmd.DispatchCompute(copyShader, 0, DivUp(batchSize, elementsPerThread * groupSize), 1, 1);
  22. remainingDWords -= batchSize;
  23. srcOffsetInDWords += batchSize;
  24. dstOffsetInDwords += batchSize;
  25. }
  26. }
  27. static public void CopyBuffer(ComputeShader copyShader, GraphicsBuffer src, int srcOffsetInDWords, GraphicsBuffer dst, int dstOffsetInDwords, int sizeInDwords)
  28. {
  29. CommandBuffer cmd = new CommandBuffer();
  30. CopyBuffer(copyShader, cmd, src, srcOffsetInDWords, dst, dstOffsetInDwords, sizeInDwords);
  31. Graphics.ExecuteCommandBuffer(cmd);
  32. }
  33. static public int DivUp(int x, int y) => (x + y - 1) / y;
  34. static public int DivUp(int x, uint y) => (x + (int)y - 1) / (int)y;
  35. static public uint DivUp(uint x, uint y) => (x + y - 1) / y;
  36. static public uint3 DivUp(uint3 x, uint3 y) => (x + y - 1) / y;
  37. /// <summary>
  38. /// Immediately executes the pending work on the command buffer.
  39. /// This is useful for preventing TDR, which can happen when scheduling too much work in one CommandBuffer.
  40. /// </summary>
  41. /// <param name="cmd">Command buffer to execute.</param>
  42. static public void Flush(CommandBuffer cmd)
  43. {
  44. Graphics.ExecuteCommandBuffer(cmd);
  45. cmd.Clear();
  46. GL.Flush();
  47. }
  48. }
  49. }