CCStencilStateManager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/CCStencilStateManager.h"
  23. #include "base/CCDirector.h"
  24. #include "renderer/CCGLProgramCache.h"
  25. #include "renderer/ccGLStateCache.h"
  26. #include "renderer/CCRenderer.h"
  27. #include "renderer/CCRenderState.h"
  28. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
  29. #define CC_CLIPPING_NODE_OPENGLES 0
  30. #else
  31. #define CC_CLIPPING_NODE_OPENGLES 1
  32. #endif
  33. NS_CC_BEGIN
  34. GLint StencilStateManager::s_layer = -1;
  35. StencilStateManager::StencilStateManager()
  36. : _alphaThreshold(1.0f)
  37. , _inverted(false)
  38. , _currentStencilEnabled(GL_FALSE)
  39. , _currentStencilWriteMask(~0)
  40. , _currentStencilFunc(GL_ALWAYS)
  41. , _currentStencilRef(0)
  42. , _currentStencilValueMask(~0)
  43. , _currentStencilFail(GL_KEEP)
  44. , _currentStencilPassDepthFail(GL_KEEP)
  45. , _currentStencilPassDepthPass(GL_KEEP)
  46. , _currentDepthWriteMask(GL_TRUE)
  47. , _currentAlphaTestEnabled(GL_FALSE)
  48. , _currentAlphaTestFunc(GL_ALWAYS)
  49. , _currentAlphaTestRef(1)
  50. {
  51. }
  52. void StencilStateManager::drawFullScreenQuadClearStencil()
  53. {
  54. Director* director = Director::getInstance();
  55. CCASSERT(nullptr != director, "Director is null when setting matrix stack");
  56. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  57. director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  58. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  59. director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  60. Vec2 vertices[] = {
  61. Vec2(-1.0f, -1.0f),
  62. Vec2(1.0f, -1.0f),
  63. Vec2(1.0f, 1.0f),
  64. Vec2(-1.0f, 1.0f)
  65. };
  66. auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
  67. int colorLocation = glProgram->getUniformLocation("u_color");
  68. CHECK_GL_ERROR_DEBUG();
  69. Color4F color(1, 1, 1, 1);
  70. glProgram->use();
  71. glProgram->setUniformsForBuiltins();
  72. glProgram->setUniformLocationWith4fv(colorLocation, (GLfloat*) &color.r, 1);
  73. glBindBuffer(GL_ARRAY_BUFFER, 0);
  74. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
  75. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  76. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  77. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
  78. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  79. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  80. }
  81. void StencilStateManager::setAlphaThreshold(GLfloat alphaThreshold)
  82. {
  83. _alphaThreshold = alphaThreshold;
  84. }
  85. GLfloat StencilStateManager::getAlphaThreshold()const
  86. {
  87. return _alphaThreshold;
  88. }
  89. void StencilStateManager::setInverted(bool inverted)
  90. {
  91. _inverted = inverted;
  92. }
  93. bool StencilStateManager::isInverted()const
  94. {
  95. return _inverted;
  96. }
  97. void StencilStateManager::onBeforeVisit()
  98. {
  99. ///////////////////////////////////
  100. // INIT
  101. // increment the current layer
  102. s_layer++;
  103. // mask of the current layer (ie: for layer 3: 00000100)
  104. GLint mask_layer = 0x1 << s_layer;
  105. // mask of all layers less than the current (ie: for layer 3: 00000011)
  106. GLint mask_layer_l = mask_layer - 1;
  107. // mask of all layers less than or equal to the current (ie: for layer 3: 00000111)
  108. _mask_layer_le = mask_layer | mask_layer_l;
  109. // manually save the stencil state
  110. _currentStencilEnabled = glIsEnabled(GL_STENCIL_TEST);
  111. glGetIntegerv(GL_STENCIL_WRITEMASK, (GLint *)&_currentStencilWriteMask);
  112. glGetIntegerv(GL_STENCIL_FUNC, (GLint *)&_currentStencilFunc);
  113. glGetIntegerv(GL_STENCIL_REF, &_currentStencilRef);
  114. glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&_currentStencilValueMask);
  115. glGetIntegerv(GL_STENCIL_FAIL, (GLint *)&_currentStencilFail);
  116. glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, (GLint *)&_currentStencilPassDepthFail);
  117. glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, (GLint *)&_currentStencilPassDepthPass);
  118. // enable stencil use
  119. glEnable(GL_STENCIL_TEST);
  120. // RenderState::StateBlock::_defaultState->setStencilTest(true);
  121. // check for OpenGL error while enabling stencil test
  122. CHECK_GL_ERROR_DEBUG();
  123. // all bits on the stencil buffer are readonly, except the current layer bit,
  124. // this means that operation like glClear or glStencilOp will be masked with this value
  125. glStencilMask(mask_layer);
  126. // RenderState::StateBlock::_defaultState->setStencilWrite(mask_layer);
  127. // manually save the depth test state
  128. glGetBooleanv(GL_DEPTH_WRITEMASK, &_currentDepthWriteMask);
  129. // disable depth test while drawing the stencil
  130. //glDisable(GL_DEPTH_TEST);
  131. // disable update to the depth buffer while drawing the stencil,
  132. // as the stencil is not meant to be rendered in the real scene,
  133. // it should never prevent something else to be drawn,
  134. // only disabling depth buffer update should do
  135. glDepthMask(GL_FALSE);
  136. RenderState::StateBlock::_defaultState->setDepthWrite(false);
  137. ///////////////////////////////////
  138. // CLEAR STENCIL BUFFER
  139. // manually clear the stencil buffer by drawing a fullscreen rectangle on it
  140. // setup the stencil test func like this:
  141. // for each pixel in the fullscreen rectangle
  142. // never draw it into the frame buffer
  143. // if not in inverted mode: set the current layer value to 0 in the stencil buffer
  144. // if in inverted mode: set the current layer value to 1 in the stencil buffer
  145. glStencilFunc(GL_NEVER, mask_layer, mask_layer);
  146. glStencilOp(!_inverted ? GL_ZERO : GL_REPLACE, GL_KEEP, GL_KEEP);
  147. // draw a fullscreen solid rectangle to clear the stencil buffer
  148. //ccDrawSolidRect(Vec2::ZERO, ccpFromSize([[Director sharedDirector] winSize]), Color4F(1, 1, 1, 1));
  149. drawFullScreenQuadClearStencil();
  150. ///////////////////////////////////
  151. // DRAW CLIPPING STENCIL
  152. // setup the stencil test func like this:
  153. // for each pixel in the stencil node
  154. // never draw it into the frame buffer
  155. // if not in inverted mode: set the current layer value to 1 in the stencil buffer
  156. // if in inverted mode: set the current layer value to 0 in the stencil buffer
  157. glStencilFunc(GL_NEVER, mask_layer, mask_layer);
  158. // RenderState::StateBlock::_defaultState->setStencilFunction(RenderState::STENCIL_NEVER, mask_layer, mask_layer);
  159. glStencilOp(!_inverted ? GL_REPLACE : GL_ZERO, GL_KEEP, GL_KEEP);
  160. // RenderState::StateBlock::_defaultState->setStencilOperation(
  161. // !_inverted ? RenderState::STENCIL_OP_REPLACE : RenderState::STENCIL_OP_ZERO,
  162. // RenderState::STENCIL_OP_KEEP,
  163. // RenderState::STENCIL_OP_KEEP);
  164. // enable alpha test only if the alpha threshold < 1,
  165. // indeed if alpha threshold == 1, every pixel will be drawn anyways
  166. if (_alphaThreshold < 1) {
  167. #if !CC_CLIPPING_NODE_OPENGLES
  168. // manually save the alpha test state
  169. _currentAlphaTestEnabled = glIsEnabled(GL_ALPHA_TEST);
  170. glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint *)&_currentAlphaTestFunc);
  171. glGetFloatv(GL_ALPHA_TEST_REF, &_currentAlphaTestRef);
  172. // enable alpha testing
  173. glEnable(GL_ALPHA_TEST);
  174. // check for OpenGL error while enabling alpha test
  175. CHECK_GL_ERROR_DEBUG();
  176. // pixel will be drawn only if greater than an alpha threshold
  177. glAlphaFunc(GL_GREATER, _alphaThreshold);
  178. #endif
  179. }
  180. //Draw _stencil
  181. }
  182. void StencilStateManager::onAfterDrawStencil()
  183. {
  184. // restore alpha test state
  185. if (_alphaThreshold < 1)
  186. {
  187. #if CC_CLIPPING_NODE_OPENGLES
  188. // FIXME: we need to find a way to restore the shaders of the stencil node and its children
  189. #else
  190. // manually restore the alpha test state
  191. glAlphaFunc(_currentAlphaTestFunc, _currentAlphaTestRef);
  192. if (!_currentAlphaTestEnabled)
  193. {
  194. glDisable(GL_ALPHA_TEST);
  195. }
  196. #endif
  197. }
  198. // restore the depth test state
  199. glDepthMask(_currentDepthWriteMask);
  200. RenderState::StateBlock::_defaultState->setDepthWrite(_currentDepthWriteMask != 0);
  201. //if (currentDepthTestEnabled) {
  202. // glEnable(GL_DEPTH_TEST);
  203. //}
  204. ///////////////////////////////////
  205. // DRAW CONTENT
  206. // setup the stencil test function like this:
  207. // for each pixel of this node and its children
  208. // if all layers less than or equals to the current are set to 1 in the stencil buffer
  209. // draw the pixel and keep the current layer in the stencil buffer
  210. // else
  211. // do not draw the pixel but keep the current layer in the stencil buffer
  212. glStencilFunc(GL_EQUAL, _mask_layer_le, _mask_layer_le);
  213. // RenderState::StateBlock::_defaultState->setStencilFunction(RenderState::STENCIL_EQUAL, _mask_layer_le, _mask_layer_le);
  214. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  215. // RenderState::StateBlock::_defaultState->setStencilOperation(RenderState::STENCIL_OP_KEEP, RenderState::STENCIL_OP_KEEP, RenderState::STENCIL_OP_KEEP);
  216. // draw (according to the stencil test function) this node and its children
  217. }
  218. void StencilStateManager::onAfterVisit()
  219. {
  220. ///////////////////////////////////
  221. // CLEANUP
  222. // manually restore the stencil state
  223. glStencilFunc(_currentStencilFunc, _currentStencilRef, _currentStencilValueMask);
  224. // RenderState::StateBlock::_defaultState->setStencilFunction((RenderState::StencilFunction)_currentStencilFunc, _currentStencilRef, _currentStencilValueMask);
  225. glStencilOp(_currentStencilFail, _currentStencilPassDepthFail, _currentStencilPassDepthPass);
  226. // RenderState::StateBlock::_defaultState->setStencilOperation((RenderState::StencilOperation)_currentStencilFail,
  227. // (RenderState::StencilOperation)_currentStencilPassDepthFail,
  228. // (RenderState::StencilOperation)_currentStencilPassDepthPass);
  229. glStencilMask(_currentStencilWriteMask);
  230. if (!_currentStencilEnabled)
  231. {
  232. glDisable(GL_STENCIL_TEST);
  233. // RenderState::StateBlock::_defaultState->setStencilTest(false);
  234. }
  235. // we are done using this layer, decrement
  236. s_layer--;
  237. }
  238. NS_CC_END