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

222 lines
6.4 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. #include "gl/shader-template.hpp"
  20. #include <algorithm>
  21. #include <sstream>
  22. namespace gl {
  23. shader_template::shader_template(const std::string& source_code)
  24. {
  25. source(source_code);
  26. }
  27. shader_template::shader_template()
  28. {}
  29. void shader_template::source(const std::string& source)
  30. {
  31. // Reset template
  32. template_source.clear();
  33. vertex_directives.clear();
  34. fragment_directives.clear();
  35. geometry_directives.clear();
  36. define_directives.clear();
  37. // Iterate through source line-by-line
  38. std::istringstream source_stream(source);
  39. std::string line;
  40. while (std::getline(source_stream, line))
  41. {
  42. std::string token;
  43. std::istringstream line_stream(line);
  44. // Detect `#pragma` directives
  45. if (line_stream >> token && token == "#pragma")
  46. {
  47. if (line_stream >> token)
  48. {
  49. // Map line numbers of supported directives
  50. if (token == "define")
  51. {
  52. if (line_stream >> token)
  53. define_directives.insert({token, template_source.size()});
  54. }
  55. else if (token == "vertex")
  56. vertex_directives.insert(template_source.size());
  57. else if (token == "fragment")
  58. fragment_directives.insert(template_source.size());
  59. else if (token == "geometry")
  60. geometry_directives.insert(template_source.size());
  61. }
  62. }
  63. // Append line to template source
  64. template_source.push_back(line);
  65. }
  66. }
  67. std::string shader_template::configure(gl::shader_stage stage, const dictionary_type& definitions) const
  68. {
  69. replace_stage_directives(stage);
  70. replace_define_directives(definitions);
  71. // Join vector of source lines into single string
  72. std::ostringstream stream;
  73. std::copy(template_source.begin(), template_source.end(), std::ostream_iterator<std::string>(stream, "\n"));
  74. return stream.str();
  75. }
  76. gl::shader_object* shader_template::compile(gl::shader_stage stage, const dictionary_type& definitions) const
  77. {
  78. // Generate shader object source
  79. std::string object_source = configure(stage, definitions);
  80. // Create new shader object
  81. gl::shader_object* object = new gl::shader_object(stage);
  82. // Set shader object source
  83. object->source(object_source);
  84. // Compile shader object
  85. object->compile();
  86. return object;
  87. }
  88. gl::shader_program* shader_template::build(const dictionary_type& definitions) const
  89. {
  90. gl::shader_object* vertex_object = nullptr;
  91. gl::shader_object* fragment_object = nullptr;
  92. gl::shader_object* geometry_object = nullptr;
  93. // Create shader program
  94. gl::shader_program* program = new gl::shader_program();
  95. if (has_vertex_directive())
  96. {
  97. // Compile vertex shader object and attach to shader program
  98. vertex_object = compile(gl::shader_stage::vertex, definitions);
  99. program->attach(vertex_object);
  100. }
  101. if (has_fragment_directive())
  102. {
  103. // Compile fragment shader object and attach to shader program
  104. fragment_object = compile(gl::shader_stage::fragment, definitions);
  105. program->attach(fragment_object);
  106. }
  107. if (has_geometry_directive())
  108. {
  109. // Compile fragment shader object and attach to shader program
  110. geometry_object = compile(gl::shader_stage::geometry, definitions);
  111. program->attach(geometry_object);
  112. }
  113. // Link attached shader objects into shader program
  114. program->link();
  115. if (vertex_object)
  116. {
  117. // Detach and delete vertex shader object
  118. program->detach(vertex_object);
  119. delete vertex_object;
  120. }
  121. if (fragment_object)
  122. {
  123. // Detach and delete fragment shader object
  124. program->detach(fragment_object);
  125. delete fragment_object;
  126. }
  127. if (geometry_object)
  128. {
  129. // Detach and delete geometry shader object
  130. program->detach(geometry_object);
  131. delete geometry_object;
  132. }
  133. return program;
  134. }
  135. void shader_template::replace_stage_directives(gl::shader_stage stage) const
  136. {
  137. // Determine stage directives according to the shader stage being generated
  138. const std::string vertex_directive = (stage == gl::shader_stage::vertex) ? "#define __VERTEX__" : "/* #undef __VERTEX__ */";
  139. const std::string fragment_directive = (stage == gl::shader_stage::fragment) ? "#define __FRAGMENT__" : "/* #undef __FRAGMENT__ */";
  140. const std::string geometry_directive = (stage == gl::shader_stage::geometry) ? "#define __GEOMETRY__" : "/* #undef __GEOMETRY__ */";
  141. // Handle `#pragma <stage>` directives
  142. for (std::size_t i: vertex_directives)
  143. template_source[i] = vertex_directive;
  144. for (std::size_t i: fragment_directives)
  145. template_source[i] = fragment_directive;
  146. for (std::size_t i: geometry_directives)
  147. template_source[i] = geometry_directive;
  148. }
  149. void shader_template::replace_define_directives(const dictionary_type& definitions) const
  150. {
  151. // For each `#pragma define <key>` directive
  152. for (const auto& define_directive: define_directives)
  153. {
  154. // Get a reference to the directive line
  155. std::string& line = template_source[define_directive.second];
  156. // Check if the corresponding definition was given by the configuration
  157. auto definitions_it = definitions.find(define_directive.first);
  158. if (definitions_it != definitions.end())
  159. {
  160. // Definition found, Replace `#pragma define <key>` with `#define <key>` or `#define <key> <value>`
  161. line = "#define " + define_directive.first;
  162. if (!definitions_it->second.empty())
  163. line += " " + definitions_it->second;
  164. }
  165. else
  166. {
  167. // Definition not found, replace `#pragma define <key>` with the comment `/* #undef <key> */`.
  168. line = "/* #undef " + define_directive.first + " */";
  169. }
  170. }
  171. }
  172. bool shader_template::has_vertex_directive() const
  173. {
  174. return !vertex_directives.empty();
  175. }
  176. bool shader_template::has_fragment_directive() const
  177. {
  178. return !fragment_directives.empty();
  179. }
  180. bool shader_template::has_geometry_directive() const
  181. {
  182. return !geometry_directives.empty();
  183. }
  184. bool shader_template::has_define_directive(const std::string& key) const
  185. {
  186. return (define_directives.find(key) != define_directives.end());
  187. }
  188. } // namespace gl