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

136 lines
5.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_SHADER_TEMPLATE_HPP
  20. #define ANTKEEPER_SHADER_TEMPLATE_HPP
  21. #include "gl/shader-object.hpp"
  22. #include "gl/shader-program.hpp"
  23. #include <map>
  24. #include <string>
  25. #include <unordered_set>
  26. #include <vector>
  27. namespace gl {
  28. /**
  29. * Shader templates can be used to generate multiple shader variants from a single source.
  30. *
  31. * Shader templates support the following preprocessor directives:
  32. *
  33. * * `#pragma vertex`: Replaced with `#define __VERTEX__` when generating vertex shader objects.
  34. * * `#pragma fragment`: Replaced with `#define __FRAGMENT__` when generating fragment shader objects.
  35. * * `#pragma geometry`: Replaced with `#define __GEOMETRY__` when generating geometry shader objects.
  36. * * `#pragma define <key> <value>`: Will be replaced with `#define <key> <value>` if its definition is passed to the shader template.
  37. *
  38. * @see gl::shader_stage
  39. * @see gl::shader_object
  40. * @see gl::shader_program
  41. */
  42. class shader_template
  43. {
  44. public:
  45. /// Container of definitions used to replace `#pragma define <key> <value>` directives.
  46. typedef std::unordered_map<std::string, std::string> dictionary_type;
  47. /**
  48. * Creates a shader template and sets its source code.
  49. *
  50. * @param source_code String containing the shader template source code.
  51. *
  52. * @see shader_template::source(const std::string&)
  53. */
  54. shader_template(const std::string& source_code);
  55. /**
  56. * Creates a shader template.
  57. */
  58. shader_template();
  59. /**
  60. * Replaces the source code of the shader template.
  61. *
  62. * @param source_code String containing shader template source code.
  63. */
  64. void source(const std::string& source_code);
  65. /**
  66. * Configures shader object source code given a shader stage and template dictionary.
  67. *
  68. * @param stage Shader stage of the shader object to generate. Instances of `#pragma <stage>` in the template source will be replaced with `#define __<STAGE>__`.
  69. * @param definitions Container of definitions used to replace `#pragma define <key> <value>` directives.
  70. * @return Configured shader object source code.
  71. */
  72. std::string configure(gl::shader_stage stage, const dictionary_type& definitions) const;
  73. /**
  74. * Configures and compiles a shader object.
  75. *
  76. * @param stage Shader stage of the shader object to generate. Instances of `#pragma <stage>` in the template source will be replaced with `#define __<STAGE>__`.
  77. * @param definitions Container of definitions used to replace `#pragma define <key> <value>` directives.
  78. * @return Compiled shader object.
  79. *
  80. * @exception std::runtime_error Any exceptions thrown by gl::shader_object.
  81. */
  82. gl::shader_object* compile(gl::shader_stage stage, const dictionary_type& definitions) const;
  83. /**
  84. * 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.
  85. *
  86. * @param definitions Container of definitions used to replace `#pragma define <key> <value>` directives.
  87. * @return Linked shader program.
  88. *
  89. * @exception std::runtime_error Any exceptions thrown by gl::shader_object or gl::shader_program.
  90. *
  91. * @see has_vertex_directive() const
  92. * @see has_fragment_directive() const
  93. * @see has_geometry_directive() const
  94. */
  95. gl::shader_program* build(const dictionary_type& definitions) const;
  96. /// Returns `true` if the template source contains one or more `#pragma vertex` directive.
  97. bool has_vertex_directive() const;
  98. /// Returns `true` if the template source contains one or more `#pragma fragment` directive.
  99. bool has_fragment_directive() const;
  100. /// Returns `true` if the template source contains one or more `#pragma geometry` directive.
  101. bool has_geometry_directive() const;
  102. /**
  103. * Returns `true` if the template source contains one or more instance of `#pragma define <key>`.
  104. *
  105. * @param key Definition key.
  106. */
  107. bool has_define_directive(const std::string& key) const;
  108. private:
  109. void replace_stage_directives(gl::shader_stage stage) const;
  110. void replace_define_directives(const dictionary_type& definitions) const;
  111. mutable std::vector<std::string> template_source;
  112. std::unordered_set<std::size_t> vertex_directives;
  113. std::unordered_set<std::size_t> fragment_directives;
  114. std::unordered_set<std::size_t> geometry_directives;
  115. std::multimap<std::string, std::size_t> define_directives;
  116. };
  117. } // namespace gl
  118. #endif // ANTKEEPER_SHADER_TEMPLATE_HPP