visual_server_canvas_helper.cpp 8.4 KB

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