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

123 lines
3.2 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. // Flags the OpenGL shader object for deletion
  47. glDeleteShader(gl_shader_id);
  48. }
  49. void shader_object::source(const char* buffer, std::size_t size)
  50. {
  51. // Replace OpenGL shader object source code
  52. GLint gl_length = size;
  53. glShaderSource(gl_shader_id, 1, &buffer, &gl_length);
  54. // Handle OpenGL errors
  55. switch (glGetError())
  56. {
  57. case GL_INVALID_VALUE:
  58. throw std::runtime_error("Shader object handle is not a value generated by OpenGL.");
  59. break;
  60. case GL_INVALID_OPERATION:
  61. throw std::runtime_error("Shader object handle is not a shader object.");
  62. break;
  63. }
  64. }
  65. bool shader_object::compile()
  66. {
  67. // Compile OpenGL shader object
  68. glCompileShader(gl_shader_id);
  69. // Handle OpenGL errors
  70. switch (glGetError())
  71. {
  72. case GL_INVALID_VALUE:
  73. throw std::runtime_error("Shader object handle is not a value generated by OpenGL.");
  74. break;
  75. case GL_INVALID_OPERATION:
  76. throw std::runtime_error("Shader object handle is not a shader object.");
  77. break;
  78. }
  79. // Get OpenGL shader object compilation status
  80. GLint gl_compile_status;
  81. glGetShaderiv(gl_shader_id, GL_COMPILE_STATUS, &gl_compile_status);
  82. compiled = (gl_compile_status == GL_TRUE);
  83. // Get OpenGL shader object info log length
  84. GLint gl_info_log_length;
  85. glGetShaderiv(gl_shader_id, GL_INFO_LOG_LENGTH, &gl_info_log_length);
  86. if (gl_info_log_length > 0)
  87. {
  88. // Resize string to accommodate OpenGL shader object info log
  89. info_log.resize(gl_info_log_length);
  90. // Read OpenGL shader object info log into string
  91. glGetShaderInfoLog(gl_shader_id, gl_info_log_length, &gl_info_log_length, info_log.data());
  92. // Remove redundant null terminator from string
  93. info_log.pop_back();
  94. }
  95. else
  96. {
  97. // Empty info log
  98. info_log.clear();
  99. }
  100. // Return compilation status
  101. return compiled;
  102. }
  103. } // namespace gl