BsParamBlocks.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Renderer/BsParamBlocks.h"
  4. #include "RenderAPI/BsGpuParam.h"
  5. namespace bs { namespace ct
  6. {
  7. template<class T>
  8. ParamBlockParam<T>::ParamBlockParam(const GpuParamDataDesc& paramDesc)
  9. :mParamDesc(paramDesc)
  10. { }
  11. template<class T>
  12. void ParamBlockParam<T>::set(const SPtr<GpuParamBlockBuffer>& paramBlock, const T& value, UINT32 arrayIdx) const
  13. {
  14. #if BS_DEBUG_MODE
  15. if (arrayIdx >= mParamDesc.arraySize)
  16. {
  17. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  18. toString(mParamDesc.arraySize) + ". Requested size: " + toString(arrayIdx));
  19. }
  20. #endif
  21. UINT32 elementSizeBytes = mParamDesc.elementSize * sizeof(UINT32);
  22. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T)); // Truncate if it doesn't fit within parameter size
  23. bool transposeMatrices = RenderAPI::instance().getAPIInfo().isFlagSet(RenderAPIFeatureFlag::ColumnMajorMatrices);
  24. if (TransposePolicy<T>::transposeEnabled(transposeMatrices))
  25. {
  26. auto transposed = TransposePolicy<T>::transpose(value);
  27. paramBlock->write((mParamDesc.cpuMemOffset + arrayIdx * mParamDesc.arrayElementStride) * sizeof(UINT32),
  28. &transposed, sizeBytes);
  29. }
  30. else
  31. paramBlock->write((mParamDesc.cpuMemOffset + arrayIdx * mParamDesc.arrayElementStride) * sizeof(UINT32),
  32. &value, sizeBytes);
  33. // Set unused bytes to 0
  34. if (sizeBytes < elementSizeBytes)
  35. {
  36. UINT32 diffSize = elementSizeBytes - sizeBytes;
  37. paramBlock->zeroOut((mParamDesc.cpuMemOffset + arrayIdx * mParamDesc.arrayElementStride) * sizeof(UINT32) +
  38. sizeBytes, diffSize);
  39. }
  40. }
  41. template<class T>
  42. T ParamBlockParam<T>::get(const SPtr<GpuParamBlockBuffer>& paramBlock, UINT32 arrayIdx) const
  43. {
  44. #if BS_DEBUG_MODE
  45. if (arrayIdx >= mParamDesc.arraySize)
  46. {
  47. LOGERR("Array index out of range. Array size: " + toString(mParamDesc.arraySize) + ". Requested size: " +
  48. toString(arrayIdx));
  49. return T();
  50. }
  51. #endif
  52. UINT32 elementSizeBytes = mParamDesc.elementSize * sizeof(UINT32);
  53. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T));
  54. T value;
  55. paramBlock->read((mParamDesc.cpuMemOffset + arrayIdx * mParamDesc.arrayElementStride) * sizeof(UINT32), &value,
  56. sizeBytes);
  57. return value;
  58. }
  59. template class ParamBlockParam<float>;
  60. template class ParamBlockParam<int>;
  61. template class ParamBlockParam<Color>;
  62. template class ParamBlockParam<Vector2>;
  63. template class ParamBlockParam<Vector3>;
  64. template class ParamBlockParam<Vector4>;
  65. template class ParamBlockParam<Vector2I>;
  66. template class ParamBlockParam<Vector3I>;
  67. template class ParamBlockParam<Vector4I>;
  68. template class ParamBlockParam<Matrix2>;
  69. template class ParamBlockParam<Matrix2x3>;
  70. template class ParamBlockParam<Matrix2x4>;
  71. template class ParamBlockParam<Matrix3>;
  72. template class ParamBlockParam<Matrix3x2>;
  73. template class ParamBlockParam<Matrix3x4>;
  74. template class ParamBlockParam<Matrix4>;
  75. template class ParamBlockParam<Matrix4x2>;
  76. template class ParamBlockParam<Matrix4x3>;
  77. ParamBlock::~ParamBlock()
  78. {
  79. ParamBlockManager::unregisterBlock(this);
  80. }
  81. Vector<ParamBlock*> ParamBlockManager::sToInitialize;
  82. ParamBlockManager::ParamBlockManager()
  83. {
  84. for (auto& entry : sToInitialize)
  85. entry->initialize();
  86. sToInitialize.clear();
  87. }
  88. void ParamBlockManager::registerBlock(ParamBlock* paramBlock)
  89. {
  90. if (isStarted())
  91. paramBlock->initialize();
  92. else
  93. sToInitialize.push_back(paramBlock);
  94. }
  95. void ParamBlockManager::unregisterBlock(ParamBlock* paramBlock)
  96. {
  97. auto iterFind = std::find(sToInitialize.begin(), sToInitialize.end(), paramBlock);
  98. if (iterFind != sToInitialize.end())
  99. sToInitialize.erase(iterFind);
  100. }
  101. }}