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_DATA_LOADER_H
17 #define LOADER_SHADER_DATA_LOADER_H
18 
19 #include <base/containers/string.h>
20 #include <base/containers/string_view.h>
21 #include <base/containers/vector.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()
RENDER_BEGIN_NAMESPACE()29 RENDER_BEGIN_NAMESPACE()
30 
31 /** Shader data loader.
32  * A class that can be used to load all shader related data from shader json structure.
33  */
34 class ShaderDataLoader final {
35 public:
36     /** Describes result of the parsing operation. */
37     struct LoadResult {
38         LoadResult() = default;
39         explicit LoadResult(const BASE_NS::string& aError) : success(false), error(aError) {}
40 
41         /** Indicates, whether the parsing operation is successful. */
42         bool success { true };
43 
44         /** In case of parsing error, contains the description of the error. */
45         BASE_NS::string error;
46     };
47 
48     /** Describes a single shader variant. */
49     struct ShaderVariant {
50         bool renderSlotDefaultShader { false };
51         BASE_NS::string variantName;
52         BASE_NS::string displayName;
53 
54         BASE_NS::string vertexShader;
55         BASE_NS::string fragmentShader;
56         BASE_NS::string computeShader;
57 
58         BASE_NS::string vertexInputDeclaration;
59         BASE_NS::string pipelineLayout;
60         GraphicsState graphicsState;
61 
62         BASE_NS::string renderSlot;
63         BASE_NS::vector<BASE_NS::string> customGraphicsStateSlotNames;
64         BASE_NS::string shaderFileStr;
65         BASE_NS::string materialMetadata;
66 
67         GraphicsStateFlags stateFlags { 0U };
68     };
69 
70     /** Uri of the loaded file.
71      * @return Uri path.
72      */
73     BASE_NS::string_view GetUri() const;
74 
75     /** Base shader for variants.
76      * @return Base shader path.
77      */
78     BASE_NS::string_view GetBaseShader() const;
79 
80     /** Base category for all shader variants.
81      * @return Base category for shaders.
82      */
83     BASE_NS::string_view GetBaseCategory() const;
84 
85     /** Get all shader variants.
86      * @return Array view of shader variants.
87      */
88     BASE_NS::array_view<const ShaderVariant> GetShaderVariants() const;
89 
90     /** Loads shader state from given uri, using file manager.
91      * @param fileManager A file manager to access the file in given uri.
92      * @param uri Uri to json file.
93      * @return A structure containing result for the parsing operation.
94      */
95     LoadResult Load(CORE_NS::IFileManager& fileManager, BASE_NS::string_view uri);
96 
97 private:
98     LoadResult Load(BASE_NS::string&& jsonData);
99 
100     BASE_NS::string uri_;
101     BASE_NS::string baseShader_;
102     BASE_NS::string baseCategory_;
103 
104     BASE_NS::vector<ShaderVariant> shaderVariants_;
105 };
106 CORE_END_NAMESPACE()
107 
108 #endif // LOADER_SHADER_DATA_LOADER_H
109