BsGpuResourceData.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Implementation
  6. * @{
  7. */
  8. /**
  9. * You can use this class as a storage for reading and writing from/to various GPU resources. It is meant to be created
  10. * on sim thread and used on the core thread. This class is abstract and specific resource types need to implement their
  11. * own type of GpuResourceData.
  12. *
  13. * @note
  14. * Normal use of this class involves requesting an instance of GpuResourceData from a Resource, then scheduling a read
  15. * or write on that resource using the provided instance. Instance will be locked while it is used by the core thread
  16. * and sim thread will be allowed to access it when the operation ends. Caller can track AsyncOp%s regarding the
  17. * read/write operation to be notified when it is complete.
  18. * @note
  19. * If you allocate an internal buffer to store the resource data, the ownership of the buffer will always remain with
  20. * the initial instance of the class. If that initial instance is deleted, any potential copies will point to garbage
  21. * data.
  22. */
  23. class BS_CORE_EXPORT GpuResourceData : public IReflectable
  24. {
  25. public:
  26. GpuResourceData();
  27. GpuResourceData(const GpuResourceData& copy);
  28. virtual ~GpuResourceData();
  29. GpuResourceData& operator=(const GpuResourceData& rhs);
  30. /** Returns pointer to the internal buffer. */
  31. UINT8* getData() const;
  32. /**
  33. * Allocates an internal buffer of a certain size. If there is another buffer already allocated, it will be freed
  34. * and new one will be allocated. Buffer size is determined based on parameters used for initializing the class.
  35. */
  36. void allocateInternalBuffer();
  37. /**
  38. * Allocates an internal buffer of a certain size. If there is another buffer already allocated, it will be freed
  39. * and new one will be allocated.
  40. *
  41. * @param[in] size The size of the buffer in bytes.
  42. */
  43. void allocateInternalBuffer(UINT32 size);
  44. /**
  45. * Frees the internal buffer that was allocated using allocateInternalBuffer(). Called automatically when the
  46. * instance of the class is destroyed.
  47. */
  48. void freeInternalBuffer();
  49. /**
  50. * Makes the internal data pointer point to some external data. No copying is done, so you must ensure that external
  51. * data exists as long as this class uses it. You are also responsible for deleting the data when you are done
  52. * with it.
  53. *
  54. * @note If any internal data is allocated, it is freed.
  55. */
  56. void setExternalBuffer(UINT8* data);
  57. /** Checks if the internal buffer is locked due to some other thread using it. */
  58. bool isLocked() const { return mLocked; }
  59. /** Locks the data and makes it available only to the core thread. */
  60. void _lock() const;
  61. /** Unlocks the data and makes it available to all threads. */
  62. void _unlock() const;
  63. protected:
  64. /**
  65. * Returns the size of the internal buffer in bytes. This is calculated based on parameters provided upon
  66. * construction and specific implementation details.
  67. */
  68. virtual UINT32 getInternalBufferSize() const = 0;
  69. private:
  70. UINT8* mData;
  71. bool mOwnsData;
  72. mutable bool mLocked;
  73. /************************************************************************/
  74. /* SERIALIZATION */
  75. /************************************************************************/
  76. public:
  77. friend class GpuResourceDataRTTI;
  78. static RTTITypeBase* getRTTIStatic();
  79. virtual RTTITypeBase* getRTTI() const override;
  80. };
  81. /** @} */
  82. }