BsGpuParamBlockBuffer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. protected:
  63. /**
  64. * @copydoc CoreObjectCore::syncToCore
  65. */
  66. virtual void syncToCore(const CoreSyncData& data);
  67. GpuParamBlockUsage mUsage;
  68. UINT32 mSize;
  69. UINT8* mCachedData;
  70. bool mGPUBufferDirty;
  71. };
  72. /**
  73. * @brief Represents a GPU parameter block buffer. Parameter block buffers
  74. * are bound to GPU programs which then fetch parameters from those buffers.
  75. *
  76. * Writing or reading from this buffer will translate directly to API calls
  77. * that update the GPU.
  78. *
  79. * @note Sim thread only.
  80. */
  81. class BS_CORE_EXPORT GpuParamBlockBuffer : public CoreObject
  82. {
  83. public:
  84. GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage);
  85. virtual ~GpuParamBlockBuffer();
  86. /**
  87. * @brief Write some data to the specified offset in the buffer.
  88. *
  89. * @note All values are in bytes.
  90. * Actual hardware buffer update is delayed until rendering.
  91. */
  92. void write(UINT32 offset, const void* data, UINT32 size);
  93. /**
  94. * @brief Read some data from the specified offset in the buffer.
  95. *
  96. * @note All values are in bytes.
  97. * This reads from the cached CPU buffer. Actual hardware buffer can be read
  98. * from the core thread.
  99. */
  100. void read(UINT32 offset, void* data, UINT32 size);
  101. /**
  102. * @brief Clear specified section of the buffer to zero.
  103. *
  104. * @note All values are in bytes.
  105. * Actual hardware buffer update is delayed until rendering.
  106. */
  107. void zeroOut(UINT32 offset, UINT32 size);
  108. /**
  109. * @brief Returns internal cached data of the buffer.
  110. */
  111. const UINT8* getCachedData() const { return mCachedData; }
  112. /**
  113. * @brief Returns the size of the buffer in bytes.
  114. */
  115. UINT32 getSize() const { return mSize; }
  116. /**
  117. * @brief Retrieves a core implementation of a GPU param block buffer usable only from the
  118. * core thread.
  119. */
  120. SPtr<GpuParamBlockBufferCore> getCore() const;
  121. /**
  122. * @copydoc HardwareBufferManager::createGpuParamBlockBuffer
  123. */
  124. static GpuParamBlockBufferPtr create(UINT32 size, GpuParamBlockUsage usage = GPBU_DYNAMIC);
  125. protected:
  126. /**
  127. * @copydoc CoreObject::createCore
  128. */
  129. SPtr<CoreObjectCore> createCore() const;
  130. /**
  131. * @copydoc CoreObject::syncToCore
  132. */
  133. virtual CoreSyncData syncToCore(FrameAlloc* allocator);
  134. GpuParamBlockUsage mUsage;
  135. UINT32 mSize;
  136. UINT8* mCachedData;
  137. };
  138. /**
  139. * @brief Implementation of a GpuParamBlock buffer that doesn't use a GPU buffer
  140. * for storage. Used with APIs that do not support GPU parameter buffers.
  141. */
  142. class BS_CORE_EXPORT GenericGpuParamBlockBufferCore : public GpuParamBlockBufferCore
  143. {
  144. public:
  145. GenericGpuParamBlockBufferCore(UINT32 size, GpuParamBlockUsage usage);
  146. ~GenericGpuParamBlockBufferCore();
  147. /**
  148. * @copydoc GpuParamBlockBufferCore::writeData
  149. */
  150. void writeToGPU(const UINT8* data);
  151. /**
  152. * @copydoc GpuParamBlockBufferCore::readData.
  153. */
  154. void readFromGPU(UINT8* data) const;
  155. protected:
  156. UINT8* mData;
  157. /**
  158. * @copydoc CoreObjectCore::initialize
  159. */
  160. virtual void initialize();
  161. };
  162. }