123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #include "renderer/CCPass.h"
- #include "renderer/CCGLProgramState.h"
- #include "renderer/CCGLProgram.h"
- #include "renderer/CCTexture2D.h"
- #include "renderer/ccGLStateCache.h"
- #include "renderer/CCTechnique.h"
- #include "renderer/CCMaterial.h"
- #include "renderer/CCVertexAttribBinding.h"
- #include "base/ccTypes.h"
- #include "2d/CCNode.h"
- #include <xxhash.h>
- NS_CC_BEGIN
- Pass* Pass::create(Technique* technique)
- {
- auto pass = new (std::nothrow) Pass();
- if (pass && pass->init(technique))
- {
- pass->autorelease();
- return pass;
- }
- CC_SAFE_DELETE(pass);
- return nullptr;
- }
- Pass* Pass::createWithGLProgramState(Technique* technique, GLProgramState* programState)
- {
- auto pass = new (std::nothrow) Pass();
- if (pass && pass->initWithGLProgramState(technique, programState))
- {
- pass->autorelease();
- return pass;
- }
- CC_SAFE_DELETE(pass);
- return nullptr;
- }
- bool Pass::init(Technique* technique)
- {
- _parent = technique;
- return true;
- }
- bool Pass::initWithGLProgramState(Technique* technique, GLProgramState *glProgramState)
- {
- _parent = technique;
- _glProgramState = glProgramState;
- CC_SAFE_RETAIN(_glProgramState);
- return true;
- }
- Pass::Pass()
- : _glProgramState(nullptr)
- , _vertexAttribBinding(nullptr)
- {
- }
- Pass::~Pass()
- {
- CC_SAFE_RELEASE(_glProgramState);
- CC_SAFE_RELEASE(_vertexAttribBinding);
- }
- Pass* Pass::clone() const
- {
- auto pass = new (std::nothrow) Pass();
- if (pass)
- {
- RenderState::cloneInto(pass);
- pass->_glProgramState = _glProgramState->clone();
- CC_SAFE_RETAIN(pass->_glProgramState);
- pass->_vertexAttribBinding = _vertexAttribBinding;
- CC_SAFE_RETAIN(pass->_vertexAttribBinding);
- pass->autorelease();
- }
- return pass;
- }
- GLProgramState* Pass::getGLProgramState() const
- {
- return _glProgramState;
- }
- void Pass::setGLProgramState(GLProgramState* glProgramState)
- {
- if ( _glProgramState != glProgramState) {
- CC_SAFE_RELEASE(_glProgramState);
- _glProgramState = glProgramState;
- CC_SAFE_RETAIN(_glProgramState);
- _hashDirty = true;
- }
- }
- uint32_t Pass::getHash() const
- {
- if (_hashDirty || _state->isDirty()) {
- uint32_t glProgram = (uint32_t)_glProgramState->getGLProgram()->getProgram();
- uint32_t textureid = _texture ? _texture->getName() : -1;
- uint32_t stateblockid = _state->getHash();
- _hash = glProgram ^ textureid ^ stateblockid;
- _hashDirty = false;
- }
- return _hash;
- }
- void Pass::bind(const Mat4& modelView)
- {
- bind(modelView, true);
- }
- void Pass::bind(const Mat4& modelView, bool bindAttributes)
- {
-
- if (bindAttributes && _vertexAttribBinding)
- _vertexAttribBinding->bind();
- auto glprogramstate = _glProgramState ? _glProgramState : getTarget()->getGLProgramState();
- glprogramstate->applyGLProgram(modelView);
- glprogramstate->applyUniforms();
-
- RenderState::bind(this);
- }
- Node* Pass::getTarget() const
- {
- CCASSERT(_parent && _parent->_parent, "Pass must have a Technique and Material");
- Material *material = static_cast<Material*>(_parent->_parent);
- return material->_target;
- }
- void Pass::unbind()
- {
- RenderState::StateBlock::restore(0);
- _vertexAttribBinding->unbind();
- }
- void Pass::setVertexAttribBinding(VertexAttribBinding* binding)
- {
- if (_vertexAttribBinding != binding)
- {
- CC_SAFE_RELEASE(_vertexAttribBinding);
- _vertexAttribBinding = binding;
- CC_SAFE_RETAIN(_vertexAttribBinding);
- }
- }
- VertexAttribBinding* Pass::getVertexAttributeBinding() const
- {
- return _vertexAttribBinding;
- }
- NS_CC_END
|