• 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 #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 
53         BASE_NS::string vertexShader;
54         BASE_NS::string fragmentShader;
55         BASE_NS::string computeShader;
56 
57         BASE_NS::string vertexInputDeclaration;
58         BASE_NS::string pipelineLayout;
59         GraphicsState graphicsState;
60 
61         BASE_NS::string renderSlot;
62         BASE_NS::vector<BASE_NS::string> customGraphicsStateSlotNames;
63         BASE_NS::string materialMetadata;
64     };
65 
66     /** Uri of the loaded file.
67      * @return Uri path.
68      */
69     BASE_NS::string_view GetUri() const;
70 
71     /** Base shader for variants.
72      * @return Base shader path.
73      */
74     BASE_NS::string_view GetBaseShader() const;
75 
76     /** Get all shader variants.
77      * @return Array view of shader variants.
78      */
79     BASE_NS::array_view<const ShaderVariant> GetShaderVariants() const;
80 
81     /** Loads shader state from given uri, using file manager.
82      * @param fileManager A file manager to access the file in given uri.
83      * @param uri Uri to json file.
84      * @return A structure containing result for the parsing operation.
85      */
86     LoadResult Load(CORE_NS::IFileManager& fileManager, BASE_NS::string_view uri);
87 
88 private:
89     LoadResult Load(BASE_NS::string&& jsonData);
90 
91     BASE_NS::string uri_;
92     BASE_NS::string baseShader_;
93 
94     BASE_NS::vector<ShaderVariant> shaderVariants_;
95 };
96 CORE_END_NAMESPACE()
97 
98 #endif // LOADER_SHADER_DATA_LOADER_H
99