axmol/cocos/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"
CC_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();
2019-11-23 20:27:39 +08:00
if (!_sharedStateRegistry->init())
{
2021-12-08 00:11:53 +08:00
CC_SAFE_DELETE(_sharedStateRegistry);
2019-11-23 20:27:39 +08:00
}
return _sharedStateRegistry;
}
/** purges the cache. It releases the retained instance. */
void ProgramStateRegistry::destroyInstance()
{
CC_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-23 20:27:39 +08:00
if (it == this->_registry.end())
2019-11-26 22:37:20 +08:00
this->_registry.emplace(key, program);
2019-11-23 20:27:39 +08:00
else
it->second = program;
}
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)
2021-12-08 00:11:53 +08:00
return new ProgramState(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->getProgramType();
}
return programType;
}
2021-12-25 10:04:45 +08:00
// end of _backend group
2019-11-23 20:27:39 +08:00
/// @}
CC_BACKEND_END