shader.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**************************************************************************/
  2. /* shader.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 "core/io/resource.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "scene/resources/texture.h"
  35. #include "shader_include.h"
  36. class Shader : public Resource {
  37. GDCLASS(Shader, Resource);
  38. OBJ_SAVE_TYPE(Shader);
  39. public:
  40. enum Mode {
  41. MODE_SPATIAL,
  42. MODE_CANVAS_ITEM,
  43. MODE_PARTICLES,
  44. MODE_SKY,
  45. MODE_FOG,
  46. MODE_MAX
  47. };
  48. private:
  49. mutable RID shader_rid;
  50. mutable String preprocessed_code;
  51. mutable Mutex shader_rid_mutex;
  52. Mode mode = MODE_SPATIAL;
  53. HashSet<Ref<ShaderInclude>> include_dependencies;
  54. String code;
  55. String include_path;
  56. HashMap<StringName, HashMap<int, Ref<Texture>>> default_textures;
  57. void _check_shader_rid() const;
  58. void _dependency_changed();
  59. void _recompile();
  60. virtual void _update_shader() const; //used for visual shader
  61. Array _get_shader_uniform_list(bool p_get_groups = false);
  62. protected:
  63. #ifndef DISABLE_DEPRECATED
  64. void _set_default_texture_parameter_bind_compat_95126(const StringName &p_name, const Ref<Texture2D> &p_texture, int p_index = 0);
  65. Ref<Texture2D> _get_default_texture_parameter_bind_compat_95126(const StringName &p_name, int p_index = 0) const;
  66. static void _bind_compatibility_methods();
  67. #endif // DISABLE_DEPRECATED
  68. static void _bind_methods();
  69. public:
  70. //void set_mode(Mode p_mode);
  71. virtual Mode get_mode() const;
  72. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  73. void set_include_path(const String &p_path);
  74. void set_code(const String &p_code);
  75. String get_code() const;
  76. void inspect_native_shader_code();
  77. void get_shader_uniform_list(List<PropertyInfo> *p_params, bool p_get_groups = false) const;
  78. void set_default_texture_parameter(const StringName &p_name, const Ref<Texture> &p_texture, int p_index = 0);
  79. Ref<Texture> get_default_texture_parameter(const StringName &p_name, int p_index = 0) const;
  80. void get_default_texture_parameter_list(List<StringName> *r_textures) const;
  81. virtual bool is_text_shader() const;
  82. virtual RID get_rid() const override;
  83. Shader();
  84. ~Shader();
  85. };
  86. VARIANT_ENUM_CAST(Shader::Mode);
  87. class ResourceFormatLoaderShader : public ResourceFormatLoader {
  88. public:
  89. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  90. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  91. virtual bool handles_type(const String &p_type) const override;
  92. virtual String get_resource_type(const String &p_path) const override;
  93. };
  94. class ResourceFormatSaverShader : public ResourceFormatSaver {
  95. public:
  96. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
  97. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
  98. virtual bool recognize(const Ref<Resource> &p_resource) const override;
  99. };