renderer_canvas_render.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. typedef uint64_t PolygonID;
  121. 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;
  122. virtual void free_polygon(PolygonID p_polygon) = 0;
  123. //also easier to wrap to avoid mistakes
  124. struct Polygon {
  125. PolygonID polygon_id;
  126. Rect2 rect_cache;
  127. _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>()) {
  128. ERR_FAIL_COND(polygon_id != 0);
  129. {
  130. uint32_t pc = p_points.size();
  131. const Vector2 *v2 = p_points.ptr();
  132. rect_cache.position = *v2;
  133. for (uint32_t i = 1; i < pc; i++) {
  134. rect_cache.expand_to(v2[i]);
  135. }
  136. }
  137. polygon_id = singleton->request_polygon(p_indices, p_points, p_colors, p_uvs, p_bones, p_weights);
  138. }
  139. _FORCE_INLINE_ Polygon() { polygon_id = 0; }
  140. _FORCE_INLINE_ ~Polygon() {
  141. if (polygon_id) {
  142. singleton->free_polygon(polygon_id);
  143. }
  144. }
  145. };
  146. //item
  147. struct Item {
  148. //commands are allocated in blocks of 4k to improve performance
  149. //and cache coherence.
  150. //blocks always grow but never shrink.
  151. struct CommandBlock {
  152. enum {
  153. MAX_SIZE = 4096
  154. };
  155. uint32_t usage;
  156. uint8_t *memory = nullptr;
  157. };
  158. struct Command {
  159. enum Type {
  160. TYPE_RECT,
  161. TYPE_NINEPATCH,
  162. TYPE_POLYGON,
  163. TYPE_PRIMITIVE,
  164. TYPE_MESH,
  165. TYPE_MULTIMESH,
  166. TYPE_PARTICLES,
  167. TYPE_TRANSFORM,
  168. TYPE_CLIP_IGNORE,
  169. TYPE_ANIMATION_SLICE,
  170. };
  171. Command *next = nullptr;
  172. Type type;
  173. virtual ~Command() {}
  174. };
  175. struct CommandRect : public Command {
  176. Rect2 rect;
  177. Color modulate;
  178. Rect2 source;
  179. uint16_t flags;
  180. float outline;
  181. float px_range;
  182. RID texture;
  183. CommandRect() {
  184. flags = 0;
  185. outline = 0;
  186. px_range = 1;
  187. type = TYPE_RECT;
  188. }
  189. };
  190. struct CommandNinePatch : public Command {
  191. Rect2 rect;
  192. Rect2 source;
  193. float margin[4];
  194. bool draw_center;
  195. Color color;
  196. RS::NinePatchAxisMode axis_x;
  197. RS::NinePatchAxisMode axis_y;
  198. RID texture;
  199. CommandNinePatch() {
  200. draw_center = true;
  201. type = TYPE_NINEPATCH;
  202. }
  203. };
  204. struct CommandPolygon : public Command {
  205. RS::PrimitiveType primitive;
  206. Polygon polygon;
  207. RID texture;
  208. CommandPolygon() {
  209. type = TYPE_POLYGON;
  210. }
  211. };
  212. struct CommandPrimitive : public Command {
  213. uint32_t point_count;
  214. Vector2 points[4];
  215. Vector2 uvs[4];
  216. Color colors[4];
  217. RID texture;
  218. CommandPrimitive() {
  219. type = TYPE_PRIMITIVE;
  220. }
  221. };
  222. struct CommandMesh : public Command {
  223. RID mesh;
  224. Transform2D transform;
  225. Color modulate;
  226. RID mesh_instance;
  227. RID texture;
  228. CommandMesh() { type = TYPE_MESH; }
  229. ~CommandMesh();
  230. };
  231. struct CommandMultiMesh : public Command {
  232. RID multimesh;
  233. RID texture;
  234. CommandMultiMesh() { type = TYPE_MULTIMESH; }
  235. };
  236. struct CommandParticles : public Command {
  237. RID particles;
  238. RID texture;
  239. CommandParticles() { type = TYPE_PARTICLES; }
  240. };
  241. struct CommandTransform : public Command {
  242. Transform2D xform;
  243. CommandTransform() { type = TYPE_TRANSFORM; }
  244. };
  245. struct CommandClipIgnore : public Command {
  246. bool ignore;
  247. CommandClipIgnore() {
  248. type = TYPE_CLIP_IGNORE;
  249. ignore = false;
  250. }
  251. };
  252. struct CommandAnimationSlice : public Command {
  253. double animation_length = 0;
  254. double slice_begin = 0;
  255. double slice_end = 0;
  256. double offset = 0;
  257. CommandAnimationSlice() {
  258. type = TYPE_ANIMATION_SLICE;
  259. }
  260. };
  261. struct ViewportRender {
  262. RenderingServer *owner = nullptr;
  263. void *udata = nullptr;
  264. Rect2 rect;
  265. };
  266. // For interpolation we store the current local xform,
  267. // and the previous xform from the previous tick.
  268. Transform2D xform_curr;
  269. Transform2D xform_prev;
  270. bool clip : 1;
  271. bool visible : 1;
  272. bool behind : 1;
  273. bool update_when_visible : 1;
  274. bool on_interpolate_transform_list : 1;
  275. bool interpolated : 1;
  276. bool use_identity_transform : 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. int32_t instance_allocated_shader_uniforms_offset = -1;
  294. Item *next = nullptr;
  295. struct CopyBackBuffer {
  296. Rect2 rect;
  297. Rect2 screen_rect;
  298. bool full;
  299. };
  300. CopyBackBuffer *copy_back_buffer = nullptr;
  301. Color final_modulate;
  302. Transform2D final_transform;
  303. Rect2 final_clip_rect;
  304. Item *final_clip_owner = nullptr;
  305. Item *material_owner = nullptr;
  306. Item *canvas_group_owner = nullptr;
  307. ViewportRender *vp_render = nullptr;
  308. bool distance_field;
  309. bool light_masked;
  310. bool repeat_source;
  311. Point2 repeat_size;
  312. int repeat_times = 1;
  313. Item *repeat_source_item = nullptr;
  314. Rect2 global_rect_cache;
  315. const Rect2 &get_rect() const;
  316. Command *commands = nullptr;
  317. Command *last_command = nullptr;
  318. Vector<CommandBlock> blocks;
  319. uint32_t current_block;
  320. #ifdef DEBUG_ENABLED
  321. mutable double debug_redraw_time = 0;
  322. #endif
  323. template <typename T>
  324. T *alloc_command() {
  325. T *command = nullptr;
  326. if (commands == nullptr) {
  327. // As the most common use case of canvas items is to
  328. // use only one command, the first is done with it's
  329. // own allocation. The rest of them use blocks.
  330. command = memnew(T);
  331. command->next = nullptr;
  332. commands = command;
  333. last_command = command;
  334. } else {
  335. //Subsequent commands go into a block.
  336. while (true) {
  337. if (unlikely(current_block == (uint32_t)blocks.size())) {
  338. // If we need more blocks, we allocate them
  339. // (they won't be freed until this CanvasItem is
  340. // deleted, though).
  341. CommandBlock cb;
  342. cb.memory = (uint8_t *)memalloc(CommandBlock::MAX_SIZE);
  343. cb.usage = 0;
  344. blocks.push_back(cb);
  345. }
  346. CommandBlock *c = &blocks.write[current_block];
  347. size_t space_left = CommandBlock::MAX_SIZE - c->usage;
  348. if (space_left < sizeof(T)) {
  349. current_block++;
  350. continue;
  351. }
  352. //allocate block and add to the linked list
  353. void *memory = c->memory + c->usage;
  354. command = memnew_placement(memory, T);
  355. command->next = nullptr;
  356. last_command->next = command;
  357. last_command = command;
  358. c->usage += sizeof(T);
  359. break;
  360. }
  361. }
  362. rect_dirty = true;
  363. return command;
  364. }
  365. void clear() {
  366. // The first one is always allocated on heap
  367. // the rest go in the blocks
  368. Command *c = commands;
  369. while (c) {
  370. Command *n = c->next;
  371. if (c == commands) {
  372. memdelete(commands);
  373. commands = nullptr;
  374. } else {
  375. c->~Command();
  376. }
  377. c = n;
  378. }
  379. {
  380. uint32_t cbc = MIN((current_block + 1), (uint32_t)blocks.size());
  381. CommandBlock *blockptr = blocks.ptrw();
  382. for (uint32_t i = 0; i < cbc; i++) {
  383. blockptr[i].usage = 0;
  384. }
  385. }
  386. last_command = nullptr;
  387. commands = nullptr;
  388. current_block = 0;
  389. clip = false;
  390. rect_dirty = true;
  391. final_clip_owner = nullptr;
  392. material_owner = nullptr;
  393. light_masked = false;
  394. }
  395. RS::CanvasItemTextureFilter texture_filter;
  396. RS::CanvasItemTextureRepeat texture_repeat;
  397. Item() {
  398. commands = nullptr;
  399. last_command = nullptr;
  400. current_block = 0;
  401. light_mask = 1;
  402. vp_render = nullptr;
  403. next = nullptr;
  404. final_clip_owner = nullptr;
  405. canvas_group_owner = nullptr;
  406. clip = false;
  407. final_modulate = Color(1, 1, 1, 1);
  408. visible = true;
  409. rect_dirty = true;
  410. custom_rect = false;
  411. behind = false;
  412. material_owner = nullptr;
  413. copy_back_buffer = nullptr;
  414. distance_field = false;
  415. light_masked = false;
  416. update_when_visible = false;
  417. z_final = 0;
  418. texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  419. texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  420. repeat_source = false;
  421. on_interpolate_transform_list = false;
  422. interpolated = true;
  423. use_identity_transform = false;
  424. }
  425. virtual ~Item() {
  426. clear();
  427. for (int i = 0; i < blocks.size(); i++) {
  428. memfree(blocks[i].memory);
  429. }
  430. if (copy_back_buffer) {
  431. memdelete(copy_back_buffer);
  432. }
  433. }
  434. };
  435. 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;
  436. struct LightOccluderInstance {
  437. bool enabled : 1;
  438. bool on_interpolate_transform_list : 1;
  439. bool interpolated : 1;
  440. RID canvas;
  441. RID polygon;
  442. RID occluder;
  443. Rect2 aabb_cache;
  444. Transform2D xform_curr;
  445. Transform2D xform_prev;
  446. Transform2D xform_cache;
  447. int light_mask;
  448. bool sdf_collision;
  449. RS::CanvasOccluderPolygonCullMode cull_cache;
  450. LightOccluderInstance *next = nullptr;
  451. LightOccluderInstance() {
  452. enabled = true;
  453. on_interpolate_transform_list = false;
  454. interpolated = false;
  455. sdf_collision = false;
  456. next = nullptr;
  457. light_mask = 1;
  458. cull_cache = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  459. }
  460. };
  461. virtual RID light_create() = 0;
  462. virtual void light_set_texture(RID p_rid, RID p_texture) = 0;
  463. virtual void light_set_use_shadow(RID p_rid, bool p_enable) = 0;
  464. 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, const Rect2 &p_light_rect) = 0;
  465. 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;
  466. virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) = 0;
  467. virtual RID occluder_polygon_create() = 0;
  468. virtual void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) = 0;
  469. virtual void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) = 0;
  470. virtual void set_shadow_texture_size(int p_size) = 0;
  471. virtual bool free(RID p_rid) = 0;
  472. virtual void update() = 0;
  473. virtual void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) = 0;
  474. virtual uint32_t get_pipeline_compilations(RS::PipelineSource p_source) = 0;
  475. RendererCanvasRender() {
  476. ERR_FAIL_COND_MSG(singleton != nullptr, "A RendererCanvasRender singleton already exists.");
  477. singleton = this;
  478. }
  479. virtual ~RendererCanvasRender() {
  480. singleton = nullptr;
  481. }
  482. };
  483. #endif // RENDERER_CANVAS_RENDER_H