Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions axmol/2d/AtlasNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,19 @@ void AtlasNode::updateAtlasValues()
}

// AtlasNode - draw
void AtlasNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
void AtlasNode::draw(const SceneRenderState& state, const Mat4& transform, uint32_t flags)
{
if (_textureAtlas->getTotalQuads() == 0)
return;

auto programState = _quadCommand.unsafePS();

const auto& projectionMat = Camera::getVisitingViewProjectionMatrix();
const auto& projectionMat = state.getViewProjectionMatrix();
programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));

_quadCommand.init(_globalZOrder, _textureAtlas->getTexture(), _blendFunc, _textureAtlas->getQuads(), _quadsToDraw,
transform, flags);
renderer->addCommand(&_quadCommand);
transform, flags, state.getView());
state.getRenderer()->addCommand(&_quadCommand);
}

// AtlasNode - RGBA protocol
Expand Down
2 changes: 1 addition & 1 deletion axmol/2d/AtlasNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AX_DLL AtlasNode : public Node, public TextureProtocol
virtual void updateAtlasValues();

// Overrides
void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
void draw(const SceneRenderState& state, const Mat4& transform, uint32_t flags) override;
Texture2D* getTexture() const override;
void setTexture(Texture2D* texture) override;
bool isOpacityModifyRGB() const override;
Expand Down
42 changes: 21 additions & 21 deletions axmol/2d/ClippingNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,26 @@ void ClippingNode::onExit()
Node::onExit();
}

void ClippingNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
void ClippingNode::visit(const SceneRenderState& state, const Mat4& parentTransform, uint32_t parentFlags)
{
if (!_visible || !hasContent())
return;

AXASSERT(_stencil, "No stencil set");

uint32_t flags = processParentFlags(parentTransform, parentFlags);
uint32_t flags = processParentFlags(state, parentTransform, parentFlags);

// Add group command

auto* groupCommandStencil = renderer->getNextGroupCommand();
auto* groupCommandStencil = state.getRenderer()->getNextGroupCommand();
groupCommandStencil->init(_globalZOrder);
renderer->addCommand(groupCommandStencil);
state.getRenderer()->addCommand(groupCommandStencil);

renderer->pushGroup(groupCommandStencil->getRenderQueueID());
state.getRenderer()->pushGroup(groupCommandStencil->getRenderQueueID());

// _beforeVisitCmd.init(_globalZOrder);
// _beforeVisitCmd.func = AX_CALLBACK_0(StencilStateManager::onBeforeVisit, _stencilStateManager);
// renderer->addCommand(&_beforeVisitCmd);
// state.getRenderer()->addCommand(&_beforeVisitCmd);
_stencilStateManager->onBeforeVisit(_globalZOrder);

auto alphaThreshold = this->getAlphaThreshold();
Expand All @@ -167,23 +167,23 @@ void ClippingNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32

AX_SAFE_RELEASE_NULL(programState);
}
_stencil->visit(renderer, _modelViewTransform, flags);
_stencil->visit(state, _modelViewTransform, flags);

auto afterDrawStencilCmd = renderer->nextCallbackCommand();
auto afterDrawStencilCmd = state.getRenderer()->nextCallbackCommand();
afterDrawStencilCmd->init(_globalZOrder);
afterDrawStencilCmd->func = AX_CALLBACK_0(StencilStateManager::onAfterDrawStencil, _stencilStateManager);
renderer->addCommand(afterDrawStencilCmd);
state.getRenderer()->addCommand(afterDrawStencilCmd);

bool visibleByCamera = isVisitableByVisitingCamera();
bool visibleByCamera = isVisitableByCamera(state.cameraFlag);

// `_groupCommandChildren` is used as a barrier
// to ensure commands above be executed before children nodes
auto* groupCommandChildren = renderer->getNextGroupCommand();
auto* groupCommandChildren = state.getRenderer()->getNextGroupCommand();

groupCommandChildren->init(_globalZOrder);
renderer->addCommand(groupCommandChildren);
state.getRenderer()->addCommand(groupCommandChildren);

renderer->pushGroup(groupCommandChildren->getRenderQueueID());
state.getRenderer()->pushGroup(groupCommandChildren->getRenderQueueID());

if (!_children.empty())
{
Expand All @@ -195,30 +195,30 @@ void ClippingNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32
auto node = _children.at(i);

if (node && node->getLocalZOrder() < 0)
node->visit(renderer, _modelViewTransform, flags);
node->visit(state, _modelViewTransform, flags);
else
break;
}
// self draw
if (visibleByCamera)
this->draw(renderer, _modelViewTransform, flags);
this->draw(state, _modelViewTransform, flags);

for (auto it = _children.cbegin() + i, itCend = _children.cend(); it != itCend; ++it)
(*it)->visit(renderer, _modelViewTransform, flags);
(*it)->visit(state, _modelViewTransform, flags);
}
else if (visibleByCamera)
{
this->draw(renderer, _modelViewTransform, flags);
this->draw(state, _modelViewTransform, flags);
}

renderer->popGroup();
state.getRenderer()->popGroup();

auto _afterVisitCmd = renderer->nextCallbackCommand();
auto _afterVisitCmd = state.getRenderer()->nextCallbackCommand();
_afterVisitCmd->init(_globalZOrder);
_afterVisitCmd->func = AX_CALLBACK_0(StencilStateManager::onAfterVisit, _stencilStateManager);
renderer->addCommand(_afterVisitCmd);
state.getRenderer()->addCommand(_afterVisitCmd);

renderer->popGroup();
state.getRenderer()->popGroup();
}

void ClippingNode::setGlobalZOrder(float globalZOrder)
Expand Down
2 changes: 1 addition & 1 deletion axmol/2d/ClippingNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class AX_DLL ClippingNode : public Node
* @lua NA
*/
void onExit() override;
void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
void visit(const SceneRenderState& state, const Mat4& parentTransform, uint32_t parentFlags) override;

void setGlobalZOrder(float globalZOrder) override;

Expand Down
12 changes: 6 additions & 6 deletions axmol/2d/ClippingRectangleNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ void ClippingRectangleNode::onAfterVisitScissor()
_director->getRenderer()->setScissorTest(_oldScissorTest);
}

void ClippingRectangleNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
void ClippingRectangleNode::visit(const SceneRenderState& state, const Mat4& parentTransform, uint32_t parentFlags)
{
auto beforeVisitCmdScissor = renderer->nextCallbackCommand();
auto beforeVisitCmdScissor = state.getRenderer()->nextCallbackCommand();
beforeVisitCmdScissor->init(_globalZOrder);
beforeVisitCmdScissor->func = AX_CALLBACK_0(ClippingRectangleNode::onBeforeVisitScissor, this);
renderer->addCommand(beforeVisitCmdScissor);
state.getRenderer()->addCommand(beforeVisitCmdScissor);

Node::visit(renderer, parentTransform, parentFlags);
Node::visit(state, parentTransform, parentFlags);

auto afterVisitCmdScissor = renderer->nextCallbackCommand();
auto afterVisitCmdScissor = state.getRenderer()->nextCallbackCommand();
afterVisitCmdScissor->init(_globalZOrder);
afterVisitCmdScissor->func = AX_CALLBACK_0(ClippingRectangleNode::onAfterVisitScissor, this);
renderer->addCommand(afterVisitCmdScissor);
state.getRenderer()->addCommand(afterVisitCmdScissor);
}

} // namespace ax
2 changes: 1 addition & 1 deletion axmol/2d/ClippingRectangleNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class AX_DLL ClippingRectangleNode : public Node
void setClippingEnabled(bool enabled) { _clippingEnabled = enabled; }

// virtual void draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) override;
void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
void visit(const SceneRenderState& state, const Mat4& parentTransform, uint32_t parentFlags) override;

protected:
ClippingRectangleNode() = default;
Expand Down
24 changes: 12 additions & 12 deletions axmol/2d/DrawNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ void DrawNode::updateBlendState(CustomCommand& cmd)
}
}

void DrawNode::updateUniforms(const Mat4& transform, CustomCommand& cmd)
void DrawNode::updateUniforms(const SceneRenderState& state, const Mat4& transform, CustomCommand& cmd)
{
auto pipelinePS = cmd.unsafePS();
const auto& matrixP = Camera::getVisitingViewProjectionMatrix();
const auto& matrixP = state.getViewProjectionMatrix();
Mat4 matrixMVP = matrixP * transform;
auto mvpLocation = pipelinePS->getUniformLocation("u_MVPMatrix");
pipelinePS->setUniform(mvpLocation, matrixMVP.m, sizeof(matrixMVP.m));
Expand All @@ -209,33 +209,33 @@ void DrawNode::updateUniforms(const Mat4& transform, CustomCommand& cmd)
pipelinePS->setUniform(alphaUniformLocation, &alpha, sizeof(alpha));
}

void DrawNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
void DrawNode::draw(const SceneRenderState& state, const Mat4& transform, uint32_t flags)
{
if (_trianglesDirty || _pointsDirty || _linesDirty)
updateBuffers();

if (_customCommandTriangle.getVertexDrawCount() > 0)
{
updateBlendState(_customCommandTriangle);
updateUniforms(transform, _customCommandTriangle);
updateUniforms(state, transform, _customCommandTriangle);
_customCommandTriangle.init(_globalZOrder);
renderer->addCommand(&_customCommandTriangle);
state.getRenderer()->addCommand(&_customCommandTriangle);
}

if (_customCommandPoint.getVertexDrawCount() > 0)
{
updateBlendState(_customCommandPoint);
updateUniforms(transform, _customCommandPoint);
updateUniforms(state, transform, _customCommandPoint);
_customCommandPoint.init(_globalZOrder);
renderer->addCommand(&_customCommandPoint);
state.getRenderer()->addCommand(&_customCommandPoint);
}

if (_customCommandLine.getVertexDrawCount() > 0)
{
updateBlendState(_customCommandLine);
updateUniforms(transform, _customCommandLine);
updateUniforms(state, transform, _customCommandLine);
_customCommandLine.init(_globalZOrder);
renderer->addCommand(&_customCommandLine);
state.getRenderer()->addCommand(&_customCommandLine);
}
}

Expand Down Expand Up @@ -688,16 +688,16 @@ void DrawNode::setBlendFunc(const BlendFunc& blendFunc)
_blendFunc = blendFunc;
}

void DrawNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
void DrawNode::visit(const SceneRenderState& state, const Mat4& parentTransform, uint32_t parentFlags)
{
if (_isolated)
{
// ignore `parentTransform` from parent
Node::visit(renderer, Mat4::identity, parentFlags);
Node::visit(state, Mat4::identity, parentFlags);
}
else
{
Node::visit(renderer, parentTransform, parentFlags);
Node::visit(state, parentTransform, parentFlags);
}
}

Expand Down
6 changes: 3 additions & 3 deletions axmol/2d/DrawNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,9 @@ class AX_DLL DrawNode : public Node, public BlendProtocol
void setBlendFunc(const BlendFunc& blendFunc) override;

// Overrides
void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
void draw(const SceneRenderState& state, const Mat4& transform, uint32_t flags) override;

void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
void visit(const SceneRenderState& state, const Mat4& parentTransform, uint32_t parentFlags) override;

/**
* When isolated is set, the position of the node is no longer affected by parent nodes.
Expand All @@ -564,7 +564,7 @@ class AX_DLL DrawNode : public Node, public BlendProtocol
void freeShaderInternal(CustomCommand& cmd);

void updateBlendState(CustomCommand& cmd);
void updateUniforms(const Mat4& transform, CustomCommand& cmd);
void updateUniforms(const SceneRenderState& state, const Mat4& transform, CustomCommand& cmd);

bool _trianglesDirty : 1 = false;
bool _pointsDirty : 1 = false;
Expand Down
17 changes: 9 additions & 8 deletions axmol/2d/FastTMXLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,24 @@ int FastTMXLayer::batchIndexForGID(uint32_t gid) const
return -1;
}

void FastTMXLayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
void FastTMXLayer::draw(const SceneRenderState& state, const Mat4& transform, uint32_t flags)
{
updateTotalQuads();

const float refTileX = _batches.empty() ? 1.0f : _batches[0].tilesetInfo->_tileSize.x;
auto cam = Camera::getVisitingCamera();
auto cam = state.getCamera();
if (!cam)
return;
if (flags != 0 || _dirty || _quadsDirty || !_cameraPositionDirty.fuzzyEquals(cam->getPosition(), refTileX) ||
const Vec2 cameraPosition(state.getView().position.x, state.getView().position.y);
if (flags != 0 || _dirty || _quadsDirty || !_cameraPositionDirty.fuzzyEquals(cameraPosition, refTileX) ||
_cameraZoomDirty != cam->getZoom())
{
_cameraPositionDirty = cam->getPosition();
_cameraPositionDirty = cameraPosition;
auto zoom = _cameraZoomDirty = cam->getZoom();
Vec2 s = _director->getVisibleSize();
const Vec2& anchor = getAnchorPoint();
auto rect = Rect(cam->getPositionX() - s.width * zoom * (anchor.x == 0.0f ? 0.5f : anchor.x),
cam->getPositionY() - s.height * zoom * (anchor.y == 0.0f ? 0.5f : anchor.y), s.width * zoom,
auto rect = Rect(cameraPosition.x - s.width * zoom * (anchor.x == 0.0f ? 0.5f : anchor.x),
cameraPosition.y - s.height * zoom * (anchor.y == 0.0f ? 0.5f : anchor.y), s.width * zoom,
s.height * zoom);

Mat4 inv = transform;
Expand All @@ -240,7 +241,7 @@ void FastTMXLayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flag
_dirty = false;
}

const auto& projectionMat = Camera::getVisitingViewProjectionMatrix();
const auto& projectionMat = state.getViewProjectionMatrix();
Mat4 finalMat = projectionMat * _modelViewTransform;
// Submit batches lowest-firstGid first so base/terrain tiles (low GIDs) draw behind
// overlay/object tiles (high GIDs) that share the same vertexZ.
Expand All @@ -252,7 +253,7 @@ void FastTMXLayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flag
{
// All commands in a batch share the same program, so the cached location is valid.
cmd->unsafePS()->setUniform(it->mvpMatrixLocation, finalMat.m, sizeof(finalMat.m));
renderer->addCommand(cmd);
state.getRenderer()->addCommand(cmd);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion axmol/2d/FastTMXLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class AX_DLL FastTMXLayer : public Node
// Override
//
std::string getDescription() const override;
void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
void draw(const SceneRenderState& state, const Mat4& transform, uint32_t flags) override;
void removeChild(Node* child, bool cleanup = true) override;

/** Map from gid of animated tile to its instance.
Expand Down
14 changes: 6 additions & 8 deletions axmol/2d/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void GridBase::beforeDraw()
_renderTexturePass->clear(ClearFlag::COLOR, {.color = _clearColor, .depth = 1.0f, .stencil = 0});
}

void GridBase::afterDraw(ax::Node* /*target*/)
void GridBase::afterDraw(ax::Node* /*target*/, const SceneRenderState& state)
{
auto renderer = Director::getInstance()->getRenderer();

Expand All @@ -231,7 +231,7 @@ void GridBase::afterDraw(ax::Node* /*target*/)

renderer->addCallbackCommand([this]() -> void { beforeBlit(); });

blit();
blit(state);

renderer->addCallbackCommand([this]() -> void { afterBlit(); });
}
Expand Down Expand Up @@ -349,13 +349,12 @@ void Grid3D::afterBlit()
}
}

void Grid3D::blit()
void Grid3D::blit(const SceneRenderState& state)
{
updateVertexBuffer();
_drawCommand.init(GRID_BLIT_ORDER, _blendFunc);
Director::getInstance()->getRenderer()->addCommand(&_drawCommand);
auto camera = Camera::getVisitingCamera();
ax::Mat4 projectionMat = camera ? camera->getVisitingViewProjectionMatrix() : Mat4::identity;
ax::Mat4 projectionMat = state.getViewProjectionMatrix();
auto programState = _drawCommand.unsafePS();
programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
programState->setTexture(_textureLocation, 0, _texture->getRHITexture());
Expand Down Expand Up @@ -622,13 +621,12 @@ TiledGrid3D* TiledGrid3D::create(const Vec2& gridSize, Texture2D* texture, bool
return ret;
}

void TiledGrid3D::blit()
void TiledGrid3D::blit(const SceneRenderState& state)
{
updateVertexBuffer();
_drawCommand.init(GRID_BLIT_ORDER, _blendFunc);
Director::getInstance()->getRenderer()->addCommand(&_drawCommand);
auto camera = Camera::getVisitingCamera();
ax::Mat4 projectionMat = camera ? camera->getVisitingViewProjectionMatrix() : Mat4::identity;
ax::Mat4 projectionMat = state.getViewProjectionMatrix();
auto programState = _drawCommand.unsafePS();
programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
programState->setTexture(_textureLocation, 0, _texture->getRHITexture());
Expand Down
Loading