shader_baker_export_plugin.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**************************************************************************/
  2. /* shader_baker_export_plugin.h */
  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. #pragma once
  31. #include "editor/export/editor_export_plugin.h"
  32. #include "servers/rendering/renderer_rd/shader_rd.h"
  33. #include "servers/rendering/rendering_shader_container.h"
  34. class ShaderBakerExportPluginPlatform : public RefCounted {
  35. GDCLASS(ShaderBakerExportPluginPlatform, RefCounted);
  36. public:
  37. virtual RenderingShaderContainerFormat *create_shader_container_format(const Ref<EditorExportPlatform> &p_platform) = 0;
  38. virtual bool matches_driver(const String &p_driver) = 0;
  39. virtual ~ShaderBakerExportPluginPlatform() {}
  40. };
  41. class ShaderBakerExportPlugin : public EditorExportPlugin {
  42. protected:
  43. struct WorkItem {
  44. String cache_path;
  45. String shader_name;
  46. Vector<String> stage_sources;
  47. int64_t variant = 0;
  48. };
  49. struct WorkResult {
  50. // 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.
  51. Vector<PackedByteArray> variant_data;
  52. };
  53. struct ShaderGroupItem {
  54. String cache_path;
  55. LocalVector<int> variants;
  56. LocalVector<WorkerThreadPool::TaskID> variant_tasks;
  57. };
  58. String shader_cache_platform_name;
  59. String shader_cache_renderer_name;
  60. String shader_cache_export_path;
  61. RBSet<String> shader_paths_processed;
  62. HashMap<String, WorkResult> shader_work_results;
  63. Mutex shader_work_results_mutex;
  64. LocalVector<ShaderGroupItem> shader_group_items;
  65. RenderingShaderContainerFormat *shader_container_format = nullptr;
  66. String shader_container_driver;
  67. Vector<Ref<ShaderBakerExportPluginPlatform>> platforms;
  68. uint64_t customization_configuration_hash = 0;
  69. uint32_t tasks_processed = 0;
  70. uint32_t tasks_total = 0;
  71. std::atomic<bool> tasks_cancelled;
  72. BinaryMutex tasks_mutex;
  73. ConditionVariable tasks_condition;
  74. virtual String get_name() const override;
  75. virtual bool _is_active(const Vector<String> &p_features) const;
  76. virtual bool _initialize_container_format(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features);
  77. virtual void _cleanup_container_format();
  78. virtual bool _initialize_cache_directory();
  79. virtual bool _begin_customize_resources(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) override;
  80. virtual bool _begin_customize_scenes(const Ref<EditorExportPlatform> &p_platform, const Vector<String> &p_features) override;
  81. virtual void _end_customize_resources() override;
  82. virtual Ref<Resource> _customize_resource(const Ref<Resource> &p_resource, const String &p_path) override;
  83. virtual Node *_customize_scene(Node *p_root, const String &p_path) override;
  84. virtual uint64_t _get_customization_configuration_hash() const override;
  85. virtual void _customize_shader_version(ShaderRD *p_shader, RID p_version);
  86. void _process_work_item(WorkItem p_work_item);
  87. public:
  88. ShaderBakerExportPlugin();
  89. virtual ~ShaderBakerExportPlugin() override;
  90. void add_platform(Ref<ShaderBakerExportPluginPlatform> p_platform);
  91. void remove_platform(Ref<ShaderBakerExportPluginPlatform> p_platform);
  92. };