• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "shader_state_loader.h"
17 
18 #include <cctype>
19 #include <locale>
20 
21 #include <base/containers/string.h>
22 #include <core/io/intf_file_manager.h>
23 #include <core/namespace.h>
24 
25 #include "json_util.h"
26 #include "shader_state_loader_util.h"
27 #include "util/log.h"
28 
29 using namespace BASE_NS;
30 using namespace CORE_NS;
31 
32 RENDER_BEGIN_NAMESPACE()
33 namespace {
LoadImpl(const string_view jsonString)34 ShaderStateLoaderUtil::ShaderStateResult LoadImpl(const string_view jsonString)
35 {
36     const auto json = json::parse(jsonString.data());
37     if (json) {
38         ShaderStateLoaderUtil::ShaderStateResult ssr = ShaderStateLoaderUtil::LoadStates(json);
39         return ssr;
40     } else {
41         ShaderStateLoaderUtil::ShaderStateResult ssr;
42         ssr.res.success = true;
43         ssr.res.error = "Invalid json file.";
44         return ssr;
45     }
46 }
47 } // namespace
48 
Load(IFileManager & fileManager,const string_view uri)49 ShaderStateLoader::LoadResult ShaderStateLoader::Load(IFileManager& fileManager, const string_view uri)
50 {
51     uri_ = uri;
52     IFile::Ptr file = fileManager.OpenFile(uri);
53     if (!file) {
54         PLUGIN_LOG_D("Error loading '%s'", uri_.c_str());
55         return LoadResult("Failed to open file.");
56     }
57 
58     const uint64_t byteLength = file->GetLength();
59 
60     string raw;
61     raw.resize(static_cast<size_t>(byteLength));
62 
63     if (file->Read(raw.data(), byteLength) != byteLength) {
64         PLUGIN_LOG_D("Error loading '%s'", uri_.c_str());
65         return LoadResult("Failed to read file.");
66     }
67 
68     ShaderStateLoaderUtil::ShaderStateResult ssr = RENDER_NS::LoadImpl(string_view(raw));
69     graphicsStates_ = move(ssr.states.states);
70     graphicsStateVariantData_ = move(ssr.states.variantData);
71 
72     return ssr.res;
73 }
74 
GetUri() const75 string_view ShaderStateLoader::GetUri() const
76 {
77     return uri_;
78 }
79 
GetGraphicsStateVariantData() const80 array_view<const ShaderStateLoaderVariantData> ShaderStateLoader::GetGraphicsStateVariantData() const
81 {
82     return graphicsStateVariantData_;
83 }
84 
GetGraphicsStates() const85 array_view<const GraphicsState> ShaderStateLoader::GetGraphicsStates() const
86 {
87     return graphicsStates_;
88 }
89 
90 RENDER_END_NAMESPACE()
91