| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Assets/ShaderAsset.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "Common/JsonUtil.h" |
| 10 | #include "Common/Logging/Log.h" |
| 11 | #include "Common/StringUtil.h" |
| 12 | #include "Common/VariantUtil.h" |
| 13 | #include "VideoCommon/Assets/CustomAssetLibrary.h" |
| 14 | #include "VideoCommon/ShaderGenCommon.h" |
| 15 | |
| 16 | namespace VideoCommon |
| 17 | { |
| 18 | template <typename ElementType, std::size_t ElementCount, typename PropertyType> |
| 19 | bool ParseNumeric(const CustomAssetLibrary::AssetID& asset_id, const picojson::value& json_value, |
| 20 | std::string_view code_name, PropertyType* value) |
| 21 | { |
| 22 | static_assert(ElementCount <= 4, "Numeric data expected to be four elements or less"); |
| 23 | if constexpr (ElementCount == 1) |
| 24 | { |
| 25 | if (!json_value.is<double>()) |
| 26 | { |
| 27 | ERROR_LOG_FMT(VIDEO, |
| 28 | "Asset id '{}' shader has attribute '{}' where " |
| 29 | "a double was expected but not provided.", |
| 30 | asset_id, code_name); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | *value = static_cast<ElementType>(json_value.get<double>()); |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | if (!json_value.is<picojson::array>()) |
| 39 | { |
| 40 | ERROR_LOG_FMT(VIDEO, |
| 41 | "Asset id '{}' shader has attribute '{}' where " |
| 42 | "an array was expected but not provided.", |
| 43 | asset_id, code_name); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | const auto json_data = json_value.get<picojson::array>(); |
| 48 | |
| 49 | if (json_data.size() != ElementCount) |
| 50 | { |
| 51 | ERROR_LOG_FMT(VIDEO, |
| 52 | "Asset id '{}' shader has attribute '{}' with incorrect number " |
| 53 | "of elements, expected {}", |
| 54 | asset_id, code_name, ElementCount); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | if (!std::ranges::all_of(json_data, &picojson::value::is<double>)) |
| 59 | { |
| 60 | ERROR_LOG_FMT(VIDEO, |
| 61 | "Asset id '{}' shader has attribute '{}' where " |
| 62 | "all elements are not of type double.", |
| 63 | asset_id, code_name); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | std::array<ElementType, ElementCount> data; |
| 68 | for (std::size_t i = 0; i < ElementCount; i++) |
| 69 | { |
| 70 | data[i] = static_cast<ElementType>(json_data[i].get<double>()); |
| 71 | } |
| 72 | *value = std::move(data); |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | static bool ParseShaderValue(const CustomAssetLibrary::AssetID& asset_id, |
| 79 | const picojson::value& json_value, std::string_view code_name, |
| 80 | std::string_view type, ShaderProperty::Value* value) |
| 81 | { |
| 82 | if (type == "int") |
| 83 | { |
| 84 | return ParseNumeric<s32, 1>(asset_id, json_value, code_name, value); |
| 85 | } |
| 86 | else if (type == "int2") |
| 87 | { |
| 88 | return ParseNumeric<s32, 2>(asset_id, json_value, code_name, value); |
| 89 | } |
| 90 | else if (type == "int3") |
| 91 | { |
| 92 | return ParseNumeric<s32, 3>(asset_id, json_value, code_name, value); |
| 93 | } |
| 94 | else if (type == "int4") |
| 95 | { |
| 96 | return ParseNumeric<s32, 4>(asset_id, json_value, code_name, value); |
| 97 | } |
| 98 | else if (type == "float") |
| 99 | { |
| 100 | return ParseNumeric<float, 1>(asset_id, json_value, code_name, value); |
| 101 | } |
| 102 | else if (type == "float2") |
| 103 | { |
| 104 | return ParseNumeric<float, 2>(asset_id, json_value, code_name, value); |
| 105 | } |
| 106 | else if (type == "float3") |
| 107 | { |
| 108 | return ParseNumeric<float, 3>(asset_id, json_value, code_name, value); |
| 109 | } |
| 110 | else if (type == "float4") |
| 111 | { |
| 112 | return ParseNumeric<float, 4>(asset_id, json_value, code_name, value); |
| 113 | } |
| 114 | else if (type == "rgb") |
| 115 | { |
| 116 | ShaderProperty::RGB rgb; |
| 117 | if (!ParseNumeric<float, 3>(asset_id, json_value, code_name, &rgb.value)) |
| 118 | return false; |
| 119 | *value = rgb; |
| 120 | return true; |
| 121 | } |
| 122 | else if (type == "rgba") |
| 123 | { |
| 124 | ShaderProperty::RGBA rgba; |
| 125 | if (!ParseNumeric<float, 4>(asset_id, json_value, code_name, &rgba.value)) |
| 126 | return false; |
| 127 | *value = rgba; |
| 128 | return true; |
| 129 | } |
| 130 | else if (type == "bool") |
| 131 | { |
| 132 | if (json_value.is<bool>()) |
| 133 | { |
| 134 | *value = json_value.get<bool>(); |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse the json, value is not valid for type '{}'", |
| 140 | asset_id, type); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | static bool ParseShaderProperties(const VideoCommon::CustomAssetLibrary::AssetID& asset_id, |
| 145 | const picojson::array& properties_data, |
| 146 | std::vector<ShaderProperty>* shader_properties) |
| 147 | { |
| 148 | if (!shader_properties) [[unlikely]] |
| 149 | return false; |
| 150 | |
| 151 | for (const auto& property_data : properties_data) |
| 152 | { |
| 153 | VideoCommon::ShaderProperty property; |
| 154 | if (!property_data.is<picojson::object>()) |
| 155 | { |
| 156 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, property is not the right json type", |
| 157 | asset_id); |
| 158 | return false; |
| 159 | } |
| 160 | const auto& property_data_obj = property_data.get<picojson::object>(); |
| 161 | |
| 162 | const auto type_iter = property_data_obj.find("type"); |
| 163 | if (type_iter == property_data_obj.end()) |
| 164 | { |
| 165 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, property entry 'type' not found", |
| 166 | asset_id); |
| 167 | return false; |
| 168 | } |
| 169 | if (!type_iter->second.is<std::string>()) |
| 170 | { |
| 171 | ERROR_LOG_FMT(VIDEO, |
| 172 | "Asset '{}' failed to parse json, property entry 'type' is not " |
| 173 | "the right json type", |
| 174 | asset_id); |
| 175 | return false; |
| 176 | } |
| 177 | std::string type = type_iter->second.to_str(); |
| 178 | Common::ToLower(&type); |
| 179 | |
| 180 | const auto description_iter = property_data_obj.find("description"); |
| 181 | if (description_iter == property_data_obj.end()) |
| 182 | { |
| 183 | ERROR_LOG_FMT(VIDEO, |
| 184 | "Asset '{}' failed to parse json, property entry 'description' not found", |
| 185 | asset_id); |
| 186 | return false; |
| 187 | } |
| 188 | if (!description_iter->second.is<std::string>()) |
| 189 | { |
| 190 | ERROR_LOG_FMT(VIDEO, |
| 191 | "Asset '{}' failed to parse json, property entry 'description' is not " |
| 192 | "the right json type", |
| 193 | asset_id); |
| 194 | return false; |
| 195 | } |
| 196 | property.description = description_iter->second.to_str(); |
| 197 | |
| 198 | const auto code_name_iter = property_data_obj.find("code_name"); |
| 199 | if (code_name_iter == property_data_obj.end()) |
| 200 | { |
| 201 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, property entry 'code_name' not found", |
| 202 | asset_id); |
| 203 | return false; |
| 204 | } |
| 205 | if (!code_name_iter->second.is<std::string>()) |
| 206 | { |
| 207 | ERROR_LOG_FMT(VIDEO, |
| 208 | "Asset '{}' failed to parse json, property entry 'code_name' is not " |
| 209 | "the right json type", |
| 210 | asset_id); |
| 211 | return false; |
| 212 | } |
| 213 | property.name = code_name_iter->second.to_str(); |
| 214 | |
| 215 | const auto default_iter = property_data_obj.find("default"); |
| 216 | if (default_iter != property_data_obj.end()) |
| 217 | { |
| 218 | if (!ParseShaderValue(asset_id, default_iter->second, property.name, type, |
| 219 | &property.default_value)) |
| 220 | { |
| 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | property.default_value = ShaderProperty::GetDefaultValueFromTypeName(type); |
| 227 | } |
| 228 | |
| 229 | shader_properties->push_back(std::move(property)); |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool RasterSurfaceShaderData::FromJson(const VideoCommon::CustomAssetLibrary::AssetID& asset_id, |
| 236 | const picojson::object& json, RasterSurfaceShaderData* data) |
| 237 | { |
| 238 | const auto parse_properties = [&](const char* name, std::string_view source, |
| 239 | std::vector<ShaderProperty>* shader_properties) -> bool { |
| 240 | const auto properties_iter = json.find(name); |
| 241 | if (properties_iter == json.end()) |
| 242 | { |
| 243 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, '{}' not found", asset_id, name); |
| 244 | return false; |
| 245 | } |
| 246 | if (!properties_iter->second.is<picojson::array>()) |
| 247 | { |
| 248 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, '{}' is not the right json type", |
| 249 | asset_id, name); |
| 250 | return false; |
| 251 | } |
| 252 | const auto& properties_array = properties_iter->second.get<picojson::array>(); |
| 253 | |
| 254 | return ParseShaderProperties(asset_id, properties_array, shader_properties); |
| 255 | }; |
| 256 | |
| 257 | const auto parse_samplers = |
| 258 | [&](const char* name, |
| 259 | std::vector<VideoCommon::RasterSurfaceShaderData::SamplerData>* samplers_out) -> bool { |
| 260 | const auto samplers_iter = json.find(name); |
| 261 | if (samplers_iter == json.end()) |
| 262 | { |
| 263 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, '{}' not found", asset_id, name); |
| 264 | return false; |
| 265 | } |
| 266 | if (!samplers_iter->second.is<picojson::array>()) |
| 267 | { |
| 268 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, '{}' is not the right json type", |
| 269 | asset_id, name); |
| 270 | return false; |
| 271 | } |
| 272 | const auto& samplers_array = samplers_iter->second.get<picojson::array>(); |
| 273 | if (!std::ranges::all_of(samplers_array, [](const picojson::value& json_data) { |
| 274 | return json_data.is<picojson::object>(); |
| 275 | })) |
| 276 | { |
| 277 | ERROR_LOG_FMT(VIDEO, "Asset '{}' failed to parse json, '{}' must contain objects", asset_id, |
| 278 | name); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | for (const auto& sampler_json : samplers_array) |
| 283 | { |
| 284 | auto& sampler_json_obj = sampler_json.get<picojson::object>(); |
| 285 | |
| 286 | SamplerData sampler; |
| 287 | |
| 288 | if (const auto sampler_name = ReadStringFromJson(sampler_json_obj, "name")) |
| 289 | { |
| 290 | sampler.name = *sampler_name; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | ERROR_LOG_FMT(VIDEO, |
| 295 | "Asset '{}' failed to parse sampler json, 'name' not found or wrong type", |
| 296 | asset_id); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | if (const auto sampler_type = |
| 301 | ReadNumericFromJson<AbstractTextureType>(sampler_json_obj, "type")) |
| 302 | { |
| 303 | sampler.type = *sampler_type; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | ERROR_LOG_FMT(VIDEO, |
| 308 | "Asset '{}' failed to parse sampler json, 'type' not found or wrong type", |
| 309 | asset_id); |
| 310 | return false; |
| 311 | } |
| 312 | samplers_out->push_back(std::move(sampler)); |
| 313 | } |
| 314 | |
| 315 | return true; |
| 316 | }; |
| 317 | |
| 318 | if (!parse_properties("properties", data->pixel_source, &data->uniform_properties)) |
| 319 | return false; |
| 320 | |
| 321 | if (!parse_samplers("samplers", &data->samplers)) |
| 322 | return false; |
| 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | void RasterSurfaceShaderData::ToJson(picojson::object& obj, const RasterSurfaceShaderData& data) |
| 328 | { |
| 329 | const auto add_property = [](picojson::array* json_properties, const ShaderProperty& property) { |
| 330 | picojson::object json_property; |
| 331 | |
| 332 | json_property.emplace("code_name", property.name); |
| 333 | json_property.emplace("description", property.description); |
| 334 | |
| 335 | std::visit(overloaded{[&](s32 default_value) { |
| 336 | json_property.emplace("type", "int"); |
| 337 | json_property.emplace("default", static_cast<double>(default_value)); |
| 338 | }, |
| 339 | [&](const std::array<s32, 2>& default_value) { |
| 340 | json_property.emplace("type", "int2"); |
| 341 | json_property.emplace("default", ToJsonArray(default_value)); |
| 342 | }, |
| 343 | [&](const std::array<s32, 3>& default_value) { |
| 344 | json_property.emplace("type", "int3"); |
| 345 | json_property.emplace("default", ToJsonArray(default_value)); |
| 346 | }, |
| 347 | [&](const std::array<s32, 4>& default_value) { |
| 348 | json_property.emplace("type", "int4"); |
| 349 | json_property.emplace("default", ToJsonArray(default_value)); |
| 350 | }, |
| 351 | [&](float default_value) { |
| 352 | json_property.emplace("type", "float"); |
| 353 | json_property.emplace("default", static_cast<double>(default_value)); |
| 354 | }, |
| 355 | [&](const std::array<float, 2>& default_value) { |
| 356 | json_property.emplace("type", "float2"); |
| 357 | json_property.emplace("default", ToJsonArray(default_value)); |
| 358 | }, |
| 359 | [&](const std::array<float, 3>& default_value) { |
| 360 | json_property.emplace("type", "float3"); |
| 361 | json_property.emplace("default", ToJsonArray(default_value)); |
| 362 | }, |
| 363 | [&](const std::array<float, 4>& default_value) { |
| 364 | json_property.emplace("type", "float4"); |
| 365 | json_property.emplace("default", ToJsonArray(default_value)); |
| 366 | }, |
| 367 | [&](const ShaderProperty::RGB& default_value) { |
| 368 | json_property.emplace("type", "rgb"); |
| 369 | json_property.emplace("default", ToJsonArray(default_value.value)); |
| 370 | }, |
| 371 | [&](const ShaderProperty::RGBA& default_value) { |
| 372 | json_property.emplace("type", "rgba"); |
| 373 | json_property.emplace("default", ToJsonArray(default_value.value)); |
| 374 | }, |
| 375 | [&](bool default_value) { |
| 376 | json_property.emplace("type", "bool"); |
| 377 | json_property.emplace("default", default_value); |
| 378 | }}, |
| 379 | property.default_value); |
| 380 | |
| 381 | json_properties->emplace_back(std::move(json_property)); |
| 382 | }; |
| 383 | |
| 384 | const auto add_sampler = [](picojson::array* json_samplers, const SamplerData& sampler) { |
| 385 | picojson::object json_sampler; |
| 386 | json_sampler.emplace("name", sampler.name); |
| 387 | json_sampler.emplace("type", static_cast<double>(sampler.type)); |
| 388 | json_samplers->emplace_back(std::move(json_sampler)); |
| 389 | }; |
| 390 | |
| 391 | picojson::array json_properties; |
| 392 | for (const auto& property : data.uniform_properties) |
| 393 | { |
| 394 | add_property(&json_properties, property); |
| 395 | } |
| 396 | obj.emplace("properties", std::move(json_properties)); |
| 397 | |
| 398 | picojson::array json_samplers; |
| 399 | for (const auto& sampler : data.samplers) |
| 400 | { |
| 401 | add_sampler(&json_samplers, sampler); |
| 402 | } |
| 403 | obj.emplace("samplers", json_samplers); |
| 404 | } |
| 405 | |
| 406 | std::span<const std::string_view> ShaderProperty::GetValueTypeNames() |
| 407 | { |
| 408 | static constexpr std::array<std::string_view, 11> values = { |
| 409 | "int", "int2", "int3", "int4", "float", "float2", "float3", "float4", "rgb", "rgba", "bool"}; |
| 410 | return values; |
| 411 | } |
| 412 | |
| 413 | ShaderProperty::Value ShaderProperty::GetDefaultValueFromTypeName(std::string_view name) |
| 414 | { |
| 415 | if (name == "int") |
| 416 | { |
| 417 | return 0; |
| 418 | } |
| 419 | else if (name == "int2") |
| 420 | { |
| 421 | return std::array<s32, 2>{}; |
| 422 | } |
| 423 | else if (name == "int3") |
| 424 | { |
| 425 | return std::array<s32, 3>{}; |
| 426 | } |
| 427 | else if (name == "int4") |
| 428 | { |
| 429 | return std::array<s32, 4>{}; |
| 430 | } |
| 431 | else if (name == "float") |
| 432 | { |
| 433 | return 0.0f; |
| 434 | } |
| 435 | else if (name == "float2") |
| 436 | { |
| 437 | return std::array<float, 2>{}; |
| 438 | } |
| 439 | else if (name == "float3") |
| 440 | { |
| 441 | return std::array<float, 3>{}; |
| 442 | } |
| 443 | else if (name == "float4") |
| 444 | { |
| 445 | return std::array<float, 4>{}; |
| 446 | } |
| 447 | else if (name == "rgb") |
| 448 | { |
| 449 | return RGB{}; |
| 450 | } |
| 451 | else if (name == "rgba") |
| 452 | { |
| 453 | return RGBA{}; |
| 454 | } |
| 455 | else if (name == "bool") |
| 456 | { |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | return Value{}; |
| 461 | } |
| 462 | |
| 463 | void ShaderProperty::WriteAsShaderCode(ShaderCode& shader_source, const ShaderProperty& property) |
| 464 | { |
| 465 | const auto write_shader = [&](std::string_view type, u32 element_count) { |
| 466 | if (element_count == 1) |
| 467 | { |
| 468 | shader_source.Write("{} {};\n", type, property.name); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | shader_source.Write("{}{} {};\n", type, element_count, property.name); |
| 473 | } |
| 474 | |
| 475 | for (std::size_t i = element_count; i < 4; i++) |
| 476 | { |
| 477 | shader_source.Write("{} {}_padding_{};\n", type, property.name, i + 1); |
| 478 | } |
| 479 | }; |
| 480 | std::visit(overloaded{[&](s32) { write_shader("int", 1); }, |
| 481 | [&](const std::array<s32, 2>&) { write_shader("int", 2); }, |
| 482 | [&](const std::array<s32, 3>&) { write_shader("int", 3); }, |
| 483 | [&](const std::array<s32, 4>&) { write_shader("int", 4); }, |
| 484 | [&](float) { write_shader("float", 1); }, |
| 485 | [&](const std::array<float, 2>&) { write_shader("float", 2); }, |
| 486 | [&](const std::array<float, 3>&) { write_shader("float", 3); }, |
| 487 | [&](const std::array<float, 4>&) { write_shader("float", 4); }, |
| 488 | [&](const ShaderProperty::RGB&) { write_shader("float", 3); }, |
| 489 | [&](const ShaderProperty::RGBA&) { write_shader("float", 4); }, |
| 490 | [&](bool) { write_shader("bool", 1); }}, |
| 491 | property.default_value); |
| 492 | } |
| 493 | |
| 494 | CustomAssetLibrary::LoadInfo |
| 495 | RasterSurfaceShaderAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id) |
| 496 | { |
| 497 | auto potential_data = std::make_shared<RasterSurfaceShaderData>(); |
| 498 | const auto loaded_info = |
| 499 | m_owning_library->LoadRasterSurfaceShader(asset_id, potential_data.get()); |
| 500 | if (loaded_info.bytes_loaded == 0) |
| 501 | return {}; |
| 502 | { |
| 503 | std::lock_guard lk(m_data_lock); |
| 504 | m_loaded = true; |
| 505 | m_data = std::move(potential_data); |
| 506 | } |
| 507 | return loaded_info; |
| 508 | } |
| 509 | } // namespace VideoCommon |