BsHardwareBuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 BansheeEngine
  6. {
  7. /** @addtogroup RenderAPI-Internal
  8. * @{
  9. */
  10. /**
  11. * Abstract class defining common features of hardware buffers. Hardware buffers usually represent areas of memory the
  12. * GPU or the driver can access directly.
  13. *
  14. * @note Core thread only.
  15. * @note Be aware that reading from non-system memory hardware buffers is usually slow and should be avoided.
  16. */
  17. class BS_CORE_EXPORT HardwareBuffer
  18. {
  19. public:
  20. virtual ~HardwareBuffer() {}
  21. /**
  22. * Locks a portion of the buffer and returns pointer to the locked area. You must call unlock() when done.
  23. *
  24. * @param[in] offset Offset in bytes from which to lock the buffer.
  25. * @param[in] length Length of the area you want to lock, in bytes.
  26. * @param[in] options Signifies what you want to do with the returned pointer. Caller must ensure not to do
  27. * anything he hasn't requested (for example don't try to read from the buffer unless you
  28. * requested it here).
  29. * @param[in] syncMask Mask that determines how are read or write operations synchronized with other operations
  30. * on this resource. This corresponds to the sync mask property on a CommandBuffer object.
  31. * If reading, the system will wait on all commands buffers with the shared mask bits
  32. * before continuing.
  33. * If writing, Setting a mask that's different from used command buffers allows the writes
  34. * to be queued independantly from normal rendering commands, allowing for async execution.
  35. *
  36. * It's up to the caller to ensure the usage is valid (e.g. not reading something that is
  37. * currently being written to with a different sync mask). If not sure leave at default.
  38. */
  39. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 syncMask = 0x00000001)
  40. {
  41. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  42. void* ret = map(offset, length, options, syncMask);
  43. mIsLocked = true;
  44. mLockStart = offset;
  45. mLockSize = length;
  46. return ret;
  47. }
  48. /**
  49. * Locks the entire buffer and returns pointer to the locked area. You must call unlock() when done.
  50. *
  51. * @param[in] options Signifies what you want to do with the returned pointer. Caller must ensure not to do
  52. * anything he hasn't requested (for example don't try to read from the buffer unless you
  53. * requested it here).
  54. * @param[in] syncMask Mask that determines how are read or write operations synchronized with other operations
  55. * on this resource. This corresponds to the sync mask property on a CommandBuffer object.
  56. * If reading, the system will wait on all commands buffers with the shared mask bits
  57. * before continuing.
  58. * If writing, Setting a mask that's different from used command buffers allows the writes
  59. * to be queued independantly from normal rendering commands, allowing for async execution.
  60. *
  61. * It's up to the caller to ensure the usage is valid (e.g. not reading something that is
  62. * currently being written to with a different sync mask). If not sure leave at default.
  63. */
  64. void* lock(GpuLockOptions options, UINT32 syncMask = 0x00000001)
  65. {
  66. return this->lock(0, mSizeInBytes, options, syncMask);
  67. }
  68. /** Releases the lock on this buffer. */
  69. virtual void unlock()
  70. {
  71. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  72. unmap();
  73. mIsLocked = false;
  74. }
  75. /**
  76. * Reads data from a portion of the buffer and copies it to the destination buffer. Caller must ensure destination
  77. * buffer is large enough.
  78. *
  79. * @param[in] offset Offset in bytes from which to copy the data.
  80. * @param[in] length Length of the area you want to copy, in bytes.
  81. * @param[in] dest Destination buffer large enough to store the read data.
  82. * @param[in] syncMask Mask that determines how is the read operation synchronized with other operations on
  83. * this resource. This corresponds to the sync mask property on a CommandBuffer object.
  84. * The system will wait on all commands buffers with the shared mask bits before continuing
  85. * with the read.
  86. *
  87. * It's up to the caller to ensure the usage is valid (e.g. not reading something that is
  88. * currently being written to with a different sync mask). If not sure leave at default.
  89. */
  90. virtual void readData(UINT32 offset, UINT32 length, void* dest, UINT32 syncMask = 0x00000001) = 0;
  91. /**
  92. * Writes data into a portion of the buffer from the source memory.
  93. *
  94. * @param[in] offset Offset in bytes from which to copy the data.
  95. * @param[in] length Length of the area you want to copy, in bytes.
  96. * @param[in] source Source buffer containing the data to write.
  97. * @param[in] writeFlags Optional write flags that may affect performance.
  98. * @param[in] syncMask Mask that determines how is the write operation synchronized with other operations
  99. * on this resource. This corresponds to the sync mask property on a CommandBuffer object.
  100. * Setting a mask that's different from used command buffers allows the writes to be queued
  101. * independantly from normal rendering commands, allowing for async execution.
  102. *
  103. * It's up to the caller to ensure the usage is valid (e.g. not writing to something that
  104. * is currently being used by the GPU). If not sure leave at default.
  105. */
  106. virtual void writeData(UINT32 offset, UINT32 length, const void* source,
  107. BufferWriteType writeFlags = BWT_NORMAL, UINT32 syncMask = 0x00000001) = 0;
  108. /**
  109. * Copies data from a specific portion of the source buffer into a specific portion of this buffer.
  110. *
  111. * @param[in] srcBuffer Buffer to copy from.
  112. * @param[in] srcOffset Offset into the source buffer to start copying from, in bytes.
  113. * @param[in] dstOffset Offset into this buffer to start copying to, in bytes.
  114. * @param[in] length Size of the data to copy, in bytes.
  115. * @param[in] discardWholeBuffer Specify true if the data in the current buffer can be entirely discarded. This
  116. * may improve performance.
  117. * @param[in] syncMask Mask that determines how is the copy operation synchronized with other
  118. * operations on this resource. This corresponds to the sync mask property on a
  119. * CommandBuffer object. Setting a mask that's different from used command buffers
  120. * allows the copy to be queued independantly from normal rendering commands,
  121. * allowing for async execution.
  122. *
  123. * It's up to the caller to ensure the usage is valid (e.g. not reading from
  124. * something that is currently being written to, or writing to something that is
  125. * currently being read).
  126. */
  127. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  128. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false, UINT32 syncMask = 0x00000001)
  129. {
  130. const void *srcData = srcBuffer.lock(
  131. srcOffset, length, GBL_READ_ONLY, syncMask);
  132. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BWT_DISCARD : BWT_NORMAL, syncMask);
  133. srcBuffer.unlock();
  134. }
  135. /**
  136. * Copy data from the provided buffer into this buffer. If buffers are not the same size, smaller size will be used.
  137. *
  138. * @param[in] syncMask Mask that determines how is the copy operation synchronized with other
  139. * operations on this resource. This corresponds to the sync mask property on a
  140. * CommandBuffer object. Setting a mask that's different from used command buffers
  141. * allows the copy to be queued independantly from normal rendering commands,
  142. * allowing for async execution.
  143. *
  144. * It's up to the caller to ensure the usage is valid (e.g. not reading from
  145. * something that is currently being written to, or writing to something that is
  146. * currently being read).
  147. */
  148. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 syncMask = 0x00000001)
  149. {
  150. UINT32 sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
  151. copyData(srcBuffer, 0, 0, sz, true, syncMask);
  152. }
  153. /** Returns the size of this buffer in bytes. */
  154. UINT32 getSizeInBytes() const { return mSizeInBytes; }
  155. /** Returns the Usage flags with which this buffer was created. */
  156. GpuBufferUsage getUsage() const { return mUsage; }
  157. /** Returns whether this buffer is held in system memory. */
  158. bool isSystemMemory() const { return mSystemMemory; }
  159. /** Returns whether or not this buffer is currently locked. */
  160. bool isLocked() const { return mIsLocked; }
  161. protected:
  162. friend class HardwareBufferManager;
  163. /**
  164. * Constructs a new buffer.
  165. *
  166. * @param[in] usage Determines most common usage of the buffer. Usually has effect on what type of
  167. * memory will be buffer allocated in but that depends on render API. Specify dynamic
  168. * if you plan on modifying it often, static otherwise.
  169. * @param[in] systemMemory If enabled the the buffer will be kept in the system memory. System memory buffers
  170. * are often used as a source or destination for copies from/to other buffers. Some
  171. * APIs don't allow reading from non-system memory buffers.
  172. */
  173. HardwareBuffer(GpuBufferUsage usage, bool systemMemory)
  174. : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory)
  175. { }
  176. /** @copydoc lock */
  177. virtual void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 syncMask) = 0;
  178. /** @copydoc unlock */
  179. virtual void unmap() = 0;
  180. protected:
  181. UINT32 mSizeInBytes;
  182. GpuBufferUsage mUsage;
  183. bool mIsLocked;
  184. UINT32 mLockStart;
  185. UINT32 mLockSize;
  186. bool mSystemMemory;
  187. };
  188. /** @} */
  189. }