CCPass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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. Ideas taken from:
  21. - GamePlay3D: http://gameplay3d.org/
  22. - OGRE3D: http://www.ogre3d.org/
  23. - Qt3D: http://qt-project.org/
  24. ****************************************************************************/
  25. #include "renderer/CCPass.h"
  26. #include "renderer/CCGLProgramState.h"
  27. #include "renderer/CCGLProgram.h"
  28. #include "renderer/CCTexture2D.h"
  29. #include "renderer/ccGLStateCache.h"
  30. #include "renderer/CCTechnique.h"
  31. #include "renderer/CCMaterial.h"
  32. #include "renderer/CCVertexAttribBinding.h"
  33. #include "base/ccTypes.h"
  34. #include "2d/CCNode.h"
  35. #include <xxhash.h>
  36. NS_CC_BEGIN
  37. Pass* Pass::create(Technique* technique)
  38. {
  39. auto pass = new (std::nothrow) Pass();
  40. if (pass && pass->init(technique))
  41. {
  42. pass->autorelease();
  43. return pass;
  44. }
  45. CC_SAFE_DELETE(pass);
  46. return nullptr;
  47. }
  48. Pass* Pass::createWithGLProgramState(Technique* technique, GLProgramState* programState)
  49. {
  50. auto pass = new (std::nothrow) Pass();
  51. if (pass && pass->initWithGLProgramState(technique, programState))
  52. {
  53. pass->autorelease();
  54. return pass;
  55. }
  56. CC_SAFE_DELETE(pass);
  57. return nullptr;
  58. }
  59. bool Pass::init(Technique* technique)
  60. {
  61. _parent = technique;
  62. return true;
  63. }
  64. bool Pass::initWithGLProgramState(Technique* technique, GLProgramState *glProgramState)
  65. {
  66. _parent = technique;
  67. _glProgramState = glProgramState;
  68. CC_SAFE_RETAIN(_glProgramState);
  69. return true;
  70. }
  71. Pass::Pass()
  72. : _glProgramState(nullptr)
  73. , _vertexAttribBinding(nullptr)
  74. {
  75. }
  76. Pass::~Pass()
  77. {
  78. CC_SAFE_RELEASE(_glProgramState);
  79. CC_SAFE_RELEASE(_vertexAttribBinding);
  80. }
  81. Pass* Pass::clone() const
  82. {
  83. auto pass = new (std::nothrow) Pass();
  84. if (pass)
  85. {
  86. RenderState::cloneInto(pass);
  87. pass->_glProgramState = _glProgramState->clone();
  88. CC_SAFE_RETAIN(pass->_glProgramState);
  89. pass->_vertexAttribBinding = _vertexAttribBinding;
  90. CC_SAFE_RETAIN(pass->_vertexAttribBinding);
  91. pass->autorelease();
  92. }
  93. return pass;
  94. }
  95. GLProgramState* Pass::getGLProgramState() const
  96. {
  97. return _glProgramState;
  98. }
  99. void Pass::setGLProgramState(GLProgramState* glProgramState)
  100. {
  101. if ( _glProgramState != glProgramState) {
  102. CC_SAFE_RELEASE(_glProgramState);
  103. _glProgramState = glProgramState;
  104. CC_SAFE_RETAIN(_glProgramState);
  105. _hashDirty = true;
  106. }
  107. }
  108. uint32_t Pass::getHash() const
  109. {
  110. if (_hashDirty || _state->isDirty()) {
  111. uint32_t glProgram = (uint32_t)_glProgramState->getGLProgram()->getProgram();
  112. uint32_t textureid = _texture ? _texture->getName() : -1;
  113. uint32_t stateblockid = _state->getHash();
  114. _hash = glProgram ^ textureid ^ stateblockid;
  115. // _hash = XXH32((const void*)intArray, sizeof(intArray), 0);
  116. _hashDirty = false;
  117. }
  118. return _hash;
  119. }
  120. void Pass::bind(const Mat4& modelView)
  121. {
  122. bind(modelView, true);
  123. }
  124. void Pass::bind(const Mat4& modelView, bool bindAttributes)
  125. {
  126. // vertex attribs
  127. if (bindAttributes && _vertexAttribBinding)
  128. _vertexAttribBinding->bind();
  129. auto glprogramstate = _glProgramState ? _glProgramState : getTarget()->getGLProgramState();
  130. glprogramstate->applyGLProgram(modelView);
  131. glprogramstate->applyUniforms();
  132. //set render state
  133. RenderState::bind(this);
  134. }
  135. Node* Pass::getTarget() const
  136. {
  137. CCASSERT(_parent && _parent->_parent, "Pass must have a Technique and Material");
  138. Material *material = static_cast<Material*>(_parent->_parent);
  139. return material->_target;
  140. }
  141. void Pass::unbind()
  142. {
  143. RenderState::StateBlock::restore(0);
  144. _vertexAttribBinding->unbind();
  145. }
  146. void Pass::setVertexAttribBinding(VertexAttribBinding* binding)
  147. {
  148. if (_vertexAttribBinding != binding)
  149. {
  150. CC_SAFE_RELEASE(_vertexAttribBinding);
  151. _vertexAttribBinding = binding;
  152. CC_SAFE_RETAIN(_vertexAttribBinding);
  153. }
  154. }
  155. VertexAttribBinding* Pass::getVertexAttributeBinding() const
  156. {
  157. return _vertexAttribBinding;
  158. }
  159. NS_CC_END