@@ -80,22 +80,6 @@ PNGImage PNGImage::Load(const mjCBase* obj, mjResource* resource,
80
80
LodePNGColorType color_type) {
81
81
PNGImage image;
82
82
image.color_type_ = color_type;
83
- mjCCache *cache = reinterpret_cast <mjCCache*>(mj_getCache ()->impl_ );
84
-
85
- // cache callback
86
- auto callback = [&image](const void * data) {
87
- const PNGImage *cached_image = static_cast <const PNGImage*>(data);
88
- if (cached_image->color_type_ == image.color_type_ ) {
89
- image = *cached_image;
90
- return true ;
91
- }
92
- return false ;
93
- };
94
-
95
- // try loading from cache
96
- if (cache && cache->PopulateData (resource->name , resource, callback)) {
97
- return image;
98
- }
99
83
100
84
// open PNG resource
101
85
const unsigned char * buffer;
@@ -146,16 +130,6 @@ PNGImage PNGImage::Load(const mjCBase* obj, mjResource* resource,
146
130
throw mjCError (obj, " %s" , ss.str ().c_str ());
147
131
}
148
132
149
- // insert raw image data into cache
150
- if (cache) {
151
- PNGImage *cached_image = new PNGImage (image);;
152
- std::size_t size = image.Size ();
153
- std::shared_ptr<const void > cached_data (cached_image, +[] (const void * data) {
154
- delete static_cast <const PNGImage*>(data);
155
- });
156
- cache->Insert (" " , resource->name , resource, cached_data, size);
157
- }
158
-
159
133
return image;
160
134
}
161
135
@@ -4397,6 +4371,12 @@ mjCHField::~mjCHField() {
4397
4371
}
4398
4372
4399
4373
4374
+ std::string mjCHField::GetCacheId (const mjResource* resource, const std::string& asset_type) {
4375
+ std::stringstream ss;
4376
+ ss << " mjCHField:" << resource->name << " ;ARGS:content_type=" << asset_type;
4377
+ return ss.str ();
4378
+ }
4379
+
4400
4380
4401
4381
// load elevation data from custom format
4402
4382
void mjCHField::LoadCustom (mjResource* resource) {
@@ -4493,6 +4473,8 @@ void mjCHField::Compile(const mjVFS* vfs) {
4493
4473
throw mjCError (this , " hfield specified from file and manually" );
4494
4474
}
4495
4475
4476
+ mjCCache* cache = reinterpret_cast <mjCCache*>(mj_getCache ()->impl_ );
4477
+
4496
4478
std::string asset_type = GetAssetContentType (file_, content_type_);
4497
4479
4498
4480
// fallback to custom
@@ -4514,16 +4496,46 @@ void mjCHField::Compile(const mjVFS* vfs) {
4514
4496
FilePath filename = meshdir_ + FilePath (file_);
4515
4497
mjResource* resource = LoadResource (modelfiledir_.Str (), filename.Str (), vfs);
4516
4498
4517
- try {
4518
- if (asset_type == " image/png" ) {
4519
- LoadPNG (resource);
4520
- } else {
4521
- LoadCustom (resource);
4522
- }
4499
+ struct CachedHField {
4500
+ int nrow, ncol;
4501
+ std::vector<float > data;
4502
+ };
4503
+
4504
+ // cache callback
4505
+ auto callback = [&](const void * cached_data) {
4506
+ const CachedHField* cached_hfield =
4507
+ static_cast <const CachedHField*>(cached_data);
4508
+ nrow = cached_hfield->nrow ;
4509
+ ncol = cached_hfield->ncol ;
4510
+ this ->data = cached_hfield->data ;
4511
+ return true ;
4512
+ };
4513
+
4514
+ // try loading from cache
4515
+ if (cache && cache->PopulateData (GetCacheId (resource, asset_type), resource, callback)) {
4523
4516
mju_closeResource (resource);
4524
- } catch (mjCError err) {
4517
+ } else {
4518
+ try {
4519
+ if (asset_type == " image/png" ) {
4520
+ LoadPNG (resource);
4521
+ } else {
4522
+ LoadCustom (resource);
4523
+ }
4524
+ } catch (mjCError err) {
4525
+ mju_closeResource (resource);
4526
+ throw err;
4527
+ }
4528
+
4529
+ if (cache) {
4530
+ CachedHField* cached_hfield = new CachedHField;
4531
+ cached_hfield->nrow = nrow;
4532
+ cached_hfield->ncol = ncol;
4533
+ cached_hfield->data = this ->data ;
4534
+ std::size_t size = sizeof (CachedHField) + this ->data .size () * sizeof (float );
4535
+ std::shared_ptr<CachedHField> cached_data{cached_hfield};
4536
+ cache->Insert (" " , GetCacheId (resource, asset_type), resource, cached_data, size);
4537
+ }
4525
4538
mju_closeResource (resource);
4526
- throw err;
4527
4539
}
4528
4540
}
4529
4541
@@ -5021,10 +5033,36 @@ void mjCTexture::FlipIfNeeded(std::vector<std::byte>& image, unsigned int w,
5021
5033
}
5022
5034
}
5023
5035
5036
+ std::string mjCTexture::GetCacheId (const mjResource* resource, const std::string& asset_type) {
5037
+ std::stringstream ss;
5038
+ ss << resource->name << " ;ARGS:content_type=" << asset_type << " ,nchannel=" << nchannel
5039
+ << " ,hflip=" << hflip << " ,vflip=" << vflip << " ;" ;
5040
+ return ss.str ();
5041
+ }
5042
+
5024
5043
// load from PNG or custom file, flip if specified
5025
5044
void mjCTexture::LoadFlip (std::string filename, const mjVFS* vfs,
5026
5045
std::vector<std::byte>& image,
5027
5046
unsigned int & w, unsigned int & h, bool & is_srgb) {
5047
+ mjCCache* cache = reinterpret_cast <mjCCache*>(mj_getCache ()->impl_ );
5048
+
5049
+ struct CachedImage {
5050
+ unsigned int w, h, n_ch;
5051
+ bool is_srgb;
5052
+ std::vector<std::byte> image;
5053
+ };
5054
+
5055
+ // cache callback
5056
+ auto callback = [&](const void * data) {
5057
+ const CachedImage* cached_image =
5058
+ static_cast <const CachedImage*>(data);
5059
+ w = cached_image->w ;
5060
+ h = cached_image->h ;
5061
+ is_srgb = cached_image->is_srgb ;
5062
+ image = cached_image->image ;
5063
+ return true ;
5064
+ };
5065
+
5028
5066
std::string asset_type = GetAssetContentType (filename, content_type_);
5029
5067
5030
5068
// fallback to custom
@@ -5036,7 +5074,12 @@ void mjCTexture::LoadFlip(std::string filename, const mjVFS* vfs,
5036
5074
throw mjCError (this , " unsupported content type: '%s'" , asset_type.c_str ());
5037
5075
}
5038
5076
5077
+ // try loading from cache
5039
5078
mjResource* resource = LoadResource (modelfiledir_.Str (), filename, vfs);
5079
+ if (cache && cache->PopulateData (GetCacheId (resource, asset_type), resource, callback)) {
5080
+ mju_closeResource (resource);
5081
+ return ;
5082
+ }
5040
5083
5041
5084
try {
5042
5085
if (asset_type == " image/png" ) {
@@ -5049,13 +5092,23 @@ void mjCTexture::LoadFlip(std::string filename, const mjVFS* vfs,
5049
5092
} else {
5050
5093
LoadCustom (resource, image, w, h, is_srgb);
5051
5094
}
5052
- mju_closeResource (resource);
5053
5095
} catch (mjCError err) {
5054
5096
mju_closeResource (resource);
5055
5097
throw err;
5056
5098
}
5057
5099
5058
5100
FlipIfNeeded (image, w, h);
5101
+ if (cache) {
5102
+ CachedImage* cached_texture = new CachedImage;
5103
+ cached_texture->w = w;
5104
+ cached_texture->h = h;
5105
+ cached_texture->is_srgb = is_srgb;
5106
+ cached_texture->image = image;
5107
+ std::size_t size = sizeof (CachedImage) + image.size ();
5108
+ std::shared_ptr<CachedImage> cached_data{cached_texture};
5109
+ cache->Insert (" " , GetCacheId (resource, asset_type), resource, cached_data, size);
5110
+ }
5111
+ mju_closeResource (resource);
5059
5112
}
5060
5113
5061
5114
// load 2D
0 commit comments