BsGpuBuffer.h 8.8 KB

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