ComputeSystem.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifdef JPH_USE_CPU_COMPUTE
  32. /// Factory function to create a compute system that falls back to CPU.
  33. /// This is intended mainly for debugging purposes and is not optimized for performance
  34. extern JPH_EXPORT ComputeSystemResult CreateComputeSystemCPU();
  35. #endif
  36. #ifdef JPH_USE_DX12
  37. /// Factory function to create a compute system using DirectX 12
  38. extern JPH_EXPORT ComputeSystemResult CreateComputeSystemDX12();
  39. /// Factory function to create the default compute system for this platform
  40. inline ComputeSystemResult CreateComputeSystem() { return CreateComputeSystemDX12(); }
  41. #elif defined(JPH_USE_MTL)
  42. /// Factory function to create a compute system using Metal
  43. extern JPH_EXPORT ComputeSystemResult CreateComputeSystemMTL();
  44. /// Factory function to create the default compute system for this platform
  45. inline ComputeSystemResult CreateComputeSystem() { return CreateComputeSystemMTL(); }
  46. #elif defined(JPH_USE_VK)
  47. /// Factory function to create the default compute system for this platform
  48. inline ComputeSystemResult CreateComputeSystem() { return CreateComputeSystemVK(); }
  49. #else
  50. /// Fallback implementation when no compute system is available
  51. inline ComputeSystemResult CreateComputeSystem() { ComputeSystemResult result; result.SetError("Not implemented"); return result; }
  52. #endif
  53. JPH_NAMESPACE_END