renderer_canvas_render.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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_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. bool use_canvas_group = false;
  275. int light_mask;
  276. int z_final;
  277. mutable bool custom_rect;
  278. mutable bool rect_dirty;
  279. mutable Rect2 rect;
  280. RID material;
  281. RID skeleton;
  282. Item *next = nullptr;
  283. struct CopyBackBuffer {
  284. Rect2 rect;
  285. Rect2 screen_rect;
  286. bool full;
  287. };
  288. CopyBackBuffer *copy_back_buffer = nullptr;
  289. Color final_modulate;
  290. Transform2D final_transform;
  291. Rect2 final_clip_rect;
  292. Item *final_clip_owner = nullptr;
  293. Item *material_owner = nullptr;
  294. Item *canvas_group_owner = nullptr;
  295. ViewportRender *vp_render = nullptr;
  296. bool distance_field;
  297. bool light_masked;
  298. Rect2 global_rect_cache;
  299. const Rect2 &get_rect() const;
  300. Command *commands = nullptr;
  301. Command *last_command = nullptr;
  302. Vector<CommandBlock> blocks;
  303. uint32_t current_block;
  304. #ifdef DEBUG_ENABLED
  305. mutable double debug_redraw_time = 0;
  306. #endif
  307. template <class T>
  308. T *alloc_command() {
  309. T *command = nullptr;
  310. if (commands == nullptr) {
  311. // As the most common use case of canvas items is to
  312. // use only one command, the first is done with it's
  313. // own allocation. The rest of them use blocks.
  314. command = memnew(T);
  315. command->next = nullptr;
  316. commands = command;
  317. last_command = command;
  318. } else {
  319. //Subsequent commands go into a block.
  320. while (true) {
  321. if (unlikely(current_block == (uint32_t)blocks.size())) {
  322. // If we need more blocks, we allocate them
  323. // (they won't be freed until this CanvasItem is
  324. // deleted, though).
  325. CommandBlock cb;
  326. cb.memory = (uint8_t *)memalloc(CommandBlock::MAX_SIZE);
  327. cb.usage = 0;
  328. blocks.push_back(cb);
  329. }
  330. CommandBlock *c = &blocks.write[current_block];
  331. size_t space_left = CommandBlock::MAX_SIZE - c->usage;
  332. if (space_left < sizeof(T)) {
  333. current_block++;
  334. continue;
  335. }
  336. //allocate block and add to the linked list
  337. void *memory = c->memory + c->usage;
  338. command = memnew_placement(memory, T);
  339. command->next = nullptr;
  340. last_command->next = command;
  341. last_command = command;
  342. c->usage += sizeof(T);
  343. break;
  344. }
  345. }
  346. rect_dirty = true;
  347. return command;
  348. }
  349. void clear() {
  350. // The first one is always allocated on heap
  351. // the rest go in the blocks
  352. Command *c = commands;
  353. while (c) {
  354. Command *n = c->next;
  355. if (c == commands) {
  356. memdelete(commands);
  357. commands = nullptr;
  358. } else {
  359. c->~Command();
  360. }
  361. c = n;
  362. }
  363. {
  364. uint32_t cbc = MIN((current_block + 1), (uint32_t)blocks.size());
  365. CommandBlock *blockptr = blocks.ptrw();
  366. for (uint32_t i = 0; i < cbc; i++) {
  367. blockptr[i].usage = 0;
  368. }
  369. }
  370. last_command = nullptr;
  371. commands = nullptr;
  372. current_block = 0;
  373. clip = false;
  374. rect_dirty = true;
  375. final_clip_owner = nullptr;
  376. material_owner = nullptr;
  377. light_masked = false;
  378. }
  379. RS::CanvasItemTextureFilter texture_filter;
  380. RS::CanvasItemTextureRepeat texture_repeat;
  381. Item() {
  382. commands = nullptr;
  383. last_command = nullptr;
  384. current_block = 0;
  385. light_mask = 1;
  386. vp_render = nullptr;
  387. next = nullptr;
  388. final_clip_owner = nullptr;
  389. canvas_group_owner = nullptr;
  390. clip = false;
  391. final_modulate = Color(1, 1, 1, 1);
  392. visible = true;
  393. rect_dirty = true;
  394. custom_rect = false;
  395. behind = false;
  396. material_owner = nullptr;
  397. copy_back_buffer = nullptr;
  398. distance_field = false;
  399. light_masked = false;
  400. update_when_visible = false;
  401. z_final = 0;
  402. texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  403. texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  404. }
  405. virtual ~Item() {
  406. clear();
  407. for (int i = 0; i < blocks.size(); i++) {
  408. memfree(blocks[i].memory);
  409. }
  410. if (copy_back_buffer) {
  411. memdelete(copy_back_buffer);
  412. }
  413. }
  414. };
  415. 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;
  416. struct LightOccluderInstance {
  417. bool enabled;
  418. RID canvas;
  419. RID polygon;
  420. RID occluder;
  421. Rect2 aabb_cache;
  422. Transform2D xform;
  423. Transform2D xform_cache;
  424. int light_mask;
  425. bool sdf_collision;
  426. RS::CanvasOccluderPolygonCullMode cull_cache;
  427. LightOccluderInstance *next = nullptr;
  428. LightOccluderInstance() {
  429. enabled = true;
  430. sdf_collision = false;
  431. next = nullptr;
  432. light_mask = 1;
  433. cull_cache = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  434. }
  435. };
  436. virtual RID light_create() = 0;
  437. virtual void light_set_texture(RID p_rid, RID p_texture) = 0;
  438. virtual void light_set_use_shadow(RID p_rid, bool p_enable) = 0;
  439. 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;
  440. 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;
  441. virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) = 0;
  442. virtual RID occluder_polygon_create() = 0;
  443. virtual void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) = 0;
  444. virtual void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) = 0;
  445. virtual void set_shadow_texture_size(int p_size) = 0;
  446. virtual bool free(RID p_rid) = 0;
  447. virtual void update() = 0;
  448. virtual void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) = 0;
  449. RendererCanvasRender() { singleton = this; }
  450. virtual ~RendererCanvasRender() {}
  451. };
  452. #endif // RENDERER_CANVAS_RENDER_H