BsHardwareBuffer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /** @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. */
  30. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options)
  31. {
  32. assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
  33. void* ret = lockImpl(offset, length, options);
  34. mIsLocked = true;
  35. mLockStart = offset;
  36. mLockSize = length;
  37. return ret;
  38. }
  39. /**
  40. * Locks the entire buffer and returns pointer to the locked area. You must call unlock() when done.
  41. *
  42. * @param[in] options Signifies what you want to do with the returned pointer. Caller must ensure not to do
  43. * anything he hasn't requested (for example don't try to read from the buffer unless you
  44. * requested it here).
  45. */
  46. void* lock(GpuLockOptions options)
  47. {
  48. return this->lock(0, mSizeInBytes, options);
  49. }
  50. /** Releases the lock on this buffer. */
  51. virtual void unlock()
  52. {
  53. assert(isLocked() && "Cannot unlock this buffer, it is not locked!");
  54. unlockImpl();
  55. mIsLocked = false;
  56. }
  57. /**
  58. * Reads data from a portion of the buffer and copies it to the destination buffer. Caller must ensure destination
  59. * buffer is large enough.
  60. *
  61. * @param[in] offset Offset in bytes from which to copy the data.
  62. * @param[in] length Length of the area you want to copy, in bytes.
  63. * @param[in] dest Destination buffer large enough to store the read data.
  64. */
  65. virtual void readData(UINT32 offset, UINT32 length, void* dest) = 0;
  66. /**
  67. * Writes data into a portion of the buffer from the source memory.
  68. *
  69. * @param[in] offset Offset in bytes from which to copy the data.
  70. * @param[in] length Length of the area you want to copy, in bytes.
  71. * @param[in] source Source buffer containing the data to write.
  72. * @param[in] 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. * Copies data from a specific portion of the source buffer into a specific portion of this buffer.
  78. *
  79. * @param[in] srcBuffer Buffer to copy from.
  80. * @param[in] srcOffset Offset into the source buffer to start copying from, in bytes.
  81. * @param[in] dstOffset Offset into this buffer to start copying to, in bytes.
  82. * @param[in] length Size of the data to copy, in bytes.
  83. * @param[in] discardWholeBuffer Specify true if the data in the current buffer can be entirely discarded. This
  84. * may improve performance.
  85. */
  86. virtual void copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  87. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false)
  88. {
  89. const void *srcData = srcBuffer.lock(
  90. srcOffset, length, GBL_READ_ONLY);
  91. this->writeData(dstOffset, length, srcData, discardWholeBuffer ? BufferWriteType::Discard : BufferWriteType::Normal);
  92. srcBuffer.unlock();
  93. }
  94. /**
  95. * Copy data from the provided buffer into this buffer. If buffers are not the same size, smaller size will be used.
  96. */
  97. virtual void copyData(HardwareBuffer& srcBuffer)
  98. {
  99. UINT32 sz = std::min(getSizeInBytes(), srcBuffer.getSizeInBytes());
  100. copyData(srcBuffer, 0, 0, sz, true);
  101. }
  102. /** Returns the size of this buffer in bytes. */
  103. UINT32 getSizeInBytes(void) const { return mSizeInBytes; }
  104. /** Returns the Usage flags with which this buffer was created. */
  105. GpuBufferUsage getUsage() const { return mUsage; }
  106. /** Returns whether this buffer is held in system memory. */
  107. bool isSystemMemory() const { return mSystemMemory; }
  108. /** Returns whether or not this buffer is currently locked. */
  109. bool isLocked() const { return mIsLocked; }
  110. protected:
  111. friend class HardwareBufferManager;
  112. /**
  113. * Constructs a new buffer.
  114. *
  115. * @param[in] usage Determines most common usage of the buffer. Usually has effect on what type of
  116. * memory will be buffer allocated in but that depends on render API. Specify dynamic
  117. * if you plan on modifying it often, static otherwise.
  118. * @param[in] systemMemory If enabled the the buffer will be kept in the system memory. System memory buffers
  119. * are often used as a source or destination for copies from/to other buffers. Some
  120. * APIs don't allow reading from non-system memory buffers.
  121. */
  122. HardwareBuffer(GpuBufferUsage usage, bool systemMemory)
  123. : mUsage(usage), mIsLocked(false), mSystemMemory(systemMemory)
  124. { }
  125. /** @copydoc lock */
  126. virtual void* lockImpl(UINT32 offset, UINT32 length, GpuLockOptions options) = 0;
  127. /** @copydoc unlock */
  128. virtual void unlockImpl() = 0;
  129. protected:
  130. UINT32 mSizeInBytes;
  131. GpuBufferUsage mUsage;
  132. bool mIsLocked;
  133. UINT32 mLockStart;
  134. UINT32 mLockSize;
  135. bool mSystemMemory;
  136. };
  137. /** @} */
  138. }