| 1 | /**************************************************************************/ |
| 2 | /* canvas_item_material.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 CANVAS_ITEM_MATERIAL_H |
| 32 | #define CANVAS_ITEM_MATERIAL_H |
| 33 | |
| 34 | #include "scene/resources/material.h" |
| 35 | |
| 36 | class CanvasItemMaterial : public Material { |
| 37 | GDCLASS(CanvasItemMaterial, Material); |
| 38 | |
| 39 | public: |
| 40 | enum BlendMode { |
| 41 | BLEND_MODE_MIX, |
| 42 | BLEND_MODE_ADD, |
| 43 | BLEND_MODE_SUB, |
| 44 | BLEND_MODE_MUL, |
| 45 | BLEND_MODE_PREMULT_ALPHA, |
| 46 | BLEND_MODE_DISABLED |
| 47 | }; |
| 48 | |
| 49 | enum LightMode { |
| 50 | LIGHT_MODE_NORMAL, |
| 51 | LIGHT_MODE_UNSHADED, |
| 52 | LIGHT_MODE_LIGHT_ONLY |
| 53 | }; |
| 54 | |
| 55 | private: |
| 56 | union MaterialKey { |
| 57 | struct { |
| 58 | uint32_t blend_mode : 4; |
| 59 | uint32_t light_mode : 4; |
| 60 | uint32_t particles_animation : 1; |
| 61 | uint32_t invalid_key : 1; |
| 62 | }; |
| 63 | |
| 64 | uint32_t key = 0; |
| 65 | |
| 66 | static uint32_t hash(const MaterialKey &p_key) { |
| 67 | return hash_murmur3_one_32(p_key.key); |
| 68 | } |
| 69 | bool operator==(const MaterialKey &p_key) const { |
| 70 | return key == p_key.key; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | struct ShaderNames { |
| 75 | StringName particles_anim_h_frames; |
| 76 | StringName particles_anim_v_frames; |
| 77 | StringName particles_anim_loop; |
| 78 | }; |
| 79 | |
| 80 | static ShaderNames *shader_names; |
| 81 | |
| 82 | struct ShaderData { |
| 83 | RID shader; |
| 84 | int users = 0; |
| 85 | }; |
| 86 | |
| 87 | static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map; |
| 88 | |
| 89 | MaterialKey current_key; |
| 90 | |
| 91 | _FORCE_INLINE_ MaterialKey _compute_key() const { |
| 92 | MaterialKey mk; |
| 93 | mk.key = 0; |
| 94 | mk.blend_mode = blend_mode; |
| 95 | mk.light_mode = light_mode; |
| 96 | mk.particles_animation = particles_animation; |
| 97 | return mk; |
| 98 | } |
| 99 | |
| 100 | static Mutex material_mutex; |
| 101 | static SelfList<CanvasItemMaterial>::List *dirty_materials; |
| 102 | SelfList<CanvasItemMaterial> element; |
| 103 | |
| 104 | void _update_shader(); |
| 105 | _FORCE_INLINE_ void _queue_shader_change(); |
| 106 | _FORCE_INLINE_ bool _is_shader_dirty() const; |
| 107 | |
| 108 | BlendMode blend_mode = BLEND_MODE_MIX; |
| 109 | LightMode light_mode = LIGHT_MODE_NORMAL; |
| 110 | bool particles_animation = false; |
| 111 | |
| 112 | // Proper values set in constructor. |
| 113 | int particles_anim_h_frames = 0; |
| 114 | int particles_anim_v_frames = 0; |
| 115 | bool particles_anim_loop = false; |
| 116 | |
| 117 | protected: |
| 118 | static void _bind_methods(); |
| 119 | void _validate_property(PropertyInfo &p_property) const; |
| 120 | |
| 121 | public: |
| 122 | void set_blend_mode(BlendMode p_blend_mode); |
| 123 | BlendMode get_blend_mode() const; |
| 124 | |
| 125 | void set_light_mode(LightMode p_light_mode); |
| 126 | LightMode get_light_mode() const; |
| 127 | |
| 128 | void set_particles_animation(bool p_particles_anim); |
| 129 | bool get_particles_animation() const; |
| 130 | |
| 131 | void set_particles_anim_h_frames(int p_frames); |
| 132 | int get_particles_anim_h_frames() const; |
| 133 | void set_particles_anim_v_frames(int p_frames); |
| 134 | int get_particles_anim_v_frames() const; |
| 135 | |
| 136 | void set_particles_anim_loop(bool p_loop); |
| 137 | bool get_particles_anim_loop() const; |
| 138 | |
| 139 | static void init_shaders(); |
| 140 | static void finish_shaders(); |
| 141 | static void flush_changes(); |
| 142 | |
| 143 | virtual RID get_shader_rid() const override; |
| 144 | |
| 145 | virtual Shader::Mode get_shader_mode() const override; |
| 146 | |
| 147 | CanvasItemMaterial(); |
| 148 | virtual ~CanvasItemMaterial(); |
| 149 | }; |
| 150 | |
| 151 | VARIANT_ENUM_CAST(CanvasItemMaterial::BlendMode) |
| 152 | VARIANT_ENUM_CAST(CanvasItemMaterial::LightMode) |
| 153 | |
| 154 | #endif // CANVAS_ITEM_MATERIAL_H |
| 155 | |