2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
Copyright 2013 BlackBerry Inc.
|
|
|
|
Copyright (c) 2015-2017 Chukong Technologies
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "renderer/backend/Program.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "renderer/Pass.h"
|
|
|
|
#include "base/Configuration.h"
|
|
|
|
#include "3d/MeshVertexIndexData.h"
|
|
|
|
#include "3d/3DProgramInfo.h"
|
|
|
|
#include "3d/VertexAttribBinding.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static std::vector<VertexAttribBinding*> __vertexAttribBindingCache;
|
|
|
|
|
2022-09-24 11:38:41 +08:00
|
|
|
VertexAttribBinding::VertexAttribBinding() : _meshIndexData(nullptr), _programState(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
VertexAttribBinding::~VertexAttribBinding()
|
|
|
|
{
|
|
|
|
// Delete from the vertex attribute binding cache.
|
2021-12-25 10:04:45 +08:00
|
|
|
std::vector<VertexAttribBinding*>::iterator itr =
|
|
|
|
std::find(__vertexAttribBindingCache.begin(), __vertexAttribBindingCache.end(), this);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (itr != __vertexAttribBindingCache.end())
|
|
|
|
{
|
|
|
|
__vertexAttribBindingCache.erase(itr);
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_meshIndexData);
|
|
|
|
AX_SAFE_RELEASE(_programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
VertexAttribBinding* VertexAttribBinding::create(MeshIndexData* meshIndexData, Pass* pass, MeshCommand* command)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(meshIndexData && pass && pass->getProgramState(), "Invalid MeshIndexData and/or programState");
|
2019-11-23 20:27:39 +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];
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_ASSERT(b);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (b->_meshIndexData == meshIndexData && b->_programState == pass->getProgramState())
|
|
|
|
{
|
|
|
|
// Found a match!
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
b = new VertexAttribBinding();
|
|
|
|
if (b->init(meshIndexData, pass, command))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
b->autorelease();
|
2022-08-08 13:18:33 +08:00
|
|
|
__vertexAttribBindingCache.emplace_back(b);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool VertexAttribBinding::init(MeshIndexData* meshIndexData, Pass* pass, MeshCommand* command)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(meshIndexData && pass && pass->getProgramState(), "Invalid arguments");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_meshIndexData = meshIndexData;
|
|
|
|
_meshIndexData->retain();
|
2022-09-24 11:01:45 +08:00
|
|
|
_programState = pass->getProgramState();
|
2019-11-23 20:27:39 +08:00
|
|
|
_programState->retain();
|
|
|
|
|
|
|
|
auto meshVertexData = meshIndexData->getMeshVertexData();
|
|
|
|
auto attributeCount = meshVertexData->getMeshVertexAttribCount();
|
|
|
|
|
|
|
|
// Parse and set attributes
|
|
|
|
parseAttributes();
|
|
|
|
int offset = 0;
|
2023-08-31 21:20:23 +08:00
|
|
|
auto vertexLayout = _programState->getMutableVertexLayout();
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto k = 0; k < attributeCount; k++)
|
|
|
|
{
|
|
|
|
auto meshattribute = meshVertexData->getMeshVertexAttrib(k);
|
2023-08-31 21:20:23 +08:00
|
|
|
setVertexAttribPointer(vertexLayout, shaderinfos::getAttributeName(meshattribute.vertexAttrib),
|
|
|
|
meshattribute.type, false,
|
2021-12-25 10:04:45 +08:00
|
|
|
offset, 1 << k);
|
2019-11-23 20:27:39 +08:00
|
|
|
offset += meshattribute.getAttribSizeBytes();
|
|
|
|
}
|
|
|
|
|
2023-08-31 21:20:23 +08:00
|
|
|
vertexLayout->setStride(offset);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(offset == meshVertexData->getSizePerVertex(), "vertex layout mismatch!");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t VertexAttribBinding::getVertexAttribsFlags() const
|
|
|
|
{
|
|
|
|
return _vertexAttribsFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexAttribBinding::parseAttributes()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_programState, "invalid glprogram");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_vertexAttribsFlags = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool VertexAttribBinding::hasAttribute(const shaderinfos::VertexKey& key) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& name = shaderinfos::getAttributeName(key);
|
2022-09-24 11:38:41 +08:00
|
|
|
auto& attributes = _programState->getProgram()->getActiveAttributes();
|
|
|
|
return attributes.find(name) != attributes.end();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-09-24 11:38:41 +08:00
|
|
|
const backend::AttributeBindInfo* VertexAttribBinding::getVertexAttribValue(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-09-24 11:38:41 +08:00
|
|
|
auto& attributes = _programState->getProgram()->getActiveAttributes();
|
|
|
|
const auto itr = attributes.find(name);
|
|
|
|
if (itr != attributes.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
return &itr->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-08-31 21:20:23 +08:00
|
|
|
void VertexAttribBinding::setVertexAttribPointer(VertexLayout* vertexLayout,
|
|
|
|
std::string_view name,
|
2021-12-25 10:04:45 +08:00
|
|
|
backend::VertexFormat type,
|
|
|
|
bool normalized,
|
|
|
|
int offset,
|
|
|
|
int flag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto v = getVertexAttribValue(name);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (v)
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
// AXLOG("axmol: set attribute '%s' location: %d, offset: %d", name.c_str(), v->location, offset);
|
2023-08-31 21:20:23 +08:00
|
|
|
vertexLayout->setAttrib(name, v->location, type, offset, normalized);
|
2019-11-23 20:27:39 +08:00
|
|
|
_vertexAttribsFlags |= flag;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
// AXLOG("axmol: warning: Attribute not found: %s", name.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|