canvas_item_material.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*************************************************************************/
  2. /* canvas_item_material.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "canvas_item_material.h"
  31. #include "core/version.h"
  32. Mutex CanvasItemMaterial::material_mutex;
  33. SelfList<CanvasItemMaterial>::List *CanvasItemMaterial::dirty_materials = nullptr;
  34. HashMap<CanvasItemMaterial::MaterialKey, CanvasItemMaterial::ShaderData, CanvasItemMaterial::MaterialKey> CanvasItemMaterial::shader_map;
  35. CanvasItemMaterial::ShaderNames *CanvasItemMaterial::shader_names = nullptr;
  36. void CanvasItemMaterial::init_shaders() {
  37. dirty_materials = memnew(SelfList<CanvasItemMaterial>::List);
  38. shader_names = memnew(ShaderNames);
  39. shader_names->particles_anim_h_frames = "particles_anim_h_frames";
  40. shader_names->particles_anim_v_frames = "particles_anim_v_frames";
  41. shader_names->particles_anim_loop = "particles_anim_loop";
  42. }
  43. void CanvasItemMaterial::finish_shaders() {
  44. memdelete(dirty_materials);
  45. memdelete(shader_names);
  46. dirty_materials = nullptr;
  47. }
  48. void CanvasItemMaterial::_update_shader() {
  49. dirty_materials->remove(&element);
  50. MaterialKey mk = _compute_key();
  51. if (mk.key == current_key.key) {
  52. return; //no update required in the end
  53. }
  54. if (shader_map.has(current_key)) {
  55. shader_map[current_key].users--;
  56. if (shader_map[current_key].users == 0) {
  57. //deallocate shader, as it's no longer in use
  58. RS::get_singleton()->free(shader_map[current_key].shader);
  59. shader_map.erase(current_key);
  60. }
  61. }
  62. current_key = mk;
  63. if (shader_map.has(mk)) {
  64. RS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
  65. shader_map[mk].users++;
  66. return;
  67. }
  68. //must create a shader!
  69. // Add a comment to describe the shader origin (useful when converting to ShaderMaterial).
  70. String code = "// NOTE: Shader automatically converted from " VERSION_NAME " " VERSION_FULL_CONFIG "'s CanvasItemMaterial.\n\n";
  71. code += "shader_type canvas_item;\nrender_mode ";
  72. switch (blend_mode) {
  73. case BLEND_MODE_MIX:
  74. code += "blend_mix";
  75. break;
  76. case BLEND_MODE_ADD:
  77. code += "blend_add";
  78. break;
  79. case BLEND_MODE_SUB:
  80. code += "blend_sub";
  81. break;
  82. case BLEND_MODE_MUL:
  83. code += "blend_mul";
  84. break;
  85. case BLEND_MODE_PREMULT_ALPHA:
  86. code += "blend_premul_alpha";
  87. break;
  88. case BLEND_MODE_DISABLED:
  89. code += "blend_disabled";
  90. break;
  91. }
  92. switch (light_mode) {
  93. case LIGHT_MODE_NORMAL:
  94. break;
  95. case LIGHT_MODE_UNSHADED:
  96. code += ",unshaded";
  97. break;
  98. case LIGHT_MODE_LIGHT_ONLY:
  99. code += ",light_only";
  100. break;
  101. }
  102. code += ";\n";
  103. if (particles_animation) {
  104. code += "uniform int particles_anim_h_frames;\n";
  105. code += "uniform int particles_anim_v_frames;\n";
  106. code += "uniform bool particles_anim_loop;\n\n";
  107. code += "void vertex() {\n";
  108. code += " float h_frames = float(particles_anim_h_frames);\n";
  109. code += " float v_frames = float(particles_anim_v_frames);\n";
  110. code += " VERTEX.xy /= vec2(h_frames, v_frames);\n";
  111. code += " float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);\n";
  112. code += " float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));\n";
  113. code += " if (!particles_anim_loop) {\n";
  114. code += " particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);\n";
  115. code += " } else {\n";
  116. code += " particle_frame = mod(particle_frame, particle_total_frames);\n";
  117. code += " }";
  118. code += " UV /= vec2(h_frames, v_frames);\n";
  119. code += " UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames);\n";
  120. code += "}\n";
  121. }
  122. ShaderData shader_data;
  123. shader_data.shader = RS::get_singleton()->shader_create();
  124. shader_data.users = 1;
  125. RS::get_singleton()->shader_set_code(shader_data.shader, code);
  126. shader_map[mk] = shader_data;
  127. RS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
  128. }
  129. void CanvasItemMaterial::flush_changes() {
  130. MutexLock lock(material_mutex);
  131. while (dirty_materials->first()) {
  132. dirty_materials->first()->self()->_update_shader();
  133. }
  134. }
  135. void CanvasItemMaterial::_queue_shader_change() {
  136. MutexLock lock(material_mutex);
  137. if (is_initialized && !element.in_list()) {
  138. dirty_materials->add(&element);
  139. }
  140. }
  141. bool CanvasItemMaterial::_is_shader_dirty() const {
  142. MutexLock lock(material_mutex);
  143. return element.in_list();
  144. }
  145. void CanvasItemMaterial::set_blend_mode(BlendMode p_blend_mode) {
  146. blend_mode = p_blend_mode;
  147. _queue_shader_change();
  148. }
  149. CanvasItemMaterial::BlendMode CanvasItemMaterial::get_blend_mode() const {
  150. return blend_mode;
  151. }
  152. void CanvasItemMaterial::set_light_mode(LightMode p_light_mode) {
  153. light_mode = p_light_mode;
  154. _queue_shader_change();
  155. }
  156. CanvasItemMaterial::LightMode CanvasItemMaterial::get_light_mode() const {
  157. return light_mode;
  158. }
  159. void CanvasItemMaterial::set_particles_animation(bool p_particles_anim) {
  160. particles_animation = p_particles_anim;
  161. _queue_shader_change();
  162. notify_property_list_changed();
  163. }
  164. bool CanvasItemMaterial::get_particles_animation() const {
  165. return particles_animation;
  166. }
  167. void CanvasItemMaterial::set_particles_anim_h_frames(int p_frames) {
  168. particles_anim_h_frames = p_frames;
  169. RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_h_frames, p_frames);
  170. }
  171. int CanvasItemMaterial::get_particles_anim_h_frames() const {
  172. return particles_anim_h_frames;
  173. }
  174. void CanvasItemMaterial::set_particles_anim_v_frames(int p_frames) {
  175. particles_anim_v_frames = p_frames;
  176. RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_v_frames, p_frames);
  177. }
  178. int CanvasItemMaterial::get_particles_anim_v_frames() const {
  179. return particles_anim_v_frames;
  180. }
  181. void CanvasItemMaterial::set_particles_anim_loop(bool p_loop) {
  182. particles_anim_loop = p_loop;
  183. RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_loop, particles_anim_loop);
  184. }
  185. bool CanvasItemMaterial::get_particles_anim_loop() const {
  186. return particles_anim_loop;
  187. }
  188. void CanvasItemMaterial::_validate_property(PropertyInfo &p_property) const {
  189. if (p_property.name.begins_with("particles_anim_") && !particles_animation) {
  190. p_property.usage = PROPERTY_USAGE_NONE;
  191. }
  192. }
  193. RID CanvasItemMaterial::get_shader_rid() const {
  194. ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
  195. return shader_map[current_key].shader;
  196. }
  197. Shader::Mode CanvasItemMaterial::get_shader_mode() const {
  198. return Shader::MODE_CANVAS_ITEM;
  199. }
  200. void CanvasItemMaterial::_bind_methods() {
  201. ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &CanvasItemMaterial::set_blend_mode);
  202. ClassDB::bind_method(D_METHOD("get_blend_mode"), &CanvasItemMaterial::get_blend_mode);
  203. ClassDB::bind_method(D_METHOD("set_light_mode", "light_mode"), &CanvasItemMaterial::set_light_mode);
  204. ClassDB::bind_method(D_METHOD("get_light_mode"), &CanvasItemMaterial::get_light_mode);
  205. ClassDB::bind_method(D_METHOD("set_particles_animation", "particles_anim"), &CanvasItemMaterial::set_particles_animation);
  206. ClassDB::bind_method(D_METHOD("get_particles_animation"), &CanvasItemMaterial::get_particles_animation);
  207. ClassDB::bind_method(D_METHOD("set_particles_anim_h_frames", "frames"), &CanvasItemMaterial::set_particles_anim_h_frames);
  208. ClassDB::bind_method(D_METHOD("get_particles_anim_h_frames"), &CanvasItemMaterial::get_particles_anim_h_frames);
  209. ClassDB::bind_method(D_METHOD("set_particles_anim_v_frames", "frames"), &CanvasItemMaterial::set_particles_anim_v_frames);
  210. ClassDB::bind_method(D_METHOD("get_particles_anim_v_frames"), &CanvasItemMaterial::get_particles_anim_v_frames);
  211. ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "loop"), &CanvasItemMaterial::set_particles_anim_loop);
  212. ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &CanvasItemMaterial::get_particles_anim_loop);
  213. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Subtract,Multiply,Premultiplied Alpha"), "set_blend_mode", "get_blend_mode");
  214. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mode", PROPERTY_HINT_ENUM, "Normal,Unshaded,Light Only"), "set_light_mode", "get_light_mode");
  215. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_animation"), "set_particles_animation", "get_particles_animation");
  216. ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_h_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_h_frames", "get_particles_anim_h_frames");
  217. ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_v_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_v_frames", "get_particles_anim_v_frames");
  218. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
  219. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  220. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  221. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  222. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  223. BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
  224. BIND_ENUM_CONSTANT(LIGHT_MODE_NORMAL);
  225. BIND_ENUM_CONSTANT(LIGHT_MODE_UNSHADED);
  226. BIND_ENUM_CONSTANT(LIGHT_MODE_LIGHT_ONLY);
  227. }
  228. CanvasItemMaterial::CanvasItemMaterial() :
  229. element(this) {
  230. set_particles_anim_h_frames(1);
  231. set_particles_anim_v_frames(1);
  232. set_particles_anim_loop(false);
  233. current_key.invalid_key = 1;
  234. is_initialized = true;
  235. _queue_shader_change();
  236. }
  237. CanvasItemMaterial::~CanvasItemMaterial() {
  238. MutexLock lock(material_mutex);
  239. if (shader_map.has(current_key)) {
  240. shader_map[current_key].users--;
  241. if (shader_map[current_key].users == 0) {
  242. //deallocate shader, as it's no longer in use
  243. RS::get_singleton()->free(shader_map[current_key].shader);
  244. shader_map.erase(current_key);
  245. }
  246. RS::get_singleton()->material_set_shader(_get_material(), RID());
  247. }
  248. }