| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- /****************************************************************************
- Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
- http://www.cocos2d-x.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "Assembler.hpp"
- #include "../NodeProxy.hpp"
- #include "../ModelBatcher.hpp"
- #include "../MeshBuffer.hpp"
- #include "../../renderer/Scene.h"
- #include "math/CCMath.h"
- #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
- #include "cocos/scripting/js-bindings/manual/jsb_conversions.hpp"
- #include "cocos/scripting/js-bindings/auto/jsb_renderer_auto.hpp"
- #include "../RenderFlow.hpp"
- RENDERER_BEGIN
- Assembler::IARenderData::IARenderData()
- {
-
- }
- Assembler::IARenderData::IARenderData(const IARenderData& o)
- {
- meshIndex = o.meshIndex;
- verticesStart = o.verticesStart;
- verticesCount = o.verticesCount;
- indicesStart = o.indicesStart;
- indicesCount = o.indicesCount;
- setEffect(o.getEffect());
- }
- Assembler::IARenderData::~IARenderData()
- {
- CC_SAFE_RELEASE(_effect);
- }
- void Assembler::IARenderData::setEffect(EffectVariant* effect)
- {
- if (effect == _effect) return;
- CC_SAFE_RELEASE(_effect);
- _effect = effect;
- CC_SAFE_RETAIN(_effect);
- }
- EffectVariant* Assembler::IARenderData::getEffect() const
- {
- return _effect;
- }
- Assembler::Assembler()
- {
-
- }
- Assembler::~Assembler()
- {
- CC_SAFE_RELEASE_NULL(_datas);
- CC_SAFE_RELEASE(_vfmt);
- }
- void Assembler::updateMeshIndex(std::size_t iaIndex, int meshIndex)
- {
- if (iaIndex >= _iaDatas.size())
- {
- _iaDatas.resize(iaIndex + 1);
- }
- IARenderData& ia = _iaDatas[iaIndex];
- ia.meshIndex = meshIndex;
- }
- void Assembler::updateIndicesRange(std::size_t iaIndex, int start, int count)
- {
- if (iaIndex >= _iaDatas.size())
- {
- _iaDatas.resize(iaIndex + 1);
- }
- IARenderData& ia = _iaDatas[iaIndex];
- ia.indicesStart = start;
- ia.indicesCount = count;
- }
- void Assembler::updateVerticesRange(std::size_t iaIndex, int start, int count)
- {
- if (iaIndex >= _iaDatas.size())
- {
- _iaDatas.resize(iaIndex + 1);
- }
- IARenderData& ia = _iaDatas[iaIndex];
- ia.verticesStart = start;
- ia.verticesCount = count;
-
- enableDirty(AssemblerBase::VERTICES_OPACITY_CHANGED);
- }
- void Assembler::updateEffect(std::size_t iaIndex, EffectVariant* effect)
- {
- if (iaIndex >= _iaDatas.size())
- {
- _iaDatas.resize(iaIndex + 1);
- }
- IARenderData& ia = _iaDatas[iaIndex];
- ia.setEffect(effect);
- }
- void Assembler::reset()
- {
- _iaDatas.clear();
- }
- void Assembler::handle(NodeProxy *node, ModelBatcher* batcher, Scene* scene)
- {
- batcher->commit(node, this, node->getCullingMask());
- }
- void Assembler::fillBuffers(NodeProxy* node, ModelBatcher* batcher, std::size_t index)
- {
- if(!_datas || !_vfmt)
- {
- return;
- }
-
- MeshBuffer* buffer = batcher->getBuffer(_vfmt);
-
- const IARenderData& ia = _iaDatas[index];
- std::size_t meshIndex = ia.meshIndex >= 0 ? ia.meshIndex : index;
-
- RenderData* data = _datas->getRenderData(meshIndex);
- if (!data)
- {
- return;
- }
-
- CCASSERT(data->getVBytes() % _bytesPerVertex == 0, "Assembler::fillBuffers vertices data doesn't follow vertex format");
- uint32_t vertexCount = ia.verticesCount >= 0 ? (uint32_t)ia.verticesCount : (uint32_t)data->getVBytes() / _bytesPerVertex;
- uint32_t indexCount = ia.indicesCount >= 0 ? (uint32_t)ia.indicesCount : (uint32_t)data->getIBytes() / sizeof(unsigned short);
- uint32_t vertexStart = (uint32_t)ia.verticesStart;
-
- // must retrieve offset before request
- auto& bufferOffset = buffer->request(vertexCount, indexCount);
- uint32_t vBufferOffset = bufferOffset.vByte / sizeof(float);
- uint32_t indexId = bufferOffset.index;
- uint32_t vertexId = bufferOffset.vertex;
- uint32_t vertexOffset = vertexId - vertexStart;
- uint32_t num = _vfPos->num;
- float* worldVerts = buffer->vData + vBufferOffset;
- memcpy(worldVerts, data->getVertices() + vertexStart * _bytesPerVertex, vertexCount * _bytesPerVertex);
-
- // Calculate vertices world positions
- if (!_useModel && !_ignoreWorldMatrix)
- {
- size_t dataPerVertex = _bytesPerVertex / sizeof(float);
- float* ptrPos = worldVerts + _posOffset;
- auto& worldMat = node->getWorldMatrix();
-
- switch (num) {
- // Vertex is X Y Z Format
- case 3:
- for (uint32_t i = 0; i < vertexCount; ++i)
- {
- ((cocos2d::Vec3*)ptrPos)->transformMat4(*((cocos2d::Vec3*)ptrPos), worldMat);
- ptrPos += dataPerVertex;
- }
- break;
- // Vertex is X Y Format
- case 2:
- for (uint32_t i = 0; i < vertexCount; ++i)
- {
- float z = ptrPos[2];
- ptrPos[2] = 0;
- worldMat.transformPoint((cocos2d::Vec3*)ptrPos);
- ptrPos[2] = z;
- ptrPos += dataPerVertex;
- }
- break;
- }
- }
-
- // Copy index buffer with vertex offset
- uint16_t* indices = (uint16_t*)data->getIndices();
- uint16_t* dst = buffer->iData;
- for (auto i = 0, j = ia.indicesStart; i < indexCount; ++i, ++j)
- {
- dst[indexId++] = vertexOffset + indices[j];
- }
- }
- void Assembler::setVertexFormat(VertexFormat* vfmt)
- {
- if (_vfmt == vfmt) return;
- CC_SAFE_RETAIN(vfmt);
- CC_SAFE_RELEASE(_vfmt);
- _vfmt = vfmt;
- if (_vfmt)
- {
- _bytesPerVertex = _vfmt->getBytes();
- _vfPos = _vfmt->getElement(ATTRIB_NAME_POSITION_HASH);
- _posOffset = _vfPos->offset / 4;
- _vfColor = _vfmt->getElement(ATTRIB_NAME_COLOR_HASH);
- if (_vfColor != nullptr)
- {
- _alphaOffset = _vfColor->offset + 3;
- }
- }
- }
- void Assembler::setRenderDataList(RenderDataList* datas)
- {
- if (_datas == datas) return;
- CC_SAFE_RELEASE(_datas);
- _datas = datas;
- CC_SAFE_RETAIN(_datas);
- }
- void Assembler::updateOpacity(std::size_t index, uint8_t opacity)
- {
- // has no color info in vertex buffer
- if(!_vfColor || !_datas || !_vfmt)
- {
- return;
- }
-
- const IARenderData& ia = _iaDatas[index];
- std::size_t meshIndex = ia.meshIndex >= 0 ? ia.meshIndex : index;
-
- RenderData* data = _datas->getRenderData(meshIndex);
- if (!data)
- {
- return;
- }
-
- CCASSERT(data->getVBytes() % _bytesPerVertex == 0, "Assembler::updateOpacity vertices data doesn't follow vertex format");
- uint32_t vertexCount = (uint32_t)data->getVBytes() / _bytesPerVertex;
-
- size_t dataPerVertex = _bytesPerVertex / sizeof(uint8_t);
- uint8_t* ptrAlpha = (uint8_t*)data->getVertices() + _alphaOffset;
- const Vector<Pass*>& passes = ia.getEffect()->getPasses();
- if (passes.at(0)->getBlendSrc() == BlendFactor::ONE)
- {
- float alpha = opacity / 255.0;
- for (uint32_t i = 0; i < vertexCount; ++i)
- {
- *(ptrAlpha-1) *= alpha;
- *(ptrAlpha-2) *= alpha;
- *(ptrAlpha-3) *= alpha;
-
- *ptrAlpha = opacity;
- ptrAlpha += dataPerVertex;
- }
- }
- else
- {
- for (uint32_t i = 0; i < vertexCount; ++i)
- {
- *ptrAlpha = opacity;
- ptrAlpha += dataPerVertex;
- }
- }
-
- *_dirty &= ~VERTICES_OPACITY_CHANGED;
- }
- RENDERER_END
|