axmol/core/renderer/backend/ProgramStateRegistry.cpp

76 lines
2.0 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
#include "ProgramStateRegistry.h"
NS_AX_BACKEND_BEGIN
2019-11-23 20:27:39 +08:00
static ProgramStateRegistry* _sharedStateRegistry = nullptr;
/** returns the shared instance */
ProgramStateRegistry* ProgramStateRegistry::getInstance()
{
if (_sharedStateRegistry)
return _sharedStateRegistry;
2021-12-08 00:11:53 +08:00
_sharedStateRegistry = new ProgramStateRegistry();
2019-11-23 20:27:39 +08:00
if (!_sharedStateRegistry->init())
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_sharedStateRegistry);
2019-11-23 20:27:39 +08:00
}
return _sharedStateRegistry;
}
/** purges the cache. It releases the retained instance. */
void ProgramStateRegistry::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_sharedStateRegistry);
2019-11-23 20:27:39 +08:00
}
bool ProgramStateRegistry::init()
{
return true;
}
void ProgramStateRegistry::registerProgram(uint32_t programType, int textureSamplerFlags, uint32_t builtinProgramType)
2021-12-25 10:04:45 +08:00
{
uint32_t key = (static_cast<uint32_t>(programType) << 16) | textureSamplerFlags;
2021-12-25 10:04:45 +08:00
auto it = this->_registry.find(key);
2019-11-23 20:27:39 +08:00
if (it == this->_registry.end())
this->_registry.emplace(key, builtinProgramType);
2019-11-23 20:27:39 +08:00
else
it->second = builtinProgramType;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void ProgramStateRegistry::clearPrograms()
{
2019-11-23 20:27:39 +08:00
this->_registry.clear();
}
2021-12-25 10:04:45 +08:00
ProgramState* ProgramStateRegistry::newProgramState(uint32_t programType, int textureSamplerFlags)
{
uint32_t key = ((programType) << 16) | textureSamplerFlags;
2021-12-25 10:04:45 +08:00
auto it = this->_registry.find(key);
if (it != this->_registry.end())
{
2019-11-23 20:27:39 +08:00
auto fallback = it->second;
if (fallback)
return new ProgramState(Program::getBuiltinProgram(fallback));
2019-11-23 20:27:39 +08:00
}
2021-12-08 00:11:53 +08:00
return new ProgramState(Program::getBuiltinProgram(programType));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
uint32_t ProgramStateRegistry::getProgramType(uint32_t programType, int textureSamplerFlags)
{
uint32_t key = (static_cast<uint32_t>(programType) << 16) | textureSamplerFlags;
2021-12-25 10:04:45 +08:00
auto it = this->_registry.find(key);
if (it != this->_registry.end())
{
2019-11-23 20:27:39 +08:00
auto fallback = it->second;
if (fallback)
return fallback;
2019-11-23 20:27:39 +08:00
}
return programType;
}
2021-12-25 10:04:45 +08:00
// end of _backend group
2019-11-23 20:27:39 +08:00
/// @}
NS_AX_BACKEND_END