Merge pull request #200 from halx99/attach-programstate

More clearly programState management
This commit is contained in:
HALX99 2020-09-09 02:38:12 -07:00 committed by GitHub
commit 67a6c03462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 179 additions and 177 deletions

View File

@ -42,38 +42,6 @@ NS_CC_BEGIN
// AtlasNode - Creation & Init
AtlasNode::AtlasNode()
{
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
attachProgramState(new (std::nothrow) backend::ProgramState(program));
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");
auto vertexLayout = _programState->getVertexLayout();
//a_position
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
_programState->getAttributeLocation(backend::Attribute::POSITION),
backend::VertexFormat::FLOAT3,
0,
false);
//a_texCoord
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
_programState->getAttributeLocation(backend::Attribute::TEXCOORD),
backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords),
false);
//a_color
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR,
_programState->getAttributeLocation(backend::Attribute::COLOR),
backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors),
true);
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
}
AtlasNode::~AtlasNode()
{
CC_SAFE_RELEASE(_textureAtlas);
@ -117,8 +85,9 @@ bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeigh
}
_textureAtlas->initWithTexture(texture, itemsToRender);
updateProgramStateTexture(texture);
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture);
this->updateBlendFunc();
this->updateOpacityModifyRGB();
@ -129,6 +98,41 @@ bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeigh
return true;
}
bool AtlasNode::attachProgramState(backend::ProgramState* programState)
{
if (Node::attachProgramState(programState))
{
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
auto vertexLayout = _programState->getVertexLayout();
//a_position
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
_programState->getAttributeLocation(backend::Attribute::POSITION),
backend::VertexFormat::FLOAT3,
0,
false);
//a_texCoord
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
_programState->getAttributeLocation(backend::Attribute::TEXCOORD),
backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords),
false);
//a_color
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR,
_programState->getAttributeLocation(backend::Attribute::COLOR),
backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors),
true);
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
updateProgramStateTexture(_textureAtlas->getTexture());
return true;
}
return false;
}
// AtlasNode - Atlas generation

View File

@ -99,8 +99,10 @@ public:
void setQuadsToDraw(ssize_t quadsToDraw);
size_t getQuadsToDraw() const;
bool attachProgramState(backend::ProgramState* programState) override;
CC_CONSTRUCTOR_ACCESS:
AtlasNode();
AtlasNode() = default;
virtual ~AtlasNode();
/** Initializes an AtlasNode with an Atlas file the width and height of each item and the quantity of items to render*/
@ -141,7 +143,6 @@ protected:
bool _ignoreContentScaleFactor = false;
QuadCommand _quadCommand;
backend::UniformLocation _textureLocation;
backend::UniformLocation _mvpMatrixLocation;
private:

View File

@ -689,18 +689,21 @@ void Label::setVertexLayout()
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
}
void Label::setProgramState(backend::ProgramState *programState)
bool Label::attachProgramState(backend::ProgramState* programState)
{
Node::setProgramState(programState);
updateUniformLocations();
for (auto &batch : _batchCommands)
{
updateBatchCommand(batch);
}
if (Node::attachProgramState(programState)) {
updateUniformLocations();
for (auto& batch : _batchCommands)
{
updateBatchCommand(batch);
}
auto &quadPipeline = _quadCommand.getPipelineDescriptor();
setVertexLayout();
quadPipeline.programState = _programState;
auto& quadPipeline = _quadCommand.getPipelineDescriptor();
setVertexLayout();
quadPipeline.programState = _programState;
return true;
}
return false;
}
void Label::updateShaderProgram()

View File

@ -639,9 +639,9 @@ public:
float getAdditionalKerning() const;
/**
* set ProgramState of current render command
* Attach ProgramState of current render command
*/
virtual void setProgramState(backend::ProgramState *programState) override;
bool attachProgramState(backend::ProgramState *programState) override;
FontAtlas* getFontAtlas() { return _fontAtlas; }

View File

@ -40,20 +40,7 @@ NS_CC_BEGIN
//CCLabelAtlas - Creation & Init
LabelAtlas* LabelAtlas::create()
{
LabelAtlas* ret = new (std::nothrow) LabelAtlas();
if (ret)
{
ret->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(ret);
}
return ret;
}
LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
@ -102,6 +89,24 @@ LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& fnt
return ret;
}
LabelAtlas* LabelAtlas::create(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
LabelAtlas* ret = new (std::nothrow) LabelAtlas();
if (ret)
{
if (ret->initWithString(string, texture, itemWidth, itemHeight, startCharMap))
{
ret->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(ret);
}
}
return ret;
}
bool LabelAtlas::initWithString(const std::string& theString, const std::string& fntFile)
{
std::string pathStr = FileUtils::getInstance()->fullPathForFilename(fntFile);

View File

@ -56,12 +56,6 @@ NS_CC_BEGIN
class CC_DLL LabelAtlas : public AtlasNode, public LabelProtocol
{
public:
/**
* Creates an empty LabelAtlas.
* User need to call initWithString(...) later to make this object work properly.
*/
static LabelAtlas* create();
/** Creates the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */
static LabelAtlas* create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
@ -71,6 +65,11 @@ public:
*/
static LabelAtlas* create(const std::string& string, const std::string& fntFile);
/**
* Creates the LabelAtlas with a string, a texture, the width and height of each element and the starting char of the atlas.
*/
static LabelAtlas* create(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
/** Initializes the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */
bool initWithString(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);

View File

@ -281,8 +281,7 @@ MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const st
bool MenuItemAtlasFont::initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
{
CCASSERT( value.size() != 0, "value length must be greater than 0");
LabelAtlas *label = LabelAtlas::create();
label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
LabelAtlas *label = LabelAtlas::create(value, charMapFile, itemWidth, itemHeight, startCharMap);
if (MenuItemLabel::initWithLabel(label, callback))
{
// do something ?

View File

@ -211,40 +211,42 @@ void MotionStreak::setTexture(Texture2D *texture)
_texture = texture;
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, _texture);
if (_texture)
_programState->setTexture(_texture->getBackendTexture());
}
}
void MotionStreak::setProgramState(backend::ProgramState* programState)
bool MotionStreak::attachProgramState(backend::ProgramState* programState)
{
CCASSERT(programState, "argument should not be nullptr");
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
Node::setProgramState(programState);
pipelineDescriptor.programState = _programState;
if (Node::attachProgramState(programState)) {
CCASSERT(programState, "argument should not be nullptr");
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
_mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_texture");
_mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_texture");
auto vertexLayout = _programState->getVertexLayout();
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("a_position");
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
auto vertexLayout = _programState->getVertexLayout();
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("a_position");
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
}
iter = attributeInfo.find("a_texCoord");
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, 2 * sizeof(float), false);
}
iter = attributeInfo.find("a_color");
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, 4 * sizeof(float), true);
}
vertexLayout->setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t));
updateProgramStateTexture(_texture);
return true;
}
iter = attributeInfo.find("a_texCoord");
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, 2 * sizeof(float), false);
}
iter = attributeInfo.find("a_color");
if (iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, 4 * sizeof(float), true);
}
vertexLayout->setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t));
return false;
}
void MotionStreak::setBlendFunc(const BlendFunc &blendFunc)

View File

@ -147,7 +147,7 @@ public:
_startingPositionInitialized = bStartingPositionInitialized;
}
void setProgramState(backend::ProgramState* programState) override;
bool attachProgramState(backend::ProgramState* programState) override;
CC_CONSTRUCTOR_ACCESS:
MotionStreak();

View File

@ -2132,16 +2132,7 @@ void Node::setProgramStateWithRegistry(backend::ProgramType programType, Texture
{
auto formatEXT = texture ? texture->getTextureFormatEXT() : 0;
auto programState = backend::ProgramStateRegistry::getInstance()->newProgramState(programType, formatEXT);
setProgramState(programState);
programState->release();
}
void Node::updateProgramStateTexture(Texture2D* texture)
{
if (texture == nullptr || texture->getBackendTexture() == nullptr || _programState == nullptr)
return;
_programState->setTexture(texture->getBackendTexture());
attachProgramState(programState);
}
void Node::setProgramState(backend::ProgramState* programState)
@ -2161,6 +2152,14 @@ bool Node::attachProgramState(backend::ProgramState* programState)
return false;
}
void Node::updateProgramStateTexture(Texture2D* texture)
{
if (texture == nullptr || texture->getBackendTexture() == nullptr || _programState == nullptr)
return;
_programState->setTexture(texture->getBackendTexture());
}
backend::ProgramState* Node::getProgramState() const
{
return _programState;

View File

@ -1768,13 +1768,22 @@ public:
*/
virtual void setCameraMask(unsigned short mask, bool applyChildren = true);
/**
* Sets ProgramState with retain
* @param programState
*/
virtual void setProgramState(backend::ProgramState* programState);
bool attachProgramState(backend::ProgramState* programState);
backend::ProgramState* getProgramState() const;
void setProgramStateWithRegistry(backend::ProgramType programType, Texture2D* texture);
void updateProgramStateTexture(Texture2D* texture);
virtual backend::ProgramState* getProgramState() const;
/**
* Attach/Change current ProgramState without retain, override me
* @param programState should be new or retain if it has other owner
*/
virtual bool attachProgramState(backend::ProgramState* programState);
void updateProgramStateTexture(Texture2D* texture);
CC_CONSTRUCTOR_ACCESS:
// Nodes should be created using create();

View File

@ -376,9 +376,7 @@ void Sprite::setVertexLayout()
void Sprite::updateShaders(const char* vert, const char* frag)
{
auto* program = backend::Device::getInstance()->newProgram(vert, frag);
auto programState = new (std::nothrow) backend::ProgramState(program);
setProgramState(programState);
CC_SAFE_RELEASE(programState);
attachProgramState(new (std::nothrow) backend::ProgramState(program));
CC_SAFE_RELEASE(program);
}
@ -387,25 +385,25 @@ void Sprite::setProgramState(backend::ProgramType type)
setProgramStateWithRegistry(type, _texture);
}
void Sprite::setProgramState(backend::ProgramState *programState)
bool Sprite::attachProgramState(backend::ProgramState *programState)
{
CCASSERT(programState, "argument should not be nullptr");
auto& pipelineDescriptor = _trianglesCommand.getPipelineDescriptor();
Node::setProgramState(programState);
pipelineDescriptor.programState = _programState;
if (Node::attachProgramState(programState)) {
auto& pipelineDescriptor = _trianglesCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation(backend::Uniform::MVP_MATRIX);
_textureLocation = pipelineDescriptor.programState->getUniformLocation(backend::Uniform::TEXTURE);
_mvpMatrixLocation = _programState->getUniformLocation(backend::Uniform::MVP_MATRIX);
setVertexLayout();
updateProgramStateTexture(_texture);
setMVPMatrixUniform();
setVertexLayout();
updateProgramStateTexture(_texture);
setMVPMatrixUniform();
return true;
}
return false;
}
void Sprite::setTexture(Texture2D *texture)
{
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture);
CCASSERT(! _batchNode || (texture && texture == _batchNode->getTexture()), "CCSprite: Batched sprites should use the same texture as the batchnode");
// accept texture==nil as argument
CCASSERT( !texture || dynamic_cast<Texture2D*>(texture), "setTexture expects a Texture2D. Invalid argument");
@ -437,7 +435,8 @@ void Sprite::setTexture(Texture2D *texture)
}
updateBlendFunc();
}
updateProgramStateTexture(_texture);
setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
}
Texture2D* Sprite::getTexture() const
@ -1729,9 +1728,4 @@ void Sprite::setMVPMatrixUniform()
programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
}
backend::ProgramState* Sprite::getProgramState() const
{
return _programState;
}
NS_CC_END

View File

@ -418,14 +418,9 @@ public:
TextureAtlas* getTextureAtlas() const { return _textureAtlas; }
/**
* Set ProgramState
* Attach new ProgramState
*/
virtual void setProgramState(backend::ProgramState *programState) override;
/**
* Get current ProgramState
*/
virtual backend::ProgramState *getProgramState() const override;
bool attachProgramState(backend::ProgramState *programState) override;
/**
* Sets the weak reference of the TextureAtlas when the sprite is rendered using via SpriteBatchNode.
@ -625,6 +620,7 @@ CC_CONSTRUCTOR_ACCESS :
virtual void setVertexLayout();
virtual void updateShaders(const char* vert, const char* frag);
using Node::setProgramState;
void setProgramState(backend::ProgramType type);
protected:
@ -663,7 +659,6 @@ protected:
TrianglesCommand _trianglesCommand;
backend::UniformLocation _mvpMatrixLocation;
backend::UniformLocation _textureLocation;
#if CC_SPRITE_DEBUG_DRAW
DrawNode *_debugDrawNode = nullptr;
#endif //CC_SPRITE_DEBUG_DRAW

View File

@ -103,7 +103,6 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity/* = DEFAU
_textureAtlas->initWithTexture(tex, capacity);
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, tex);
updateProgramStateTexture(_textureAtlas->getTexture());
updateBlendFunc();
@ -148,15 +147,19 @@ void SpriteBatchNode::setVertexLayout()
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
}
void SpriteBatchNode::setProgramState(backend::ProgramState *programState)
bool SpriteBatchNode::attachProgramState(backend::ProgramState *programState)
{
CCASSERT(programState, "programState should not be nullptr");
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
Node::setProgramState(programState);
pipelineDescriptor.programState = _programState;
setVertexLayout();
setUniformLocation();
if (Node::attachProgramState(programState)) {
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
setVertexLayout();
updateProgramStateTexture(_textureAtlas->getTexture());
setUniformLocation();
return true;
}
return false;
}
bool SpriteBatchNode::init()
@ -694,7 +697,6 @@ void SpriteBatchNode::setTexture(Texture2D *texture)
{
_textureAtlas->setTexture(texture);
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture);
updateProgramStateTexture(texture);
updateBlendFunc();
}

View File

@ -212,9 +212,9 @@ public:
virtual std::string getDescription() const override;
/**
* Set ProgramState
* Attach new ProgramState
*/
virtual void setProgramState(backend::ProgramState *programState) override;
bool attachProgramState(backend::ProgramState *programState) override;
/** Inserts a quad at a certain index into the texture atlas. The Sprite won't be added into the children array.
* This method should be called only when you are dealing with very big AtlasSprite and when most of the Sprite won't be updated.

View File

@ -119,8 +119,8 @@ public:
virtual const BlendFunc &getBlendFunc() const override;
// overrides
/** set ProgramState, you should bind attributes by yourself */
virtual void setProgramState(backend::ProgramState *programState) override;
/** Sets ProgramState, you should bind attributes by yourself */
void setProgramState(backend::ProgramState *programState) override;
/*
* Get AABB

View File

@ -1275,22 +1275,19 @@ void Director::createStatsLabel()
*/
float scaleFactor = 1 / CC_CONTENT_SCALE_FACTOR();
_FPSLabel = LabelAtlas::create();
_FPSLabel = LabelAtlas::create(fpsString, texture, 12, 32, '.');
_FPSLabel->retain();
_FPSLabel->setIgnoreContentScaleFactor(true);
_FPSLabel->initWithString(fpsString, texture, 12, 32 , '.');
_FPSLabel->setScale(scaleFactor);
_drawnBatchesLabel = LabelAtlas::create();
_drawnBatchesLabel = LabelAtlas::create(drawBatchString, texture, 12, 32, '.');
_drawnBatchesLabel->retain();
_drawnBatchesLabel->setIgnoreContentScaleFactor(true);
_drawnBatchesLabel->initWithString(drawBatchString, texture, 12, 32, '.');
_drawnBatchesLabel->setScale(scaleFactor);
_drawnVerticesLabel = LabelAtlas::create();
_drawnVerticesLabel = LabelAtlas::create(drawVerticesString, texture, 12, 32, '.');
_drawnVerticesLabel->retain();
_drawnVerticesLabel->setIgnoreContentScaleFactor(true);
_drawnVerticesLabel->initWithString(drawVerticesString, texture, 12, 32, '.');
_drawnVerticesLabel->setScale(scaleFactor);

View File

@ -56366,16 +56366,6 @@ int lua_cocos2dx_LabelAtlas_create(lua_State* tolua_S)
} while (0);
ok = true;
do
{
if (argc == 0)
{
cocos2d::LabelAtlas* ret = cocos2d::LabelAtlas::create();
object_to_luaval<cocos2d::LabelAtlas>(tolua_S, "cc.LabelAtlas",(cocos2d::LabelAtlas*)ret);
return 1;
}
} while (0);
ok = true;
do
{
if (argc == 2)
{

View File

@ -975,7 +975,7 @@ public:
sprite->setProgramState(programState);
return sprite;
}
virtual void setProgramState(backend::ProgramState* programState) override;
bool attachProgramState(backend::ProgramState* programState) override;
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
protected:
@ -983,16 +983,19 @@ protected:
};
void MySprite::setProgramState(backend::ProgramState* programState)
bool MySprite::attachProgramState(backend::ProgramState* programState)
{
Sprite::setProgramState(programState);
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
pipelineDescriptor.programState = programState;
if (Sprite::attachProgramState(programState)) {
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
pipelineDescriptor.programState = programState;
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
_customCommand.createVertexBuffer(sizeof(V3F_C4B_T2F), 4, CustomCommand::BufferUsage::STATIC);
_customCommand.updateVertexBuffer(&_quad, 4*sizeof(V3F_C4B_T2F));
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
_customCommand.createVertexBuffer(sizeof(V3F_C4B_T2F), 4, CustomCommand::BufferUsage::STATIC);
_customCommand.updateVertexBuffer(&_quad, 4 * sizeof(V3F_C4B_T2F));
return true;
}
return false;
}
void MySprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)