CmGpuBuffer.h 7.2 KB

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