rasterizer_canvas_gles3.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**************************************************************************/
  2. /* rasterizer_canvas_gles3.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 RASTERIZER_CANVAS_GLES3_H
  31. #define RASTERIZER_CANVAS_GLES3_H
  32. #ifdef GLES3_ENABLED
  33. #include "rasterizer_scene_gles3.h"
  34. #include "servers/rendering/renderer_canvas_render.h"
  35. #include "servers/rendering/renderer_compositor.h"
  36. #include "storage/material_storage.h"
  37. #include "storage/texture_storage.h"
  38. #include "drivers/gles3/shaders/canvas.glsl.gen.h"
  39. #include "drivers/gles3/shaders/canvas_occlusion.glsl.gen.h"
  40. class RasterizerSceneGLES3;
  41. class RasterizerCanvasGLES3 : public RendererCanvasRender {
  42. static RasterizerCanvasGLES3 *singleton;
  43. _FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4);
  44. _FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3);
  45. _FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4);
  46. _FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4);
  47. enum {
  48. INSTANCE_FLAGS_LIGHT_COUNT_SHIFT = 0, // 4 bits for light count.
  49. INSTANCE_FLAGS_CLIP_RECT_UV = (1 << 4),
  50. INSTANCE_FLAGS_TRANSPOSE_RECT = (1 << 5),
  51. INSTANCE_FLAGS_USE_MSDF = (1 << 6),
  52. INSTANCE_FLAGS_USE_LCD = (1 << 7),
  53. INSTANCE_FLAGS_NINEPACH_DRAW_CENTER = (1 << 8),
  54. INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT = 9,
  55. INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT = 11,
  56. INSTANCE_FLAGS_SHADOW_MASKED_SHIFT = 13, // 16 bits.
  57. };
  58. enum {
  59. BATCH_FLAGS_INSTANCING_MASK = 0x7F,
  60. BATCH_FLAGS_INSTANCING_HAS_COLORS = (1 << 7),
  61. BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8),
  62. BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 9),
  63. BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 10),
  64. };
  65. enum {
  66. LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF,
  67. LIGHT_FLAGS_BLEND_SHIFT = 16,
  68. LIGHT_FLAGS_BLEND_MASK = (3 << 16),
  69. LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16),
  70. LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16),
  71. LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16),
  72. LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16),
  73. LIGHT_FLAGS_HAS_SHADOW = (1 << 20),
  74. LIGHT_FLAGS_FILTER_SHIFT = 22
  75. };
  76. enum {
  77. MAX_RENDER_ITEMS = 256 * 1024,
  78. MAX_LIGHT_TEXTURES = 1024,
  79. MAX_LIGHTS_PER_ITEM = 16,
  80. DEFAULT_MAX_LIGHTS_PER_RENDER = 256,
  81. };
  82. /******************/
  83. /**** LIGHTING ****/
  84. /******************/
  85. struct CanvasLight {
  86. RID texture;
  87. struct {
  88. bool enabled = false;
  89. float z_far;
  90. float y_offset;
  91. Transform2D directional_xform;
  92. } shadow;
  93. };
  94. RID_Owner<CanvasLight> canvas_light_owner;
  95. struct OccluderPolygon {
  96. RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  97. int line_point_count = 0;
  98. GLuint vertex_buffer = 0;
  99. GLuint vertex_array = 0;
  100. GLuint index_buffer = 0;
  101. int sdf_point_count = 0;
  102. int sdf_index_count = 0;
  103. GLuint sdf_vertex_buffer = 0;
  104. GLuint sdf_vertex_array = 0;
  105. GLuint sdf_index_buffer = 0;
  106. bool sdf_is_lines = false;
  107. };
  108. RID_Owner<OccluderPolygon> occluder_polygon_owner;
  109. void _update_shadow_atlas();
  110. struct {
  111. CanvasOcclusionShaderGLES3 shader;
  112. RID shader_version;
  113. } shadow_render;
  114. struct LightUniform {
  115. float matrix[8]; //light to texture coordinate matrix
  116. float shadow_matrix[8]; //light to shadow coordinate matrix
  117. float color[4];
  118. uint8_t shadow_color[4];
  119. uint32_t flags; //index to light texture
  120. float shadow_pixel_size;
  121. float height;
  122. float position[2];
  123. float shadow_z_far_inv;
  124. float shadow_y_ofs;
  125. float atlas_rect[4];
  126. };
  127. static_assert(sizeof(LightUniform) % 16 == 0, "2D light UBO size must be a multiple of 16 bytes");
  128. public:
  129. enum {
  130. BASE_UNIFORM_LOCATION = 0,
  131. GLOBAL_UNIFORM_LOCATION = 1,
  132. LIGHT_UNIFORM_LOCATION = 2,
  133. INSTANCE_UNIFORM_LOCATION = 3,
  134. MATERIAL_UNIFORM_LOCATION = 4,
  135. };
  136. struct StateBuffer {
  137. float canvas_transform[16];
  138. float screen_transform[16];
  139. float canvas_normal_transform[16];
  140. float canvas_modulate[4];
  141. float screen_pixel_size[2];
  142. float time;
  143. uint32_t use_pixel_snap;
  144. float sdf_to_tex[4];
  145. float sdf_to_screen[2];
  146. float screen_to_sdf[2];
  147. uint32_t directional_light_count;
  148. float tex_to_sdf;
  149. uint32_t pad1;
  150. uint32_t pad2;
  151. };
  152. static_assert(sizeof(StateBuffer) % 16 == 0, "2D state UBO size must be a multiple of 16 bytes");
  153. struct PolygonBuffers {
  154. GLuint vertex_buffer = 0;
  155. GLuint vertex_array = 0;
  156. GLuint index_buffer = 0;
  157. int count = 0;
  158. bool color_disabled = false;
  159. Color color = Color(1.0, 1.0, 1.0, 1.0);
  160. };
  161. struct {
  162. HashMap<PolygonID, PolygonBuffers> polygons;
  163. PolygonID last_id = 0;
  164. } polygon_buffers;
  165. RendererCanvasRender::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;
  166. void free_polygon(PolygonID p_polygon) override;
  167. struct InstanceData {
  168. float world[6];
  169. float color_texture_pixel_size[2];
  170. union {
  171. //rect
  172. struct {
  173. float modulation[4];
  174. union {
  175. float msdf[4];
  176. float ninepatch_margins[4];
  177. };
  178. float dst_rect[4];
  179. float src_rect[4];
  180. float pad[2];
  181. };
  182. //primitive
  183. struct {
  184. float points[6]; // vec2 points[3]
  185. float uvs[6]; // vec2 points[3]
  186. uint32_t colors[6]; // colors encoded as half
  187. };
  188. };
  189. uint32_t flags;
  190. uint32_t instance_uniforms_ofs;
  191. uint32_t lights[4];
  192. };
  193. static_assert(sizeof(InstanceData) == 128, "2D instance data struct size must be 128 bytes");
  194. struct Data {
  195. GLuint canvas_quad_vertices;
  196. GLuint canvas_quad_array;
  197. GLuint indexed_quad_buffer;
  198. GLuint indexed_quad_array;
  199. GLuint particle_quad_vertices;
  200. GLuint particle_quad_array;
  201. GLuint ninepatch_vertices;
  202. GLuint ninepatch_elements;
  203. RID canvas_shader_default_version;
  204. uint32_t max_lights_per_render = 256;
  205. uint32_t max_lights_per_item = 16;
  206. uint32_t max_instances_per_buffer = 16384;
  207. uint32_t max_instance_buffer_size = 16384 * 128;
  208. } data;
  209. struct Batch {
  210. // Position in the UBO measured in bytes
  211. uint32_t start = 0;
  212. uint32_t instance_count = 0;
  213. uint32_t instance_buffer_index = 0;
  214. RID tex;
  215. RS::CanvasItemTextureFilter filter = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
  216. RS::CanvasItemTextureRepeat repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
  217. GLES3::CanvasShaderData::BlendMode blend_mode = GLES3::CanvasShaderData::BLEND_MODE_MIX;
  218. Color blend_color = Color(1.0, 1.0, 1.0, 1.0);
  219. Item *clip = nullptr;
  220. RID material;
  221. GLES3::CanvasMaterialData *material_data = nullptr;
  222. uint64_t vertex_input_mask = RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_FORMAT_COLOR | RS::ARRAY_FORMAT_TEX_UV;
  223. uint64_t specialization = 0;
  224. const Item::Command *command = nullptr;
  225. Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch.
  226. uint32_t primitive_points = 0;
  227. uint32_t flags = 0;
  228. uint32_t specular_shininess = 0.0;
  229. bool lights_disabled = false;
  230. };
  231. // DataBuffer contains our per-frame data. I.e. the resources that are updated each frame.
  232. // We track them and ensure that they don't get reused until at least 2 frames have passed
  233. // to avoid the GPU stalling to wait for a resource to become available.
  234. struct DataBuffer {
  235. Vector<GLuint> instance_buffers;
  236. GLuint light_ubo = 0;
  237. GLuint state_ubo = 0;
  238. uint64_t last_frame_used = -3;
  239. GLsync fence = GLsync();
  240. };
  241. struct State {
  242. LocalVector<DataBuffer> canvas_instance_data_buffers;
  243. LocalVector<Batch> canvas_instance_batches;
  244. uint32_t current_data_buffer_index = 0;
  245. uint32_t current_instance_buffer_index = 0;
  246. uint32_t current_batch_index = 0;
  247. uint32_t last_item_index = 0;
  248. InstanceData *instance_data_array = nullptr;
  249. LightUniform *light_uniforms = nullptr;
  250. GLuint shadow_texture = 0;
  251. GLuint shadow_depth_buffer = 0;
  252. GLuint shadow_fb = 0;
  253. int shadow_texture_size = 2048;
  254. bool using_directional_lights = false;
  255. RID current_tex;
  256. RS::CanvasItemTextureFilter current_filter_mode = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
  257. RS::CanvasItemTextureRepeat current_repeat_mode = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
  258. bool transparent_render_target = false;
  259. double time = 0.0;
  260. RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  261. RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  262. } state;
  263. Item *items[MAX_RENDER_ITEMS];
  264. RID default_canvas_texture;
  265. RID default_canvas_group_material;
  266. RID default_canvas_group_shader;
  267. RID default_clip_children_material;
  268. RID default_clip_children_shader;
  269. typedef void Texture;
  270. void canvas_begin(RID p_to_render_target, bool p_to_backbuffer, bool p_backbuffer_has_mipmaps);
  271. //virtual void draw_window_margins(int *black_margin, RID *black_image) override;
  272. void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
  273. void reset_canvas();
  274. RID light_create() override;
  275. void light_set_texture(RID p_rid, RID p_texture) override;
  276. void light_set_use_shadow(RID p_rid, bool p_enable) override;
  277. 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;
  278. 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;
  279. void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override;
  280. RID occluder_polygon_create() override;
  281. void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override;
  282. void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override;
  283. void set_shadow_texture_size(int p_size) override;
  284. bool free(RID p_rid) override;
  285. void update() override;
  286. void _bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat);
  287. void _prepare_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, uint32_t &r_index, Size2 &r_texpixel_size);
  288. 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, RenderingMethod::RenderInfo *r_render_info = nullptr) override;
  289. 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, RenderingMethod::RenderInfo *r_render_info = nullptr, bool p_backbuffer_has_mipmaps = false);
  290. void _record_item_commands(const Item *p_item, RID p_render_target, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, GLES3::CanvasShaderData::BlendMode p_blend_mode, Light *p_lights, uint32_t &r_index, bool &r_break_batch, bool &r_sdf_used, const Point2 &p_repeat_offset);
  291. void _render_batch(Light *p_lights, uint32_t p_index, RenderingMethod::RenderInfo *r_render_info = nullptr);
  292. bool _bind_material(GLES3::CanvasMaterialData *p_material_data, CanvasShaderGLES3::ShaderVariant p_variant, uint64_t p_specialization);
  293. void _new_batch(bool &r_batch_broken);
  294. void _add_to_batch(uint32_t &r_index, bool &r_batch_broken);
  295. void _allocate_instance_data_buffer();
  296. void _allocate_instance_buffer();
  297. void _enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate = 1);
  298. void set_time(double p_time);
  299. virtual void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) override {
  300. if (p_enabled) {
  301. WARN_PRINT_ONCE("Debug CanvasItem Redraw is not available yet when using the Compatibility renderer.");
  302. }
  303. }
  304. virtual uint32_t get_pipeline_compilations(RS::PipelineSource p_source) override { return 0; }
  305. static RasterizerCanvasGLES3 *get_singleton();
  306. RasterizerCanvasGLES3();
  307. ~RasterizerCanvasGLES3();
  308. };
  309. #endif // GLES3_ENABLED
  310. #endif // RASTERIZER_CANVAS_GLES3_H