BsGpuBuffer.h 9.4 KB

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