Antkeeper  0.0.1
shader-object.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 
21 #include <glad/gl.h>
22 #include <stdexcept>
23 
24 namespace gl {
25 
26 static constexpr GLenum gl_shader_type_lut[] =
27 {
28  GL_VERTEX_SHADER,
29  GL_FRAGMENT_SHADER,
30  GL_GEOMETRY_SHADER
31 };
32 
34  m_stage{stage}
35 {
36  // Look up OpenGL shader type enumeration that corresponds to the given stage
37  const GLenum gl_shader_type = gl_shader_type_lut[static_cast<std::size_t>(m_stage)];
38 
39  // Create an OpenGL shader object
40  gl_shader_id = glCreateShader(gl_shader_type);
41  if (!gl_shader_id)
42  {
43  throw std::runtime_error("Unable to create OpenGL shader object");
44  }
45 }
46 
48 {
49  glDeleteShader(gl_shader_id);
50 }
51 
52 void shader_object::source(std::string_view source_code)
53 {
54  // Replace OpenGL shader object source code
55  const GLint gl_length = static_cast<GLint>(source_code.length());
56  const GLchar* gl_string = source_code.data();
57  glShaderSource(gl_shader_id, 1, &gl_string, &gl_length);
58 }
59 
61 {
62  m_compiled = false;
63  info_log.clear();
64 
65  // Compile OpenGL shader object
66  glCompileShader(gl_shader_id);
67 
68  // Get OpenGL shader object compilation status
69  GLint gl_compile_status;
70  glGetShaderiv(gl_shader_id, GL_COMPILE_STATUS, &gl_compile_status);
71  m_compiled = (gl_compile_status == GL_TRUE);
72 
73  // Get OpenGL shader object info log length
74  GLint gl_info_log_length;
75  glGetShaderiv(gl_shader_id, GL_INFO_LOG_LENGTH, &gl_info_log_length);
76 
77  if (gl_info_log_length > 0)
78  {
79  // Resize string to accommodate OpenGL shader object info log
80  info_log.resize(gl_info_log_length);
81 
82  // Read OpenGL shader object info log into string
83  glGetShaderInfoLog(gl_shader_id, gl_info_log_length, &gl_info_log_length, info_log.data());
84 
85  // Remove redundant null terminator from string
86  info_log.pop_back();
87  }
88 
89  // Return compilation status
90  return m_compiled;
91 }
92 
93 } // namespace gl
bool compile()
Compiles the shader object.
~shader_object()
Destroys a shader object.
void source(std::string_view source_code)
Replaces the source code of the shader object.
shader_object(shader_stage stage)
Creates an empty shader object for the specified shader stage.
Graphics library interface.
Definition: window.hpp:28
shader_stage
Enumerates all supported shader stages.