canvas_item_material.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. MutexLock lock(material_mutex);
  136. if (_is_initialized() && !element.in_list()) {
  137. dirty_materials.add(&element);
  138. }
  139. }
  140. void CanvasItemMaterial::set_blend_mode(BlendMode p_blend_mode) {
  141. blend_mode = p_blend_mode;
  142. _queue_shader_change();
  143. }
  144. CanvasItemMaterial::BlendMode CanvasItemMaterial::get_blend_mode() const {
  145. return blend_mode;
  146. }
  147. void CanvasItemMaterial::set_light_mode(LightMode p_light_mode) {
  148. light_mode = p_light_mode;
  149. _queue_shader_change();
  150. }
  151. CanvasItemMaterial::LightMode CanvasItemMaterial::get_light_mode() const {
  152. return light_mode;
  153. }
  154. void CanvasItemMaterial::set_particles_animation(bool p_particles_anim) {
  155. particles_animation = p_particles_anim;
  156. _queue_shader_change();
  157. notify_property_list_changed();
  158. }
  159. bool CanvasItemMaterial::get_particles_animation() const {
  160. return particles_animation;
  161. }
  162. void CanvasItemMaterial::set_particles_anim_h_frames(int p_frames) {
  163. particles_anim_h_frames = p_frames;
  164. RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_h_frames, p_frames);
  165. }
  166. int CanvasItemMaterial::get_particles_anim_h_frames() const {
  167. return particles_anim_h_frames;
  168. }
  169. void CanvasItemMaterial::set_particles_anim_v_frames(int p_frames) {
  170. particles_anim_v_frames = p_frames;
  171. RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_v_frames, p_frames);
  172. }
  173. int CanvasItemMaterial::get_particles_anim_v_frames() const {
  174. return particles_anim_v_frames;
  175. }
  176. void CanvasItemMaterial::set_particles_anim_loop(bool p_loop) {
  177. particles_anim_loop = p_loop;
  178. RS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_loop, particles_anim_loop);
  179. }
  180. bool CanvasItemMaterial::get_particles_anim_loop() const {
  181. return particles_anim_loop;
  182. }
  183. void CanvasItemMaterial::_validate_property(PropertyInfo &p_property) const {
  184. if (p_property.name.begins_with("particles_anim_") && !particles_animation) {
  185. p_property.usage = PROPERTY_USAGE_NONE;
  186. }
  187. }
  188. RID CanvasItemMaterial::get_shader_rid() const {
  189. ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
  190. return shader_map[current_key].shader;
  191. }
  192. Shader::Mode CanvasItemMaterial::get_shader_mode() const {
  193. return Shader::MODE_CANVAS_ITEM;
  194. }
  195. void CanvasItemMaterial::_bind_methods() {
  196. ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &CanvasItemMaterial::set_blend_mode);
  197. ClassDB::bind_method(D_METHOD("get_blend_mode"), &CanvasItemMaterial::get_blend_mode);
  198. ClassDB::bind_method(D_METHOD("set_light_mode", "light_mode"), &CanvasItemMaterial::set_light_mode);
  199. ClassDB::bind_method(D_METHOD("get_light_mode"), &CanvasItemMaterial::get_light_mode);
  200. ClassDB::bind_method(D_METHOD("set_particles_animation", "particles_anim"), &CanvasItemMaterial::set_particles_animation);
  201. ClassDB::bind_method(D_METHOD("get_particles_animation"), &CanvasItemMaterial::get_particles_animation);
  202. ClassDB::bind_method(D_METHOD("set_particles_anim_h_frames", "frames"), &CanvasItemMaterial::set_particles_anim_h_frames);
  203. ClassDB::bind_method(D_METHOD("get_particles_anim_h_frames"), &CanvasItemMaterial::get_particles_anim_h_frames);
  204. ClassDB::bind_method(D_METHOD("set_particles_anim_v_frames", "frames"), &CanvasItemMaterial::set_particles_anim_v_frames);
  205. ClassDB::bind_method(D_METHOD("get_particles_anim_v_frames"), &CanvasItemMaterial::get_particles_anim_v_frames);
  206. ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "loop"), &CanvasItemMaterial::set_particles_anim_loop);
  207. ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &CanvasItemMaterial::get_particles_anim_loop);
  208. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Subtract,Multiply,Premultiplied Alpha"), "set_blend_mode", "get_blend_mode");
  209. ADD_PROPERTY(PropertyInfo(Variant::INT, "light_mode", PROPERTY_HINT_ENUM, "Normal,Unshaded,Light Only"), "set_light_mode", "get_light_mode");
  210. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_animation"), "set_particles_animation", "get_particles_animation");
  211. 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");
  212. 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");
  213. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
  214. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  215. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  216. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  217. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  218. BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
  219. BIND_ENUM_CONSTANT(LIGHT_MODE_NORMAL);
  220. BIND_ENUM_CONSTANT(LIGHT_MODE_UNSHADED);
  221. BIND_ENUM_CONSTANT(LIGHT_MODE_LIGHT_ONLY);
  222. }
  223. CanvasItemMaterial::CanvasItemMaterial() :
  224. element(this) {
  225. _set_material(RS::get_singleton()->material_create());
  226. set_particles_anim_h_frames(1);
  227. set_particles_anim_v_frames(1);
  228. set_particles_anim_loop(false);
  229. current_key.invalid_key = 1;
  230. _mark_initialized(callable_mp(this, &CanvasItemMaterial::_queue_shader_change), callable_mp(this, &CanvasItemMaterial::_update_shader));
  231. }
  232. CanvasItemMaterial::~CanvasItemMaterial() {
  233. MutexLock lock(material_mutex);
  234. ERR_FAIL_NULL(RenderingServer::get_singleton());
  235. if (shader_map.has(current_key)) {
  236. shader_map[current_key].users--;
  237. if (shader_map[current_key].users == 0) {
  238. //deallocate shader, as it's no longer in use
  239. RS::get_singleton()->free(shader_map[current_key].shader);
  240. shader_map.erase(current_key);
  241. }
  242. RS::get_singleton()->material_set_shader(_get_material(), RID());
  243. }
  244. }