BsHardwareBuffer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. See @ref queuesDoc.
  32. */
  33. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx = 0, UINT32 queueIdx = 0)
  34. {
  35. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  36. void* ret = map(offset, length, options, deviceIdx, queueIdx);
  37. mIsLocked = true;
  38. mLockStart = offset;
  39. mLockSize = length;
  40. return ret;
  41. }
  42. /**
  43. * Locks the entire buffer and returns pointer to the locked area. You must call unlock() when done.
  44. *
  45. * @param[in] options Signifies what you want to do with the returned pointer. Caller must ensure not to do
  46. * anything he hasn't requested (for example don't try to read from the buffer unless you
  47. * requested it here).
  48. * @param[in] deviceIdx Index of the device whose memory to map. If the buffer doesn't exist on this device,
  49. * the method returns null.
  50. * @param[in] queueIdx Device queue to perform any read/write operations on. See @ref queuesDoc.
  51. */
  52. void* lock(GpuLockOptions options, UINT32 deviceIdx = 0, UINT32 queueIdx = 0)
  53. {
  54. return this->lock(0, mSize, options, deviceIdx, queueIdx);
  55. }
  56. /** Releases the lock on this buffer. */
  57. virtual void unlock()
  58. {
  59. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  60. unmap();
  61. mIsLocked = false;
  62. }
  63. /**
  64. * Reads data from a portion of the buffer and copies it to the destination buffer. Caller must ensure destination
  65. * buffer is large enough.
  66. *
  67. * @param[in] offset Offset in bytes from which to copy the data.
  68. * @param[in] length Length of the area you want to copy, in bytes.
  69. * @param[in] dest Destination buffer large enough to store the read data. Data is written from the start
  70. * of the buffer (@p offset is only applied to the source).
  71. * @param[in] deviceIdx Index of the device whose memory to read. If the buffer doesn't exist on this device,
  72. * no data will be read.
  73. * @param[in] queueIdx Device queue to perform the read operation on. See @ref queuesDoc.
  74. */
  75. virtual void readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx = 0, UINT32 queueIdx = 0) = 0;
  76. /**
  77. * Writes data into a portion of the buffer from the source memory.
  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] source Source buffer containing the data to write. Data is read from the start of the buffer
  82. * (@p offset is only applied to the destination).
  83. * @param[in] writeFlags Optional write flags that may affect performance.
  84. * @param[in] queueIdx Device queue to perform the write operation on. See @ref queuesDoc.
  85. */
  86. virtual void writeData(UINT32 offset, UINT32 length, const void* source,
  87. BufferWriteType writeFlags = BWT_NORMAL, UINT32 queueIdx = 0) = 0;
  88. /**
  89. * Copies data from a specific portion of the source buffer into a specific portion of this buffer.
  90. *
  91. * @param[in] srcBuffer Buffer to copy from.
  92. * @param[in] srcOffset Offset into the source buffer to start copying from, in bytes.
  93. * @param[in] dstOffset Offset into this buffer to start copying to, in bytes.
  94. * @param[in] length Size of the data to copy, in bytes.
  95. * @param[in] discardWholeBuffer Specify true if the data in the current buffer can be entirely discarded. This
  96. * may improve performance.
  97. * @param[in] queueIdx Device queue to perform the copy operation on. See @ref queuesDoc.
  98. */
  99. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  100. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false, UINT32 queueIdx = 0)
  101. {
  102. const void *srcData = srcBuffer.lock(
  103. srcOffset, length, GBL_READ_ONLY, queueIdx);
  104. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BWT_DISCARD : BWT_NORMAL, queueIdx);
  105. srcBuffer.unlock();
  106. }
  107. /**
  108. * Copy data from the provided buffer into this buffer. If buffers are not the same size, smaller size will be used.
  109. *
  110. * @param[in] srcBuffer Hardware buffer to copy from.
  111. * @param[in] queueIdx Device queue to perform the copy operation on. See @ref queuesDoc.
  112. */
  113. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 queueIdx = 0)
  114. {
  115. UINT32 sz = std::min(getSize(), srcBuffer.getSize());
  116. copyData(srcBuffer, 0, 0, sz, true, queueIdx);
  117. }
  118. /** Returns the size of this buffer in bytes. */
  119. UINT32 getSize() const { return mSize; }
  120. /** Returns whether or not this buffer is currently locked. */
  121. bool isLocked() const { return mIsLocked; }
  122. protected:
  123. friend class HardwareBufferManager;
  124. /**
  125. * Constructs a new buffer.
  126. *
  127. * @param[in] size Size of the buffer, in bytes.
  128. */
  129. HardwareBuffer(UINT32 size)
  130. : mSize(size), mIsLocked(false)
  131. { }
  132. /** @copydoc lock */
  133. virtual void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx,
  134. UINT32 queueIdx) { return nullptr; }
  135. /** @copydoc unlock */
  136. virtual void unmap() { }
  137. protected:
  138. UINT32 mSize;
  139. bool mIsLocked;
  140. UINT32 mLockStart;
  141. UINT32 mLockSize;
  142. };
  143. /** @} */
  144. }