RenderFlow.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #pragma once
  21. #include "../Macro.h"
  22. #include "NodeProxy.hpp"
  23. #include "ModelBatcher.hpp"
  24. #include "../renderer/Scene.h"
  25. #include "../renderer/ForwardRenderer.h"
  26. #include "../gfx/DeviceGraphics.h"
  27. #include "ParallelTask.hpp"
  28. RENDERER_BEGIN
  29. /**
  30. * @addtogroup scene
  31. * @{
  32. */
  33. /**
  34. * @brief This class is responsible for the rendering process.\n
  35. * It visits the node tree, let nodes commit their render handles, starts the device level rendering process with ForwardRenderer.\n
  36. * JS API: renderer.RenderFlow
  37. @code
  38. // You actually shouldn't create RenderFlow by yourself, you can
  39. let renderFlow = cc.RenderFlow._nativeFlow;
  40. @endcode
  41. */
  42. #define NODE_LEVEL_INVALID 0xffffffff
  43. class RenderFlow
  44. {
  45. public:
  46. enum RenderFlowFlag {
  47. // sync js render flag
  48. DONOTHING = 1 << 0,
  49. BREAK_FLOW = 1 << 1,
  50. LOCAL_TRANSFORM = 1 << 2,
  51. WORLD_TRANSFORM = 1 << 3,
  52. TRANSFORM = LOCAL_TRANSFORM | WORLD_TRANSFORM,
  53. UPDATE_RENDER_DATA = 1 << 4,
  54. OPACITY = 1 << 5,
  55. COLOR = 1 << 6,
  56. RENDER = 1 << 7,
  57. CHILDREN = 1 << 8,
  58. POST_RENDER = 1 << 9,
  59. FINAL = 1 << 10,
  60. PRE_CALCULATE_VERTICES = 1 << 28,
  61. // native render flag
  62. REORDER_CHILDREN = 1 << 29,
  63. // world matrix changed
  64. WORLD_TRANSFORM_CHANGED = 1 << 30,
  65. // cascade opacity changed
  66. NODE_OPACITY_CHANGED = 1 << 31,
  67. };
  68. enum ParallelStage {
  69. NONE = 1 << 0,
  70. LOCAL_MAT = 1 << 1,
  71. WORLD_MAT = 1 << 2,
  72. CALC_VERTICES = 1 << 3,
  73. };
  74. struct LevelInfo{
  75. uint32_t* dirty = nullptr;
  76. uint32_t* parentDirty = nullptr;
  77. cocos2d::Mat4* parentWorldMat = nullptr;
  78. uint8_t* parentRealOpacity = nullptr;
  79. cocos2d::Mat4* localMat = nullptr;
  80. cocos2d::Mat4* worldMat = nullptr;
  81. uint8_t* opacity = nullptr;
  82. uint8_t* realOpacity = nullptr;
  83. };
  84. static RenderFlow *getInstance()
  85. {
  86. return _instance;
  87. }
  88. /*
  89. * @brief The constructor.
  90. * @param[in] device
  91. * @param[in] scene
  92. * @param[in] forward
  93. */
  94. RenderFlow(DeviceGraphics* device, Scene* scene, ForwardRenderer* forward);
  95. /*
  96. * @brief The destructor.
  97. */
  98. ~RenderFlow();
  99. /*
  100. * @brief Gets the ModelBatcher which is responsible for collecting render Models.
  101. */
  102. ModelBatcher* getModelBatcher() const { return _batcher; };
  103. /*
  104. * @brief Gets the DeviceGraphics using by the current RenderFlow.
  105. */
  106. DeviceGraphics* getDevice() const { return _device; };
  107. /*
  108. * @brief Gets the render Scene which manages all render Models.
  109. */
  110. Scene* getRenderScene() const { return _scene; };
  111. /**
  112. * @brief Render the scene specified by its root node.
  113. * @param[in] scene The root node.
  114. */
  115. void render(NodeProxy* scene, float deltaTime, Camera *camera = nullptr);
  116. /**
  117. * @brief Visit a node tree.
  118. */
  119. void visit(NodeProxy* rootNode);
  120. /**
  121. * @brief Calculate local matrix.
  122. * @param[in] tid It must rather than -1 if enable multiple thread.
  123. */
  124. void calculateLocalMatrix(int tid = -1);
  125. /**
  126. * @brief Calculate world matrix.
  127. */
  128. void calculateWorldMatrix();
  129. /**
  130. * @brief Calculate world matrix by level.
  131. * @param[in] tid Thread id.
  132. * @param[level] level Node level.
  133. */
  134. void calculateLevelWorldMatrix(int tid = -1, int stage = -1);
  135. /**
  136. * @brief remove node level
  137. */
  138. void removeNodeLevel(std::size_t level, cocos2d::Mat4* worldMat);
  139. /**
  140. * @brief insert node level
  141. */
  142. void insertNodeLevel(std::size_t level, const LevelInfo& levelInfo);
  143. private:
  144. static RenderFlow *_instance;
  145. ModelBatcher* _batcher = nullptr;
  146. Scene* _scene = nullptr;
  147. DeviceGraphics* _device = nullptr;
  148. ForwardRenderer* _forward = nullptr;
  149. std::size_t _curLevel = 0;
  150. std::vector<std::vector<LevelInfo>> _levelInfoArr;
  151. ParallelStage _parallelStage = ParallelStage::NONE;
  152. ParallelTask* _paralleTask = nullptr;
  153. };
  154. // end of scene group
  155. /// @}
  156. RENDERER_END