BsGpuParamBlockBuffer.cpp 4.8 KB

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