2
0

CmGpuParamBlock.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "CmGpuParamBlock.h"
  2. #include "CmGpuParamDesc.h"
  3. #include "CmHardwareBufferManager.h"
  4. #include "CmException.h"
  5. namespace CamelotEngine
  6. {
  7. GpuParamBlockBuffer::GpuParamBlockBuffer(UINT32 size, GpuParamBlockUsage usage)
  8. :mData(CM_NEW_BYTES(size, ScratchAlloc)), mSize(size), mUsage(usage)
  9. {
  10. memset(mData, 0, mSize);
  11. }
  12. GpuParamBlockBuffer::~GpuParamBlockBuffer()
  13. {
  14. if(mData != nullptr)
  15. CM_DELETE_BYTES(mData, ScratchAlloc);
  16. }
  17. void GpuParamBlockBuffer::writeAll(const void* data)
  18. {
  19. memcpy(mData, data, mSize);
  20. }
  21. const UINT8* GpuParamBlockBuffer::getDataPtr(UINT32 offset) const
  22. {
  23. #if CM_DEBUG_MODE
  24. if(offset < 0 || offset >= mSize)
  25. {
  26. CM_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  27. "Available range: 0 .. " + toString(mSize) + ". " \
  28. "Wanted range: " + toString(offset) + " .. " + toString(offset) + ".");
  29. }
  30. #endif
  31. return &mData[offset];
  32. }
  33. GpuParamBlock::GpuParamBlock()
  34. :mDirty(true), mUsage(GPBU_DYNAMIC), mData(nullptr), mBuffer(nullptr)
  35. {
  36. }
  37. GpuParamBlock::~GpuParamBlock()
  38. {
  39. if(mData != nullptr)
  40. CM_DELETE_BYTES(mData, ScratchAlloc);
  41. }
  42. void GpuParamBlock::initialize(const GpuParamBlockDesc& desc, GpuParamBlockUsage usage)
  43. {
  44. mSize = desc.blockSize * sizeof(UINT32);
  45. mData = CM_NEW_BYTES(mSize, ScratchAlloc);
  46. memset(mData, 0, mSize);
  47. mUsage = usage;
  48. CoreObject::initialize();
  49. }
  50. void GpuParamBlock::initialize_internal()
  51. {
  52. mBuffer = createBuffer();
  53. CoreObject::initialize_internal();
  54. }
  55. void GpuParamBlock::destroy_internal()
  56. {
  57. if(mBuffer != nullptr)
  58. CM_DELETE(mBuffer, GpuParamBlockBuffer, PoolAlloc);
  59. CoreObject::destroy_internal();
  60. }
  61. GpuParamBlockBuffer* GpuParamBlock::createBuffer() const
  62. {
  63. return CM_NEW(GpuParamBlockBuffer, PoolAlloc) GpuParamBlockBuffer(mSize, mUsage);
  64. }
  65. void GpuParamBlock::write(UINT32 offset, const void* data, UINT32 size)
  66. {
  67. #if CM_DEBUG_MODE
  68. if(offset < 0 || (offset + size) > mSize)
  69. {
  70. CM_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  71. "Available range: 0 .. " + toString(mSize) + ". " \
  72. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  73. }
  74. #endif
  75. memcpy(mData + offset, data, size);
  76. mDirty = true;
  77. }
  78. void GpuParamBlock::zeroOut(UINT32 offset, UINT32 size)
  79. {
  80. #if CM_DEBUG_MODE
  81. if(offset < 0 || (offset + size) > mSize)
  82. {
  83. CM_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  84. "Available range: 0 .. " + toString(mSize) + ". " \
  85. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  86. }
  87. #endif
  88. memset(mData + offset, 0, size);
  89. mDirty = true;
  90. }
  91. void GpuParamBlock::updateBuffer()
  92. {
  93. if(mDirty)
  94. {
  95. mDirty = false;
  96. // Need to copy the data, as non-render threads might modify
  97. // the data before render thread has a chance to process it
  98. // TODO - Use an allocator
  99. UINT8* dataCopy = CM_NEW_BYTES(mSize, ScratchAlloc);
  100. memcpy(dataCopy, mData, mSize);
  101. queueGpuCommand(getThisPtr(), boost::bind(&GpuParamBlock::updateBuffer_internal, this, dataCopy));
  102. }
  103. }
  104. void GpuParamBlock::updateBuffer_internal(UINT8* data)
  105. {
  106. assert(mBuffer != nullptr);
  107. mBuffer->writeAll(data);
  108. CM_DELETE_BYTES(data, ScratchAlloc);
  109. }
  110. GpuParamBlockPtr GpuParamBlock::create(const GpuParamBlockDesc& desc)
  111. {
  112. return HardwareBufferManager::instance().createGpuParamBlock(desc);
  113. }
  114. }