BsGpuResourceData.h 3.4 KB

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