renderer_canvas_render_rd.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "servers/rendering/renderer_canvas_render.h"
  33. #include "servers/rendering/renderer_compositor.h"
  34. #include "servers/rendering/renderer_rd/pipeline_cache_rd.h"
  35. #include "servers/rendering/renderer_rd/shaders/canvas.glsl.gen.h"
  36. #include "servers/rendering/renderer_rd/shaders/canvas_occlusion.glsl.gen.h"
  37. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  38. #include "servers/rendering/rendering_device.h"
  39. #include "servers/rendering/shader_compiler.h"
  40. class RendererCanvasRenderRD : public RendererCanvasRender {
  41. enum {
  42. BASE_UNIFORM_SET = 0,
  43. MATERIAL_UNIFORM_SET = 1,
  44. TRANSFORMS_UNIFORM_SET = 2,
  45. CANVAS_TEXTURE_UNIFORM_SET = 3,
  46. };
  47. const int SAMPLERS_BINDING_FIRST_INDEX = 10;
  48. enum ShaderVariant {
  49. SHADER_VARIANT_QUAD,
  50. SHADER_VARIANT_NINEPATCH,
  51. SHADER_VARIANT_PRIMITIVE,
  52. SHADER_VARIANT_PRIMITIVE_POINTS,
  53. SHADER_VARIANT_ATTRIBUTES,
  54. SHADER_VARIANT_ATTRIBUTES_POINTS,
  55. SHADER_VARIANT_QUAD_LIGHT,
  56. SHADER_VARIANT_NINEPATCH_LIGHT,
  57. SHADER_VARIANT_PRIMITIVE_LIGHT,
  58. SHADER_VARIANT_PRIMITIVE_POINTS_LIGHT,
  59. SHADER_VARIANT_ATTRIBUTES_LIGHT,
  60. SHADER_VARIANT_ATTRIBUTES_POINTS_LIGHT,
  61. SHADER_VARIANT_MAX
  62. };
  63. enum {
  64. FLAGS_INSTANCING_MASK = 0x7F,
  65. FLAGS_INSTANCING_HAS_COLORS = (1 << 7),
  66. FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8),
  67. FLAGS_CLIP_RECT_UV = (1 << 9),
  68. FLAGS_TRANSPOSE_RECT = (1 << 10),
  69. FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR = (1 << 11),
  70. FLAGS_NINEPACH_DRAW_CENTER = (1 << 12),
  71. FLAGS_USING_PARTICLES = (1 << 13),
  72. FLAGS_USE_SKELETON = (1 << 15),
  73. FLAGS_NINEPATCH_H_MODE_SHIFT = 16,
  74. FLAGS_NINEPATCH_V_MODE_SHIFT = 18,
  75. FLAGS_LIGHT_COUNT_SHIFT = 20,
  76. FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 26),
  77. FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 27),
  78. FLAGS_USE_MSDF = (1 << 28),
  79. FLAGS_USE_LCD = (1 << 29),
  80. FLAGS_FLIP_H = (1 << 30),
  81. FLAGS_FLIP_V = (1 << 31),
  82. };
  83. enum {
  84. LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF,
  85. LIGHT_FLAGS_BLEND_SHIFT = 16,
  86. LIGHT_FLAGS_BLEND_MASK = (3 << 16),
  87. LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16),
  88. LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16),
  89. LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16),
  90. LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16),
  91. LIGHT_FLAGS_HAS_SHADOW = (1 << 20),
  92. LIGHT_FLAGS_FILTER_SHIFT = 22
  93. };
  94. enum {
  95. MAX_RENDER_ITEMS = 256 * 1024,
  96. MAX_LIGHT_TEXTURES = 1024,
  97. MAX_LIGHTS_PER_ITEM = 16,
  98. DEFAULT_MAX_LIGHTS_PER_RENDER = 256
  99. };
  100. /****************/
  101. /**** SHADER ****/
  102. /****************/
  103. enum PipelineVariant {
  104. PIPELINE_VARIANT_QUAD,
  105. PIPELINE_VARIANT_NINEPATCH,
  106. PIPELINE_VARIANT_PRIMITIVE_TRIANGLES,
  107. PIPELINE_VARIANT_PRIMITIVE_LINES,
  108. PIPELINE_VARIANT_PRIMITIVE_POINTS,
  109. PIPELINE_VARIANT_ATTRIBUTE_TRIANGLES,
  110. PIPELINE_VARIANT_ATTRIBUTE_TRIANGLE_STRIP,
  111. PIPELINE_VARIANT_ATTRIBUTE_LINES,
  112. PIPELINE_VARIANT_ATTRIBUTE_LINES_STRIP,
  113. PIPELINE_VARIANT_ATTRIBUTE_POINTS,
  114. PIPELINE_VARIANT_QUAD_LCD_BLEND,
  115. PIPELINE_VARIANT_MAX
  116. };
  117. enum PipelineLightMode {
  118. PIPELINE_LIGHT_MODE_DISABLED,
  119. PIPELINE_LIGHT_MODE_ENABLED,
  120. PIPELINE_LIGHT_MODE_MAX
  121. };
  122. struct PipelineVariants {
  123. PipelineCacheRD variants[PIPELINE_LIGHT_MODE_MAX][PIPELINE_VARIANT_MAX];
  124. };
  125. struct {
  126. CanvasShaderRD canvas_shader;
  127. RID default_version;
  128. RID default_version_rd_shader;
  129. RID quad_index_buffer;
  130. RID quad_index_array;
  131. PipelineVariants pipeline_variants;
  132. ShaderCompiler compiler;
  133. } shader;
  134. struct CanvasShaderData : public RendererRD::MaterialStorage::ShaderData {
  135. enum BlendMode { //used internally
  136. BLEND_MODE_MIX,
  137. BLEND_MODE_ADD,
  138. BLEND_MODE_SUB,
  139. BLEND_MODE_MUL,
  140. BLEND_MODE_PMALPHA,
  141. BLEND_MODE_DISABLED,
  142. };
  143. bool valid = false;
  144. RID version;
  145. PipelineVariants pipeline_variants;
  146. Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
  147. Vector<uint32_t> ubo_offsets;
  148. uint32_t ubo_size = 0;
  149. String code;
  150. bool uses_screen_texture = false;
  151. bool uses_screen_texture_mipmaps = false;
  152. bool uses_sdf = false;
  153. bool uses_time = false;
  154. virtual void set_code(const String &p_Code);
  155. virtual bool is_animated() const;
  156. virtual bool casts_shadows() const;
  157. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  158. CanvasShaderData() {}
  159. virtual ~CanvasShaderData();
  160. };
  161. RendererRD::MaterialStorage::ShaderData *_create_shader_func();
  162. static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() {
  163. return static_cast<RendererCanvasRenderRD *>(singleton)->_create_shader_func();
  164. }
  165. struct CanvasMaterialData : public RendererRD::MaterialStorage::MaterialData {
  166. CanvasShaderData *shader_data = nullptr;
  167. RID uniform_set;
  168. RID uniform_set_srgb;
  169. virtual void set_render_priority(int p_priority) {}
  170. virtual void set_next_pass(RID p_pass) {}
  171. virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  172. virtual ~CanvasMaterialData();
  173. };
  174. RendererRD::MaterialStorage::MaterialData *_create_material_func(CanvasShaderData *p_shader);
  175. static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
  176. return static_cast<RendererCanvasRenderRD *>(singleton)->_create_material_func(static_cast<CanvasShaderData *>(p_shader));
  177. }
  178. /**************************/
  179. /**** CANVAS TEXTURES *****/
  180. /**************************/
  181. struct {
  182. RS::CanvasItemTextureFilter default_filter;
  183. RS::CanvasItemTextureRepeat default_repeat;
  184. } default_samplers;
  185. /******************/
  186. /**** POLYGONS ****/
  187. /******************/
  188. struct PolygonBuffers {
  189. RD::VertexFormatID vertex_format_id;
  190. RID vertex_buffer;
  191. RID vertex_array;
  192. RID index_buffer;
  193. RID indices;
  194. };
  195. struct {
  196. HashMap<PolygonID, PolygonBuffers> polygons;
  197. PolygonID last_id;
  198. } polygon_buffers;
  199. /********************/
  200. /**** PRIMITIVES ****/
  201. /********************/
  202. struct {
  203. RID index_array[4];
  204. } primitive_arrays;
  205. /*******************/
  206. /**** MATERIALS ****/
  207. /*******************/
  208. /******************/
  209. /**** LIGHTING ****/
  210. /******************/
  211. struct CanvasLight {
  212. RID texture;
  213. struct {
  214. bool enabled = false;
  215. float z_far;
  216. float y_offset;
  217. Transform2D directional_xform;
  218. } shadow;
  219. };
  220. RID_Owner<CanvasLight> canvas_light_owner;
  221. struct ShadowRenderPushConstant {
  222. float projection[16];
  223. float modelview[8];
  224. float direction[2];
  225. float z_far;
  226. float pad;
  227. };
  228. struct OccluderPolygon {
  229. RS::CanvasOccluderPolygonCullMode cull_mode;
  230. int line_point_count;
  231. RID vertex_buffer;
  232. RID vertex_array;
  233. RID index_buffer;
  234. RID index_array;
  235. int sdf_point_count;
  236. int sdf_index_count;
  237. RID sdf_vertex_buffer;
  238. RID sdf_vertex_array;
  239. RID sdf_index_buffer;
  240. RID sdf_index_array;
  241. bool sdf_is_lines;
  242. };
  243. struct LightUniform {
  244. float matrix[8]; //light to texture coordinate matrix
  245. float shadow_matrix[8]; //light to shadow coordinate matrix
  246. float color[4];
  247. uint8_t shadow_color[4];
  248. uint32_t flags; //index to light texture
  249. float shadow_pixel_size;
  250. float height;
  251. float position[2];
  252. float shadow_z_far_inv;
  253. float shadow_y_ofs;
  254. float atlas_rect[4];
  255. };
  256. RID_Owner<OccluderPolygon> occluder_polygon_owner;
  257. enum ShadowRenderMode {
  258. SHADOW_RENDER_MODE_SHADOW,
  259. SHADOW_RENDER_MODE_SDF,
  260. };
  261. enum {
  262. SHADOW_RENDER_SDF_TRIANGLES,
  263. SHADOW_RENDER_SDF_LINES,
  264. };
  265. struct {
  266. CanvasOcclusionShaderRD shader;
  267. RID shader_version;
  268. RID render_pipelines[3];
  269. RID sdf_render_pipelines[2];
  270. RD::VertexFormatID vertex_format;
  271. RD::VertexFormatID sdf_vertex_format;
  272. RD::FramebufferFormatID framebuffer_format;
  273. RD::FramebufferFormatID sdf_framebuffer_format;
  274. } shadow_render;
  275. /***************/
  276. /**** STATE ****/
  277. /***************/
  278. //state that does not vary across rendering all items
  279. struct State {
  280. //state buffer
  281. struct Buffer {
  282. float canvas_transform[16];
  283. float screen_transform[16];
  284. float canvas_normal_transform[16];
  285. float canvas_modulate[4];
  286. float screen_pixel_size[2];
  287. float time;
  288. uint32_t use_pixel_snap;
  289. float sdf_to_tex[4];
  290. float sdf_to_screen[2];
  291. float screen_to_sdf[2];
  292. uint32_t directional_light_count;
  293. float tex_to_sdf;
  294. uint32_t pad1;
  295. uint32_t pad2;
  296. };
  297. LightUniform *light_uniforms = nullptr;
  298. RID lights_uniform_buffer;
  299. RID canvas_state_buffer;
  300. RID shadow_sampler;
  301. RID shadow_texture;
  302. RID shadow_depth_texture;
  303. RID shadow_fb;
  304. int shadow_texture_size = 2048;
  305. RID default_transforms_uniform_set;
  306. uint32_t max_lights_per_render;
  307. uint32_t max_lights_per_item;
  308. double time;
  309. } state;
  310. struct PushConstant {
  311. float world[6];
  312. uint32_t flags;
  313. uint32_t specular_shininess;
  314. union {
  315. //rect
  316. struct {
  317. float modulation[4];
  318. union {
  319. float msdf[4];
  320. float ninepatch_margins[4];
  321. };
  322. float dst_rect[4];
  323. float src_rect[4];
  324. float pad[2];
  325. };
  326. //primitive
  327. struct {
  328. float points[6]; // vec2 points[3]
  329. float uvs[6]; // vec2 points[3]
  330. uint32_t colors[6]; // colors encoded as half
  331. };
  332. };
  333. float color_texture_pixel_size[2];
  334. uint32_t lights[4];
  335. };
  336. Item *items[MAX_RENDER_ITEMS];
  337. bool using_directional_lights = false;
  338. RID default_canvas_texture;
  339. RID default_canvas_group_shader;
  340. RID default_canvas_group_material;
  341. RID default_clip_children_material;
  342. RID default_clip_children_shader;
  343. RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR;
  344. RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;
  345. RID _create_base_uniform_set(RID p_to_render_target, bool p_backbuffer);
  346. bool debug_redraw = false;
  347. Color debug_redraw_color;
  348. double debug_redraw_time = 1.0;
  349. inline void _bind_canvas_texture(RD::DrawListID p_draw_list, RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, RID &r_last_texture, PushConstant &push_constant, Size2 &r_texpixel_size, bool p_texture_is_data = false); //recursive, so regular inline used instead.
  350. void _render_item(RenderingDevice::DrawListID p_draw_list, RID p_render_target, const Item *p_item, RenderingDevice::FramebufferFormatID p_framebuffer_format, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, Light *p_lights, PipelineVariants *p_pipeline_variants, bool &r_sdf_used);
  351. void _render_items(RID 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);
  352. _FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4);
  353. _FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3);
  354. _FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4);
  355. _FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4);
  356. void _update_shadow_atlas();
  357. public:
  358. 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>());
  359. void free_polygon(PolygonID p_polygon);
  360. RID light_create();
  361. void light_set_texture(RID p_rid, RID p_texture);
  362. void light_set_use_shadow(RID p_rid, bool p_enable);
  363. 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);
  364. 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);
  365. virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders);
  366. RID occluder_polygon_create();
  367. void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed);
  368. void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode);
  369. 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);
  370. virtual void set_shadow_texture_size(int p_size);
  371. void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color);
  372. void set_time(double p_time);
  373. void update();
  374. bool free(RID p_rid);
  375. RendererCanvasRenderRD();
  376. ~RendererCanvasRenderRD();
  377. };
  378. #endif // RENDERER_CANVAS_RENDER_RD_H