• 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_LOADER_H
17 #define LOADER_SHADER_LOADER_H
18 
19 #include <base/containers/array_view.h>
20 #include <base/containers/string.h>
21 #include <base/containers/string_view.h>
22 #include <base/containers/unordered_map.h>
23 #include <base/containers/vector.h>
24 #include <core/io/intf_directory.h>
25 #include <core/namespace.h>
26 #include <render/device/intf_device.h>
27 #include <render/device/pipeline_state_desc.h>
28 #include <render/namespace.h>
29 #include <render/resource_handle.h>
30 
31 #include "device/shader_manager.h"
32 #include "loader/shader_data_loader.h"
33 
34 CORE_BEGIN_NAMESPACE()
35 class IFileManager;
36 CORE_END_NAMESPACE()
37 RENDER_BEGIN_NAMESPACE()
38 class PipelineLayoutLoader;
39 class VertexInputDeclarationLoader;
40 class ShaderStateLoader;
41 struct ShaderStateLoaderVariantData;
42 
43 /** ShaderLoader.
44  * A class that can be used to load all the shaders states from shaders:// and reload the shaders e.g. when the shader
45  * has been modified. In addition handles vertex input declaration loading.
46  */
47 class ShaderLoader {
48 public:
49     /** Constructor.
50      * @param fileManager File manager to be used when loading files.
51      * @param aResourceManager Resource manager to be used when loading shaders.
52      */
53     ShaderLoader(CORE_NS::IFileManager& fileManager, ShaderManager& shaderManager, DeviceBackendType type);
54 
55     /** Destructor. */
56     ~ShaderLoader() = default;
57 
58     /** Looks for json files under default shader data paths, parses them, and loads the listed shaders. */
59     void Load(const ShaderManager::ShaderFilePathDesc& desc);
60     /** Looks for json files under default shader data paths, parses them, and loads the listed data. */
61     void LoadFile(BASE_NS::string_view uri, const bool forceReload);
62 
63     /** Reloads the given list of shader spv files.
64      * Works only properly with RENDER_DEV_ENABLED.
65      * @param shaderSourceFiles List of shader files.
66      */
67     void Reload(const BASE_NS::array_view<BASE_NS::string>& shaderSpvFiles);
68 
69 private:
70     void HandleShaderFile(
71         BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry, const bool forceReload);
72     void HandleShaderStateFile(BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry);
73     void HandlePipelineLayoutFile(BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry);
74     void HandleVertexInputDeclarationFile(BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry);
75     void RecurseDirectory(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
76     struct ShaderFile {
77         BASE_NS::vector<uint8_t> data;
78         BASE_NS::vector<uint8_t> reflectionData;
79         ShaderModuleCreateInfo info;
80     };
81     ShaderFile LoadShaderFile(BASE_NS::string_view shader, ShaderStageFlags stageBits);
82     RenderHandleReference CreateComputeShader(const ShaderDataLoader& shaderStateLoader, const bool forceReload);
83     RenderHandleReference CreateGraphicsShader(const ShaderDataLoader& shaderStateLoader, const bool forceReload);
84     RenderHandleReference CreateShader(const ShaderDataLoader& shaderStateLoader, const bool forceReload);
85 
86     void LoadShaderStates(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
87     void CreateShaderStates(BASE_NS::string_view uri,
88         const BASE_NS::array_view<const ShaderStateLoaderVariantData>& variantData,
89         const BASE_NS::array_view<const GraphicsState>& states);
90 
91     void LoadVids(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
92     RenderHandleReference CreateVertexInputDeclaration(const VertexInputDeclarationLoader& loader);
93 
94     void LoadPipelineLayouts(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
95     RenderHandleReference CreatePipelineLayout(const PipelineLayoutLoader& loader);
96 
97     CORE_NS::IFileManager& fileManager_;
98     ShaderManager& shaderMgr_;
99     DeviceBackendType type_;
100 
101     struct ShaderModuleShaders {
102         ShaderStageFlags shaderStageFlags { 0u };
103         BASE_NS::vector<BASE_NS::string> shaderNames;
104     };
105 #if (RENDER_DEV_ENABLED == 1)
106     // Maps shader source file to shader resource names which use the shader.
107     // For book-keeping in dev mode for spv reloading.
108     BASE_NS::unordered_map<BASE_NS::string, ShaderModuleShaders> fileToShaderNames_;
109 #endif
110 };
111 RENDER_END_NAMESPACE()
112 
113 #endif // LOADER_SHADER_LOADER_H
114