1 /*
2  * Copyright (c) 2024 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 #ifndef LOADER_SHADER_STATE_LOADER_H
17 #define LOADER_SHADER_STATE_LOADER_H
18 
19 #include <base/containers/string.h>
20 #include <base/containers/vector.h>
21 #include <base/util/uid.h>
22 #include <core/namespace.h>
23 #include <render/device/pipeline_state_desc.h>
24 #include <render/namespace.h>
25 
26 CORE_BEGIN_NAMESPACE()
27 class IFileManager;
28 CORE_END_NAMESPACE()
29 RENDER_BEGIN_NAMESPACE()
30 
31 struct ShaderStateLoaderVariantData {
32     BASE_NS::string renderSlot;
33     BASE_NS::string variantName;
34 
35     BASE_NS::string baseShaderState;
36     BASE_NS::string baseVariantName;
37 
38     GraphicsStateFlags stateFlags { 0U };
39     bool renderSlotDefaultState { false };
40 };
41 
42 /** Shader state loader.
43  * A class that can be used to load shader (graphics) state data from a json.
44  */
45 class ShaderStateLoader final {
46 public:
47     /** Describes result of the parsing operation. */
48     struct LoadResult {
49         LoadResult() = default;
LoadResultLoadResult50         explicit LoadResult(const BASE_NS::string& aError) : success(false), error(aError) {}
51 
52         /** Indicates, whether the parsing operation is successful. */
53         bool success { true };
54 
55         /** In case of parsing error, contains the description of the error. */
56         BASE_NS::string error;
57     };
58 
59     struct GraphicsStates {
60         BASE_NS::vector<GraphicsState> states;
61         BASE_NS::vector<ShaderStateLoaderVariantData> variantData;
62     };
63 
64     /** Retrieve uri of shader state.
65      * @return String view to uri of shader state.
66      */
67     BASE_NS::string_view GetUri() const;
68 
69     /** Retrieve graphics state variant names.
70      * @return Graphics state variant names, as defined in the json file.
71      */
72     BASE_NS::array_view<const ShaderStateLoaderVariantData> GetGraphicsStateVariantData() const;
73 
74     /** Retrieve graphics states.
75      * @return Graphics states, as defined in the json file.
76      */
77     BASE_NS::array_view<const GraphicsState> GetGraphicsStates() const;
78 
79     /** Loads shader state from given uri, using file manager.
80      * @param fileManager A file manager to access the file in given uri.
81      * @param uri Uri to json file.
82      * @return A structure containing result for the parsing operation.
83      */
84     LoadResult Load(CORE_NS::IFileManager& fileManager, BASE_NS::string_view uri);
85 
86 private:
87     BASE_NS::string uri_;
88     BASE_NS::vector<GraphicsState> graphicsStates_;
89     BASE_NS::vector<ShaderStateLoaderVariantData> graphicsStateVariantData_;
90 };
91 RENDER_END_NAMESPACE()
92 
93 #endif // LOADER_SHADER_STATE_LOADER_H
94