Skip to content

Commit a8de1cc

Browse files
author
Juan Andrés Nin
authored
Add custom programs to ProgramCache (cocos2d#20496)
Store and retrieve custom user-defined programs to ProgramCache.
1 parent 121f8e9 commit a8de1cc

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

cocos/renderer/backend/ProgramCache.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ namespace
6464
}
6565

6666
std::unordered_map<backend::ProgramType, backend::Program*> ProgramCache::_cachedPrograms;
67+
std::unordered_map<std::string, backend::Program*> ProgramCache::_cachedCustomPrograms;
68+
6769
ProgramCache* ProgramCache::_sharedProgramCache = nullptr;
6870

6971
ProgramCache* ProgramCache::getInstance()
@@ -303,6 +305,29 @@ void ProgramCache::removeAllPrograms()
303305
program.second->release();
304306
}
305307
_cachedPrograms.clear();
308+
309+
for (auto& program : _cachedCustomPrograms)
310+
{
311+
program.second->release();
312+
}
313+
_cachedCustomPrograms.clear();
314+
}
315+
316+
void ProgramCache::addCustomProgram(const std::string &key, backend::Program *program)
317+
{
318+
_cachedCustomPrograms.emplace(key, program);
319+
}
320+
321+
backend::Program* ProgramCache::getCustomProgram(const std::string &key) const
322+
{
323+
const auto& iter = ProgramCache::_cachedCustomPrograms.find(key);
324+
if (ProgramCache::_cachedCustomPrograms.end() != iter)
325+
{
326+
return iter->second;
327+
}
328+
329+
CCLOG("Warning: program %s not found in cache.", key.c_str());
330+
return nullptr;
306331
}
307332

308333
CC_BACKEND_END

cocos/renderer/backend/ProgramCache.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ class ProgramCache : public Ref
6969
*/
7070
void removeAllPrograms();
7171

72+
/**
73+
* Add custom program to the cache.
74+
* @param key Specifies the key used to store the program.
75+
* @param program Specifies the program to store in the cache.
76+
*/
77+
void addCustomProgram(const std::string& key, backend::Program* program);
78+
79+
/// Get custom program from cache.
80+
backend::Program* getCustomProgram(const std::string& key) const;
81+
7282
protected:
7383
ProgramCache() = default;
7484
virtual ~ProgramCache();
@@ -82,6 +92,7 @@ class ProgramCache : public Ref
8292
void addProgram(ProgramType type);
8393

8494
static std::unordered_map<backend::ProgramType, backend::Program*> _cachedPrograms; ///< The cached program object.
95+
static std::unordered_map<std::string, backend::Program*> _cachedCustomPrograms; ///< The cached custom program object.
8596
static ProgramCache *_sharedProgramCache; ///< A shared instance of the program cache.
8697
};
8798

0 commit comments

Comments
 (0)