BsGpuParamBlockBuffer.h 4.9 KB

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