Editor: Restructure editor code

Moving various editor files into sub folders to reduce clutter
This commit is contained in:
A Thousand Ships
2025-06-10 16:47:26 +02:00
parent 3954b2459d
commit f11aff3841
601 changed files with 1195 additions and 1019 deletions

View File

@@ -33,7 +33,7 @@
#include "core/crypto/crypto_core.h"
#include "core/io/dir_access.h"
#include "core/io/plist.h"
#include "editor/editor_paths.h"
#include "editor/file_system/editor_paths.h"
#include "lipo.h"
#include "macho.h"

View File

@@ -0,0 +1,139 @@
/**************************************************************************/
/* dedicated_server_export_plugin.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 "dedicated_server_export_plugin.h"
EditorExportPreset::FileExportMode DedicatedServerExportPlugin::_get_export_mode_for_path(const String &p_path) {
Ref<EditorExportPreset> preset = get_export_preset();
ERR_FAIL_COND_V(preset.is_null(), EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED);
EditorExportPreset::FileExportMode mode = preset->get_file_export_mode(p_path);
if (mode != EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED) {
return mode;
}
String path = p_path;
if (path.begins_with("res://")) {
path = path.substr(6);
}
Vector<String> parts = path.split("/");
while (parts.size() > 0) {
parts.resize(parts.size() - 1);
String test_path = "res://";
if (parts.size() > 0) {
test_path += String("/").join(parts) + "/";
}
mode = preset->get_file_export_mode(test_path);
if (mode != EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED) {
break;
}
}
return mode;
}
PackedStringArray DedicatedServerExportPlugin::_get_export_features(const Ref<EditorExportPlatform> &p_platform, bool p_debug) const {
PackedStringArray ret;
Ref<EditorExportPreset> preset = get_export_preset();
ERR_FAIL_COND_V(preset.is_null(), ret);
if (preset->is_dedicated_server()) {
ret.append("dedicated_server");
}
return ret;
}
uint64_t DedicatedServerExportPlugin::_get_customization_configuration_hash() const {
Ref<EditorExportPreset> preset = get_export_preset();
ERR_FAIL_COND_V(preset.is_null(), 0);
if (preset->get_export_filter() != EditorExportPreset::EXPORT_CUSTOMIZED) {
return 0;
}
return preset->get_customized_files().hash();
}
bool DedicatedServerExportPlugin::_begin_customize_scenes(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) {
Ref<EditorExportPreset> preset = get_export_preset();
ERR_FAIL_COND_V(preset.is_null(), false);
current_export_mode = EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED;
return preset->get_export_filter() == EditorExportPreset::EXPORT_CUSTOMIZED;
}
bool DedicatedServerExportPlugin::_begin_customize_resources(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) {
Ref<EditorExportPreset> preset = get_export_preset();
ERR_FAIL_COND_V(preset.is_null(), false);
current_export_mode = EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED;
return preset->get_export_filter() == EditorExportPreset::EXPORT_CUSTOMIZED;
}
Node *DedicatedServerExportPlugin::_customize_scene(Node *p_root, const String &p_path) {
// Simply set the export mode based on the scene path. All the real
// customization happens in _customize_resource().
current_export_mode = _get_export_mode_for_path(p_path);
return nullptr;
}
Ref<Resource> DedicatedServerExportPlugin::_customize_resource(const Ref<Resource> &p_resource, const String &p_path) {
// If the resource has a path, we use that to get our export mode. But if it
// doesn't, we assume that this resource is embedded in the last resource with
// a path.
if (p_path != "") {
current_export_mode = _get_export_mode_for_path(p_path);
}
if (p_resource.is_valid() && current_export_mode == EditorExportPreset::MODE_FILE_STRIP && p_resource->has_method("create_placeholder")) {
Callable::CallError err;
Ref<Resource> result = p_resource->callp("create_placeholder", nullptr, 0, err);
if (err.error == Callable::CallError::CALL_OK) {
return result;
}
}
return Ref<Resource>();
}
void DedicatedServerExportPlugin::_end_customize_scenes() {
current_export_mode = EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED;
}
void DedicatedServerExportPlugin::_end_customize_resources() {
current_export_mode = EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED;
}

View File

@@ -0,0 +1,55 @@
/**************************************************************************/
/* dedicated_server_export_plugin.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. */
/**************************************************************************/
#pragma once
#include "editor/export/editor_export_plugin.h"
class DedicatedServerExportPlugin : public EditorExportPlugin {
private:
EditorExportPreset::FileExportMode current_export_mode;
EditorExportPreset::FileExportMode _get_export_mode_for_path(const String &p_path);
protected:
String get_name() const override { return "DedicatedServer"; }
PackedStringArray _get_export_features(const Ref<EditorExportPlatform> &p_platform, bool p_debug) const override;
uint64_t _get_customization_configuration_hash() const override;
bool _begin_customize_scenes(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) override;
bool _begin_customize_resources(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) override;
Node *_customize_scene(Node *p_root, const String &p_path) override;
Ref<Resource> _customize_resource(const Ref<Resource> &p_resource, const String &p_path) override;
void _end_customize_scenes() override;
void _end_customize_resources() override;
};

View File

@@ -32,7 +32,7 @@
#include "core/config/project_settings.h"
#include "core/io/config_file.h"
#include "editor/editor_settings.h"
#include "editor/settings/editor_settings.h"
EditorExport *EditorExport::singleton = nullptr;

View File

@@ -42,13 +42,13 @@
#include "core/io/zip_io.h"
#include "core/math/random_pcg.h"
#include "core/version.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
#include "editor/editor_paths.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/export/editor_export.h"
#include "editor/plugins/script_editor_plugin.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/file_system/editor_paths.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "editor_export_plugin.h"
#include "scene/resources/image_texture.h"

View File

@@ -34,13 +34,13 @@
#include "core/io/plist.h"
#include "core/string/translation.h"
#include "editor/editor_node.h"
#include "editor/editor_paths.h"
#include "editor/editor_string_names.h"
#include "editor/export/editor_export.h"
#include "editor/export/lipo.h"
#include "editor/export/macho.h"
#include "editor/file_system/editor_paths.h"
#include "editor/import/resource_importer_texture_settings.h"
#include "editor/plugins/script_editor_plugin.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/themes/editor_scale.h"
#include "main/main.h"

View File

@@ -40,8 +40,8 @@
#include "core/io/zip_io.h"
#include "core/os/os.h"
#include "core/templates/safe_refcount.h"
#include "editor/editor_settings.h"
#include "editor/export/editor_export_platform.h"
#include "editor/settings/editor_settings.h"
#include "main/splash.gen.h"
#include "scene/resources/image_texture.h"

View File

@@ -34,13 +34,13 @@
#include "core/io/json.h"
#include "core/io/zip_io.h"
#include "core/version.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
#include "editor/editor_paths.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/export/editor_export_preset.h"
#include "editor/progress_dialog.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/file_system/editor_paths.h"
#include "editor/gui/progress_dialog.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/file_dialog.h"
#include "scene/gui/line_edit.h"

View File

@@ -0,0 +1,169 @@
/**************************************************************************/
/* gdextension_export_plugin.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. */
/**************************************************************************/
#pragma once
#include "core/extension/gdextension_library_loader.h"
#include "editor/export/editor_export.h"
class GDExtensionExportPlugin : public EditorExportPlugin {
protected:
virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features);
virtual String get_name() const { return "GDExtension"; }
};
void GDExtensionExportPlugin::_export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) {
if (p_type != "GDExtension") {
return;
}
Ref<ConfigFile> config;
config.instantiate();
Error err = config->load(p_path);
ERR_FAIL_COND_MSG(err, "Failed to load GDExtension file: " + p_path);
// Check whether this GDExtension should be exported.
bool android_aar_plugin = config->get_value("configuration", "android_aar_plugin", false);
if (android_aar_plugin && p_features.has("android")) {
// The gdextension configuration and Android .so files will be provided by the Android aar
// plugin it's part of, so we abort here.
skip();
return;
}
ERR_FAIL_COND_MSG(!config->has_section_key("configuration", "entry_symbol"), "Failed to export GDExtension file, missing entry symbol: " + p_path);
String entry_symbol = config->get_value("configuration", "entry_symbol");
HashSet<String> all_archs;
all_archs.insert("x86_32");
all_archs.insert("x86_64");
all_archs.insert("arm32");
all_archs.insert("arm64");
all_archs.insert("rv64");
all_archs.insert("ppc64");
all_archs.insert("wasm32");
all_archs.insert("loongarch64");
all_archs.insert("universal");
HashSet<String> archs;
HashSet<String> features_wo_arch;
Vector<String> features_vector;
for (const String &tag : p_features) {
if (all_archs.has(tag)) {
archs.insert(tag);
} else {
features_wo_arch.insert(tag);
}
features_vector.append(tag);
}
if (archs.is_empty()) {
archs.insert("unknown_arch"); // Not archs specified, still try to match.
}
HashSet<String> libs_added;
struct FoundLibInfo {
int count = 0;
Vector<String> libs;
};
HashMap<String, FoundLibInfo> libs_found;
for (const String &arch_tag : archs) {
if (arch_tag != "universal") {
libs_found[arch_tag] = FoundLibInfo();
}
}
for (const String &arch_tag : archs) {
PackedStringArray tags;
String library_path = GDExtensionLibraryLoader::find_extension_library(
p_path, config, [features_wo_arch, arch_tag](const String &p_feature) { return features_wo_arch.has(p_feature) || (p_feature == arch_tag); }, &tags);
if (libs_added.has(library_path)) {
continue; // Universal library, already added for another arch, do not duplicate.
}
if (!library_path.is_empty()) {
libs_added.insert(library_path);
add_shared_object(library_path, tags);
if (p_features.has("apple_embedded") && (library_path.ends_with(".a") || library_path.ends_with(".xcframework"))) {
String additional_code = "extern void register_dynamic_symbol(char *name, void *address);\n"
"extern void add_apple_embedded_platform_init_callback(void (*cb)());\n"
"\n"
"extern \"C\" void $ENTRY();\n"
"void $ENTRY_init() {\n"
" if (&$ENTRY) register_dynamic_symbol((char *)\"$ENTRY\", (void *)$ENTRY);\n"
"}\n"
"struct $ENTRY_struct {\n"
" $ENTRY_struct() {\n"
" add_apple_embedded_platform_init_callback($ENTRY_init);\n"
" }\n"
"};\n"
"$ENTRY_struct $ENTRY_struct_instance;\n\n";
additional_code = additional_code.replace("$ENTRY", entry_symbol);
add_apple_embedded_platform_cpp_code(additional_code);
String linker_flags = "-Wl,-U,_" + entry_symbol;
add_apple_embedded_platform_linker_flags(linker_flags);
}
// Update found library info.
if (arch_tag == "universal") {
for (const String &sub_arch_tag : archs) {
if (sub_arch_tag != "universal") {
libs_found[sub_arch_tag].count++;
libs_found[sub_arch_tag].libs.push_back(library_path);
}
}
} else {
libs_found[arch_tag].count++;
libs_found[arch_tag].libs.push_back(library_path);
}
}
Vector<SharedObject> dependencies_shared_objects = GDExtensionLibraryLoader::find_extension_dependencies(p_path, config, [p_features](String p_feature) { return p_features.has(p_feature); });
for (const SharedObject &shared_object : dependencies_shared_objects) {
_add_shared_object(shared_object);
}
}
for (const KeyValue<String, FoundLibInfo> &E : libs_found) {
if (E.value.count == 0) {
if (get_export_platform().is_valid()) {
get_export_platform()->add_message(EditorExportPlatform::EXPORT_MESSAGE_WARNING, TTR("GDExtension"), vformat(TTR("No \"%s\" library found for GDExtension: \"%s\". Possible feature flags for your platform: %s"), E.key, p_path, String(", ").join(features_vector)));
}
} else if (E.value.count > 1) {
if (get_export_platform().is_valid()) {
get_export_platform()->add_message(EditorExportPlatform::EXPORT_MESSAGE_WARNING, TTR("GDExtension"), vformat(TTR("Multiple \"%s\" libraries found for GDExtension: \"%s\": \"%s\"."), E.key, p_path, String(", ").join(E.value.libs)));
}
}
}
}

View File

@@ -32,15 +32,15 @@
#include "core/config/project_settings.h"
#include "core/version.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
#include "editor/editor_properties.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/export/editor_export.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/gui/editor_file_dialog.h"
#include "editor/import/resource_importer_texture_settings.h"
#include "editor/project_settings_editor.h"
#include "editor/inspector/editor_properties.h"
#include "editor/settings/editor_settings.h"
#include "editor/settings/project_settings_editor.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/check_button.h"
#include "scene/gui/item_list.h"

View File

@@ -0,0 +1,34 @@
/**************************************************************************/
/* register_exporters.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. */
/**************************************************************************/
#pragma once
void register_exporter_types();
void register_exporters();

View File

@@ -0,0 +1,466 @@
/**************************************************************************/
/* shader_baker_export_plugin.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_baker_export_plugin.h"
#include "core/config/project_settings.h"
#include "core/version.h"
#include "editor/editor_node.h"
#include "scene/3d/label_3d.h"
#include "scene/3d/sprite_3d.h"
#include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
// Ensure that AlphaCut is the same between the two classes so we can share the code to detect transparency.
static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_DISABLED, Label3D::ALPHA_CUT_DISABLED));
static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_DISCARD, Label3D::ALPHA_CUT_DISCARD));
static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_OPAQUE_PREPASS, Label3D::ALPHA_CUT_OPAQUE_PREPASS));
static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_HASH, Label3D::ALPHA_CUT_HASH));
static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_MAX, Label3D::ALPHA_CUT_MAX));
String ShaderBakerExportPlugin::get_name() const {
return "ShaderBaker";
}
bool ShaderBakerExportPlugin::_is_active(const Vector<String> &p_features) const {
// Shader baker should only work when a RendererRD driver is active, as the embedded shaders won't be found otherwise.
return RendererSceneRenderRD::get_singleton() != nullptr && RendererRD::MaterialStorage::get_singleton() != nullptr && p_features.has("shader_baker");
}
bool ShaderBakerExportPlugin::_initialize_container_format(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) {
Variant driver_variant = GLOBAL_GET("rendering/rendering_device/driver." + p_platform->get_os_name().to_lower());
if (!driver_variant.is_string()) {
driver_variant = GLOBAL_GET("rendering/rendering_device/driver");
if (!driver_variant.is_string()) {
return false;
}
}
shader_container_driver = driver_variant;
for (Ref<ShaderBakerExportPluginPlatform> platform : platforms) {
if (platform->matches_driver(shader_container_driver)) {
shader_container_format = platform->create_shader_container_format(p_platform);
ERR_FAIL_NULL_V_MSG(shader_container_format, false, "Unable to create shader container format for the export platform.");
return true;
}
}
return false;
}
void ShaderBakerExportPlugin::_cleanup_container_format() {
if (shader_container_format != nullptr) {
memdelete(shader_container_format);
shader_container_format = nullptr;
}
}
bool ShaderBakerExportPlugin::_initialize_cache_directory() {
shader_cache_export_path = get_export_base_path().path_join("shader_baker").path_join(shader_cache_platform_name).path_join(shader_container_driver);
if (!DirAccess::dir_exists_absolute(shader_cache_export_path)) {
Error err = DirAccess::make_dir_recursive_absolute(shader_cache_export_path);
ERR_FAIL_COND_V_MSG(err != OK, false, "Can't create shader cache folder for exporting.");
}
return true;
}
bool ShaderBakerExportPlugin::_begin_customize_resources(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) {
if (!_is_active(p_features)) {
return false;
}
if (!_initialize_container_format(p_platform, p_features)) {
return false;
}
shader_cache_platform_name = p_platform->get_os_name();
shader_cache_renderer_name = RendererSceneRenderRD::get_singleton()->get_name();
tasks_processed = 0;
tasks_total = 0;
tasks_cancelled = false;
StringBuilder to_hash;
to_hash.append("[GodotVersionNumber]");
to_hash.append(GODOT_VERSION_NUMBER);
to_hash.append("[GodotVersionHash]");
to_hash.append(GODOT_VERSION_HASH);
to_hash.append("[Renderer]");
to_hash.append(shader_cache_renderer_name);
customization_configuration_hash = to_hash.as_string().hash64();
BitField<RenderingShaderLibrary::FeatureBits> renderer_features = {};
bool xr_enabled = GLOBAL_GET("xr/shaders/enabled");
renderer_features.set_flag(RenderingShaderLibrary::FEATURE_ADVANCED_BIT);
if (xr_enabled) {
renderer_features.set_flag(RenderingShaderLibrary::FEATURE_MULTIVIEW_BIT);
}
int vrs_mode = GLOBAL_GET("rendering/vrs/mode");
if (vrs_mode != 0) {
renderer_features.set_flag(RenderingShaderLibrary::FEATURE_VRS_BIT);
}
// Both FP16 and FP32 variants should be included.
renderer_features.set_flag(RenderingShaderLibrary::FEATURE_FP16_BIT);
renderer_features.set_flag(RenderingShaderLibrary::FEATURE_FP32_BIT);
RendererSceneRenderRD::get_singleton()->enable_features(renderer_features);
// Included all shaders created by renderers and effects.
ShaderRD::shaders_embedded_set_lock();
const ShaderRD::ShaderVersionPairSet &pair_set = ShaderRD::shaders_embedded_set_get();
for (Pair<ShaderRD *, RID> pair : pair_set) {
_customize_shader_version(pair.first, pair.second);
}
ShaderRD::shaders_embedded_set_unlock();
// Include all shaders created by embedded materials.
RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
material_storage->shader_embedded_set_lock();
const HashSet<RID> &rid_set = material_storage->shader_embedded_set_get();
for (RID rid : rid_set) {
RendererRD::MaterialStorage::ShaderData *shader_data = material_storage->shader_get_data(rid);
if (shader_data != nullptr) {
Pair<ShaderRD *, RID> shader_version_pair = shader_data->get_native_shader_and_version();
if (shader_version_pair.first != nullptr) {
_customize_shader_version(shader_version_pair.first, shader_version_pair.second);
}
}
}
material_storage->shader_embedded_set_unlock();
return true;
}
bool ShaderBakerExportPlugin::_begin_customize_scenes(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) {
if (!_is_active(p_features)) {
return false;
}
if (shader_container_format == nullptr) {
// Resource customization failed to initialize.
return false;
}
return true;
}
void ShaderBakerExportPlugin::_end_customize_resources() {
if (!_initialize_cache_directory()) {
return;
}
// Run a progress bar that waits for all shader baking tasks to finish.
bool progress_active = true;
EditorProgress editor_progress("baking_shaders", TTR("Baking shaders"), tasks_total);
editor_progress.step("Baking...", 0);
while (progress_active) {
uint32_t tasks_for_progress = 0;
{
MutexLock lock(tasks_mutex);
if (tasks_processed >= tasks_total) {
progress_active = false;
} else {
tasks_condition.wait(lock);
tasks_for_progress = tasks_processed;
}
}
if (progress_active && editor_progress.step("Baking...", tasks_for_progress)) {
// User skipped the shader baker, we just don't pack the shaders in the project.
tasks_cancelled = true;
progress_active = false;
}
}
String shader_cache_user_dir = ShaderRD::get_shader_cache_user_dir();
for (const ShaderGroupItem &group_item : shader_group_items) {
// Wait for all shader compilation tasks of the group to be finished.
for (WorkerThreadPool::TaskID task_id : group_item.variant_tasks) {
WorkerThreadPool::get_singleton()->wait_for_task_completion(task_id);
}
if (!tasks_cancelled) {
WorkResult work_result;
{
MutexLock lock(shader_work_results_mutex);
work_result = shader_work_results[group_item.cache_path];
}
PackedByteArray cache_file_bytes = ShaderRD::save_shader_cache_bytes(group_item.variants, work_result.variant_data);
add_file(shader_cache_user_dir.path_join(group_item.cache_path), cache_file_bytes, false);
String cache_file_path = shader_cache_export_path.path_join(group_item.cache_path);
if (!DirAccess::exists(cache_file_path)) {
DirAccess::make_dir_recursive_absolute(cache_file_path.get_base_dir());
}
Ref<FileAccess> cache_file_access = FileAccess::open(cache_file_path, FileAccess::WRITE);
if (cache_file_access.is_valid()) {
cache_file_access->store_buffer(cache_file_bytes);
}
}
}
if (!tasks_cancelled) {
String file_cache_path = shader_cache_export_path.path_join("file_cache");
Ref<FileAccess> cache_list_access = FileAccess::open(file_cache_path, FileAccess::READ_WRITE);
if (cache_list_access.is_null()) {
cache_list_access = FileAccess::open(file_cache_path, FileAccess::WRITE);
}
if (cache_list_access.is_valid()) {
String cache_list_line;
while (cache_list_line = cache_list_access->get_line(), !cache_list_line.is_empty()) {
// Only add if it wasn't already added.
if (!shader_paths_processed.has(cache_list_line)) {
PackedByteArray cache_file_bytes = FileAccess::get_file_as_bytes(shader_cache_export_path.path_join(cache_list_line));
if (!cache_file_bytes.is_empty()) {
add_file(shader_cache_user_dir.path_join(cache_list_line), cache_file_bytes, false);
}
}
shader_paths_processed.erase(cache_list_line);
}
for (const String &shader_path : shader_paths_processed) {
cache_list_access->store_line(shader_path);
}
cache_list_access->close();
}
}
shader_paths_processed.clear();
shader_work_results.clear();
shader_group_items.clear();
_cleanup_container_format();
}
Ref<Resource> ShaderBakerExportPlugin::_customize_resource(const Ref<Resource> &p_resource, const String &p_path) {
RendererRD::MaterialStorage *singleton = RendererRD::MaterialStorage::get_singleton();
DEV_ASSERT(singleton != nullptr);
Ref<Material> material = p_resource;
if (material.is_valid()) {
RID material_rid = material->get_rid();
if (material_rid.is_valid()) {
RendererRD::MaterialStorage::ShaderData *shader_data = singleton->material_get_shader_data(material_rid);
if (shader_data != nullptr) {
Pair<ShaderRD *, RID> shader_version_pair = shader_data->get_native_shader_and_version();
if (shader_version_pair.first != nullptr) {
_customize_shader_version(shader_version_pair.first, shader_version_pair.second);
}
}
}
}
return Ref<Resource>();
}
Node *ShaderBakerExportPlugin::_customize_scene(Node *p_root, const String &p_path) {
LocalVector<Node *> nodes_to_visit;
nodes_to_visit.push_back(p_root);
while (!nodes_to_visit.is_empty()) {
// Visit all nodes recursively in the scene to find the Label3Ds and Sprite3Ds.
Node *node = nodes_to_visit[nodes_to_visit.size() - 1];
nodes_to_visit.remove_at(nodes_to_visit.size() - 1);
Label3D *label_3d = Object::cast_to<Label3D>(node);
Sprite3D *sprite_3d = Object::cast_to<Sprite3D>(node);
if (label_3d != nullptr || sprite_3d != nullptr) {
// Create materials for Label3D and Sprite3D, which are normally generated at runtime on demand.
HashMap<StringName, Variant> properties;
// These must match the defaults set by Sprite3D/Label3D.
properties["transparent"] = true; // Label3D doesn't have this property, but it is always true anyway.
properties["shaded"] = false;
properties["double_sided"] = true;
properties["no_depth_test"] = false;
properties["fixed_size"] = false;
properties["billboard"] = StandardMaterial3D::BILLBOARD_DISABLED;
properties["texture_filter"] = StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
properties["alpha_antialiasing_mode"] = StandardMaterial3D::ALPHA_ANTIALIASING_OFF;
properties["alpha_cut"] = SpriteBase3D::ALPHA_CUT_DISABLED;
List<PropertyInfo> property_list;
node->get_property_list(&property_list);
for (const PropertyInfo &info : property_list) {
bool valid = false;
Variant property = node->get(info.name, &valid);
if (valid) {
properties[info.name] = property;
}
}
// This must follow the logic in Sprite3D::draw_texture_rect().
BaseMaterial3D::Transparency mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_DISABLED;
if (properties["transparent"]) {
SpriteBase3D::AlphaCutMode acm = SpriteBase3D::AlphaCutMode(int(properties["alpha_cut"]));
if (acm == SpriteBase3D::ALPHA_CUT_DISCARD) {
mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_SCISSOR;
} else if (acm == SpriteBase3D::ALPHA_CUT_OPAQUE_PREPASS) {
mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_DEPTH_PRE_PASS;
} else if (acm == SpriteBase3D::ALPHA_CUT_HASH) {
mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_HASH;
} else {
mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA;
}
}
StandardMaterial3D::BillboardMode billboard_mode = StandardMaterial3D::BillboardMode(int(properties["billboard"]));
Ref<Material> sprite_3d_material = StandardMaterial3D::get_material_for_2d(bool(properties["shaded"]), mat_transparency, bool(properties["double_sided"]), billboard_mode == StandardMaterial3D::BILLBOARD_ENABLED, billboard_mode == StandardMaterial3D::BILLBOARD_FIXED_Y, false, bool(properties["no_depth_test"]), bool(properties["fixed_size"]), BaseMaterial3D::TextureFilter(int(properties["texture_filter"])), BaseMaterial3D::AlphaAntiAliasing(int(properties["alpha_antialiasing_mode"])));
_customize_resource(sprite_3d_material, String());
if (label_3d != nullptr) {
// Generate variants with and without MSDF support since we don't have access to the font here.
Ref<Material> label_3d_material = StandardMaterial3D::get_material_for_2d(bool(properties["shaded"]), mat_transparency, bool(properties["double_sided"]), billboard_mode == StandardMaterial3D::BILLBOARD_ENABLED, billboard_mode == StandardMaterial3D::BILLBOARD_FIXED_Y, true, bool(properties["no_depth_test"]), bool(properties["fixed_size"]), BaseMaterial3D::TextureFilter(int(properties["texture_filter"])), BaseMaterial3D::AlphaAntiAliasing(int(properties["alpha_antialiasing_mode"])));
_customize_resource(label_3d_material, String());
}
}
// Visit children.
int child_count = node->get_child_count();
for (int i = 0; i < child_count; i++) {
nodes_to_visit.push_back(node->get_child(i));
}
}
return nullptr;
}
uint64_t ShaderBakerExportPlugin::_get_customization_configuration_hash() const {
return customization_configuration_hash;
}
void ShaderBakerExportPlugin::_customize_shader_version(ShaderRD *p_shader, RID p_version) {
const int64_t variant_count = p_shader->get_variant_count();
const int64_t group_count = p_shader->get_group_count();
LocalVector<ShaderGroupItem> group_items;
group_items.resize(group_count);
RBSet<uint32_t> groups_to_compile;
for (int64_t i = 0; i < group_count; i++) {
if (!p_shader->is_group_enabled(i)) {
continue;
}
String cache_path = p_shader->version_get_cache_file_relative_path(p_version, i, shader_container_driver);
if (shader_paths_processed.has(cache_path)) {
continue;
}
shader_paths_processed.insert(cache_path);
groups_to_compile.insert(i);
group_items[i].cache_path = cache_path;
group_items[i].variants = p_shader->get_group_to_variants(i);
{
MutexLock lock(shader_work_results_mutex);
shader_work_results[cache_path].variant_data.resize(variant_count);
}
}
for (int64_t i = 0; i < variant_count; i++) {
int group = p_shader->get_variant_to_group(i);
if (!p_shader->is_variant_enabled(i) || !groups_to_compile.has(group)) {
continue;
}
WorkItem work_item;
work_item.cache_path = group_items[group].cache_path;
work_item.shader_name = p_shader->get_name();
work_item.stage_sources = p_shader->version_build_variant_stage_sources(p_version, i);
work_item.variant = i;
WorkerThreadPool::TaskID task_id = WorkerThreadPool::get_singleton()->add_template_task(this, &ShaderBakerExportPlugin::_process_work_item, work_item);
group_items[group].variant_tasks.push_back(task_id);
tasks_total++;
}
for (uint32_t i : groups_to_compile) {
shader_group_items.push_back(group_items[i]);
}
}
void ShaderBakerExportPlugin::_process_work_item(WorkItem p_work_item) {
if (!tasks_cancelled) {
// Only process the item if the tasks haven't been cancelled by the user yet.
Vector<RD::ShaderStageSPIRVData> spirv_data = ShaderRD::compile_stages(p_work_item.stage_sources);
ERR_FAIL_COND_MSG(spirv_data.is_empty(), "Unable to retrieve SPIR-V data for shader");
RD::ShaderReflection shader_refl;
Error err = RenderingDeviceCommons::reflect_spirv(spirv_data, shader_refl);
ERR_FAIL_COND_MSG(err != OK, "Unable to reflect SPIR-V data that was compiled");
Ref<RenderingShaderContainer> shader_container = shader_container_format->create_container();
shader_container->set_from_shader_reflection(p_work_item.shader_name, shader_refl);
// Compile shader binary from SPIR-V.
bool code_compiled = shader_container->set_code_from_spirv(spirv_data);
ERR_FAIL_COND_MSG(!code_compiled, vformat("Failed to compile code to native for SPIR-V."));
PackedByteArray shader_bytes = shader_container->to_bytes();
{
MutexLock lock(shader_work_results_mutex);
shader_work_results[p_work_item.cache_path].variant_data.ptrw()[p_work_item.variant] = shader_bytes;
}
}
{
MutexLock lock(tasks_mutex);
tasks_processed++;
}
tasks_condition.notify_one();
}
ShaderBakerExportPlugin::ShaderBakerExportPlugin() {
// Do nothing.
}
ShaderBakerExportPlugin::~ShaderBakerExportPlugin() {
// Do nothing.
}
void ShaderBakerExportPlugin::add_platform(Ref<ShaderBakerExportPluginPlatform> p_platform) {
platforms.push_back(p_platform);
}
void ShaderBakerExportPlugin::remove_platform(Ref<ShaderBakerExportPluginPlatform> p_platform) {
platforms.erase(p_platform);
}

View File

@@ -0,0 +1,102 @@
/**************************************************************************/
/* shader_baker_export_plugin.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. */
/**************************************************************************/
#pragma once
#include "editor/export/editor_export_plugin.h"
#include "servers/rendering/renderer_rd/shader_rd.h"
#include "servers/rendering/rendering_shader_container.h"
class ShaderBakerExportPluginPlatform : public RefCounted {
GDCLASS(ShaderBakerExportPluginPlatform, RefCounted);
public:
virtual RenderingShaderContainerFormat *create_shader_container_format(const Ref<EditorExportPlatform> &p_platform) = 0;
virtual bool matches_driver(const String &p_driver) = 0;
virtual ~ShaderBakerExportPluginPlatform() {}
};
class ShaderBakerExportPlugin : public EditorExportPlugin {
protected:
struct WorkItem {
String cache_path;
String shader_name;
Vector<String> stage_sources;
int64_t variant = 0;
};
struct WorkResult {
// Since this result is per group, this vector will have gaps in the data it covers as the indices must stay relative to all variants.
Vector<PackedByteArray> variant_data;
};
struct ShaderGroupItem {
String cache_path;
LocalVector<int> variants;
LocalVector<WorkerThreadPool::TaskID> variant_tasks;
};
String shader_cache_platform_name;
String shader_cache_renderer_name;
String shader_cache_export_path;
RBSet<String> shader_paths_processed;
HashMap<String, WorkResult> shader_work_results;
Mutex shader_work_results_mutex;
LocalVector<ShaderGroupItem> shader_group_items;
RenderingShaderContainerFormat *shader_container_format = nullptr;
String shader_container_driver;
Vector<Ref<ShaderBakerExportPluginPlatform>> platforms;
uint64_t customization_configuration_hash = 0;
uint32_t tasks_processed = 0;
uint32_t tasks_total = 0;
std::atomic<bool> tasks_cancelled;
BinaryMutex tasks_mutex;
ConditionVariable tasks_condition;
virtual String get_name() const override;
virtual bool _is_active(const Vector<String> &p_features) const;
virtual bool _initialize_container_format(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features);
virtual void _cleanup_container_format();
virtual bool _initialize_cache_directory();
virtual bool _begin_customize_resources(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) override;
virtual bool _begin_customize_scenes(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) override;
virtual void _end_customize_resources() override;
virtual Ref<Resource> _customize_resource(const Ref<Resource> &p_resource, const String &p_path) override;
virtual Node *_customize_scene(Node *p_root, const String &p_path) override;
virtual uint64_t _get_customization_configuration_hash() const override;
virtual void _customize_shader_version(ShaderRD *p_shader, RID p_version);
void _process_work_item(WorkItem p_work_item);
public:
ShaderBakerExportPlugin();
virtual ~ShaderBakerExportPlugin() override;
void add_platform(Ref<ShaderBakerExportPluginPlatform> p_platform);
void remove_platform(Ref<ShaderBakerExportPluginPlatform> p_platform);
};