shader.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. RID shader;
  51. Mode mode = MODE_SPATIAL;
  52. HashSet<Ref<ShaderInclude>> include_dependencies;
  53. String code;
  54. String include_path;
  55. HashMap<StringName, HashMap<int, Ref<Texture2D>>> default_textures;
  56. void _dependency_changed();
  57. void _recompile();
  58. virtual void _update_shader() const; //used for visual shader
  59. Array _get_shader_uniform_list(bool p_get_groups = false);
  60. protected:
  61. static void _bind_methods();
  62. public:
  63. //void set_mode(Mode p_mode);
  64. virtual Mode get_mode() const;
  65. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  66. void set_include_path(const String &p_path);
  67. void set_code(const String &p_code);
  68. String get_code() const;
  69. void get_shader_uniform_list(List<PropertyInfo> *p_params, bool p_get_groups = false) const;
  70. void set_default_texture_parameter(const StringName &p_name, const Ref<Texture2D> &p_texture, int p_index = 0);
  71. Ref<Texture2D> get_default_texture_parameter(const StringName &p_name, int p_index = 0) const;
  72. void get_default_texture_parameter_list(List<StringName> *r_textures) const;
  73. virtual bool is_text_shader() const;
  74. virtual RID get_rid() const override;
  75. Shader();
  76. ~Shader();
  77. };
  78. VARIANT_ENUM_CAST(Shader::Mode);
  79. class ResourceFormatLoaderShader : public ResourceFormatLoader {
  80. public:
  81. 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);
  82. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  83. virtual bool handles_type(const String &p_type) const;
  84. virtual String get_resource_type(const String &p_path) const;
  85. };
  86. class ResourceFormatSaverShader : public ResourceFormatSaver {
  87. public:
  88. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0);
  89. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const;
  90. virtual bool recognize(const Ref<Resource> &p_resource) const;
  91. };
  92. #endif // SHADER_H