axmol/core/renderer/backend/ProgramManager.cpp

309 lines
13 KiB
C++
Raw Normal View History

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"
#include "renderer/Shaders.h"
#include "base/Macros.h"
#include "base/Configuration.h"
2019-11-23 20:27:39 +08:00
#include "xxhash.h"
2023-07-25 19:06:12 +08:00
#include <inttypes.h>
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
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
}
ProgramManager::ProgramManager()
{
_programIdGen = XXH64_createState();
}
2022-10-12 19:44:31 +08:00
ProgramManager::~ProgramManager()
2019-11-23 20:27:39 +08:00
{
XXH64_freeState(_programIdGen);
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
// ### end of vertex layout setup functions
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
{
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,
VertexLayoutType::Sprite);
registerProgram(ProgramType::DUAL_SAMPLER, positionTextureColor_vert, dualSampler_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::LABEL_DISTANCE_NORMAL, positionTextureColor_vert, label_distanceNormal_frag,
VertexLayoutType::Sprite);
registerProgram(ProgramType::LABEL_NORMAL, positionTextureColor_vert, label_normal_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::LABLE_OUTLINE, positionTextureColor_vert, label_outline_frag,
VertexLayoutType::Sprite);
registerProgram(ProgramType::LABEL_DISTANCE_OUTLINE, positionTextureColor_vert, label_distanceOutline_frag,
VertexLayoutType::Sprite);
registerProgram(ProgramType::LABLE_DISTANCE_GLOW, positionTextureColor_vert, label_distanceGlow_frag,
VertexLayoutType::Sprite);
registerProgram(ProgramType::POSITION_COLOR_LENGTH_TEXTURE, positionColorLengthTexture_vert,
positionColorLengthTexture_frag, VertexLayoutType::DrawNode);
registerProgram(ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE, positionColorTextureAsPointsize_vert,
positionColor_frag, VertexLayoutType::DrawNode);
registerProgram(ProgramType::POSITION_COLOR, positionColor_vert, positionColor_frag, VertexLayoutType::posColor);
registerProgram(ProgramType::LAYER_RADIA_GRADIENT, position_vert, layer_radialGradient_frag, VertexLayoutType::Pos);
registerProgram(ProgramType::POSITION_TEXTURE, positionTexture_vert, positionTexture_frag,
VertexLayoutType::Texture);
registerProgram(ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST, positionTextureColor_vert,
positionTextureColorAlphaTest_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::POSITION_UCOLOR, positionUColor_vert, positionColor_frag, VertexLayoutType::Pos);
registerProgram(ProgramType::DUAL_SAMPLER_GRAY, positionTextureColor_vert, dualSampler_gray_frag,
VertexLayoutType::Sprite);
registerProgram(ProgramType::GRAY_SCALE, positionTextureColor_vert, grayScale_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::LINE_COLOR_3D, lineColor_vert, lineColor_frag, VertexLayoutType::DrawNode3D);
registerProgram(ProgramType::CAMERA_CLEAR, cameraClear_vert, cameraClear_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::SKYBOX_3D, skybox_vert, skybox_frag, VertexLayoutType::SkyBox);
registerProgram(ProgramType::SKINPOSITION_TEXTURE_3D, skinPositionTexture_vert, colorTexture_frag,
VertexLayoutType::Unspec);
registerProgram(ProgramType::SKINPOSITION_NORMAL_TEXTURE_3D, skinPositionNormalTexture_vert,
colorNormalTexture_frag, VertexLayoutType::Unspec);
registerProgram(ProgramType::POSITION_NORMAL_TEXTURE_3D, positionNormalTexture_vert, colorNormalTexture_frag,
VertexLayoutType::Unspec);
registerProgram(ProgramType::POSITION_TEXTURE_3D, positionTexture3D_vert, colorTexture_frag,
VertexLayoutType::Unspec);
registerProgram(ProgramType::POSITION_TEXTURE_3D_INSTANCE, positionTextureInstance_vert, colorTexture_frag,
VertexLayoutType::Unspec);
registerProgram(ProgramType::POSITION_3D, positionTexture3D_vert, color_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::POSITION_NORMAL_3D, positionNormalTexture_vert, colorNormal_frag,
VertexLayoutType::Unspec);
registerProgram(ProgramType::POSITION_BUMPEDNORMAL_TEXTURE_3D, positionNormalTexture_vert_1,
colorNormalTexture_frag_1, VertexLayoutType::Unspec);
registerProgram(ProgramType::SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D, skinPositionNormalTexture_vert_1,
colorNormalTexture_frag_1, VertexLayoutType::Unspec);
registerProgram(ProgramType::TERRAIN_3D, terrain_vert, terrain_frag, VertexLayoutType::Terrain3D);
registerProgram(ProgramType::PARTICLE_TEXTURE_3D, particle_vert, particleTexture_frag, VertexLayoutType::PU3D);
registerProgram(ProgramType::PARTICLE_COLOR_3D, particle_vert, particleColor_frag, VertexLayoutType::PU3D);
registerProgram(ProgramType::QUAD_COLOR_2D, quadColor_vert, quadColor_frag, VertexLayoutType::Unspec);
registerProgram(ProgramType::QUAD_TEXTURE_2D, quadTexture_vert, quadTexture_frag, VertexLayoutType::Unspec);
registerProgram(ProgramType::HSV, positionTextureColor_vert, hsv_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::HSV_DUAL_SAMPLER, positionTextureColor_vert, dualSampler_hsv_frag,
VertexLayoutType::Sprite);
registerProgram(ProgramType::VIDEO_TEXTURE_YUY2, positionTextureColor_vert, videoTextureYUY2_frag,
VertexLayoutType::Sprite);
registerProgram(ProgramType::VIDEO_TEXTURE_NV12, positionTextureColor_vert, videoTextureNV12_frag,
VertexLayoutType::Sprite);
// The builtin dual sampler shader registry
ProgramStateRegistry::getInstance()->registerProgram(ProgramType::POSITION_TEXTURE_COLOR,
TextureSamplerFlag::DUAL_SAMPLER, ProgramType::DUAL_SAMPLER);
2021-12-25 10:04:45 +08:00
ProgramStateRegistry::getInstance()->registerProgram(ProgramType::GRAY_SCALE, TextureSamplerFlag::DUAL_SAMPLER,
ProgramType::DUAL_SAMPLER_GRAY);
2021-12-25 10:04:45 +08:00
ProgramStateRegistry::getInstance()->registerProgram(ProgramType::HSV, TextureSamplerFlag::DUAL_SAMPLER,
ProgramType::HSV_DUAL_SAMPLER);
2019-11-23 20:27:39 +08:00
return true;
}
Program* ProgramManager::getBuiltinProgram(uint32_t type)
{
assert(type < ProgramType::BUILTIN_COUNT);
auto& info = _builtinRegistry[static_cast<int>(type)];
return loadProgram(info.vsName, info.fsName, type, static_cast<uint64_t>(type), info.vlt);
}
Program* ProgramManager::loadProgram(uint64_t progId)
{
if (progId < ProgramType::BUILTIN_COUNT)
return getBuiltinProgram(static_cast<uint32_t>(progId));
auto it = _customRegistry.find(progId);
if (it != _customRegistry.end())
return loadProgram(it->second.vsName, it->second.fsName, ProgramType::CUSTOM_PROGRAM, progId, it->second.vlt);
return nullptr;
}
Program* ProgramManager::loadProgram(std::string_view vsName, std::string_view fsName, VertexLayoutType vlt)
2019-11-23 20:27:39 +08:00
{
return loadProgram(vsName, fsName, ProgramType::CUSTOM_PROGRAM, computeProgramId(vsName, fsName), vlt);
}
Program* ProgramManager::loadProgram(std::string_view vsName,
std::string_view fsName,
uint32_t progType,
uint64_t progId,
VertexLayoutType vlt)
{
assert(!vsName.empty() && !fsName.empty());
auto it = _cachedPrograms.find(progId);
if (it != _cachedPrograms.end())
return it->second;
2023-07-25 19:06:12 +08:00
AXLOG("Loading shader: %" PRIu64 " %s, %s ...", progId, vsName.data(), fsName.data());
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);
2021-12-25 10:04:45 +08:00
if (program)
{
program->setProgramIds(progType, progId);
if (vlt < VertexLayoutType::Count)
program->setupVertexLayout(vlt);
_cachedPrograms.emplace(progId, program);
2019-11-23 20:27:39 +08:00
}
return program;
2019-11-23 20:27:39 +08:00
}
uint64_t ProgramManager::registerCustomProgram(std::string_view vsName,
std::string_view fsName,
VertexLayoutType vlt,
bool force)
2019-11-23 20:27:39 +08:00
{
return registerProgram(ProgramType::CUSTOM_PROGRAM, vsName, fsName, vlt, force);
}
uint64_t ProgramManager::registerProgram(uint32_t progType,
std::string_view vsName,
std::string_view fsName,
VertexLayoutType vlt,
bool force)
{
uint64_t progId = 0;
if (progType < ProgramType::BUILTIN_COUNT)
{
_builtinRegistry[static_cast<int>(progType)] = {vsName, fsName, vlt};
progId = progType;
}
2021-12-25 10:04:45 +08:00
else
{
progId = computeProgramId(vsName, fsName);
auto it = _customRegistry.find(progId);
if (it == _customRegistry.end())
_customRegistry.emplace(progId, BuiltinRegInfo{vsName, fsName, vlt});
else if (force)
it->second = BuiltinRegInfo{vsName, fsName, vlt};
else
progId = 0;
2019-11-23 20:27:39 +08:00
}
return progId;
2019-11-23 20:27:39 +08:00
}
uint64_t ProgramManager::computeProgramId(std::string_view vsName, std::string_view fsName)
{
XXH64_reset(_programIdGen, 0);
XXH64_update(_programIdGen, vsName.data(), vsName.length());
XXH64_update(_programIdGen, fsName.data(), fsName.length());
return XXH64_digest(_programIdGen);
}
void ProgramManager::unloadProgram(Program* program)
2019-11-23 20:27:39 +08:00
{
if (!program)
{
return;
}
2021-12-25 10:04:45 +08:00
auto it = _cachedPrograms.find(program->_programId);
if (it != _cachedPrograms.end())
2019-11-23 20:27:39 +08:00
{
it->second->release();
_cachedPrograms.erase(it);
2019-11-23 20:27:39 +08:00
}
}
void ProgramManager::unloadUnusedPrograms()
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;
}
}
}
void ProgramManager::unloadAllPrograms()
2019-11-23 20:27:39 +08:00
{
ProgramStateRegistry::getInstance()->clearPrograms();
for (auto&& program : _cachedPrograms)
2019-11-23 20:27:39 +08:00
{
program.second->release();
}
_cachedPrograms.clear();
}
NS_AX_BACKEND_END