BsGpuBuffer.h 9.8 KB

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