BsGpuParamBlockBuffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCoreObject.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Core thread version of a GPU param block buffer.
  8. *
  9. * @see GpuParamBlockBuffer
  10. *
  11. * @note Core thread only.
  12. */
  13. class BS_CORE_EXPORT GpuParamBlockBufferCore : public CoreObjectCore
  14. {
  15. public:
  16. GpuParamBlockBufferCore(UINT32 size, GpuParamBlockUsage usage);
  17. virtual ~GpuParamBlockBufferCore();
  18. /**
  19. * @brief Writes all of the specified data to the buffer.
  20. * Data size must be the same size as the buffer;
  21. */
  22. virtual void writeToGPU(const UINT8* data) = 0;
  23. /**
  24. * @brief Copies data from the internal buffer to a pre-allocated array.
  25. * Be aware this generally isn't a very fast operation as reading
  26. * from the GPU will most definitely involve a CPU-GPU sync point.
  27. *
  28. * @param [in,out] data Array where the data will be written to. Must be of
  29. * "getSize()" bytes.
  30. */
  31. virtual void readFromGPU(UINT8* data) const = 0;
  32. /**
  33. * @brief Flushes any cached data into the actual GPU buffer.
  34. */
  35. void flushToGPU();
  36. /**
  37. * @brief Write some data to the specified offset in the buffer.
  38. *
  39. * @note All values are in bytes.
  40. * Actual hardware buffer update is delayed until rendering.
  41. */
  42. void write(UINT32 offset, const void* data, UINT32 size);
  43. /**
  44. * @brief Read some data from the specified offset in the buffer.
  45. *
  46. * @note All values are in bytes.
  47. * This reads from the cached CPU buffer. Actual hardware buffer can be read
  48. * from the core thread.
  49. */
  50. void read(UINT32 offset, void* data, UINT32 size);
  51. /**
  52. * @brief Clear specified section of the buffer to zero.
  53. *
  54. * @note All values are in bytes.
  55. * Actual hardware buffer update is delayed until rendering.
  56. */
  57. void zeroOut(UINT32 offset, UINT32 size);
  58. /**
  59. * @brief Returns the size of the buffer in bytes.
  60. */
  61. UINT32 getSize() const { return mSize; }
  62. /**
  63. * @copydoc HardwareBufferCoreManager::createGpuParamBlockBuffer
  64. */
  65. static SPtr<GpuParamBlockBufferCore> create(UINT32 size, GpuParamBlockUsage usage = GPBU_DYNAMIC);
  66. protected:
  67. /**
  68. * @copydoc CoreObjectCore::syncToCore
  69. */
  70. virtual void syncToCore(const CoreSyncData& data) override;
  71. GpuParamBlockUsage mUsage;
  72. UINT32 mSize;
  73. UINT8* mCachedData;
  74. bool mGPUBufferDirty;
  75. };
  76. /**
  77. * @brief Represents a GPU parameter block buffer. Parameter block buffers
  78. * are bound to GPU programs which then fetch parameters from those buffers.
  79. *
  80. * Writing or reading from this buffer will translate directly to API calls
  81. * that update the GPU.
  82. *
  83. * @note Sim thread only.
  84. */
  85. class BS_CORE_EXPORT GpuParamBlockBuffer : public CoreObject
  86. {
  87. public:
  88. GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage);
  89. virtual ~GpuParamBlockBuffer();
  90. /**
  91. * @brief Write some data to the specified offset in the buffer.
  92. *
  93. * @note All values are in bytes.
  94. * Actual hardware buffer update is delayed until rendering.
  95. */
  96. void write(UINT32 offset, const void* data, UINT32 size);
  97. /**
  98. * @brief Read some data from the specified offset in the buffer.
  99. *
  100. * @note All values are in bytes.
  101. * This reads from the cached CPU buffer. Actual hardware buffer can be read
  102. * from the core thread.
  103. */
  104. void read(UINT32 offset, void* data, UINT32 size);
  105. /**
  106. * @brief Clear specified section of the buffer to zero.
  107. *
  108. * @note All values are in bytes.
  109. * Actual hardware buffer update is delayed until rendering.
  110. */
  111. void zeroOut(UINT32 offset, UINT32 size);
  112. /**
  113. * @brief Returns internal cached data of the buffer.
  114. */
  115. const UINT8* getCachedData() const { return mCachedData; }
  116. /**
  117. * @brief Returns the size of the buffer in bytes.
  118. */
  119. UINT32 getSize() const { return mSize; }
  120. /**
  121. * @brief Retrieves a core implementation of a GPU param block buffer usable only from the
  122. * core thread.
  123. */
  124. SPtr<GpuParamBlockBufferCore> getCore() const;
  125. /**
  126. * @copydoc HardwareBufferManager::createGpuParamBlockBuffer
  127. */
  128. static GpuParamBlockBufferPtr create(UINT32 size, GpuParamBlockUsage usage = GPBU_DYNAMIC);
  129. protected:
  130. /**
  131. * @copydoc CoreObject::createCore
  132. */
  133. SPtr<CoreObjectCore> createCore() const override;
  134. /**
  135. * @copydoc CoreObject::syncToCore
  136. */
  137. virtual CoreSyncData syncToCore(FrameAlloc* allocator) override;
  138. GpuParamBlockUsage mUsage;
  139. UINT32 mSize;
  140. UINT8* mCachedData;
  141. };
  142. /**
  143. * @brief Implementation of a GpuParamBlock buffer that doesn't use a GPU buffer
  144. * for storage. Used with APIs that do not support GPU parameter buffers.
  145. */
  146. class BS_CORE_EXPORT GenericGpuParamBlockBufferCore : public GpuParamBlockBufferCore
  147. {
  148. public:
  149. GenericGpuParamBlockBufferCore(UINT32 size, GpuParamBlockUsage usage);
  150. ~GenericGpuParamBlockBufferCore();
  151. /**
  152. * @copydoc GpuParamBlockBufferCore::writeData
  153. */
  154. void writeToGPU(const UINT8* data) override;
  155. /**
  156. * @copydoc GpuParamBlockBufferCore::readData.
  157. */
  158. void readFromGPU(UINT8* data) const override;
  159. protected:
  160. UINT8* mData;
  161. /**
  162. * @copydoc CoreObjectCore::initialize
  163. */
  164. virtual void initialize() override;
  165. };
  166. }