axmol/extensions/spine/SkeletonTwoColorBatch.cpp

434 lines
15 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>
#include <stddef.h> // offsetof
#include "base/ccTypes.h"
#include "base/ccUtils.h"
2019-12-12 23:26:12 +08:00
#include "xxhash.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/Device.h"
2019-11-23 20:27:39 +08:00
USING_NS_CC;
#define EVENT_AFTER_DRAW_RESET_POSITION "director_after_draw"
using std::max;
#define INITIAL_SIZE (10000)
#define MAX_VERTICES 64000
#define MAX_INDICES 64000
#define STRINGIFY(A) #A
2019-12-12 23:26:12 +08:00
namespace {
const char* TWO_COLOR_TINT_VERTEX_SHADER = STRINGIFY(
uniform mat4 u_PMatrix;
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec4 a_color2;
attribute vec2 a_texCoords;
\n#ifdef GL_ES\n
varying lowp vec4 v_light;
varying lowp vec4 v_dark;
varying mediump vec2 v_texCoord;
\n#else\n
varying vec4 v_light;
varying vec4 v_dark;
varying vec2 v_texCoord;
\n#endif\n
void main() {
v_light = a_color;
v_dark = a_color2;
v_texCoord = a_texCoords;
gl_Position = u_PMatrix * a_position;
}
);
const char* TWO_COLOR_TINT_FRAGMENT_SHADER = STRINGIFY(
\n#ifdef GL_ES\n
precision lowp float;
\n#endif\n
uniform sampler2D u_tex0;
2019-12-12 23:26:12 +08:00
varying vec4 v_light;
varying vec4 v_dark;
varying vec2 v_texCoord;
void main() {
vec4 texColor = texture2D(u_tex0, v_texCoord);
2019-12-12 23:26:12 +08:00
float alpha = texColor.a * v_light.a;
gl_FragColor.a = alpha;
gl_FragColor.rgb = ((texColor.a - 1.0) * v_dark.a + 1.0 - texColor.rgb) * v_dark.rgb + texColor.rgb * v_light.rgb;
}
);
std::shared_ptr<backend::ProgramState> __twoColorProgramState = nullptr;
backend::UniformLocation __locPMatrix;
backend::UniformLocation __locTexture;
static void updateProgramStateLayout(backend::ProgramState* programState) {
2019-12-12 23:26:12 +08:00
__locPMatrix = programState->getUniformLocation("u_PMatrix");
__locTexture = programState->getUniformLocation("u_tex0");
2019-12-12 23:26:12 +08:00
auto layout = programState->getVertexLayout();
2020-01-05 03:09:32 +08:00
auto locPosition = programState->getAttributeLocation("a_position");
auto locTexcoord = programState->getAttributeLocation("a_texCoords");
auto locColor = programState->getAttributeLocation("a_color");
auto locColor2 = programState->getAttributeLocation("a_color2");
layout->setAttribute("a_position", locPosition, backend::VertexFormat::FLOAT3, offsetof(spine::V3F_C4B_C4B_T2F, position), false);
layout->setAttribute("a_color", locColor, backend::VertexFormat::UBYTE4, offsetof(spine::V3F_C4B_C4B_T2F, color), true);
layout->setAttribute("a_color2", locColor2, backend::VertexFormat::UBYTE4, offsetof(spine::V3F_C4B_C4B_T2F, color2), true);
layout->setAttribute("a_texCoords", locTexcoord, backend::VertexFormat::FLOAT2, offsetof(spine::V3F_C4B_C4B_T2F, texCoords), false);
2019-12-12 23:26:12 +08:00
layout->setLayout(sizeof(spine::V3F_C4B_C4B_T2F));
}
static void initTwoColorProgramState()
{
if (__twoColorProgramState)
{
return;
}
auto program = backend::Device::getInstance()->newProgram(TWO_COLOR_TINT_VERTEX_SHADER, TWO_COLOR_TINT_FRAGMENT_SHADER);
auto* programState = new backend::ProgramState(program);
program->release();
updateProgramStateLayout(programState);
2019-12-12 23:26:12 +08:00
__twoColorProgramState = std::shared_ptr<backend::ProgramState>(programState);
}
}
2019-11-23 20:27:39 +08:00
namespace spine {
2020-01-05 03:09:32 +08:00
TwoColorTrianglesCommand::TwoColorTrianglesCommand() :_materialID(0), _texture(nullptr), _blendType(BlendFunc::DISABLE) {
2019-11-23 20:27:39 +08:00
_type = RenderCommand::Type::CUSTOM_COMMAND;
}
void TwoColorTrianglesCommand::init(float globalOrder, cocos2d::Texture2D *texture, cocos2d::backend::ProgramState* programState, BlendFunc blendType, const TwoColorTriangles& triangles, const Mat4& mv, uint32_t flags) {
2019-11-23 20:27:39 +08:00
updateCommandPipelineDescriptor(programState);
2019-12-12 23:26:12 +08:00
const cocos2d::Mat4& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
auto finalMatrix = projectionMat * mv;
2019-11-23 20:27:39 +08:00
2019-12-12 23:26:12 +08:00
_programState->setUniform(_locPMatrix, finalMatrix.m, sizeof(finalMatrix.m));
_programState->setTexture(_locTexture, 0, texture->getBackendTexture());
RenderCommand::init(globalOrder, mv, flags);
2019-11-23 20:27:39 +08:00
2019-12-12 23:26:12 +08:00
_triangles = triangles;
if (_triangles.indexCount % 3 != 0) {
int count = _triangles.indexCount;
_triangles.indexCount = count / 3 * 3;
CCLOGERROR("Resize indexCount from %d to %d, size must be multiple times of 3", count, _triangles.indexCount);
}
_mv = mv;
if (_blendType.src != blendType.src || _blendType.dst != blendType.dst ||
2020-01-05 03:09:32 +08:00
_texture != texture->getBackendTexture() || _pipelineDescriptor.programState != _programState)
2019-12-12 23:26:12 +08:00
{
_texture = texture->getBackendTexture();
2019-11-23 20:27:39 +08:00
_blendType = blendType;
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
_prog = _programState->getProgram();
auto& blendDescriptor = _pipelineDescriptor.blendDescriptor;
blendDescriptor.blendEnabled = true;
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor = blendType.src;
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor = blendType.dst;
2019-11-23 20:27:39 +08:00
generateMaterialID();
}
}
2019-12-12 23:26:12 +08:00
void TwoColorTrianglesCommand::updateCommandPipelineDescriptor(cocos2d::backend::ProgramState* programState)
2019-12-12 23:26:12 +08:00
{
// OPTIMIZE ME: all commands belong a same Node should share a same programState like SkeletonBatch
2019-12-12 23:26:12 +08:00
if (!__twoColorProgramState)
{
initTwoColorProgramState();
}
bool needsUpdateStateLayout = false;
auto& pipelinePS = _pipelineDescriptor.programState;
if (programState != nullptr)
{
if (_programState != programState) {
CC_SAFE_RELEASE(_programState);
_programState = programState; // Because the programState belong to Node, so no need to clone
CC_SAFE_RETAIN(_programState);
needsUpdateStateLayout = true;
}
}
else {
needsUpdateStateLayout = _programState != nullptr && _programState->getProgram() != __twoColorProgramState->getProgram();
CC_SAFE_RELEASE(_programState);
_programState = __twoColorProgramState->clone();
}
CCASSERT(_programState, "programState should not be null");
pipelinePS = _programState;
if (needsUpdateStateLayout)
updateProgramStateLayout(pipelinePS);
2020-01-05 03:09:32 +08:00
_locPMatrix = __locPMatrix;
_locTexture = __locTexture;
2019-12-12 23:26:12 +08:00
}
2020-01-05 03:09:32 +08:00
TwoColorTrianglesCommand::~TwoColorTrianglesCommand()
2019-12-12 23:26:12 +08:00
{
CC_SAFE_RELEASE_NULL(_programState);
2019-11-23 20:27:39 +08:00
}
void TwoColorTrianglesCommand::generateMaterialID() {
// do not batch if using custom uniforms (since we cannot batch) it
2019-12-12 23:26:12 +08:00
struct
2019-11-23 20:27:39 +08:00
{
2019-12-12 23:26:12 +08:00
void* texture;
void* prog;
backend::BlendFactor src;
backend::BlendFactor dst;
}hashMe;
// NOTE: Initialize hashMe struct to make the value of padding bytes be filled with zero.
// It's important since XXH32 below will also consider the padding bytes which probably
// are set to random values by different compilers.
memset(&hashMe, 0, sizeof(hashMe));
hashMe.texture = _texture;
hashMe.src = _blendType.src;
hashMe.dst = _blendType.dst;
hashMe.prog = _prog;
_materialID = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
2019-11-23 20:27:39 +08:00
}
2019-12-12 23:26:12 +08:00
void TwoColorTrianglesCommand::draw(Renderer *r) {
SkeletonTwoColorBatch::getInstance()->batch(r, this);
2019-11-23 20:27:39 +08:00
}
2019-12-12 23:26:12 +08:00
void TwoColorTrianglesCommand::updateVertexAndIndexBuffer(Renderer *r, V3F_C4B_C4B_T2F *vertices, int verticesSize, uint16_t *indices, int indicesSize)
{
if(verticesSize != _vertexCapacity)
createVertexBuffer(sizeof(V3F_C4B_C4B_T2F), verticesSize, CustomCommand::BufferUsage::DYNAMIC);
if(indicesSize != _indexCapacity)
createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, indicesSize, CustomCommand::BufferUsage::DYNAMIC);
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
updateVertexBuffer(vertices, sizeof(V3F_C4B_C4B_T2F) * verticesSize);
updateIndexBuffer(indices, sizeof(uint16_t) * indicesSize);
2019-11-23 20:27:39 +08:00
}
static SkeletonTwoColorBatch* instance = nullptr;
SkeletonTwoColorBatch* SkeletonTwoColorBatch::getInstance () {
if (!instance) instance = new SkeletonTwoColorBatch();
return instance;
}
void SkeletonTwoColorBatch::destroyInstance () {
if (instance) {
delete instance;
instance = nullptr;
}
}
2019-12-12 23:26:12 +08:00
SkeletonTwoColorBatch::SkeletonTwoColorBatch () : _vertexBuffer(0), _indexBuffer(0) {
_commandsPool.reserve(INITIAL_SIZE);
2019-11-23 20:27:39 +08:00
for (unsigned int i = 0; i < INITIAL_SIZE; i++) {
_commandsPool.push_back(new TwoColorTrianglesCommand());
}
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
reset ();
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
// 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-12-12 23:26:12 +08:00
});
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
}
SkeletonTwoColorBatch::~SkeletonTwoColorBatch () {
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++) {
delete _commandsPool[i];
_commandsPool[i] = nullptr;
}
2019-12-12 23:26:12 +08:00
delete[] _vertexBuffer;
delete[] _indexBuffer;
2019-11-23 20:27:39 +08:00
}
2020-01-05 03:09:32 +08:00
void SkeletonTwoColorBatch::update (float delta) {
2019-11-23 20:27:39 +08:00
reset();
}
V3F_C4B_C4B_T2F* SkeletonTwoColorBatch::allocateVertices(uint32_t numVertices) {
if (_vertices.size() - _numVertices < numVertices) {
V3F_C4B_C4B_T2F* oldData = _vertices.data();
_vertices.resize((_vertices.size() + numVertices) * 2 + 1);
V3F_C4B_C4B_T2F* newData = _vertices.data();
for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
TwoColorTrianglesCommand* command = _commandsPool[i];
TwoColorTriangles& triangles = (TwoColorTriangles&)command->getTriangles();
triangles.verts = newData + (triangles.verts - oldData);
}
}
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
V3F_C4B_C4B_T2F* vertices = _vertices.data() + _numVertices;
_numVertices += numVertices;
return vertices;
}
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
void SkeletonTwoColorBatch::deallocateVertices(uint32_t numVertices) {
_numVertices -= numVertices;
}
unsigned short* SkeletonTwoColorBatch::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++) {
TwoColorTrianglesCommand* command = _commandsPool[i];
TwoColorTriangles& triangles = (TwoColorTriangles&)command->getTriangles();
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 SkeletonTwoColorBatch::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
}
TwoColorTrianglesCommand* SkeletonTwoColorBatch::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, backend::ProgramState* programState, cocos2d::BlendFunc blendType, const TwoColorTriangles& triangles, const cocos2d::Mat4& mv, uint32_t flags) {
2019-11-23 20:27:39 +08:00
TwoColorTrianglesCommand* command = nextFreeCommand();
command->init(globalOrder, texture, programState, blendType, triangles, mv, flags);
2019-12-12 23:26:12 +08:00
command->updateVertexAndIndexBuffer(renderer, triangles.verts, triangles.vertCount, triangles.indices, triangles.indexCount);
2020-01-05 03:09:32 +08:00
renderer->addCommand(command);
2019-11-23 20:27:39 +08:00
return command;
}
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
void SkeletonTwoColorBatch::batch (cocos2d::Renderer *renderer, TwoColorTrianglesCommand* command) {
2019-11-23 20:27:39 +08:00
if (_numVerticesBuffer + command->getTriangles().vertCount >= MAX_VERTICES || _numIndicesBuffer + command->getTriangles().indexCount >= MAX_INDICES) {
2019-12-12 23:26:12 +08:00
flush(renderer, _lastCommand);
}
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
uint32_t materialID = command->getMaterialID();
if (_lastCommand && _lastCommand->getMaterialID() != materialID) {
flush(renderer, _lastCommand);
2019-11-23 20:27:39 +08:00
}
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
memcpy(_vertexBuffer + _numVerticesBuffer, command->getTriangles().verts, sizeof(V3F_C4B_C4B_T2F) * command->getTriangles().vertCount);
const Mat4& modelView = command->getModelView();
for (int i = _numVerticesBuffer; i < _numVerticesBuffer + command->getTriangles().vertCount; i++) {
modelView.transformPoint(&_vertexBuffer[i].position);
}
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
unsigned short vertexOffset = (unsigned short)_numVerticesBuffer;
unsigned short* indices = command->getTriangles().indices;
for (int i = 0, j = _numIndicesBuffer; i < command->getTriangles().indexCount; i++, j++) {
_indexBuffer[j] = indices[i] + vertexOffset;
}
2020-01-05 03:09:32 +08:00
2019-11-23 20:27:39 +08:00
_numVerticesBuffer += command->getTriangles().vertCount;
_numIndicesBuffer += command->getTriangles().indexCount;
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
if (command->isForceFlush()) {
flush(renderer, command);
2019-11-23 20:27:39 +08:00
}
_lastCommand = command;
}
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
void SkeletonTwoColorBatch::flush (cocos2d::Renderer *renderer, TwoColorTrianglesCommand* materialCommand) {
2019-11-23 20:27:39 +08:00
if (!materialCommand)
return;
2020-01-05 03:09:32 +08:00
2019-12-12 23:26:12 +08:00
materialCommand->updateVertexAndIndexBuffer(renderer, _vertexBuffer, _numVerticesBuffer, _indexBuffer, _numIndicesBuffer);
renderer->addCommand(materialCommand);
_numVerticesBuffer = 0;
2019-11-23 20:27:39 +08:00
_numIndicesBuffer = 0;
_numBatches++;
}
void SkeletonTwoColorBatch::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
_numVerticesBuffer = 0;
_numIndicesBuffer = 0;
_lastCommand = nullptr;
_numBatches = 0;
}
TwoColorTrianglesCommand* SkeletonTwoColorBatch::nextFreeCommand() {
if (_commandsPool.size() <= _nextFreeCommand) {
2019-12-12 23:26:12 +08:00
unsigned int newSize = _commandsPool.size() * 2 + 1;
for (int i = _commandsPool.size(); i < newSize; i++) {
2019-11-23 20:27:39 +08:00
_commandsPool.push_back(new TwoColorTrianglesCommand());
}
}
TwoColorTrianglesCommand* command = _commandsPool[_nextFreeCommand++];
command->setForceFlush(false);
return command;
}
}
2020-01-05 03:09:32 +08:00
#endif