BsHardwareBuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 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 the read operation on. Using a non-default queue index
  92. * allows the GPU to perform read operations while executing rendering or compute
  93. * operations on the same time.
  94. *
  95. * This value is a global queue index which encodes both the queue type and queue index.
  96. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  97. */
  98. virtual void readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx = 0, UINT32 queueIdx = 0) = 0;
  99. /**
  100. * Writes data into a portion of the buffer from the source memory.
  101. *
  102. * @param[in] offset Offset in bytes from which to copy the data.
  103. * @param[in] length Length of the area you want to copy, in bytes.
  104. * @param[in] source Source buffer containing the data to write. Data is read from the start of the buffer
  105. * (@p offset is only applied to the destination).
  106. * @param[in] writeFlags Optional write flags that may affect performance.
  107. * @param[in] queueIdx Device queue to perform any write operations on. Using a non-default queue index
  108. * allows the GPU to perform write operations while executing rendering or compute
  109. * operations on the same time.
  110. *
  111. * Note that when writing to a buffer that is being used on a command buffer with a
  112. * different queue you must ensure to provide the command buffer with a valid sync mask
  113. * so it knows to wait before the write operation completes.
  114. *
  115. * This value is a global queue index which encodes both the queue type and queue index.
  116. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  117. */
  118. virtual void writeData(UINT32 offset, UINT32 length, const void* source,
  119. BufferWriteType writeFlags = BWT_NORMAL, UINT32 queueIdx = 0) = 0;
  120. /**
  121. * Copies data from a specific portion of the source buffer into a specific portion of this buffer.
  122. *
  123. * @param[in] srcBuffer Buffer to copy from.
  124. * @param[in] srcOffset Offset into the source buffer to start copying from, in bytes.
  125. * @param[in] dstOffset Offset into this buffer to start copying to, in bytes.
  126. * @param[in] length Size of the data to copy, in bytes.
  127. * @param[in] discardWholeBuffer Specify true if the data in the current buffer can be entirely discarded. This
  128. * may improve performance.
  129. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue
  130. * index allows the GPU to perform write or read operations while executing
  131. * rendering or compute operations on the same time.
  132. *
  133. * Note that when writing to a buffer that is being used on a command buffer with a
  134. * different queue you must ensure to provide the command buffer with a valid sync
  135. * mask so it knows to wait before the write operation completes.
  136. *
  137. * This value is a global queue index which encodes both the queue type and queue
  138. * index. Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  139. */
  140. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  141. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false, UINT32 queueIdx = 0)
  142. {
  143. const void *srcData = srcBuffer.lock(
  144. srcOffset, length, GBL_READ_ONLY, queueIdx);
  145. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BWT_DISCARD : BWT_NORMAL, queueIdx);
  146. srcBuffer.unlock();
  147. }
  148. /**
  149. * Copy data from the provided buffer into this buffer. If buffers are not the same size, smaller size will be used.
  150. *
  151. * @param[in] srcBuffer Hardware buffer to copy from.
  152. * @param[in] queueIdx Device queue to perform any read/write operations on. Using a non-default queue index
  153. * allows the GPU to perform write or read operations while executing rendering or compute
  154. * operations on the same time.
  155. *
  156. * Note that when writing to a buffer that is being used on a command buffer with a
  157. * different queue you must ensure to provide the command buffer with a valid sync mask
  158. * so it knows to wait before the write operation completes.
  159. *
  160. * This value is a global queue index which encodes both the queue type and queue index.
  161. * Retrieve it from CommandSyncMask::getGlobalQueueIdx().
  162. */
  163. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 queueIdx = 0)
  164. {
  165. UINT32 sz = std::min(getSize(), srcBuffer.getSize());
  166. copyData(srcBuffer, 0, 0, sz, true, queueIdx);
  167. }
  168. /** Returns the size of this buffer in bytes. */
  169. UINT32 getSize() const { return mSize; }
  170. /** Returns whether or not this buffer is currently locked. */
  171. bool isLocked() const { return mIsLocked; }
  172. protected:
  173. friend class HardwareBufferManager;
  174. /**
  175. * Constructs a new buffer.
  176. *
  177. * @param[in] size Size of the buffer, in bytes.
  178. */
  179. HardwareBuffer(UINT32 size)
  180. : mSize(size), mIsLocked(false)
  181. { }
  182. /** @copydoc lock */
  183. virtual void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx,
  184. UINT32 queueIdx) { return nullptr; }
  185. /** @copydoc unlock */
  186. virtual void unmap() { }
  187. protected:
  188. UINT32 mSize;
  189. bool mIsLocked;
  190. UINT32 mLockStart;
  191. UINT32 mLockSize;
  192. };
  193. /** @} */
  194. }