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

107 lines
3.5 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 "renderer/shader-template.hpp"
  23. #include "gl/shader-object.hpp"
  24. #include "gl/shader-program.hpp"
  25. #include <sstream>
  26. /**
  27. * Handles `#pragma include` directives by loading the specified text files and inserting them in place.
  28. */
  29. static void handle_includes(text_file* source, resource_manager* resource_manager)
  30. {
  31. // For each line in the source
  32. for (std::size_t i = 0; i < source->size(); ++i)
  33. {
  34. std::string token;
  35. std::istringstream line_stream((*source)[i]);
  36. // If line contains a `#pragma include` directive
  37. if (line_stream >> token && token == "#pragma" &&
  38. line_stream >> token && token == "include")
  39. {
  40. // If third token is enclosed in quotes or angled brackets
  41. if (line_stream >> token && token.size() > 2 &&
  42. ((token.front() == '\"' && token.back() == '\"') ||
  43. (token.front() == '<' && token.back() == '>')))
  44. {
  45. // Extract include path
  46. std::string path = token.substr(1, token.length() - 2);
  47. // Load include file
  48. if (!resource_manager->load<text_file>(path))
  49. {
  50. (*source)[i] = "#error file not found (" + path + ")";
  51. }
  52. else
  53. {
  54. // Create copy of include file
  55. text_file include_file = *(resource_manager->load<text_file>(path));
  56. // Handle `#pragma include` directives inside include file
  57. handle_includes(&include_file, resource_manager);
  58. // Replace #pragma include directive with include file contents
  59. source->erase(source->begin() + i);
  60. source->insert(source->begin() + i, include_file.begin(), include_file.end());
  61. i += include_file.size() - 1;
  62. }
  63. }
  64. else
  65. {
  66. (*source)[i] = "#error malformed include directive (" + (*source)[i] + ")";
  67. }
  68. }
  69. }
  70. }
  71. template <>
  72. gl::shader_program* resource_loader<gl::shader_program>::load(resource_manager* resource_manager, PHYSFS_File* file)
  73. {
  74. // Load shader template source
  75. text_file source_lines = *resource_loader<text_file>::load(resource_manager, file);
  76. // Handle `#pragma include` directives
  77. handle_includes(&source_lines, resource_manager);
  78. // Join vector of source lines into single string
  79. std::ostringstream stream;
  80. std::copy(source_lines.begin(), source_lines.end(), std::ostream_iterator<std::string>(stream, "\n"));
  81. // Create shader template
  82. shader_template* shader = new shader_template(stream.str());
  83. // Build shader program
  84. gl::shader_program* program = shader->build(shader_template::dictionary_type());
  85. // Check if shader program was linked successfully
  86. if (!program->was_linked())
  87. {
  88. throw std::runtime_error("Shader program linking failed: " + program->get_info_log());
  89. }
  90. // Destroy shader template
  91. delete shader;
  92. return program;
  93. }