axmol/cocos/renderer/backend/ProgramStateRegistry.cpp

73 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;
_sharedStateRegistry = new (std::nothrow) ProgramStateRegistry();
if (!_sharedStateRegistry->init())
{
CC_SAFE_RELEASE_NULL(_sharedStateRegistry);
}
return _sharedStateRegistry;
}
/** purges the cache. It releases the retained instance. */
void ProgramStateRegistry::destroyInstance()
{
CC_SAFE_RELEASE_NULL(_sharedStateRegistry);
}
bool ProgramStateRegistry::init()
{
return true;
}
void ProgramStateRegistry::registerProgram(uint32_t programType, int textureFormatEXT, Program* program)
2019-11-23 20:27:39 +08:00
{
uint32_t key = (static_cast<uint32_t>(programType) << 16) | textureFormatEXT;
auto it = this->_registry.find(key);
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;
}
void ProgramStateRegistry::clearPrograms() {
this->_registry.clear();
}
ProgramState* ProgramStateRegistry::newProgramState(uint32_t programType, int textureFormatEXT)
2019-11-23 20:27:39 +08:00
{
uint32_t key = ((programType) << 16) | textureFormatEXT;
2019-11-23 20:27:39 +08:00
auto it = this->_registry.find(key);
if (it != this->_registry.end()) {
auto fallback = it->second;
if (fallback)
return new(std::nothrow) ProgramState(fallback);
}
return new(std::nothrow) ProgramState(Program::getBuiltinProgram(programType));
2019-11-23 20:27:39 +08:00
}
uint32_t ProgramStateRegistry::getProgramType(uint32_t programType, int textureFormatEXT)
2019-11-23 20:27:39 +08:00
{
uint32_t key = (static_cast<uint32_t>(programType) << 16) | textureFormatEXT;
auto it = this->_registry.find(key);
if (it != this->_registry.end()) {
auto fallback = it->second;
if (fallback)
return fallback->getProgramType();
}
return programType;
}
//end of _backend group
/// @}
CC_BACKEND_END