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