Fix issue with zero size cache.
PiperOrigin-RevId: 819773517 Change-Id: Iff1523e17ffbd1dff02bee71db896c64bd8b565c
This commit is contained in:
committed by
Copybara-Service
parent
5715cebbb9
commit
8cbfc42767
@@ -1783,14 +1783,12 @@ void mj_clearCache(mjCache* cache) {
|
|||||||
|
|
||||||
// get the internal asset cache used by the compiler
|
// get the internal asset cache used by the compiler
|
||||||
mjCache* mj_getCache() {
|
mjCache* mj_getCache() {
|
||||||
|
static mjCache cache_cwrapper = {0};
|
||||||
// mjCCache is not trivially destructible and so the global cache needs to
|
// mjCCache is not trivially destructible and so the global cache needs to
|
||||||
// allocated on the heap
|
// allocated on the heap
|
||||||
if constexpr (kGlobalCacheSize != 0) {
|
if constexpr (kGlobalCacheSize != 0) {
|
||||||
static mjCCache* cache = new(std::nothrow) mjCCache(kGlobalCacheSize);
|
static mjCCache* cache = new(std::nothrow) mjCCache(kGlobalCacheSize);
|
||||||
static mjCache cache_cwrapper;
|
cache_cwrapper.impl_ = cache->Capacity() > 0 ? cache : nullptr;
|
||||||
cache_cwrapper.impl_ = cache;
|
|
||||||
return cache->Capacity() > 0 ? &cache_cwrapper : nullptr;
|
|
||||||
} else {
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
return &cache_cwrapper;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,10 +116,10 @@ bool mjCCache::PopulateData(const std::string& id, const mjResource* resource, m
|
|||||||
}
|
}
|
||||||
|
|
||||||
mjCAsset* asset = &(it->second);
|
mjCAsset* asset = &(it->second);
|
||||||
asset->IncrementAccess();
|
|
||||||
|
|
||||||
// update priority queue
|
// update priority queue
|
||||||
entries_.erase(asset);
|
entries_.erase(asset);
|
||||||
|
asset->IncrementAccess();
|
||||||
entries_.insert(asset);
|
entries_.insert(asset);
|
||||||
|
|
||||||
return asset->PopulateData(fn);
|
return asset->PopulateData(fn);
|
||||||
|
|||||||
@@ -751,6 +751,42 @@ TEST_F(PluginTest, RecompileComparePngCache) {
|
|||||||
mj_deleteVFS(vfs.get());
|
mj_deleteVFS(vfs.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(PluginTest, DisableCache) {
|
||||||
|
static constexpr char xml[] = R"(
|
||||||
|
<mujoco>
|
||||||
|
<asset>
|
||||||
|
<texture content_type="image/png" file="tex.png" type="2d"/>
|
||||||
|
<material name="material" texture="tex"/>
|
||||||
|
</asset>
|
||||||
|
|
||||||
|
<worldbody>
|
||||||
|
<geom type="plane" material="material" size="4 4 4"/>
|
||||||
|
</worldbody>
|
||||||
|
</mujoco>
|
||||||
|
)";
|
||||||
|
|
||||||
|
mjCache* cache = mj_getCache();
|
||||||
|
std::size_t capacity = mj_getCacheCapacity(cache);
|
||||||
|
mj_setCacheCapacity(cache, 0);
|
||||||
|
|
||||||
|
auto vfs = std::make_unique<mjVFS>();
|
||||||
|
mj_defaultVFS(vfs.get());
|
||||||
|
mj_addBufferVFS(vfs.get(), "tex.png", tex1, sizeof(tex1));
|
||||||
|
|
||||||
|
std::array<char, 1024> error;
|
||||||
|
|
||||||
|
// load model once
|
||||||
|
mjModel* m = LoadModelFromString(xml, error.data(), error.size(), vfs.get());
|
||||||
|
|
||||||
|
EXPECT_EQ(mj_getCacheSize(cache), 0);
|
||||||
|
EXPECT_EQ(m->ntexdata, 18); // w x h x rgb = 3 x 2 x 3
|
||||||
|
|
||||||
|
mj_setCacheCapacity(cache, capacity);
|
||||||
|
|
||||||
|
mj_deleteModel(m);
|
||||||
|
mj_deleteVFS(vfs.get());
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------- test textures ------------------------------
|
// -------------------------------- test textures ------------------------------
|
||||||
|
|
||||||
TEST_F(PluginTest, TextureFromBuffer) {
|
TEST_F(PluginTest, TextureFromBuffer) {
|
||||||
|
|||||||
Reference in New Issue
Block a user