CmHardwareBuffer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmCommonEnums.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Abstract class defining common features of hardware buffers. Hardware buffers usually
  8. * represent areas of memory the GPU or the driver can access directly.
  9. *
  10. * @note Be aware that reading from non-system memory hardware buffers is usually slow and should be avoided.
  11. */
  12. class CM_EXPORT HardwareBuffer
  13. {
  14. public:
  15. virtual ~HardwareBuffer() {}
  16. /**
  17. * @brief Locks a portion of the buffer and returns pointer to the locked area.
  18. * You must call "unlock" when done.
  19. *
  20. * @param offset Offset in bytes from which to lock the buffer.
  21. * @param length Length of the area you want to lock, in bytes.
  22. * @param options Signifies what you want to do with the returned pointer.
  23. * Caller must ensure not to do anything he hasn't requested.
  24. * (e.g. don't try to read from the buffer unless you requested
  25. * it here).
  26. */
  27. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options)
  28. {
  29. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  30. void* ret = lockImpl(offset, length, options);
  31. mIsLocked = true;
  32. mLockStart = offset;
  33. mLockSize = length;
  34. return ret;
  35. }
  36. /**
  37. * @brief Locks the entire buffer and returns pointer to the locked area.
  38. * You must call "unlock" when done.
  39. *
  40. * @param options Signifies what you want to do with the returned pointer.
  41. * Caller must ensure not to do anything he hasn't requested.
  42. * (e.g. don't try to read from the buffer unless you requested
  43. * it here).
  44. */
  45. void* lock(GpuLockOptions options)
  46. {
  47. return this->lock(0, mSizeInBytes, options);
  48. }
  49. /**
  50. * @brief Releases the lock on this buffer.
  51. */
  52. virtual void unlock()
  53. {
  54. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  55. unlockImpl();
  56. mIsLocked = false;
  57. }
  58. /**
  59. * @brief Reads data from a portion of the buffer and copies it to the destination
  60. * buffer. Caller must ensure destination buffer is large enough.
  61. *
  62. * @param offset Offset in bytes from which to copy the data.
  63. * @param length Length of the area you want to copy, in bytes.
  64. * @param dest Destination buffer large enough to store the read data.
  65. */
  66. virtual void readData(UINT32 offset, UINT32 length, void* dest) = 0;
  67. /**
  68. * @brief Writes data into a portion of the buffer from the source memory.
  69. *
  70. * @param offset Offset in bytes from which to copy the data.
  71. * @param length Length of the area you want to copy, in bytes.
  72. * @param source Source buffer containing the data to write.
  73. * @param writeFlags Optional write flags that may affect performance.
  74. */
  75. virtual void writeData(UINT32 offset, UINT32 length, const void* source,
  76. BufferWriteType writeFlags = BufferWriteType::Normal) = 0;
  77. /**
  78. * @brief Copies data from a specific portion of the source buffer into a specific portion
  79. * of this buffer.
  80. *
  81. * @param srcBuffer Buffer to copy from.
  82. * @param srcOffset Offset into the source buffer to start copying from, in bytes.
  83. * @param dstOffset Offset into this buffer to start copying to, in bytes.
  84. * @param length Size of the data to copy, in bytes.
  85. * @param discardWholeBuffer Specify true if the data in the current buffer can be entirely discarded. This
  86. * may improve performance.
  87. */
  88. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  89. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false)
  90. {
  91. const void *srcData = srcBuffer.lock(
  92. srcOffset, length, GBL_READ_ONLY);
  93. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BufferWriteType::Discard : BufferWriteType::Normal);
  94. srcBuffer.unlock();
  95. }
  96. /**
  97. * @brief Copy data from the provided buffer into this buffer. If buffers
  98. * are not the same size, smaller size will be used.
  99. */
  100. virtual void copyData(HardwareBuffer& srcBuffer)
  101. {
  102. UINT32 sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
  103. copyData(srcBuffer, 0, 0, sz, true);
  104. }
  105. /**
  106. * @brief Returns the size of this buffer in bytes.
  107. */
  108. UINT32 getSizeInBytes(void) const { return mSizeInBytes; }
  109. /**
  110. * @brief Returns the Usage flags with which this buffer was created.
  111. */
  112. GpuBufferUsage getUsage() const { return mUsage; }
  113. /**
  114. * @brief Returns whether this buffer is held in system memory.
  115. */
  116. bool isSystemMemory() const { return mSystemMemory; }
  117. /**
  118. * @brief Returns whether or not this buffer is currently locked.
  119. */
  120. bool isLocked() const { return mIsLocked; }
  121. protected:
  122. friend class HardwareBufferManager;
  123. /**
  124. * @brief Constructs a new buffer.
  125. *
  126. * @param usage Determines most common usage of the buffer. Usually has effect on what
  127. * type of memory will be buffer allocated in but that depends on render API.
  128. * Specify dynamic if you plan on modifying it often, static otherwise.
  129. * @param systemMemory If enabled the the buffer will be kept in the system memory. System memory
  130. * buffers are often used as a source or destination for copies from/to other
  131. * buffers. Some APIs don't allow reading from non-system memory buffers.
  132. */
  133. HardwareBuffer(GpuBufferUsage usage, bool systemMemory)
  134. : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory)
  135. { }
  136. /**
  137. * @copydoc lock
  138. */
  139. virtual void* lockImpl(UINT32 offset, UINT32 length, GpuLockOptions options) = 0;
  140. /**
  141. * @copydoc unlock
  142. */
  143. virtual void unlockImpl() = 0;
  144. protected:
  145. UINT32 mSizeInBytes;
  146. GpuBufferUsage mUsage;
  147. bool mIsLocked;
  148. UINT32 mLockStart;
  149. UINT32 mLockSize;
  150. bool mSystemMemory;
  151. };
  152. }