| 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 | |
| 31 | #ifndef RASTERIZER_CANVAS_GLES3_H |
| 32 | #define RASTERIZER_CANVAS_GLES3_H |
| 33 | |
| 34 | #ifdef GLES3_ENABLED |
| 35 | |
| 36 | #include "rasterizer_scene_gles3.h" |
| 37 | #include "servers/rendering/renderer_canvas_render.h" |
| 38 | #include "servers/rendering/renderer_compositor.h" |
| 39 | #include "storage/material_storage.h" |
| 40 | #include "storage/texture_storage.h" |
| 41 | |
| 42 | #include "drivers/gles3/shaders/canvas.glsl.gen.h" |
| 43 | #include "drivers/gles3/shaders/canvas_occlusion.glsl.gen.h" |
| 44 | |
| 45 | class RasterizerSceneGLES3; |
| 46 | |
| 47 | class RasterizerCanvasGLES3 : public RendererCanvasRender { |
| 48 | static RasterizerCanvasGLES3 *singleton; |
| 49 | |
| 50 | _FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4); |
| 51 | _FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3); |
| 52 | |
| 53 | _FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4); |
| 54 | _FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4); |
| 55 | |
| 56 | enum { |
| 57 | |
| 58 | FLAGS_INSTANCING_MASK = 0x7F, |
| 59 | FLAGS_INSTANCING_HAS_COLORS = (1 << 7), |
| 60 | FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8), |
| 61 | |
| 62 | FLAGS_CLIP_RECT_UV = (1 << 9), |
| 63 | FLAGS_TRANSPOSE_RECT = (1 << 10), |
| 64 | |
| 65 | FLAGS_NINEPACH_DRAW_CENTER = (1 << 12), |
| 66 | FLAGS_USING_PARTICLES = (1 << 13), |
| 67 | |
| 68 | FLAGS_USE_SKELETON = (1 << 15), |
| 69 | FLAGS_NINEPATCH_H_MODE_SHIFT = 16, |
| 70 | FLAGS_NINEPATCH_V_MODE_SHIFT = 18, |
| 71 | FLAGS_LIGHT_COUNT_SHIFT = 20, |
| 72 | |
| 73 | FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 26), |
| 74 | FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 27), |
| 75 | |
| 76 | FLAGS_USE_MSDF = (1 << 28), |
| 77 | FLAGS_USE_LCD = (1 << 29), |
| 78 | |
| 79 | FLAGS_FLIP_H = (1 << 30), |
| 80 | FLAGS_FLIP_V = (1 << 31), |
| 81 | }; |
| 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 | }; |
| 95 | |
| 96 | enum { |
| 97 | MAX_RENDER_ITEMS = 256 * 1024, |
| 98 | MAX_LIGHT_TEXTURES = 1024, |
| 99 | MAX_LIGHTS_PER_ITEM = 16, |
| 100 | DEFAULT_MAX_LIGHTS_PER_RENDER = 256, |
| 101 | }; |
| 102 | |
| 103 | /******************/ |
| 104 | /**** LIGHTING ****/ |
| 105 | /******************/ |
| 106 | |
| 107 | struct CanvasLight { |
| 108 | RID texture; |
| 109 | struct { |
| 110 | bool enabled = false; |
| 111 | float z_far; |
| 112 | float y_offset; |
| 113 | Transform2D directional_xform; |
| 114 | } shadow; |
| 115 | }; |
| 116 | |
| 117 | RID_Owner<CanvasLight> canvas_light_owner; |
| 118 | |
| 119 | struct OccluderPolygon { |
| 120 | RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED; |
| 121 | int line_point_count = 0; |
| 122 | GLuint vertex_buffer = 0; |
| 123 | GLuint vertex_array = 0; |
| 124 | GLuint index_buffer = 0; |
| 125 | |
| 126 | int sdf_point_count = 0; |
| 127 | int sdf_index_count = 0; |
| 128 | GLuint sdf_vertex_buffer = 0; |
| 129 | GLuint sdf_vertex_array = 0; |
| 130 | GLuint sdf_index_buffer = 0; |
| 131 | bool sdf_is_lines = false; |
| 132 | }; |
| 133 | |
| 134 | RID_Owner<OccluderPolygon> occluder_polygon_owner; |
| 135 | |
| 136 | void _update_shadow_atlas(); |
| 137 | |
| 138 | struct { |
| 139 | CanvasOcclusionShaderGLES3 shader; |
| 140 | RID shader_version; |
| 141 | } shadow_render; |
| 142 | |
| 143 | struct LightUniform { |
| 144 | float matrix[8]; //light to texture coordinate matrix |
| 145 | float shadow_matrix[8]; //light to shadow coordinate matrix |
| 146 | float color[4]; |
| 147 | |
| 148 | uint8_t shadow_color[4]; |
| 149 | uint32_t flags; //index to light texture |
| 150 | float shadow_pixel_size; |
| 151 | float height; |
| 152 | |
| 153 | float position[2]; |
| 154 | float shadow_z_far_inv; |
| 155 | float shadow_y_ofs; |
| 156 | |
| 157 | float atlas_rect[4]; |
| 158 | }; |
| 159 | |
| 160 | public: |
| 161 | enum { |
| 162 | BASE_UNIFORM_LOCATION = 0, |
| 163 | GLOBAL_UNIFORM_LOCATION = 1, |
| 164 | LIGHT_UNIFORM_LOCATION = 2, |
| 165 | INSTANCE_UNIFORM_LOCATION = 3, |
| 166 | MATERIAL_UNIFORM_LOCATION = 4, |
| 167 | }; |
| 168 | |
| 169 | struct StateBuffer { |
| 170 | float canvas_transform[16]; |
| 171 | float screen_transform[16]; |
| 172 | float canvas_normal_transform[16]; |
| 173 | float canvas_modulate[4]; |
| 174 | |
| 175 | float screen_pixel_size[2]; |
| 176 | float time; |
| 177 | uint32_t use_pixel_snap; |
| 178 | |
| 179 | float sdf_to_tex[4]; |
| 180 | float sdf_to_screen[2]; |
| 181 | float screen_to_sdf[2]; |
| 182 | |
| 183 | uint32_t directional_light_count; |
| 184 | float tex_to_sdf; |
| 185 | uint32_t pad1; |
| 186 | uint32_t pad2; |
| 187 | }; |
| 188 | |
| 189 | struct PolygonBuffers { |
| 190 | GLuint vertex_buffer = 0; |
| 191 | GLuint vertex_array = 0; |
| 192 | GLuint index_buffer = 0; |
| 193 | int count = 0; |
| 194 | bool color_disabled = false; |
| 195 | Color color = Color(1.0, 1.0, 1.0, 1.0); |
| 196 | }; |
| 197 | |
| 198 | struct { |
| 199 | HashMap<PolygonID, PolygonBuffers> polygons; |
| 200 | PolygonID last_id = 0; |
| 201 | } polygon_buffers; |
| 202 | |
| 203 | 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; |
| 204 | void free_polygon(PolygonID p_polygon) override; |
| 205 | |
| 206 | struct InstanceData { |
| 207 | float world[6]; |
| 208 | float color_texture_pixel_size[2]; |
| 209 | union { |
| 210 | //rect |
| 211 | struct { |
| 212 | float modulation[4]; |
| 213 | union { |
| 214 | float msdf[4]; |
| 215 | float ninepatch_margins[4]; |
| 216 | }; |
| 217 | float dst_rect[4]; |
| 218 | float src_rect[4]; |
| 219 | float pad[2]; |
| 220 | }; |
| 221 | //primitive |
| 222 | struct { |
| 223 | float points[6]; // vec2 points[3] |
| 224 | float uvs[6]; // vec2 points[3] |
| 225 | uint32_t colors[6]; // colors encoded as half |
| 226 | }; |
| 227 | }; |
| 228 | uint32_t flags; |
| 229 | uint32_t specular_shininess; |
| 230 | uint32_t lights[4]; |
| 231 | }; |
| 232 | |
| 233 | struct Data { |
| 234 | GLuint canvas_quad_vertices; |
| 235 | GLuint canvas_quad_array; |
| 236 | |
| 237 | GLuint indexed_quad_buffer; |
| 238 | GLuint indexed_quad_array; |
| 239 | |
| 240 | GLuint particle_quad_vertices; |
| 241 | GLuint particle_quad_array; |
| 242 | |
| 243 | GLuint ninepatch_vertices; |
| 244 | GLuint ninepatch_elements; |
| 245 | |
| 246 | RID canvas_shader_default_version; |
| 247 | |
| 248 | uint32_t max_lights_per_render = 256; |
| 249 | uint32_t max_lights_per_item = 16; |
| 250 | uint32_t max_instances_per_buffer = 16384; |
| 251 | uint32_t max_instance_buffer_size = 16384 * 128; |
| 252 | } data; |
| 253 | |
| 254 | struct Batch { |
| 255 | // Position in the UBO measured in bytes |
| 256 | uint32_t start = 0; |
| 257 | uint32_t instance_count = 0; |
| 258 | uint32_t instance_buffer_index = 0; |
| 259 | |
| 260 | RID tex; |
| 261 | RS::CanvasItemTextureFilter filter = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; |
| 262 | RS::CanvasItemTextureRepeat repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; |
| 263 | |
| 264 | GLES3::CanvasShaderData::BlendMode blend_mode = GLES3::CanvasShaderData::BLEND_MODE_MIX; |
| 265 | Color blend_color = Color(1.0, 1.0, 1.0, 1.0); |
| 266 | |
| 267 | Item *clip = nullptr; |
| 268 | |
| 269 | RID material; |
| 270 | GLES3::CanvasMaterialData *material_data = nullptr; |
| 271 | CanvasShaderGLES3::ShaderVariant shader_variant = CanvasShaderGLES3::MODE_QUAD; |
| 272 | |
| 273 | const Item::Command *command = nullptr; |
| 274 | Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch. |
| 275 | uint32_t primitive_points = 0; |
| 276 | |
| 277 | bool lights_disabled = false; |
| 278 | }; |
| 279 | |
| 280 | // DataBuffer contains our per-frame data. I.e. the resources that are updated each frame. |
| 281 | // We track them and ensure that they don't get reused until at least 2 frames have passed |
| 282 | // to avoid the GPU stalling to wait for a resource to become available. |
| 283 | struct DataBuffer { |
| 284 | Vector<GLuint> instance_buffers; |
| 285 | GLuint light_ubo = 0; |
| 286 | GLuint state_ubo = 0; |
| 287 | uint64_t last_frame_used = -3; |
| 288 | GLsync fence = GLsync(); |
| 289 | }; |
| 290 | |
| 291 | struct State { |
| 292 | LocalVector<DataBuffer> canvas_instance_data_buffers; |
| 293 | LocalVector<Batch> canvas_instance_batches; |
| 294 | uint32_t current_data_buffer_index = 0; |
| 295 | uint32_t current_instance_buffer_index = 0; |
| 296 | uint32_t current_batch_index = 0; |
| 297 | uint32_t last_item_index = 0; |
| 298 | |
| 299 | InstanceData *instance_data_array = nullptr; |
| 300 | |
| 301 | LightUniform *light_uniforms = nullptr; |
| 302 | |
| 303 | GLuint shadow_texture = 0; |
| 304 | GLuint shadow_depth_buffer = 0; |
| 305 | GLuint shadow_fb = 0; |
| 306 | int shadow_texture_size = 2048; |
| 307 | |
| 308 | bool using_directional_lights = false; |
| 309 | |
| 310 | RID current_tex; |
| 311 | RS::CanvasItemTextureFilter current_filter_mode = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; |
| 312 | RS::CanvasItemTextureRepeat current_repeat_mode = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; |
| 313 | |
| 314 | bool transparent_render_target = false; |
| 315 | |
| 316 | double time = 0.0; |
| 317 | |
| 318 | RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT; |
| 319 | RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT; |
| 320 | } state; |
| 321 | |
| 322 | Item *items[MAX_RENDER_ITEMS]; |
| 323 | |
| 324 | RID default_canvas_texture; |
| 325 | RID default_canvas_group_material; |
| 326 | RID default_canvas_group_shader; |
| 327 | RID default_clip_children_material; |
| 328 | RID default_clip_children_shader; |
| 329 | |
| 330 | typedef void Texture; |
| 331 | |
| 332 | void canvas_begin(RID p_to_render_target, bool p_to_backbuffer); |
| 333 | |
| 334 | //virtual void draw_window_margins(int *black_margin, RID *black_image) override; |
| 335 | void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample); |
| 336 | |
| 337 | void reset_canvas(); |
| 338 | |
| 339 | RID light_create() override; |
| 340 | void light_set_texture(RID p_rid, RID p_texture) override; |
| 341 | void light_set_use_shadow(RID p_rid, bool p_enable) override; |
| 342 | 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) override; |
| 343 | 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; |
| 344 | |
| 345 | void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override; |
| 346 | RID occluder_polygon_create() override; |
| 347 | void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override; |
| 348 | void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override; |
| 349 | void set_shadow_texture_size(int p_size) override; |
| 350 | |
| 351 | bool free(RID p_rid) override; |
| 352 | void update() override; |
| 353 | |
| 354 | void _bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat); |
| 355 | 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); |
| 356 | |
| 357 | 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) override; |
| 358 | 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); |
| 359 | void _record_item_commands(const Item *p_item, RID p_render_target, const Transform2D &p_canvas_transform_inverse, Item *¤t_clip, GLES3::CanvasShaderData::BlendMode p_blend_mode, Light *p_lights, uint32_t &r_index, bool &r_break_batch, bool &r_sdf_used); |
| 360 | void _render_batch(Light *p_lights, uint32_t p_index); |
| 361 | bool _bind_material(GLES3::CanvasMaterialData *p_material_data, CanvasShaderGLES3::ShaderVariant p_variant, uint64_t p_specialization); |
| 362 | void _new_batch(bool &r_batch_broken); |
| 363 | void _add_to_batch(uint32_t &r_index, bool &r_batch_broken); |
| 364 | void _allocate_instance_data_buffer(); |
| 365 | void _allocate_instance_buffer(); |
| 366 | void _enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate = 1); |
| 367 | |
| 368 | void set_time(double p_time); |
| 369 | |
| 370 | static RasterizerCanvasGLES3 *get_singleton(); |
| 371 | RasterizerCanvasGLES3(); |
| 372 | ~RasterizerCanvasGLES3(); |
| 373 | }; |
| 374 | |
| 375 | #endif // GLES3_ENABLED |
| 376 | |
| 377 | #endif // RASTERIZER_CANVAS_GLES3_H |
| 378 | |