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

216 lines
6.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. #include "resources/resource-loader.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "resources/text-file.hpp"
  22. #include "gl/shader-stage.hpp"
  23. #include "gl/shader-object.hpp"
  24. #include "gl/shader-program.hpp"
  25. #include <sstream>
  26. #include <unordered_set>
  27. #include <iterator>
  28. /**
  29. * Tokenizes a line of shader source code.
  30. */
  31. static std::vector<std::string> tokenize(const std::string& line)
  32. {
  33. std::vector<std::string> tokens;
  34. std::string token;
  35. std::istringstream linestream(line);
  36. while (linestream >> token)
  37. tokens.push_back(token);
  38. return tokens;
  39. }
  40. /**
  41. * Handles `#pragma include` directives by loading the specified text files and inserting them in place.
  42. */
  43. static void handle_includes(text_file* source, resource_manager* resource_manager)
  44. {
  45. // For each line in the source
  46. for (std::size_t i = 0; i < source->size(); ++i)
  47. {
  48. // Tokenize line
  49. std::vector<std::string> tokens = tokenize((*source)[i]);
  50. // Look for `#pragma include` directives
  51. if (tokens.size() == 3 && tokens[0] == "#pragma" && tokens[1] == "include")
  52. {
  53. // Get path to include file
  54. std::string path = tokens[2].substr(1, tokens[2].length() - 2);
  55. // Load include file
  56. if (!resource_manager->load<text_file>(path))
  57. {
  58. std::string message = std::string("Failed to load shader include file \"") + path + std::string("\"");
  59. throw std::runtime_error(message.c_str());
  60. }
  61. text_file include_file = *(resource_manager->load<text_file>(path));
  62. // Handle `#pragma include` directives inside include file
  63. handle_includes(&include_file, resource_manager);
  64. // Replace #pragma include directive with include file contents
  65. source->erase(source->begin() + i);
  66. source->insert(source->begin() + i, include_file.begin(), include_file.end());
  67. i += include_file.size() - 1;
  68. }
  69. }
  70. }
  71. /**
  72. * Injects the `DEBUG` or `NDEBUG` macros after the `#version` directive.
  73. */
  74. static void inject_debug_macro(text_file* source)
  75. {
  76. // For each line in the source
  77. for (std::size_t i = 0; i < source->size(); ++i)
  78. {
  79. // Tokenize line
  80. std::vector<std::string> tokens = tokenize((*source)[i]);
  81. // Inject DEBUG and NDEBUG macros
  82. if (!tokens.empty() && tokens[0] == "#version")
  83. {
  84. #if defined(NDEBUG)
  85. source->insert(source->begin() + i + 1, "#define NDEBUG");
  86. #else
  87. source->insert(source->begin() + i + 1, "#define DEBUG");
  88. #endif
  89. return;
  90. }
  91. }
  92. }
  93. /**
  94. * Injects a shader type macro definition after the `#version` directive.
  95. */
  96. static void inject_shader_stage_macro(text_file* source, gl::shader_stage stage)
  97. {
  98. const char* vertex_macro = "#define _VERTEX";
  99. const char* fragment_macro = "#define _FRAGMENT";
  100. const char* geometry_macro = "#define _GEOMETRY";
  101. const char* macro = nullptr;
  102. if (stage == gl::shader_stage::vertex)
  103. macro = vertex_macro;
  104. else if (stage == gl::shader_stage::fragment)
  105. macro = fragment_macro;
  106. else if (stage == gl::shader_stage::geometry)
  107. macro = geometry_macro;
  108. // For each line in the source
  109. for (std::size_t i = 0; i < source->size(); ++i)
  110. {
  111. // Tokenize line
  112. std::vector<std::string> tokens = tokenize((*source)[i]);
  113. // Inject shader stage macro
  114. if (!tokens.empty() && tokens[0] == "#version")
  115. {
  116. source->insert(source->begin() + i + 1, macro);
  117. return;
  118. }
  119. }
  120. }
  121. static std::unordered_set<gl::shader_stage> detect_shader_stages(text_file* source)
  122. {
  123. std::unordered_set<gl::shader_stage> types;
  124. // For each line in the source
  125. for (std::size_t i = 0; i < source->size(); ++i)
  126. {
  127. // Tokenize line
  128. std::vector<std::string> tokens = tokenize((*source)[i]);
  129. // Look for `#pragma include` directives
  130. if (tokens.size() == 2 && tokens[0] == "#pragma")
  131. {
  132. if (tokens[1] == "vertex")
  133. {
  134. types.insert(gl::shader_stage::vertex);
  135. }
  136. else if (tokens[1] == "fragment")
  137. {
  138. types.insert(gl::shader_stage::fragment);
  139. }
  140. else if (tokens[1] == "geometry")
  141. {
  142. types.insert(gl::shader_stage::geometry);
  143. }
  144. }
  145. }
  146. return types;
  147. }
  148. static std::string generate_source_buffer(const std::vector<std::string>& source)
  149. {
  150. std::ostringstream stream;
  151. std::copy(source.begin(), source.end(), std::ostream_iterator<std::string>(stream, "\n"));
  152. return stream.str();
  153. }
  154. template <>
  155. gl::shader_program* resource_loader<gl::shader_program>::load(resource_manager* resource_manager, PHYSFS_File* file)
  156. {
  157. // Load shader source
  158. text_file* source = resource_loader<text_file>::load(resource_manager, file);
  159. // Handle `#pragma include` directives
  160. handle_includes(source, resource_manager);
  161. // Inject `DEBUG` or `NDEBUG` macro definitions
  162. inject_debug_macro(source);
  163. // Detect declared shader types via the `#pragma vertex`, `#pragma fragment` and `#pragma geometry` directives
  164. std::unordered_set<gl::shader_stage> shade_stages = detect_shader_stages(source);
  165. // Load detected shaders
  166. std::list<gl::shader_object*> shaders;
  167. for (gl::shader_stage stage: shade_stages)
  168. {
  169. text_file stage_source = *source;
  170. inject_shader_stage_macro(&stage_source, stage);
  171. std::string source_buffer = generate_source_buffer(stage_source);
  172. gl::shader_object* object = new gl::shader_object(stage);
  173. object->source(source_buffer.c_str(), source_buffer.length());
  174. object->compile();
  175. shaders.push_back(object);
  176. }
  177. // Create shader program
  178. gl::shader_program* program = new gl::shader_program(shaders);
  179. // Delete shaders objects
  180. for (gl::shader_object* shader: shaders)
  181. {
  182. delete shader;
  183. }
  184. return program;
  185. }