CmGpuParamBlock.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "CmGpuParamBlock.h"
  2. #include "CmGpuParamDesc.h"
  3. #include "CmHardwareBufferManager.h"
  4. #include "CmException.h"
  5. namespace CamelotEngine
  6. {
  7. GpuParamBlockBuffer::GpuParamBlockBuffer(const GpuParamBlockDesc& desc)
  8. :mSize(desc.blockSize * sizeof(UINT32)), mOwnsSharedData(true)
  9. {
  10. mData = new UINT8[mSize];
  11. memset(mData, 0, mSize);
  12. sharedData = new GpuParamBlockSharedData();
  13. sharedData->mDirty = true;
  14. sharedData->mInitialized = false;
  15. }
  16. GpuParamBlockBuffer::~GpuParamBlockBuffer()
  17. {
  18. delete [] mData;
  19. if(mOwnsSharedData)
  20. delete sharedData;
  21. }
  22. void GpuParamBlockBuffer::write(UINT32 offset, const void* data, UINT32 size)
  23. {
  24. #if CM_DEBUG_MODE
  25. if(offset < 0 || (offset + size) > mSize)
  26. {
  27. CM_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  28. "Available range: 0 .. " + toString(mSize) + ". " \
  29. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  30. }
  31. #endif
  32. memcpy(mData + offset, data, size);
  33. sharedData->mDirty = true;
  34. }
  35. void GpuParamBlockBuffer::zeroOut(UINT32 offset, UINT32 size)
  36. {
  37. #if CM_DEBUG_MODE
  38. if(offset < 0 || (offset + size) > mSize)
  39. {
  40. CM_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  41. "Available range: 0 .. " + toString(mSize) + ". " \
  42. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  43. }
  44. #endif
  45. memset(mData + offset, 0, size);
  46. sharedData->mDirty = true;
  47. }
  48. const UINT8* GpuParamBlockBuffer::getDataPtr(UINT32 offset) const
  49. {
  50. #if CM_DEBUG_MODE
  51. if(offset < 0 || offset >= mSize)
  52. {
  53. CM_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  54. "Available range: 0 .. " + toString(mSize) + ". " \
  55. "Wanted range: " + toString(offset) + " .. " + toString(offset) + ".");
  56. }
  57. #endif
  58. return &mData[offset];
  59. }
  60. void GpuParamBlockBuffer::updateIfDirty()
  61. {
  62. sharedData->mDirty = false;
  63. // Do nothing
  64. }
  65. GpuParamBlockBufferPtr GpuParamBlockBuffer::clone() const
  66. {
  67. GpuParamBlockBufferPtr clonedParamBlock(new GpuParamBlockBuffer(*this));
  68. clonedParamBlock->mData = new UINT8[mSize];
  69. clonedParamBlock->mSize = mSize;
  70. clonedParamBlock->mOwnsSharedData = false;
  71. memcpy(clonedParamBlock->mData, mData, mSize);
  72. return clonedParamBlock;
  73. }
  74. GpuParamBlockBufferPtr GpuParamBlockBuffer::create(const GpuParamBlockDesc& desc)
  75. {
  76. return HardwareBufferManager::instance().createGpuParamBlockBuffer(desc);
  77. }
  78. }