renderer_canvas_render_rd.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /**************************************************************************/
  2. /* renderer_canvas_render_rd.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_RD_H
  31. #define RENDERER_CANVAS_RENDER_RD_H
  32. #include "core/templates/lru.h"
  33. #include "servers/rendering/renderer_canvas_render.h"
  34. #include "servers/rendering/renderer_compositor.h"
  35. #include "servers/rendering/renderer_rd/pipeline_cache_rd.h"
  36. #include "servers/rendering/renderer_rd/pipeline_hash_map_rd.h"
  37. #include "servers/rendering/renderer_rd/shaders/canvas.glsl.gen.h"
  38. #include "servers/rendering/renderer_rd/shaders/canvas_occlusion.glsl.gen.h"
  39. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  40. #include "servers/rendering/rendering_device.h"
  41. #include "servers/rendering/shader_compiler.h"
  42. class RendererCanvasRenderRD : public RendererCanvasRender {
  43. enum {
  44. BASE_UNIFORM_SET = 0,
  45. MATERIAL_UNIFORM_SET = 1,
  46. TRANSFORMS_UNIFORM_SET = 2,
  47. BATCH_UNIFORM_SET = 3,
  48. };
  49. const int SAMPLERS_BINDING_FIRST_INDEX = 10;
  50. // The size of the ring buffer to store GPU buffers. Triple-buffering the max expected frames in flight.
  51. static const uint32_t BATCH_DATA_BUFFER_COUNT = 3;
  52. enum ShaderVariant {
  53. SHADER_VARIANT_QUAD,
  54. SHADER_VARIANT_NINEPATCH,
  55. SHADER_VARIANT_PRIMITIVE,
  56. SHADER_VARIANT_PRIMITIVE_POINTS,
  57. SHADER_VARIANT_ATTRIBUTES,
  58. SHADER_VARIANT_ATTRIBUTES_POINTS,
  59. SHADER_VARIANT_MAX
  60. };
  61. enum {
  62. INSTANCE_FLAGS_LIGHT_COUNT_SHIFT = 0, // 4 bits for light count.
  63. INSTANCE_FLAGS_CLIP_RECT_UV = (1 << 4),
  64. INSTANCE_FLAGS_TRANSPOSE_RECT = (1 << 5),
  65. INSTANCE_FLAGS_USE_MSDF = (1 << 6),
  66. INSTANCE_FLAGS_USE_LCD = (1 << 7),
  67. INSTANCE_FLAGS_NINEPACH_DRAW_CENTER = (1 << 8),
  68. INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT = 9,
  69. INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT = 11,
  70. INSTANCE_FLAGS_SHADOW_MASKED_SHIFT = 13, // 16 bits.
  71. };
  72. enum {
  73. BATCH_FLAGS_INSTANCING_MASK = 0x7F,
  74. BATCH_FLAGS_INSTANCING_HAS_COLORS = (1 << 7),
  75. BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8),
  76. BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 9),
  77. BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 10),
  78. };
  79. enum {
  80. CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR = (1 << 0),
  81. };
  82. enum {
  83. LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF,
  84. LIGHT_FLAGS_BLEND_SHIFT = 16,
  85. LIGHT_FLAGS_BLEND_MASK = (3 << 16),
  86. LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16),
  87. LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16),
  88. LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16),
  89. LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16),
  90. LIGHT_FLAGS_HAS_SHADOW = (1 << 20),
  91. LIGHT_FLAGS_FILTER_SHIFT = 22
  92. };
  93. enum {
  94. MAX_RENDER_ITEMS = 256 * 1024,
  95. MAX_LIGHT_TEXTURES = 1024,
  96. MAX_LIGHTS_PER_ITEM = 16,
  97. DEFAULT_MAX_LIGHTS_PER_RENDER = 256
  98. };
  99. /****************/
  100. /**** SHADER ****/
  101. /****************/
  102. struct ShaderSpecialization {
  103. union {
  104. struct {
  105. uint32_t use_lighting : 1;
  106. };
  107. uint32_t packed_0;
  108. };
  109. };
  110. struct PipelineKey {
  111. ShaderVariant variant = SHADER_VARIANT_MAX;
  112. RD::FramebufferFormatID framebuffer_format_id = RD::INVALID_FORMAT_ID;
  113. RD::VertexFormatID vertex_format_id = RD::INVALID_ID;
  114. RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_MAX;
  115. ShaderSpecialization shader_specialization = {};
  116. uint32_t lcd_blend = 0;
  117. uint32_t ubershader = 0;
  118. uint32_t hash() const {
  119. uint32_t h = hash_murmur3_one_32(variant);
  120. h = hash_murmur3_one_32(framebuffer_format_id, h);
  121. h = hash_murmur3_one_32(vertex_format_id, h);
  122. h = hash_murmur3_one_32(render_primitive, h);
  123. h = hash_murmur3_one_32(shader_specialization.packed_0, h);
  124. h = hash_murmur3_one_32(lcd_blend, h);
  125. h = hash_murmur3_one_32(ubershader, h);
  126. return hash_fmix32(h);
  127. }
  128. };
  129. struct CanvasShaderData : public RendererRD::MaterialStorage::ShaderData {
  130. Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
  131. int blend_mode = 0;
  132. Vector<uint32_t> ubo_offsets;
  133. uint32_t ubo_size = 0;
  134. String code;
  135. RID version;
  136. PipelineHashMapRD<PipelineKey, CanvasShaderData, void (CanvasShaderData::*)(PipelineKey)> pipeline_hash_map;
  137. static const uint32_t VERTEX_INPUT_MASKS_SIZE = SHADER_VARIANT_MAX * 2;
  138. std::atomic<uint64_t> vertex_input_masks[VERTEX_INPUT_MASKS_SIZE] = {};
  139. bool uses_screen_texture = false;
  140. bool uses_screen_texture_mipmaps = false;
  141. bool uses_sdf = false;
  142. bool uses_time = false;
  143. void _clear_vertex_input_mask_cache();
  144. void _create_pipeline(PipelineKey p_pipeline_key);
  145. virtual void set_code(const String &p_Code);
  146. virtual bool is_animated() const;
  147. virtual bool casts_shadows() const;
  148. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  149. RID get_shader(ShaderVariant p_shader_variant, bool p_ubershader) const;
  150. uint64_t get_vertex_input_mask(ShaderVariant p_shader_variant, bool p_ubershader);
  151. bool is_valid() const;
  152. CanvasShaderData();
  153. virtual ~CanvasShaderData();
  154. };
  155. struct {
  156. // Data must be guaranteed to be erased before the rest on the destructor.
  157. CanvasShaderData *default_version_data = nullptr;
  158. CanvasShaderRD canvas_shader;
  159. RID default_version_rd_shader;
  160. RID quad_index_buffer;
  161. RID quad_index_array;
  162. ShaderCompiler compiler;
  163. uint32_t pipeline_compilations[RS::PIPELINE_SOURCE_MAX] = {};
  164. Mutex mutex;
  165. } shader;
  166. RendererRD::MaterialStorage::ShaderData *_create_shader_func();
  167. static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() {
  168. return static_cast<RendererCanvasRenderRD *>(singleton)->_create_shader_func();
  169. }
  170. struct CanvasMaterialData : public RendererRD::MaterialStorage::MaterialData {
  171. CanvasShaderData *shader_data = nullptr;
  172. RID uniform_set;
  173. RID uniform_set_srgb;
  174. virtual void set_render_priority(int p_priority) {}
  175. virtual void set_next_pass(RID p_pass) {}
  176. virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  177. virtual ~CanvasMaterialData();
  178. };
  179. RendererRD::MaterialStorage::MaterialData *_create_material_func(CanvasShaderData *p_shader);
  180. static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
  181. return static_cast<RendererCanvasRenderRD *>(singleton)->_create_material_func(static_cast<CanvasShaderData *>(p_shader));
  182. }
  183. /**************************/
  184. /**** CANVAS TEXTURES *****/
  185. /**************************/
  186. struct {
  187. RS::CanvasItemTextureFilter default_filter;
  188. RS::CanvasItemTextureRepeat default_repeat;
  189. } default_samplers;
  190. /******************/
  191. /**** POLYGONS ****/
  192. /******************/
  193. struct PolygonBuffers {
  194. RD::VertexFormatID vertex_format_id;
  195. RID vertex_buffer;
  196. RID vertex_array;
  197. RID index_buffer;
  198. RID indices;
  199. uint32_t primitive_count = 0;
  200. };
  201. struct {
  202. HashMap<PolygonID, PolygonBuffers> polygons;
  203. PolygonID last_id;
  204. } polygon_buffers;
  205. /********************/
  206. /**** PRIMITIVES ****/
  207. /********************/
  208. struct {
  209. RID index_array[4];
  210. } primitive_arrays;
  211. /*******************/
  212. /**** MATERIALS ****/
  213. /*******************/
  214. /******************/
  215. /**** LIGHTING ****/
  216. /******************/
  217. struct CanvasLight {
  218. RID texture;
  219. struct {
  220. bool enabled = false;
  221. float z_far;
  222. float y_offset;
  223. Transform2D directional_xform;
  224. } shadow;
  225. };
  226. RID_Owner<CanvasLight> canvas_light_owner;
  227. struct PositionalShadowRenderPushConstant {
  228. float modelview[8];
  229. float rotation[4];
  230. float direction[2];
  231. float z_far;
  232. uint32_t pad;
  233. float z_near;
  234. uint32_t cull_mode;
  235. float pad2[2];
  236. };
  237. struct ShadowRenderPushConstant {
  238. float projection[16];
  239. float modelview[8];
  240. float direction[2];
  241. float z_far;
  242. uint32_t cull_mode;
  243. };
  244. struct OccluderPolygon {
  245. RS::CanvasOccluderPolygonCullMode cull_mode;
  246. int line_point_count;
  247. RID vertex_buffer;
  248. RID vertex_array;
  249. RID index_buffer;
  250. RID index_array;
  251. int sdf_point_count;
  252. int sdf_index_count;
  253. RID sdf_vertex_buffer;
  254. RID sdf_vertex_array;
  255. RID sdf_index_buffer;
  256. RID sdf_index_array;
  257. bool sdf_is_lines;
  258. };
  259. struct LightUniform {
  260. float matrix[8]; //light to texture coordinate matrix
  261. float shadow_matrix[8]; //light to shadow coordinate matrix
  262. float color[4];
  263. uint8_t shadow_color[4];
  264. uint32_t flags; //index to light texture
  265. float shadow_pixel_size;
  266. float height;
  267. float position[2];
  268. float shadow_z_far_inv;
  269. float shadow_y_ofs;
  270. float atlas_rect[4];
  271. };
  272. RID_Owner<OccluderPolygon> occluder_polygon_owner;
  273. enum ShadowRenderMode {
  274. SHADOW_RENDER_MODE_DIRECTIONAL_SHADOW,
  275. SHADOW_RENDER_MODE_POSITIONAL_SHADOW,
  276. SHADOW_RENDER_MODE_SDF,
  277. };
  278. enum {
  279. SHADOW_RENDER_SDF_TRIANGLES,
  280. SHADOW_RENDER_SDF_LINES,
  281. };
  282. struct {
  283. CanvasOcclusionShaderRD shader;
  284. RID shader_version;
  285. RID render_pipelines[2];
  286. RID sdf_render_pipelines[2];
  287. RD::VertexFormatID vertex_format;
  288. RD::VertexFormatID sdf_vertex_format;
  289. RD::FramebufferFormatID framebuffer_format;
  290. RD::FramebufferFormatID sdf_framebuffer_format;
  291. } shadow_render;
  292. /***************/
  293. /**** STATE ****/
  294. /***************/
  295. //state that does not vary across rendering all items
  296. struct InstanceData {
  297. float world[6];
  298. uint32_t flags;
  299. uint32_t pad1;
  300. union {
  301. //rect
  302. struct {
  303. float modulation[4];
  304. union {
  305. float msdf[4];
  306. float ninepatch_margins[4];
  307. };
  308. float dst_rect[4];
  309. float src_rect[4];
  310. float pad[2];
  311. };
  312. //primitive
  313. struct {
  314. float points[6]; // vec2 points[3]
  315. float uvs[6]; // vec2 points[3]
  316. uint32_t colors[6]; // colors encoded as half
  317. };
  318. };
  319. float color_texture_pixel_size[2];
  320. uint32_t lights[4];
  321. };
  322. struct PushConstant {
  323. uint32_t base_instance_index;
  324. ShaderSpecialization shader_specialization;
  325. uint32_t specular_shininess;
  326. uint32_t batch_flags;
  327. };
  328. // TextureState is used to determine when a new batch is required due to a change of texture state.
  329. struct TextureState {
  330. static const uint32_t FILTER_SHIFT = 0;
  331. static const uint32_t FILTER_BITS = 3;
  332. static const uint32_t FILTER_MASK = (1 << FILTER_BITS) - 1;
  333. static const uint32_t REPEAT_SHIFT = FILTER_BITS;
  334. static const uint32_t REPEAT_BITS = 2;
  335. static const uint32_t REPEAT_MASK = (1 << REPEAT_BITS) - 1;
  336. static const uint32_t TEXTURE_IS_DATA_SHIFT = REPEAT_SHIFT + REPEAT_BITS;
  337. static const uint32_t TEXTURE_IS_DATA_BITS = 1;
  338. static const uint32_t TEXTURE_IS_DATA_MASK = (1 << TEXTURE_IS_DATA_BITS) - 1;
  339. static const uint32_t LINEAR_COLORS_SHIFT = TEXTURE_IS_DATA_SHIFT + TEXTURE_IS_DATA_BITS;
  340. static const uint32_t LINEAR_COLORS_BITS = 1;
  341. static const uint32_t LINEAR_COLORS_MASK = (1 << LINEAR_COLORS_BITS) - 1;
  342. RID texture;
  343. uint32_t other = 0;
  344. TextureState() {}
  345. TextureState(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, bool p_texture_is_data, bool p_use_linear_colors) {
  346. texture = p_texture;
  347. other = (((uint32_t)p_base_filter & FILTER_MASK) << FILTER_SHIFT) |
  348. (((uint32_t)p_base_repeat & REPEAT_MASK) << REPEAT_SHIFT) |
  349. (((uint32_t)p_texture_is_data & TEXTURE_IS_DATA_MASK) << TEXTURE_IS_DATA_SHIFT) |
  350. (((uint32_t)p_use_linear_colors & LINEAR_COLORS_MASK) << LINEAR_COLORS_SHIFT);
  351. }
  352. _ALWAYS_INLINE_ RS::CanvasItemTextureFilter texture_filter() const {
  353. return (RS::CanvasItemTextureFilter)((other >> FILTER_SHIFT) & FILTER_MASK);
  354. }
  355. _ALWAYS_INLINE_ RS::CanvasItemTextureRepeat texture_repeat() const {
  356. return (RS::CanvasItemTextureRepeat)((other >> REPEAT_SHIFT) & REPEAT_MASK);
  357. }
  358. _ALWAYS_INLINE_ bool linear_colors() const {
  359. return (other >> LINEAR_COLORS_SHIFT) & LINEAR_COLORS_MASK;
  360. }
  361. _ALWAYS_INLINE_ bool texture_is_data() const {
  362. return (other >> TEXTURE_IS_DATA_SHIFT) & TEXTURE_IS_DATA_MASK;
  363. }
  364. _ALWAYS_INLINE_ bool operator==(const TextureState &p_val) const {
  365. return (texture == p_val.texture) && (other == p_val.other);
  366. }
  367. _ALWAYS_INLINE_ bool operator!=(const TextureState &p_val) const {
  368. return (texture != p_val.texture) || (other != p_val.other);
  369. }
  370. _ALWAYS_INLINE_ bool is_valid() const { return texture.is_valid(); }
  371. _ALWAYS_INLINE_ bool is_null() const { return texture.is_null(); }
  372. uint32_t hash() const {
  373. uint32_t hash = hash_murmur3_one_64(texture.get_id());
  374. return hash_murmur3_one_32(other, hash);
  375. }
  376. };
  377. struct TextureInfo {
  378. TextureState state;
  379. RID diffuse;
  380. RID normal;
  381. RID specular;
  382. RID sampler;
  383. Vector2 texpixel_size;
  384. uint32_t specular_shininess = 0;
  385. uint32_t flags = 0;
  386. };
  387. /// A key used to uniquely identify a distinct BATCH_UNIFORM_SET
  388. struct RIDSetKey {
  389. TextureState state;
  390. RID instance_data;
  391. RIDSetKey() {
  392. }
  393. RIDSetKey(TextureState p_state, RID p_instance_data) :
  394. state(p_state),
  395. instance_data(p_instance_data) {
  396. }
  397. _ALWAYS_INLINE_ bool operator==(const RIDSetKey &p_val) const {
  398. return state == p_val.state && instance_data == p_val.instance_data;
  399. }
  400. _ALWAYS_INLINE_ bool operator!=(const RIDSetKey &p_val) const {
  401. return !(*this == p_val);
  402. }
  403. _ALWAYS_INLINE_ uint32_t hash() const {
  404. uint32_t h = state.hash();
  405. h = hash_murmur3_one_64(instance_data.get_id(), h);
  406. return hash_fmix32(h);
  407. }
  408. };
  409. static void _before_evict(RendererCanvasRenderRD::RIDSetKey &p_key, RID &p_rid);
  410. static void _uniform_set_invalidation_callback(void *p_userdata);
  411. typedef LRUCache<RIDSetKey, RID, HashableHasher<RIDSetKey>, HashMapComparatorDefault<RIDSetKey>, _before_evict> RIDCache;
  412. RIDCache rid_set_to_uniform_set;
  413. struct Batch {
  414. // Position in the UBO measured in bytes
  415. uint32_t start = 0;
  416. uint32_t instance_count = 0;
  417. uint32_t instance_buffer_index = 0;
  418. TextureInfo *tex_info;
  419. Color modulate = Color(1.0, 1.0, 1.0, 1.0);
  420. Item *clip = nullptr;
  421. RID material;
  422. CanvasMaterialData *material_data = nullptr;
  423. const Item::Command *command = nullptr;
  424. Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch.
  425. ShaderVariant shader_variant = SHADER_VARIANT_QUAD;
  426. RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES;
  427. bool use_lighting = false;
  428. // batch-specific data
  429. union {
  430. // TYPE_PRIMITIVE
  431. uint32_t primitive_points = 0;
  432. // TYPE_PARTICLES
  433. uint32_t mesh_instance_count;
  434. };
  435. bool has_blend = false;
  436. uint32_t flags = 0;
  437. };
  438. HashMap<TextureState, TextureInfo, HashableHasher<TextureState>> texture_info_map;
  439. // per-frame buffers
  440. struct DataBuffer {
  441. LocalVector<RID> instance_buffers;
  442. };
  443. struct State {
  444. //state buffer
  445. struct Buffer {
  446. float canvas_transform[16];
  447. float screen_transform[16];
  448. float canvas_normal_transform[16];
  449. float canvas_modulate[4];
  450. float screen_pixel_size[2];
  451. float time;
  452. uint32_t use_pixel_snap;
  453. float sdf_to_tex[4];
  454. float sdf_to_screen[2];
  455. float screen_to_sdf[2];
  456. uint32_t directional_light_count;
  457. float tex_to_sdf;
  458. uint32_t flags;
  459. uint32_t pad2;
  460. };
  461. DataBuffer canvas_instance_data_buffers[BATCH_DATA_BUFFER_COUNT];
  462. LocalVector<Batch> canvas_instance_batches;
  463. uint32_t current_data_buffer_index = 0;
  464. uint32_t current_instance_buffer_index = 0;
  465. uint32_t current_batch_index = 0;
  466. uint32_t last_instance_index = 0;
  467. InstanceData *instance_data_array = nullptr;
  468. uint32_t max_instances_per_buffer = 16384;
  469. uint32_t max_instance_buffer_size = 16384 * sizeof(InstanceData);
  470. Vector<RD::Uniform> batch_texture_uniforms;
  471. RID current_batch_uniform_set;
  472. LightUniform *light_uniforms = nullptr;
  473. RID lights_uniform_buffer;
  474. RID canvas_state_buffer;
  475. RID shadow_sampler;
  476. RID shadow_texture;
  477. RID shadow_depth_texture;
  478. RID shadow_fb;
  479. int shadow_texture_size = 2048;
  480. RID shadow_occluder_buffer;
  481. uint32_t shadow_occluder_buffer_size;
  482. RID shadow_ocluder_uniform_set;
  483. RID default_transforms_uniform_set;
  484. uint32_t max_lights_per_render;
  485. double time;
  486. } state;
  487. Item *items[MAX_RENDER_ITEMS];
  488. TextureInfo default_texture_info;
  489. bool using_directional_lights = false;
  490. RID default_canvas_texture;
  491. RID default_canvas_group_shader;
  492. RID default_canvas_group_material;
  493. RID default_clip_children_material;
  494. RID default_clip_children_shader;
  495. RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR;
  496. RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;
  497. RID _create_base_uniform_set(RID p_to_render_target, bool p_backbuffer);
  498. bool debug_redraw = false;
  499. Color debug_redraw_color;
  500. double debug_redraw_time = 1.0;
  501. // A structure to store cached render target information
  502. struct RenderTarget {
  503. // Current render target for the canvas.
  504. RID render_target;
  505. bool use_linear_colors = false;
  506. };
  507. inline RID _get_pipeline_specialization_or_ubershader(CanvasShaderData *p_shader_data, PipelineKey &r_pipeline_key, PushConstant &r_push_constant, RID p_mesh_instance = RID(), void *p_surface = nullptr, uint32_t p_surface_index = 0, RID *r_vertex_array = nullptr);
  508. void _render_batch_items(RenderTarget p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, bool &r_sdf_used, bool p_to_backbuffer = false, RenderingMethod::RenderInfo *r_render_info = nullptr);
  509. void _record_item_commands(const Item *p_item, RenderTarget p_render_target, const Transform2D &p_base_transform, Item *&r_current_clip, Light *p_lights, uint32_t &r_index, bool &r_batch_broken, bool &r_sdf_used, Batch *&r_current_batch);
  510. void _render_batch(RD::DrawListID p_draw_list, CanvasShaderData *p_shader_data, RenderingDevice::FramebufferFormatID p_framebuffer_format, Light *p_lights, Batch const *p_batch, RenderingMethod::RenderInfo *r_render_info = nullptr);
  511. void _prepare_batch_texture_info(RID p_texture, TextureState &p_state, TextureInfo *p_info);
  512. InstanceData *new_instance_data(float *p_world, uint32_t *p_lights, uint32_t p_base_flags, uint32_t p_index, TextureInfo *p_info);
  513. [[nodiscard]] Batch *_new_batch(bool &r_batch_broken);
  514. void _add_to_batch(uint32_t &r_index, bool &r_batch_broken, Batch *&r_current_batch);
  515. void _allocate_instance_buffer();
  516. _FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4);
  517. _FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3);
  518. _FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4);
  519. _FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4);
  520. void _update_shadow_atlas();
  521. void _update_occluder_buffer(uint32_t p_size);
  522. public:
  523. 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>()) override;
  524. void free_polygon(PolygonID p_polygon) override;
  525. RID light_create() override;
  526. void light_set_texture(RID p_rid, RID p_texture) override;
  527. void light_set_use_shadow(RID p_rid, bool p_enable) override;
  528. 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, const Rect2 &p_light_rect) override;
  529. 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) override;
  530. virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override;
  531. RID occluder_polygon_create() override;
  532. void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override;
  533. void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override;
  534. void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_light_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, RenderingMethod::RenderInfo *r_render_info = nullptr) override;
  535. virtual void set_shadow_texture_size(int p_size) override;
  536. void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) override;
  537. uint32_t get_pipeline_compilations(RS::PipelineSource p_source) override;
  538. void set_time(double p_time);
  539. void update() override;
  540. bool free(RID p_rid) override;
  541. RendererCanvasRenderRD();
  542. ~RendererCanvasRenderRD();
  543. };
  544. #endif // RENDERER_CANVAS_RENDER_RD_H