Debian Codegraph @

servers/rendering/shader_include_db.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* shader_include_db.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "shader_include_db.h"
32
33 HashMap<String, String> ShaderIncludeDB::built_in_includes;
34
35 void ShaderIncludeDB::_bind_methods() {
36 ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("list_built_in_include_files"), &ShaderIncludeDB::list_built_in_include_files);
37 ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("has_built_in_include_file", "filename"), &ShaderIncludeDB::has_built_in_include_file);
38 ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("get_built_in_include_file", "filename"), &ShaderIncludeDB::get_built_in_include_file);
39 }
40
41 void ShaderIncludeDB::register_built_in_include_file(const String &p_filename, const String &p_shader_code) {
42 built_in_includes[p_filename] = p_shader_code;
43 }
44
45 PackedStringArray ShaderIncludeDB::list_built_in_include_files() {
46 PackedStringArray ret;
47
48 for (const KeyValue<String, String> &e : built_in_includes) {
49 ret.push_back(e.key);
50 }
51
52 return ret;
53 }
54
55 bool ShaderIncludeDB::has_built_in_include_file(const String &p_filename) {
56 return built_in_includes.has(p_filename);
57 }
58
59 String ShaderIncludeDB::get_built_in_include_file(const String &p_filename) {
60 const String *ptr = built_in_includes.getptr(p_filename);
61
62 return ptr ? *ptr : String();
63 }
64
65 String ShaderIncludeDB::parse_include_files(const String &p_code) {
66 // Prevent needless processing if we don't have any includes.
67 if (p_code.find("#include ") == -1) {
68 return p_code;
69 }
70
71 const String include = "#include ";
72 String parsed_code;
73
74 Vector<String> lines = p_code.split("\n");
75 int line_count = lines.size();
76 for (int i = 0; i < line_count; i++) {
77 const String &l = lines[i];
78
79 if (l.begins_with(include)) {
80 String include_file = l.replace(include, "").strip_edges();
81 if (include_file[0] == '"') {
82 int end_pos = include_file.find_char('"', 1);
83 if (end_pos >= 0) {
84 include_file = include_file.substr(1, end_pos - 1);
85
86 String include_code = ShaderIncludeDB::get_built_in_include_file(include_file);
87 if (!include_code.is_empty()) {
88 // Add these lines into our parse list so we parse them as well.
89 Vector<String> include_lines = include_code.split("\n");
90
91 for (int j = include_lines.size() - 1; j >= 0; j--) {
92 lines.insert(i + 1, include_lines[j]);
93 }
94
95 line_count = lines.size();
96 } else {
97 // Just add it back in, this will cause a compile error to alert the user.
98 parsed_code += l + "\n";
99 }
100 } else {
101 // Include as is.
102 parsed_code += l + "\n";
103 }
104 } else {
105 // Include as is.
106 parsed_code += l + "\n";
107 }
108 } else {
109 // Include as is.
110 parsed_code += l + "\n";
111 }
112 }
113
114 return parsed_code;
115 }