axmol/cocos/renderer/CCTrianglesCommand.cpp

114 lines
4.2 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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 "renderer/CCTrianglesCommand.h"
#include "xxhash.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTexture2D.h"
#include "base//ccUtils.h"
NS_CC_BEGIN
TrianglesCommand::TrianglesCommand()
{
_type = RenderCommand::Type::TRIANGLES_COMMAND;
}
2021-12-25 10:04:45 +08:00
void TrianglesCommand::init(float globalOrder,
Texture2D* texture,
const BlendFunc& blendType,
const Triangles& triangles,
const Mat4& mv,
uint32_t flags)
2019-11-23 20:27:39 +08:00
{
RenderCommand::init(globalOrder, mv, flags);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_triangles = triangles;
2021-12-25 10:04:45 +08:00
if (_triangles.indexCount % 3 != 0)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
unsigned int count = _triangles.indexCount;
2019-11-23 20:27:39 +08:00
_triangles.indexCount = count / 3 * 3;
CCLOGERROR("Resize indexCount from %d to %d, size must be multiple times of 3", count, _triangles.indexCount);
}
_mv = mv;
2020-10-23 11:37:52 +08:00
auto programType = _pipelineDescriptor.programState->getProgram()->getProgramType();
2021-12-25 10:04:45 +08:00
auto uniformID = _pipelineDescriptor.programState->getUniformID();
if (_programType != programType || _texture != texture->getBackendTexture() || _blendType != blendType ||
_uniformID != uniformID)
2019-11-23 20:27:39 +08:00
{
2020-10-23 11:37:52 +08:00
_programType = programType;
2021-12-25 10:04:45 +08:00
_texture = texture->getBackendTexture();
_blendType = blendType;
_uniformID = uniformID;
2021-12-25 10:04:45 +08:00
// since it would be too expensive to check the uniforms, simplify enable batching for built-in program.
if (_programType == backend::ProgramType::CUSTOM_PROGRAM)
setSkipBatching(true);
2021-12-25 10:04:45 +08:00
// TODO: minggo set it in Node?
auto& blendDescriptor = _pipelineDescriptor.blendDescriptor;
blendDescriptor.blendEnabled = true;
2019-11-23 20:27:39 +08:00
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor = blendType.src;
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor = blendType.dst;
2021-12-25 10:04:45 +08:00
if (!isSkipBatching())
2019-11-23 20:27:39 +08:00
generateMaterialID();
}
}
void TrianglesCommand::updateMaterialID()
{
setSkipBatching(false);
generateMaterialID();
}
2021-12-25 10:04:45 +08:00
TrianglesCommand::~TrianglesCommand() {}
2019-11-23 20:27:39 +08:00
void TrianglesCommand::generateMaterialID()
{
struct
{
void* texture;
uint32_t programType;
2020-10-23 11:37:52 +08:00
uint32_t uniformID;
2019-11-23 20:27:39 +08:00
backend::BlendFactor src;
backend::BlendFactor dst;
2021-12-25 10:04:45 +08:00
} hashMe;
2019-11-23 20:27:39 +08:00
// NOTE: Initialize hashMe struct to make the value of padding bytes be filled with zero.
// It's important since XXH32 below will also consider the padding bytes which probably
// are set to random values by different compilers.
memset(&hashMe, 0, sizeof(hashMe));
2021-12-25 10:04:45 +08:00
hashMe.texture = _texture;
hashMe.src = _blendType.src;
hashMe.dst = _blendType.dst;
hashMe.programType = _programType;
2021-12-25 10:04:45 +08:00
hashMe.uniformID = _uniformID;
_materialID = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
2019-11-23 20:27:39 +08:00
}
NS_CC_END