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

164 lines
5.2 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. #ifndef ANTKEEPER_GL_SHADER_PROGRAM_HPP
  20. #define ANTKEEPER_GL_SHADER_PROGRAM_HPP
  21. #include <string>
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. #include <cstdint>
  25. #include <memory>
  26. #include <engine/utility/hash/fnv1a.hpp>
  27. namespace gl {
  28. class shader_object;
  29. class shader_variable;
  30. /**
  31. * Shader program which can be linked to shader objects and executed.
  32. *
  33. * @see gl::shader_object
  34. */
  35. class shader_program
  36. {
  37. public:
  38. /**
  39. * Creates an empty shader program.
  40. *
  41. * @exception std::runtime_error An error occurred while creating an OpenGL shader program.
  42. */
  43. shader_program();
  44. /**
  45. * Destroys a shader program.
  46. */
  47. ~shader_program();
  48. shader_program(const shader_program&) = delete;
  49. shader_program(shader_program&&) = delete;
  50. shader_program& operator=(const shader_program&) = delete;
  51. shader_program& operator=(shader_program&&) = delete;
  52. /**
  53. * Attaches a shader object to the shader program. Attaching a shader object has no effect on a shader program until shader_program::link() is called.
  54. *
  55. * @param object Shader object to attach.
  56. *
  57. * @exception std::runtime_error Shader object is already attached to the shader program.
  58. * @exception std::runtime_error OpenGL shader program is not a valid program object.
  59. * @exception std::runtime_error OpenGL shader object is not a valid shader object.
  60. * @exception std::runtime_error OpenGL shader object is already attached to the shader program.
  61. *
  62. * @see shader_program::link()
  63. */
  64. void attach(const shader_object& object);
  65. /**
  66. * Detaches a shader object from the shader program. Detaching a shader object has no effect on a shader program until shader_program::link() is called.
  67. *
  68. * @param object Shader object to detach.
  69. *
  70. * @exception std::runtime_error Shader object is not attached to the shader program.
  71. * @exception std::runtime_error OpenGL shader program is not a valid program object.
  72. * @exception std::runtime_error OpenGL shader object is not a valid shader object.
  73. * @exception std::runtime_error OpenGL shader object is not attached to the shader program.
  74. *
  75. * @see shader_program::link()
  76. */
  77. void detach(const shader_object& object);
  78. /**
  79. * Detaches all shader objects from the shader program.
  80. *
  81. * @exception std::runtime_error Shader object is not attached to the shader program.
  82. * @exception std::runtime_error OpenGL shader program is not a valid program object.
  83. * @exception std::runtime_error OpenGL shader object is not a valid shader object.
  84. * @exception std::runtime_error OpenGL shader object is not attached to the shader program.
  85. *
  86. * @see shader_program::detach(const shader_object*)
  87. */
  88. void detach_all();
  89. /**
  90. * Links all attached shader objects to create an executable shader program.
  91. *
  92. * @return `true` if the attached shader objects were successfully linked into the shader program, `false` otherwise.
  93. *
  94. * @warning All existing of the shader program's variables will be invalidated if the program is re-linked.
  95. */
  96. bool link();
  97. /// Returns `true` if the shader program has been successfully linked, `false` otherwise.
  98. [[nodiscard]] inline bool linked() const noexcept
  99. {
  100. return m_linked;
  101. }
  102. /**
  103. * Returns all active shader variables in the shader program.
  104. *
  105. * @return Map of 32-bit FNV-1a hash values of shader variable names to shader variables.
  106. */
  107. [[nodiscard]] inline const std::unordered_map<hash::fnv1a32_t, const std::unique_ptr<const shader_variable>>& variables() const noexcept
  108. {
  109. return variable_map;
  110. }
  111. /**
  112. * Returns a pointer to an active shader variable with the given name, or `nullptr` if not found.
  113. *
  114. * @param key 32-bit FNV-1a hash value of a shader variable name.
  115. *
  116. * @return Pointer to the active shader variable with the given name, or `nullptr` if not found.
  117. */
  118. [[nodiscard]] inline const shader_variable* variable(hash::fnv1a32_t key) const
  119. {
  120. if (auto i = variable_map.find(key); i != variable_map.end())
  121. {
  122. return i->second.get();
  123. }
  124. return nullptr;
  125. }
  126. /**
  127. * Returns the info log that contains debug information when linking fails.
  128. */
  129. [[nodiscard]] inline const std::string& info() const noexcept
  130. {
  131. return info_log;
  132. }
  133. private:
  134. friend class pipeline;
  135. void load_variables();
  136. unsigned int gl_program_id{0};
  137. bool m_linked{false};
  138. std::unordered_set<const shader_object*> attached_objects;
  139. std::unordered_map<hash::fnv1a32_t, const std::unique_ptr<const shader_variable>> variable_map;
  140. std::string info_log;
  141. };
  142. } // namespace gl
  143. #endif // ANTKEEPER_GL_SHADER_PROGRAM_HPP