renderer_canvas_render.h 16 KB

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