💿🐜 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.

93 lines
2.6 KiB

  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. #include <engine/gl/shader-object.hpp>
  20. #include <glad/gl.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. m_stage{stage}
  31. {
  32. // Look up OpenGL shader type enumeration that corresponds to the given stage
  33. const GLenum gl_shader_type = gl_shader_type_lut[static_cast<std::size_t>(m_stage)];
  34. // Create an OpenGL shader object
  35. gl_shader_id = glCreateShader(gl_shader_type);
  36. if (!gl_shader_id)
  37. {
  38. throw std::runtime_error("Unable to create OpenGL shader object");
  39. }
  40. }
  41. shader_object::~shader_object()
  42. {
  43. glDeleteShader(gl_shader_id);
  44. }
  45. void shader_object::source(std::string_view source_code)
  46. {
  47. // Replace OpenGL shader object source code
  48. const GLint gl_length = static_cast<GLint>(source_code.length());
  49. const GLchar* gl_string = source_code.data();
  50. glShaderSource(gl_shader_id, 1, &gl_string, &gl_length);
  51. }
  52. bool shader_object::compile()
  53. {
  54. m_compiled = false;
  55. info_log.clear();
  56. // Compile OpenGL shader object
  57. glCompileShader(gl_shader_id);
  58. // Get OpenGL shader object compilation status
  59. GLint gl_compile_status;
  60. glGetShaderiv(gl_shader_id, GL_COMPILE_STATUS, &gl_compile_status);
  61. m_compiled = (gl_compile_status == GL_TRUE);
  62. // Get OpenGL shader object info log length
  63. GLint gl_info_log_length;
  64. glGetShaderiv(gl_shader_id, GL_INFO_LOG_LENGTH, &gl_info_log_length);
  65. if (gl_info_log_length > 0)
  66. {
  67. // Resize string to accommodate OpenGL shader object info log
  68. info_log.resize(gl_info_log_length);
  69. // Read OpenGL shader object info log into string
  70. glGetShaderInfoLog(gl_shader_id, gl_info_log_length, &gl_info_log_length, info_log.data());
  71. // Remove redundant null terminator from string
  72. info_log.pop_back();
  73. }
  74. // Return compilation status
  75. return m_compiled;
  76. }
  77. } // namespace gl