2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "ProgramCache.h"
|
|
|
|
#include "Device.h"
|
|
|
|
#include "ShaderModule.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string getShaderMacrosForLight()
|
|
|
|
{
|
|
|
|
char def[256];
|
|
|
|
auto conf = Configuration::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
snprintf(def, sizeof(def) - 1,
|
|
|
|
"\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
|
|
|
|
"\n#define MAX_POINT_LIGHT_NUM %d \n"
|
|
|
|
"\n#define MAX_SPOT_LIGHT_NUM %d \n",
|
|
|
|
conf->getMaxSupportDirLightInShader(), conf->getMaxSupportPointLightInShader(),
|
|
|
|
conf->getMaxSupportSpotLightInShader());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return std::string(def);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ProgramCache* ProgramCache::_sharedProgramCache = nullptr;
|
|
|
|
|
|
|
|
ProgramCache* ProgramCache::getInstance()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_sharedProgramCache)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sharedProgramCache = new ProgramCache();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_sharedProgramCache->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
CC_SAFE_DELETE(_sharedProgramCache);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return _sharedProgramCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramCache::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_sharedProgramCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramCache::~ProgramCache()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& program : _cachedPrograms)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(program.second);
|
|
|
|
}
|
|
|
|
CCLOGINFO("deallocing ProgramCache: %p", this);
|
|
|
|
ShaderCache::destroyInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramCache::init()
|
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
registerProgramFactory(ProgramType::POSITION_TEXTURE_COLOR, positionTextureColor_vert, positionTextureColor_frag);
|
2021-06-27 20:58:50 +08:00
|
|
|
registerProgramFactory(ProgramType::DUAL_SAMPLER, positionTextureColor_vert, dualSampler_frag);
|
2020-10-16 16:23:14 +08:00
|
|
|
registerProgramFactory(ProgramType::LABEL_DISTANCE_NORMAL, positionTextureColor_vert, label_distanceNormal_frag);
|
|
|
|
registerProgramFactory(ProgramType::LABEL_NORMAL, positionTextureColor_vert, label_normal_frag);
|
|
|
|
registerProgramFactory(ProgramType::LABLE_OUTLINE, positionTextureColor_vert, labelOutline_frag);
|
2021-12-25 10:04:45 +08:00
|
|
|
registerProgramFactory(ProgramType::LABLE_DISTANCEFIELD_GLOW, positionTextureColor_vert,
|
|
|
|
labelDistanceFieldGlow_frag);
|
|
|
|
registerProgramFactory(ProgramType::POSITION_COLOR_LENGTH_TEXTURE, positionColorLengthTexture_vert,
|
|
|
|
positionColorLengthTexture_frag);
|
|
|
|
registerProgramFactory(ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE, positionColorTextureAsPointsize_vert,
|
|
|
|
positionColor_frag);
|
2020-10-16 16:23:14 +08:00
|
|
|
registerProgramFactory(ProgramType::POSITION_COLOR, positionColor_vert, positionColor_frag);
|
|
|
|
registerProgramFactory(ProgramType::LAYER_RADIA_GRADIENT, position_vert, layer_radialGradient_frag);
|
|
|
|
registerProgramFactory(ProgramType::POSITION_TEXTURE, positionTexture_vert, positionTexture_frag);
|
2021-12-25 10:04:45 +08:00
|
|
|
registerProgramFactory(ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST, positionTextureColor_vert,
|
|
|
|
positionTextureColorAlphaTest_frag);
|
2020-11-16 11:20:12 +08:00
|
|
|
registerProgramFactory(ProgramType::POSITION_UCOLOR, positionUColor_vert, positionColor_frag);
|
2021-06-27 20:58:50 +08:00
|
|
|
registerProgramFactory(ProgramType::DUAL_SAMPLER_GRAY, positionTextureColor_vert, dualSampler_gray_frag);
|
2020-10-16 16:23:14 +08:00
|
|
|
registerProgramFactory(ProgramType::GRAY_SCALE, positionTextureColor_vert, grayScale_frag);
|
|
|
|
registerProgramFactory(ProgramType::LINE_COLOR_3D, lineColor3D_vert, lineColor3D_frag);
|
|
|
|
registerProgramFactory(ProgramType::CAMERA_CLEAR, cameraClear_vert, cameraClear_frag);
|
|
|
|
registerProgramFactory(ProgramType::SKYBOX_3D, CC3D_skybox_vert, CC3D_skybox_frag);
|
|
|
|
registerProgramFactory(ProgramType::SKINPOSITION_TEXTURE_3D, CC3D_skinPositionTexture_vert, CC3D_colorTexture_frag);
|
|
|
|
auto lightDef = getShaderMacrosForLight();
|
2021-12-25 10:04:45 +08:00
|
|
|
registerProgramFactory(ProgramType::SKINPOSITION_NORMAL_TEXTURE_3D, lightDef + CC3D_skinPositionNormalTexture_vert,
|
|
|
|
lightDef + CC3D_colorNormalTexture_frag);
|
|
|
|
registerProgramFactory(ProgramType::POSITION_NORMAL_TEXTURE_3D, lightDef + CC3D_positionNormalTexture_vert,
|
|
|
|
lightDef + CC3D_colorNormalTexture_frag);
|
2020-10-16 16:23:14 +08:00
|
|
|
registerProgramFactory(ProgramType::POSITION_TEXTURE_3D, CC3D_positionTexture_vert, CC3D_colorTexture_frag);
|
|
|
|
registerProgramFactory(ProgramType::POSITION_3D, CC3D_positionTexture_vert, CC3D_color_frag);
|
2021-12-25 10:04:45 +08:00
|
|
|
registerProgramFactory(ProgramType::POSITION_NORMAL_3D, lightDef + CC3D_positionNormalTexture_vert,
|
|
|
|
lightDef + CC3D_colorNormal_frag);
|
2020-10-16 16:23:14 +08:00
|
|
|
const char* normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
|
2021-12-25 10:04:45 +08:00
|
|
|
registerProgramFactory(ProgramType::POSITION_BUMPEDNORMAL_TEXTURE_3D,
|
|
|
|
lightDef + normalMapDef + CC3D_positionNormalTexture_vert,
|
|
|
|
lightDef + normalMapDef + CC3D_colorNormalTexture_frag);
|
|
|
|
registerProgramFactory(ProgramType::SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D,
|
|
|
|
lightDef + normalMapDef + CC3D_skinPositionNormalTexture_vert,
|
|
|
|
lightDef + normalMapDef + CC3D_colorNormalTexture_frag);
|
2020-10-16 16:23:14 +08:00
|
|
|
registerProgramFactory(ProgramType::TERRAIN_3D, CC3D_terrain_vert, CC3D_terrain_frag);
|
|
|
|
registerProgramFactory(ProgramType::PARTICLE_TEXTURE_3D, CC3D_particle_vert, CC3D_particleTexture_frag);
|
|
|
|
registerProgramFactory(ProgramType::PARTICLE_COLOR_3D, CC3D_particle_vert, CC3D_particleColor_frag);
|
|
|
|
registerProgramFactory(ProgramType::HSV, positionTextureColor_vert, hsv_frag);
|
2021-06-27 20:58:50 +08:00
|
|
|
registerProgramFactory(ProgramType::HSV_DUAL_SAMPLER, positionTextureColor_vert, dualSampler_hsv_frag);
|
|
|
|
|
|
|
|
// The builtin dual sampler shader registry
|
|
|
|
ProgramStateRegistry::getInstance()->registerProgram(ProgramType::POSITION_TEXTURE_COLOR,
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureSamplerFlag::DUAL_SAMPLER,
|
|
|
|
getBuiltinProgram(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,
|
|
|
|
getBuiltinProgram(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,
|
|
|
|
getBuiltinProgram(ProgramType::HSV_DUAL_SAMPLER));
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:23:14 +08:00
|
|
|
backend::Program* ProgramCache::getCustomProgram(uint32_t type) const
|
|
|
|
{
|
|
|
|
return getBuiltinProgram(type | ProgramType::CUSTOM_PROGRAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
backend::Program* ProgramCache::getBuiltinProgram(uint32_t type) const
|
|
|
|
{
|
|
|
|
auto iter = _cachedPrograms.find(type);
|
|
|
|
if (iter != _cachedPrograms.end())
|
|
|
|
return iter->second;
|
|
|
|
|
|
|
|
return addProgram(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
Program* ProgramCache::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
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ProgramCache::registerCustomProgramFactory(uint32_t type,
|
|
|
|
std::string vertShaderSource,
|
|
|
|
std::string fragShaderSource)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
auto internalType = ProgramType::CUSTOM_PROGRAM | type;
|
|
|
|
registerProgramFactory(internalType, std::move(vertShaderSource), std::move(fragShaderSource));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ProgramCache::registerProgramFactory(uint32_t internalType,
|
|
|
|
std::string&& vertShaderSource,
|
|
|
|
std::string&& fragShaderSource)
|
2020-10-16 16:23:14 +08:00
|
|
|
{
|
|
|
|
auto constructProgram = [vsrc = std::move(vertShaderSource), fsrc = std::move(fragShaderSource)]() {
|
|
|
|
return backend::Device::getInstance()->newProgram(vsrc, fsrc);
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (internalType < ProgramType::BUILTIN_COUNT)
|
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
_builtinFactories[internalType] = constructProgram;
|
|
|
|
}
|
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())
|
|
|
|
_customFactories.emplace(internalType, constructProgram);
|
|
|
|
else
|
|
|
|
it->second = constructProgram;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramCache::removeProgram(backend::Program* program)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramCache::removeUnusedProgram()
|
|
|
|
{
|
|
|
|
for (auto iter = _cachedPrograms.cbegin(); iter != _cachedPrograms.cend();)
|
|
|
|
{
|
|
|
|
auto program = iter->second;
|
|
|
|
if (program->getReferenceCount() == 1)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// CCLOG("cocos2d: TextureCache: removing unused program");
|
2019-11-23 20:27:39 +08:00
|
|
|
program->release();
|
|
|
|
iter = _cachedPrograms.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramCache::removeAllPrograms()
|
|
|
|
{
|
|
|
|
ProgramStateRegistry::getInstance()->clearPrograms();
|
|
|
|
for (auto& program : _cachedPrograms)
|
|
|
|
{
|
|
|
|
program.second->release();
|
|
|
|
}
|
|
|
|
_cachedPrograms.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|