Antkeeper  0.0.1
shader-program.cpp
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 
23 #include <glad/gl.h>
24 #include <stdexcept>
25 #include <string_view>
26 
27 namespace gl {
28 
30 {
31  // Create an OpenGL shader program
32  gl_program_id = glCreateProgram();
33 
34  // Handle OpenGL errors
35  if (!gl_program_id)
36  {
37  throw std::runtime_error("Unable to create OpenGL shader program");
38  }
39 }
40 
42 {
43  // Detach all shader objects
44  detach_all();
45 
46  // Delete the OpenGL shader program
47  glDeleteProgram(gl_program_id);
48 }
49 
51 {
52  if (attached_objects.find(&object) != attached_objects.end())
53  {
54  throw std::runtime_error("OpenGL shader object already attached to the shader program");
55  }
56  else
57  {
58  // Check that both the OpenGL shader program and OpenGL shader object are valid
59  if (glIsProgram(gl_program_id) != GL_TRUE)
60  {
61  throw std::runtime_error("Invalid OpenGL shader program");
62  }
63  if (glIsShader(object.gl_shader_id) != GL_TRUE)
64  {
65  throw std::runtime_error("Invalid OpenGL shader object");
66  }
67 
68  // Attach the OpenGL shader object to the OpenGL shader program
69  glAttachShader(gl_program_id, object.gl_shader_id);
70 
71  // Add shader object to set of attached objects
72  attached_objects.insert(&object);
73  }
74 }
75 
77 {
78  if (attached_objects.find(&object) == attached_objects.end())
79  {
80  throw std::runtime_error("Shader object is not attached to the shader program.");
81  }
82  else
83  {
84  // Check that both the OpenGL shader program and OpenGL shader object are valid
85  if (glIsProgram(gl_program_id) != GL_TRUE)
86  {
87  throw std::runtime_error("Invalid OpenGL shader program");
88  }
89  if (glIsShader(object.gl_shader_id) != GL_TRUE)
90  {
91  throw std::runtime_error("Invalid OpenGL shader object");
92  }
93 
94  // Detach the OpenGL shader object from the OpenGL shader program
95  glDetachShader(gl_program_id, object.gl_shader_id);
96 
97  // Remove shader object from set of attached objects
98  attached_objects.erase(&object);
99  }
100 }
101 
103 {
104  while (!attached_objects.empty())
105  {
106  detach(**attached_objects.begin());
107  }
108 }
109 
111 {
112  m_linked = false;
113  info_log.clear();
114  variable_map.clear();
115 
116  // Check that the OpenGL shader program is valid
117  if (glIsProgram(gl_program_id) != GL_TRUE)
118  {
119  throw std::runtime_error("Invalid OpenGL shader program");
120  }
121 
122  // Link OpenGL shader program
123  glLinkProgram(gl_program_id);
124 
125  // Get OpenGL shader program linking status
126  GLint gl_link_status;
127  glGetProgramiv(gl_program_id, GL_LINK_STATUS, &gl_link_status);
128  m_linked = (gl_link_status == GL_TRUE);
129 
130  // Populate info log string
131  if (m_linked)
132  {
133  // Load shader variables
134  load_variables();
135  }
136  else
137  {
138  // Get OpenGL shader program info log length
139  GLint gl_info_log_length;
140  glGetProgramiv(gl_program_id, GL_INFO_LOG_LENGTH, &gl_info_log_length);
141 
142  if (gl_info_log_length > 0)
143  {
144  // Resize string to accommodate OpenGL shader program info log
145  info_log.resize(gl_info_log_length);
146 
147  // Read OpenGL shader program info log into string
148  glGetProgramInfoLog(gl_program_id, gl_info_log_length, &gl_info_log_length, info_log.data());
149 
150  // Remove redundant null terminator from string
151  info_log.pop_back();
152  }
153  }
154 
155  return m_linked;
156 }
157 
158 void shader_program::load_variables()
159 {
160  variable_map.clear();
161 
162  // Get maximum uniform name length
163  GLint max_uniform_name_length = 0;
164  glGetProgramiv(gl_program_id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_uniform_name_length);
165 
166  // Allocate uniform name buffer
167  std::string uniform_name(static_cast<std::size_t>(max_uniform_name_length), '\0');
168 
169  // Get number of active uniforms in the shader
170  GLint active_uniform_count = 0;
171  glGetProgramiv(gl_program_id, GL_ACTIVE_UNIFORMS, &active_uniform_count);
172 
173  // Init texture unit index
174  GLint texture_index = 0;
175 
176  // For each active uniform
177  for (GLint uniform_index = 0; uniform_index < active_uniform_count; ++uniform_index)
178  {
179  // Get uniform info
180  GLsizei uniform_name_length;
181  GLint uniform_size;
182  GLenum uniform_type;
183  glGetActiveUniform(gl_program_id, static_cast<GLuint>(uniform_index), static_cast<GLsizei>(max_uniform_name_length), &uniform_name_length, &uniform_size, &uniform_type, uniform_name.data());
184 
185  // Get uniform location
186  const GLint uniform_location = glGetUniformLocation(gl_program_id, uniform_name.c_str());
187  if (uniform_location == -1)
188  {
189  throw std::runtime_error("Unable to get shader uniform location");
190  }
191 
192  // Get length of variable name by stripping array notation from uniform name
193  std::size_t uniform_base_name_length = static_cast<std::size_t>(uniform_name_length);
194  if (std::size_t i = std::string_view(uniform_name.c_str(), uniform_name_length).find_first_of("["); i != std::string::npos)
195  {
196  uniform_base_name_length = i;
197  }
198 
199  // Hash variable name to get variable key
200  const hash::fnv1a32_t variable_key = hash::fnv1a32<char>({uniform_name.c_str(), uniform_base_name_length});
201 
202  // Get size of variable
203  const std::size_t variable_size = static_cast<std::size_t>(uniform_size);
204 
205  // Construct shader variable
206  std::unique_ptr<const gl::shader_variable> variable;
207  switch (uniform_type)
208  {
209  case GL_BOOL:
210  variable = std::make_unique<const gl_shader_bool>(variable_size, uniform_location);
211  break;
212  case GL_BOOL_VEC2:
213  variable = std::make_unique<const gl_shader_bvec2>(variable_size, uniform_location);
214  break;
215  case GL_BOOL_VEC3:
216  variable = std::make_unique<const gl_shader_bvec3>(variable_size, uniform_location);
217  break;
218  case GL_BOOL_VEC4:
219  variable = std::make_unique<const gl_shader_bvec4>(variable_size, uniform_location);
220  break;
221 
222  case GL_INT:
223  variable = std::make_unique<const gl_shader_int>(variable_size, uniform_location);
224  break;
225  case GL_INT_VEC2:
226  variable = std::make_unique<const gl_shader_ivec2>(variable_size, uniform_location);
227  break;
228  case GL_INT_VEC3:
229  variable = std::make_unique<const gl_shader_ivec3>(variable_size, uniform_location);
230  break;
231  case GL_INT_VEC4:
232  variable = std::make_unique<const gl_shader_ivec4>(variable_size, uniform_location);
233  break;
234 
235  case GL_UNSIGNED_INT:
236  variable = std::make_unique<const gl_shader_uint>(variable_size, uniform_location);
237  break;
238  case GL_UNSIGNED_INT_VEC2:
239  variable = std::make_unique<const gl_shader_uvec2>(variable_size, uniform_location);
240  break;
241  case GL_UNSIGNED_INT_VEC3:
242  variable = std::make_unique<const gl_shader_uvec3>(variable_size, uniform_location);
243  break;
244  case GL_UNSIGNED_INT_VEC4:
245  variable = std::make_unique<const gl_shader_uvec4>(variable_size, uniform_location);
246  break;
247 
248  case GL_FLOAT:
249  variable = std::make_unique<const gl_shader_float>(variable_size, uniform_location);
250  break;
251  case GL_FLOAT_VEC2:
252  variable = std::make_unique<const gl_shader_fvec2>(variable_size, uniform_location);
253  break;
254  case GL_FLOAT_VEC3:
255  variable = std::make_unique<const gl_shader_fvec3>(variable_size, uniform_location);
256  break;
257  case GL_FLOAT_VEC4:
258  variable = std::make_unique<const gl_shader_fvec4>(variable_size, uniform_location);
259  break;
260 
261  case GL_FLOAT_MAT2:
262  variable = std::make_unique<const gl_shader_fmat2>(variable_size, uniform_location);
263  break;
264  case GL_FLOAT_MAT3:
265  variable = std::make_unique<const gl_shader_fmat3>(variable_size, uniform_location);
266  break;
267  case GL_FLOAT_MAT4:
268  variable = std::make_unique<const gl_shader_fmat4>(variable_size, uniform_location);
269  break;
270 
271  case GL_SAMPLER_1D:
272  case GL_SAMPLER_1D_SHADOW:
273  variable = std::make_unique<const gl_shader_texture_1d>(variable_size, uniform_location, texture_index);
274  texture_index += uniform_size;
275  break;
276 
277  case GL_SAMPLER_2D:
278  case GL_SAMPLER_2D_SHADOW:
279  variable = std::make_unique<const gl_shader_texture_2d>(variable_size, uniform_location, texture_index);
280  texture_index += uniform_size;
281  break;
282 
283  case GL_SAMPLER_3D:
284  variable = std::make_unique<const gl_shader_texture_3d>(variable_size, uniform_location, texture_index);
285  texture_index += uniform_size;
286  break;
287 
288  case GL_SAMPLER_CUBE:
289  variable = std::make_unique<const gl_shader_texture_cube>(variable_size, uniform_location, texture_index);
290  texture_index += uniform_size;
291  break;
292 
293  default:
294  throw std::runtime_error("Unsupported to shader uniform type");
295  break;
296  }
297 
298  // Map variable to variable key
299  variable_map.emplace(variable_key, std::move(variable));
300  }
301 }
302 
303 } // namespace gl
Shader object which can be compiled and linked to a shader program.
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.
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.
void detach(const shader_object &object)
Detaches a shader object from the shader program.
Graphics library interface.
Definition: window.hpp:28
32-bit FNV-1a hash value.
Definition: fnv1a.hpp:117