2
0

ComputeSystem.h 2.9 KB

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