2
0

CmGpuResourceData.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include "CmPrerequisites.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 CM_EXPORT GpuResourceData : public IReflectable
  21. {
  22. public:
  23. GpuResourceData();
  24. GpuResourceData(const GpuResourceData& copy);
  25. virtual ~GpuResourceData();
  26. /**
  27. * @brief Returns pointer to the internal buffer.
  28. */
  29. UINT8* getData() const;
  30. /**
  31. * @brief Allocates an internal buffer of a certain size. If there is another
  32. * buffer already allocated, it will be freed and new one will be allocated.
  33. * Buffer size is determined based on parameters used for initializing the class.
  34. */
  35. void allocateInternalBuffer();
  36. /**
  37. * @brief Allocates an internal buffer of a certain size. If there is another
  38. * buffer already allocated, it will be freed and new one will be allocated.
  39. *
  40. * @param size The size of the buffer in bytes.
  41. */
  42. void allocateInternalBuffer(UINT32 size);
  43. /**
  44. * @brief Frees the internal buffer that was allocated using "allocateInternal". Called automatically
  45. * when the instance of the class is destroyed.
  46. */
  47. void freeInternalBuffer();
  48. /**
  49. * @brief Makes the internal data pointer point to some external data. No copying is done,
  50. * so you must ensure that external data exists as long as this class uses it. You are also
  51. * responsible for deleting the data when you are done with it.
  52. *
  53. * @note If any internal data is allocated, it is freed.
  54. */
  55. void setExternalBuffer(UINT8* data);
  56. /**
  57. * @brief Locks the data and makes it available only to the core thread.
  58. *
  59. * @note Internal method.
  60. */
  61. void _lock() const;
  62. /**
  63. * @brief Unlocks the data and makes it available to all threads.
  64. *
  65. * @note Internal method.
  66. */
  67. void _unlock() const;
  68. protected:
  69. /**
  70. * @brief Returns the size of the internal buffer in bytes. This is calculated based
  71. * on parameters provided upon construction and specific implementation details.
  72. */
  73. virtual UINT32 getInternalBufferSize() = 0;
  74. private:
  75. UINT8* mData;
  76. bool mOwnsData;
  77. mutable bool mLocked;
  78. /************************************************************************/
  79. /* SERIALIZATION */
  80. /************************************************************************/
  81. public:
  82. friend class GpuResourceDataRTTI;
  83. static RTTITypeBase* getRTTIStatic();
  84. virtual RTTITypeBase* getRTTI() const;
  85. };
  86. }