BsGpuParamBlockBuffer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "CoreThread/BsCoreObject.h"
  6. namespace bs
  7. {
  8. /** @addtogroup RenderAPI
  9. * @{
  10. */
  11. /**
  12. * Represents a GPU parameter block buffer. Parameter block buffers are bound to GPU programs which then fetch
  13. * parameters from those buffers.
  14. *
  15. * Writing or reading from this buffer will translate directly to API calls that update the GPU.
  16. *
  17. * @note Sim thread only.
  18. */
  19. class BS_CORE_EXPORT GpuParamBlockBuffer : public CoreObject
  20. {
  21. public:
  22. GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage);
  23. virtual ~GpuParamBlockBuffer();
  24. /**
  25. * Write some data to the specified offset in the buffer.
  26. *
  27. * @note All values are in bytes. Actual hardware buffer update is delayed until rendering.
  28. */
  29. void write(UINT32 offset, const void* data, UINT32 size);
  30. /**
  31. * Read some data from the specified offset in the buffer.
  32. *
  33. * @note All values are in bytes. This reads from the cached CPU buffer and not from the GPU.
  34. */
  35. void read(UINT32 offset, void* data, UINT32 size);
  36. /**
  37. * Clear specified section of the buffer to zero.
  38. *
  39. * @note All values are in bytes. Actual hardware buffer update is delayed until rendering.
  40. */
  41. void zeroOut(UINT32 offset, UINT32 size);
  42. /** Returns internal cached data of the buffer. */
  43. const UINT8* getCachedData() const { return mCachedData; }
  44. /** Returns the size of the buffer in bytes. */
  45. UINT32 getSize() const { return mSize; }
  46. /** Retrieves a core implementation of a GPU param block buffer usable only from the core thread. */
  47. SPtr<ct::GpuParamBlockBuffer> getCore() const;
  48. /** @copydoc HardwareBufferManager::createGpuParamBlockBuffer */
  49. static SPtr<GpuParamBlockBuffer> create(UINT32 size, GpuParamBlockUsage usage = GPBU_DYNAMIC);
  50. protected:
  51. /** @copydoc CoreObject::createCore */
  52. SPtr<ct::CoreObject> createCore() const override;
  53. /** @copydoc CoreObject::syncToCore */
  54. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  55. GpuParamBlockUsage mUsage;
  56. UINT32 mSize;
  57. UINT8* mCachedData;
  58. };
  59. /** @} */
  60. namespace ct
  61. {
  62. /** @addtogroup RenderAPI-Internal
  63. * @{
  64. */
  65. /**
  66. * Core thread version of a bs::GpuParamBlockBuffer.
  67. *
  68. * @note Core thread only.
  69. */
  70. class BS_CORE_EXPORT GpuParamBlockBuffer : public CoreObject
  71. {
  72. public:
  73. GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage, GpuDeviceFlags deviceMask);
  74. virtual ~GpuParamBlockBuffer();
  75. /**
  76. * Writes all of the specified data to the buffer. Data size must be the same size as the buffer.
  77. *
  78. * @param[in] data Data to write. Must match the size of the buffer.
  79. * @param[in] queueIdx Device queue to perform the write operation on. See @ref queuesDoc.
  80. */
  81. virtual void writeToGPU(const UINT8* data, UINT32 queueIdx = 0) = 0;
  82. /**
  83. * Flushes any cached data into the actual GPU buffer.
  84. *
  85. * @param[in] queueIdx Device queue to perform the write operation on. See @ref queuesDoc.
  86. */
  87. void flushToGPU(UINT32 queueIdx = 0);
  88. /**
  89. * Write some data to the specified offset in the buffer.
  90. *
  91. * @note All values are in bytes. Actual hardware buffer update is delayed until rendering or until
  92. * flushToGPU() is called.
  93. */
  94. void write(UINT32 offset, const void* data, UINT32 size);
  95. /**
  96. * Read some data from the specified offset in the buffer.
  97. *
  98. * @note All values are in bytes. This reads from the cached CPU buffer and not directly from the GPU.
  99. */
  100. void read(UINT32 offset, void* data, UINT32 size);
  101. /**
  102. * Clear specified section of the buffer to zero.
  103. *
  104. * @note All values are in bytes. Actual hardware buffer update is delayed until rendering or until
  105. * flushToGPU() is called.
  106. */
  107. void zeroOut(UINT32 offset, UINT32 size);
  108. /** Returns the size of the buffer in bytes. */
  109. UINT32 getSize() const { return mSize; }
  110. /** @copydoc HardwareBufferManager::createGpuParamBlockBuffer */
  111. static SPtr<GpuParamBlockBuffer> create(UINT32 size, GpuParamBlockUsage usage = GPBU_DYNAMIC,
  112. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  113. protected:
  114. /** @copydoc CoreObject::syncToCore */
  115. void syncToCore(const CoreSyncData& data) override;
  116. GpuParamBlockUsage mUsage;
  117. UINT32 mSize;
  118. UINT8* mCachedData;
  119. bool mGPUBufferDirty;
  120. };
  121. /** @} */
  122. }
  123. }