CmBindableGpuParams.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "CmBindableGpuParams.h"
  2. #include "CmGpuParams.h"
  3. #include "CmGpuParamDesc.h"
  4. #include "CmBindableGpuParamBlock.h"
  5. #include "CmGpuParamBlockBuffer.h"
  6. #include "CmDebug.h"
  7. namespace CamelotFramework
  8. {
  9. BindableGpuParams::BindableGpuParams(const GpuParamDesc& sourceParamDesc)
  10. :mOwnsData(true), mParamDesc(sourceParamDesc), mData(nullptr), mNumParamBlocks(0),
  11. mNumTextures(0), mNumSamplerStates(0),mParamBlocks(nullptr), mParamBlockBuffers(nullptr), mTextures(nullptr), mSamplerStates(nullptr)
  12. {
  13. // Actual allocation of all data happens in parent GpuParams
  14. }
  15. BindableGpuParams::BindableGpuParams(const BindableGpuParams& source)
  16. :mParamDesc(source.mParamDesc)
  17. {
  18. mOwnsData = true;
  19. source.mOwnsData = false;
  20. mData = source.mData;
  21. mNumParamBlocks = source.mNumParamBlocks;
  22. mNumTextures = source.mNumTextures;
  23. mNumSamplerStates = source.mNumSamplerStates;
  24. mParamBlocks = source.mParamBlocks;
  25. mParamBlockBuffers = source.mParamBlockBuffers;
  26. mTextures = source.mTextures;
  27. mSamplerStates = source.mSamplerStates;
  28. }
  29. BindableGpuParams::~BindableGpuParams()
  30. {
  31. if(mOwnsData && mData != nullptr)
  32. {
  33. cm_free(mData);
  34. // TODO - Dealloc using stack
  35. }
  36. }
  37. // TODO - Forbid copying but allow move semantics
  38. GpuParamBlockBufferPtr BindableGpuParams::getParamBlockBuffer(UINT32 slot) const
  39. {
  40. if(slot < 0 || slot >= mNumParamBlocks)
  41. {
  42. CM_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  43. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  44. }
  45. return mParamBlockBuffers[slot];
  46. }
  47. GpuParamBlockBufferPtr BindableGpuParams::getParamBlockBuffer(const String& name) const
  48. {
  49. auto iterFind = mParamDesc.paramBlocks.find(name);
  50. if(iterFind == mParamDesc.paramBlocks.end())
  51. {
  52. LOGWRN("Cannot find parameter block with the name: " + name);
  53. return nullptr;
  54. }
  55. return mParamBlockBuffers[iterFind->second.slot];
  56. }
  57. HTexture BindableGpuParams::getTexture(UINT32 slot)
  58. {
  59. if(slot < 0 || slot >= mNumTextures)
  60. {
  61. CM_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  62. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  63. }
  64. return mTextures[slot];
  65. }
  66. HSamplerState BindableGpuParams::getSamplerState(UINT32 slot)
  67. {
  68. if(slot < 0 || slot >= mNumSamplerStates)
  69. {
  70. CM_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  71. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  72. }
  73. return mSamplerStates[slot];
  74. }
  75. void BindableGpuParams::updateHardwareBuffers()
  76. {
  77. for(size_t i = 0; i < mNumParamBlocks; i++)
  78. {
  79. if(mParamBlocks[i] != nullptr && mParamBlockBuffers[i] != nullptr)
  80. {
  81. if(mParamBlocks[i]->isDirty())
  82. mParamBlocks[i]->uploadToBuffer(mParamBlockBuffers[i]);
  83. }
  84. }
  85. }
  86. }