2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 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 "ShaderCache.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
|
|
|
|
CC_BACKEND_BEGIN
|
|
|
|
|
|
|
|
std::unordered_map<std::size_t, backend::ShaderModule*> ShaderCache::_cachedShaders;
|
|
|
|
ShaderCache* ShaderCache::_sharedShaderCache = nullptr;
|
|
|
|
|
|
|
|
ShaderCache* ShaderCache::getInstance()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_sharedShaderCache)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sharedShaderCache = new ShaderCache();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_sharedShaderCache->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
CC_SAFE_DELETE(_sharedShaderCache);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return _sharedShaderCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderCache::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_sharedShaderCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderCache::~ShaderCache()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& shaderModule : _cachedShaders)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(shaderModule.second);
|
|
|
|
}
|
|
|
|
CCLOGINFO("deallocing ProgramCache: %p", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCache::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend::ShaderModule* ShaderCache::newVertexShaderModule(const std::string& shaderSource)
|
|
|
|
{
|
|
|
|
auto vertexShaderModule = newShaderModule(backend::ShaderStage::VERTEX, shaderSource);
|
|
|
|
return vertexShaderModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend::ShaderModule* ShaderCache::newFragmentShaderModule(const std::string& shaderSource)
|
|
|
|
{
|
|
|
|
auto fragmenShaderModule = newShaderModule(backend::ShaderStage::FRAGMENT, shaderSource);
|
|
|
|
return fragmenShaderModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend::ShaderModule* ShaderCache::newShaderModule(backend::ShaderStage stage, const std::string& shaderSource)
|
|
|
|
{
|
|
|
|
std::size_t key = std::hash<std::string>{}(shaderSource);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto iter = _cachedShaders.find(key);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_cachedShaders.end() != iter)
|
|
|
|
return iter->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto shader = backend::Device::getInstance()->newShaderModule(stage, shaderSource);
|
2019-11-27 15:39:41 +08:00
|
|
|
shader->setHashValue(key);
|
2019-11-23 20:27:39 +08:00
|
|
|
_cachedShaders.emplace(key, shader);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderCache::removeUnusedShader()
|
|
|
|
{
|
|
|
|
for (auto iter = _cachedShaders.cbegin(); iter != _cachedShaders.cend();)
|
|
|
|
{
|
|
|
|
auto shaderModule = iter->second;
|
|
|
|
if (shaderModule->getReferenceCount() == 1)
|
|
|
|
{
|
|
|
|
// CCLOG("cocos2d: TextureCache: removing unused program");
|
|
|
|
shaderModule->release();
|
|
|
|
iter = _cachedShaders.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BACKEND_END
|