summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
diff options
context:
space:
mode:
authorAdmiral H. Curtiss <pikachu025@gmail.com>2023-07-03 03:24:11 +0200
committerGitHub <noreply@github.com>2023-07-03 03:24:11 +0200
commit03661223069aab4291b65c86727b4b9711b5878b (patch)
tree48be308c35ab304620960a0b214b924f6f1f0e24 /Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
parentda2784a391c5afc513d7628e6710b46a473adc09 (diff)
parent6ea49c6746de19c09ede7f539cf3cafc42280e5a (diff)
Merge pull request #12009 from iwubcode/shader_asset
VideoCommon: add a pixel shader asset
Diffstat (limited to 'Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp')
-rw-r--r--Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp b/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
index 9c126bf9e1..c234b4088e 100644
--- a/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
+++ b/Source/Core/VideoCommon/Assets/DirectFilesystemAssetLibrary.cpp
@@ -10,6 +10,7 @@
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "VideoCommon/Assets/CustomTextureData.h"
+#include "VideoCommon/Assets/ShaderAsset.h"
namespace VideoCommon
{
@@ -49,6 +50,100 @@ DirectFilesystemAssetLibrary::GetLastAssetWriteTime(const AssetID& asset_id) con
return {};
}
+CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadPixelShader(const AssetID& asset_id,
+ PixelShaderData* data)
+{
+ const auto asset_map = GetAssetMapForID(asset_id);
+
+ // Asset map for a pixel shader is the shader and some metadata
+ if (asset_map.size() != 2)
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have two files mapped!", asset_id);
+ return {};
+ }
+
+ const auto metadata = asset_map.find("metadata");
+ const auto shader = asset_map.find("shader");
+ if (metadata == asset_map.end())
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a metadata entry mapped!", asset_id);
+ return {};
+ }
+
+ if (shader == asset_map.end())
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' expected to have a shader entry mapped!", asset_id);
+ return {};
+ }
+
+ std::size_t metadata_size;
+ {
+ std::error_code ec;
+ metadata_size = std::filesystem::file_size(metadata->second, ec);
+ if (ec)
+ {
+ ERROR_LOG_FMT(VIDEO,
+ "Asset '{}' error - failed to get shader metadata file size with error '{}'!",
+ asset_id, ec);
+ return {};
+ }
+ }
+ std::size_t shader_size;
+ {
+ std::error_code ec;
+ shader_size = std::filesystem::file_size(shader->second, ec);
+ if (ec)
+ {
+ ERROR_LOG_FMT(VIDEO,
+ "Asset '{}' error - failed to get shader source file size with error '{}'!",
+ asset_id, ec);
+ return {};
+ }
+ }
+ const auto approx_mem_size = metadata_size + shader_size;
+
+ if (!File::ReadFileToString(shader->second.string(), data->m_shader_source))
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the shader file '{}',", asset_id,
+ shader->second.string());
+ return {};
+ }
+
+ std::string json_data;
+ if (!File::ReadFileToString(metadata->second.string(), json_data))
+ {
+ ERROR_LOG_FMT(VIDEO, "Asset '{}' error - failed to load the json file '{}',", asset_id,
+ metadata->second.string());
+ return {};
+ }
+
+ picojson::value root;
+ const auto error = picojson::parse(root, json_data);
+
+ if (!error.empty())
+ {
+ ERROR_LOG_FMT(VIDEO,
+ "Asset '{}' error - failed to load the json file '{}', due to parse error: {}",
+ asset_id, metadata->second.string(), error);
+ return {};
+ }
+ if (!root.is<picojson::object>())
+ {
+ ERROR_LOG_FMT(
+ VIDEO,
+ "Asset '{}' error - failed to load the json file '{}', due to root not being an object!",
+ asset_id, metadata->second.string());
+ return {};
+ }
+
+ const auto& root_obj = root.get<picojson::object>();
+
+ if (!PixelShaderData::FromJson(asset_id, root_obj, data))
+ return {};
+
+ return LoadInfo{approx_mem_size, GetLastAssetWriteTime(asset_id)};
+}
+
CustomAssetLibrary::LoadInfo DirectFilesystemAssetLibrary::LoadTexture(const AssetID& asset_id,
CustomTextureData* data)
{