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

116 lines
3.1 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. #ifndef ANTKEEPER_GL_SHADER_OBJECT_HPP
  20. #define ANTKEEPER_GL_SHADER_OBJECT_HPP
  21. #include "gl/shader-stage.hpp"
  22. #include <cstdlib>
  23. #include <string>
  24. namespace gl {
  25. class shader_program;
  26. /**
  27. * Shader object which can be compiled and linked to a shader program.
  28. *
  29. * @see gl::shader_program
  30. * @see gl::shader_stage
  31. */
  32. class shader_object
  33. {
  34. public:
  35. /**
  36. * Creates an empty shader object for the specified shader stage.
  37. *
  38. * @param stage Shader stage in which this shader object will be used.
  39. *
  40. * @exception std::runtime_error An error occurred while creating an OpenGL shader object.
  41. */
  42. shader_object(shader_stage stage);
  43. /**
  44. * Destroys a shader object.
  45. */
  46. ~shader_object();
  47. /**
  48. * Replaces the source code of the shader object.
  49. *
  50. * @param buffer Buffer containing shader object source code.
  51. * @param size Size of the source code, in bytes.
  52. *
  53. * @exception std::runtime_error Shader object handle is not a value generated by OpenGL.
  54. * @exception std::runtime_error Shader object handle is not a shader object.
  55. */
  56. void source(const char* buffer, std::size_t size);
  57. /**
  58. * Compiles the shader object.
  59. *
  60. * @return `true` if the shader object was compiled successfully, `false` otherwise. If compilation fails, check the info log via shader_object::get_info_log() for more information.
  61. *
  62. * @exception std::runtime_error Shader object handle is not a value generated by OpenGL.
  63. * @exception std::runtime_error Shader object handle is not a shader object.
  64. *
  65. * @see shader_object::get_info_log()
  66. */
  67. bool compile();
  68. /// Returns the shader stage of this shader object.
  69. shader_stage get_stage() const;
  70. /// Returns the shader object info log, which is updated when the shader is compiled.
  71. const std::string& get_info_log() const;
  72. /// Returns `true` if the shader object has been successfully compiled, `false` otherwise.
  73. bool was_compiled() const;
  74. shader_object(const shader_object&) = delete;
  75. shader_object& operator=(const shader_object&) = delete;
  76. private:
  77. friend class shader_program;
  78. unsigned int gl_shader_id;
  79. shader_stage stage;
  80. bool compiled;
  81. std::string info_log;
  82. };
  83. inline shader_stage shader_object::get_stage() const
  84. {
  85. return stage;
  86. }
  87. inline const std::string& shader_object::get_info_log() const
  88. {
  89. return info_log;
  90. }
  91. inline bool shader_object::was_compiled() const
  92. {
  93. return compiled;
  94. }
  95. } // namespace gl
  96. #endif // ANTKEEPER_GL_SHADER_OBJECT_HPP