BsGpuBuffer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGpuBufferView.h"
  4. #include "BsCoreObject.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Information about a GPU buffer.
  9. */
  10. class BS_CORE_EXPORT GpuBufferProperties
  11. {
  12. public:
  13. GpuBufferProperties(UINT32 elementCount, UINT32 elementSize, GpuBufferType type,
  14. GpuBufferUsage usage, bool randomGpuWrite, bool useCounter);
  15. /**
  16. * @brief Returns the type of the GPU buffer. Type determines which kind of views (if any) can be created
  17. * for the buffer, and how is data read or modified in it.
  18. */
  19. GpuBufferType getType() const { return mType; }
  20. /**
  21. * @brief Returns buffer usage which determines how are planning on updating the buffer contents.
  22. */
  23. GpuBufferUsage getUsage() const { return mUsage; }
  24. /**
  25. * @brief Return whether the buffer supports random reads and writes within the GPU programs.
  26. */
  27. bool getRandomGpuWrite() const { return mRandomGpuWrite; }
  28. /**
  29. * @brief Returns whether the buffer supports counter use within GPU programs.
  30. */
  31. bool getUseCounter() const { return mUseCounter; }
  32. /**
  33. * @brief Returns number of elements in the buffer.
  34. */
  35. UINT32 getElementCount() const { return mElementCount; }
  36. /**
  37. * @brief Returns size of a single element in the buffer in bytes.
  38. */
  39. UINT32 getElementSize() const { return mElementSize; }
  40. protected:
  41. GpuBufferType mType;
  42. GpuBufferUsage mUsage;
  43. bool mRandomGpuWrite;
  44. bool mUseCounter;
  45. UINT32 mElementCount;
  46. UINT32 mElementSize;
  47. };
  48. /**
  49. * @brief Core thread version of a GpuBuffer.
  50. *
  51. * @see GpuBuffer
  52. *
  53. * @note Core thread only.
  54. */
  55. class BS_CORE_EXPORT GpuBufferCore : public CoreObjectCore
  56. {
  57. public:
  58. virtual ~GpuBufferCore();
  59. /**
  60. * @brief Locks the buffer returning a pointer to the internal buffer data that you may then
  61. * read or write to. Caller must ensure it will only perform actions promised in the
  62. * provided GPU lock options parameter.
  63. *
  64. * @param offset Number of bytes at which to lock the buffer. Returned pointer points to this location.
  65. * @param length Number of bytes to lock.
  66. * @param options How to lock the buffer. Certain options offer better performance than others.
  67. */
  68. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options) = 0;
  69. /**
  70. * @brief Unlocks a previously locked buffer. Any pointers to internal buffers returned when
  71. * it was locked will become invalid.
  72. */
  73. virtual void unlock() = 0;
  74. /**
  75. * @brief Reads buffer data into the previously allocated buffer.
  76. *
  77. * @param offset Number of bytes at which to start reading the buffer.
  78. * @param length Number of bytes to read.
  79. * @param pDest Previously allocated buffer of "length" bytes size.
  80. */
  81. virtual void readData(UINT32 offset, UINT32 length, void* pDest) = 0;
  82. /**
  83. * @brief Writes data into the buffer.
  84. *
  85. * @param offset Number of bytes at which to start writing to the buffer.
  86. * @param length Number of bytes to write.
  87. * @param pDest Previously allocated buffer used to retrieve the data from.
  88. * @param writeFlags Flags that may be used to improve performance for specific use cases.
  89. */
  90. virtual void writeData(UINT32 offset, UINT32 length, const void* pSource, BufferWriteType writeFlags = BufferWriteType::Normal) = 0;
  91. /**
  92. * @brief Copies data from another buffer into this buffer.
  93. *
  94. * @param srcBuffer Buffer to copy the data from.
  95. * @param srcOffset Offset in bytes into the source buffer - this is where reading starts from.
  96. * @param dstOffset Offset in bytes into the destination buffer - this is where writing starts from.
  97. * @param length Number of bytes to copy from source to destination.
  98. * @param discardWholeBuffer If true, the contents of the current buffer will be entirely discarded. This can improve
  99. * performance if you know you wont be needing that data any more.
  100. */
  101. virtual void copyData(GpuBufferCore& srcBuffer, UINT32 srcOffset,
  102. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false) = 0;
  103. /**
  104. * @brief Returns properties describing the buffer.
  105. */
  106. const GpuBufferProperties& getProperties() const { return mProperties; }
  107. /**
  108. * @brief Creates a buffer view that may be used for binding a buffer to a slot in the pipeline. Views allow you to specify
  109. * how is data in the buffer organized to make it easier for the pipeline to interpret.
  110. *
  111. * @param buffer Buffer to create the view for.
  112. * @param firstElement Position of the first element visible by the view.
  113. * @param elementWidth Width of one element in bytes.
  114. * @param numElements Number of elements in the buffer.
  115. * @param useCounter Should the buffer allow use of a counter. This is only relevant for random read write buffers.
  116. * @param usage Determines type of the view we are creating, and which slots in the pipeline will the view be bindable to.
  117. *
  118. * @note If a view with this exact parameters already exists, it will be returned and new one will not be created.
  119. *
  120. * Only Default and RandomWrite views are supported for this type of buffer.
  121. * TODO Low Priority: Perhaps reflect this limitation by having an enum with only
  122. * those two options?
  123. */
  124. static GpuBufferView* requestView(const SPtr<GpuBufferCore>& buffer, UINT32 firstElement, UINT32 elementWidth, UINT32 numElements, bool useCounter, GpuViewUsage usage);
  125. /**
  126. * @brief Releases a view created with requestView.
  127. *
  128. * @note View will only truly get released once all references to it are released.
  129. */
  130. static void releaseView(GpuBufferView* view);
  131. protected:
  132. GpuBufferCore(UINT32 elementCount, UINT32 elementSize, GpuBufferType type,
  133. GpuBufferUsage usage, bool randomGpuWrite = false, bool useCounter = false);
  134. /**
  135. * @brief Creates an empty view for the current buffer.
  136. */
  137. virtual GpuBufferView* createView() = 0;
  138. /**
  139. * @brief Destroys a view previously created for this buffer.
  140. */
  141. virtual void destroyView(GpuBufferView* view) = 0;
  142. /**
  143. * @brief Destroys all buffer views regardless if their reference count is
  144. * zero or not.
  145. */
  146. void clearBufferViews();
  147. /**
  148. * @brief Helper class to help with reference counting for GPU buffer views.
  149. */
  150. struct GpuBufferReference
  151. {
  152. GpuBufferReference(GpuBufferView* _view)
  153. :view(_view), refCount(0)
  154. { }
  155. GpuBufferView* view;
  156. UINT32 refCount;
  157. };
  158. UnorderedMap<GPU_BUFFER_DESC, GpuBufferReference*, GpuBufferView::HashFunction, GpuBufferView::EqualFunction> mBufferViews;
  159. GpuBufferProperties mProperties;
  160. };
  161. /**
  162. * @brief Handles a generic GPU buffer that you may use for storing any kind of data you wish to be accessible
  163. * to the GPU. These buffers may be bounds to GPU program binding slots and accessed from a GPU program,
  164. * or may be used by fixed pipeline in some way.
  165. *
  166. * Buffer types:
  167. * - Raw buffers containing a block of bytes that are up to the GPU program to interpret.
  168. * - Structured buffer containing an array of structures compliant to a certain layout. Similar to raw
  169. * buffer but easier to interpret the data.
  170. * - Random read/write buffers that allow you to write to random parts of the buffer from within
  171. * the GPU program, and then read it later. These can only be bound to pixel and compute stages.
  172. * - Append/Consume buffers also allow you to write to them, but in a stack-like fashion, usually where one set
  173. * of programs produces data while other set consumes it from the same buffer. Append/Consume buffers are structured
  174. * by default.
  175. *
  176. * @note Sim thread only.
  177. */
  178. class BS_CORE_EXPORT GpuBuffer : public CoreObject
  179. {
  180. public:
  181. virtual ~GpuBuffer() { }
  182. /**
  183. * @brief Returns properties describing the buffer.
  184. */
  185. const GpuBufferProperties& getProperties() const { return mProperties; }
  186. /**
  187. * @brief Retrieves a core implementation of a GPU buffer usable only from the
  188. * core thread.
  189. */
  190. SPtr<GpuBufferCore> getCore() const;
  191. protected:
  192. friend class HardwareBufferManager;
  193. GpuBuffer(UINT32 elementCount, UINT32 elementSize, GpuBufferType type, GpuBufferUsage usage,
  194. bool randomGpuWrite = false, bool useCounter = false);
  195. /**
  196. * @copydoc CoreObject::createCore
  197. */
  198. SPtr<CoreObjectCore> createCore() const override;
  199. /**
  200. * @copydoc HardwareBufferManager::createGpuParamBlockBuffer
  201. */
  202. static GpuParamBlockBufferPtr create(UINT32 size, GpuParamBlockUsage usage = GPBU_DYNAMIC);
  203. GpuBufferProperties mProperties;
  204. };
  205. }