2
0

ComputeQueueDX12.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #ifdef JPH_USE_DX12
  6. #include <Jolt/Compute/ComputeQueue.h>
  7. #include <Jolt/Compute/DX12/ComputeShaderDX12.h>
  8. #include <Jolt/Core/UnorderedSet.h>
  9. JPH_NAMESPACE_BEGIN
  10. class ComputeBufferDX12;
  11. /// A command queue for DirectX for executing compute workloads on the GPU.
  12. class JPH_EXPORT ComputeQueueDX12 final : public ComputeQueue
  13. {
  14. public:
  15. JPH_OVERRIDE_NEW_DELETE
  16. /// Destructor
  17. virtual ~ComputeQueueDX12() override;
  18. /// Initialize the queue
  19. bool Initialize(ID3D12Device *inDevice, D3D12_COMMAND_LIST_TYPE inType, ComputeQueueResult &outResult);
  20. /// Start the command list (requires waiting until the previous one is finished)
  21. ID3D12GraphicsCommandList * Start();
  22. // See: ComputeQueue
  23. virtual void SetShader(const ComputeShader *inShader) override;
  24. virtual void SetConstantBuffer(const char *inName, const ComputeBuffer *inBuffer) override;
  25. virtual void SetBuffer(const char *inName, const ComputeBuffer *inBuffer) override;
  26. virtual void SetRWBuffer(const char *inName, ComputeBuffer *inBuffer, EBarrier inBarrier = EBarrier::Yes) override;
  27. virtual void ScheduleReadback(ComputeBuffer *inDst, const ComputeBuffer *inSrc) override;
  28. virtual void Dispatch(uint inThreadGroupsX, uint inThreadGroupsY, uint inThreadGroupsZ) override;
  29. virtual void Execute() override;
  30. virtual void Wait() override;
  31. private:
  32. /// Copy the CPU buffer to the GPU buffer if needed
  33. void SyncCPUToGPU(const ComputeBufferDX12 *inBuffer);
  34. ComPtr<ID3D12CommandQueue> mCommandQueue; ///< The command queue that will hold command lists
  35. ComPtr<ID3D12CommandAllocator> mCommandAllocator; ///< Allocator that holds the memory for the commands
  36. ComPtr<ID3D12GraphicsCommandList> mCommandList; ///< The command list that will hold the render commands / state changes
  37. HANDLE mFenceEvent = INVALID_HANDLE_VALUE; ///< Fence event, used to wait for rendering to complete
  38. ComPtr<ID3D12Fence> mFence; ///< Fence object, used to signal the fence event
  39. UINT64 mFenceValue = 0; ///< Current fence value, each time we need to wait we will signal the fence with this value, wait for it and then increase the value
  40. RefConst<ComputeShaderDX12> mShader = nullptr; ///< Current active shader
  41. bool mIsStarted = false; ///< If the command list has been started (reset) and is ready to record commands
  42. bool mIsExecuting = false; ///< If a command list is currently executing on the queue
  43. UnorderedSet<RefConst<ComputeBuffer>> mUsedBuffers; ///< Buffers that are in use by the current execution, these will be retained until execution is finished so that we don't free buffers that are in use
  44. Array<ComPtr<ID3D12Resource>> mDelayedFreedBuffers; ///< Buffers freed during the execution
  45. };
  46. JPH_NAMESPACE_END
  47. #endif // JPH_USE_DX12