BsHardwareBuffer.h 5.9 KB

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