BsGpuParamBlockBuffer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "BsGpuParamBlockBuffer.h"
  2. #include "BsHardwareBufferManager.h"
  3. #include "BsFrameAlloc.h"
  4. namespace BansheeEngine
  5. {
  6. GpuParamBlockBufferCore::GpuParamBlockBufferCore(UINT32 size, GpuParamBlockUsage usage)
  7. :mSize(size), mUsage(usage), mCachedData(nullptr), mGPUBufferDirty(false)
  8. {
  9. if (mSize > 0)
  10. mCachedData = (UINT8*)bs_alloc(mSize);
  11. memset(mCachedData, 0, mSize);
  12. }
  13. GpuParamBlockBufferCore::~GpuParamBlockBufferCore()
  14. {
  15. if (mCachedData != nullptr)
  16. bs_free(mCachedData);
  17. }
  18. void GpuParamBlockBufferCore::write(UINT32 offset, const void* data, UINT32 size)
  19. {
  20. #if BS_DEBUG_MODE
  21. if (offset < 0 || (offset + size) > mSize)
  22. {
  23. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  24. "Available range: 0 .. " + toString(mSize) + ". " \
  25. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  26. }
  27. #endif
  28. memcpy(mCachedData + offset, data, size);
  29. mGPUBufferDirty = true;
  30. }
  31. void GpuParamBlockBufferCore::read(UINT32 offset, void* data, UINT32 size)
  32. {
  33. #if BS_DEBUG_MODE
  34. if (offset < 0 || (offset + size) > mSize)
  35. {
  36. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  37. "Available range: 0 .. " + toString(mSize) + ". " \
  38. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  39. }
  40. #endif
  41. memcpy(data, mCachedData + offset, size);
  42. }
  43. void GpuParamBlockBufferCore::zeroOut(UINT32 offset, UINT32 size)
  44. {
  45. #if BS_DEBUG_MODE
  46. if (offset < 0 || (offset + size) > mSize)
  47. {
  48. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  49. "Available range: 0 .. " + toString(mSize) + ". " \
  50. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  51. }
  52. #endif
  53. memset(mCachedData + offset, 0, size);
  54. mGPUBufferDirty = true;
  55. }
  56. void GpuParamBlockBufferCore::flushToGPU()
  57. {
  58. if (mGPUBufferDirty)
  59. {
  60. writeToGPU(mCachedData);
  61. mGPUBufferDirty = false;
  62. }
  63. }
  64. void GpuParamBlockBufferCore::syncToCore(const CoreSyncData& data)
  65. {
  66. assert(mSize == data.getBufferSize());
  67. write(0, data.getBuffer(), data.getBufferSize());
  68. }
  69. GpuParamBlockBuffer::GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage)
  70. :mSize(size), mUsage(usage), mCachedData(nullptr)
  71. {
  72. if (mSize > 0)
  73. mCachedData = (UINT8*)bs_alloc(mSize);
  74. memset(mCachedData, 0, mSize);
  75. }
  76. GpuParamBlockBuffer::~GpuParamBlockBuffer()
  77. {
  78. if (mCachedData != nullptr)
  79. bs_free(mCachedData);
  80. }
  81. void GpuParamBlockBuffer::write(UINT32 offset, const void* data, UINT32 size)
  82. {
  83. #if BS_DEBUG_MODE
  84. if (offset < 0 || (offset + size) > mSize)
  85. {
  86. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  87. "Available range: 0 .. " + toString(mSize) + ". " \
  88. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  89. }
  90. #endif
  91. memcpy(mCachedData + offset, data, size);
  92. markCoreDirty();
  93. }
  94. void GpuParamBlockBuffer::read(UINT32 offset, void* data, UINT32 size)
  95. {
  96. #if BS_DEBUG_MODE
  97. if (offset < 0 || (offset + size) > mSize)
  98. {
  99. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  100. "Available range: 0 .. " + toString(mSize) + ". " \
  101. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  102. }
  103. #endif
  104. memcpy(data, mCachedData + offset, size);
  105. }
  106. void GpuParamBlockBuffer::zeroOut(UINT32 offset, UINT32 size)
  107. {
  108. #if BS_DEBUG_MODE
  109. if (offset < 0 || (offset + size) > mSize)
  110. {
  111. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  112. "Available range: 0 .. " + toString(mSize) + ". " \
  113. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  114. }
  115. #endif
  116. memset(mCachedData + offset, 0, size);
  117. markCoreDirty();
  118. }
  119. SPtr<GpuParamBlockBufferCore> GpuParamBlockBuffer::getCore() const
  120. {
  121. return std::static_pointer_cast<GpuParamBlockBufferCore>(mCoreSpecific);
  122. }
  123. SPtr<CoreObjectCore> GpuParamBlockBuffer::createCore() const
  124. {
  125. return HardwareBufferCoreManager::instance().createGpuParamBlockBufferInternal(mSize, mUsage);
  126. }
  127. CoreSyncData GpuParamBlockBuffer::syncToCore(FrameAlloc* allocator)
  128. {
  129. UINT8* buffer = allocator->alloc(mSize);
  130. read(0, buffer, mSize);
  131. return CoreSyncData(buffer, mSize);
  132. }
  133. GenericGpuParamBlockBufferCore::GenericGpuParamBlockBufferCore(UINT32 size, GpuParamBlockUsage usage)
  134. :GpuParamBlockBufferCore(size, usage), mData(nullptr)
  135. { }
  136. GenericGpuParamBlockBufferCore::~GenericGpuParamBlockBufferCore()
  137. {
  138. if (mData != nullptr)
  139. bs_free<ScratchAlloc>(mData);
  140. }
  141. void GenericGpuParamBlockBufferCore::writeToGPU(const UINT8* data)
  142. {
  143. memcpy(mData, data, mSize);
  144. }
  145. void GenericGpuParamBlockBufferCore::readFromGPU(UINT8* data) const
  146. {
  147. memcpy(data, mData, mSize);
  148. }
  149. void GenericGpuParamBlockBufferCore::initialize()
  150. {
  151. if (mSize > 0)
  152. mData = (UINT8*)bs_alloc<ScratchAlloc>(mSize);
  153. else
  154. mData = nullptr;
  155. memset(mData, 0, mSize);
  156. GpuParamBlockBufferCore::initialize();
  157. }
  158. static GpuParamBlockBufferPtr create(UINT32 size, GpuParamBlockUsage usage)
  159. {
  160. return HardwareBufferManager::instance().createGpuParamBlockBuffer(size, usage);
  161. }
  162. }