axmol/core/renderer/backend/ProgramStateRegistry.cpp

76 lines
2.0 KiB
C++
Raw Normal View History

#include "ProgramStateRegistry.h"
NS_AX_BACKEND_BEGIN
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();
if (!_sharedStateRegistry->init())
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_sharedStateRegistry);
}
return _sharedStateRegistry;
}
/** purges the cache. It releases the retained instance. */
void ProgramStateRegistry::destroyInstance()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE_NULL(_sharedStateRegistry);
}
bool ProgramStateRegistry::init()
{
return true;
}
2021-12-25 10:04:45 +08:00
void ProgramStateRegistry::registerProgram(uint32_t programType, int textureSamplerFlags, Program* program)
{
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-22 03:36:01 +08:00
if (it == this->_registry.end())
2019-11-26 22:37:20 +08:00
this->_registry.emplace(key, program);
2019-11-22 03:36:01 +08:00
else
it->second = program;
}
2021-12-25 10:04:45 +08:00
void ProgramStateRegistry::clearPrograms()
{
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-22 03:36:01 +08:00
auto fallback = it->second;
if (fallback)
2021-12-08 00:11:53 +08:00
return new ProgramState(fallback);
}
2021-12-08 00:11:53 +08:00
return new ProgramState(Program::getBuiltinProgram(programType));
}
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-22 03:36:01 +08:00
auto fallback = it->second;
if (fallback)
return fallback->getProgramType();
}
return programType;
}
2021-12-25 10:04:45 +08:00
// end of _backend group
/// @}
NS_AX_BACKEND_END