BsHardwareBuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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] deviceIdx Index of the device whose memory to map. If the buffer doesn't exist on this device,
  30. * the method returns null.
  31. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue index
  32. * allows the GPU to perform write or read operations while executing rendering or compute
  33. * operations on the same time.
  34. *
  35. * Note that when writing to a buffer that is being used on a command buffer with a
  36. * different queue you must ensure to provide the command buffer with a valid sync mask
  37. * so it knows to wait before the write operation completes.
  38. *
  39. * This value is a global queue index which encodes both the queue type and queue index.
  40. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  41. */
  42. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx = 0, UINT32 queueIdx = 0)
  43. {
  44. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  45. void* ret = map(offset, length, options, deviceIdx, queueIdx);
  46. mIsLocked = true;
  47. mLockStart = offset;
  48. mLockSize = length;
  49. return ret;
  50. }
  51. /**
  52. * Locks the entire buffer and returns pointer to the locked area. You must call unlock() when done.
  53. *
  54. * @param[in] options Signifies what you want to do with the returned pointer. Caller must ensure not to do
  55. * anything he hasn't requested (for example don't try to read from the buffer unless you
  56. * requested it here).
  57. * @param[in] deviceIdx Index of the device whose memory to map. If the buffer doesn't exist on this device,
  58. * the method returns null.
  59. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue index
  60. * allows the GPU to perform write or read operations while executing rendering or compute
  61. * operations on the same time.
  62. *
  63. * Note that when writing to a buffer that is being used on a command buffer with a
  64. * different queue you must ensure to provide the command buffer with a valid sync mask
  65. * so it knows to wait before the write operation completes.
  66. *
  67. * This value is a global queue index which encodes both the queue type and queue index.
  68. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  69. */
  70. void* lock(GpuLockOptions options, UINT32 deviceIdx = 0, UINT32 queueIdx = 0)
  71. {
  72. return this->lock(0, mSize, options, deviceIdx, queueIdx);
  73. }
  74. /** Releases the lock on this buffer. */
  75. virtual void unlock()
  76. {
  77. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  78. unmap();
  79. mIsLocked = false;
  80. }
  81. /**
  82. * Reads data from a portion of the buffer and copies it to the destination buffer. Caller must ensure destination
  83. * buffer is large enough.
  84. *
  85. * @param[in] offset Offset in bytes from which to copy the data.
  86. * @param[in] length Length of the area you want to copy, in bytes.
  87. * @param[in] dest Destination buffer large enough to store the read data. Data is written from the start
  88. * of the buffer (@p offset is only applied to the source).
  89. * @param[in] deviceIdx Index of the device whose memory to read. If the buffer doesn't exist on this device,
  90. * no data will be read.
  91. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue index
  92. * allows the GPU to perform write or read operations while executing rendering or compute
  93. * operations on the same time.
  94. *
  95. * Note that when writing to a buffer that is being used on a command buffer with a
  96. * different queue you must ensure to provide the command buffer with a valid sync mask
  97. * so it knows to wait before the write operation completes.
  98. *
  99. * This value is a global queue index which encodes both the queue type and queue index.
  100. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  101. */
  102. virtual void readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx = 0, UINT32 queueIdx = 0) = 0;
  103. /**
  104. * Writes data into a portion of the buffer from the source memory.
  105. *
  106. * @param[in] offset Offset in bytes from which to copy the data.
  107. * @param[in] length Length of the area you want to copy, in bytes.
  108. * @param[in] source Source buffer containing the data to write. Data is read from the start of the buffer
  109. * (@p offset is only applied to the destination).
  110. * @param[in] writeFlags Optional write flags that may affect performance.
  111. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue index
  112. * allows the GPU to perform write or read operations while executing rendering or compute
  113. * operations on the same time.
  114. *
  115. * Note that when writing to a buffer that is being used on a command buffer with a
  116. * different queue you must ensure to provide the command buffer with a valid sync mask
  117. * so it knows to wait before the write operation completes.
  118. *
  119. * This value is a global queue index which encodes both the queue type and queue index.
  120. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  121. */
  122. virtual void writeData(UINT32 offset, UINT32 length, const void* source,
  123. BufferWriteType writeFlags = BWT_NORMAL, UINT32 queueIdx = 0) = 0;
  124. /**
  125. * Copies data from a specific portion of the source buffer into a specific portion of this buffer.
  126. *
  127. * @param[in] srcBuffer Buffer to copy from.
  128. * @param[in] srcOffset Offset into the source buffer to start copying from, in bytes.
  129. * @param[in] dstOffset Offset into this buffer to start copying to, in bytes.
  130. * @param[in] length Size of the data to copy, in bytes.
  131. * @param[in] discardWholeBuffer Specify true if the data in the current buffer can be entirely discarded. This
  132. * may improve performance.
  133. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue
  134. * index allows the GPU to perform write or read operations while executing
  135. * rendering or compute operations on the same time.
  136. *
  137. * Note that when writing to a buffer that is being used on a command buffer with a
  138. * different queue you must ensure to provide the command buffer with a valid sync
  139. * mask so it knows to wait before the write operation completes.
  140. *
  141. * This value is a global queue index which encodes both the queue type and queue
  142. * index. Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  143. */
  144. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  145. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false, UINT32 queueIdx = 0)
  146. {
  147. const void *srcData = srcBuffer.lock(
  148. srcOffset, length, GBL_READ_ONLY, queueIdx);
  149. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BWT_DISCARD : BWT_NORMAL, queueIdx);
  150. srcBuffer.unlock();
  151. }
  152. /**
  153. * Copy data from the provided buffer into this buffer. If buffers are not the same size, smaller size will be used.
  154. *
  155. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue index
  156. * allows the GPU to perform write or read operations while executing rendering or compute
  157. * operations on the same time.
  158. *
  159. * Note that when writing to a buffer that is being used on a command buffer with a
  160. * different queue you must ensure to provide the command buffer with a valid sync mask
  161. * so it knows to wait before the write operation completes.
  162. *
  163. * This value is a global queue index which encodes both the queue type and queue index.
  164. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  165. */
  166. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 queueIdx = 0)
  167. {
  168. UINT32 sz = std::min(getSize(), srcBuffer.getSize());
  169. copyData(srcBuffer, 0, 0, sz, true, queueIdx);
  170. }
  171. /** Returns the size of this buffer in bytes. */
  172. UINT32 getSize() const { return mSize; }
  173. /** Returns whether or not this buffer is currently locked. */
  174. bool isLocked() const { return mIsLocked; }
  175. protected:
  176. friend class HardwareBufferManager;
  177. /**
  178. * Constructs a new buffer.
  179. *
  180. * @param[in] size Size of the buffer, in bytes.
  181. */
  182. HardwareBuffer(UINT32 size)
  183. : mSize(size), mIsLocked(false)
  184. { }
  185. /** @copydoc lock */
  186. virtual void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx,
  187. UINT32 queueIdx) { return nullptr; }
  188. /** @copydoc unlock */
  189. virtual void unmap() { }
  190. protected:
  191. UINT32 mSize;
  192. bool mIsLocked;
  193. UINT32 mLockStart;
  194. UINT32 mLockSize;
  195. };
  196. /** @} */
  197. }