BsHardwareBuffer.h 6.1 KB

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