BsGpuParamBlockBuffer.cpp 4.8 KB

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