2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ShaderCache.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_sharedShaderCache);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return _sharedShaderCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderCache::destroyInstance()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_sharedShaderCache);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderCache::~ShaderCache()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shaderModule : _cachedShaders)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(shaderModule.second);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGINFO("deallocing ProgramCache: %p", this);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCache::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
backend::ShaderModule* ShaderCache::newVertexShaderModule(std::string_view shaderSource)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto vertexShaderModule = newShaderModule(backend::ShaderStage::VERTEX, shaderSource);
|
|
|
|
return vertexShaderModule;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
backend::ShaderModule* ShaderCache::newFragmentShaderModule(std::string_view shaderSource)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto fragmenShaderModule = newShaderModule(backend::ShaderStage::FRAGMENT, shaderSource);
|
|
|
|
return fragmenShaderModule;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
backend::ShaderModule* ShaderCache::newShaderModule(backend::ShaderStage stage, std::string_view shaderSource)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
std::size_t key = std::hash<std::string_view>{}(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)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXLOG("cocos2d: TextureCache: removing unused program");
|
2019-11-23 20:27:39 +08:00
|
|
|
shaderModule->release();
|
|
|
|
iter = _cachedShaders.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|