CCStencilStateManager.cpp 12 KB

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