Debian Codegraph @

Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/Assets/DirectFilesystemAssetLibrary.h"
5
6 #include <vector>
7
8 #include <fmt/std.h>
9
10 #include "Common/CommonPaths.h"
11 #include "Common/FileUtil.h"
12 #include "Common/IOFile.h"
13 #include "Common/JsonUtil.h"
14 #include "Common/Logging/Log.h"
15 #include "Common/StringUtil.h"
16 #include "Core/System.h"
17 #include "VideoCommon/Assets/MaterialAsset.h"
18 #include "VideoCommon/Assets/MeshAsset.h"
19 #include "VideoCommon/Assets/ShaderAsset.h"
20 #include "VideoCommon/Assets/TextureAsset.h"
21 #include "VideoCommon/Assets/TextureAssetUtils.h"
22 #include "VideoCommon/RenderState.h"
23 #include "VideoCommon/Resources/CustomResourceManager.h"
24
25 namespace VideoCommon
26 {
27 namespace
28 {
29 std::size_t GetAssetSize(const CustomTextureData& data)
30 {
31 std::size_t total = 0;
32 for (const auto& slice : data.m_slices)
33 {
34 for (const auto& level : slice.m_levels)
35 {
36 total += level.data.size();
37 }
38 }
39 return total;
40 }
41 } // namespace
42
43 CustomAssetLibrary::LoadInfo
44 DirectFilesystemAssetLibrary::LoadRasterSurfaceShader(const AssetID& asset_id,
45 RasterSurfaceShaderData* data)
46 {
47 const auto asset_map = GetAssetMapForID(asset_id);
48
49 // Asset map for a pixel shader is the shader and some metadata
50 if (asset_map.size() != 3)
51 {
52 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have three files mapped!", asset_id);
53 return {};
54 }
55
56 const auto metadata = asset_map.find("metadata");
57 if (metadata == asset_map.end())
58 {
59 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a metadata entry mapped!", asset_id);
60 return {};
61 }
62
63 const auto vertex_shader = asset_map.find("vertex_shader");
64 if (vertex_shader == asset_map.end())
65 {
66 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a vertex shader entry mapped!", asset_id);
67 return {};
68 }
69
70 const auto pixel_shader = asset_map.find("pixel_shader");
71 if (pixel_shader == asset_map.end())
72 {
73 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a pixel shader entry mapped!", asset_id);
74 return {};
75 }
76
77 std::size_t metadata_size;
78 {
79 std::error_code ec;
80 metadata_size = std::filesystem::file_size(metadata->second, ec);
81 if (ec)
82 {
83 ERROR_LOG_FMT(VIDEO,
84 "Asset '{}' error - failed to get shader metadata file size with error '{}'!",
85 asset_id, ec);
86 return {};
87 }
88 }
89 std::size_t vertex_shader_size;
90 {
91 std::error_code ec;
92 vertex_shader_size = std::filesystem::file_size(vertex_shader->second, ec);
93 if (ec)
94 {
95 ERROR_LOG_FMT(
96 VIDEO, "Asset '{}' error - failed to get vertex shader source file size with error '{}'!",
97 asset_id, ec);
98 return {};
99 }
100 }
101 std::size_t pixel_shader_size;
102 {
103 std::error_code ec;
104 pixel_shader_size = std::filesystem::file_size(pixel_shader->second, ec);
105 if (ec)
106 {
107 ERROR_LOG_FMT(
108 VIDEO, "Asset '{}' error - failed to get pixel shader source file size with error '{}'!",
109 asset_id, ec);
110 return {};
111 }
112 }
113 const auto approx_mem_size = metadata_size + vertex_shader_size + pixel_shader_size;
114
115 if (!File::ReadFileToString(PathToString(vertex_shader->second), data->vertex_source))
116 {
117 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the vertex shader file '{}',",
118 asset_id, PathToString(vertex_shader->second));
119 return {};
120 }
121
122 if (!File::ReadFileToString(PathToString(pixel_shader->second), data->pixel_source))
123 {
124 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the pixel shader file '{}',", asset_id,
125 PathToString(pixel_shader->second));
126 return {};
127 }
128
129 picojson::value root;
130 std::string error;
131 if (!JsonFromFile(PathToString(metadata->second), &root, &error))
132 {
133 ERROR_LOG_FMT(VIDEO,
134 "Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
135 asset_id, PathToString(metadata->second), error);
136 return {};
137 }
138
139 if (!root.is<picojson::object>())
140 {
141 ERROR_LOG_FMT(
142 VIDEO,
143 "Asset '{}' error - failed to load the json file '{}', due to root not being an object!",
144 asset_id, PathToString(metadata->second));
145 return {};
146 }
147
148 const auto& root_obj = root.get<picojson::object>();
149
150 if (!RasterSurfaceShaderData::FromJson(asset_id, root_obj, data))
151 return {};
152
153 const std::string graphics_mod_builtin =
154 File::GetSysDirectory() + GRAPHICSMOD_DIR + "/Builtin" + "/Shaders";
155
156 data->shader_includer = std::make_unique<VideoCommon::ShaderIncluder>(
157 PathToString(pixel_shader->second.parent_path()), graphics_mod_builtin);
158
159 return LoadInfo{approx_mem_size};
160 }
161
162 CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMaterial(const AssetID& asset_id,
163 MaterialData* data)
164 {
165 const auto asset_map = GetAssetMapForID(asset_id);
166
167 // Material is expected to have one asset mapped
168 if (asset_map.empty() || asset_map.size() > 1)
169 {
170 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - material expected to have one file mapped!", asset_id);
171 return {};
172 }
173 const auto& asset_path = asset_map.begin()->second;
174
175 std::size_t metadata_size;
176 {
177 std::error_code ec;
178 metadata_size = std::filesystem::file_size(asset_path, ec);
179 if (ec)
180 {
181 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to get material file size with error '{}'!",
182 asset_id, ec);
183 return {};
184 }
185 }
186
187 picojson::value root;
188 std::string error;
189 if (!JsonFromFile(PathToString(asset_path), &root, &error))
190 {
191 ERROR_LOG_FMT(
192 VIDEO,
193 "Asset '{}' error - material failed to load the json file '{}', due to parse error: {}",
194 asset_id, PathToString(asset_path), error);
195 return {};
196 }
197 if (!root.is<picojson::object>())
198 {
199 ERROR_LOG_FMT(VIDEO,
200 "Asset '{}' error - material failed to load the json file '{}', due to root not "
201 "being an object!",
202 asset_id, PathToString(asset_path));
203 return {};
204 }
205
206 const auto& root_obj = root.get<picojson::object>();
207
208 if (!MaterialData::FromJson(asset_id, root_obj, data))
209 {
210 ERROR_LOG_FMT(VIDEO,
211 "Asset '{}' error - material failed to load the json file '{}', as material "
212 "json could not be parsed!",
213 asset_id, PathToString(asset_path));
214 return {};
215 }
216
217 return LoadInfo{metadata_size};
218 }
219
220 CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadMesh(const AssetID& asset_id,
221 MeshData* data)
222 {
223 const auto asset_map = GetAssetMapForID(asset_id);
224
225 // Asset map for a mesh is the mesh and some metadata
226 if (asset_map.size() != 2)
227 {
228 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have two files mapped!", asset_id);
229 return {};
230 }
231
232 const auto metadata = asset_map.find("metadata");
233 const auto mesh = asset_map.find("mesh");
234 if (metadata == asset_map.end())
235 {
236 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a metadata entry mapped!", asset_id);
237 return {};
238 }
239
240 if (mesh == asset_map.end())
241 {
242 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a mesh entry mapped!", asset_id);
243 return {};
244 }
245
246 std::size_t metadata_size;
247 {
248 std::error_code ec;
249 metadata_size = std::filesystem::file_size(metadata->second, ec);
250 if (ec)
251 {
252 ERROR_LOG_FMT(VIDEO,
253 "Asset '{}' error - failed to get mesh metadata file size with error '{}'!",
254 asset_id, ec);
255 return {};
256 }
257 }
258 std::size_t mesh_size;
259 {
260 std::error_code ec;
261 mesh_size = std::filesystem::file_size(mesh->second, ec);
262 if (ec)
263 {
264 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to get mesh file size with error '{}'!",
265 asset_id, ec);
266 return {};
267 }
268 }
269 const auto approx_mem_size = metadata_size + mesh_size;
270
271 File::IOFile file(PathToString(mesh->second), "rb");
272 if (!file.IsOpen())
273 {
274 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to open mesh file '{}'!", asset_id,
275 PathToString(mesh->second));
276 return {};
277 }
278
279 std::vector<u8> bytes;
280 bytes.reserve(file.GetSize());
281 file.ReadBytes(bytes.data(), file.GetSize());
282 if (!MeshData::FromDolphinMesh(bytes, data))
283 {
284 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the mesh file '{}'!", asset_id,
285 PathToString(mesh->second));
286 return {};
287 }
288
289 picojson::value root;
290 std::string error;
291 if (!JsonFromFile(PathToString(metadata->second), &root, &error))
292 {
293 ERROR_LOG_FMT(VIDEO,
294 "Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
295 asset_id, PathToString(metadata->second), error);
296 return {};
297 }
298 if (!root.is<picojson::object>())
299 {
300 ERROR_LOG_FMT(
301 VIDEO,
302 "Asset '{}' error - failed to load the json file '{}', due to root not being an object!",
303 asset_id, PathToString(metadata->second));
304 return {};
305 }
306
307 const auto& root_obj = root.get<picojson::object>();
308
309 if (!MeshData::FromJson(asset_id, root_obj, data))
310 return {};
311
312 return LoadInfo{approx_mem_size};
313 }
314
315 CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadTexture(const AssetID& asset_id,
316 CustomTextureData* data)
317 {
318 const auto asset_map = GetAssetMapForID(asset_id);
319 if (asset_map.empty())
320 {
321 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - raw texture expected to have one or two files mapped!",
322 asset_id);
323 return {};
324 }
325
326 const auto texture_path = asset_map.find("texture");
327
328 if (texture_path == asset_map.end())
329 {
330 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a texture entry mapped!", asset_id);
331 return {};
332 }
333
334 if (!LoadTextureDataFromFile(asset_id, texture_path->second, AbstractTextureType::Texture_2D,
335 data))
336 {
337 return {};
338 }
339 if (!PurgeInvalidMipsFromTextureData(asset_id, data))
340 return {};
341
342 return LoadInfo{GetAssetSize(*data)};
343 }
344
345 CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadTexture(const AssetID& asset_id,
346 TextureAndSamplerData* data)
347 {
348 const auto asset_map = GetAssetMapForID(asset_id);
349
350 // Texture can optionally have a metadata file as well
351 if (asset_map.empty() || asset_map.size() > 2)
352 {
353 ERROR_LOG_FMT(VIDEO, "Asset '{}' error - raw texture expected to have one or two files mapped!",
354 asset_id);
355 return {};
356 }
357
358 const auto metadata = asset_map.find("metadata");
359 const auto texture_path = asset_map.find("texture");
360
361 if (texture_path == asset_map.end())
362 {
363 ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a texture entry mapped!", asset_id);
364 return {};
365 }
366
367 std::size_t metadata_size = 0;
368 if (metadata != asset_map.end())
369 {
370 std::error_code ec;
371 metadata_size = std::filesystem::file_size(metadata->second, ec);
372 if (ec)
373 {
374 ERROR_LOG_FMT(VIDEO,
375 "Asset '{}' error - failed to get texture metadata file size with error '{}'!",
376 asset_id, ec);
377 return {};
378 }
379
380 picojson::value root;
381 std::string error;
382 if (!JsonFromFile(PathToString(metadata->second), &root, &error))
383 {
384 ERROR_LOG_FMT(VIDEO,
385 "Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
386 asset_id, PathToString(metadata->second), error);
387 return {};
388 }
389 if (!root.is<picojson::object>())
390 {
391 ERROR_LOG_FMT(
392 VIDEO,
393 "Asset '{}' error - failed to load the json file '{}', due to root not being an object!",
394 asset_id, PathToString(metadata->second));
395 return {};
396 }
397
398 const auto& root_obj = root.get<picojson::object>();
399 if (!TextureAndSamplerData::FromJson(asset_id, root_obj, data))
400 {
401 return {};
402 }
403 }
404 else
405 {
406 data->sampler = RenderState::GetLinearSamplerState();
407 data->type = AbstractTextureType::Texture_2D;
408 }
409
410 if (!LoadTextureDataFromFile(asset_id, texture_path->second, data->type, &data->texture_data))
411 return {};
412 if (!PurgeInvalidMipsFromTextureData(asset_id, &data->texture_data))
413 return {};
414
415 return LoadInfo{GetAssetSize(data->texture_data) + metadata_size};
416 }
417
418 void DirectFilesystemAssetLibrary::SetAssetIDMapData(const AssetID& asset_id,
419 VideoCommon::Assets::AssetMap asset_path_map)
420 {
421 VideoCommon::Assets::AssetMap previous_asset_map;
422 {
423 std::lock_guard lk(m_asset_map_lock);
424 previous_asset_map = m_asset_id_to_asset_map_path[asset_id];
425 }
426
427 {
428 std::lock_guard lk(m_path_map_lock);
429 for (const auto& [name, path] : previous_asset_map)
430 {
431 m_path_to_asset_id.erase(PathToString(path));
432 }
433
434 for (const auto& [name, path] : asset_path_map)
435 {
436 m_path_to_asset_id[PathToString(path)] = asset_id;
437 }
438 }
439
440 {
441 std::lock_guard lk(m_asset_map_lock);
442 m_asset_id_to_asset_map_path[asset_id] = std::move(asset_path_map);
443 }
444 }
445
446 void DirectFilesystemAssetLibrary::PathModified(std::string_view path)
447 {
448 std::lock_guard lk(m_path_map_lock);
449 if (const auto iter = m_path_to_asset_id.find(path); iter != m_path_to_asset_id.end())
450 {
451 auto& system = Core::System::GetInstance();
452 auto& resource_manager = system.GetCustomResourceManager();
453 resource_manager.MarkAssetDirty(iter->second);
454 }
455 }
456
457 VideoCommon::Assets::AssetMap
458 DirectFilesystemAssetLibrary::GetAssetMapForID(const AssetID& asset_id) const
459 {
460 std::lock_guard lk(m_asset_map_lock);
461 if (auto iter = m_asset_id_to_asset_map_path.find(asset_id);
462 iter != m_asset_id_to_asset_map_path.end())
463 {
464 return iter->second;
465 }
466 return {};
467 }
468 } // namespace VideoCommon