-
-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to include built-in include files #94193
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||
<?xml version="1.0" encoding="UTF-8" ?> | ||||||
<class name="ShaderIncludeDB" inherits="Object" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> | ||||||
<brief_description> | ||||||
Internal database of built in shader include files. | ||||||
</brief_description> | ||||||
<description> | ||||||
This object contains shader fragments from Godot's internal shaders. These can be used when access to internal uniform buffers and/or internal functions is required for instance when composing compositor effects or compute shaders. Only fragments for the current rendering device are loaded. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
</description> | ||||||
<tutorials> | ||||||
</tutorials> | ||||||
<methods> | ||||||
<method name="get_built_in_include_file" qualifiers="static"> | ||||||
<return type="String" /> | ||||||
<param index="0" name="filename" type="String" /> | ||||||
<description> | ||||||
Returns the code for the built-in shader fragment. You can also access this in your shader code through [code]#include "filename"[/code]. | ||||||
</description> | ||||||
</method> | ||||||
<method name="has_built_in_include_file" qualifiers="static"> | ||||||
<return type="bool" /> | ||||||
<param index="0" name="filename" type="String" /> | ||||||
<description> | ||||||
Returns [code]true[/code] if an include file with this name exists. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
</description> | ||||||
</method> | ||||||
<method name="list_built_in_include_files" qualifiers="static"> | ||||||
<return type="PackedStringArray" /> | ||||||
<description> | ||||||
Returns a list of built-in include files that are currently registered. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
</description> | ||||||
</method> | ||||||
</methods> | ||||||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/**************************************************************************/ | ||
/* shader_include_db.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "shader_include_db.h" | ||
|
||
HashMap<String, String> ShaderIncludeDB::built_in_includes; | ||
|
||
void ShaderIncludeDB::_bind_methods() { | ||
ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("list_built_in_include_files"), &ShaderIncludeDB::list_built_in_include_files); | ||
ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("has_built_in_include_file", "filename"), &ShaderIncludeDB::has_built_in_include_file); | ||
ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("get_built_in_include_file", "filename"), &ShaderIncludeDB::get_built_in_include_file); | ||
} | ||
|
||
void ShaderIncludeDB::register_built_in_include_file(const String &p_filename, const String &p_shader_code) { | ||
built_in_includes[p_filename] = p_shader_code; | ||
} | ||
|
||
PackedStringArray ShaderIncludeDB::list_built_in_include_files() { | ||
PackedStringArray ret; | ||
|
||
for (const KeyValue<String, String> &e : built_in_includes) { | ||
ret.push_back(e.key); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
bool ShaderIncludeDB::has_built_in_include_file(const String &p_filename) { | ||
return built_in_includes.has(p_filename); | ||
} | ||
|
||
String ShaderIncludeDB::get_built_in_include_file(const String &p_filename) { | ||
const String *ptr = built_in_includes.getptr(p_filename); | ||
|
||
return ptr ? *ptr : String(); | ||
} | ||
|
||
String ShaderIncludeDB::parse_include_files(const String &p_code) { | ||
// Prevent needless processing if we don't have any includes. | ||
if (p_code.find("#include ") == -1) { | ||
return p_code; | ||
} | ||
|
||
const String include = "#include "; | ||
String parsed_code; | ||
|
||
Vector<String> lines = p_code.split("\n"); | ||
int line_count = lines.size(); | ||
for (int i = 0; i < line_count; i++) { | ||
const String &l = lines[i]; | ||
|
||
if (l.begins_with(include)) { | ||
String include_file = l.replace(include, "").strip_edges(); | ||
if (include_file[0] == '"') { | ||
int end_pos = include_file.find_char('"', 1); | ||
if (end_pos >= 0) { | ||
include_file = include_file.substr(1, end_pos - 1); | ||
|
||
String include_code = ShaderIncludeDB::get_built_in_include_file(include_file); | ||
if (!include_code.is_empty()) { | ||
// Add these lines into our parse list so we parse them as well. | ||
Vector<String> include_lines = include_code.split("\n"); | ||
|
||
for (int j = include_lines.size() - 1; j >= 0; j--) { | ||
lines.insert(i + 1, include_lines[j]); | ||
} | ||
|
||
line_count = lines.size(); | ||
} else { | ||
// Just add it back in, this will cause a compile error to alert the user. | ||
parsed_code += l + "\n"; | ||
} | ||
} else { | ||
// Include as is. | ||
parsed_code += l + "\n"; | ||
} | ||
} else { | ||
// Include as is. | ||
parsed_code += l + "\n"; | ||
} | ||
} else { | ||
// Include as is. | ||
parsed_code += l + "\n"; | ||
} | ||
} | ||
|
||
return parsed_code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/**************************************************************************/ | ||
/* shader_include_db.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef SHADER_INCLUDE_DB_H | ||
#define SHADER_INCLUDE_DB_H | ||
|
||
#include "core/object/class_db.h" | ||
|
||
class ShaderIncludeDB : public Object { | ||
GDCLASS(ShaderIncludeDB, Object) | ||
|
||
private: | ||
static HashMap<String, String> built_in_includes; | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
public: | ||
static void register_built_in_include_file(const String &p_filename, const String &p_shader_code); | ||
static PackedStringArray list_built_in_include_files(); | ||
static bool has_built_in_include_file(const String &p_filename); | ||
static String get_built_in_include_file(const String &p_filename); | ||
static String parse_include_files(const String &p_code); | ||
}; | ||
|
||
#endif // SHADER_INCLUDE_DB_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.