BsHardwareBuffer.h 6.2 KB

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