BsGpuResourceData.h 3.7 KB

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