2015-05-13 10:58:09 +08:00
|
|
|
/**
|
|
|
|
Copyright 2013 BlackBerry Inc.
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2015-2017 Chukong Technologies
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2015-05-13 10:58:09 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
Original file from GamePlay3D: http://gameplay3d.org
|
|
|
|
|
|
|
|
This file was modified to fit the cocos2d-x project
|
|
|
|
*/
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
#include "renderer/backend/Program.h"
|
|
|
|
#include "renderer/CCPass.h"
|
2015-05-13 10:58:09 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
#include "3d/CCMeshVertexIndexData.h"
|
2019-01-30 09:35:17 +08:00
|
|
|
#include "3d/CC3DProgramInfo.h"
|
2019-02-15 14:30:10 +08:00
|
|
|
#include "3d/CCVertexAttribBinding.h"
|
2015-05-13 10:58:09 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
|
2019-06-05 17:58:33 +08:00
|
|
|
static uint32_t __maxVertexAttribs = 0;
|
2015-05-13 10:58:09 +08:00
|
|
|
static std::vector<VertexAttribBinding*> __vertexAttribBindingCache;
|
|
|
|
|
|
|
|
VertexAttribBinding::VertexAttribBinding() :
|
2019-06-05 17:58:33 +08:00
|
|
|
_meshIndexData(nullptr), _programState(nullptr), _attributes()
|
2015-05-13 10:58:09 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexAttribBinding::~VertexAttribBinding()
|
|
|
|
{
|
|
|
|
// Delete from the vertex attribute binding cache.
|
|
|
|
std::vector<VertexAttribBinding*>::iterator itr = std::find(__vertexAttribBindingCache.begin(), __vertexAttribBindingCache.end(), this);
|
|
|
|
if (itr != __vertexAttribBindingCache.end())
|
|
|
|
{
|
|
|
|
__vertexAttribBindingCache.erase(itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_RELEASE(_meshIndexData);
|
2019-01-30 09:35:17 +08:00
|
|
|
CC_SAFE_RELEASE(_programState);
|
2015-05-13 10:58:09 +08:00
|
|
|
_attributes.clear();
|
|
|
|
}
|
|
|
|
|
2019-03-14 16:22:54 +08:00
|
|
|
VertexAttribBinding* VertexAttribBinding::create(MeshIndexData* meshIndexData, Pass* pass, MeshCommand *command)
|
2015-05-13 10:58:09 +08:00
|
|
|
{
|
2019-01-30 09:35:17 +08:00
|
|
|
CCASSERT(meshIndexData && pass && pass->getProgramState(), "Invalid MeshIndexData and/or programState");
|
2015-05-13 10:58:09 +08:00
|
|
|
|
|
|
|
// Search for an existing vertex attribute binding that can be used.
|
|
|
|
VertexAttribBinding* b;
|
|
|
|
for (size_t i = 0, count = __vertexAttribBindingCache.size(); i < count; ++i)
|
|
|
|
{
|
|
|
|
b = __vertexAttribBindingCache[i];
|
|
|
|
CC_ASSERT(b);
|
2019-01-30 09:35:17 +08:00
|
|
|
if (b->_meshIndexData == meshIndexData && b->_programState == pass->getProgramState())
|
2015-05-13 10:58:09 +08:00
|
|
|
{
|
|
|
|
// Found a match!
|
2019-03-14 16:22:54 +08:00
|
|
|
command->getPipelineDescriptor().vertexLayout = *b->_vertexLayout;
|
2015-05-13 10:58:09 +08:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b = new (std::nothrow) VertexAttribBinding();
|
2019-03-14 16:22:54 +08:00
|
|
|
if (b && b->init(meshIndexData, pass, command))
|
2015-05-13 10:58:09 +08:00
|
|
|
{
|
|
|
|
b->autorelease();
|
|
|
|
__vertexAttribBindingCache.push_back(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2019-03-14 16:22:54 +08:00
|
|
|
bool VertexAttribBinding::init(MeshIndexData* meshIndexData, Pass* pass, MeshCommand *command)
|
2015-05-13 10:58:09 +08:00
|
|
|
{
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
CCASSERT(meshIndexData && pass && pass->getProgramState(), "Invalid arguments");
|
2015-05-13 10:58:09 +08:00
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
auto programState = pass->getProgramState();
|
|
|
|
|
2019-03-14 16:22:54 +08:00
|
|
|
_vertexLayout = &command->getPipelineDescriptor().vertexLayout;
|
2019-01-30 09:35:17 +08:00
|
|
|
|
2015-05-13 10:58:09 +08:00
|
|
|
_meshIndexData = meshIndexData;
|
|
|
|
_meshIndexData->retain();
|
2019-01-30 09:35:17 +08:00
|
|
|
_programState = programState;
|
|
|
|
_programState->retain();
|
2015-05-13 10:58:09 +08:00
|
|
|
|
|
|
|
auto meshVertexData = meshIndexData->getMeshVertexData();
|
|
|
|
auto attributeCount = meshVertexData->getMeshVertexAttribCount();
|
|
|
|
|
|
|
|
|
|
|
|
// Parse and set attributes
|
|
|
|
parseAttributes();
|
2019-01-30 09:35:17 +08:00
|
|
|
int offset = 0;
|
2015-05-13 10:58:09 +08:00
|
|
|
for (auto k = 0; k < attributeCount; k++)
|
|
|
|
{
|
|
|
|
auto meshattribute = meshVertexData->getMeshVertexAttrib(k);
|
|
|
|
setVertexAttribPointer(
|
2019-01-30 09:35:17 +08:00
|
|
|
shaderinfos::getAttributeName(meshattribute.vertexAttrib),
|
2015-05-13 10:58:09 +08:00
|
|
|
meshattribute.type,
|
2019-06-05 17:58:33 +08:00
|
|
|
false,
|
2019-02-15 14:30:10 +08:00
|
|
|
offset,
|
2019-05-05 15:26:34 +08:00
|
|
|
1 << k
|
2019-02-15 14:30:10 +08:00
|
|
|
);
|
2019-01-30 09:35:17 +08:00
|
|
|
offset += meshattribute.getAttribSizeBytes();
|
2015-05-13 10:58:09 +08:00
|
|
|
}
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
_vertexLayout->setLayout(offset, backend::VertexStepMode::VERTEX);
|
2015-05-13 10:58:09 +08:00
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
CCASSERT(offset == meshVertexData->getSizePerVertex(), "vertex layout mismatch!");
|
2015-05-13 10:58:09 +08:00
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
return true;
|
2015-05-13 10:58:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t VertexAttribBinding::getVertexAttribsFlags() const
|
|
|
|
{
|
|
|
|
return _vertexAttribsFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexAttribBinding::parseAttributes()
|
|
|
|
{
|
2019-01-30 09:35:17 +08:00
|
|
|
CCASSERT(_programState, "invalid glprogram");
|
2015-05-13 10:58:09 +08:00
|
|
|
|
|
|
|
_attributes.clear();
|
|
|
|
_vertexAttribsFlags = 0;
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
auto program = _programState->getProgram();
|
2019-03-12 19:40:13 +08:00
|
|
|
auto& attributes = program->getActiveAttributes();
|
|
|
|
_attributes = attributes;
|
2018-09-11 14:39:30 +08:00
|
|
|
}
|
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
bool VertexAttribBinding::hasAttribute(const shaderinfos::VertexKey &key) const
|
|
|
|
{
|
|
|
|
auto &name = shaderinfos::getAttributeName(key);
|
|
|
|
return _attributes.find(name) != _attributes.end();
|
|
|
|
}
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
backend::AttributeBindInfo* VertexAttribBinding::getVertexAttribValue(const std::string& name)
|
2015-05-13 10:58:09 +08:00
|
|
|
{
|
|
|
|
const auto itr = _attributes.find(name);
|
|
|
|
if( itr != _attributes.end())
|
|
|
|
return &itr->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-05 17:58:33 +08:00
|
|
|
void VertexAttribBinding::setVertexAttribPointer(const std::string &name, backend::VertexFormat type, bool normalized, int offset, int flag)
|
2015-05-13 10:58:09 +08:00
|
|
|
{
|
|
|
|
auto v = getVertexAttribValue(name);
|
|
|
|
if(v) {
|
2019-02-15 14:30:10 +08:00
|
|
|
// CCLOG("cocos2d: set attribute '%s' location: %d, offset: %d", name.c_str(), v->location, offset);
|
2019-05-05 15:26:34 +08:00
|
|
|
_vertexLayout->setAttribute(name, v->location, type, offset, normalized);
|
2019-02-15 14:30:10 +08:00
|
|
|
_vertexAttribsFlags |= flag;
|
2015-05-13 10:58:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-08 09:07:37 +08:00
|
|
|
// CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str());
|
2015-05-13 10:58:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|