BsGpuParamBlock.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsGpuParamBlock.h"
  5. #include "BsGpuParamDesc.h"
  6. #include "BsGpuParamBlockBuffer.h"
  7. #include "BsHardwareBufferManager.h"
  8. #include "BsException.h"
  9. namespace BansheeEngine
  10. {
  11. GpuParamBlock::GpuParamBlock(UINT32 size)
  12. :mDirty(true), mData(nullptr), mSize(size)
  13. {
  14. if (mSize > 0)
  15. mData = (UINT8*)bs_alloc<ScratchAlloc>(mSize);
  16. memset(mData, 0, mSize);
  17. }
  18. GpuParamBlock::GpuParamBlock(GpuParamBlock* otherBlock)
  19. {
  20. mSize = otherBlock->mSize;
  21. if (mSize > 0)
  22. mData = (UINT8*)bs_alloc<ScratchAlloc>(mSize);
  23. else
  24. mData = nullptr;
  25. write(0, otherBlock->getData(), otherBlock->getSize());
  26. mDirty = otherBlock->mDirty;
  27. }
  28. GpuParamBlock::~GpuParamBlock()
  29. {
  30. if(mData != nullptr)
  31. bs_free<ScratchAlloc>(mData);
  32. }
  33. void GpuParamBlock::write(UINT32 offset, const void* data, UINT32 size)
  34. {
  35. #if BS_DEBUG_MODE
  36. if(offset < 0 || (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(mData + offset, data, size);
  44. mDirty = true;
  45. }
  46. void GpuParamBlock::read(UINT32 offset, void* data, UINT32 size)
  47. {
  48. #if BS_DEBUG_MODE
  49. if(offset < 0 || (offset + size) > mSize)
  50. {
  51. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  52. "Available range: 0 .. " + toString(mSize) + ". " \
  53. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  54. }
  55. #endif
  56. memcpy(data, mData + offset, size);
  57. }
  58. void GpuParamBlock::zeroOut(UINT32 offset, UINT32 size)
  59. {
  60. #if BS_DEBUG_MODE
  61. if(offset < 0 || (offset + size) > mSize)
  62. {
  63. BS_EXCEPT(InvalidParametersException, "Wanted range is out of buffer bounds. " \
  64. "Available range: 0 .. " + toString(mSize) + ". " \
  65. "Wanted range: " + toString(offset) + " .. " + toString(offset + size) + ".");
  66. }
  67. #endif
  68. memset(mData + offset, 0, size);
  69. mDirty = true;
  70. }
  71. void GpuParamBlock::uploadToBuffer(const GpuParamBlockBufferPtr& buffer)
  72. {
  73. buffer->writeData(mData);
  74. mDirty = false;
  75. }
  76. }