BsGpuParamBlockBuffer.h 5.2 KB

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