BsGpuParamBlockBuffer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, GpuDeviceFlags deviceMask);
  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. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  55. protected:
  56. /** @copydoc CoreObjectCore::syncToCore */
  57. 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, GpuDeviceFlags deviceMask);
  71. ~GenericGpuParamBlockBufferCore();
  72. /** @copydoc GpuParamBlockBufferCore::writeToGPU */
  73. void writeToGPU(const UINT8* data) override;
  74. /** @copydoc GpuParamBlockBufferCore::readFromGPU */
  75. void readFromGPU(UINT8* data) const override;
  76. protected:
  77. UINT8* mData;
  78. /** @copydoc CoreObjectCore::initialize */
  79. void initialize() override;
  80. };
  81. /** @} */
  82. /** @addtogroup RenderAPI
  83. * @{
  84. */
  85. /**
  86. * Represents a GPU parameter block buffer. Parameter block buffers are bound to GPU programs which then fetch
  87. * parameters from those buffers.
  88. *
  89. * Writing or reading from this buffer will translate directly to API calls that update the GPU.
  90. *
  91. * @note Sim thread only.
  92. */
  93. class BS_CORE_EXPORT GpuParamBlockBuffer : public CoreObject
  94. {
  95. public:
  96. GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage);
  97. virtual ~GpuParamBlockBuffer();
  98. /**
  99. * Write some data to the specified offset in the buffer.
  100. *
  101. * @note All values are in bytes. Actual hardware buffer update is delayed until rendering.
  102. */
  103. void write(UINT32 offset, const void* data, UINT32 size);
  104. /**
  105. * Read some data from the specified offset in the buffer.
  106. *
  107. * @note All values are in bytes. This reads from the cached CPU buffer and not from the GPU.
  108. */
  109. void read(UINT32 offset, void* data, UINT32 size);
  110. /**
  111. * Clear specified section of the buffer to zero.
  112. *
  113. * @note All values are in bytes. Actual hardware buffer update is delayed until rendering.
  114. */
  115. void zeroOut(UINT32 offset, UINT32 size);
  116. /** Returns internal cached data of the buffer. */
  117. const UINT8* getCachedData() const { return mCachedData; }
  118. /** Returns the size of the buffer in bytes. */
  119. UINT32 getSize() const { return mSize; }
  120. /** Retrieves a core implementation of a GPU param block buffer usable only from the core thread. */
  121. SPtr<GpuParamBlockBufferCore> getCore() const;
  122. /** @copydoc HardwareBufferManager::createGpuParamBlockBuffer */
  123. static SPtr<GpuParamBlockBuffer> create(UINT32 size, GpuParamBlockUsage usage = GPBU_DYNAMIC);
  124. protected:
  125. /** @copydoc CoreObject::createCore */
  126. SPtr<CoreObjectCore> createCore() const override;
  127. /** @copydoc CoreObject::syncToCore */
  128. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  129. GpuParamBlockUsage mUsage;
  130. UINT32 mSize;
  131. UINT8* mCachedData;
  132. };
  133. /** @endcond */
  134. /** @} */
  135. }