@@ -117,21 +117,26 @@ void DefaultBinaryIO::_write(const luisa::string &file_path, luisa::span<std::by
117
117
_unlock (idx, true );
118
118
}
119
119
120
- DefaultBinaryIO::DefaultBinaryIO (Context &&ctx, bool headless) noexcept
120
+ DefaultBinaryIO::DefaultBinaryIO (Context &&ctx, bool headless, bool use_lmdb ) noexcept
121
121
: _ctx(std::move(ctx)),
122
- _headless (headless) {
122
+ _headless (headless),
123
+ _use_lmdb(use_lmdb) {
123
124
if (!headless) {
124
125
_cache_dir = _ctx.create_runtime_subdir (" .cache" sv);
125
126
_data_dir = _ctx.create_runtime_subdir (" .data" sv);
126
- _data_lmdb.create (_data_dir, std::max<size_t >(126ull , std::thread::hardware_concurrency () * 2 ));
127
- _cache_lmdb.create (_cache_dir, std::max<size_t >(126ull , std::thread::hardware_concurrency () * 2 ));
127
+ if (use_lmdb) {
128
+ _data_lmdb.create (_data_dir, std::max<size_t >(126ull , std::thread::hardware_concurrency () * 2 ));
129
+ _cache_lmdb.create (_cache_dir, std::max<size_t >(126ull , std::thread::hardware_concurrency () * 2 ));
130
+ }
128
131
}
129
132
}
130
133
131
134
DefaultBinaryIO::~DefaultBinaryIO () noexcept {
132
135
if (!_headless) {
133
- _data_lmdb.destroy ();
134
- _cache_lmdb.destroy ();
136
+ if (_use_lmdb) {
137
+ _data_lmdb.destroy ();
138
+ _cache_lmdb.destroy ();
139
+ }
135
140
}
136
141
}
137
142
@@ -145,15 +150,33 @@ luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_bytecode(luisa::str
145
150
}
146
151
147
152
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_cache (luisa::string_view name) const noexcept {
148
- auto r = _cache_lmdb->read (name);
149
- if (r.empty ()) return {};
150
- return luisa::make_unique<LMDBBinaryStream>(r.data (), r.size ());
153
+ if (_use_lmdb) {
154
+ auto r = _cache_lmdb->read (name);
155
+ if (r.empty ()) return {};
156
+ return luisa::make_unique<LMDBBinaryStream>(r.data (), r.size ());
157
+ } else {
158
+ std::filesystem::path local_path{name};
159
+ if (local_path.is_absolute ()) {
160
+ return _read (luisa::to_string (name));
161
+ }
162
+ auto file_path = luisa::to_string (_cache_dir / name);
163
+ return _read (file_path);
164
+ }
151
165
}
152
166
153
167
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_internal_shader (luisa::string_view name) const noexcept {
154
- auto r = _data_lmdb->read (name);
155
- if (r.empty ()) return {};
156
- return luisa::make_unique<LMDBBinaryStream>(r.data (), r.size ());
168
+ if (_use_lmdb) {
169
+ auto r = _data_lmdb->read (name);
170
+ if (r.empty ()) return {};
171
+ return luisa::make_unique<LMDBBinaryStream>(r.data (), r.size ());
172
+ } else {
173
+ std::filesystem::path local_path{name};
174
+ if (local_path.is_absolute ()) {
175
+ return _read (luisa::to_string (name));
176
+ }
177
+ auto file_path = luisa::to_string (_data_dir / name);
178
+ return _read (file_path);
179
+ }
157
180
}
158
181
159
182
luisa::unique_ptr<BinaryStream> DefaultBinaryIO::read_shader_source (luisa::string_view name) const noexcept {
@@ -185,16 +208,39 @@ luisa::filesystem::path DefaultBinaryIO::write_shader_source(luisa::string_view
185
208
}
186
209
187
210
luisa::filesystem::path DefaultBinaryIO::write_shader_cache (luisa::string_view name, luisa::span<std::byte const > data) const noexcept {
188
- _cache_lmdb->write (name, data);
189
- return _cache_dir / name;
211
+ if (_use_lmdb) {
212
+ _cache_lmdb->write (name, data);
213
+ return _cache_dir / name;
214
+ } else {
215
+ std::filesystem::path local_path{name};
216
+ if (local_path.is_absolute ()) {
217
+ _write (luisa::to_string (name), data);
218
+ return local_path;
219
+ }
220
+ auto file_path = _cache_dir / name;
221
+ _write (luisa::to_string (file_path), data);
222
+ return file_path;
223
+ }
190
224
}
191
225
192
226
luisa::filesystem::path DefaultBinaryIO::write_internal_shader (luisa::string_view name, luisa::span<std::byte const > data) const noexcept {
193
- _data_lmdb->write (name, data);
194
- return _data_dir / name;
227
+ if (_use_lmdb) {
228
+ _data_lmdb->write (name, data);
229
+ return _data_dir / name;
230
+ } else {
231
+ std::filesystem::path local_path{name};
232
+ if (local_path.is_absolute ()) {
233
+ _write (luisa::to_string (name), data);
234
+ return local_path;
235
+ }
236
+ auto file_path = _data_dir / name;
237
+ _write (luisa::to_string (file_path), data);
238
+ return file_path;
239
+ }
195
240
}
196
241
197
242
void DefaultBinaryIO::clear_shader_cache () const noexcept {
243
+ if (!_use_lmdb) return ;
198
244
_cache_lmdb.destroy ();
199
245
std::error_code ec;
200
246
for (auto &&dir : std::filesystem::directory_iterator (_cache_dir)) {
0 commit comments