rasterizer_canvas_gles3.h 14 KB

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