BsParamBlocks.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. T 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. bool transposeMatrices = RenderAPI::instance().getAPIInfo().isFlagSet(RenderAPIFeatureFlag::ColumnMajorMatrices);
  58. if (TransposePolicy<T>::transposeEnabled(transposeMatrices))
  59. return TransposePolicy<T>::transpose(value);
  60. else
  61. return value;
  62. }
  63. template class ParamBlockParam<float>;
  64. template class ParamBlockParam<int>;
  65. template class ParamBlockParam<Color>;
  66. template class ParamBlockParam<Vector2>;
  67. template class ParamBlockParam<Vector3>;
  68. template class ParamBlockParam<Vector4>;
  69. template class ParamBlockParam<Vector2I>;
  70. template class ParamBlockParam<Vector3I>;
  71. template class ParamBlockParam<Vector4I>;
  72. template class ParamBlockParam<Matrix2>;
  73. template class ParamBlockParam<Matrix2x3>;
  74. template class ParamBlockParam<Matrix2x4>;
  75. template class ParamBlockParam<Matrix3>;
  76. template class ParamBlockParam<Matrix3x2>;
  77. template class ParamBlockParam<Matrix3x4>;
  78. template class ParamBlockParam<Matrix4>;
  79. template class ParamBlockParam<Matrix4x2>;
  80. template class ParamBlockParam<Matrix4x3>;
  81. ParamBlock::~ParamBlock()
  82. {
  83. ParamBlockManager::unregisterBlock(this);
  84. }
  85. Vector<ParamBlock*> ParamBlockManager::sToInitialize;
  86. ParamBlockManager::ParamBlockManager()
  87. {
  88. for (auto& entry : sToInitialize)
  89. entry->initialize();
  90. sToInitialize.clear();
  91. }
  92. void ParamBlockManager::registerBlock(ParamBlock* paramBlock)
  93. {
  94. if (isStarted())
  95. paramBlock->initialize();
  96. else
  97. sToInitialize.push_back(paramBlock);
  98. }
  99. void ParamBlockManager::unregisterBlock(ParamBlock* paramBlock)
  100. {
  101. auto iterFind = std::find(sToInitialize.begin(), sToInitialize.end(), paramBlock);
  102. if (iterFind != sToInitialize.end())
  103. sToInitialize.erase(iterFind);
  104. }
  105. }}