BsGpuParamBlockBuffer.h 5.2 KB

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