BsHardwareBuffer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. namespace BansheeEngine
  4. {
  5. /**
  6. * @brief Abstract class defining common features of hardware buffers. Hardware buffers usually
  7. * represent areas of memory the GPU or the driver can access directly.
  8. *
  9. * @note Be aware that reading from non-system memory hardware buffers is usually slow and should be avoided.
  10. */
  11. class BS_CORE_EXPORT HardwareBuffer
  12. {
  13. public:
  14. virtual ~HardwareBuffer() {}
  15. /**
  16. * @brief Locks a portion of the buffer and returns pointer to the locked area.
  17. * You must call "unlock" when done.
  18. *
  19. * @param offset Offset in bytes from which to lock the buffer.
  20. * @param length Length of the area you want to lock, in bytes.
  21. * @param options Signifies what you want to do with the returned pointer.
  22. * Caller must ensure not to do anything he hasn't requested.
  23. * (e.g. don't try to read from the buffer unless you requested
  24. * it here).
  25. */
  26. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options)
  27. {
  28. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  29. void* ret = lockImpl(offset, length, options);
  30. mIsLocked = true;
  31. mLockStart = offset;
  32. mLockSize = length;
  33. return ret;
  34. }
  35. /**
  36. * @brief Locks the entire buffer and returns pointer to the locked area.
  37. * You must call "unlock" when done.
  38. *
  39. * @param options Signifies what you want to do with the returned pointer.
  40. * Caller must ensure not to do anything he hasn't requested.
  41. * (e.g. don't try to read from the buffer unless you requested
  42. * it here).
  43. */
  44. void* lock(GpuLockOptions options)
  45. {
  46. return this->lock(0, mSizeInBytes, options);
  47. }
  48. /**
  49. * @brief Releases the lock on this buffer.
  50. */
  51. virtual void unlock()
  52. {
  53. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  54. unlockImpl();
  55. mIsLocked = false;
  56. }
  57. /**
  58. * @brief Reads data from a portion of the buffer and copies it to the destination
  59. * buffer. Caller must ensure destination buffer is large enough.
  60. *
  61. * @param offset Offset in bytes from which to copy the data.
  62. * @param length Length of the area you want to copy, in bytes.
  63. * @param dest Destination buffer large enough to store the read data.
  64. */
  65. virtual void readData(UINT32 offset, UINT32 length, void* dest) = 0;
  66. /**
  67. * @brief Writes data into a portion of the buffer from the source memory.
  68. *
  69. * @param offset Offset in bytes from which to copy the data.
  70. * @param length Length of the area you want to copy, in bytes.
  71. * @param source Source buffer containing the data to write.
  72. * @param writeFlags Optional write flags that may affect performance.
  73. */
  74. virtual void writeData(UINT32 offset, UINT32 length, const void* source,
  75. BufferWriteType writeFlags = BufferWriteType::Normal) = 0;
  76. /**
  77. * @brief Copies data from a specific portion of the source buffer into a specific portion
  78. * of this buffer.
  79. *
  80. * @param srcBuffer Buffer to copy from.
  81. * @param srcOffset Offset into the source buffer to start copying from, in bytes.
  82. * @param dstOffset Offset into this buffer to start copying to, in bytes.
  83. * @param length Size of the data to copy, in bytes.
  84. * @param discardWholeBuffer Specify true if the data in the current buffer can be entirely discarded. This
  85. * may improve performance.
  86. */
  87. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  88. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false)
  89. {
  90. const void *srcData = srcBuffer.lock(
  91. srcOffset, length, GBL_READ_ONLY);
  92. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BufferWriteType::Discard : BufferWriteType::Normal);
  93. srcBuffer.unlock();
  94. }
  95. /**
  96. * @brief Copy data from the provided buffer into this buffer. If buffers
  97. * are not the same size, smaller size will be used.
  98. */
  99. virtual void copyData(HardwareBuffer& srcBuffer)
  100. {
  101. UINT32 sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
  102. copyData(srcBuffer, 0, 0, sz, true);
  103. }
  104. /**
  105. * @brief Returns the size of this buffer in bytes.
  106. */
  107. UINT32 getSizeInBytes(void) const { return mSizeInBytes; }
  108. /**
  109. * @brief Returns the Usage flags with which this buffer was created.
  110. */
  111. GpuBufferUsage getUsage() const { return mUsage; }
  112. /**
  113. * @brief Returns whether this buffer is held in system memory.
  114. */
  115. bool isSystemMemory() const { return mSystemMemory; }
  116. /**
  117. * @brief Returns whether or not this buffer is currently locked.
  118. */
  119. bool isLocked() const { return mIsLocked; }
  120. protected:
  121. friend class HardwareBufferManager;
  122. /**
  123. * @brief Constructs a new buffer.
  124. *
  125. * @param usage Determines most common usage of the buffer. Usually has effect on what
  126. * type of memory will be buffer allocated in but that depends on render API.
  127. * Specify dynamic if you plan on modifying it often, static otherwise.
  128. * @param systemMemory If enabled the the buffer will be kept in the system memory. System memory
  129. * buffers are often used as a source or destination for copies from/to other
  130. * buffers. Some APIs don't allow reading from non-system memory buffers.
  131. */
  132. HardwareBuffer(GpuBufferUsage usage, bool systemMemory)
  133. : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory)
  134. { }
  135. /**
  136. * @copydoc lock
  137. */
  138. virtual void* lockImpl(UINT32 offset, UINT32 length, GpuLockOptions options) = 0;
  139. /**
  140. * @copydoc unlock
  141. */
  142. virtual void unlockImpl() = 0;
  143. protected:
  144. UINT32 mSizeInBytes;
  145. GpuBufferUsage mUsage;
  146. bool mIsLocked;
  147. UINT32 mLockStart;
  148. UINT32 mLockSize;
  149. bool mSystemMemory;
  150. };
  151. }