CustomAssembler.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "CustomAssembler.hpp"
  21. #include "../NodeProxy.hpp"
  22. #include "../ModelBatcher.hpp"
  23. #include "../../renderer/Scene.h"
  24. RENDERER_BEGIN
  25. CustomAssembler::CustomAssembler()
  26. {
  27. }
  28. CustomAssembler::~CustomAssembler()
  29. {
  30. for (std::size_t i = 0, n = _iaPool.size(); i < n; i++)
  31. {
  32. auto ia = _iaPool[i];
  33. delete ia;
  34. }
  35. _iaPool.clear();
  36. }
  37. void CustomAssembler::handle(NodeProxy *node, ModelBatcher* batcher, Scene* scene)
  38. {
  39. batcher->commitIA(node, this, node->getCullingMask());
  40. }
  41. void CustomAssembler::reset()
  42. {
  43. _iaCount = 0;
  44. for (auto it = _iaPool.begin(); it != _iaPool.end(); it++)
  45. {
  46. (*it)->clear();
  47. }
  48. }
  49. void CustomAssembler::updateIARange(std::size_t index, int start, int count)
  50. {
  51. auto ia = adjustIA(index);
  52. if (!ia) return;
  53. ia->setCount(count);
  54. ia->setStart(start);
  55. }
  56. void CustomAssembler::updateIABuffer(std::size_t index, VertexBuffer* vb, IndexBuffer* ib)
  57. {
  58. auto ia = adjustIA(index);
  59. if (!ia) return;
  60. ia->setVertexBuffer(vb);
  61. ia->setIndexBuffer(ib);
  62. }
  63. InputAssembler* CustomAssembler::adjustIA(std::size_t index)
  64. {
  65. auto size = _iaPool.size();
  66. InputAssembler* ia = nullptr;
  67. if (index == size)
  68. {
  69. ia = new InputAssembler();
  70. _iaPool.push_back(ia);
  71. }
  72. else if (index < size)
  73. {
  74. ia = _iaPool[index];
  75. }
  76. else
  77. {
  78. cocos2d::log("CustomAssembler:updateIA index:%zu is out of range", index);
  79. return nullptr;
  80. }
  81. auto newIACount = index + 1;
  82. if (_iaCount < newIACount)
  83. {
  84. _iaCount = newIACount;
  85. }
  86. return ia;
  87. }
  88. InputAssembler* CustomAssembler::getIA(std::size_t index) const
  89. {
  90. if (index >= _iaCount)
  91. {
  92. return nullptr;
  93. }
  94. return _iaPool[index];
  95. }
  96. void CustomAssembler::updateEffect(std::size_t index, EffectVariant* effect)
  97. {
  98. auto size = _effects.size();
  99. if (index == size)
  100. {
  101. _effects.pushBack(effect);
  102. return;
  103. }
  104. else if (index < size)
  105. {
  106. _effects.replace(index, effect);
  107. return;
  108. }
  109. cocos2d::log("CustomAssembler:updateEffect index:%zu out of range", index);
  110. }
  111. RENDERER_END