shader.h 5.1 KB

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