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