2
0

ComputeQueue.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/Reference.h>
  6. #include <Jolt/Core/NonCopyable.h>
  7. #include <Jolt/Core/Result.h>
  8. JPH_NAMESPACE_BEGIN
  9. class ComputeShader;
  10. class ComputeBuffer;
  11. /// A command queue for executing compute workloads on the GPU.
  12. ///
  13. /// Note that only a single thread should be using a ComputeQueue at any time (although an implementation could be made that is thread safe).
  14. class JPH_EXPORT ComputeQueue : public RefTarget<ComputeQueue>, public NonCopyable
  15. {
  16. public:
  17. JPH_OVERRIDE_NEW_DELETE
  18. /// Destructor
  19. virtual ~ComputeQueue() = default;
  20. /// Activate a shader. Shader must be set first before buffers can be bound.
  21. /// After every Dispatch call, the shader must be set again and all buffers must be bound again.
  22. virtual void SetShader(const ComputeShader *inShader) = 0;
  23. /// If a barrier should be placed before accessing the buffer
  24. enum class EBarrier
  25. {
  26. Yes,
  27. No
  28. };
  29. /// Bind a constant buffer to the shader. Note that the contents of the buffer cannot be modified until execution finishes.
  30. /// A reference to the buffer is added to make sure it stays alive until execution finishes.
  31. /// @param inName Name of the buffer as specified in the shader.
  32. /// @param inBuffer The buffer to bind.
  33. virtual void SetConstantBuffer(const char *inName, const ComputeBuffer *inBuffer) = 0;
  34. /// Bind a read only buffer to the shader. Note that the contents of the buffer cannot be modified on CPU until execution finishes (only relevant for buffers of type UploadBuffer).
  35. /// A reference to the buffer is added to make sure it stays alive until execution finishes.
  36. /// @param inName Name of the buffer as specified in the shader.
  37. /// @param inBuffer The buffer to bind.
  38. virtual void SetBuffer(const char *inName, const ComputeBuffer *inBuffer) = 0;
  39. /// Bind a read/write buffer to the shader.
  40. /// A reference to the buffer is added to make sure it stays alive until execution finishes.
  41. /// @param inName Name of the buffer as specified in the shader.
  42. /// @param inBuffer The buffer to bind.
  43. /// @param inBarrier If set to Yes, a barrier will be placed before accessing the buffer to ensure all previous writes to the buffer are visible.
  44. virtual void SetRWBuffer(const char *inName, ComputeBuffer *inBuffer, EBarrier inBarrier = EBarrier::Yes) = 0;
  45. /// Dispatch a compute shader with the specified number of thread groups
  46. virtual void Dispatch(uint inThreadGroupsX, uint inThreadGroupsY = 1, uint inThreadGroupsZ = 1) = 0;
  47. /// Schedule buffer to be copied from GPU to CPU.
  48. /// A reference to the buffers is added to make sure they stay alive until execution finishes.
  49. virtual void ScheduleReadback(ComputeBuffer *inDst, const ComputeBuffer *inSrc) = 0;
  50. /// Execute accumulated command list.
  51. /// No more commands can be added until Wait is called.
  52. virtual void Execute() = 0;
  53. /// After executing, this waits until execution is done.
  54. /// This also makes sure that any readback operations have completed and the data is available on CPU.
  55. virtual void Wait() = 0;
  56. /// Execute and wait for the command list to finish
  57. /// @see Execute, Wait
  58. void ExecuteAndWait()
  59. {
  60. Execute();
  61. Wait();
  62. }
  63. };
  64. using ComputeQueueResult = Result<Ref<ComputeQueue>>;
  65. JPH_NAMESPACE_END