Antkeeper  0.0.1
shader-program.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_PROGRAM_HPP
21 #define ANTKEEPER_GL_SHADER_PROGRAM_HPP
22 
23 #include <string>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include <cstdint>
27 #include <memory>
29 
30 namespace gl {
31 
32 class shader_object;
33 class shader_variable;
34 
41 {
42 public:
49 
54 
55  shader_program(const shader_program&) = delete;
59 
72  void attach(const shader_object& object);
73 
86  void detach(const shader_object& object);
87 
98  void detach_all();
99 
107  bool link();
108 
110  [[nodiscard]] inline bool linked() const noexcept
111  {
112  return m_linked;
113  }
114 
120  [[nodiscard]] inline const std::unordered_map<hash::fnv1a32_t, const std::unique_ptr<const shader_variable>>& variables() const noexcept
121  {
122  return variable_map;
123  }
124 
132  [[nodiscard]] inline const shader_variable* variable(hash::fnv1a32_t key) const
133  {
134  if (auto i = variable_map.find(key); i != variable_map.end())
135  {
136  return i->second.get();
137  }
138 
139  return nullptr;
140  }
141 
145  [[nodiscard]] inline const std::string& info() const noexcept
146  {
147  return info_log;
148  }
149 
150 private:
151  friend class pipeline;
152 
153  void load_variables();
154 
155  unsigned int gl_program_id{0};
156  bool m_linked{false};
157  std::unordered_set<const shader_object*> attached_objects;
158  std::unordered_map<hash::fnv1a32_t, const std::unique_ptr<const shader_variable>> variable_map;
159  std::string info_log;
160 };
161 
162 } // namespace gl
163 
164 #endif // ANTKEEPER_GL_SHADER_PROGRAM_HPP
Graphics pipeline interface.
Definition: pipeline.hpp:48
Shader object which can be compiled and linked to a shader program.
Shader program which can be linked to shader objects and executed.
shader_program()
Creates an empty shader program.
const shader_variable * variable(hash::fnv1a32_t key) const
Returns a pointer to an active shader variable with the given name, or nullptr if not found.
const std::unordered_map< hash::fnv1a32_t, const std::unique_ptr< const shader_variable > > & variables() const noexcept
Returns all active shader variables in the shader program.
shader_program(shader_program &&)=delete
void attach(const shader_object &object)
Attaches a shader object to the shader program.
bool link()
Links all attached shader objects to create an executable shader program.
~shader_program()
Destroys a shader program.
void detach_all()
Detaches all shader objects from the shader program.
const std::string & info() const noexcept
Returns the info log that contains debug information when linking fails.
shader_program(const shader_program &)=delete
bool linked() const noexcept
Returns true if the shader program has been successfully linked, false otherwise.
void detach(const shader_object &object)
Detaches a shader object from the shader program.
shader_program & operator=(const shader_program &)=delete
shader_program & operator=(shader_program &&)=delete
Shader program variable.
Graphics library interface.
Definition: window.hpp:28
32-bit FNV-1a hash value.
Definition: fnv1a.hpp:117