2
0

ComputeQueueVK.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/Compute/ComputeQueue.h>
  6. #ifdef JPH_USE_VK
  7. #include <Jolt/Compute/VK/ComputeShaderVK.h>
  8. #include <Jolt/Compute/VK/BufferVK.h>
  9. #include <Jolt/Core/UnorderedMap.h>
  10. #include <Jolt/Core/UnorderedSet.h>
  11. JPH_NAMESPACE_BEGIN
  12. class ComputeSystemVK;
  13. class ComputeBufferVK;
  14. /// A command queue for Vulkan for executing compute workloads on the GPU.
  15. class JPH_EXPORT ComputeQueueVK final : public ComputeQueue
  16. {
  17. public:
  18. JPH_OVERRIDE_NEW_DELETE
  19. /// Constructor / Destructor
  20. explicit ComputeQueueVK(ComputeSystemVK *inComputeSystem) : mComputeSystem(inComputeSystem) { }
  21. virtual ~ComputeQueueVK() override;
  22. /// Initialize the queue
  23. bool Initialize(uint32 inComputeQueueIndex, ComputeQueueResult &outResult);
  24. // See: ComputeQueue
  25. virtual void SetShader(const ComputeShader *inShader) override;
  26. virtual void SetConstantBuffer(const char *inName, const ComputeBuffer *inBuffer) override;
  27. virtual void SetBuffer(const char *inName, const ComputeBuffer *inBuffer) override;
  28. virtual void SetRWBuffer(const char *inName, ComputeBuffer *inBuffer, EBarrier inBarrier = EBarrier::Yes) override;
  29. virtual void ScheduleReadback(ComputeBuffer *inDst, const ComputeBuffer *inSrc) override;
  30. virtual void Dispatch(uint inThreadGroupsX, uint inThreadGroupsY, uint inThreadGroupsZ) override;
  31. virtual void Execute() override;
  32. virtual void Wait() override;
  33. private:
  34. bool BeginCommandBuffer();
  35. // Copy the CPU buffer to the GPU buffer if needed
  36. void SyncCPUToGPU(const ComputeBufferVK *inBuffer);
  37. ComputeSystemVK * mComputeSystem;
  38. VkQueue mQueue = VK_NULL_HANDLE;
  39. VkCommandPool mCommandPool = VK_NULL_HANDLE;
  40. VkDescriptorPool mDescriptorPool = VK_NULL_HANDLE;
  41. VkCommandBuffer mCommandBuffer = VK_NULL_HANDLE;
  42. bool mCommandBufferRecording = false; ///< If we are currently recording commands into the command buffer
  43. VkFence mFence = VK_NULL_HANDLE;
  44. bool mIsExecuting = false; ///< If Execute has been called and we are waiting for it to finish
  45. RefConst<ComputeShaderVK> mShader; ///< Shader that has been activated
  46. Array<VkDescriptorBufferInfo> mBufferInfos; ///< List of parameters that will be sent to the current shader
  47. 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
  48. Array<BufferVK> mDelayedFreedBuffers; ///< Hardware buffers that need to be freed after execution is done
  49. };
  50. JPH_NAMESPACE_END
  51. #endif // JPH_USE_VK