renderer_canvas_render.h 14 KB

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