Debian Codegraph @

Source/Core/VideoCommon/Assets/MaterialAsset.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/Assets/MaterialAsset.h"
5
6 #include <algorithm>
7 #include <string_view>
8 #include <vector>
9
10 #include "Common/JsonUtil.h"
11 #include "Common/Logging/Log.h"
12 #include "Common/StringUtil.h"
13 #include "Common/VariantUtil.h"
14 #include "VideoCommon/Assets/CustomAssetLibrary.h"
15
16 namespace VideoCommon
17 {
18 namespace
19 {
20 // While not optimal, we pad our data to match std140 shader requirements
21 // this memory constant indicates the memory stride for a single uniform
22 // regardless of data type
23 constexpr std::size_t MemorySize = sizeof(float) * 4;
24
25 template <typename ElementType, std::size_t ElementCount>
26 bool ParseNumeric(const CustomAssetLibrary::AssetID& asset_id, const picojson::value& json_value,
27 MaterialProperty::Value* value)
28 {
29 static_assert(ElementCount <= 4, "Numeric data expected to be four elements or less");
30 if constexpr (ElementCount == 1)
31 {
32 if (!json_value.is<double>())
33 {
34 ERROR_LOG_FMT(VIDEO,
35 "Asset id '{}' material has attribute where "
36 "a double was expected but not provided.",
37 asset_id);
38 return false;
39 }
40
41 *value = static_cast<ElementType>(json_value.get<double>());
42 }
43 else
44 {
45 if (!json_value.is<picojson::array>())
46 {
47 ERROR_LOG_FMT(VIDEO,
48 "Asset id '{}' material has attribute where "
49 "an array was expected but not provided.",
50 asset_id);
51 return false;
52 }
53
54 const auto json_data = json_value.get<picojson::array>();
55
56 if (json_data.size() != ElementCount)
57 {
58 ERROR_LOG_FMT(VIDEO,
59 "Asset id '{}' material has attribute with incorrect number "
60 "of elements, expected {}",
61 asset_id, ElementCount);
62 return false;
63 }
64
65 if (!std::ranges::all_of(json_data, &picojson::value::is<double>))
66 {
67 ERROR_LOG_FMT(VIDEO,
68 "Asset id '{}' material has attribute where "
69 "all elements are not of type double.",
70 asset_id);
71 return false;
72 }
73
74 std::array<ElementType, ElementCount> data;
75 for (std::size_t i = 0; i < ElementCount; i++)
76 {
77 data[i] = static_cast<ElementType>(json_data[i].get<double>());
78 }
79 *value = std::move(data);
80 }
81
82 return true;
83 }
84 bool ParsePropertyValue(const CustomAssetLibrary::AssetID& asset_id,
85 const picojson::value& json_value, std::string_view type,
86 MaterialProperty::Value* value)
87 {
88 if (type == "int")
89 {
90 return ParseNumeric<s32, 1>(asset_id, json_value, value);
91 }
92 else if (type == "int2")
93 {
94 return ParseNumeric<s32, 2>(asset_id, json_value, value);
95 }
96 else if (type == "int3")
97 {
98 return ParseNumeric<s32, 3>(asset_id, json_value, value);
99 }
100 else if (type == "int4")
101 {
102 return ParseNumeric<s32, 4>(asset_id, json_value, value);
103 }
104 else if (type == "float")
105 {
106 return ParseNumeric<float, 1>(asset_id, json_value, value);
107 }
108 else if (type == "float2")
109 {
110 return ParseNumeric<float, 2>(asset_id, json_value, value);
111 }
112 else if (type == "float3")
113 {
114 return ParseNumeric<float, 3>(asset_id, json_value, value);
115 }
116 else if (type == "float4")
117 {
118 return ParseNumeric<float, 4>(asset_id, json_value, value);
119 }
120 else if (type == "bool")
121 {
122 if (json_value.is<bool>())
123 {
124 *value = json_value.get<bool>();
125 return true;
126 }
127 }
128
129 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse the json, value is not valid for type '{}'",
130 asset_id, type);
131 return false;
132 }
133
134 bool ParseMaterialProperties(const CustomAssetLibrary::AssetID& asset_id,
135 const picojson::array& values_data,
136 std::vector<MaterialProperty>* material_property)
137 {
138 for (const auto& value_data : values_data)
139 {
140 VideoCommon::MaterialProperty property;
141 if (!value_data.is<picojson::object>())
142 {
143 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse the json, value is not the right json type",
144 asset_id);
145 return false;
146 }
147 const auto& value_data_obj = value_data.get<picojson::object>();
148
149 const auto type_iter = value_data_obj.find("type");
150 if (type_iter == value_data_obj.end())
151 {
152 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse the json, value entry 'type' not found",
153 asset_id);
154 return false;
155 }
156 if (!type_iter->second.is<std::string>())
157 {
158 ERROR_LOG_FMT(VIDEO,
159 "Asset '{}' failed to parse the json, value entry 'type' is not "
160 "the right json type",
161 asset_id);
162 return false;
163 }
164 std::string type = type_iter->second.to_str();
165 Common::ToLower(&type);
166
167 const auto value_iter = value_data_obj.find("value");
168 if (value_iter != value_data_obj.end())
169 {
170 if (!ParsePropertyValue(asset_id, value_iter->second, type, &property.m_value))
171 {
172 return false;
173 }
174 }
175
176 material_property->push_back(std::move(property));
177 }
178
179 return true;
180 }
181 } // namespace
182
183 void MaterialProperty::WriteToMemory(u8*& buffer, const MaterialProperty& property)
184 {
185 const auto write_memory = [&](const void* raw_value, std::size_t data_size) {
186 std::memcpy(buffer, raw_value, data_size);
187 std::memset(buffer + data_size, 0, MemorySize - data_size);
188 buffer += MemorySize;
189 };
190 std::visit(
191 overloaded{
192 [&](s32 value) { write_memory(&value, sizeof(s32)); },
193 [&](const std::array<s32, 2>& value) { write_memory(value.data(), sizeof(s32) * 2); },
194 [&](const std::array<s32, 3>& value) { write_memory(value.data(), sizeof(s32) * 3); },
195 [&](const std::array<s32, 4>& value) { write_memory(value.data(), sizeof(s32) * 4); },
196 [&](float value) { write_memory(&value, sizeof(float)); },
197 [&](const std::array<float, 2>& value) { write_memory(value.data(), sizeof(float) * 2); },
198 [&](const std::array<float, 3>& value) { write_memory(value.data(), sizeof(float) * 3); },
199 [&](const std::array<float, 4>& value) { write_memory(value.data(), sizeof(float) * 4); },
200
201 // Bool has the size of an int in the shader
202 [&](bool value) {
203 u32 val = static_cast<u32>(value);
204 write_memory(&val, sizeof(u32));
205 }},
206 property.m_value);
207 }
208
209 std::size_t MaterialProperty::GetMemorySize(const MaterialProperty& property)
210 {
211 std::size_t result = 0;
212 std::visit(overloaded{[&](s32 value) { result = MemorySize; },
213 [&](const std::array<s32, 2>&) { result = MemorySize; },
214 [&](const std::array<s32, 3>&) { result = MemorySize; },
215 [&](const std::array<s32, 4>&) { result = MemorySize; },
216 [&](float) { result = MemorySize; },
217 [&](const std::array<float, 2>&) { result = MemorySize; },
218 [&](const std::array<float, 3>&) { result = MemorySize; },
219 [&](const std::array<float, 4>&) { result = MemorySize; },
220 [&](bool) { result = MemorySize; }},
221 property.m_value);
222
223 return result;
224 }
225
226 bool MaterialData::FromJson(const CustomAssetLibrary::AssetID& asset_id,
227 const picojson::object& json, MaterialData* data)
228 {
229 const auto parse_properties = [&](const char* name,
230 std::vector<MaterialProperty>* material_properties) -> bool {
231 const auto properties_iter = json.find(name);
232 if (properties_iter == json.end())
233 {
234 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, '{}' not found", asset_id, name);
235 return false;
236 }
237 if (!properties_iter->second.is<picojson::array>())
238 {
239 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, '{}' is not the right json type",
240 asset_id, name);
241 return false;
242 }
243 const auto& properties_array = properties_iter->second.get<picojson::array>();
244
245 if (!ParseMaterialProperties(asset_id, properties_array, material_properties))
246 return false;
247 return true;
248 };
249
250 if (!parse_properties("properties", &data->properties))
251 return false;
252
253 if (const auto shader_asset = ReadStringFromJson(json, "shader_asset"))
254 {
255 data->shader_asset = *shader_asset;
256 }
257 else
258 {
259 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'shader_asset' not found or wrong type",
260 asset_id);
261 return false;
262 }
263
264 if (const auto next_material_asset = ReadStringFromJson(json, "next_material_asset"))
265 {
266 data->next_material_asset = *next_material_asset;
267 }
268 else
269 {
270 ERROR_LOG_FMT(VIDEO,
271 "Asset '{}' failed to parse json, 'next_material_asset' not found or wrong type",
272 asset_id);
273 return false;
274 }
275
276 if (const auto cull_mode = ReadNumericFromJson<CullMode>(json, "cull_mode"))
277 {
278 data->cull_mode = *cull_mode;
279 }
280
281 if (const auto depth_state = json.find("depth_state");
282 depth_state != json.end() && depth_state->second.is<picojson::object>())
283 {
284 auto& json_obj = depth_state->second.get<picojson::object>();
285 DepthState state;
286 state.test_enable = ReadNumericFromJson<u32>(json_obj, "test_enable").value_or(0);
287 state.update_enable = ReadNumericFromJson<u32>(json_obj, "update_enable").value_or(0);
288 state.func = ReadNumericFromJson<CompareMode>(json_obj, "func").value_or(CompareMode::Never);
289 data->depth_state = state;
290 }
291
292 if (const auto blending_state = json.find("blending_state");
293 blending_state != json.end() && blending_state->second.is<picojson::object>())
294 {
295 auto& json_obj = blending_state->second.get<picojson::object>();
296 BlendingState state;
297 state.blend_enable = ReadNumericFromJson<u32>(json_obj, "blend_enable").value_or(0);
298 state.logic_op_enable = ReadNumericFromJson<u32>(json_obj, "logic_op_enable").value_or(0);
299 state.color_update = ReadNumericFromJson<u32>(json_obj, "color_update").value_or(0);
300 state.alpha_update = ReadNumericFromJson<u32>(json_obj, "alpha_update").value_or(0);
301 state.subtract = ReadNumericFromJson<u32>(json_obj, "subtract").value_or(0);
302 state.subtract_alpha = ReadNumericFromJson<u32>(json_obj, "subtract_alpha").value_or(0);
303 state.use_dual_src = ReadNumericFromJson<u32>(json_obj, "use_dual_src").value_or(0);
304 state.dst_factor =
305 ReadNumericFromJson<DstBlendFactor>(json_obj, "dst_factor").value_or(DstBlendFactor::Zero);
306 state.src_factor =
307 ReadNumericFromJson<SrcBlendFactor>(json_obj, "src_factor").value_or(SrcBlendFactor::Zero);
308 state.dst_factor_alpha = ReadNumericFromJson<DstBlendFactor>(json_obj, "dst_factor_alpha")
309 .value_or(DstBlendFactor::Zero);
310 state.src_factor_alpha = ReadNumericFromJson<SrcBlendFactor>(json_obj, "src_factor_alpha")
311 .value_or(SrcBlendFactor::Zero);
312 state.logic_mode =
313 ReadNumericFromJson<LogicOp>(json_obj, "logic_mode").value_or(LogicOp::Clear);
314 data->blending_state = state;
315 }
316
317 const auto textures_iter = json.find("textures");
318 if (textures_iter == json.end())
319 {
320 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'textures' not found", asset_id);
321 return false;
322 }
323 if (!textures_iter->second.is<picojson::array>())
324 {
325 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'textures' is not the right json type",
326 asset_id);
327 return false;
328 }
329 const auto& textures_array = textures_iter->second.get<picojson::array>();
330 if (!std::ranges::all_of(textures_array, [](const picojson::value& json_data) {
331 return json_data.is<picojson::object>();
332 }))
333 {
334 ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, 'textures' must contain objects",
335 asset_id);
336 return false;
337 }
338
339 for (const auto& texture_json : textures_array)
340 {
341 auto& texture_json_obj = texture_json.get<picojson::object>();
342
343 TextureSamplerValue sampler_value;
344 if (!TextureSamplerValue::FromJson(texture_json_obj, &sampler_value))
345 return false;
346 data->textures.push_back(std::move(sampler_value));
347 }
348
349 return true;
350 }
351
352 void MaterialData::ToJson(picojson::object* obj, const MaterialData& data)
353 {
354 if (!obj) [[unlikely]]
355 return;
356
357 auto& json_obj = *obj;
358
359 const auto add_property = [](picojson::array* json_properties, const MaterialProperty& property) {
360 picojson::object json_property;
361
362 std::visit(overloaded{[&](s32 value) {
363 json_property["type"] = picojson::value{"int"};
364 json_property["value"] = picojson::value{static_cast<double>(value)};
365 },
366 [&](const std::array<s32, 2>& value) {
367 json_property["type"] = picojson::value{"int2"};
368 json_property["value"] = picojson::value{ToJsonArray(value)};
369 },
370 [&](const std::array<s32, 3>& value) {
371 json_property["type"] = picojson::value{"int3"};
372 json_property["value"] = picojson::value{ToJsonArray(value)};
373 },
374 [&](const std::array<s32, 4>& value) {
375 json_property["type"] = picojson::value{"int4"};
376 json_property["value"] = picojson::value{ToJsonArray(value)};
377 },
378 [&](float value) {
379 json_property["type"] = picojson::value{"float"};
380 json_property["value"] = picojson::value{static_cast<double>(value)};
381 },
382 [&](const std::array<float, 2>& value) {
383 json_property["type"] = picojson::value{"float2"};
384 json_property["value"] = picojson::value{ToJsonArray(value)};
385 },
386 [&](const std::array<float, 3>& value) {
387 json_property["type"] = picojson::value{"float3"};
388 json_property["value"] = picojson::value{ToJsonArray(value)};
389 },
390 [&](const std::array<float, 4>& value) {
391 json_property["type"] = picojson::value{"float4"};
392 json_property["value"] = picojson::value{ToJsonArray(value)};
393 },
394 [&](bool value) {
395 json_property["type"] = picojson::value{"bool"};
396 json_property["value"] = picojson::value{value};
397 }},
398 property.m_value);
399
400 json_properties->emplace_back(std::move(json_property));
401 };
402
403 picojson::array json_properties;
404 for (const auto& property : data.properties)
405 {
406 add_property(&json_properties, property);
407 }
408 json_obj.emplace("properties", std::move(json_properties));
409
410 json_obj.emplace("shader_asset", data.shader_asset);
411 json_obj.emplace("next_material_asset", data.next_material_asset);
412
413 if (data.cull_mode)
414 {
415 json_obj.emplace("cull_mode", static_cast<double>(*data.cull_mode));
416 }
417
418 if (data.depth_state)
419 {
420 picojson::object depth_state_json;
421 depth_state_json.emplace("test_enable",
422 static_cast<double>(data.depth_state->test_enable.Value()));
423 depth_state_json.emplace("update_enable",
424 static_cast<double>(data.depth_state->update_enable.Value()));
425 depth_state_json.emplace("func", static_cast<double>(data.depth_state->func.Value()));
426 json_obj.emplace("depth_state", depth_state_json);
427 }
428
429 if (data.blending_state)
430 {
431 picojson::object blending_state_json;
432 blending_state_json.emplace("blend_enable",
433 static_cast<double>(data.blending_state->blend_enable.Value()));
434 blending_state_json.emplace("logic_op_enable",
435 static_cast<double>(data.blending_state->logic_op_enable.Value()));
436 blending_state_json.emplace("color_update",
437 static_cast<double>(data.blending_state->color_update.Value()));
438 blending_state_json.emplace("alpha_update",
439 static_cast<double>(data.blending_state->alpha_update.Value()));
440 blending_state_json.emplace("subtract",
441 static_cast<double>(data.blending_state->subtract.Value()));
442 blending_state_json.emplace("subtract_alpha",
443 static_cast<double>(data.blending_state->subtract_alpha.Value()));
444 blending_state_json.emplace("use_dual_src",
445 static_cast<double>(data.blending_state->use_dual_src.Value()));
446 blending_state_json.emplace("dst_factor",
447 static_cast<double>(data.blending_state->dst_factor.Value()));
448 blending_state_json.emplace("src_factor",
449 static_cast<double>(data.blending_state->src_factor.Value()));
450 blending_state_json.emplace("dst_factor_alpha",
451 static_cast<double>(data.blending_state->dst_factor_alpha.Value()));
452 blending_state_json.emplace("src_factor_alpha",
453 static_cast<double>(data.blending_state->src_factor_alpha.Value()));
454 blending_state_json.emplace("logic_mode",
455 static_cast<double>(data.blending_state->logic_mode.Value()));
456 json_obj.emplace("blending_state", blending_state_json);
457 }
458
459 picojson::array json_textures;
460 for (const auto& texture : data.textures)
461 {
462 picojson::object json_texture;
463 TextureSamplerValue::ToJson(&json_texture, texture);
464 json_textures.emplace_back(std::move(json_texture));
465 }
466 json_obj.emplace("textures", json_textures);
467 }
468
469 CustomAssetLibrary::LoadInfo MaterialAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id)
470 {
471 auto potential_data = std::make_shared<MaterialData>();
472 const auto loaded_info = m_owning_library->LoadMaterial(asset_id, potential_data.get());
473 if (loaded_info.bytes_loaded == 0)
474 return {};
475 {
476 std::lock_guard lk(m_data_lock);
477 m_loaded = true;
478 m_data = std::move(potential_data);
479 }
480 return loaded_info;
481 }
482 } // namespace VideoCommon