BsHardwareBuffer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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] commandBuffer Command buffer to queue the copy operation on. If null, main command buffer is
  98. * used.
  99. */
  100. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset, UINT32 dstOffset, UINT32 length,
  101. bool discardWholeBuffer = false, const SPtr<ct::CommandBuffer>& commandBuffer = nullptr) = 0;
  102. /**
  103. * Copy data from the provided buffer into this buffer. If buffers are not the same size, smaller size will be used.
  104. *
  105. * @param[in] srcBuffer Hardware buffer to copy from.
  106. * @param[in] commandBuffer Command buffer to queue the copy operation on. If null, main command buffer is
  107. * used.
  108. */
  109. virtual void copyData(HardwareBuffer& srcBuffer, const SPtr<ct::CommandBuffer>& commandBuffer = nullptr)
  110. {
  111. UINT32 sz = std::min(getSize(), srcBuffer.getSize());
  112. copyData(srcBuffer, 0, 0, sz, true, commandBuffer);
  113. }
  114. /** Returns the size of this buffer in bytes. */
  115. UINT32 getSize() const { return mSize; }
  116. /** Returns whether or not this buffer is currently locked. */
  117. bool isLocked() const { return mIsLocked; }
  118. protected:
  119. friend class HardwareBufferManager;
  120. /**
  121. * Constructs a new buffer.
  122. *
  123. * @param[in] size Size of the buffer, in bytes.
  124. */
  125. HardwareBuffer(UINT32 size)
  126. : mSize(size), mIsLocked(false)
  127. { }
  128. /** @copydoc lock */
  129. virtual void* map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx,
  130. UINT32 queueIdx) { return nullptr; }
  131. /** @copydoc unlock */
  132. virtual void unmap() { }
  133. protected:
  134. UINT32 mSize;
  135. bool mIsLocked;
  136. UINT32 mLockStart;
  137. UINT32 mLockSize;
  138. };
  139. /** @} */
  140. }