Skip to content

Commit

Permalink
Fix renderer test case batching (axmolengine#2291)
Browse files Browse the repository at this point in the history
* Ensure sprites using the same custom shader are batched

* Fix for incorrect ProgramId check

* Enhance check for valid batch ID

* Use method name that indicates purpose
  • Loading branch information
rh101 authored Dec 27, 2024
1 parent cfa6f51 commit 69c14f7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/renderer/TrianglesCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void TrianglesCommand::init(float globalOrder,
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor = blendType.src;
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor = blendType.dst;

if (_batchId == -1)
if (!_pipelineDescriptor.programState->isBatchable())
setSkipBatching(true);

if (!isSkipBatching())
Expand Down
2 changes: 1 addition & 1 deletion core/renderer/backend/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class AX_DLL Program : public Object
* Get program id.
* @return The program id.
*/
int64_t getProgramId() const { return _programId; }
uint64_t getProgramId() const { return _programId; }

/**
* Get uniform buffer size in bytes that can hold all the uniforms.
Expand Down
4 changes: 2 additions & 2 deletions core/renderer/backend/ProgramManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ class AX_DLL ProgramManager
};

BuiltinRegInfo _builtinRegistry[(int)backend::ProgramType::BUILTIN_COUNT];
std::unordered_map<int64_t, BuiltinRegInfo> _customRegistry;
std::unordered_map<uint64_t, BuiltinRegInfo> _customRegistry;

std::unordered_map<int64_t, Program*> _cachedPrograms; ///< The cached program object.
std::unordered_map<uint64_t, Program*> _cachedPrograms; ///< The cached program object.

XXH64_state_s* _programIdGen;

Expand Down
5 changes: 5 additions & 0 deletions core/renderer/backend/ProgramState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,18 @@ bool ProgramState::init(Program* program)

const auto programId = program->getProgramId();
if (programId < ProgramType::BUILTIN_COUNT)
{
this->_batchId = programId;
this->_isBatchable = true;
}

return true;
}

void ProgramState::updateBatchId()
{
_batchId = XXH64(_uniformBuffers.data(), _uniformBuffers.size(), _program->getProgramId());
_isBatchable = true;
}

void ProgramState::resetUniforms()
Expand Down Expand Up @@ -229,6 +233,7 @@ ProgramState* ProgramState::clone() const
cp->_vertexLayout = !_ownVertexLayout ? _vertexLayout : new VertexLayout(*_vertexLayout);

cp->_batchId = this->_batchId;
cp->_isBatchable = this->_isBatchable;
return cp;
}

Expand Down
8 changes: 7 additions & 1 deletion core/renderer/backend/ProgramState.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ class AX_DLL ProgramState : public Object
*/
uint64_t getBatchId() const { return _batchId; };

/*
* Gets the state of the batch ID. If true, then batch ID is valid
*/
bool isBatchable() const { return _isBatchable; };

/*
* Update batchID of current program state, by default, custom program was traits with mutable uniforms
* so batch ID was set to -1 indicate batch was disabled
Expand Down Expand Up @@ -399,7 +404,8 @@ class AX_DLL ProgramState : public Object
VertexLayout* _vertexLayout = nullptr;
bool _ownVertexLayout = false;

uint64_t _batchId = -1;
uint64_t _batchId = -1;
bool _isBatchable = false;

#if AX_ENABLE_CACHE_TEXTURE_DATA
EventListenerCustom* _backToForegroundListener = nullptr;
Expand Down
6 changes: 5 additions & 1 deletion tests/cpp-tests/Source/NewRendererTest/NewRendererTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,9 @@ RendererUniformBatch::RendererUniformBatch()
Size s = Director::getInstance()->getWinSize();

auto blurState = createBlurProgramState();
blurState->updateBatchId();
auto sepiaState = createSepiaProgramState();
sepiaState->updateBatchId();

auto x_inc = s.width / 20;
auto y_inc = s.height / 6;
Expand Down Expand Up @@ -896,8 +898,10 @@ RendererUniformBatch2::RendererUniformBatch2()
{
Size s = Director::getInstance()->getWinSize();

auto blurState = createBlurProgramState();
auto blurState = createBlurProgramState();
blurState->updateBatchId();
auto sepiaState = createSepiaProgramState();
sepiaState->updateBatchId();

auto x_inc = s.width / 20;
auto y_inc = s.height / 6;
Expand Down

0 comments on commit 69c14f7

Please sign in to comment.