cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
ShaderManager.hpp
Go to the documentation of this file.
1///
2/// @file ShaderManager.hpp
3/// @brief This file contains the ShaderManager class declaration
4/// @namespace cae
5///
6
7#pragma once
8
10
11#include <memory>
12
13namespace cae
14{
15 ///
16 /// @class ShaderManager
17 /// @brief Class for managing shaders
18 /// @namespace cae
19 ///
21 {
22 public:
23 explicit ShaderManager(
24 const std::vector<std::function<std::shared_ptr<IShaderFrontend>()>> &shaderFrontendFactories,
25 const std::function<std::shared_ptr<IShaderIR>()> &shaderIRFactory = nullptr)
26 {
27 for (const auto &factory : shaderFrontendFactories)
28 {
29 auto frontend = factory();
30 registerFrontend(frontend);
31 }
32 if (shaderIRFactory)
33 {
34 registerIR(shaderIRFactory());
35 }
36 }
37 ~ShaderManager() = default;
38
39 ShaderManager(const ShaderManager &) = delete;
43
44 std::unordered_map<ShaderID, ShaderIRModule> build(const std::vector<ShaderSourceDesc> &sources,
45 const ShaderSourceType targetIR) const
46 {
47 std::unordered_map<ShaderID, ShaderIRModule> out;
48
49 const auto irProcessor = m_irs.at(targetIR);
50 for (const auto &src : sources)
51 {
52 const auto f = m_frontends.at(src.type);
53 ShaderIRModule ir = f->compile(src);
54 const ShaderIRModule final = irProcessor->process(ir);
55 out[src.id] = final;
56 }
57
58 return out;
59 }
60
61 template <std::ranges::input_range R> void optimizeAll(const ShaderSourceType irType, R &&modules) const
62 {
63 if (auto it = m_irs.find(irType); it != m_irs.end())
64 {
65 std::vector<ShaderIRModule *> ptrs;
66 for (auto &m : modules)
67 {
68 ptrs.push_back(&m);
69 }
70
71 std::vector<ShaderIRModule> tmp;
72 tmp.reserve(ptrs.size());
73 for (auto *p : ptrs)
74 {
75 tmp.push_back(*p);
76 }
77
78 it->second->optimize(tmp);
79
80 for (size_t i = 0; i < ptrs.size(); ++i)
81 {
82 *ptrs[i] = std::move(tmp[i]);
83 }
84 }
85 }
86
87 private:
88 void registerFrontend(const std::shared_ptr<IShaderFrontend> &f) { m_frontends[f->sourceType()] = f; }
89 void registerIR(const std::shared_ptr<IShaderIR> &ir) { m_irs[ir->irType()] = ir; }
90
91 std::unordered_map<ShaderSourceType, std::shared_ptr<IShaderFrontend>> m_frontends;
92 std::unordered_map<ShaderSourceType, std::shared_ptr<IShaderIR>> m_irs;
93
94 }; // class ShaderManager
95
96} // namespace cae
This file contains the ShaderIR interface.
Class for managing shaders.
ShaderManager & operator=(ShaderManager &&)=delete
ShaderManager(const std::vector< std::function< std::shared_ptr< IShaderFrontend >()> > &shaderFrontendFactories, const std::function< std::shared_ptr< IShaderIR >()> &shaderIRFactory=nullptr)
ShaderManager & operator=(const ShaderManager &)=delete
std::unordered_map< ShaderSourceType, std::shared_ptr< IShaderIR > > m_irs
void registerIR(const std::shared_ptr< IShaderIR > &ir)
std::unordered_map< ShaderSourceType, std::shared_ptr< IShaderFrontend > > m_frontends
ShaderManager(const ShaderManager &)=delete
~ShaderManager()=default
ShaderManager(ShaderManager &&)=delete
std::unordered_map< ShaderID, ShaderIRModule > build(const std::vector< ShaderSourceDesc > &sources, const ShaderSourceType targetIR) const
void registerFrontend(const std::shared_ptr< IShaderFrontend > &f)
void optimizeAll(const ShaderSourceType irType, R &&modules) const
Struct for shader intermediate representation module.