Antkeeper  0.0.1
shader-template.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GL_SHADER_TEMPLATE_HPP
21 #define ANTKEEPER_GL_SHADER_TEMPLATE_HPP
22 
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <unordered_map>
30 #include <unordered_set>
31 #include <vector>
32 
33 namespace gl {
34 
50 {
51 public:
53  using dictionary_type = std::unordered_map<std::string, std::string>;
54 
61  explicit shader_template(const text_file& source_code);
62  explicit shader_template(text_file&& source_code);
64 
73  shader_template(text_file&& source_code, std::vector<std::shared_ptr<text_file>>&& include_files);
74 
78  constexpr shader_template() noexcept = default;
79 
86  void source(const text_file& source_code);
87  void source(text_file&& source_code);
89 
98  [[nodiscard]] std::string configure(gl::shader_stage stage, const dictionary_type& definitions = {}) const;
99 
110  [[nodiscard]] std::unique_ptr<gl::shader_object> compile(gl::shader_stage stage, const dictionary_type& definitions = {}) const;
111 
125  [[nodiscard]] std::unique_ptr<gl::shader_program> build(const dictionary_type& definitions = {}) const;
126 
128  [[nodiscard]] inline bool has_vertex_directive() const noexcept
129  {
130  return !m_vertex_directives.empty();
131  }
132 
134  [[nodiscard]] inline bool has_fragment_directive() const noexcept
135  {
136  return !m_fragment_directives.empty();
137  }
138 
140  [[nodiscard]] inline bool has_geometry_directive() const noexcept
141  {
142  return !m_geometry_directives.empty();
143  }
144 
150  [[nodiscard]] bool has_define_directive(const std::string& key) const;
151 
153  [[nodiscard]] inline constexpr std::size_t hash() const noexcept
154  {
155  return m_hash;
156  }
157 
158 private:
159  void find_directives();
160  void rehash();
161  void replace_stage_directives(gl::shader_stage stage) const;
162  void replace_define_directives(const dictionary_type& definitions) const;
163 
164  mutable text_file m_template_source;
165  std::unordered_set<std::size_t> m_vertex_directives;
166  std::unordered_set<std::size_t> m_fragment_directives;
167  std::unordered_set<std::size_t> m_geometry_directives;
168  std::multimap<std::string, std::size_t> m_define_directives;
169  std::size_t m_hash{0};
170  std::vector<std::shared_ptr<text_file>> m_include_files;
171 };
172 
173 } // namespace gl
174 
175 #endif // ANTKEEPER_GL_SHADER_TEMPLATE_HPP
Template used to for generating one or more shader variants from a single source.
std::string configure(gl::shader_stage stage, const dictionary_type &definitions={}) const
Configures shader object source code for a given shader stage and template dictionary.
constexpr std::size_t hash() const noexcept
Returns a hash of the template source code.
std::unique_ptr< gl::shader_program > build(const dictionary_type &definitions={}) const
Configures and compiles shader objects, then links them into a shader program.
std::unordered_map< std::string, std::string > dictionary_type
Container of definitions used to generate #pragma define <key> <value> directives.
bool has_geometry_directive() const noexcept
Returns true if the template source contains one or more #pragma geometry directive.
constexpr shader_template() noexcept=default
Constructs an empty shader template.
bool has_define_directive(const std::string &key) const
Returns true if the template source contains one or more instance of #pragma define <key>.
bool has_vertex_directive() const noexcept
Returns true if the template source contains one or more #pragma vertex directive.
void source(const text_file &source_code)
Replaces the source code of the shader template.
std::unique_ptr< gl::shader_object > compile(gl::shader_stage stage, const dictionary_type &definitions={}) const
Configures and compiles a shader object.
bool has_fragment_directive() const noexcept
Returns true if the template source contains one or more #pragma fragment directive.
Graphics library interface.
Definition: window.hpp:28
shader_stage
Enumerates all supported shader stages.
Virtual text file.
Definition: text-file.hpp:30