axmol/core/renderer/Pass.cpp

367 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Ideas taken from:
- GamePlay3D: http://gameplay3d.org/
- OGRE3D: http://www.ogre3d.org/
- Qt3D: http://qt-project.org/
****************************************************************************/
#include "renderer/Pass.h"
2019-11-23 20:27:39 +08:00
#include <xxhash.h>
#include "renderer/Texture2D.h"
#include "renderer/Technique.h"
#include "renderer/Material.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/ProgramState.h"
#include "3d/MeshVertexIndexData.h"
#include "3d/VertexAttribBinding.h"
#include "base/Director.h"
#include "renderer/Renderer.h"
#include "base/Types.h"
#include "base/Utils.h"
#include "2d/Node.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// uniform names
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
static const char* s_dirLightUniformColorName = "u_DirLightSourceColor";
static const char* s_dirLightUniformDirName = "u_DirLightSourceDirection";
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
static const char* s_pointLightUniformColorName = "u_PointLightSourceColor";
static const char* s_pointLightUniformPositionName = "u_PointLightSourcePosition";
static const char* s_pointLightUniformRangeInverseName = "u_PointLightSourceRangeInverse";
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
static const char* s_spotLightUniformColorName = "u_SpotLightSourceColor";
static const char* s_spotLightUniformPositionName = "u_SpotLightSourcePosition";
static const char* s_spotLightUniformDirName = "u_SpotLightSourceDirection";
static const char* s_spotLightUniformInnerAngleCosName = "u_SpotLightSourceInnerAngleCos";
static const char* s_spotLightUniformOuterAngleCosName = "u_SpotLightSourceOuterAngleCos";
static const char* s_spotLightUniformRangeInverseName = "u_SpotLightSourceRangeInverse";
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
static const char* s_ambientLightUniformColorName = "u_AmbientLightSourceColor";
2019-11-23 20:27:39 +08:00
Pass* Pass::create(Technique* technique)
{
2021-12-08 00:11:53 +08:00
auto pass = new Pass();
if (pass->init(technique))
2019-11-23 20:27:39 +08:00
{
pass->autorelease();
return pass;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(pass);
2019-11-23 20:27:39 +08:00
return nullptr;
}
Pass* Pass::createWithProgramState(Technique* technique, backend::ProgramState* programState)
{
2021-12-08 00:11:53 +08:00
auto pass = new Pass();
if (pass->initWithProgramState(technique, programState))
2019-11-23 20:27:39 +08:00
{
pass->autorelease();
return pass;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(pass);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool Pass::init(Technique* technique)
{
_technique = technique;
return true;
}
2021-12-25 10:04:45 +08:00
bool Pass::initWithProgramState(Technique* technique, backend::ProgramState* programState)
2019-11-23 20:27:39 +08:00
{
_technique = technique;
setProgramState(programState);
return true;
}
2021-12-25 10:04:45 +08:00
Pass::Pass() {}
2019-11-23 20:27:39 +08:00
Pass::~Pass()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_vertexAttribBinding);
AX_SAFE_RELEASE(_programState);
2019-11-23 20:27:39 +08:00
}
Pass* Pass::clone() const
{
2021-12-25 10:04:45 +08:00
auto pass = new Pass();
2021-12-08 00:11:53 +08:00
pass->_renderState = _renderState;
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
pass->setProgramState(_programState->clone());
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
pass->_vertexAttribBinding = _vertexAttribBinding;
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(pass->_vertexAttribBinding);
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
pass->setTechnique(_technique);
pass->autorelease();
2019-11-23 20:27:39 +08:00
return pass;
}
backend::ProgramState* Pass::getProgramState() const
{
2021-12-25 10:04:45 +08:00
return _programState;
2019-11-23 20:27:39 +08:00
}
void Pass::setProgramState(backend::ProgramState* programState)
{
if (_programState != programState)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_programState);
2019-11-23 20:27:39 +08:00
_programState = programState;
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(_programState);
2019-11-23 20:27:39 +08:00
initUniformLocations();
_hashDirty = true;
}
}
void Pass::initUniformLocations()
{
2021-12-25 10:04:45 +08:00
auto* ps = _programState;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_locMVPMatrix = ps->getUniformLocation("u_MVPMatrix");
_locMVMatrix = ps->getUniformLocation("u_MVMatrix");
_locPMatrix = ps->getUniformLocation("u_PMatrix");
2019-11-23 20:27:39 +08:00
_locNormalMatrix = ps->getUniformLocation("u_NormalMatrix");
_locTexture = ps->getUniformLocation("u_tex0");
2019-11-23 20:27:39 +08:00
_locNormalTexture = ps->getUniformLocation("u_normalTex");
2021-12-25 10:04:45 +08:00
_locColor = ps->getUniformLocation("u_color");
2019-11-23 20:27:39 +08:00
_locMatrixPalette = ps->getUniformLocation("u_matrixPalette");
_locDirLightColor = ps->getUniformLocation(s_dirLightUniformColorName);
2021-12-25 10:04:45 +08:00
_locDirLightDir = ps->getUniformLocation(s_dirLightUniformDirName);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_locPointLightColor = ps->getUniformLocation(s_pointLightUniformColorName);
_locPointLightPosition = ps->getUniformLocation(s_pointLightUniformPositionName);
2019-11-23 20:27:39 +08:00
_locPointLightRangeInverse = ps->getUniformLocation(s_pointLightUniformRangeInverseName);
2021-12-25 10:04:45 +08:00
_locSpotLightColor = ps->getUniformLocation(s_spotLightUniformColorName);
_locSpotLightPosition = ps->getUniformLocation(s_spotLightUniformPositionName);
_locSpotLightDir = ps->getUniformLocation(s_spotLightUniformDirName);
2019-11-23 20:27:39 +08:00
_locSpotLightInnerAngleCos = ps->getUniformLocation(s_spotLightUniformInnerAngleCosName);
_locSpotLightOuterAngleCos = ps->getUniformLocation(s_spotLightUniformOuterAngleCosName);
2021-12-25 10:04:45 +08:00
_locSpotLightRangeInverse = ps->getUniformLocation(s_spotLightUniformRangeInverseName);
2019-11-23 20:27:39 +08:00
_locAmbientLigthColor = ps->getUniformLocation(s_ambientLightUniformColorName);
}
2021-12-25 10:04:45 +08:00
void Pass::draw(MeshCommand* meshCommand,
float globalZOrder,
backend::Buffer* vertexBuffer,
backend::Buffer* indexBuffer,
MeshCommand::PrimitiveType primitive,
MeshCommand::IndexFormat indexFormat,
unsigned int indexCount,
const Mat4& modelView)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
meshCommand->setBeforeCallback(AX_CALLBACK_0(Pass::onBeforeVisitCmd, this, meshCommand));
meshCommand->setAfterCallback(AX_CALLBACK_0(Pass::onAfterVisitCmd, this, meshCommand));
2019-11-23 20:27:39 +08:00
meshCommand->init(globalZOrder, modelView);
meshCommand->setPrimitiveType(primitive);
meshCommand->setIndexBuffer(indexBuffer, indexFormat);
meshCommand->setVertexBuffer(vertexBuffer);
meshCommand->setIndexDrawInfo(0, indexCount);
meshCommand->getPipelineDescriptor().programState = _programState;
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
2019-11-23 20:27:39 +08:00
renderer->addCommand(meshCommand);
}
void Pass::updateMVPUniform(const Mat4& modelView)
{
2021-12-25 10:04:45 +08:00
auto& matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto mvp = matrixP * modelView;
2019-11-23 20:27:39 +08:00
_programState->setUniform(_locMVPMatrix, mvp.m, sizeof(mvp.m));
if (_locMVMatrix)
{
_programState->setUniform(_locMVMatrix, modelView.m, sizeof(modelView.m));
}
if (_locPMatrix)
{
_programState->setUniform(_locPMatrix, matrixP.m, sizeof(matrixP.m));
}
if (_locNormalMatrix)
{
auto normalMatrix = utils::getNormalMat3OfMat4(modelView);
2023-08-09 14:37:43 +08:00
_programState->setUniform(_locNormalMatrix, &normalMatrix, sizeof(normalMatrix));
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void Pass::onBeforeVisitCmd(MeshCommand* command)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// save renderer states
2019-11-23 20:27:39 +08:00
_rendererDepthTestEnabled = renderer->getDepthTest();
2021-12-25 10:04:45 +08:00
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();
_rendererCullMode = renderer->getCullMode();
_rendererDepthWrite = renderer->getDepthWrite();
_rendererWinding = renderer->getWinding();
// apply state blocks
2019-11-23 20:27:39 +08:00
_renderState.bindPass(this, command);
updateMVPUniform(command->getMV());
}
2021-12-25 10:04:45 +08:00
void Pass::onAfterVisitCmd(MeshCommand* command)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
2019-11-23 20:27:39 +08:00
// restore renderer states
renderer->setDepthTest(_rendererDepthTestEnabled);
renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
renderer->setCullMode(_rendererCullMode);
renderer->setDepthWrite(_rendererDepthWrite);
renderer->setWinding(_rendererWinding);
}
Node* Pass::getTarget() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(_technique && _technique->_material, "Pass must have a Technique and Material");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Material* material = _technique->_material;
2019-11-23 20:27:39 +08:00
return material->_target;
}
2021-12-25 10:04:45 +08:00
void Pass::setTechnique(Technique* technique)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_technique = technique; // weak reference
2019-11-23 20:27:39 +08:00
}
void Pass::setVertexAttribBinding(VertexAttribBinding* binding)
{
if (_vertexAttribBinding != binding)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_vertexAttribBinding);
2019-11-23 20:27:39 +08:00
_vertexAttribBinding = binding;
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(_vertexAttribBinding);
2019-11-23 20:27:39 +08:00
}
}
VertexAttribBinding* Pass::getVertexAttributeBinding() const
{
return _vertexAttribBinding;
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformTexture(uint32_t slot, backend::TextureBackend* tex)
2019-11-23 20:27:39 +08:00
{
_programState->setTexture(_locTexture, slot, tex);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformNormTexture(uint32_t slot, backend::TextureBackend* tex)
2019-11-23 20:27:39 +08:00
{
_programState->setTexture(_locNormalTexture, slot, tex);
}
2021-12-25 10:04:45 +08:00
#define TRY_SET_UNIFORM(loc) \
do \
{ \
if (loc) \
{ \
_programState->setUniform(loc, data, (uint32_t)dataLen); \
} \
} while (false)
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void Pass::setUniformColor(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locColor);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformMatrixPalette(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locMatrixPalette);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformDirLightColor(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locDirLightColor);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformDirLightDir(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locDirLightDir);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformPointLightColor(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locPointLightColor);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformPointLightPosition(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locPointLightPosition);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformPointLightRangeInverse(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locPointLightRangeInverse);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformSpotLightColor(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locSpotLightColor);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformSpotLightPosition(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locSpotLightPosition);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformSpotLightDir(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locSpotLightDir);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformSpotLightInnerAngleCos(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locSpotLightInnerAngleCos);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformSpotLightOuterAngleCos(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locSpotLightOuterAngleCos);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformSpotLightRangeInverse(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locSpotLightRangeInverse);
}
2021-12-25 10:04:45 +08:00
void Pass::setUniformAmbientLigthColor(const void* data, size_t dataLen)
2019-11-23 20:27:39 +08:00
{
TRY_SET_UNIFORM(_locAmbientLigthColor);
}
NS_AX_END