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_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
34CORE_BEGIN_NAMESPACE()
35class IFileManager;
36CORE_END_NAMESPACE()
37RENDER_BEGIN_NAMESPACE()
38class PipelineLayoutLoader;
39class VertexInputDeclarationLoader;
40class ShaderStateLoader;
41struct 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 */
47class ShaderLoader {
48public:
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 with given paths, parses them, and loads the listed shaders. */
59    void Load(const ShaderManager::ShaderFilePathDesc& desc);
60    /** Looks for json files with given path, parses them, and loads the listed data. */
61    void LoadFile(BASE_NS::string_view uri, const bool forceReload);
62
63private:
64    void HandleShaderFile(
65        BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry, const bool forceReload);
66    void HandleShaderStateFile(BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry);
67    void HandlePipelineLayoutFile(BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry);
68    void HandleVertexInputDeclarationFile(BASE_NS::string_view currentPath, const CORE_NS::IDirectory::Entry& entry);
69    void RecurseDirectory(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
70    struct ShaderFile {
71        BASE_NS::vector<uint8_t> data;
72        BASE_NS::vector<uint8_t> reflectionData;
73        ShaderModuleCreateInfo info;
74    };
75    ShaderFile LoadShaderFile(BASE_NS::string_view shader, ShaderStageFlags stageBits);
76    RenderHandleReference CreateComputeShader(const ShaderDataLoader& dataLoader, const bool forceReload);
77    RenderHandleReference CreateGraphicsShader(const ShaderDataLoader& dataLoader, const bool forceReload);
78    RenderHandleReference CreateShader(const ShaderDataLoader& dataLoader, const bool forceReload);
79
80    void LoadShaderStates(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
81    void CreateShaderStates(BASE_NS::string_view uri,
82        const BASE_NS::array_view<const ShaderStateLoaderVariantData>& variantData,
83        const BASE_NS::array_view<const GraphicsState>& states);
84
85    void LoadVids(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
86    RenderHandleReference CreateVertexInputDeclaration(const VertexInputDeclarationLoader& loader);
87
88    void LoadPipelineLayouts(BASE_NS::string_view currentPath, const CORE_NS::IDirectory& directory);
89    RenderHandleReference CreatePipelineLayout(const PipelineLayoutLoader& loader);
90
91    CORE_NS::IFileManager& fileManager_;
92    ShaderManager& shaderMgr_;
93    DeviceBackendType type_;
94
95    struct ShaderModuleShaders {
96        ShaderStageFlags shaderStageFlags { 0u };
97        BASE_NS::vector<BASE_NS::string> shaderNames;
98    };
99#if (RENDER_DEV_ENABLED == 1)
100    // Maps shader source file to shader resource names which use the shader.
101    // For book-keeping in dev mode for spv reloading.
102    BASE_NS::unordered_map<BASE_NS::string, ShaderModuleShaders> fileToShaderNames_;
103#endif
104};
105RENDER_END_NAMESPACE()
106
107#endif // LOADER_SHADER_LOADER_H
108