visual_server_canvas_helper.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**************************************************************************/
  2. /* visual_server_canvas_helper.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 "visual_server_canvas_helper.h"
  31. #include "servers/visual/rasterizer.h"
  32. #include "servers/visual_server.h"
  33. LocalVector<MultiRect> VisualServerCanvasHelper::_tilemap_multirects;
  34. Mutex VisualServerCanvasHelper::_tilemap_mutex;
  35. bool VisualServerCanvasHelper::_multirect_enabled = true;
  36. MultiRect::MultiRect() {
  37. begin();
  38. }
  39. MultiRect::~MultiRect() {
  40. end();
  41. }
  42. void MultiRect::begin() {
  43. DEV_CHECK_ONCE(!rects.size());
  44. rects.clear();
  45. sources.clear();
  46. state.flags = 0;
  47. state_set = false;
  48. }
  49. void MultiRect::add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, RID p_normal_map, bool p_clip_uv) {
  50. bool new_common_data = true;
  51. Rect2 rect = p_rect;
  52. Rect2 source = p_src_rect;
  53. // To make the rendering code as efficient as possible,
  54. // a single MultiRect command should have identical flips and transpose etc.
  55. // If these change, it flushes the previous multirect and starts a new one.
  56. uint32_t flags = 0;
  57. if (p_rect.size.x < 0) {
  58. flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  59. rect.size.x = -rect.size.x;
  60. }
  61. if (source.size.x < 0) {
  62. flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  63. source.size.x = -source.size.x;
  64. }
  65. if (p_rect.size.y < 0) {
  66. flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  67. rect.size.y = -rect.size.y;
  68. }
  69. if (source.size.y < 0) {
  70. flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  71. source.size.y = -source.size.y;
  72. }
  73. if (p_transpose) {
  74. flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  75. SWAP(rect.size.x, rect.size.y);
  76. }
  77. if (p_clip_uv) {
  78. flags |= RasterizerCanvas::CANVAS_RECT_CLIP_UV;
  79. }
  80. VisualServerCanvasHelper::State s;
  81. s.item = p_canvas_item;
  82. s.texture = p_texture;
  83. s.modulate = p_modulate;
  84. s.normal_map = p_normal_map;
  85. s.flags = flags;
  86. if (!is_empty()) {
  87. if ((state != s) ||
  88. (rects.size() >= MAX_RECTS)) {
  89. end();
  90. } else {
  91. new_common_data = false;
  92. }
  93. }
  94. if (new_common_data) {
  95. state = s;
  96. }
  97. rects.push_back(rect);
  98. sources.push_back(source);
  99. }
  100. void MultiRect::begin(const VisualServerCanvasHelper::State &p_state) {
  101. DEV_CHECK_ONCE(!rects.size());
  102. rects.clear();
  103. sources.clear();
  104. state = p_state;
  105. state_set = true;
  106. }
  107. uint32_t MultiRect::flags_from_rects(Rect2 &r_rect, Rect2 &r_source) {
  108. uint32_t flags = 0;
  109. if (r_rect.size.x < 0) {
  110. flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  111. r_rect.size.x = -r_rect.size.x;
  112. }
  113. if (r_rect.size.y < 0) {
  114. flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  115. r_rect.size.y = -r_rect.size.y;
  116. }
  117. if (r_source.size.x < 0) {
  118. flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  119. r_source.size.x = -r_source.size.x;
  120. }
  121. if (r_source.size.y < 0) {
  122. flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  123. r_source.size.y = -r_source.size.y;
  124. }
  125. return flags;
  126. }
  127. bool MultiRect::add_pre_flipped(const Rect2 &p_rect, const Rect2 &p_src_rect) {
  128. if (rects.is_full()) {
  129. return false;
  130. }
  131. *rects.request() = p_rect;
  132. *sources.request() = p_src_rect;
  133. return true;
  134. }
  135. bool MultiRect::add(const Rect2 &p_rect, const Rect2 &p_src_rect, bool p_commit_on_flip_change) {
  136. if (rects.is_full()) {
  137. return false;
  138. }
  139. Rect2 rect = p_rect;
  140. Rect2 source = p_src_rect;
  141. uint32_t flags = flags_from_rects(rect, source);
  142. if (state_set) {
  143. // if we are changing these flips, we can no longer continue the same multirect
  144. if ((state.flags & (RasterizerCanvas::CANVAS_RECT_FLIP_H | RasterizerCanvas::CANVAS_RECT_FLIP_V)) != flags) {
  145. // different state requires a new multirect
  146. return false;
  147. }
  148. } else {
  149. state.flags |= flags;
  150. state_set = true;
  151. }
  152. *rects.request() = rect;
  153. *sources.request() = source;
  154. return true;
  155. }
  156. void MultiRect::end() {
  157. if (!is_empty()) {
  158. if (VisualServerCanvasHelper::_multirect_enabled) {
  159. VisualServer::get_singleton()->canvas_item_add_texture_multirect_region(state.item, rects, state.texture, sources, state.modulate, state.flags, state.normal_map);
  160. } else {
  161. // legacy path
  162. bool transpose = state.flags & RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  163. bool clip_uv = state.flags & RasterizerCanvas::CANVAS_RECT_CLIP_UV;
  164. for (uint32_t n = 0; n < rects.size(); n++) {
  165. VisualServer::get_singleton()->canvas_item_add_texture_rect_region(state.item, rects[n], state.texture, sources[n], state.modulate, transpose, state.normal_map, clip_uv);
  166. }
  167. }
  168. rects.clear();
  169. sources.clear();
  170. }
  171. state_set = false;
  172. }
  173. void VisualServerCanvasHelper::tilemap_begin() {
  174. if (_multirect_enabled) {
  175. _tilemap_mutex.lock();
  176. }
  177. }
  178. void VisualServerCanvasHelper::tilemap_add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, RID p_normal_map, bool p_clip_uv) {
  179. if (!_multirect_enabled) {
  180. VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, p_texture, p_src_rect, p_modulate, p_transpose, p_normal_map, p_clip_uv);
  181. return;
  182. }
  183. Rect2 rect = p_rect;
  184. Rect2 source = p_src_rect;
  185. // To make the rendering code as efficient as possible,
  186. // a single MultiRect command should have identical flips and transpose etc.
  187. // If these change, it flushes the previous multirect and starts a new one.
  188. uint32_t flags = MultiRect::flags_from_rects(rect, source);
  189. if (p_transpose) {
  190. flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  191. SWAP(rect.size.x, rect.size.y);
  192. }
  193. if (p_clip_uv) {
  194. flags |= RasterizerCanvas::CANVAS_RECT_CLIP_UV;
  195. }
  196. State state;
  197. state.item = p_canvas_item;
  198. state.texture = p_texture;
  199. state.modulate = p_modulate;
  200. state.normal_map = p_normal_map;
  201. state.flags = flags;
  202. // attempt to add to existing multirect
  203. for (int n = _tilemap_multirects.size() - 1; n >= 0; n--) {
  204. MultiRect &mr = _tilemap_multirects[n];
  205. // matches state?
  206. if (mr.state == state) {
  207. // add .. this may fail if the multirect is full
  208. if (mr.add_pre_flipped(rect, source)) {
  209. return;
  210. }
  211. }
  212. // disallow if we overlap a multirect
  213. if (mr.overlaps(rect)) {
  214. break;
  215. }
  216. }
  217. // create new multirect
  218. _tilemap_multirects.resize(_tilemap_multirects.size() + 1);
  219. MultiRect &mr = _tilemap_multirects[_tilemap_multirects.size() - 1];
  220. mr.begin(state);
  221. mr.add_pre_flipped(rect, source);
  222. }
  223. void VisualServerCanvasHelper::tilemap_end() {
  224. if (!_multirect_enabled) {
  225. return;
  226. }
  227. for (uint32_t n = 0; n < _tilemap_multirects.size(); n++) {
  228. _tilemap_multirects[n].end();
  229. }
  230. _tilemap_multirects.clear();
  231. _tilemap_mutex.unlock();
  232. }