| 1 | /**************************************************************************/ |
| 2 | /* shader_baker_export_plugin.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 | |
| 31 | #include "shader_baker_export_plugin.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/dir_access.h" |
| 35 | #include "core/version.h" |
| 36 | #include "editor/editor_node.h" |
| 37 | #include "scene/3d/label_3d.h" |
| 38 | #include "scene/3d/sprite_3d.h" |
| 39 | #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h" |
| 40 | #include "servers/rendering/renderer_rd/storage_rd/material_storage.h" |
| 41 | #include "servers/rendering/rendering_shader_container.h" |
| 42 | |
| 43 | // Ensure that AlphaCut is the same between the two classes so we can share the code to detect transparency. |
| 44 | static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_DISABLED, Label3D::ALPHA_CUT_DISABLED)); |
| 45 | static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_DISCARD, Label3D::ALPHA_CUT_DISCARD)); |
| 46 | static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_OPAQUE_PREPASS, Label3D::ALPHA_CUT_OPAQUE_PREPASS)); |
| 47 | static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_HASH, Label3D::ALPHA_CUT_HASH)); |
| 48 | static_assert(ENUM_MEMBERS_EQUAL(SpriteBase3D::ALPHA_CUT_MAX, Label3D::ALPHA_CUT_MAX)); |
| 49 | |
| 50 | String ShaderBakerExportPlugin::get_name() const { |
| 51 | return "ShaderBaker"; |
| 52 | } |
| 53 | |
| 54 | bool ShaderBakerExportPlugin::_is_active(const Vector<String> &p_features) const { |
| 55 | // Shader baker should only work when a RendererRD driver is active, as the embedded shaders won't be found otherwise. |
| 56 | return RendererSceneRenderRD::get_singleton() != nullptr && RendererRD::MaterialStorage::get_singleton() != nullptr && p_features.has("shader_baker"); |
| 57 | } |
| 58 | |
| 59 | bool ShaderBakerExportPlugin::_initialize_container_format(const Ref<EditorExportPlatform> &p_platform, const Ref<EditorExportPreset> &p_preset) { |
| 60 | shader_container_driver = p_preset->get_project_setting("rendering/rendering_device/driver"); |
| 61 | ERR_FAIL_COND_V_MSG(shader_container_driver.is_empty(), false, "Invalid `rendering/rendering_device/driver` setting, disabling shader baking."); |
| 62 | |
| 63 | for (Ref<ShaderBakerExportPluginPlatform> platform : platforms) { |
| 64 | if (platform->matches_driver(shader_container_driver)) { |
| 65 | shader_container_format = platform->create_shader_container_format(p_platform, p_preset); |
| 66 | ERR_FAIL_NULL_V_MSG(shader_container_format, false, "Unable to create shader container format for the export platform."); |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | void ShaderBakerExportPlugin::_cleanup_container_format() { |
| 75 | if (shader_container_format != nullptr) { |
| 76 | memdelete(shader_container_format); |
| 77 | shader_container_format = nullptr; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool ShaderBakerExportPlugin::_initialize_cache_directory() { |
| 82 | shader_cache_export_path = get_export_base_path().path_join("shader_baker").path_join(shader_cache_platform_name).path_join(shader_container_driver); |
| 83 | |
| 84 | if (!DirAccess::dir_exists_absolute(shader_cache_export_path)) { |
| 85 | Error err = DirAccess::make_dir_recursive_absolute(shader_cache_export_path); |
| 86 | ERR_FAIL_COND_V_MSG(err != OK, false, "Can't create shader cache folder for exporting."); |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool ShaderBakerExportPlugin::_begin_customize_resources(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) { |
| 93 | if (!_is_active(p_features)) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if (!_initialize_container_format(p_platform, get_export_preset())) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) { |
| 102 | WARN_PRINT("Shader baker can't generate a compatible shader when run with --generate-spirv-debug-info. Restart the editor without this argument if you want to bake shaders."); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | shader_cache_platform_name = p_platform->get_os_name(); |
| 107 | shader_cache_renderer_name = RendererSceneRenderRD::get_singleton()->get_name(); |
| 108 | tasks_processed = 0; |
| 109 | tasks_total = 0; |
| 110 | tasks_cancelled = false; |
| 111 | |
| 112 | StringBuilder to_hash; |
| 113 | to_hash.append("[GodotVersionNumber]"); |
| 114 | to_hash.append(GODOT_VERSION_NUMBER); |
| 115 | to_hash.append("[GodotVersionHash]"); |
| 116 | to_hash.append(GODOT_VERSION_HASH); |
| 117 | to_hash.append("[Renderer]"); |
| 118 | to_hash.append(shader_cache_renderer_name); |
| 119 | customization_configuration_hash = to_hash.as_string().hash64(); |
| 120 | |
| 121 | BitField<RenderingShaderLibrary::FeatureBits> renderer_features = {}; |
| 122 | #ifndef XR_DISABLED |
| 123 | bool xr_enabled = GLOBAL_GET("xr/shaders/enabled"); |
| 124 | renderer_features.set_flag(RenderingShaderLibrary::FEATURE_ADVANCED_BIT); |
| 125 | if (xr_enabled) { |
| 126 | renderer_features.set_flag(RenderingShaderLibrary::FEATURE_MULTIVIEW_BIT); |
| 127 | } |
| 128 | #endif // XR_DISABLED |
| 129 | |
| 130 | int vrs_mode = GLOBAL_GET("rendering/vrs/mode"); |
| 131 | if (vrs_mode != 0) { |
| 132 | renderer_features.set_flag(RenderingShaderLibrary::FEATURE_VRS_BIT); |
| 133 | } |
| 134 | |
| 135 | // Both FP16 and FP32 variants should be included. |
| 136 | renderer_features.set_flag(RenderingShaderLibrary::FEATURE_FP16_BIT); |
| 137 | renderer_features.set_flag(RenderingShaderLibrary::FEATURE_FP32_BIT); |
| 138 | |
| 139 | RendererSceneRenderRD::get_singleton()->enable_features(renderer_features); |
| 140 | |
| 141 | // Included all shaders created by renderers and effects. |
| 142 | ShaderRD::shaders_embedded_set_lock(); |
| 143 | const ShaderRD::ShaderVersionPairSet &pair_set = ShaderRD::shaders_embedded_set_get(); |
| 144 | for (Pair<ShaderRD *, RID> pair : pair_set) { |
| 145 | _customize_shader_version(pair.first, pair.second); |
| 146 | } |
| 147 | |
| 148 | ShaderRD::shaders_embedded_set_unlock(); |
| 149 | |
| 150 | // Include all shaders created by embedded materials. |
| 151 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 152 | material_storage->shader_embedded_set_lock(); |
| 153 | const HashSet<RID> &rid_set = material_storage->shader_embedded_set_get(); |
| 154 | for (RID rid : rid_set) { |
| 155 | RendererRD::MaterialStorage::ShaderData *shader_data = material_storage->shader_get_data(rid); |
| 156 | if (shader_data != nullptr) { |
| 157 | Pair<ShaderRD *, RID> shader_version_pair = shader_data->get_native_shader_and_version(); |
| 158 | if (shader_version_pair.first != nullptr) { |
| 159 | _customize_shader_version(shader_version_pair.first, shader_version_pair.second); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | material_storage->shader_embedded_set_unlock(); |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | bool ShaderBakerExportPlugin::_begin_customize_scenes(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) { |
| 170 | if (!_is_active(p_features)) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (shader_container_format == nullptr) { |
| 175 | // Resource customization failed to initialize. |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | void ShaderBakerExportPlugin::_end_customize_resources() { |
| 183 | if (!_initialize_cache_directory()) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | // Run a progress bar that waits for all shader baking tasks to finish. |
| 188 | bool progress_active = true; |
| 189 | EditorProgress editor_progress("baking_shaders", TTR("Baking shaders"), tasks_total); |
| 190 | editor_progress.step("Baking...", 0); |
| 191 | while (progress_active) { |
| 192 | uint32_t tasks_for_progress = 0; |
| 193 | { |
| 194 | MutexLock lock(tasks_mutex); |
| 195 | if (tasks_processed >= tasks_total) { |
| 196 | progress_active = false; |
| 197 | } else { |
| 198 | tasks_condition.wait(lock); |
| 199 | tasks_for_progress = tasks_processed; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (progress_active && editor_progress.step("Baking...", tasks_for_progress)) { |
| 204 | // User skipped the shader baker, we just don't pack the shaders in the project. |
| 205 | tasks_cancelled = true; |
| 206 | progress_active = false; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | String shader_cache_user_dir = ShaderRD::get_shader_cache_user_dir(); |
| 211 | for (const ShaderGroupItem &group_item : shader_group_items) { |
| 212 | // Wait for all shader compilation tasks of the group to be finished. |
| 213 | for (WorkerThreadPool::TaskID task_id : group_item.variant_tasks) { |
| 214 | WorkerThreadPool::get_singleton()->wait_for_task_completion(task_id); |
| 215 | } |
| 216 | |
| 217 | if (!tasks_cancelled) { |
| 218 | WorkResult work_result; |
| 219 | { |
| 220 | MutexLock lock(shader_work_results_mutex); |
| 221 | work_result = shader_work_results[group_item.cache_path]; |
| 222 | } |
| 223 | |
| 224 | PackedByteArray cache_file_bytes = ShaderRD::save_shader_cache_bytes(group_item.variants, work_result.variant_data); |
| 225 | add_file(shader_cache_user_dir.path_join(group_item.cache_path), cache_file_bytes, false); |
| 226 | |
| 227 | String cache_file_path = shader_cache_export_path.path_join(group_item.cache_path); |
| 228 | if (!DirAccess::exists(cache_file_path)) { |
| 229 | DirAccess::make_dir_recursive_absolute(cache_file_path.get_base_dir()); |
| 230 | } |
| 231 | |
| 232 | Ref<FileAccess> cache_file_access = FileAccess::open(cache_file_path, FileAccess::WRITE); |
| 233 | if (cache_file_access.is_valid()) { |
| 234 | cache_file_access->store_buffer(cache_file_bytes); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (!tasks_cancelled) { |
| 240 | String file_cache_path = shader_cache_export_path.path_join("file_cache"); |
| 241 | Ref<FileAccess> cache_list_access = FileAccess::open(file_cache_path, FileAccess::READ_WRITE); |
| 242 | if (cache_list_access.is_null()) { |
| 243 | cache_list_access = FileAccess::open(file_cache_path, FileAccess::WRITE); |
| 244 | } |
| 245 | |
| 246 | if (cache_list_access.is_valid()) { |
| 247 | String cache_list_line; |
| 248 | while (cache_list_line = cache_list_access->get_line(), !cache_list_line.is_empty()) { |
| 249 | // Only add if it wasn't already added. |
| 250 | if (!shader_paths_processed.has(cache_list_line)) { |
| 251 | PackedByteArray cache_file_bytes = FileAccess::get_file_as_bytes(shader_cache_export_path.path_join(cache_list_line)); |
| 252 | if (!cache_file_bytes.is_empty()) { |
| 253 | add_file(shader_cache_user_dir.path_join(cache_list_line), cache_file_bytes, false); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | shader_paths_processed.erase(cache_list_line); |
| 258 | } |
| 259 | |
| 260 | for (const String &shader_path : shader_paths_processed) { |
| 261 | cache_list_access->store_line(shader_path); |
| 262 | } |
| 263 | |
| 264 | cache_list_access->close(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | shader_paths_processed.clear(); |
| 269 | shader_work_results.clear(); |
| 270 | shader_group_items.clear(); |
| 271 | |
| 272 | _cleanup_container_format(); |
| 273 | } |
| 274 | |
| 275 | Ref<Resource> ShaderBakerExportPlugin::_customize_resource(const Ref<Resource> &p_resource, const String &p_path) { |
| 276 | RendererRD::MaterialStorage *singleton = RendererRD::MaterialStorage::get_singleton(); |
| 277 | DEV_ASSERT(singleton != nullptr); |
| 278 | |
| 279 | Ref<Material> material = p_resource; |
| 280 | if (material.is_valid()) { |
| 281 | RID material_rid = material->get_rid(); |
| 282 | if (material_rid.is_valid()) { |
| 283 | RendererRD::MaterialStorage::ShaderData *shader_data = singleton->material_get_shader_data(material_rid); |
| 284 | if (shader_data != nullptr) { |
| 285 | Pair<ShaderRD *, RID> shader_version_pair = shader_data->get_native_shader_and_version(); |
| 286 | if (shader_version_pair.first != nullptr) { |
| 287 | _customize_shader_version(shader_version_pair.first, shader_version_pair.second); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return Ref<Resource>(); |
| 294 | } |
| 295 | |
| 296 | Node *ShaderBakerExportPlugin::_customize_scene(Node *p_root, const String &p_path) { |
| 297 | LocalVector<Node *> nodes_to_visit; |
| 298 | nodes_to_visit.push_back(p_root); |
| 299 | while (!nodes_to_visit.is_empty()) { |
| 300 | // Visit all nodes recursively in the scene to find the Label3Ds and Sprite3Ds. |
| 301 | Node *node = nodes_to_visit[nodes_to_visit.size() - 1]; |
| 302 | nodes_to_visit.remove_at(nodes_to_visit.size() - 1); |
| 303 | |
| 304 | Label3D *label_3d = Object::cast_to<Label3D>(node); |
| 305 | Sprite3D *sprite_3d = Object::cast_to<Sprite3D>(node); |
| 306 | if (label_3d != nullptr || sprite_3d != nullptr) { |
| 307 | // Create materials for Label3D and Sprite3D, which are normally generated at runtime on demand. |
| 308 | HashMap<StringName, Variant> properties; |
| 309 | |
| 310 | // These must match the defaults set by Sprite3D/Label3D. |
| 311 | properties["transparent"] = true; // Label3D doesn't have this property, but it is always true anyway. |
| 312 | properties["shaded"] = false; |
| 313 | properties["double_sided"] = true; |
| 314 | properties["no_depth_test"] = false; |
| 315 | properties["fixed_size"] = false; |
| 316 | properties["billboard"] = StandardMaterial3D::BILLBOARD_DISABLED; |
| 317 | properties["texture_filter"] = StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS; |
| 318 | properties["alpha_antialiasing_mode"] = StandardMaterial3D::ALPHA_ANTIALIASING_OFF; |
| 319 | properties["alpha_cut"] = SpriteBase3D::ALPHA_CUT_DISABLED; |
| 320 | |
| 321 | List<PropertyInfo> property_list; |
| 322 | node->get_property_list(&property_list); |
| 323 | for (const PropertyInfo &info : property_list) { |
| 324 | bool valid = false; |
| 325 | Variant property = node->get(info.name, &valid); |
| 326 | if (valid) { |
| 327 | properties[info.name] = property; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // This must follow the logic in Sprite3D::draw_texture_rect(). |
| 332 | BaseMaterial3D::Transparency mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_DISABLED; |
| 333 | if (properties["transparent"]) { |
| 334 | SpriteBase3D::AlphaCutMode acm = SpriteBase3D::AlphaCutMode(int(properties["alpha_cut"])); |
| 335 | if (acm == SpriteBase3D::ALPHA_CUT_DISCARD) { |
| 336 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_SCISSOR; |
| 337 | } else if (acm == SpriteBase3D::ALPHA_CUT_OPAQUE_PREPASS) { |
| 338 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_DEPTH_PRE_PASS; |
| 339 | } else if (acm == SpriteBase3D::ALPHA_CUT_HASH) { |
| 340 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_HASH; |
| 341 | } else { |
| 342 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | StandardMaterial3D::BillboardMode billboard_mode = StandardMaterial3D::BillboardMode(int(properties["billboard"])); |
| 347 | 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"]))); |
| 348 | _customize_resource(sprite_3d_material, String()); |
| 349 | |
| 350 | if (label_3d != nullptr) { |
| 351 | // Generate variants with and without MSDF support since we don't have access to the font here. |
| 352 | 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"]))); |
| 353 | _customize_resource(label_3d_material, String()); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Visit children. |
| 358 | int child_count = node->get_child_count(); |
| 359 | for (int i = 0; i < child_count; i++) { |
| 360 | nodes_to_visit.push_back(node->get_child(i)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return nullptr; |
| 365 | } |
| 366 | |
| 367 | uint64_t ShaderBakerExportPlugin::_get_customization_configuration_hash() const { |
| 368 | return customization_configuration_hash; |
| 369 | } |
| 370 | |
| 371 | void ShaderBakerExportPlugin::_customize_shader_version(ShaderRD *p_shader, RID p_version) { |
| 372 | const int64_t variant_count = p_shader->get_variant_count(); |
| 373 | const int64_t group_count = p_shader->get_group_count(); |
| 374 | LocalVector<ShaderGroupItem> group_items; |
| 375 | group_items.resize(group_count); |
| 376 | |
| 377 | RBSet<uint32_t> groups_to_compile; |
| 378 | for (int64_t i = 0; i < group_count; i++) { |
| 379 | if (!p_shader->is_group_enabled(i)) { |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | String cache_path = p_shader->version_get_cache_file_relative_path(p_version, i, shader_container_driver); |
| 384 | if (shader_paths_processed.has(cache_path)) { |
| 385 | continue; |
| 386 | } |
| 387 | |
| 388 | shader_paths_processed.insert(cache_path); |
| 389 | groups_to_compile.insert(i); |
| 390 | |
| 391 | group_items[i].cache_path = cache_path; |
| 392 | group_items[i].variants = p_shader->get_group_to_variants(i); |
| 393 | |
| 394 | { |
| 395 | MutexLock lock(shader_work_results_mutex); |
| 396 | shader_work_results[cache_path].variant_data.resize(variant_count); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | for (int64_t i = 0; i < variant_count; i++) { |
| 401 | int group = p_shader->get_variant_to_group(i); |
| 402 | if (!p_shader->is_variant_enabled(i) || !groups_to_compile.has(group)) { |
| 403 | continue; |
| 404 | } |
| 405 | |
| 406 | WorkItem work_item; |
| 407 | work_item.cache_path = group_items[group].cache_path; |
| 408 | work_item.shader_name = p_shader->get_name(); |
| 409 | work_item.stage_sources = p_shader->version_build_variant_stage_sources(p_version, i); |
| 410 | work_item.dynamic_buffers = p_shader->get_dynamic_buffers(); |
| 411 | work_item.variant = i; |
| 412 | |
| 413 | WorkerThreadPool::TaskID task_id = WorkerThreadPool::get_singleton()->add_template_task(this, &ShaderBakerExportPlugin::_process_work_item, work_item); |
| 414 | group_items[group].variant_tasks.push_back(task_id); |
| 415 | tasks_total++; |
| 416 | } |
| 417 | |
| 418 | for (uint32_t i : groups_to_compile) { |
| 419 | shader_group_items.push_back(group_items[i]); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void ShaderBakerExportPlugin::_process_work_item(WorkItem p_work_item) { |
| 424 | if (!tasks_cancelled) { |
| 425 | // Only process the item if the tasks haven't been cancelled by the user yet. |
| 426 | Vector<RD::ShaderStageSPIRVData> spirv_data = ShaderRD::compile_stages(p_work_item.stage_sources, p_work_item.dynamic_buffers); |
| 427 | if (unlikely(spirv_data.is_empty())) { |
| 428 | ERR_PRINT("Unable to retrieve SPIR-V data for shader."); |
| 429 | } else { |
| 430 | Ref<RenderingShaderContainer> shader_container = shader_container_format->create_container(); |
| 431 | |
| 432 | // Compile shader binary from SPIR-V. |
| 433 | bool code_compiled = shader_container->set_code_from_spirv(p_work_item.shader_name, spirv_data); |
| 434 | if (unlikely(!code_compiled)) { |
| 435 | ERR_PRINT("Failed to compile code to native for SPIR-V."); |
| 436 | } else { |
| 437 | PackedByteArray shader_bytes = shader_container->to_bytes(); |
| 438 | { |
| 439 | MutexLock lock(shader_work_results_mutex); |
| 440 | shader_work_results[p_work_item.cache_path].variant_data.ptrw()[p_work_item.variant] = shader_bytes; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | { |
| 447 | MutexLock lock(tasks_mutex); |
| 448 | tasks_processed++; |
| 449 | } |
| 450 | |
| 451 | tasks_condition.notify_one(); |
| 452 | } |
| 453 | |
| 454 | ShaderBakerExportPlugin::ShaderBakerExportPlugin() { |
| 455 | // Do nothing. |
| 456 | } |
| 457 | |
| 458 | ShaderBakerExportPlugin::~ShaderBakerExportPlugin() { |
| 459 | // Do nothing. |
| 460 | } |
| 461 | |
| 462 | void ShaderBakerExportPlugin::add_platform(Ref<ShaderBakerExportPluginPlatform> p_platform) { |
| 463 | platforms.push_back(p_platform); |
| 464 | } |
| 465 | |
| 466 | void ShaderBakerExportPlugin::remove_platform(Ref<ShaderBakerExportPluginPlatform> p_platform) { |
| 467 | platforms.erase(p_platform); |
| 468 | } |