shader_preprocessor.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /**************************************************************************/
  2. /* shader_preprocessor.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. #include "shader_preprocessor.h"
  31. #include "core/math/expression.h"
  32. const char32_t CURSOR = 0xFFFF;
  33. // Tokenizer
  34. void ShaderPreprocessor::Tokenizer::add_generated(const ShaderPreprocessor::Token &p_t) {
  35. generated.push_back(p_t);
  36. }
  37. char32_t ShaderPreprocessor::Tokenizer::next() {
  38. if (index < size) {
  39. return code[index++];
  40. }
  41. return 0;
  42. }
  43. int ShaderPreprocessor::Tokenizer::get_line() const {
  44. return line;
  45. }
  46. int ShaderPreprocessor::Tokenizer::get_index() const {
  47. return index;
  48. }
  49. void ShaderPreprocessor::Tokenizer::get_and_clear_generated(LocalVector<char32_t> *r_out) {
  50. for (uint32_t i = 0; i < generated.size(); i++) {
  51. r_out->push_back(generated[i].text);
  52. }
  53. generated.clear();
  54. }
  55. void ShaderPreprocessor::Tokenizer::backtrack(char32_t p_what) {
  56. while (index >= 0) {
  57. char32_t c = code[index];
  58. if (c == p_what) {
  59. break;
  60. }
  61. index--;
  62. }
  63. }
  64. char32_t ShaderPreprocessor::Tokenizer::peek() {
  65. if (index < size) {
  66. return code[index];
  67. }
  68. return 0;
  69. }
  70. int ShaderPreprocessor::Tokenizer::consume_line_continuations(int p_offset) {
  71. int skips = 0;
  72. for (int i = index + p_offset; i < size; i++) {
  73. char32_t c = code[i];
  74. if (c == '\\') {
  75. if (i + 1 < size && code[i + 1] == '\n') {
  76. // This line ends with "\" and "\n" continuation.
  77. add_generated(Token('\n', line));
  78. line++;
  79. skips++;
  80. i = i + 2;
  81. index = i;
  82. } else {
  83. break;
  84. }
  85. } else if (!is_whitespace(c)) {
  86. break;
  87. }
  88. }
  89. return skips;
  90. }
  91. LocalVector<ShaderPreprocessor::Token> ShaderPreprocessor::Tokenizer::advance(char32_t p_what) {
  92. LocalVector<ShaderPreprocessor::Token> tokens;
  93. while (index < size) {
  94. char32_t c = code[index++];
  95. if (c == '\\' && consume_line_continuations(-1) > 0) {
  96. continue;
  97. }
  98. if (c == '\n') {
  99. add_generated(ShaderPreprocessor::Token('\n', line));
  100. line++;
  101. }
  102. tokens.push_back(ShaderPreprocessor::Token(c, line));
  103. if (c == p_what || c == 0) {
  104. return tokens;
  105. }
  106. }
  107. return LocalVector<ShaderPreprocessor::Token>();
  108. }
  109. void ShaderPreprocessor::Tokenizer::skip_whitespace() {
  110. while (is_char_space(peek())) {
  111. next();
  112. }
  113. }
  114. bool ShaderPreprocessor::Tokenizer::consume_empty_line() {
  115. // Read until newline and return true if the content was all whitespace/empty.
  116. return tokens_to_string(advance('\n')).strip_edges().size() == 0;
  117. }
  118. String ShaderPreprocessor::Tokenizer::get_identifier(bool *r_is_cursor, bool p_started) {
  119. if (r_is_cursor != nullptr) {
  120. *r_is_cursor = false;
  121. }
  122. LocalVector<char32_t> text;
  123. while (true) {
  124. char32_t c = peek();
  125. if (c == '\\' && consume_line_continuations(0) > 0) {
  126. continue;
  127. }
  128. if (is_char_end(c) || c == '(' || c == ')' || c == ',' || c == ';') {
  129. break;
  130. }
  131. if (is_whitespace(c) && p_started) {
  132. break;
  133. }
  134. if (!is_whitespace(c)) {
  135. p_started = true;
  136. }
  137. char32_t n = next();
  138. if (n == CURSOR) {
  139. if (r_is_cursor != nullptr) {
  140. *r_is_cursor = true;
  141. }
  142. } else {
  143. if (p_started) {
  144. text.push_back(n);
  145. }
  146. }
  147. }
  148. String id = vector_to_string(text);
  149. if (!id.is_valid_ascii_identifier()) {
  150. return "";
  151. }
  152. return id;
  153. }
  154. String ShaderPreprocessor::Tokenizer::peek_identifier() {
  155. const int original = index;
  156. const int original_line = line;
  157. String id = get_identifier();
  158. index = original;
  159. line = original_line;
  160. return id;
  161. }
  162. ShaderPreprocessor::Token ShaderPreprocessor::Tokenizer::get_token() {
  163. while (index < size) {
  164. const char32_t c = code[index++];
  165. const Token t = ShaderPreprocessor::Token(c, line);
  166. switch (c) {
  167. case ' ':
  168. case '\t':
  169. skip_whitespace();
  170. return ShaderPreprocessor::Token(' ', line);
  171. case '\n':
  172. line++;
  173. return t;
  174. default:
  175. return t;
  176. }
  177. }
  178. return ShaderPreprocessor::Token(char32_t(0), line);
  179. }
  180. ShaderPreprocessor::Tokenizer::Tokenizer(const String &p_code) {
  181. code = p_code;
  182. line = 0;
  183. index = 0;
  184. size = code.size();
  185. }
  186. // ShaderPreprocessor::CommentRemover
  187. String ShaderPreprocessor::CommentRemover::get_error() const {
  188. if (comments_open != 0) {
  189. return "Block comment mismatch";
  190. }
  191. return "";
  192. }
  193. int ShaderPreprocessor::CommentRemover::get_error_line() const {
  194. if (comments_open != 0) {
  195. return comment_line_open;
  196. }
  197. return -1;
  198. }
  199. char32_t ShaderPreprocessor::CommentRemover::peek() const {
  200. if (index < code.size()) {
  201. return code[index];
  202. }
  203. return 0;
  204. }
  205. bool ShaderPreprocessor::CommentRemover::advance(char32_t p_what) {
  206. while (index < code.size()) {
  207. char32_t c = code[index++];
  208. if (c == '\n') {
  209. line++;
  210. stripped.push_back('\n');
  211. }
  212. if (c == p_what) {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. String ShaderPreprocessor::CommentRemover::strip() {
  219. stripped.clear();
  220. index = 0;
  221. line = 0;
  222. comment_line_open = 0;
  223. comments_open = 0;
  224. strings_open = 0;
  225. while (index < code.size()) {
  226. char32_t c = code[index++];
  227. if (c == CURSOR) {
  228. // Cursor. Maintain.
  229. stripped.push_back(c);
  230. } else if (c == '"') {
  231. if (strings_open <= 0) {
  232. strings_open++;
  233. } else {
  234. strings_open--;
  235. }
  236. stripped.push_back(c);
  237. } else if (c == '/' && strings_open == 0) {
  238. char32_t p = peek();
  239. if (p == '/') { // Single line comment.
  240. advance('\n');
  241. } else if (p == '*') { // Start of a block comment.
  242. index++;
  243. comment_line_open = line;
  244. comments_open++;
  245. while (advance('*')) {
  246. if (peek() == '/') { // End of a block comment.
  247. comments_open--;
  248. index++;
  249. break;
  250. }
  251. }
  252. } else {
  253. stripped.push_back(c);
  254. }
  255. } else if (c == '*' && strings_open == 0) {
  256. if (peek() == '/') { // Unmatched end of a block comment.
  257. comment_line_open = line;
  258. comments_open--;
  259. } else {
  260. stripped.push_back(c);
  261. }
  262. } else if (c == '\n') {
  263. line++;
  264. stripped.push_back(c);
  265. } else {
  266. stripped.push_back(c);
  267. }
  268. }
  269. return vector_to_string(stripped);
  270. }
  271. ShaderPreprocessor::CommentRemover::CommentRemover(const String &p_code) {
  272. code = p_code;
  273. index = 0;
  274. line = 0;
  275. comment_line_open = 0;
  276. comments_open = 0;
  277. strings_open = 0;
  278. }
  279. // ShaderPreprocessor::Token
  280. ShaderPreprocessor::Token::Token() {
  281. text = 0;
  282. line = -1;
  283. }
  284. ShaderPreprocessor::Token::Token(char32_t p_text, int p_line) {
  285. text = p_text;
  286. line = p_line;
  287. }
  288. // ShaderPreprocessor
  289. bool ShaderPreprocessor::is_char_word(char32_t p_char) {
  290. if ((p_char >= '0' && p_char <= '9') ||
  291. (p_char >= 'a' && p_char <= 'z') ||
  292. (p_char >= 'A' && p_char <= 'Z') ||
  293. p_char == '_') {
  294. return true;
  295. }
  296. return false;
  297. }
  298. bool ShaderPreprocessor::is_char_space(char32_t p_char) {
  299. return p_char == ' ' || p_char == '\t';
  300. }
  301. bool ShaderPreprocessor::is_char_end(char32_t p_char) {
  302. return p_char == '\n' || p_char == 0;
  303. }
  304. String ShaderPreprocessor::vector_to_string(const LocalVector<char32_t> &p_v, int p_start, int p_end) {
  305. const int stop = (p_end == -1) ? p_v.size() : p_end;
  306. const int count = stop - p_start;
  307. String result;
  308. result.resize(count + 1);
  309. for (int i = 0; i < count; i++) {
  310. result[i] = p_v[p_start + i];
  311. }
  312. result[count] = 0; // Ensure string is null terminated for length() to work.
  313. return result;
  314. }
  315. String ShaderPreprocessor::tokens_to_string(const LocalVector<Token> &p_tokens) {
  316. LocalVector<char32_t> result;
  317. for (const Token &token : p_tokens) {
  318. result.push_back(token.text);
  319. }
  320. return vector_to_string(result);
  321. }
  322. void ShaderPreprocessor::process_directive(Tokenizer *p_tokenizer) {
  323. bool is_cursor;
  324. String directive = p_tokenizer->get_identifier(&is_cursor, true);
  325. if (is_cursor) {
  326. state->completion_type = COMPLETION_TYPE_DIRECTIVE;
  327. }
  328. if (directive == "if") {
  329. process_if(p_tokenizer);
  330. } else if (directive == "ifdef") {
  331. process_ifdef(p_tokenizer);
  332. } else if (directive == "ifndef") {
  333. process_ifndef(p_tokenizer);
  334. } else if (directive == "elif") {
  335. process_elif(p_tokenizer);
  336. } else if (directive == "else") {
  337. process_else(p_tokenizer);
  338. } else if (directive == "endif") {
  339. process_endif(p_tokenizer);
  340. } else if (directive == "error") {
  341. process_error(p_tokenizer);
  342. } else if (directive == "define") {
  343. process_define(p_tokenizer);
  344. } else if (directive == "undef") {
  345. process_undef(p_tokenizer);
  346. } else if (directive == "include") {
  347. process_include(p_tokenizer);
  348. } else if (directive == "pragma") {
  349. process_pragma(p_tokenizer);
  350. } else {
  351. set_error(RTR("Unknown directive."), p_tokenizer->get_line());
  352. }
  353. }
  354. void ShaderPreprocessor::process_define(Tokenizer *p_tokenizer) {
  355. const int line = p_tokenizer->get_line();
  356. String label = p_tokenizer->get_identifier();
  357. if (label.is_empty()) {
  358. set_error(RTR("Invalid macro name."), line);
  359. return;
  360. }
  361. if (state->defines.has(label)) {
  362. set_error(RTR("Macro redefinition."), line);
  363. return;
  364. }
  365. Vector<String> args;
  366. if (p_tokenizer->peek() == '(') {
  367. // Macro has arguments.
  368. p_tokenizer->get_token();
  369. while (true) {
  370. String name = p_tokenizer->get_identifier();
  371. if (name.is_empty()) {
  372. set_error(RTR("Invalid argument name."), line);
  373. return;
  374. }
  375. args.push_back(name);
  376. p_tokenizer->skip_whitespace();
  377. char32_t next = p_tokenizer->get_token().text;
  378. if (next == ')') {
  379. break;
  380. } else if (next != ',') {
  381. set_error(RTR("Expected a comma in the macro argument list."), line);
  382. return;
  383. }
  384. }
  385. }
  386. String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  387. if (body.begins_with("##")) {
  388. set_error(RTR("'##' must not appear at beginning of macro expansion."), line);
  389. return;
  390. }
  391. if (body.ends_with("##")) {
  392. set_error(RTR("'##' must not appear at end of macro expansion."), line);
  393. return;
  394. }
  395. Define *define = memnew(Define);
  396. if (!args.is_empty()) {
  397. define->arguments = args;
  398. }
  399. define->body = body;
  400. state->defines[label] = define;
  401. }
  402. void ShaderPreprocessor::process_elif(Tokenizer *p_tokenizer) {
  403. const int line = p_tokenizer->get_line();
  404. if (state->current_branch == nullptr || state->current_branch->else_defined) {
  405. set_error(vformat(RTR("Unmatched '%s' directive."), "elif"), line);
  406. return;
  407. }
  408. if (state->previous_region != nullptr) {
  409. state->previous_region->to_line = line - 1;
  410. }
  411. String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  412. if (body.is_empty()) {
  413. set_error(RTR("Missing condition."), line);
  414. return;
  415. }
  416. Error error = expand_condition(body, line, body);
  417. if (error != OK) {
  418. return;
  419. }
  420. error = expand_macros(body, line, body);
  421. if (error != OK) {
  422. return;
  423. }
  424. Expression expression;
  425. Vector<String> names;
  426. error = expression.parse(body, names);
  427. if (error != OK) {
  428. set_error(expression.get_error_text(), line);
  429. return;
  430. }
  431. Variant v = expression.execute(Array(), nullptr, false);
  432. if (v.get_type() == Variant::NIL) {
  433. set_error(RTR("Condition evaluation error."), line);
  434. return;
  435. }
  436. bool skip = false;
  437. for (int i = 0; i < state->current_branch->conditions.size(); i++) {
  438. if (state->current_branch->conditions[i]) {
  439. skip = true;
  440. break;
  441. }
  442. }
  443. bool success = !skip && v.booleanize();
  444. start_branch_condition(p_tokenizer, success, true);
  445. if (state->save_regions) {
  446. add_region(line + 1, success, state->previous_region->parent);
  447. }
  448. }
  449. void ShaderPreprocessor::process_else(Tokenizer *p_tokenizer) {
  450. const int line = p_tokenizer->get_line();
  451. if (state->current_branch == nullptr || state->current_branch->else_defined) {
  452. set_error(vformat(RTR("Unmatched '%s' directive."), "else"), line);
  453. return;
  454. }
  455. if (state->previous_region != nullptr) {
  456. state->previous_region->to_line = line - 1;
  457. }
  458. if (!p_tokenizer->consume_empty_line()) {
  459. set_error(vformat(RTR("Invalid '%s' directive."), "else"), line);
  460. }
  461. bool skip = false;
  462. for (int i = 0; i < state->current_branch->conditions.size(); i++) {
  463. if (state->current_branch->conditions[i]) {
  464. skip = true;
  465. break;
  466. }
  467. }
  468. state->current_branch->else_defined = true;
  469. if (state->save_regions) {
  470. add_region(line + 1, !skip, state->previous_region->parent);
  471. }
  472. if (skip) {
  473. Vector<String> ends;
  474. ends.push_back("endif");
  475. next_directive(p_tokenizer, ends);
  476. }
  477. }
  478. void ShaderPreprocessor::process_endif(Tokenizer *p_tokenizer) {
  479. const int line = p_tokenizer->get_line();
  480. state->condition_depth--;
  481. if (state->condition_depth < 0) {
  482. set_error(vformat(RTR("Unmatched '%s' directive."), "endif"), line);
  483. return;
  484. }
  485. if (state->previous_region != nullptr) {
  486. state->previous_region->to_line = line - 1;
  487. state->previous_region = state->previous_region->parent;
  488. }
  489. if (!p_tokenizer->consume_empty_line()) {
  490. set_error(vformat(RTR("Invalid '%s' directive."), "endif"), line);
  491. }
  492. state->current_branch = state->current_branch->parent;
  493. state->branches.pop_back();
  494. }
  495. void ShaderPreprocessor::process_error(Tokenizer *p_tokenizer) {
  496. const int line = p_tokenizer->get_line();
  497. const String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  498. if (body.is_empty()) {
  499. set_error(" ", line);
  500. } else {
  501. set_error(body, line);
  502. }
  503. if (!p_tokenizer->consume_empty_line()) {
  504. set_error(vformat(RTR("Invalid '%s' directive."), "error"), line);
  505. }
  506. }
  507. void ShaderPreprocessor::process_if(Tokenizer *p_tokenizer) {
  508. const int line = p_tokenizer->get_line();
  509. String body = tokens_to_string(p_tokenizer->advance('\n')).strip_edges();
  510. if (body.is_empty()) {
  511. set_error(RTR("Missing condition."), line);
  512. return;
  513. }
  514. Error error = expand_condition(body, line, body);
  515. if (error != OK) {
  516. return;
  517. }
  518. error = expand_macros(body, line, body);
  519. if (error != OK) {
  520. return;
  521. }
  522. Expression expression;
  523. Vector<String> names;
  524. error = expression.parse(body, names);
  525. if (error != OK) {
  526. set_error(expression.get_error_text(), line);
  527. return;
  528. }
  529. Variant v = expression.execute(Array(), nullptr, false);
  530. if (v.get_type() == Variant::NIL) {
  531. set_error(RTR("Condition evaluation error."), line);
  532. return;
  533. }
  534. bool success = v.booleanize();
  535. start_branch_condition(p_tokenizer, success);
  536. if (state->save_regions) {
  537. add_region(line + 1, success, state->previous_region);
  538. }
  539. }
  540. void ShaderPreprocessor::process_ifdef(Tokenizer *p_tokenizer) {
  541. const int line = p_tokenizer->get_line();
  542. String label = p_tokenizer->get_identifier();
  543. if (label.is_empty()) {
  544. set_error(RTR("Invalid macro name."), line);
  545. return;
  546. }
  547. if (!p_tokenizer->consume_empty_line()) {
  548. set_error(vformat(RTR("Invalid '%s' directive."), "ifdef"), line);
  549. return;
  550. }
  551. bool success = state->defines.has(label);
  552. start_branch_condition(p_tokenizer, success);
  553. if (state->save_regions) {
  554. add_region(line + 1, success, state->previous_region);
  555. }
  556. }
  557. void ShaderPreprocessor::process_ifndef(Tokenizer *p_tokenizer) {
  558. const int line = p_tokenizer->get_line();
  559. String label = p_tokenizer->get_identifier();
  560. if (label.is_empty()) {
  561. set_error(RTR("Invalid macro name."), line);
  562. return;
  563. }
  564. if (!p_tokenizer->consume_empty_line()) {
  565. set_error(vformat(RTR("Invalid '%s' directive."), "ifndef"), line);
  566. return;
  567. }
  568. bool success = !state->defines.has(label);
  569. start_branch_condition(p_tokenizer, success);
  570. if (state->save_regions) {
  571. add_region(line + 1, success, state->previous_region);
  572. }
  573. }
  574. void ShaderPreprocessor::process_include(Tokenizer *p_tokenizer) {
  575. const int line = p_tokenizer->get_line();
  576. p_tokenizer->advance('"');
  577. String path = tokens_to_string(p_tokenizer->advance('"'));
  578. for (int i = 0; i < path.length(); i++) {
  579. if (path[i] == '\n') {
  580. break; //stop parsing
  581. }
  582. if (path[i] == CURSOR) {
  583. state->completion_type = COMPLETION_TYPE_INCLUDE_PATH;
  584. break;
  585. }
  586. }
  587. path = path.substr(0, path.length() - 1);
  588. if (path.is_empty() || !p_tokenizer->consume_empty_line()) {
  589. set_error(RTR("Invalid path."), line);
  590. return;
  591. }
  592. path = path.simplify_path();
  593. if (path.is_relative_path()) {
  594. path = state->current_filename.get_base_dir().path_join(path);
  595. }
  596. if (!ResourceLoader::exists(path)) {
  597. set_error(RTR("Shader include file does not exist:") + " " + path, line);
  598. return;
  599. }
  600. Ref<Resource> res = ResourceLoader::load(path);
  601. if (res.is_null()) {
  602. set_error(RTR("Shader include load failed. Does the shader include exist? Is there a cyclic dependency?"), line);
  603. return;
  604. }
  605. Ref<ShaderInclude> shader_inc = res;
  606. if (shader_inc.is_null()) {
  607. set_error(RTR("Shader include resource type is wrong."), line);
  608. return;
  609. }
  610. String included = shader_inc->get_code();
  611. if (!included.is_empty()) {
  612. uint64_t code_hash = included.hash64();
  613. if (state->cyclic_include_hashes.find(code_hash)) {
  614. set_error(RTR("Cyclic include found") + ": " + path, line);
  615. return;
  616. }
  617. }
  618. state->shader_includes.insert(shader_inc);
  619. const String real_path = shader_inc->get_path();
  620. if (state->includes.has(real_path)) {
  621. // Already included, skip.
  622. // This is a valid check because 2 separate include paths could use some
  623. // of the same shared functions from a common shader include.
  624. return;
  625. }
  626. // Mark as included.
  627. state->includes.insert(real_path);
  628. state->include_depth++;
  629. if (state->include_depth > 25) {
  630. set_error(RTR("Shader max include depth exceeded."), line);
  631. return;
  632. }
  633. String old_filename = state->current_filename;
  634. state->current_filename = real_path;
  635. ShaderPreprocessor processor;
  636. int prev_condition_depth = state->condition_depth;
  637. state->condition_depth = 0;
  638. FilePosition fp;
  639. fp.file = state->current_filename;
  640. fp.line = line + 1;
  641. state->include_positions.push_back(fp);
  642. String result;
  643. processor.preprocess(state, included, result);
  644. add_to_output("@@>" + real_path + "\n"); // Add token for enter include path
  645. add_to_output(result);
  646. add_to_output("\n@@<" + real_path + "\n"); // Add token for exit include path.
  647. // Reset to last include if there are no errors. We want to use this as context.
  648. if (state->error.is_empty()) {
  649. state->current_filename = old_filename;
  650. state->include_positions.pop_back();
  651. } else {
  652. return;
  653. }
  654. state->include_depth--;
  655. state->condition_depth = prev_condition_depth;
  656. }
  657. void ShaderPreprocessor::process_pragma(Tokenizer *p_tokenizer) {
  658. const int line = p_tokenizer->get_line();
  659. bool is_cursor;
  660. const String label = p_tokenizer->get_identifier(&is_cursor);
  661. if (is_cursor) {
  662. state->completion_type = COMPLETION_TYPE_PRAGMA;
  663. }
  664. if (label.is_empty()) {
  665. set_error(vformat(RTR("Invalid '%s' directive."), "pragma"), line);
  666. return;
  667. }
  668. // Explicitly handle pragma values here.
  669. // If more pragma options are created, then refactor into a more defined structure.
  670. if (label == "disable_preprocessor") {
  671. state->disabled = true;
  672. } else {
  673. set_error(vformat(RTR("Invalid '%s' directive."), "pragma"), line);
  674. return;
  675. }
  676. if (!p_tokenizer->consume_empty_line()) {
  677. set_error(vformat(RTR("Invalid '%s' directive."), "pragma"), line);
  678. return;
  679. }
  680. }
  681. void ShaderPreprocessor::process_undef(Tokenizer *p_tokenizer) {
  682. const int line = p_tokenizer->get_line();
  683. const String label = p_tokenizer->get_identifier();
  684. if (label.is_empty() || !p_tokenizer->consume_empty_line()) {
  685. set_error(vformat(RTR("Invalid '%s' directive."), "undef"), line);
  686. return;
  687. }
  688. if (state->defines.has(label)) {
  689. if (state->defines[label]->is_builtin) {
  690. set_error(vformat(RTR("Cannot use '%s' on built-in define."), "undef"), line);
  691. return;
  692. }
  693. memdelete(state->defines[label]);
  694. state->defines.erase(label);
  695. }
  696. }
  697. void ShaderPreprocessor::add_region(int p_line, bool p_enabled, Region *p_parent_region) {
  698. Region region;
  699. region.file = state->current_filename;
  700. region.enabled = p_enabled;
  701. region.from_line = p_line;
  702. region.parent = p_parent_region;
  703. state->previous_region = &state->regions[region.file].push_back(region)->get();
  704. }
  705. void ShaderPreprocessor::start_branch_condition(Tokenizer *p_tokenizer, bool p_success, bool p_continue) {
  706. if (!p_continue) {
  707. state->condition_depth++;
  708. state->current_branch = &state->branches.push_back(Branch(p_success, state->current_branch))->get();
  709. } else {
  710. state->current_branch->conditions.push_back(p_success);
  711. }
  712. if (!p_success) {
  713. Vector<String> ends;
  714. ends.push_back("elif");
  715. ends.push_back("else");
  716. ends.push_back("endif");
  717. next_directive(p_tokenizer, ends);
  718. }
  719. }
  720. void ShaderPreprocessor::expand_output_macros(int p_start, int p_line_number) {
  721. String line = vector_to_string(output, p_start, output.size());
  722. Error error = expand_macros(line, p_line_number - 1, line); // We are already on next line, so -1.
  723. if (error != OK) {
  724. return;
  725. }
  726. output.resize(p_start);
  727. add_to_output(line);
  728. }
  729. Error ShaderPreprocessor::expand_condition(const String &p_string, int p_line, String &r_expanded) {
  730. // Checks bracket count to be even + check the cursor position.
  731. {
  732. int bracket_start_count = 0;
  733. int bracket_end_count = 0;
  734. for (int i = 0; i < p_string.size(); i++) {
  735. switch (p_string[i]) {
  736. case CURSOR:
  737. state->completion_type = COMPLETION_TYPE_CONDITION;
  738. break;
  739. case '(':
  740. bracket_start_count++;
  741. break;
  742. case ')':
  743. bracket_end_count++;
  744. break;
  745. }
  746. }
  747. if (bracket_start_count > bracket_end_count) {
  748. _set_expected_error(")", p_line);
  749. return FAILED;
  750. }
  751. if (bracket_end_count > bracket_start_count) {
  752. _set_expected_error("(", p_line);
  753. return FAILED;
  754. }
  755. }
  756. String result = p_string;
  757. int index = 0;
  758. int index_start = 0;
  759. int index_end = 0;
  760. while (find_match(result, "defined", index, index_start)) {
  761. bool open_bracket = false;
  762. bool found_word = false;
  763. bool word_completed = false;
  764. LocalVector<char32_t> text;
  765. int post_bracket_index = -1;
  766. int size = result.size();
  767. for (int i = (index_start - 1); i < size; i++) {
  768. char32_t c = result[i];
  769. if (c == 0) {
  770. if (found_word) {
  771. word_completed = true;
  772. }
  773. break;
  774. }
  775. char32_t cs[] = { c, '\0' };
  776. String s = String(cs);
  777. bool is_space = is_char_space(c);
  778. if (word_completed) {
  779. if (c == ')') {
  780. continue;
  781. }
  782. if (c == '|' || c == '&') {
  783. if (open_bracket) {
  784. _set_unexpected_token_error(s, p_line);
  785. return FAILED;
  786. }
  787. break;
  788. } else if (!is_space) {
  789. _set_unexpected_token_error(s, p_line);
  790. return FAILED;
  791. }
  792. } else if (is_space) {
  793. if (found_word && !open_bracket) {
  794. index_end = i;
  795. word_completed = true;
  796. }
  797. } else if (c == '(') {
  798. if (open_bracket) {
  799. _set_unexpected_token_error(s, p_line);
  800. return FAILED;
  801. }
  802. open_bracket = true;
  803. } else if (c == ')') {
  804. if (open_bracket) {
  805. if (!found_word) {
  806. _set_unexpected_token_error(s, p_line);
  807. return FAILED;
  808. }
  809. open_bracket = false;
  810. post_bracket_index = i + 1;
  811. } else {
  812. index_end = i;
  813. }
  814. word_completed = true;
  815. } else if (is_char_word(c)) {
  816. text.push_back(c);
  817. found_word = true;
  818. } else {
  819. _set_unexpected_token_error(s, p_line);
  820. return FAILED;
  821. }
  822. }
  823. if (word_completed) {
  824. if (open_bracket) {
  825. _set_expected_error(")", p_line);
  826. return FAILED;
  827. }
  828. if (post_bracket_index != -1) {
  829. index_end = post_bracket_index;
  830. }
  831. String body = state->defines.has(vector_to_string(text)) ? "true" : "false";
  832. String temp = result;
  833. result = result.substr(0, index) + body;
  834. index_start = result.length();
  835. if (index_end > 0) {
  836. result += temp.substr(index_end);
  837. }
  838. } else {
  839. set_error(RTR("Invalid macro name."), p_line);
  840. return FAILED;
  841. }
  842. }
  843. r_expanded = result;
  844. return OK;
  845. }
  846. Error ShaderPreprocessor::expand_macros(const String &p_string, int p_line, String &r_expanded) {
  847. String iterative = p_string;
  848. int pass_count = 0;
  849. bool expanded = true;
  850. while (expanded) {
  851. expanded = false;
  852. // As long as we find something to expand, keep going.
  853. for (const RBMap<String, Define *>::Element *E = state->defines.front(); E; E = E->next()) {
  854. if (expand_macros_once(iterative, p_line, E, iterative)) {
  855. expanded = true;
  856. }
  857. }
  858. pass_count++;
  859. if (pass_count > 50) {
  860. set_error(RTR("Macro expansion limit exceeded."), p_line);
  861. break;
  862. }
  863. }
  864. r_expanded = iterative;
  865. if (!state->error.is_empty()) {
  866. return FAILED;
  867. }
  868. return OK;
  869. }
  870. bool ShaderPreprocessor::expand_macros_once(const String &p_line, int p_line_number, const RBMap<String, Define *>::Element *p_define_pair, String &r_expanded) {
  871. String result = p_line;
  872. const String &key = p_define_pair->key();
  873. const Define *define = p_define_pair->value();
  874. int index_start = 0;
  875. int index = 0;
  876. if (find_match(result, key, index, index_start)) {
  877. String body = define->body;
  878. if (define->arguments.size() > 0) {
  879. // Complex macro with arguments.
  880. int args_start = -1;
  881. int args_end = -1;
  882. int brackets_open = 0;
  883. Vector<String> args;
  884. for (int i = index_start - 1; i < p_line.length(); i++) {
  885. bool add_argument = false;
  886. bool reached_end = false;
  887. char32_t c = p_line[i];
  888. if (c == '(') {
  889. brackets_open++;
  890. if (brackets_open == 1) {
  891. args_start = i + 1;
  892. args_end = -1;
  893. }
  894. } else if (c == ')') {
  895. brackets_open--;
  896. if (brackets_open == 0) {
  897. args_end = i;
  898. add_argument = true;
  899. reached_end = true;
  900. }
  901. } else if (c == ',') {
  902. if (brackets_open == 1) {
  903. args_end = i;
  904. add_argument = true;
  905. }
  906. }
  907. if (add_argument) {
  908. if (args_start == -1 || args_end == -1) {
  909. set_error(RTR("Invalid macro argument list."), p_line_number);
  910. return false;
  911. }
  912. String arg = p_line.substr(args_start, args_end - args_start).strip_edges();
  913. if (arg.is_empty()) {
  914. set_error(RTR("Invalid macro argument."), p_line_number);
  915. return false;
  916. }
  917. args.append(arg);
  918. args_start = args_end + 1;
  919. }
  920. if (reached_end) {
  921. break;
  922. }
  923. }
  924. if (args.size() != define->arguments.size()) {
  925. set_error(RTR("Invalid macro argument count."), p_line_number);
  926. return false;
  927. }
  928. // Insert macro arguments into the body.
  929. for (int i = 0; i < args.size(); i++) {
  930. String arg_name = define->arguments[i];
  931. int arg_index_start = 0;
  932. int arg_index = 0;
  933. while (find_match(body, arg_name, arg_index, arg_index_start)) {
  934. body = body.substr(0, arg_index) + args[i] + body.substr(arg_index + arg_name.length(), body.length() - (arg_index + arg_name.length()));
  935. // Manually reset arg_index_start to where the arg value of the define finishes.
  936. // This ensures we don't skip the other args of this macro in the string.
  937. arg_index_start = arg_index + args[i].length() + 1;
  938. }
  939. }
  940. concatenate_macro_body(body);
  941. result = result.substr(0, index) + " " + body + " " + result.substr(args_end + 1, result.length());
  942. } else {
  943. concatenate_macro_body(body);
  944. result = result.substr(0, index) + " " + body + " " + result.substr(index + key.length(), result.length() - (index + key.length()));
  945. }
  946. r_expanded = result;
  947. return true;
  948. }
  949. return false;
  950. }
  951. bool ShaderPreprocessor::find_match(const String &p_string, const String &p_value, int &r_index, int &r_index_start) {
  952. // Looks for value in string and then determines if the boundaries
  953. // are non-word characters. This method semi-emulates \b in regex.
  954. r_index = p_string.find(p_value, r_index_start);
  955. while (r_index > -1) {
  956. if (r_index > 0) {
  957. if (is_char_word(p_string[r_index - 1])) {
  958. r_index_start = r_index + 1;
  959. r_index = p_string.find(p_value, r_index_start);
  960. continue;
  961. }
  962. }
  963. if (r_index + p_value.length() < p_string.length()) {
  964. if (is_char_word(p_string[r_index + p_value.length()])) {
  965. r_index_start = r_index + p_value.length() + 1;
  966. r_index = p_string.find(p_value, r_index_start);
  967. continue;
  968. }
  969. }
  970. // Return and shift index start automatically for next call.
  971. r_index_start = r_index + p_value.length() + 1;
  972. return true;
  973. }
  974. return false;
  975. }
  976. void ShaderPreprocessor::concatenate_macro_body(String &r_body) {
  977. int index_start = r_body.find("##");
  978. while (index_start > -1) {
  979. int index_end = index_start + 2; // First character after ##.
  980. // The macro was checked during creation so this should never happen.
  981. ERR_FAIL_INDEX(index_end, r_body.size());
  982. // If there more than two # in a row, then it's not a concatenation.
  983. bool is_concat = true;
  984. while (index_end <= r_body.length() && r_body[index_end] == '#') {
  985. index_end++;
  986. is_concat = false;
  987. }
  988. if (!is_concat) {
  989. index_start = r_body.find("##", index_end);
  990. continue;
  991. }
  992. // Skip whitespace after ##.
  993. while (index_end < r_body.length() && is_char_space(r_body[index_end])) {
  994. index_end++;
  995. }
  996. // Skip whitespace before ##.
  997. while (index_start >= 1 && is_char_space(r_body[index_start - 1])) {
  998. index_start--;
  999. }
  1000. r_body = r_body.substr(0, index_start) + r_body.substr(index_end, r_body.length() - index_end);
  1001. index_start = r_body.find("##", index_start);
  1002. }
  1003. }
  1004. String ShaderPreprocessor::next_directive(Tokenizer *p_tokenizer, const Vector<String> &p_directives) {
  1005. const int line = p_tokenizer->get_line();
  1006. int nesting = 0;
  1007. while (true) {
  1008. p_tokenizer->advance('#');
  1009. String id = p_tokenizer->peek_identifier();
  1010. if (id.is_empty()) {
  1011. break;
  1012. }
  1013. if (nesting == 0) {
  1014. for (int i = 0; i < p_directives.size(); i++) {
  1015. if (p_directives[i] == id) {
  1016. p_tokenizer->backtrack('#');
  1017. return id;
  1018. }
  1019. }
  1020. }
  1021. if (id == "ifdef" || id == "ifndef" || id == "if") {
  1022. nesting++;
  1023. } else if (id == "endif") {
  1024. nesting--;
  1025. }
  1026. }
  1027. set_error(RTR("Can't find matching branch directive."), line);
  1028. return "";
  1029. }
  1030. void ShaderPreprocessor::add_to_output(const String &p_str) {
  1031. for (int i = 0; i < p_str.length(); i++) {
  1032. output.push_back(p_str[i]);
  1033. }
  1034. }
  1035. void ShaderPreprocessor::set_error(const String &p_error, int p_line) {
  1036. if (state->error.is_empty()) {
  1037. state->error = p_error;
  1038. FilePosition fp;
  1039. fp.file = state->current_filename;
  1040. fp.line = p_line + 1;
  1041. state->include_positions.push_back(fp);
  1042. }
  1043. }
  1044. ShaderPreprocessor::Define *ShaderPreprocessor::create_define(const String &p_body) {
  1045. ShaderPreprocessor::Define *define = memnew(Define);
  1046. define->body = p_body;
  1047. return define;
  1048. }
  1049. void ShaderPreprocessor::insert_builtin_define(String p_name, String p_value, State &p_state) {
  1050. Define *define = memnew(Define);
  1051. define->is_builtin = true;
  1052. define->body = p_value;
  1053. p_state.defines[p_name] = define;
  1054. }
  1055. void ShaderPreprocessor::clear_state() {
  1056. if (state != nullptr) {
  1057. for (const RBMap<String, Define *>::Element *E = state->defines.front(); E; E = E->next()) {
  1058. memdelete(E->get());
  1059. }
  1060. state->defines.clear();
  1061. }
  1062. state = nullptr;
  1063. }
  1064. Error ShaderPreprocessor::preprocess(State *p_state, const String &p_code, String &r_result) {
  1065. output.clear();
  1066. state = p_state;
  1067. CommentRemover remover(p_code);
  1068. String stripped = remover.strip();
  1069. String error = remover.get_error();
  1070. if (!error.is_empty()) {
  1071. set_error(error, remover.get_error_line());
  1072. return FAILED;
  1073. }
  1074. // Track code hashes to prevent cyclic include.
  1075. uint64_t code_hash = p_code.hash64();
  1076. state->cyclic_include_hashes.push_back(code_hash);
  1077. Tokenizer p_tokenizer(stripped);
  1078. int last_size = 0;
  1079. bool has_symbols_before_directive = false;
  1080. while (true) {
  1081. const Token &t = p_tokenizer.get_token();
  1082. if (t.text == 0) {
  1083. break;
  1084. }
  1085. // Add autogenerated tokens if there are any.
  1086. p_tokenizer.get_and_clear_generated(&output);
  1087. if (state->disabled) {
  1088. // Preprocessor was disabled.
  1089. // Read the rest of the file into the output.
  1090. output.push_back(t.text);
  1091. continue;
  1092. }
  1093. if (t.text == '#') {
  1094. if (has_symbols_before_directive) {
  1095. set_error(RTR("Invalid symbols placed before directive."), p_tokenizer.get_line());
  1096. state->cyclic_include_hashes.erase(code_hash); // Remove this hash.
  1097. return FAILED;
  1098. }
  1099. process_directive(&p_tokenizer);
  1100. } else {
  1101. if (is_char_end(t.text)) {
  1102. expand_output_macros(last_size, p_tokenizer.get_line());
  1103. last_size = output.size();
  1104. has_symbols_before_directive = false;
  1105. } else if (!is_char_space(t.text)) {
  1106. has_symbols_before_directive = true;
  1107. }
  1108. output.push_back(t.text);
  1109. }
  1110. if (!state->error.is_empty()) {
  1111. state->cyclic_include_hashes.erase(code_hash); // Remove this hash.
  1112. return FAILED;
  1113. }
  1114. }
  1115. state->cyclic_include_hashes.erase(code_hash); // Remove this hash.
  1116. if (!state->disabled) {
  1117. if (state->condition_depth != 0) {
  1118. set_error(RTR("Unmatched conditional statement."), p_tokenizer.line);
  1119. return FAILED;
  1120. }
  1121. expand_output_macros(last_size, p_tokenizer.get_line());
  1122. }
  1123. r_result = vector_to_string(output);
  1124. return OK;
  1125. }
  1126. Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filename, String &r_result, String *r_error_text, List<FilePosition> *r_error_position, List<Region> *r_regions, HashSet<Ref<ShaderInclude>> *r_includes, List<ScriptLanguage::CodeCompletionOption> *r_completion_options, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines, IncludeCompletionFunction p_include_completion_func) {
  1127. State pp_state;
  1128. if (!p_filename.is_empty()) {
  1129. pp_state.current_filename = p_filename;
  1130. pp_state.save_regions = r_regions != nullptr;
  1131. }
  1132. // Built-in defines.
  1133. {
  1134. const String rendering_method = OS::get_singleton()->get_current_rendering_method();
  1135. if (rendering_method == "forward_plus") {
  1136. insert_builtin_define("CURRENT_RENDERER", _MKSTR(2), pp_state);
  1137. } else if (rendering_method == "mobile") {
  1138. insert_builtin_define("CURRENT_RENDERER", _MKSTR(1), pp_state);
  1139. } else { // gl_compatibility
  1140. insert_builtin_define("CURRENT_RENDERER", _MKSTR(0), pp_state);
  1141. }
  1142. insert_builtin_define("RENDERER_COMPATIBILITY", _MKSTR(0), pp_state);
  1143. insert_builtin_define("RENDERER_MOBILE", _MKSTR(1), pp_state);
  1144. insert_builtin_define("RENDERER_FORWARD_PLUS", _MKSTR(2), pp_state);
  1145. }
  1146. Error err = preprocess(&pp_state, p_code, r_result);
  1147. if (err != OK) {
  1148. if (r_error_text) {
  1149. *r_error_text = pp_state.error;
  1150. }
  1151. if (r_error_position) {
  1152. *r_error_position = pp_state.include_positions;
  1153. }
  1154. }
  1155. if (r_regions) {
  1156. *r_regions = pp_state.regions[p_filename];
  1157. }
  1158. if (r_includes) {
  1159. *r_includes = pp_state.shader_includes;
  1160. }
  1161. if (r_completion_defines) {
  1162. for (const KeyValue<String, Define *> &E : state->defines) {
  1163. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1164. r_completion_defines->push_back(option);
  1165. }
  1166. }
  1167. if (r_completion_options) {
  1168. switch (pp_state.completion_type) {
  1169. case COMPLETION_TYPE_DIRECTIVE: {
  1170. List<String> options;
  1171. get_keyword_list(&options, true, true);
  1172. for (const String &E : options) {
  1173. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1174. r_completion_options->push_back(option);
  1175. }
  1176. } break;
  1177. case COMPLETION_TYPE_PRAGMA: {
  1178. List<String> options;
  1179. ShaderPreprocessor::get_pragma_list(&options);
  1180. for (const String &E : options) {
  1181. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1182. r_completion_options->push_back(option);
  1183. }
  1184. } break;
  1185. case COMPLETION_TYPE_CONDITION: {
  1186. ScriptLanguage::CodeCompletionOption option("defined", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1187. r_completion_options->push_back(option);
  1188. } break;
  1189. case COMPLETION_TYPE_INCLUDE_PATH: {
  1190. if (p_include_completion_func && r_completion_options) {
  1191. p_include_completion_func(r_completion_options);
  1192. }
  1193. } break;
  1194. default: {
  1195. }
  1196. }
  1197. }
  1198. clear_state();
  1199. return err;
  1200. }
  1201. void ShaderPreprocessor::get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords, bool p_ignore_context_keywords) {
  1202. r_keywords->push_back("define");
  1203. if (!p_ignore_context_keywords) {
  1204. r_keywords->push_back("defined");
  1205. }
  1206. r_keywords->push_back("elif");
  1207. if (p_include_shader_keywords) {
  1208. r_keywords->push_back("else");
  1209. }
  1210. r_keywords->push_back("endif");
  1211. r_keywords->push_back("error");
  1212. if (p_include_shader_keywords) {
  1213. r_keywords->push_back("if");
  1214. }
  1215. r_keywords->push_back("ifdef");
  1216. r_keywords->push_back("ifndef");
  1217. r_keywords->push_back("include");
  1218. r_keywords->push_back("pragma");
  1219. r_keywords->push_back("undef");
  1220. }
  1221. void ShaderPreprocessor::get_pragma_list(List<String> *r_pragmas) {
  1222. r_pragmas->push_back("disable_preprocessor");
  1223. }
  1224. ShaderPreprocessor::ShaderPreprocessor() {
  1225. }
  1226. ShaderPreprocessor::~ShaderPreprocessor() {
  1227. }