2
0

ComputeBuffer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/Core/Reference.h>
  6. #include <Jolt/Core/NonCopyable.h>
  7. #include <Jolt/Core/Result.h>
  8. JPH_NAMESPACE_BEGIN
  9. class ComputeBuffer;
  10. using ComputeBufferResult = Result<Ref<ComputeBuffer>>;
  11. /// Buffer that can be read from / written to by a compute shader
  12. class JPH_EXPORT ComputeBuffer : public RefTarget<ComputeBuffer>, public NonCopyable
  13. {
  14. public:
  15. JPH_OVERRIDE_NEW_DELETE
  16. /// Type of buffer
  17. enum class EType
  18. {
  19. UploadBuffer, ///< Buffer that can be written on the CPU and then uploaded to the GPU.
  20. ReadbackBuffer, ///< Buffer to be sent from the GPU to the CPU, used to read back data.
  21. ConstantBuffer, ///< A smallish buffer that is used to pass constants to a shader.
  22. Buffer, ///< Buffer that can be read from by a shader. Must be initialized with data at construction time and is read only thereafter.
  23. RWBuffer, ///< Buffer that can be read from and written to by a shader.
  24. };
  25. /// Constructor / Destructor
  26. ComputeBuffer(EType inType, uint64 inSize, uint inStride) : mType(inType), mSize(inSize), mStride(inStride) { }
  27. virtual ~ComputeBuffer() { JPH_ASSERT(!mIsMapped); }
  28. /// Properties
  29. EType GetType() const { return mType; }
  30. uint64 GetSize() const { return mSize; }
  31. uint GetStride() const { return mStride; }
  32. /// Mode in which the buffer is accessed
  33. enum class EMode
  34. {
  35. Read, ///< Read only access to the buffer
  36. Write, ///< Write only access to the buffer (this will discard all previous data in the buffer)
  37. };
  38. /// Map / unmap buffer (get pointer to data).
  39. void * Map(EMode inMode) { JPH_ASSERT(!mIsMapped); JPH_IF_ENABLE_ASSERTS(mIsMapped = true;) return MapInternal(inMode); }
  40. template <typename T> T * Map(EMode inMode) { JPH_ASSERT(!mIsMapped); JPH_IF_ENABLE_ASSERTS(mIsMapped = true;) JPH_ASSERT(sizeof(T) == mStride); return reinterpret_cast<T *>(MapInternal(inMode)); }
  41. void Unmap() { JPH_ASSERT(mIsMapped); JPH_IF_ENABLE_ASSERTS(mIsMapped = false;) UnmapInternal(); }
  42. /// Create a readback buffer of the same size and stride that can be used to read the data stored in this buffer on CPU.
  43. /// Note that this could also be implemented as 'return this' in case the underlying implementation allows locking GPU data on CPU directly.
  44. virtual ComputeBufferResult CreateReadBackBuffer() const = 0;
  45. protected:
  46. EType mType;
  47. uint64 mSize;
  48. uint mStride;
  49. #ifdef JPH_ENABLE_ASSERTS
  50. bool mIsMapped = false;
  51. #endif // JPH_ENABLE_ASSERTS
  52. virtual void * MapInternal(EMode inMode) = 0;
  53. virtual void UnmapInternal() = 0;
  54. };
  55. JPH_NAMESPACE_END