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 RENDERINGSERVERCANVASRENDER_H
  31. #define RENDERINGSERVERCANVASRENDER_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. };
  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 = nullptr;
  78. Light *filter_next_ptr = nullptr;
  79. Light *next_ptr = nullptr;
  80. Light *directional_next_ptr = nullptr;
  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 = nullptr;
  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 = nullptr;
  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. };
  225. struct CommandMultiMesh : public Command {
  226. RID multimesh;
  227. RID texture;
  228. CommandMultiMesh() { type = TYPE_MULTIMESH; }
  229. };
  230. struct CommandParticles : public Command {
  231. RID particles;
  232. RID texture;
  233. CommandParticles() { type = TYPE_PARTICLES; }
  234. };
  235. struct CommandTransform : public Command {
  236. Transform2D xform;
  237. CommandTransform() { type = TYPE_TRANSFORM; }
  238. };
  239. struct CommandClipIgnore : public Command {
  240. bool ignore;
  241. CommandClipIgnore() {
  242. type = TYPE_CLIP_IGNORE;
  243. ignore = false;
  244. }
  245. };
  246. struct CommandAnimationSlice : public Command {
  247. double animation_length = 0;
  248. double slice_begin = 0;
  249. double slice_end = 0;
  250. double offset = 0;
  251. CommandAnimationSlice() {
  252. type = TYPE_ANIMATION_SLICE;
  253. }
  254. };
  255. struct ViewportRender {
  256. RenderingServer *owner = nullptr;
  257. void *udata = nullptr;
  258. Rect2 rect;
  259. };
  260. Transform2D xform;
  261. bool clip;
  262. bool visible;
  263. bool behind;
  264. bool update_when_visible;
  265. struct CanvasGroup {
  266. RS::CanvasGroupMode mode;
  267. bool fit_empty;
  268. float fit_margin;
  269. bool blur_mipmaps;
  270. float clear_margin;
  271. };
  272. CanvasGroup *canvas_group = nullptr;
  273. int light_mask;
  274. int z_final;
  275. mutable bool custom_rect;
  276. mutable bool rect_dirty;
  277. mutable Rect2 rect;
  278. RID material;
  279. RID skeleton;
  280. Item *next = nullptr;
  281. struct CopyBackBuffer {
  282. Rect2 rect;
  283. Rect2 screen_rect;
  284. bool full;
  285. };
  286. CopyBackBuffer *copy_back_buffer = nullptr;
  287. Color final_modulate;
  288. Transform2D final_transform;
  289. Rect2 final_clip_rect;
  290. Item *final_clip_owner = nullptr;
  291. Item *material_owner = nullptr;
  292. Item *canvas_group_owner = nullptr;
  293. ViewportRender *vp_render = nullptr;
  294. bool distance_field;
  295. bool light_masked;
  296. Rect2 global_rect_cache;
  297. const Rect2 &get_rect() const;
  298. Command *commands = nullptr;
  299. Command *last_command = nullptr;
  300. Vector<CommandBlock> blocks;
  301. uint32_t current_block;
  302. template <class T>
  303. T *alloc_command() {
  304. T *command = nullptr;
  305. if (commands == nullptr) {
  306. // As the most common use case of canvas items is to
  307. // use only one command, the first is done with it's
  308. // own allocation. The rest of them use blocks.
  309. command = memnew(T);
  310. command->next = nullptr;
  311. commands = command;
  312. last_command = command;
  313. } else {
  314. //Subsequent commands go into a block.
  315. while (true) {
  316. if (unlikely(current_block == (uint32_t)blocks.size())) {
  317. // If we need more blocks, we allocate them
  318. // (they won't be freed until this CanvasItem is
  319. // deleted, though).
  320. CommandBlock cb;
  321. cb.memory = (uint8_t *)memalloc(CommandBlock::MAX_SIZE);
  322. cb.usage = 0;
  323. blocks.push_back(cb);
  324. }
  325. CommandBlock *c = &blocks.write[current_block];
  326. size_t space_left = CommandBlock::MAX_SIZE - c->usage;
  327. if (space_left < sizeof(T)) {
  328. current_block++;
  329. continue;
  330. }
  331. //allocate block and add to the linked list
  332. void *memory = c->memory + c->usage;
  333. command = memnew_placement(memory, T);
  334. command->next = nullptr;
  335. last_command->next = command;
  336. last_command = command;
  337. c->usage += sizeof(T);
  338. break;
  339. }
  340. }
  341. rect_dirty = true;
  342. return command;
  343. }
  344. void clear() {
  345. // The first one is always allocated on heap
  346. // the rest go in the blocks
  347. Command *c = commands;
  348. while (c) {
  349. Command *n = c->next;
  350. if (c == commands) {
  351. memdelete(commands);
  352. commands = nullptr;
  353. } else {
  354. c->~Command();
  355. }
  356. c = n;
  357. }
  358. {
  359. uint32_t cbc = MIN((current_block + 1), (uint32_t)blocks.size());
  360. CommandBlock *blockptr = blocks.ptrw();
  361. for (uint32_t i = 0; i < cbc; i++) {
  362. blockptr[i].usage = 0;
  363. }
  364. }
  365. last_command = nullptr;
  366. commands = nullptr;
  367. current_block = 0;
  368. clip = false;
  369. rect_dirty = true;
  370. final_clip_owner = nullptr;
  371. material_owner = nullptr;
  372. light_masked = false;
  373. }
  374. RS::CanvasItemTextureFilter texture_filter;
  375. RS::CanvasItemTextureRepeat texture_repeat;
  376. Item() {
  377. commands = nullptr;
  378. last_command = nullptr;
  379. current_block = 0;
  380. light_mask = 1;
  381. vp_render = nullptr;
  382. next = nullptr;
  383. final_clip_owner = nullptr;
  384. canvas_group_owner = nullptr;
  385. clip = false;
  386. final_modulate = Color(1, 1, 1, 1);
  387. visible = true;
  388. rect_dirty = true;
  389. custom_rect = false;
  390. behind = false;
  391. material_owner = nullptr;
  392. copy_back_buffer = nullptr;
  393. distance_field = false;
  394. light_masked = false;
  395. update_when_visible = false;
  396. z_final = 0;
  397. texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  398. texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  399. }
  400. virtual ~Item() {
  401. clear();
  402. for (int i = 0; i < blocks.size(); i++) {
  403. memfree(blocks[i].memory);
  404. }
  405. if (copy_back_buffer) {
  406. memdelete(copy_back_buffer);
  407. }
  408. }
  409. };
  410. 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;
  411. virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) = 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 // RENDERINGSERVERCANVASRENDER_H