BsGpuBuffer.h 7.0 KB

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