renderer_canvas_render.h 14 KB

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