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

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