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
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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.
|
|
|
|
****************************************************************************/
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "renderer/TrianglesCommand.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "xxhash.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/Texture2D.h"
|
|
|
|
#include "base//Utils.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
|
|
|
|
|
|
|
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;
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGERROR("Resize indexCount from %d to %d, size must be multiple times of 3", count, _triangles.indexCount);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
_mv = mv;
|
|
|
|
|
2023-08-13 23:58:18 +08:00
|
|
|
auto programType = _pipelineDescriptor.programState->getProgram()->getProgramType();
|
|
|
|
auto uniformID = _pipelineDescriptor.programState->getUniformID();
|
|
|
|
if (_programType != programType || _texture != texture->getBackendTexture() || _blendType != blendType ||
|
|
|
|
_uniformID != uniformID)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-08-13 23:58:18 +08:00
|
|
|
_programType = programType;
|
|
|
|
_texture = texture->getBackendTexture();
|
|
|
|
_blendType = blendType;
|
|
|
|
_uniformID = uniformID;
|
|
|
|
|
|
|
|
// 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);
|
2023-07-25 09:13:20 +08:00
|
|
|
|
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;
|
2023-08-13 23:58:18 +08:00
|
|
|
uint32_t programType;
|
|
|
|
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));
|
|
|
|
|
2023-08-13 23:58:18 +08:00
|
|
|
hashMe.texture = _texture;
|
|
|
|
hashMe.src = _blendType.src;
|
|
|
|
hashMe.dst = _blendType.dst;
|
|
|
|
hashMe.programType = _programType;
|
|
|
|
hashMe.uniformID = _uniformID;
|
|
|
|
_materialID = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|