2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
2022-09-24 10:42:11 +08:00
|
|
|
Copyright (c) 2022 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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:
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
#include "ProgramManager.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "Device.h"
|
|
|
|
#include "ShaderModule.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "renderer/Shaders.h"
|
|
|
|
#include "base/Macros.h"
|
|
|
|
#include "base/Configuration.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
ProgramManager* ProgramManager::_sharedProgramManager = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
Program* ProgramManager::newProgram(std::string_view vertShaderSource,
|
2023-07-19 23:41:16 +08:00
|
|
|
std::string_view fragShaderSource,
|
|
|
|
std::function<void(Program*)> fnSetupLayout)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-10-12 19:44:31 +08:00
|
|
|
auto program = Device::getInstance()->newProgram(vertShaderSource, fragShaderSource);
|
|
|
|
if (program)
|
|
|
|
fnSetupLayout(program);
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramManager* ProgramManager::getInstance()
|
|
|
|
{
|
|
|
|
if (!_sharedProgramManager)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-10-12 19:44:31 +08:00
|
|
|
_sharedProgramManager = new ProgramManager();
|
|
|
|
if (!_sharedProgramManager->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-10-12 19:44:31 +08:00
|
|
|
AX_SAFE_DELETE(_sharedProgramManager);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 19:44:31 +08:00
|
|
|
return _sharedProgramManager;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
void ProgramManager::destroyInstance()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-10-12 19:44:31 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_sharedProgramManager);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
ProgramManager::~ProgramManager()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& program : _cachedPrograms)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(program.second);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-10-12 19:44:31 +08:00
|
|
|
AXLOGINFO("deallocing ProgramManager: %p", this);
|
|
|
|
backend::ShaderCache::destroyInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-09-23 22:41:30 +08:00
|
|
|
/*
|
|
|
|
* shader vertex layout setup functions
|
|
|
|
*/
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupDummy(Program*) {}
|
|
|
|
void VertexLayoutHelper::setupTexture(Program* program)
|
|
|
|
{
|
2022-09-23 22:41:30 +08:00
|
|
|
auto vertexLayout = program->getVertexLayout();
|
|
|
|
|
|
|
|
/// a_position
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT2, 0, false);
|
|
|
|
/// a_texCoord
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
|
|
|
|
program->getAttributeLocation(backend::Attribute::TEXCOORD),
|
|
|
|
backend::VertexFormat::FLOAT2, 2 * sizeof(float), false);
|
|
|
|
|
|
|
|
vertexLayout->setStride(4 * sizeof(float));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupSprite(Program* program)
|
2022-09-23 22:41:30 +08:00
|
|
|
{
|
|
|
|
auto vertexLayout = program->getVertexLayout();
|
|
|
|
|
|
|
|
/// a_position
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT3, 0, false);
|
|
|
|
/// a_texCoord
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
|
|
|
|
program->getAttributeLocation(backend::Attribute::TEXCOORD),
|
|
|
|
backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords), false);
|
|
|
|
|
|
|
|
/// a_color
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, program->getAttributeLocation(backend::Attribute::COLOR),
|
|
|
|
backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors), true);
|
|
|
|
vertexLayout->setStride(sizeof(V3F_C4B_T2F));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupDrawNode(Program* program)
|
2022-09-23 22:41:30 +08:00
|
|
|
{
|
|
|
|
auto vertexLayout = program->getVertexLayout();
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT2, 0, false);
|
2022-09-23 22:41:30 +08:00
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
|
|
|
|
program->getAttributeLocation(backend::Attribute::TEXCOORD),
|
|
|
|
backend::VertexFormat::FLOAT2, offsetof(V2F_C4B_T2F, texCoords), false);
|
|
|
|
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, program->getAttributeLocation(backend::Attribute::COLOR),
|
|
|
|
backend::VertexFormat::UBYTE4, offsetof(V2F_C4B_T2F, colors), true);
|
2022-09-23 22:41:30 +08:00
|
|
|
|
|
|
|
vertexLayout->setStride(sizeof(V2F_C4B_T2F));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupDrawNode3D(Program* program)
|
2022-09-23 22:41:30 +08:00
|
|
|
{
|
|
|
|
auto vertexLayout = program->getVertexLayout();
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT3, 0, false);
|
|
|
|
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, program->getAttributeLocation(backend::Attribute::COLOR),
|
|
|
|
backend::VertexFormat::UBYTE4, sizeof(Vec3), true);
|
|
|
|
|
2022-09-23 22:41:30 +08:00
|
|
|
vertexLayout->setStride(sizeof(V3F_C4B));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupSkyBox(Program* program)
|
2022-09-23 22:41:30 +08:00
|
|
|
{
|
|
|
|
auto vertexLayout = program->getVertexLayout();
|
|
|
|
auto attrNameLoc = program->getAttributeLocation(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION);
|
|
|
|
vertexLayout->setAttribute(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION, attrNameLoc,
|
|
|
|
backend::VertexFormat::FLOAT3, 0, false);
|
|
|
|
vertexLayout->setStride(sizeof(Vec3));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupPU3D(Program* program)
|
2022-09-23 22:41:30 +08:00
|
|
|
{
|
2022-09-24 10:42:11 +08:00
|
|
|
auto vertexLayout = program->getVertexLayout();
|
|
|
|
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT3, offsetof(V3F_T2F_C4F, position), false);
|
|
|
|
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
|
|
|
|
program->getAttributeLocation(backend::Attribute::TEXCOORD),
|
|
|
|
backend::VertexFormat::FLOAT2, offsetof(V3F_T2F_C4F, uv), false);
|
|
|
|
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, program->getAttributeLocation(backend::Attribute::COLOR),
|
|
|
|
backend::VertexFormat::FLOAT4, offsetof(V3F_T2F_C4F, color), false);
|
|
|
|
|
2022-09-23 22:41:30 +08:00
|
|
|
vertexLayout->setStride(sizeof(V3F_T2F_C4F));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupPos(Program* program)
|
2022-09-23 22:41:30 +08:00
|
|
|
{
|
2022-09-24 10:42:11 +08:00
|
|
|
auto vertexLayout = program->getVertexLayout();
|
2022-09-23 22:41:30 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT2, 0, false);
|
|
|
|
vertexLayout->setStride(sizeof(Vec2));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupPosColor(Program* program)
|
2022-09-23 22:41:30 +08:00
|
|
|
{
|
|
|
|
auto vertexLayout = program->getVertexLayout();
|
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT3, 0, false);
|
2022-09-24 10:42:11 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, program->getAttributeLocation(backend::Attribute::COLOR),
|
2022-09-23 22:41:30 +08:00
|
|
|
backend::VertexFormat::FLOAT4, offsetof(V3F_C4F, colors), false);
|
|
|
|
vertexLayout->setStride(sizeof(V3F_C4F));
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:42:11 +08:00
|
|
|
void VertexLayoutHelper::setupTerrain3D(Program* program)
|
|
|
|
{
|
2022-09-23 22:41:30 +08:00
|
|
|
auto vertexLayout = program->getVertexLayout();
|
2022-09-24 10:42:11 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
|
|
|
program->getAttributeLocation(backend::Attribute::POSITION),
|
2022-09-23 22:41:30 +08:00
|
|
|
backend::VertexFormat::FLOAT3, 0, false);
|
2022-09-24 10:42:11 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
|
|
|
|
program->getAttributeLocation(backend::Attribute::TEXCOORD),
|
2022-09-23 22:41:30 +08:00
|
|
|
backend::VertexFormat::FLOAT2, offsetof(V3F_T2F_N3F, texcoord), false);
|
2022-09-24 10:42:11 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_NORMAL,
|
|
|
|
program->getAttributeLocation(backend::Attribute::NORMAL), backend::VertexFormat::FLOAT3,
|
|
|
|
offsetof(V3F_T2F_N3F, normal), false);
|
2022-09-23 22:41:30 +08:00
|
|
|
vertexLayout->setStride(sizeof(V3F_T2F_N3F));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ### end of vertex layout setup functions
|
2023-07-20 10:18:30 +08:00
|
|
|
static std::string joinPath(std::string_view path, std::string_view childPath) {
|
|
|
|
std::string ret;
|
|
|
|
ret.reserve(path.length() + childPath.length());
|
|
|
|
ret += path;
|
|
|
|
ret += childPath;
|
|
|
|
return ret;
|
|
|
|
}
|
2022-09-23 22:41:30 +08:00
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
bool ProgramManager::init()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-07-20 10:18:30 +08:00
|
|
|
auto fileUtils = FileUtils::getInstance();
|
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 || AX_TARGET_PLATFORM == AX_PLATFORM_LINUX
|
|
|
|
// load compiled shader from exeDir/axslc
|
|
|
|
auto axslcDir = joinPath(fileUtils->getAppRoot(), "axslc"sv);
|
|
|
|
fileUtils->addSearchPath(axslcDir);
|
|
|
|
#else // APPLE, ANDROID, WINRT
|
|
|
|
fileUtils->addSearchPath("axslc"sv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
registerProgram(ProgramType::POSITION_TEXTURE_COLOR, positionTextureColor_vert, positionTextureColor_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::DUAL_SAMPLER, positionTextureColor_vert, dualSampler_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::LABEL_DISTANCE_NORMAL, positionTextureColor_vert, label_distanceNormal_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::LABEL_NORMAL, positionTextureColor_vert, label_normal_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::LABLE_OUTLINE, positionTextureColor_vert, labelOutline_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::LABLE_DISTANCEFIELD_GLOW, positionTextureColor_vert,
|
2022-09-24 10:42:11 +08:00
|
|
|
labelDistanceFieldGlow_frag, VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_COLOR_LENGTH_TEXTURE, positionColorLengthTexture_vert,
|
2022-09-24 10:42:11 +08:00
|
|
|
positionColorLengthTexture_frag, VertexLayoutHelper::setupDrawNode);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE, positionColorTextureAsPointsize_vert,
|
2022-09-24 10:42:11 +08:00
|
|
|
positionColor_frag, VertexLayoutHelper::setupDrawNode);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_COLOR, positionColor_vert, positionColor_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupPosColor);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::LAYER_RADIA_GRADIENT, position_vert, layer_radialGradient_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupPos);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_TEXTURE, positionTexture_vert, positionTexture_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupTexture);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST, positionTextureColor_vert,
|
2022-09-24 10:42:11 +08:00
|
|
|
positionTextureColorAlphaTest_frag, VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_UCOLOR, positionUColor_vert, positionColor_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupPos);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::DUAL_SAMPLER_GRAY, positionTextureColor_vert, dualSampler_gray_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::GRAY_SCALE, positionTextureColor_vert, grayScale_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::LINE_COLOR_3D, lineColor3D_vert, lineColor3D_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupDrawNode3D);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::CAMERA_CLEAR, cameraClear_vert, cameraClear_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::SKYBOX_3D, skybox_vert, skybox_frag, VertexLayoutHelper::setupSkyBox);
|
|
|
|
registerProgram(ProgramType::SKINPOSITION_TEXTURE_3D, skinPositionTexture_vert, colorTexture_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::SKINPOSITION_NORMAL_TEXTURE_3D, skinPositionNormalTexture_vert,
|
2023-07-19 23:41:16 +08:00
|
|
|
colorNormalTexture_frag, VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_NORMAL_TEXTURE_3D, positionNormalTexture_vert,
|
2023-07-19 23:41:16 +08:00
|
|
|
colorNormalTexture_frag, VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_TEXTURE_3D, positionTexture3D_vert, colorTexture_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_3D, positionTexture3D_vert, color_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_NORMAL_3D, positionNormalTexture_vert, colorNormal_frag,
|
2023-07-19 23:41:16 +08:00
|
|
|
VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::POSITION_BUMPEDNORMAL_TEXTURE_3D,
|
2023-07-19 23:41:16 +08:00
|
|
|
positionNormalTexture_vert_1,
|
|
|
|
colorNormalTexture_frag_1, VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D,
|
2023-07-19 23:41:16 +08:00
|
|
|
skinPositionNormalTexture_vert_1,
|
|
|
|
colorNormalTexture_frag_1, VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::TERRAIN_3D, terrain_vert, terrain_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupTerrain3D);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::PARTICLE_TEXTURE_3D, particle_vert, particleTexture_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupPU3D);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::PARTICLE_COLOR_3D, particle_vert, particleColor_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupPU3D);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::QUAD_COLOR_2D, quadColor_vert, quadColor_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::QUAD_TEXTURE_2D, quadTexture_vert, quadTexture_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupDummy);
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::HSV, positionTextureColor_vert, hsv_frag, VertexLayoutHelper::setupSprite);
|
|
|
|
registerProgram(ProgramType::HSV_DUAL_SAMPLER, positionTextureColor_vert, dualSampler_hsv_frag,
|
2022-09-24 10:42:11 +08:00
|
|
|
VertexLayoutHelper::setupSprite);
|
2021-06-27 20:58:50 +08:00
|
|
|
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(ProgramType::VIDEO_TEXTURE_YUY2, positionTextureColor_vert,
|
|
|
|
videoTextureYUY2_frag, VertexLayoutHelper::setupSprite);
|
|
|
|
registerProgram(ProgramType::VIDEO_TEXTURE_NV12, positionTextureColor_vert,
|
|
|
|
videoTextureNV12_frag, VertexLayoutHelper::setupSprite);
|
|
|
|
registerProgram(ProgramType::VIDEO_TEXTURE_BGR32, positionTextureColor_vert,
|
|
|
|
videoTextureBGRA_frag, VertexLayoutHelper::setupSprite);
|
2023-03-25 08:37:51 +08:00
|
|
|
|
2021-06-27 20:58:50 +08:00
|
|
|
// The builtin dual sampler shader registry
|
|
|
|
ProgramStateRegistry::getInstance()->registerProgram(ProgramType::POSITION_TEXTURE_COLOR,
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureSamplerFlag::DUAL_SAMPLER,
|
2023-07-19 23:41:16 +08:00
|
|
|
ProgramType::DUAL_SAMPLER);
|
2021-06-27 20:58:50 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ProgramStateRegistry::getInstance()->registerProgram(ProgramType::GRAY_SCALE, TextureSamplerFlag::DUAL_SAMPLER,
|
2023-07-19 23:41:16 +08:00
|
|
|
ProgramType::DUAL_SAMPLER_GRAY);
|
2021-06-27 20:58:50 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ProgramStateRegistry::getInstance()->registerProgram(ProgramType::HSV, TextureSamplerFlag::DUAL_SAMPLER,
|
2023-07-19 23:41:16 +08:00
|
|
|
ProgramType::HSV_DUAL_SAMPLER);
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
Program* ProgramManager::getCustomProgram(uint32_t type) const
|
2020-10-16 16:23:14 +08:00
|
|
|
{
|
|
|
|
return getBuiltinProgram(type | ProgramType::CUSTOM_PROGRAM);
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
Program* ProgramManager::getBuiltinProgram(uint32_t type) const
|
2020-10-16 16:23:14 +08:00
|
|
|
{
|
|
|
|
auto iter = _cachedPrograms.find(type);
|
|
|
|
if (iter != _cachedPrograms.end())
|
|
|
|
return iter->second;
|
|
|
|
|
|
|
|
return addProgram(type);
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
Program* ProgramManager::addProgram(uint32_t internalType) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Program* program = nullptr;
|
2020-10-16 16:23:14 +08:00
|
|
|
if (internalType < ProgramType::BUILTIN_COUNT)
|
|
|
|
{
|
|
|
|
auto& func = _builtinFactories[internalType];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (func)
|
|
|
|
program = func();
|
2020-10-16 16:23:14 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
auto iter = _customFactories.find(internalType);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter != _customFactories.end())
|
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
auto& func = iter->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (func)
|
|
|
|
program = func();
|
2020-10-16 16:23:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (program)
|
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
program->setProgramType(internalType);
|
|
|
|
_cachedPrograms.emplace(internalType, program);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-10-16 16:23:14 +08:00
|
|
|
|
|
|
|
return program;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 10:18:30 +08:00
|
|
|
void ProgramManager::registerCustomProgram(uint32_t type,
|
2023-07-19 23:41:16 +08:00
|
|
|
std::string_view vsName,
|
|
|
|
std::string_view fsName,
|
|
|
|
std::function<void(Program*)> fnSetupLayout)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
auto internalType = ProgramType::CUSTOM_PROGRAM | type;
|
2023-07-20 10:18:30 +08:00
|
|
|
registerProgram(internalType, vsName, fsName,
|
2023-07-19 23:41:16 +08:00
|
|
|
std::move(fnSetupLayout));
|
2020-10-16 16:23:14 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 10:18:30 +08:00
|
|
|
void ProgramManager::registerProgram(uint32_t internalType,
|
2023-07-19 23:41:16 +08:00
|
|
|
std::string_view vertShaderName,
|
|
|
|
std::string_view fragShaderName,
|
|
|
|
std::function<void(Program*)> fnSetupLayout)
|
2020-10-16 16:23:14 +08:00
|
|
|
{
|
2023-07-19 23:41:16 +08:00
|
|
|
auto loadShaderFunc = [vsName = std::string{vertShaderName}, fsName = std::string{fragShaderName},
|
|
|
|
setupLayout = std::move(fnSetupLayout)]() mutable {
|
|
|
|
auto fileUtils = FileUtils::getInstance();
|
|
|
|
auto vertFile = fileUtils->fullPathForFilename(vsName);
|
|
|
|
auto fragFile = fileUtils->fullPathForFilename(fsName);
|
|
|
|
auto vertSource = fileUtils->getStringFromFile(vertFile);
|
|
|
|
auto fragSource = fileUtils->getStringFromFile(fragFile);
|
|
|
|
auto program = backend::Device::getInstance()->newProgram(vertSource, fragSource);
|
2022-09-23 22:41:30 +08:00
|
|
|
setupLayout(program);
|
|
|
|
return program;
|
2020-10-16 16:23:14 +08:00
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (internalType < ProgramType::BUILTIN_COUNT)
|
|
|
|
{
|
2023-07-19 23:41:16 +08:00
|
|
|
_builtinFactories[internalType] = loadShaderFunc;
|
2020-10-16 16:23:14 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
auto it = _customFactories.find(internalType);
|
|
|
|
if (it == _customFactories.end())
|
2023-07-19 23:41:16 +08:00
|
|
|
_customFactories.emplace(internalType, loadShaderFunc);
|
2020-10-16 16:23:14 +08:00
|
|
|
else
|
2023-07-19 23:41:16 +08:00
|
|
|
it->second = loadShaderFunc;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
void ProgramManager::removeProgram(Program* program)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!program)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
for (auto it = _cachedPrograms.cbegin(); it != _cachedPrograms.cend();)
|
|
|
|
{
|
|
|
|
if (it->second == program)
|
|
|
|
{
|
|
|
|
it->second->release();
|
|
|
|
it = _cachedPrograms.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
void ProgramManager::removeUnusedProgram()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (auto iter = _cachedPrograms.cbegin(); iter != _cachedPrograms.cend();)
|
|
|
|
{
|
|
|
|
auto program = iter->second;
|
|
|
|
if (program->getReferenceCount() == 1)
|
|
|
|
{
|
2022-10-01 16:24:52 +08:00
|
|
|
// AXLOG("axmol: TextureCache: removing unused program");
|
2019-11-23 20:27:39 +08:00
|
|
|
program->release();
|
|
|
|
iter = _cachedPrograms.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 19:44:31 +08:00
|
|
|
void ProgramManager::removeAllPrograms()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ProgramStateRegistry::getInstance()->clearPrograms();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& program : _cachedPrograms)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
program.second->release();
|
|
|
|
}
|
|
|
|
_cachedPrograms.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|