BsGpuBuffer.h 8.8 KB

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