ComputeSystem.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/ComputeShader.h>
  6. #include <Jolt/Compute/ComputeBuffer.h>
  7. #include <Jolt/Compute/ComputeQueue.h>
  8. JPH_NAMESPACE_BEGIN
  9. /// Interface to run a workload on the GPU
  10. class JPH_EXPORT ComputeSystem : public RefTarget<ComputeSystem>, public NonCopyable
  11. {
  12. public:
  13. JPH_OVERRIDE_NEW_DELETE
  14. /// Destructor
  15. virtual ~ComputeSystem() = default;
  16. /// Compile a compute shader
  17. virtual ComputeShaderResult CreateComputeShader(const char *inName, uint32 inGroupSizeX, uint32 inGroupSizeY = 1, uint32 inGroupSizeZ = 1) = 0;
  18. /// Create a buffer for use with a compute shader
  19. virtual ComputeBufferResult CreateComputeBuffer(ComputeBuffer::EType inType, uint64 inSize, uint inStride, const void *inData = nullptr) = 0;
  20. /// Create a queue for executing compute shaders
  21. virtual ComputeQueueResult CreateComputeQueue() = 0;
  22. /// Callback used when loading shaders
  23. using ShaderLoader = std::function<bool(const char *inName, Array<uint8> &outData, String &outError)>;
  24. ShaderLoader mShaderLoader = [](const char *, Array<uint8> &, String &outError) { JPH_ASSERT(false, "Override this function"); outError = "Not implemented"; return false; };
  25. };
  26. using ComputeSystemResult = Result<Ref<ComputeSystem>>;
  27. #ifdef JPH_USE_VK
  28. /// Factory function to create a compute system using Vulkan
  29. extern JPH_EXPORT ComputeSystemResult CreateComputeSystemVK();
  30. #endif
  31. /// Factory function to create a compute system that falls back to CPU.
  32. /// This is intended mainly for debugging purposes and is not optimized for performance
  33. extern JPH_EXPORT ComputeSystemResult CreateComputeSystemCPU();
  34. #ifdef JPH_USE_DX12
  35. /// Factory function to create a compute system using DirectX 12
  36. extern JPH_EXPORT ComputeSystemResult CreateComputeSystemDX12();
  37. /// Factory function to create the default compute system for this platform
  38. inline ComputeSystemResult CreateComputeSystem() { return CreateComputeSystemDX12(); }
  39. #elif defined(JPH_USE_MTL)
  40. /// Factory function to create a compute system using Metal
  41. extern JPH_EXPORT ComputeSystemResult CreateComputeSystemMTL();
  42. /// Factory function to create the default compute system for this platform
  43. inline ComputeSystemResult CreateComputeSystem() { return CreateComputeSystemMTL(); }
  44. #elif defined(JPH_USE_VK)
  45. /// Factory function to create the default compute system for this platform
  46. inline ComputeSystemResult CreateComputeSystem() { return CreateComputeSystemVK(); }
  47. #else
  48. /// Fallback implementation when no compute system is available
  49. inline ComputeSystemResult CreateComputeSystem() { ComputeSystemResult result; result.SetError("Not implemented"); return result; }
  50. #endif
  51. JPH_NAMESPACE_END