renderer_canvas_render.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**************************************************************************/
  2. /* renderer_canvas_render.h */
  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. #ifndef RENDERER_CANVAS_RENDER_H
  31. #define RENDERER_CANVAS_RENDER_H
  32. #include "servers/rendering/rendering_method.h"
  33. #include "servers/rendering_server.h"
  34. class RendererCanvasRender {
  35. public:
  36. static RendererCanvasRender *singleton;
  37. enum CanvasRectFlags {
  38. CANVAS_RECT_REGION = 1,
  39. CANVAS_RECT_TILE = 2,
  40. CANVAS_RECT_FLIP_H = 4,
  41. CANVAS_RECT_FLIP_V = 8,
  42. CANVAS_RECT_TRANSPOSE = 16,
  43. CANVAS_RECT_CLIP_UV = 32,
  44. CANVAS_RECT_IS_GROUP = 64,
  45. CANVAS_RECT_MSDF = 128,
  46. CANVAS_RECT_LCD = 256,
  47. };
  48. struct Light {
  49. bool enabled : 1;
  50. bool on_interpolate_transform_list : 1;
  51. bool interpolated : 1;
  52. Color color;
  53. Transform2D xform_curr;
  54. Transform2D xform_prev;
  55. float height;
  56. float energy;
  57. float scale;
  58. int z_min;
  59. int z_max;
  60. int layer_min;
  61. int layer_max;
  62. int item_mask;
  63. int item_shadow_mask;
  64. float directional_distance;
  65. RS::CanvasLightMode mode;
  66. RS::CanvasLightBlendMode blend_mode;
  67. RID texture;
  68. Vector2 texture_offset;
  69. RID canvas;
  70. bool use_shadow;
  71. int shadow_buffer_size;
  72. RS::CanvasLightShadowFilter shadow_filter;
  73. Color shadow_color;
  74. float shadow_smooth;
  75. //void *texture_cache; // implementation dependent
  76. Rect2 rect_cache;
  77. Transform2D xform_cache;
  78. float radius_cache; //used for shadow far plane
  79. //Projection shadow_matrix_cache;
  80. Transform2D light_shader_xform;
  81. //Vector2 light_shader_pos;
  82. Light *shadows_next_ptr = nullptr;
  83. Light *filter_next_ptr = nullptr;
  84. Light *next_ptr = nullptr;
  85. Light *directional_next_ptr = nullptr;
  86. RID light_internal;
  87. uint64_t version;
  88. int32_t render_index_cache;
  89. Light() {
  90. version = 0;
  91. enabled = true;
  92. on_interpolate_transform_list = false;
  93. interpolated = true;
  94. color = Color(1, 1, 1);
  95. shadow_color = Color(0, 0, 0, 0);
  96. height = 0;
  97. z_min = -1024;
  98. z_max = 1024;
  99. layer_min = 0;
  100. layer_max = 0;
  101. item_mask = 1;
  102. scale = 1.0;
  103. energy = 1.0;
  104. item_shadow_mask = 1;
  105. mode = RS::CANVAS_LIGHT_MODE_POINT;
  106. blend_mode = RS::CANVAS_LIGHT_BLEND_MODE_ADD;
  107. // texture_cache = nullptr;
  108. next_ptr = nullptr;
  109. directional_next_ptr = nullptr;
  110. filter_next_ptr = nullptr;
  111. use_shadow = false;
  112. shadow_buffer_size = 2048;
  113. shadow_filter = RS::CANVAS_LIGHT_FILTER_NONE;
  114. shadow_smooth = 0.0;
  115. render_index_cache = -1;
  116. directional_distance = 10000.0;
  117. }
  118. };
  119. //easier wrap to avoid mistakes
  120. struct Item;
  121. typedef uint64_t PolygonID;
  122. virtual PolygonID request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>()) = 0;
  123. virtual void free_polygon(PolygonID p_polygon) = 0;
  124. //also easier to wrap to avoid mistakes
  125. struct Polygon {
  126. PolygonID polygon_id;
  127. Rect2 rect_cache;
  128. _FORCE_INLINE_ void create(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>()) {
  129. ERR_FAIL_COND(polygon_id != 0);
  130. {
  131. uint32_t pc = p_points.size();
  132. const Vector2 *v2 = p_points.ptr();
  133. rect_cache.position = *v2;
  134. for (uint32_t i = 1; i < pc; i++) {
  135. rect_cache.expand_to(v2[i]);
  136. }
  137. }
  138. polygon_id = singleton->request_polygon(p_indices, p_points, p_colors, p_uvs, p_bones, p_weights);
  139. }
  140. _FORCE_INLINE_ Polygon() { polygon_id = 0; }
  141. _FORCE_INLINE_ ~Polygon() {
  142. if (polygon_id) {
  143. singleton->free_polygon(polygon_id);
  144. }
  145. }
  146. };
  147. //item
  148. struct Item {
  149. //commands are allocated in blocks of 4k to improve performance
  150. //and cache coherence.
  151. //blocks always grow but never shrink.
  152. struct CommandBlock {
  153. enum {
  154. MAX_SIZE = 4096
  155. };
  156. uint32_t usage;
  157. uint8_t *memory = nullptr;
  158. };
  159. struct Command {
  160. enum Type {
  161. TYPE_RECT,
  162. TYPE_NINEPATCH,
  163. TYPE_POLYGON,
  164. TYPE_PRIMITIVE,
  165. TYPE_MESH,
  166. TYPE_MULTIMESH,
  167. TYPE_PARTICLES,
  168. TYPE_TRANSFORM,
  169. TYPE_CLIP_IGNORE,
  170. TYPE_ANIMATION_SLICE,
  171. };
  172. Command *next = nullptr;
  173. Type type;
  174. virtual ~Command() {}
  175. };
  176. struct CommandRect : public Command {
  177. Rect2 rect;
  178. Color modulate;
  179. Rect2 source;
  180. uint16_t flags;
  181. float outline;
  182. float px_range;
  183. RID texture;
  184. CommandRect() {
  185. flags = 0;
  186. outline = 0;
  187. px_range = 1;
  188. type = TYPE_RECT;
  189. }
  190. };
  191. struct CommandNinePatch : public Command {
  192. Rect2 rect;
  193. Rect2 source;
  194. float margin[4];
  195. bool draw_center;
  196. Color color;
  197. RS::NinePatchAxisMode axis_x;
  198. RS::NinePatchAxisMode axis_y;
  199. RID texture;
  200. CommandNinePatch() {
  201. draw_center = true;
  202. type = TYPE_NINEPATCH;
  203. }
  204. };
  205. struct CommandPolygon : public Command {
  206. RS::PrimitiveType primitive;
  207. Polygon polygon;
  208. RID texture;
  209. CommandPolygon() {
  210. type = TYPE_POLYGON;
  211. }
  212. };
  213. struct CommandPrimitive : public Command {
  214. uint32_t point_count;
  215. Vector2 points[4];
  216. Vector2 uvs[4];
  217. Color colors[4];
  218. RID texture;
  219. CommandPrimitive() {
  220. type = TYPE_PRIMITIVE;
  221. }
  222. };
  223. struct CommandMesh : public Command {
  224. RID mesh;
  225. Transform2D transform;
  226. Color modulate;
  227. RID mesh_instance;
  228. RID texture;
  229. CommandMesh() { type = TYPE_MESH; }
  230. ~CommandMesh();
  231. };
  232. struct CommandMultiMesh : public Command {
  233. RID multimesh;
  234. RID texture;
  235. CommandMultiMesh() { type = TYPE_MULTIMESH; }
  236. };
  237. struct CommandParticles : public Command {
  238. RID particles;
  239. RID texture;
  240. CommandParticles() { type = TYPE_PARTICLES; }
  241. };
  242. struct CommandTransform : public Command {
  243. Transform2D xform;
  244. CommandTransform() { type = TYPE_TRANSFORM; }
  245. };
  246. struct CommandClipIgnore : public Command {
  247. bool ignore;
  248. CommandClipIgnore() {
  249. type = TYPE_CLIP_IGNORE;
  250. ignore = false;
  251. }
  252. };
  253. struct CommandAnimationSlice : public Command {
  254. double animation_length = 0;
  255. double slice_begin = 0;
  256. double slice_end = 0;
  257. double offset = 0;
  258. CommandAnimationSlice() {
  259. type = TYPE_ANIMATION_SLICE;
  260. }
  261. };
  262. struct ViewportRender {
  263. RenderingServer *owner = nullptr;
  264. void *udata = nullptr;
  265. Rect2 rect;
  266. };
  267. // For interpolation we store the current local xform,
  268. // and the previous xform from the previous tick.
  269. Transform2D xform_curr;
  270. Transform2D xform_prev;
  271. bool clip : 1;
  272. bool visible : 1;
  273. bool behind : 1;
  274. bool update_when_visible : 1;
  275. bool on_interpolate_transform_list : 1;
  276. bool interpolated : 1;
  277. struct CanvasGroup {
  278. RS::CanvasGroupMode mode;
  279. bool fit_empty;
  280. float fit_margin;
  281. bool blur_mipmaps;
  282. float clear_margin;
  283. };
  284. CanvasGroup *canvas_group = nullptr;
  285. bool use_canvas_group = false;
  286. int light_mask;
  287. int z_final;
  288. mutable bool custom_rect;
  289. mutable bool rect_dirty;
  290. mutable Rect2 rect;
  291. RID material;
  292. RID skeleton;
  293. Item *next = nullptr;
  294. struct CopyBackBuffer {
  295. Rect2 rect;
  296. Rect2 screen_rect;
  297. bool full;
  298. };
  299. CopyBackBuffer *copy_back_buffer = nullptr;
  300. Color final_modulate;
  301. Transform2D final_transform;
  302. Rect2 final_clip_rect;
  303. Item *final_clip_owner = nullptr;
  304. Item *material_owner = nullptr;
  305. Item *canvas_group_owner = nullptr;
  306. ViewportRender *vp_render = nullptr;
  307. bool distance_field;
  308. bool light_masked;
  309. bool repeat_source;
  310. Point2 repeat_size;
  311. int repeat_times = 1;
  312. Rect2 global_rect_cache;
  313. const Rect2 &get_rect() const;
  314. Command *commands = nullptr;
  315. Command *last_command = nullptr;
  316. Vector<CommandBlock> blocks;
  317. uint32_t current_block;
  318. #ifdef DEBUG_ENABLED
  319. mutable double debug_redraw_time = 0;
  320. #endif
  321. template <typename T>
  322. T *alloc_command() {
  323. T *command = nullptr;
  324. if (commands == nullptr) {
  325. // As the most common use case of canvas items is to
  326. // use only one command, the first is done with it's
  327. // own allocation. The rest of them use blocks.
  328. command = memnew(T);
  329. command->next = nullptr;
  330. commands = command;
  331. last_command = command;
  332. } else {
  333. //Subsequent commands go into a block.
  334. while (true) {
  335. if (unlikely(current_block == (uint32_t)blocks.size())) {
  336. // If we need more blocks, we allocate them
  337. // (they won't be freed until this CanvasItem is
  338. // deleted, though).
  339. CommandBlock cb;
  340. cb.memory = (uint8_t *)memalloc(CommandBlock::MAX_SIZE);
  341. cb.usage = 0;
  342. blocks.push_back(cb);
  343. }
  344. CommandBlock *c = &blocks.write[current_block];
  345. size_t space_left = CommandBlock::MAX_SIZE - c->usage;
  346. if (space_left < sizeof(T)) {
  347. current_block++;
  348. continue;
  349. }
  350. //allocate block and add to the linked list
  351. void *memory = c->memory + c->usage;
  352. command = memnew_placement(memory, T);
  353. command->next = nullptr;
  354. last_command->next = command;
  355. last_command = command;
  356. c->usage += sizeof(T);
  357. break;
  358. }
  359. }
  360. rect_dirty = true;
  361. return command;
  362. }
  363. void clear() {
  364. // The first one is always allocated on heap
  365. // the rest go in the blocks
  366. Command *c = commands;
  367. while (c) {
  368. Command *n = c->next;
  369. if (c == commands) {
  370. memdelete(commands);
  371. commands = nullptr;
  372. } else {
  373. c->~Command();
  374. }
  375. c = n;
  376. }
  377. {
  378. uint32_t cbc = MIN((current_block + 1), (uint32_t)blocks.size());
  379. CommandBlock *blockptr = blocks.ptrw();
  380. for (uint32_t i = 0; i < cbc; i++) {
  381. blockptr[i].usage = 0;
  382. }
  383. }
  384. last_command = nullptr;
  385. commands = nullptr;
  386. current_block = 0;
  387. clip = false;
  388. rect_dirty = true;
  389. final_clip_owner = nullptr;
  390. material_owner = nullptr;
  391. light_masked = false;
  392. }
  393. RS::CanvasItemTextureFilter texture_filter;
  394. RS::CanvasItemTextureRepeat texture_repeat;
  395. Item() {
  396. commands = nullptr;
  397. last_command = nullptr;
  398. current_block = 0;
  399. light_mask = 1;
  400. vp_render = nullptr;
  401. next = nullptr;
  402. final_clip_owner = nullptr;
  403. canvas_group_owner = nullptr;
  404. clip = false;
  405. final_modulate = Color(1, 1, 1, 1);
  406. visible = true;
  407. rect_dirty = true;
  408. custom_rect = false;
  409. behind = false;
  410. material_owner = nullptr;
  411. copy_back_buffer = nullptr;
  412. distance_field = false;
  413. light_masked = false;
  414. update_when_visible = false;
  415. z_final = 0;
  416. texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  417. texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  418. repeat_source = false;
  419. on_interpolate_transform_list = false;
  420. interpolated = true;
  421. }
  422. virtual ~Item() {
  423. clear();
  424. for (int i = 0; i < blocks.size(); i++) {
  425. memfree(blocks[i].memory);
  426. }
  427. if (copy_back_buffer) {
  428. memdelete(copy_back_buffer);
  429. }
  430. }
  431. };
  432. virtual void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, bool &r_sdf_used, RenderingMethod::RenderInfo *r_render_info = nullptr) = 0;
  433. struct LightOccluderInstance {
  434. bool enabled : 1;
  435. bool on_interpolate_transform_list : 1;
  436. bool interpolated : 1;
  437. RID canvas;
  438. RID polygon;
  439. RID occluder;
  440. Rect2 aabb_cache;
  441. Transform2D xform_curr;
  442. Transform2D xform_prev;
  443. Transform2D xform_cache;
  444. int light_mask;
  445. bool sdf_collision;
  446. RS::CanvasOccluderPolygonCullMode cull_cache;
  447. LightOccluderInstance *next = nullptr;
  448. LightOccluderInstance() {
  449. enabled = true;
  450. on_interpolate_transform_list = false;
  451. interpolated = false;
  452. sdf_collision = false;
  453. next = nullptr;
  454. light_mask = 1;
  455. cull_cache = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  456. }
  457. };
  458. virtual RID light_create() = 0;
  459. virtual void light_set_texture(RID p_rid, RID p_texture) = 0;
  460. virtual void light_set_use_shadow(RID p_rid, bool p_enable) = 0;
  461. virtual void light_update_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) = 0;
  462. virtual void light_update_directional_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_cull_distance, const Rect2 &p_clip_rect, LightOccluderInstance *p_occluders) = 0;
  463. virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) = 0;
  464. virtual RID occluder_polygon_create() = 0;
  465. virtual void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) = 0;
  466. virtual void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) = 0;
  467. virtual void set_shadow_texture_size(int p_size) = 0;
  468. virtual bool free(RID p_rid) = 0;
  469. virtual void update() = 0;
  470. virtual void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) = 0;
  471. RendererCanvasRender() { singleton = this; }
  472. virtual ~RendererCanvasRender() {}
  473. };
  474. #endif // RENDERER_CANVAS_RENDER_H