BsGpuBuffer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "BsCoreObject.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup RenderAPI
  9. * @{
  10. */
  11. /** Descriptor structure used for initialization of a GpuBuffer. */
  12. struct GPU_BUFFER_DESC
  13. {
  14. /** Number of elements in the buffer. */
  15. UINT32 elementCount;
  16. /**
  17. * Size of each individual element in the buffer, in bytes. Only needed if using non-standard buffer. If using
  18. * standard buffers element size is calculated from format and this must be zero.
  19. */
  20. UINT32 elementSize;
  21. /** Type of the buffer. Determines how is buffer seen by the GPU program and in what ways can it be used. */
  22. GpuBufferType type;
  23. /** Format if the data in the buffer. Only relevant for standard buffers, must be BF_UNKNOWN otherwise. */
  24. GpuBufferFormat format;
  25. /** Usage that tells the hardware how will be buffer be used. */
  26. GpuBufferUsage usage = GBU_STATIC;
  27. /** When true allows the GPU to write to the resource. Must be enabled if buffer type is GBT_APPENDCONSUME. */
  28. bool randomGpuWrite = false;
  29. /**
  30. * When true binds a counter that can be used from a GPU program on the buffer. Can only be used in combination
  31. * with GBT_STRUCTURED and randomGpuWrite must be enabled.
  32. */
  33. bool useCounter = false;
  34. };
  35. /**
  36. * Information about a GpuBuffer. Allows core and non-core versions of GpuBuffer to share the same structure for
  37. * properties.
  38. */
  39. class BS_CORE_EXPORT GpuBufferProperties
  40. {
  41. public:
  42. GpuBufferProperties(const GPU_BUFFER_DESC& desc);
  43. /**
  44. * Returns the type of the GPU buffer. Type determines which kind of views (if any) can be created for the buffer,
  45. * and how is data read or modified in it.
  46. */
  47. GpuBufferType getType() const { return mDesc.type; }
  48. /** Returns format used by the buffer. Only relevant for standard buffers. */
  49. GpuBufferFormat getFormat() const { return mDesc.format; }
  50. /** Returns buffer usage which determines how are planning on updating the buffer contents. */
  51. GpuBufferUsage getUsage() const { return mDesc.usage; }
  52. /** Return whether the buffer supports random reads and writes within the GPU programs. */
  53. bool getRandomGpuWrite() const { return mDesc.randomGpuWrite; }
  54. /** Returns whether the buffer supports counter use within GPU programs. */
  55. bool getUseCounter() const { return mDesc.useCounter; }
  56. /** Returns number of elements in the buffer. */
  57. UINT32 getElementCount() const { return mDesc.elementCount; }
  58. /** Returns size of a single element in the buffer in bytes. */
  59. UINT32 getElementSize() const { return mDesc.elementSize; }
  60. protected:
  61. friend class GpuBuffer;
  62. GPU_BUFFER_DESC mDesc;
  63. };
  64. /**
  65. * Handles a generic GPU buffer that you may use for storing any kind of data you wish to be accessible to the GPU.
  66. * These buffers may be bounds to GPU program binding slots and accessed from a GPU program, or may be used by fixed
  67. * pipeline in some way.
  68. *
  69. * Buffer types:
  70. * - Raw buffers containing a block of bytes that are up to the GPU program to interpret.
  71. * - Structured buffer containing an array of structures compliant to a certain layout. Similar to raw buffer but
  72. * easier to interpret the data.
  73. * - Random read/write buffers that allow you to write to random parts of the buffer from within the GPU program, and
  74. * then read it later. These can only be bound to pixel and compute stages.
  75. * - Append/Consume buffers also allow you to write to them, but in a stack-like fashion, usually where one set of
  76. * programs produces data while other set consumes it from the same buffer. Append/Consume buffers are structured
  77. * by default.
  78. *
  79. * @note Sim thread only.
  80. */
  81. class BS_CORE_EXPORT GpuBuffer : public CoreObject
  82. {
  83. public:
  84. virtual ~GpuBuffer() { }
  85. /** Returns properties describing the buffer. */
  86. const GpuBufferProperties& getProperties() const { return mProperties; }
  87. /** Retrieves a core implementation of a GPU buffer usable only from the core thread. */
  88. SPtr<GpuBufferCore> getCore() const;
  89. /** Returns the size of a single element in the buffer, of the provided format, in bytes. */
  90. static UINT32 getFormatSize(GpuBufferFormat format);
  91. /** @copydoc HardwareBufferManager::createGpuBuffer */
  92. static SPtr<GpuBuffer> create(const GPU_BUFFER_DESC& desc);
  93. protected:
  94. friend class HardwareBufferManager;
  95. GpuBuffer(const GPU_BUFFER_DESC& desc);
  96. /** @copydoc CoreObject::createCore */
  97. SPtr<CoreObjectCore> createCore() const override;
  98. GpuBufferProperties mProperties;
  99. };
  100. /** @} */
  101. /** @addtogroup RenderAPI-Internal
  102. * @{
  103. */
  104. /**
  105. * Core thread version of a GpuBuffer.
  106. *
  107. * @note Core thread only.
  108. */
  109. class BS_CORE_EXPORT GpuBufferCore : public CoreObjectCore
  110. {
  111. public:
  112. virtual ~GpuBufferCore();
  113. /**
  114. * Locks the buffer returning a pointer to the internal buffer data that you may then read or write to.
  115. * Caller must ensure it will only perform actions promised in the provided GPU lock options parameter.
  116. *
  117. * @param[in] offset Number of bytes at which to lock the buffer. Returned pointer points to this location.
  118. * @param[in] length Number of bytes to lock.
  119. * @param[in] options How to lock the buffer. Certain options offer better performance than others.
  120. */
  121. virtual void* lock(UINT32 offset, UINT32 length, GpuLockOptions options) = 0;
  122. /**
  123. * Unlocks a previously locked buffer. Any pointers to internal buffers returned when it was locked will become
  124. * invalid.
  125. */
  126. virtual void unlock() = 0;
  127. /**
  128. * Reads buffer data into the previously allocated buffer.
  129. *
  130. * @param[in] offset Number of bytes at which to start reading the buffer.
  131. * @param[in] length Number of bytes to read.
  132. * @param[in] pDest Previously allocated buffer of @p length bytes size that the data will be written to.
  133. */
  134. virtual void readData(UINT32 offset, UINT32 length, void* pDest) = 0;
  135. /**
  136. * Writes data into the buffer.
  137. *
  138. * @param[in] offset Number of bytes at which to start writing to the buffer.
  139. * @param[in] length Number of bytes to write.
  140. * @param[in] pSource Buffer containg the data to write.
  141. * @param[in] writeFlags Flags that may be used to improve performance for specific use cases.
  142. */
  143. virtual void writeData(UINT32 offset, UINT32 length, const void* pSource,
  144. BufferWriteType writeFlags = BWT_NORMAL) = 0;
  145. /**
  146. * Copies data from another buffer into this buffer.
  147. *
  148. * @param[in] srcBuffer Buffer to copy the data from.
  149. * @param[in] srcOffset Offset in bytes into the source buffer - this is where reading starts from.
  150. * @param[in] dstOffset Offset in bytes into the destination buffer - this is where writing starts from.
  151. * @param[in] length Number of bytes to copy from source to destination.
  152. * @param[in] discardWholeBuffer If true, the contents of the current buffer will be entirely discarded. This can
  153. * improve performance if you know you wont be needing that data any more.
  154. */
  155. virtual void copyData(GpuBufferCore& srcBuffer, UINT32 srcOffset,
  156. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer = false) = 0;
  157. /** Returns properties describing the buffer. */
  158. const GpuBufferProperties& getProperties() const { return mProperties; }
  159. /** @copydoc HardwareBufferCoreManager::createGpuBuffer */
  160. static SPtr<GpuBufferCore> create(const GPU_BUFFER_DESC& desc, GpuDeviceFlags deviceMask = GDF_DEFAULT);
  161. protected:
  162. GpuBufferCore(const GPU_BUFFER_DESC& desc, UINT32 deviceMask);
  163. GpuBufferProperties mProperties;
  164. };
  165. /** @} */
  166. }