renderer_canvas_render.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. Item *repeat_source_item = nullptr;
  313. Rect2 global_rect_cache;
  314. const Rect2 &get_rect() const;
  315. Command *commands = nullptr;
  316. Command *last_command = nullptr;
  317. Vector<CommandBlock> blocks;
  318. uint32_t current_block;
  319. #ifdef DEBUG_ENABLED
  320. mutable double debug_redraw_time = 0;
  321. #endif
  322. template <typename T>
  323. T *alloc_command() {
  324. T *command = nullptr;
  325. if (commands == nullptr) {
  326. // As the most common use case of canvas items is to
  327. // use only one command, the first is done with it's
  328. // own allocation. The rest of them use blocks.
  329. command = memnew(T);
  330. command->next = nullptr;
  331. commands = command;
  332. last_command = command;
  333. } else {
  334. //Subsequent commands go into a block.
  335. while (true) {
  336. if (unlikely(current_block == (uint32_t)blocks.size())) {
  337. // If we need more blocks, we allocate them
  338. // (they won't be freed until this CanvasItem is
  339. // deleted, though).
  340. CommandBlock cb;
  341. cb.memory = (uint8_t *)memalloc(CommandBlock::MAX_SIZE);
  342. cb.usage = 0;
  343. blocks.push_back(cb);
  344. }
  345. CommandBlock *c = &blocks.write[current_block];
  346. size_t space_left = CommandBlock::MAX_SIZE - c->usage;
  347. if (space_left < sizeof(T)) {
  348. current_block++;
  349. continue;
  350. }
  351. //allocate block and add to the linked list
  352. void *memory = c->memory + c->usage;
  353. command = memnew_placement(memory, T);
  354. command->next = nullptr;
  355. last_command->next = command;
  356. last_command = command;
  357. c->usage += sizeof(T);
  358. break;
  359. }
  360. }
  361. rect_dirty = true;
  362. return command;
  363. }
  364. void clear() {
  365. // The first one is always allocated on heap
  366. // the rest go in the blocks
  367. Command *c = commands;
  368. while (c) {
  369. Command *n = c->next;
  370. if (c == commands) {
  371. memdelete(commands);
  372. commands = nullptr;
  373. } else {
  374. c->~Command();
  375. }
  376. c = n;
  377. }
  378. {
  379. uint32_t cbc = MIN((current_block + 1), (uint32_t)blocks.size());
  380. CommandBlock *blockptr = blocks.ptrw();
  381. for (uint32_t i = 0; i < cbc; i++) {
  382. blockptr[i].usage = 0;
  383. }
  384. }
  385. last_command = nullptr;
  386. commands = nullptr;
  387. current_block = 0;
  388. clip = false;
  389. rect_dirty = true;
  390. final_clip_owner = nullptr;
  391. material_owner = nullptr;
  392. light_masked = false;
  393. }
  394. RS::CanvasItemTextureFilter texture_filter;
  395. RS::CanvasItemTextureRepeat texture_repeat;
  396. Item() {
  397. commands = nullptr;
  398. last_command = nullptr;
  399. current_block = 0;
  400. light_mask = 1;
  401. vp_render = nullptr;
  402. next = nullptr;
  403. final_clip_owner = nullptr;
  404. canvas_group_owner = nullptr;
  405. clip = false;
  406. final_modulate = Color(1, 1, 1, 1);
  407. visible = true;
  408. rect_dirty = true;
  409. custom_rect = false;
  410. behind = false;
  411. material_owner = nullptr;
  412. copy_back_buffer = nullptr;
  413. distance_field = false;
  414. light_masked = false;
  415. update_when_visible = false;
  416. z_final = 0;
  417. texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  418. texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  419. repeat_source = false;
  420. on_interpolate_transform_list = false;
  421. interpolated = true;
  422. }
  423. virtual ~Item() {
  424. clear();
  425. for (int i = 0; i < blocks.size(); i++) {
  426. memfree(blocks[i].memory);
  427. }
  428. if (copy_back_buffer) {
  429. memdelete(copy_back_buffer);
  430. }
  431. }
  432. };
  433. 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;
  434. struct LightOccluderInstance {
  435. bool enabled : 1;
  436. bool on_interpolate_transform_list : 1;
  437. bool interpolated : 1;
  438. RID canvas;
  439. RID polygon;
  440. RID occluder;
  441. Rect2 aabb_cache;
  442. Transform2D xform_curr;
  443. Transform2D xform_prev;
  444. Transform2D xform_cache;
  445. int light_mask;
  446. bool sdf_collision;
  447. RS::CanvasOccluderPolygonCullMode cull_cache;
  448. LightOccluderInstance *next = nullptr;
  449. LightOccluderInstance() {
  450. enabled = true;
  451. on_interpolate_transform_list = false;
  452. interpolated = false;
  453. sdf_collision = false;
  454. next = nullptr;
  455. light_mask = 1;
  456. cull_cache = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  457. }
  458. };
  459. virtual RID light_create() = 0;
  460. virtual void light_set_texture(RID p_rid, RID p_texture) = 0;
  461. virtual void light_set_use_shadow(RID p_rid, bool p_enable) = 0;
  462. 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;
  463. 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;
  464. virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) = 0;
  465. virtual RID occluder_polygon_create() = 0;
  466. virtual void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) = 0;
  467. virtual void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) = 0;
  468. virtual void set_shadow_texture_size(int p_size) = 0;
  469. virtual bool free(RID p_rid) = 0;
  470. virtual void update() = 0;
  471. virtual void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) = 0;
  472. RendererCanvasRender() {
  473. ERR_FAIL_COND_MSG(singleton != nullptr, "A RendererCanvasRender singleton already exists.");
  474. singleton = this;
  475. }
  476. virtual ~RendererCanvasRender() {
  477. singleton = nullptr;
  478. }
  479. };
  480. #endif // RENDERER_CANVAS_RENDER_H