Merge pull request #485 from adxeproject/fix-spine-uniform-location-mismatch

Fix spine command pipeline state uniform location mismatch
This commit is contained in:
halx99 2021-08-30 18:57:36 +08:00 committed by GitHub
parent 32b86c53c9
commit 97fbec0b36
2 changed files with 44 additions and 45 deletions

View File

@ -61,9 +61,8 @@ SkeletonBatch::SkeletonBatch () {
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR); auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new backend::ProgramState(program); // new default program state _programState = new backend::ProgramState(program); // new default program state
updateProgramStateLayout(_programState);
for (unsigned int i = 0; i < INITIAL_SIZE; i++) { for (unsigned int i = 0; i < INITIAL_SIZE; i++) {
_commandsPool.push_back(createNewTrianglesCommand()); _commandsPool.push_back(newCommand());
} }
reset(); reset();
// callback after drawing is finished so we can clear out the batch state // callback after drawing is finished so we can clear out the batch state
@ -81,25 +80,30 @@ SkeletonBatch::~SkeletonBatch () {
delete _commandsPool[i]; delete _commandsPool[i];
_commandsPool[i] = nullptr; _commandsPool[i] = nullptr;
} }
CC_SAFE_RELEASE(_programState); CC_SAFE_RELEASE(_programState);
} }
void SkeletonBatch::updateProgramStateLayout(cocos2d::backend::ProgramState* programState) backend::ProgramState* SkeletonBatch::updateCommandPipelinePS(SkeletonCommand* command, backend::ProgramState* programState)
{ {
auto vertexLayout = programState->getVertexLayout(); auto& currentState = command->getPipelineDescriptor().programState;
if(currentState == nullptr || currentState->getProgram() != programState->getProgram()) {
auto locPosition = programState->getAttributeLocation("a_position"); CC_SAFE_RELEASE(currentState);
auto locTexcoord = programState->getAttributeLocation("a_texCoord"); currentState = programState->clone();
auto locColor = programState->getAttributeLocation("a_color");
vertexLayout->setAttribute("a_position", locPosition, backend::VertexFormat::FLOAT3, offsetof(V3F_C4B_T2F, vertices), false); auto vertexLayout = currentState->getVertexLayout();
vertexLayout->setAttribute("a_color", locColor, backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors), true); auto locPosition = currentState->getAttributeLocation("a_position");
vertexLayout->setAttribute("a_texCoord", locTexcoord, backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords), false); auto locTexcoord = currentState->getAttributeLocation("a_texCoord");
vertexLayout->setLayout(sizeof(_vertices[0])); auto locColor = currentState->getAttributeLocation("a_color");
vertexLayout->setAttribute("a_position", locPosition, backend::VertexFormat::FLOAT3, offsetof(V3F_C4B_T2F, vertices), false);
vertexLayout->setAttribute("a_color", locColor, backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors), true);
vertexLayout->setAttribute("a_texCoord", locTexcoord, backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords), false);
vertexLayout->setLayout(sizeof(_vertices[0]));
_locMVP = programState->getUniformLocation("u_MVPMatrix"); command->_locMVP = currentState->getUniformLocation("u_MVPMatrix");
_locTexture = programState->getUniformLocation("u_texture"); command->_locTexture = currentState->getUniformLocation("u_texture");
}
return currentState;
} }
void SkeletonBatch::update (float delta) { void SkeletonBatch::update (float delta) {
@ -112,8 +116,8 @@ cocos2d::V3F_C4B_T2F* SkeletonBatch::allocateVertices(uint32_t numVertices) {
_vertices.resize((_vertices.size() + numVertices) * 2 + 1); _vertices.resize((_vertices.size() + numVertices) * 2 + 1);
cocos2d::V3F_C4B_T2F* newData = _vertices.data(); cocos2d::V3F_C4B_T2F* newData = _vertices.data();
for (uint32_t i = 0; i < this->_nextFreeCommand; i++) { for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
TrianglesCommand* command = _commandsPool[i]; SkeletonCommand* command = _commandsPool[i];
cocos2d::TrianglesCommand::Triangles& triangles = (cocos2d::TrianglesCommand::Triangles&)command->getTriangles(); SkeletonCommand::Triangles& triangles = (SkeletonCommand::Triangles&)command->getTriangles();
triangles.verts = newData + (triangles.verts - oldData); triangles.verts = newData + (triangles.verts - oldData);
} }
} }
@ -135,8 +139,8 @@ unsigned short* SkeletonBatch::allocateIndices(uint32_t numIndices) {
_indices.ensureCapacity(_indices.size() + numIndices); _indices.ensureCapacity(_indices.size() + numIndices);
unsigned short* newData = _indices.buffer(); unsigned short* newData = _indices.buffer();
for (uint32_t i = 0; i < this->_nextFreeCommand; i++) { for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
TrianglesCommand* command = _commandsPool[i]; SkeletonCommand* command = _commandsPool[i];
cocos2d::TrianglesCommand::Triangles& triangles = (cocos2d::TrianglesCommand::Triangles&)command->getTriangles(); SkeletonCommand::Triangles& triangles = (SkeletonCommand::Triangles&)command->getTriangles();
if (triangles.indices >= oldData && triangles.indices < oldData + oldSize) { if (triangles.indices >= oldData && triangles.indices < oldData + oldSize) {
triangles.indices = newData + (triangles.indices - oldData); triangles.indices = newData + (triangles.indices - oldData);
} }
@ -154,7 +158,7 @@ void SkeletonBatch::deallocateIndices(uint32_t numIndices) {
cocos2d::TrianglesCommand* SkeletonBatch::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, backend::ProgramState* programState, cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand::Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags) { cocos2d::TrianglesCommand* SkeletonBatch::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, backend::ProgramState* programState, cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand::Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags) {
TrianglesCommand* command = nextFreeCommand(); SkeletonCommand* command = nextFreeCommand();
const cocos2d::Mat4& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); const cocos2d::Mat4& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
if (programState == nullptr) if (programState == nullptr)
@ -162,17 +166,10 @@ cocos2d::TrianglesCommand* SkeletonBatch::addCommand(cocos2d::Renderer* renderer
CCASSERT(programState, "programState should not be null"); CCASSERT(programState, "programState should not be null");
auto& pipelinePS = command->getPipelineDescriptor().programState; auto pipelinePS = updateCommandPipelinePS(command, programState);
if (pipelinePS == nullptr || pipelinePS->getProgram() != programState->getProgram())
{
CC_SAFE_RELEASE(pipelinePS);
pipelinePS = programState->clone();
updateProgramStateLayout(pipelinePS);
}
pipelinePS->setUniform(_locMVP, projectionMat.m, sizeof(projectionMat.m)); pipelinePS->setUniform(command->_locMVP, projectionMat.m, sizeof(projectionMat.m));
pipelinePS->setTexture(_locTexture, 0, texture->getBackendTexture()); pipelinePS->setTexture(command->_locTexture, 0, texture->getBackendTexture());
command->init(globalOrder, texture, blendType, triangles, mv, flags); command->init(globalOrder, texture, blendType, triangles, mv, flags);
renderer->addCommand(command); renderer->addCommand(command);
@ -185,19 +182,19 @@ void SkeletonBatch::reset() {
_indices.setSize(0, 0); _indices.setSize(0, 0);
} }
cocos2d::TrianglesCommand* SkeletonBatch::nextFreeCommand() { SkeletonCommand* SkeletonBatch::nextFreeCommand() {
if (_commandsPool.size() <= _nextFreeCommand) { if (_commandsPool.size() <= _nextFreeCommand) {
unsigned int newSize = _commandsPool.size() * 2 + 1; unsigned int newSize = _commandsPool.size() * 2 + 1;
for (int i = _commandsPool.size(); i < newSize; i++) { for (int i = _commandsPool.size(); i < newSize; i++) {
_commandsPool.push_back(createNewTrianglesCommand()); _commandsPool.push_back(newCommand());
} }
} }
auto* command = _commandsPool[_nextFreeCommand++]; auto* command = _commandsPool[_nextFreeCommand++];
return command; return command;
} }
cocos2d::TrianglesCommand *SkeletonBatch::createNewTrianglesCommand() { SkeletonCommand* SkeletonBatch::newCommand() {
auto* command = new TrianglesCommand(); auto* command = new SkeletonCommand();
return command; return command;
} }
} }

View File

@ -39,6 +39,11 @@
namespace spine { namespace spine {
struct SkeletonCommand : public cocos2d::TrianglesCommand {
cocos2d::backend::UniformLocation _locMVP;
cocos2d::backend::UniformLocation _locTexture;
};
class SP_API SkeletonBatch { class SP_API SkeletonBatch {
public: public:
static SkeletonBatch* getInstance (); static SkeletonBatch* getInstance ();
@ -53,25 +58,22 @@ namespace spine {
void deallocateIndices(uint32_t numVertices); void deallocateIndices(uint32_t numVertices);
cocos2d::TrianglesCommand* addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::backend::ProgramState* programState, cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand::Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags); cocos2d::TrianglesCommand* addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::backend::ProgramState* programState, cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand::Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
void updateProgramStateLayout(cocos2d::backend::ProgramState* programState); cocos2d::backend::ProgramState* updateCommandPipelinePS(SkeletonCommand* command, cocos2d::backend::ProgramState* programState);
protected: protected:
SkeletonBatch (); SkeletonBatch ();
virtual ~SkeletonBatch (); virtual ~SkeletonBatch ();
void reset (); void reset ();
cocos2d::TrianglesCommand* nextFreeCommand (); SkeletonCommand* nextFreeCommand ();
cocos2d::TrianglesCommand* createNewTrianglesCommand(); SkeletonCommand* newCommand();
cocos2d::backend::ProgramState* _programState; // The default program state
// the default program state for batch draw
cocos2d::backend::ProgramState* _programState = nullptr;
cocos2d::backend::UniformLocation _locMVP;
cocos2d::backend::UniformLocation _locTexture;
// pool of commands // pool of commands
std::vector<cocos2d::TrianglesCommand*> _commandsPool; std::vector<SkeletonCommand*> _commandsPool;
uint32_t _nextFreeCommand; uint32_t _nextFreeCommand;
// pool of vertices // pool of vertices