renderer_canvas_render.h 16 KB

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