canvas_item_material.cpp 12 KB

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