axmol/extensions/spine/SkeletonBatch.cpp

208 lines
8.0 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/******************************************************************************
2019-12-12 23:26:12 +08:00
* Spine Runtimes License Agreement
2020-01-05 03:09:32 +08:00
* Last updated January 1, 2020. Replaces all prior versions.
2019-11-23 20:27:39 +08:00
*
2020-01-05 03:09:32 +08:00
* Copyright (c) 2013-2020, Esoteric Software LLC
2019-11-23 20:27:39 +08:00
*
2019-12-12 23:26:12 +08:00
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
2019-11-23 20:27:39 +08:00
*
2019-12-12 23:26:12 +08:00
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
2020-01-05 03:09:32 +08:00
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2019-11-23 20:27:39 +08:00
*****************************************************************************/
2020-01-05 03:09:32 +08:00
#include <spine/spine-cocos2dx.h>
#if COCOS2D_VERSION >= 0x00040000
2019-12-12 23:26:12 +08:00
#include <spine/Extension.h>
2019-11-23 20:27:39 +08:00
#include <algorithm>
USING_NS_AX;
2019-11-23 20:27:39 +08:00
#define EVENT_AFTER_DRAW_RESET_POSITION "director_after_draw"
using std::max;
#define INITIAL_SIZE (10000)
2019-12-12 23:26:12 +08:00
#include "renderer/ccShaders.h"
#include "renderer/backend/Device.h"
2022-07-05 14:13:15 +08:00
#include "renderer/backend/Types.h"
2019-12-12 23:26:12 +08:00
2019-11-23 20:27:39 +08:00
namespace spine {
static SkeletonBatch* instance = nullptr;
SkeletonBatch* SkeletonBatch::getInstance () {
if (!instance) instance = new SkeletonBatch();
return instance;
}
void SkeletonBatch::destroyInstance () {
if (instance) {
delete instance;
instance = nullptr;
}
}
SkeletonBatch::SkeletonBatch () {
2019-12-12 23:26:12 +08:00
2020-01-05 03:09:32 +08:00
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new backend::ProgramState(program); // new default program state
2020-01-05 03:09:32 +08:00
for (unsigned int i = 0; i < INITIAL_SIZE; i++) {
_commandsPool.push_back(newCommand());
2020-01-05 03:09:32 +08:00
}
reset();
// callback after drawing is finished so we can clear out the batch state
// for the next frame
Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_AFTER_DRAW_RESET_POSITION, [this](EventCustom* eventCustom) {
this->update(0);
});;
2019-11-23 20:27:39 +08:00
}
SkeletonBatch::~SkeletonBatch () {
Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(EVENT_AFTER_DRAW_RESET_POSITION);
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
for (unsigned int i = 0; i < _commandsPool.size(); i++) {
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_commandsPool[i]->getPipelineDescriptor().programState);
2019-11-23 20:27:39 +08:00
delete _commandsPool[i];
_commandsPool[i] = nullptr;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_programState);
}
backend::ProgramState* SkeletonBatch::updateCommandPipelinePS(SkeletonCommand* command, backend::ProgramState* programState)
{
auto& currentState = command->getPipelineDescriptor().programState;
2022-08-08 18:02:17 +08:00
#if defined(AXYS_VERSION)
if(currentState == nullptr || currentState->getProgram() != programState->getProgram() || currentState->getUniformID() != programState->getUniformID()) {
#else
if(currentState == nullptr || currentState->getProgram() != programState->getProgram()) {
#endif
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(currentState);
currentState = programState->clone();
auto vertexLayout = currentState->getVertexLayout();
auto locPosition = currentState->getAttributeLocation(backend::ATTRIBUTE_NAME_POSITION);
auto locTexcoord = currentState->getAttributeLocation(backend::ATTRIBUTE_NAME_TEXCOORD);
auto locColor = currentState->getAttributeLocation(backend::ATTRIBUTE_NAME_COLOR);
2022-07-05 14:36:04 +08:00
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION, locPosition, backend::VertexFormat::FLOAT3, offsetof(V3F_C4B_T2F, vertices), false);
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, locColor, backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors), true);
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD, locTexcoord, backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords), false);
vertexLayout->setLayout(sizeof(_vertices[0]));
command->_locMVP = currentState->getUniformLocation(backend::UNIFORM_NAME_MVP_MATRIX);
command->_locTexture = currentState->getUniformLocation(backend::UNIFORM_NAME_TEXTURE);
}
return currentState;
2019-11-23 20:27:39 +08:00
}
void SkeletonBatch::update (float delta) {
reset();
}
2020-01-05 03:09:32 +08:00
2022-08-08 18:02:17 +08:00
ax::V3F_C4B_T2F* SkeletonBatch::allocateVertices(uint32_t numVertices) {
2019-11-23 20:27:39 +08:00
if (_vertices.size() - _numVertices < numVertices) {
2022-08-08 18:02:17 +08:00
ax::V3F_C4B_T2F* oldData = _vertices.data();
2019-11-23 20:27:39 +08:00
_vertices.resize((_vertices.size() + numVertices) * 2 + 1);
2022-08-08 18:02:17 +08:00
ax::V3F_C4B_T2F* newData = _vertices.data();
2019-11-23 20:27:39 +08:00
for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
SkeletonCommand* command = _commandsPool[i];
SkeletonCommand::Triangles& triangles = (SkeletonCommand::Triangles&)command->getTriangles();
2019-11-23 20:27:39 +08:00
triangles.verts = newData + (triangles.verts - oldData);
}
}
2020-01-05 03:09:32 +08:00
2022-08-08 18:02:17 +08:00
ax::V3F_C4B_T2F* vertices = _vertices.data() + _numVertices;
2019-11-23 20:27:39 +08:00
_numVertices += numVertices;
return vertices;
}
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
void SkeletonBatch::deallocateVertices(uint32_t numVertices) {
_numVertices -= numVertices;
}
2020-01-05 03:09:32 +08:00
unsigned short* SkeletonBatch::allocateIndices(uint32_t numIndices) {
2019-12-12 23:26:12 +08:00
if (_indices.getCapacity() - _indices.size() < numIndices) {
unsigned short* oldData = _indices.buffer();
int oldSize = _indices.size();
_indices.ensureCapacity(_indices.size() + numIndices);
unsigned short* newData = _indices.buffer();
2019-11-23 20:27:39 +08:00
for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
SkeletonCommand* command = _commandsPool[i];
SkeletonCommand::Triangles& triangles = (SkeletonCommand::Triangles&)command->getTriangles();
2019-11-23 20:27:39 +08:00
if (triangles.indices >= oldData && triangles.indices < oldData + oldSize) {
triangles.indices = newData + (triangles.indices - oldData);
}
}
}
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
unsigned short* indices = _indices.buffer() + _indices.size();
_indices.setSize(_indices.size() + numIndices, 0);
2019-11-23 20:27:39 +08:00
return indices;
}
void SkeletonBatch::deallocateIndices(uint32_t numIndices) {
2019-12-12 23:26:12 +08:00
_indices.setSize(_indices.size() - numIndices, 0);
2019-11-23 20:27:39 +08:00
}
2020-01-05 03:09:32 +08:00
2022-08-08 18:02:17 +08:00
ax::TrianglesCommand* SkeletonBatch::addCommand(ax::Renderer* renderer, float globalOrder, ax::Texture2D* texture, backend::ProgramState* programState, ax::BlendFunc blendType, const ax::TrianglesCommand::Triangles& triangles, const ax::Mat4& mv, uint32_t flags) {
SkeletonCommand* command = nextFreeCommand();
2022-08-08 18:02:17 +08:00
const ax::Mat4& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2019-12-12 23:26:12 +08:00
if (programState == nullptr)
programState = _programState;
2022-07-16 10:43:05 +08:00
AXASSERT(programState, "programState should not be null");
2019-12-12 23:26:12 +08:00
auto pipelinePS = updateCommandPipelinePS(command, programState);
pipelinePS->setUniform(command->_locMVP, projectionMat.m, sizeof(projectionMat.m));
pipelinePS->setTexture(command->_locTexture, 0, texture->getBackendTexture());
2019-12-12 23:26:12 +08:00
command->init(globalOrder, texture, blendType, triangles, mv, flags);
renderer->addCommand(command);
2019-11-23 20:27:39 +08:00
return command;
}
void SkeletonBatch::reset() {
_nextFreeCommand = 0;
_numVertices = 0;
2019-12-12 23:26:12 +08:00
_indices.setSize(0, 0);
2019-11-23 20:27:39 +08:00
}
SkeletonCommand* SkeletonBatch::nextFreeCommand() {
2020-01-05 03:09:32 +08:00
if (_commandsPool.size() <= _nextFreeCommand) {
unsigned int newSize = _commandsPool.size() * 2 + 1;
for (int i = _commandsPool.size(); i < newSize; i++) {
_commandsPool.push_back(newCommand());
2020-01-05 03:09:32 +08:00
}
}
auto* command = _commandsPool[_nextFreeCommand++];
2019-12-12 23:26:12 +08:00
return command;
}
SkeletonCommand* SkeletonBatch::newCommand() {
auto* command = new SkeletonCommand();
2019-12-12 23:26:12 +08:00
return command;
2019-11-23 20:27:39 +08:00
}
}
2020-01-05 03:09:32 +08:00
#endif