2019-11-23 20:27:39 +08:00
|
|
|
#include "ProgramStateRegistry.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-19 23:41:16 +08:00
|
|
|
void ProgramStateRegistry::registerProgram(uint32_t programType, int textureSamplerFlags, uint32_t builtinProgramType)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-06-27 20:58:50 +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())
|
2023-07-19 23:41:16 +08:00
|
|
|
this->_registry.emplace(key, builtinProgramType);
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
2023-07-19 23:41:16 +08:00
|
|
|
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)
|
|
|
|
{
|
2021-06-27 20:58:50 +08:00
|
|
|
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)
|
2023-07-19 23:41:16 +08:00
|
|
|
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)
|
|
|
|
{
|
2021-06-27 20:58:50 +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);
|
|
|
|
if (it != this->_registry.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto fallback = it->second;
|
|
|
|
if (fallback)
|
2023-07-19 23:41:16 +08:00
|
|
|
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
|
|
|
/// @}
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|