StencilManager.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "StencilManager.hpp"
  21. #include "../Types.h"
  22. #include "../renderer/Technique.h"
  23. #include "../renderer/Pass.h"
  24. RENDERER_BEGIN
  25. static const std::string techStage = "opaque";
  26. StencilManager* StencilManager::_instance = nullptr;
  27. StencilManager::StencilManager ()
  28. : _stage(Stage::DISABLED)
  29. {
  30. }
  31. StencilManager::~StencilManager ()
  32. {
  33. }
  34. void StencilManager::reset ()
  35. {
  36. // reset stack and stage
  37. _maskStack.clear();
  38. _stage = Stage::DISABLED;
  39. }
  40. EffectVariant* StencilManager::handleEffect (EffectVariant* effect)
  41. {
  42. Vector<Pass*>& passes = (Vector<Pass*>&)effect->getPasses();
  43. if (_stage == Stage::DISABLED)
  44. {
  45. for (const auto& pass : passes)
  46. {
  47. if (pass->isStencilTest()) {
  48. pass->disableStencilTest();
  49. }
  50. }
  51. return effect;
  52. }
  53. auto size = _maskStack.size();
  54. if (size == 0 || size == size_t(-1))
  55. {
  56. return effect;
  57. }
  58. uint32_t ref;
  59. uint8_t stencilMask, writeMask;
  60. bool mask;
  61. StencilFunc func;
  62. StencilOp failOp = StencilOp::KEEP;
  63. const StencilOp& zFailOp = StencilOp::KEEP;
  64. const StencilOp& zPassOp = StencilOp::KEEP;
  65. if (_stage == Stage::ENABLED)
  66. {
  67. mask = _maskStack.back();
  68. func = StencilFunc::EQUAL;
  69. failOp = StencilOp::KEEP;
  70. ref = getStencilRef();
  71. stencilMask = ref;
  72. writeMask = getWriteMask();
  73. }
  74. else {
  75. if (_stage == Stage::CLEAR) {
  76. mask = _maskStack.back();
  77. func = StencilFunc::NEVER;
  78. failOp = mask ? StencilOp::REPLACE : StencilOp::ZERO;
  79. ref = getWriteMask();
  80. stencilMask = ref;
  81. writeMask = ref;
  82. }
  83. else if (_stage == Stage::ENTER_LEVEL) {
  84. mask = _maskStack.back();
  85. // Fill stencil mask
  86. func = StencilFunc::NEVER;
  87. failOp = mask ? StencilOp::ZERO : StencilOp::REPLACE;
  88. ref = getWriteMask();
  89. stencilMask = ref;
  90. writeMask = ref;
  91. }
  92. }
  93. for (const auto& pass : passes) {
  94. pass->setStencilFront(func, ref, stencilMask, failOp, zFailOp, zPassOp, writeMask);
  95. pass->setStencilBack(func, ref, stencilMask, failOp, zFailOp, zPassOp, writeMask);
  96. }
  97. return effect;
  98. }
  99. void StencilManager::pushMask (bool mask)
  100. {
  101. if (_maskStack.size() + 1 > _maxLevel)
  102. {
  103. cocos2d::log("StencilManager:pushMask _maxLevel:%d is out of range", _maxLevel);
  104. }
  105. _maskStack.push_back(mask);
  106. }
  107. void StencilManager::clear ()
  108. {
  109. _stage = Stage::CLEAR;
  110. }
  111. void StencilManager::enterLevel ()
  112. {
  113. _stage = Stage::ENTER_LEVEL;
  114. }
  115. void StencilManager::enableMask ()
  116. {
  117. _stage = Stage::ENABLED;
  118. }
  119. void StencilManager::exitMask ()
  120. {
  121. if (_maskStack.size() == 0) {
  122. cocos2d::log("StencilManager:exitMask _maskStack:%zu size is 0", _maskStack.size());
  123. }
  124. _maskStack.pop_back();
  125. if (_maskStack.size() == 0) {
  126. _stage = Stage::DISABLED;
  127. }
  128. else {
  129. _stage = Stage::ENABLED;
  130. }
  131. }
  132. uint8_t StencilManager::getWriteMask ()
  133. {
  134. return 0x01 << (_maskStack.size() - 1);
  135. }
  136. uint8_t StencilManager::getExitWriteMask ()
  137. {
  138. return 0x01 << _maskStack.size();
  139. }
  140. uint32_t StencilManager::getStencilRef ()
  141. {
  142. int32_t result = 0;
  143. size_t size = _maskStack.size();
  144. for (int i = 0; i < size; ++i) {
  145. result += (0x01 << i);
  146. }
  147. return result;
  148. }
  149. uint32_t StencilManager::getInvertedRef ()
  150. {
  151. int32_t result = 0;
  152. size_t size = _maskStack.size();
  153. for (int i = 0; i < size - 1; ++i)
  154. {
  155. result += (0x01 << i);
  156. }
  157. return result;
  158. }
  159. RENDERER_END