BsGpuBuffer.h 10 KB

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