BsGpuParamBlockBuffer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "Allocators/BsFrameAlloc.h"
  6. namespace bs
  7. {
  8. GpuParamBlockBuffer::GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage)
  9. :mUsage(usage), mSize(size), mCachedData(nullptr)
  10. {
  11. if (mSize > 0)
  12. mCachedData = (UINT8*)bs_alloc(mSize);
  13. memset(mCachedData, 0, mSize);
  14. }
  15. GpuParamBlockBuffer::~GpuParamBlockBuffer()
  16. {
  17. if (mCachedData != nullptr)
  18. bs_free(mCachedData);
  19. }
  20. void GpuParamBlockBuffer::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. markCoreDirty();
  32. }
  33. void GpuParamBlockBuffer::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 GpuParamBlockBuffer::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. markCoreDirty();
  57. }
  58. SPtr<ct::GpuParamBlockBuffer> GpuParamBlockBuffer::getCore() const
  59. {
  60. return std::static_pointer_cast<ct::GpuParamBlockBuffer>(mCoreSpecific);
  61. }
  62. SPtr<ct::CoreObject> GpuParamBlockBuffer::createCore() const
  63. {
  64. return ct::HardwareBufferManager::instance().createGpuParamBlockBufferInternal(mSize, mUsage);
  65. }
  66. CoreSyncData GpuParamBlockBuffer::syncToCore(FrameAlloc* allocator)
  67. {
  68. UINT8* buffer = allocator->alloc(mSize);
  69. read(0, buffer, mSize);
  70. return CoreSyncData(buffer, mSize);
  71. }
  72. SPtr<GpuParamBlockBuffer> GpuParamBlockBuffer::create(UINT32 size, GpuParamBlockUsage usage)
  73. {
  74. return HardwareBufferManager::instance().createGpuParamBlockBuffer(size, usage);
  75. }
  76. namespace ct
  77. {
  78. GpuParamBlockBuffer::GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage, GpuDeviceFlags deviceMask)
  79. :mUsage(usage), mSize(size), mCachedData(nullptr), mGPUBufferDirty(false)
  80. {
  81. if (mSize > 0)
  82. mCachedData = (UINT8*)bs_alloc(mSize);
  83. memset(mCachedData, 0, mSize);
  84. }
  85. GpuParamBlockBuffer::~GpuParamBlockBuffer()
  86. {
  87. if (mCachedData != nullptr)
  88. bs_free(mCachedData);
  89. }
  90. void GpuParamBlockBuffer::write(UINT32 offset, const void* data, UINT32 size)
  91. {
  92. #if BS_DEBUG_MODE
  93. if ((offset + size) > mSize)
  94. {
  95. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  96. "Available range: 0 .. " + toString(mSize) + ". " \
  97. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  98. }
  99. #endif
  100. memcpy(mCachedData + offset, data, size);
  101. mGPUBufferDirty = true;
  102. }
  103. void GpuParamBlockBuffer::read(UINT32 offset, void* data, UINT32 size)
  104. {
  105. #if BS_DEBUG_MODE
  106. if ((offset + size) > mSize)
  107. {
  108. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  109. "Available range: 0 .. " + toString(mSize) + ". " \
  110. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  111. }
  112. #endif
  113. memcpy(data, mCachedData + offset, size);
  114. }
  115. void GpuParamBlockBuffer::zeroOut(UINT32 offset, UINT32 size)
  116. {
  117. #if BS_DEBUG_MODE
  118. if ((offset + size) > mSize)
  119. {
  120. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  121. "Available range: 0 .. " + toString(mSize) + ". " \
  122. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  123. }
  124. #endif
  125. memset(mCachedData + offset, 0, size);
  126. mGPUBufferDirty = true;
  127. }
  128. void GpuParamBlockBuffer::flushToGPU(UINT32 queueIdx)
  129. {
  130. if (mGPUBufferDirty)
  131. {
  132. writeToGPU(mCachedData, queueIdx);
  133. mGPUBufferDirty = false;
  134. }
  135. }
  136. void GpuParamBlockBuffer::syncToCore(const CoreSyncData& data)
  137. {
  138. assert(mSize == data.getBufferSize());
  139. write(0, data.getBuffer(), data.getBufferSize());
  140. }
  141. SPtr<GpuParamBlockBuffer> GpuParamBlockBuffer::create(UINT32 size, GpuParamBlockUsage usage,
  142. GpuDeviceFlags deviceMask)
  143. {
  144. return HardwareBufferManager::instance().createGpuParamBlockBuffer(size, usage, deviceMask);
  145. }
  146. }
  147. }