Performance improvements

`Director::getMatrix()` returns `const Mat4`
This commit is contained in:
Ricardo Quesada 2014-11-04 16:46:14 -08:00
parent a95f689842
commit 3a34d6eb9d
7 changed files with 38 additions and 56 deletions

View File

@ -553,7 +553,7 @@ void RenderTexture::onBegin()
director->setProjection(director->getProjection()); director->setProjection(director->getProjection());
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
Mat4 modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); auto modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection; modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection;
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,modifiedProjection); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,modifiedProjection);
#endif #endif
@ -572,7 +572,7 @@ void RenderTexture::onBegin()
else else
{ {
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
Mat4 modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); auto modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection; modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection;
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, modifiedProjection); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, modifiedProjection);
#endif #endif

View File

@ -540,15 +540,15 @@ void Director::multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat)
void Director::pushMatrix(MATRIX_STACK_TYPE type) void Director::pushMatrix(MATRIX_STACK_TYPE type)
{ {
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type) if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
{ {
_modelViewMatrixStack.push(_modelViewMatrixStack.top()); _modelViewMatrixStack.push(_modelViewMatrixStack.top());
} }
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type) else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
{ {
_projectionMatrixStack.push(_projectionMatrixStack.top()); _projectionMatrixStack.push(_projectionMatrixStack.top());
} }
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type) else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
{ {
_textureMatrixStack.push(_textureMatrixStack.top()); _textureMatrixStack.push(_textureMatrixStack.top());
} }
@ -558,36 +558,23 @@ void Director::pushMatrix(MATRIX_STACK_TYPE type)
} }
} }
Mat4 Director::getMatrix(MATRIX_STACK_TYPE type) const Mat4& Director::getMatrix(MATRIX_STACK_TYPE type)
{ {
Mat4 result; if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW)
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{ {
result = _modelViewMatrixStack.top(); return _modelViewMatrixStack.top();
} }
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type) else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION)
{ {
result = _projectionMatrixStack.top(); return _projectionMatrixStack.top();
} }
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type) else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE)
{ {
result = _textureMatrixStack.top(); return _textureMatrixStack.top();
} }
else
{ CCASSERT(false, "unknow matrix stack type, will return modelview matrix instead");
CCASSERT(false, "unknow matrix stack type, will return modelview matrix instead"); return _modelViewMatrixStack.top();
result = _modelViewMatrixStack.top();
}
// float diffResult(0);
// for (int index = 0; index <16; ++index)
// {
// diffResult += abs(result2.mat[index] - result.mat[index]);
// }
// if(diffResult > 1e-30)
// {
// CCASSERT(false, "Error in director matrix stack");
// }
return result;
} }
void Director::setProjection(Projection projection) void Director::setProjection(Projection projection)
@ -717,17 +704,15 @@ static void GLToClipTransform(Mat4 *transformOut)
Director* director = Director::getInstance(); Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack"); CCASSERT(nullptr != director, "Director is null when seting matrix stack");
Mat4 projection; auto projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8
//if needed, we need to undo the rotation for Landscape orientation in order to get the correct positions //if needed, we need to undo the rotation for Landscape orientation in order to get the correct positions
projection = Director::getInstance()->getOpenGLView()->getReverseOrientationMatrix() * projection; projection = Director::getInstance()->getOpenGLView()->getReverseOrientationMatrix() * projection;
#endif #endif
Mat4 modelview; auto modelview = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
modelview = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
*transformOut = projection * modelview; *transformOut = projection * modelview;
} }

View File

@ -90,27 +90,12 @@ enum class MATRIX_STACK_TYPE
class CC_DLL Director : public Ref class CC_DLL Director : public Ref
{ {
private:
std::stack<Mat4> _modelViewMatrixStack;
std::stack<Mat4> _projectionMatrixStack;
std::stack<Mat4> _textureMatrixStack;
protected:
void initMatrixStack();
public:
void pushMatrix(MATRIX_STACK_TYPE type);
void popMatrix(MATRIX_STACK_TYPE type);
void loadIdentityMatrix(MATRIX_STACK_TYPE type);
void loadMatrix(MATRIX_STACK_TYPE type, const Mat4& mat);
void multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat);
Mat4 getMatrix(MATRIX_STACK_TYPE type);
void resetMatrixStack();
public: public:
static const char *EVENT_PROJECTION_CHANGED; static const char *EVENT_PROJECTION_CHANGED;
static const char* EVENT_AFTER_UPDATE; static const char* EVENT_AFTER_UPDATE;
static const char* EVENT_AFTER_VISIT; static const char* EVENT_AFTER_VISIT;
static const char* EVENT_AFTER_DRAW; static const char* EVENT_AFTER_DRAW;
/** @typedef ccDirectorProjection /** @typedef ccDirectorProjection
Possible OpenGL projections used by director Possible OpenGL projections used by director
*/ */
@ -399,6 +384,14 @@ public:
*/ */
float getFrameRate() const { return _frameRate; } float getFrameRate() const { return _frameRate; }
void pushMatrix(MATRIX_STACK_TYPE type);
void popMatrix(MATRIX_STACK_TYPE type);
void loadIdentityMatrix(MATRIX_STACK_TYPE type);
void loadMatrix(MATRIX_STACK_TYPE type, const Mat4& mat);
void multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat);
const Mat4& getMatrix(MATRIX_STACK_TYPE type);
void resetMatrixStack();
protected: protected:
void purgeDirector(); void purgeDirector();
bool _purgeDirectorInNextLoop; // this flag will be set to true in end() bool _purgeDirectorInNextLoop; // this flag will be set to true in end()
@ -417,6 +410,12 @@ protected:
void initTextureCache(); void initTextureCache();
void destroyTextureCache(); void destroyTextureCache();
void initMatrixStack();
std::stack<Mat4> _modelViewMatrixStack;
std::stack<Mat4> _projectionMatrixStack;
std::stack<Mat4> _textureMatrixStack;
/** Scheduler associated with this director /** Scheduler associated with this director
@since v2.0 @since v2.0
*/ */

View File

@ -222,7 +222,7 @@ Mat4 Skin::getNodeToWorldTransformAR() const
void Skin::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) void Skin::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{ {
Mat4 mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); auto mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
//TODO: implement z order //TODO: implement z order
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, mv); _quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, mv);

View File

@ -844,15 +844,14 @@ void GLProgram::setUniformsForBuiltins()
Director* director = Director::getInstance(); Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack"); CCASSERT(nullptr != director, "Director is null when seting matrix stack");
Mat4 matrixMV; auto matrixMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
matrixMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
setUniformsForBuiltins(matrixMV); setUniformsForBuiltins(matrixMV);
} }
void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV) void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV)
{ {
Mat4 matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); auto matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
if(_flags.usesP) if(_flags.usesP)
setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_P_MATRIX], matrixP.m, 1); setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_P_MATRIX], matrixP.m, 1);

View File

@ -167,9 +167,8 @@ void Box2DTestLayer::onDraw()
{ {
Director* director = Director::getInstance(); Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack"); CCASSERT(nullptr != director, "Director is null when seting matrix stack");
Mat4 oldMV; auto oldMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
oldMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewMV); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewMV);
world->DrawDebugData(); world->DrawDebugData();
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, oldMV); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, oldMV);

View File

@ -1447,7 +1447,7 @@ void Sprite3DWithOBBPerfromanceTest::calculateRayByLocationInView(Ray* ray, cons
{ {
auto dir = Director::getInstance(); auto dir = Director::getInstance();
auto view = dir->getWinSize(); auto view = dir->getWinSize();
Mat4 mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
Vec3 src = Vec3(location.x, location.y, -1); Vec3 src = Vec3(location.x, location.y, -1);
Vec3 nearPoint; Vec3 nearPoint;