1 /*
2 * Copyright (c) 2022 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 <base/containers/string.h>
19 #include <core/io/intf_file_manager.h>
20 #include <core/namespace.h>
21
22 #include "shader_state_loader_util.h"
23 #include "util/log.h"
24
25 using namespace BASE_NS;
26 using namespace CORE_NS;
27
28 RENDER_BEGIN_NAMESPACE()
29 namespace {
LoadImpl(const string_view jsonString)30 ShaderStateLoaderUtil::ShaderStateResult LoadImpl(const string_view jsonString)
31 {
32 const auto json = json::parse(jsonString.data());
33 if (json) {
34 ShaderStateLoaderUtil::ShaderStateResult ssr = ShaderStateLoaderUtil::LoadStates(json);
35 return ssr;
36 } else {
37 return ShaderStateLoaderUtil::ShaderStateResult { ShaderStateLoader::LoadResult { "Invalid json file." }, {} };
38 }
39 }
40 } // namespace
41
Load(IFileManager & fileManager,const string_view uri)42 ShaderStateLoader::LoadResult ShaderStateLoader::Load(IFileManager& fileManager, const string_view uri)
43 {
44 uri_ = uri;
45 IFile::Ptr file = fileManager.OpenFile(uri);
46 if (!file) {
47 PLUGIN_LOG_D("Error loading '%s'", uri_.c_str());
48 return LoadResult("Failed to open file.");
49 }
50
51 const uint64_t byteLength = file->GetLength();
52
53 string raw;
54 raw.resize(static_cast<size_t>(byteLength));
55
56 if (file->Read(raw.data(), byteLength) != byteLength) {
57 PLUGIN_LOG_D("Error loading '%s'", uri_.c_str());
58 return LoadResult("Failed to read file.");
59 }
60
61 ShaderStateLoaderUtil::ShaderStateResult ssr = RENDER_NS::LoadImpl(string_view(raw));
62 graphicsStates_ = move(ssr.states.states);
63 graphicsStateVariantData_ = move(ssr.states.variantData);
64
65 return ssr.res;
66 }
67
GetUri() const68 string_view ShaderStateLoader::GetUri() const
69 {
70 return uri_;
71 }
72
GetGraphicsStateVariantData() const73 array_view<const IShaderManager::ShaderStateLoaderVariantData> ShaderStateLoader::GetGraphicsStateVariantData() const
74 {
75 return graphicsStateVariantData_;
76 }
77
GetGraphicsStates() const78 array_view<const GraphicsState> ShaderStateLoader::GetGraphicsStates() const
79 {
80 return graphicsStates_;
81 }
82
83 RENDER_END_NAMESPACE()
84