forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ability to include build-in include files (precursor to custom…
… shader templates)
- Loading branch information
1 parent
9e60984
commit 3d9698a
Showing
9 changed files
with
273 additions
and
9 deletions.
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. | ||
</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. | ||
</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. | ||
</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::build_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) { | ||
build_in_includes[p_filename] = p_shader_code; | ||
} | ||
|
||
PackedStringArray ShaderIncludeDB::list_built_in_include_files() { | ||
PackedStringArray ret; | ||
|
||
for (const KeyValue<String, String> &e : build_in_includes) { | ||
ret.push_back(e.key); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
bool ShaderIncludeDB::has_built_in_include_file(const String &p_filename) { | ||
return build_in_includes.has(p_filename); | ||
} | ||
|
||
String ShaderIncludeDB::get_built_in_include_file(const String &p_filename) { | ||
const String *ptr = build_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 = String(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 = String(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,56 @@ | ||
/**************************************************************************/ | ||
/* 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" | ||
#include "core/string/ustring.h" | ||
#include "core/templates/hash_map.h" | ||
#include "core/variant/variant.h" | ||
|
||
class ShaderIncludeDB : public Object { | ||
GDCLASS(ShaderIncludeDB, Object) | ||
|
||
private: | ||
static HashMap<String, String> build_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 |