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

175 lines
6.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_TEMPLATE_HPP
  20. #define ANTKEEPER_GL_SHADER_TEMPLATE_HPP
  21. #include <engine/gl/shader-object.hpp>
  22. #include <engine/gl/shader-program.hpp>
  23. #include <engine/utility/text-file.hpp>
  24. #include <map>
  25. #include <memory>
  26. #include <string>
  27. #include <unordered_map>
  28. #include <unordered_set>
  29. #include <vector>
  30. namespace gl {
  31. /**
  32. * Template used to for generating one or more shader variants from a single source.
  33. *
  34. * Shader templates support the following preprocessor directives:
  35. *
  36. * * `#pragma vertex`: Replaced with `#define __VERTEX__` when generating vertex shader objects.
  37. * * `#pragma fragment`: Replaced with `#define __FRAGMENT__` when generating fragment shader objects.
  38. * * `#pragma geometry`: Replaced with `#define __GEOMETRY__` when generating geometry shader objects.
  39. * * `#pragma define <key>`: Will be replaced with `#define <key> <value>` if its definition is passed to the shader template.
  40. *
  41. * @see gl::shader_stage
  42. * @see gl::shader_object
  43. * @see gl::shader_program
  44. */
  45. class shader_template
  46. {
  47. public:
  48. /// Container of definitions used to generate `#pragma define <key> <value>` directives.
  49. using dictionary_type = std::unordered_map<std::string, std::string>;
  50. /**
  51. * Constructs a shader template and sets its source code.
  52. *
  53. * @param source_code Shader template source code.
  54. */
  55. /// @{
  56. explicit shader_template(const text_file& source_code);
  57. explicit shader_template(text_file&& source_code);
  58. /// @}
  59. /**
  60. * Constructs a shader template and sets its source code.
  61. *
  62. * @param source_code Shader template source code.
  63. * @param include_files Shader template include files.
  64. *
  65. * @note This constuctor is used to keep the loaded include files cached.
  66. */
  67. shader_template(text_file&& source_code, std::vector<std::shared_ptr<text_file>>&& include_files);
  68. /**
  69. * Constructs an empty shader template.
  70. */
  71. constexpr shader_template() noexcept = default;
  72. /**
  73. * Replaces the source code of the shader template.
  74. *
  75. * @param source_code Shader template source code.
  76. */
  77. /// @{
  78. void source(const text_file& source_code);
  79. void source(text_file&& source_code);
  80. /// @}
  81. /**
  82. * Configures shader object source code for a given shader stage and template dictionary.
  83. *
  84. * @param stage Shader stage of the shader object to generate. Instances of `#pragma <stage>` in the template source will be replaced with `#define __<STAGE>__`.
  85. * @param definitions Container of definitions used to replace `#pragma define <key> <value>` directives.
  86. *
  87. * @return Configured shader object source code.
  88. */
  89. [[nodiscard]] std::string configure(gl::shader_stage stage, const dictionary_type& definitions = {}) const;
  90. /**
  91. * Configures and compiles a shader object.
  92. *
  93. * @param stage Shader stage of the shader object to generate. Instances of `#pragma <stage>` in the template source will be replaced with `#define __<STAGE>__`.
  94. * @param definitions Container of definitions used to replace `#pragma define <key> <value>` directives.
  95. *
  96. * @return Compiled shader object.
  97. *
  98. * @exception std::runtime_error Any exceptions thrown by gl::shader_object.
  99. */
  100. [[nodiscard]] std::unique_ptr<gl::shader_object> compile(gl::shader_stage stage, const dictionary_type& definitions = {}) const;
  101. /**
  102. * Configures and compiles shader objects, then links them into a shader program. Shader object stages are determined according to the presence of `#pragma <stage>` directives.
  103. *
  104. * @param definitions Container of definitions used to replace `#pragma define <key> <value>` directives.
  105. *
  106. * @return Linked shader program.
  107. *
  108. * @exception std::runtime_error Any exceptions thrown by gl::shader_object or gl::shader_program.
  109. *
  110. * @see has_vertex_directive() const
  111. * @see has_fragment_directive() const
  112. * @see has_geometry_directive() const
  113. */
  114. [[nodiscard]] std::unique_ptr<gl::shader_program> build(const dictionary_type& definitions = {}) const;
  115. /// Returns `true` if the template source contains one or more `#pragma vertex` directive.
  116. [[nodiscard]] inline bool has_vertex_directive() const noexcept
  117. {
  118. return !m_vertex_directives.empty();
  119. }
  120. /// Returns `true` if the template source contains one or more `#pragma fragment` directive.
  121. [[nodiscard]] inline bool has_fragment_directive() const noexcept
  122. {
  123. return !m_fragment_directives.empty();
  124. }
  125. /// Returns `true` if the template source contains one or more `#pragma geometry` directive.
  126. [[nodiscard]] inline bool has_geometry_directive() const noexcept
  127. {
  128. return !m_geometry_directives.empty();
  129. }
  130. /**
  131. * Returns `true` if the template source contains one or more instance of `#pragma define <key>`.
  132. *
  133. * @param key Definition key.
  134. */
  135. [[nodiscard]] bool has_define_directive(const std::string& key) const;
  136. /// Returns a hash of the template source code.
  137. [[nodiscard]] inline constexpr std::size_t hash() const noexcept
  138. {
  139. return m_hash;
  140. }
  141. private:
  142. void find_directives();
  143. void rehash();
  144. void replace_stage_directives(gl::shader_stage stage) const;
  145. void replace_define_directives(const dictionary_type& definitions) const;
  146. mutable text_file m_template_source;
  147. std::unordered_set<std::size_t> m_vertex_directives;
  148. std::unordered_set<std::size_t> m_fragment_directives;
  149. std::unordered_set<std::size_t> m_geometry_directives;
  150. std::multimap<std::string, std::size_t> m_define_directives;
  151. std::size_t m_hash{0};
  152. std::vector<std::shared_ptr<text_file>> m_include_files;
  153. };
  154. } // namespace gl
  155. #endif // ANTKEEPER_GL_SHADER_TEMPLATE_HPP