mirror of https://github.com/axmolengine/axmol.git
fix #234, spine custom shader doesn't work.
This commit is contained in:
parent
dcc4c8d385
commit
82c864f2ed
File diff suppressed because it is too large
Load Diff
|
@ -167,7 +167,7 @@ Cocos2dExtension::Cocos2dExtension() : DefaultSpineExtension() { }
|
||||||
Cocos2dExtension::~Cocos2dExtension() { }
|
Cocos2dExtension::~Cocos2dExtension() { }
|
||||||
|
|
||||||
char *Cocos2dExtension::_readFile(const spine::String &path, int *length) {
|
char *Cocos2dExtension::_readFile(const spine::String &path, int *length) {
|
||||||
Data data = FileUtils::getInstance()->getDataFromFile(path.buffer());
|
Data data = FileUtils::getInstance()->getDataFromFile(path.buffer());
|
||||||
if (data.isNull()) return nullptr;
|
if (data.isNull()) return nullptr;
|
||||||
|
|
||||||
// avoid buffer overflow (int is shorter than ssize_t in certain platforms)
|
// avoid buffer overflow (int is shorter than ssize_t in certain platforms)
|
||||||
|
@ -178,7 +178,7 @@ char *Cocos2dExtension::_readFile(const spine::String &path, int *length) {
|
||||||
return ret;
|
return ret;
|
||||||
#else
|
#else
|
||||||
*length = static_cast<int>(data.getSize());
|
*length = static_cast<int>(data.getSize());
|
||||||
char* bytes = MALLOC(char, *length);
|
auto bytes = SpineExtension::alloc<char>(*length, __FILE__, __LINE__);
|
||||||
memcpy(bytes, data.getBytes(), *length);
|
memcpy(bytes, data.getBytes(), *length);
|
||||||
return bytes;
|
return bytes;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -60,22 +60,8 @@ void SkeletonBatch::destroyInstance () {
|
||||||
SkeletonBatch::SkeletonBatch () {
|
SkeletonBatch::SkeletonBatch () {
|
||||||
|
|
||||||
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
|
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
|
||||||
_programState = std::make_shared<backend::ProgramState>(program);
|
_programState = new backend::ProgramState(program); // new default program state
|
||||||
|
updateProgramStateLayout(_programState);
|
||||||
auto vertexLayout = _programState->getVertexLayout();
|
|
||||||
|
|
||||||
auto locPosition = _programState->getAttributeLocation("a_position");
|
|
||||||
auto locTexcoord = _programState->getAttributeLocation("a_texCoord");
|
|
||||||
auto locColor = _programState->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");
|
|
||||||
_locTexture = _programState->getUniformLocation("u_texture");
|
|
||||||
|
|
||||||
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(createNewTrianglesCommand());
|
||||||
}
|
}
|
||||||
|
@ -95,6 +81,25 @@ SkeletonBatch::~SkeletonBatch () {
|
||||||
delete _commandsPool[i];
|
delete _commandsPool[i];
|
||||||
_commandsPool[i] = nullptr;
|
_commandsPool[i] = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CC_SAFE_RELEASE(_programState);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SkeletonBatch::updateProgramStateLayout(cocos2d::backend::ProgramState* programState)
|
||||||
|
{
|
||||||
|
auto vertexLayout = programState->getVertexLayout();
|
||||||
|
|
||||||
|
auto locPosition = programState->getAttributeLocation("a_position");
|
||||||
|
auto locTexcoord = programState->getAttributeLocation("a_texCoord");
|
||||||
|
auto locColor = programState->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");
|
||||||
|
_locTexture = programState->getUniformLocation("u_texture");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonBatch::update (float delta) {
|
void SkeletonBatch::update (float delta) {
|
||||||
|
@ -148,15 +153,27 @@ void SkeletonBatch::deallocateIndices(uint32_t numIndices) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
cocos2d::TrianglesCommand* SkeletonBatch::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, 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();
|
TrianglesCommand* 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);
|
||||||
|
|
||||||
auto programState = command->getPipelineDescriptor().programState;
|
if (programState == nullptr)
|
||||||
CCASSERT(programState, "programState should not be null");
|
programState = _programState;
|
||||||
|
|
||||||
programState->setUniform(_locMVP, projectionMat.m, sizeof(projectionMat.m));
|
CCASSERT(programState, "programState should not be null");
|
||||||
programState->setTexture(_locTexture, 0, texture->getBackendTexture());
|
|
||||||
|
auto& pipelinePS = command->getPipelineDescriptor().programState;
|
||||||
|
if (pipelinePS != programState)
|
||||||
|
{
|
||||||
|
CC_SAFE_RELEASE(pipelinePS);
|
||||||
|
pipelinePS = programState;
|
||||||
|
CC_SAFE_RETAIN(pipelinePS);
|
||||||
|
|
||||||
|
updateProgramStateLayout(pipelinePS);
|
||||||
|
}
|
||||||
|
|
||||||
|
pipelinePS->setUniform(_locMVP, projectionMat.m, sizeof(projectionMat.m));
|
||||||
|
pipelinePS->setTexture(_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);
|
||||||
|
@ -177,12 +194,6 @@ cocos2d::TrianglesCommand* SkeletonBatch::nextFreeCommand() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto* command = _commandsPool[_nextFreeCommand++];
|
auto* command = _commandsPool[_nextFreeCommand++];
|
||||||
auto& pipelineDescriptor = command->getPipelineDescriptor();
|
|
||||||
if (pipelineDescriptor.programState == nullptr)
|
|
||||||
{
|
|
||||||
CCASSERT(_programState, "programState should not be null");
|
|
||||||
pipelineDescriptor.programState = _programState->clone();
|
|
||||||
}
|
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
|
|
||||||
#include <spine/spine.h>
|
#include <spine/spine.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "renderer/backend/ProgramState.h"
|
||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
|
@ -50,8 +51,10 @@ namespace spine {
|
||||||
void deallocateVertices(uint32_t numVertices);
|
void deallocateVertices(uint32_t numVertices);
|
||||||
unsigned short* allocateIndices(uint32_t numIndices);
|
unsigned short* allocateIndices(uint32_t numIndices);
|
||||||
void deallocateIndices(uint32_t numVertices);
|
void deallocateIndices(uint32_t numVertices);
|
||||||
cocos2d::TrianglesCommand* addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, 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);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SkeletonBatch ();
|
SkeletonBatch ();
|
||||||
virtual ~SkeletonBatch ();
|
virtual ~SkeletonBatch ();
|
||||||
|
@ -61,7 +64,9 @@ namespace spine {
|
||||||
cocos2d::TrianglesCommand* nextFreeCommand ();
|
cocos2d::TrianglesCommand* nextFreeCommand ();
|
||||||
|
|
||||||
cocos2d::TrianglesCommand* createNewTrianglesCommand();
|
cocos2d::TrianglesCommand* createNewTrianglesCommand();
|
||||||
std::shared_ptr<cocos2d::backend::ProgramState> _programState = nullptr;
|
|
||||||
|
// the default program state for batch draw
|
||||||
|
cocos2d::backend::ProgramState* _programState = nullptr;
|
||||||
cocos2d::backend::UniformLocation _locMVP;
|
cocos2d::backend::UniformLocation _locMVP;
|
||||||
cocos2d::backend::UniformLocation _locTexture;
|
cocos2d::backend::UniformLocation _locTexture;
|
||||||
|
|
||||||
|
|
|
@ -99,16 +99,7 @@ namespace {
|
||||||
backend::UniformLocation __locPMatrix;
|
backend::UniformLocation __locPMatrix;
|
||||||
backend::UniformLocation __locTexture;
|
backend::UniformLocation __locTexture;
|
||||||
|
|
||||||
void initTwoColorProgramState()
|
static void updateProgramStateLayout(backend::ProgramState* programState) {
|
||||||
{
|
|
||||||
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->autorelease();
|
|
||||||
|
|
||||||
__locPMatrix = programState->getUniformLocation("u_PMatrix");
|
__locPMatrix = programState->getUniformLocation("u_PMatrix");
|
||||||
__locTexture = programState->getUniformLocation("u_texture");
|
__locTexture = programState->getUniformLocation("u_texture");
|
||||||
|
|
||||||
|
@ -124,6 +115,19 @@ namespace {
|
||||||
layout->setAttribute("a_color2", locColor2, backend::VertexFormat::UBYTE4, offsetof(spine::V3F_C4B_C4B_T2F, color2), 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);
|
layout->setAttribute("a_texCoords", locTexcoord, backend::VertexFormat::FLOAT2, offsetof(spine::V3F_C4B_C4B_T2F, texCoords), false);
|
||||||
layout->setLayout(sizeof(spine::V3F_C4B_C4B_T2F));
|
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);
|
||||||
|
|
||||||
__twoColorProgramState = std::shared_ptr<backend::ProgramState>(programState);
|
__twoColorProgramState = std::shared_ptr<backend::ProgramState>(programState);
|
||||||
}
|
}
|
||||||
|
@ -136,9 +140,9 @@ TwoColorTrianglesCommand::TwoColorTrianglesCommand() :_materialID(0), _texture(n
|
||||||
_type = RenderCommand::Type::CUSTOM_COMMAND;
|
_type = RenderCommand::Type::CUSTOM_COMMAND;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TwoColorTrianglesCommand::init(float globalOrder, cocos2d::Texture2D *texture, BlendFunc blendType, const TwoColorTriangles& triangles, const Mat4& mv, uint32_t flags) {
|
void TwoColorTrianglesCommand::init(float globalOrder, cocos2d::Texture2D *texture, cocos2d::backend::ProgramState* programState, BlendFunc blendType, const TwoColorTriangles& triangles, const Mat4& mv, uint32_t flags) {
|
||||||
|
|
||||||
updateCommandPipelineDescriptor();
|
updateCommandPipelineDescriptor(programState);
|
||||||
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);
|
||||||
|
|
||||||
auto finalMatrix = projectionMat * mv;
|
auto finalMatrix = projectionMat * mv;
|
||||||
|
@ -177,18 +181,39 @@ void TwoColorTrianglesCommand::init(float globalOrder, cocos2d::Texture2D *textu
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void TwoColorTrianglesCommand::updateCommandPipelineDescriptor()
|
void TwoColorTrianglesCommand::updateCommandPipelineDescriptor(cocos2d::backend::ProgramState* programState)
|
||||||
{
|
{
|
||||||
|
// OPTIMIZE ME: all commands belong a same Node should share a same programState like SkeletonBatch
|
||||||
if (!__twoColorProgramState)
|
if (!__twoColorProgramState)
|
||||||
{
|
{
|
||||||
initTwoColorProgramState();
|
initTwoColorProgramState();
|
||||||
}
|
}
|
||||||
|
|
||||||
CC_SAFE_RELEASE_NULL(_programState);
|
bool needsUpdateStateLayout = false;
|
||||||
_programState = __twoColorProgramState->clone();
|
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);
|
||||||
|
|
||||||
_locPMatrix = __locPMatrix;
|
_locPMatrix = __locPMatrix;
|
||||||
_locTexture = __locTexture;
|
_locTexture = __locTexture;
|
||||||
_pipelineDescriptor.programState = _programState;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TwoColorTrianglesCommand::~TwoColorTrianglesCommand()
|
TwoColorTrianglesCommand::~TwoColorTrianglesCommand()
|
||||||
|
@ -330,9 +355,9 @@ void SkeletonTwoColorBatch::deallocateIndices(uint32_t numIndices) {
|
||||||
_indices.setSize(_indices.size() - numIndices, 0);
|
_indices.setSize(_indices.size() - numIndices, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TwoColorTrianglesCommand* SkeletonTwoColorBatch::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::BlendFunc blendType, const TwoColorTriangles& triangles, const cocos2d::Mat4& mv, uint32_t flags) {
|
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) {
|
||||||
TwoColorTrianglesCommand* command = nextFreeCommand();
|
TwoColorTrianglesCommand* command = nextFreeCommand();
|
||||||
command->init(globalOrder, texture, blendType, triangles, mv, flags);
|
command->init(globalOrder, texture, programState, blendType, triangles, mv, flags);
|
||||||
command->updateVertexAndIndexBuffer(renderer, triangles.verts, triangles.vertCount, triangles.indices, triangles.indexCount);
|
command->updateVertexAndIndexBuffer(renderer, triangles.verts, triangles.vertCount, triangles.indices, triangles.indexCount);
|
||||||
renderer->addCommand(command);
|
renderer->addCommand(command);
|
||||||
return command;
|
return command;
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
|
|
||||||
#include <spine/spine.h>
|
#include <spine/spine.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "renderer/backend/ProgramState.h"
|
||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
struct V3F_C4B_C4B_T2F {
|
struct V3F_C4B_C4B_T2F {
|
||||||
|
@ -57,9 +58,9 @@ namespace spine {
|
||||||
|
|
||||||
~TwoColorTrianglesCommand();
|
~TwoColorTrianglesCommand();
|
||||||
|
|
||||||
void init(float globalOrder, cocos2d::Texture2D* texture, cocos2d::BlendFunc blendType, const TwoColorTriangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
|
void init(float globalOrder, cocos2d::Texture2D* texture, cocos2d::backend::ProgramState* programState, cocos2d::BlendFunc blendType, const TwoColorTriangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
|
||||||
|
|
||||||
void updateCommandPipelineDescriptor();
|
void updateCommandPipelineDescriptor(cocos2d::backend::ProgramState* programState);
|
||||||
|
|
||||||
inline cocos2d::backend::TextureBackend* getTexture() const { return _texture; }
|
inline cocos2d::backend::TextureBackend* getTexture() const { return _texture; }
|
||||||
|
|
||||||
|
@ -118,7 +119,7 @@ namespace spine {
|
||||||
unsigned short* allocateIndices(uint32_t numIndices);
|
unsigned short* allocateIndices(uint32_t numIndices);
|
||||||
void deallocateIndices(uint32_t numIndices);
|
void deallocateIndices(uint32_t numIndices);
|
||||||
|
|
||||||
TwoColorTrianglesCommand* addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::BlendFunc blendType, const TwoColorTriangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
|
TwoColorTrianglesCommand* addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::backend::ProgramState* programState, cocos2d::BlendFunc blendType, const TwoColorTriangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
|
||||||
|
|
||||||
void batch(cocos2d::Renderer* renderer, TwoColorTrianglesCommand* command);
|
void batch(cocos2d::Renderer* renderer, TwoColorTrianglesCommand* command);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue