BsGpuResourceData.h 3.6 KB

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