💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

  1. /*
  2. * Copyright (C) 2021 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. #include "gl/shader-object.hpp"
  20. #include <glad/glad.h>
  21. #include <stdexcept>
  22. namespace gl {
  23. static constexpr GLenum gl_shader_type_lut[] =
  24. {
  25. GL_VERTEX_SHADER,
  26. GL_FRAGMENT_SHADER,
  27. GL_GEOMETRY_SHADER
  28. };
  29. shader_object::shader_object(shader_stage stage):
  30. gl_shader_id(0),
  31. stage(stage),
  32. compiled(false)
  33. {
  34. // Look up OpenGL shader type enumeration that corresponds to the given stage
  35. GLenum gl_shader_type = gl_shader_type_lut[static_cast<std::size_t>(stage)];
  36. // Create an OpenGL shader object
  37. gl_shader_id = glCreateShader(gl_shader_type);
  38. // Handle OpenGL errors
  39. if (!gl_shader_id)
  40. {
  41. throw std::runtime_error("An error occurred while creating an OpenGL shader object.");
  42. }
  43. }
  44. shader_object::~shader_object()
  45. {
  46. // Flag the OpenGL shader object for deletion
  47. glDeleteShader(gl_shader_id);
  48. }
  49. void shader_object::source(const std::string& source_code)
  50. {
  51. // Replace OpenGL shader object source code
  52. GLint gl_length = static_cast<GLint>(source_code.length());
  53. const GLchar* gl_string = source_code.c_str();
  54. glShaderSource(gl_shader_id, 1, &gl_string, &gl_length);
  55. // Handle OpenGL errors
  56. switch (glGetError())
  57. {
  58. case GL_INVALID_VALUE:
  59. throw std::runtime_error("OpenGL shader object handle is not a value generated by OpenGL.");
  60. break;
  61. case GL_INVALID_OPERATION:
  62. throw std::runtime_error("OpenGL shader object handle is not a shader object.");
  63. break;
  64. }
  65. }
  66. bool shader_object::compile()
  67. {
  68. // Compile OpenGL shader object
  69. glCompileShader(gl_shader_id);
  70. // Handle OpenGL errors
  71. switch (glGetError())
  72. {
  73. case GL_INVALID_VALUE:
  74. throw std::runtime_error("OpenGL shader object handle is not a value generated by OpenGL.");
  75. break;
  76. case GL_INVALID_OPERATION:
  77. throw std::runtime_error("OpenGL shader object handle is not a shader object.");
  78. break;
  79. }
  80. // Get OpenGL shader object compilation status
  81. GLint gl_compile_status;
  82. glGetShaderiv(gl_shader_id, GL_COMPILE_STATUS, &gl_compile_status);
  83. compiled = (gl_compile_status == GL_TRUE);
  84. // Get OpenGL shader object info log length
  85. GLint gl_info_log_length;
  86. glGetShaderiv(gl_shader_id, GL_INFO_LOG_LENGTH, &gl_info_log_length);
  87. if (gl_info_log_length > 0)
  88. {
  89. // Resize string to accommodate OpenGL shader object info log
  90. info_log.resize(gl_info_log_length);
  91. // Read OpenGL shader object info log into string
  92. glGetShaderInfoLog(gl_shader_id, gl_info_log_length, &gl_info_log_length, info_log.data());
  93. // Remove redundant null terminator from string
  94. info_log.pop_back();
  95. }
  96. else
  97. {
  98. // Empty info log
  99. info_log.clear();
  100. }
  101. // Return compilation status
  102. return compiled;
  103. }
  104. } // namespace gl