rasterizer_canvas_gles3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. FLAGS_INSTANCING_MASK = 0x7F,
  49. FLAGS_INSTANCING_HAS_COLORS = (1 << 7),
  50. FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8),
  51. FLAGS_CLIP_RECT_UV = (1 << 9),
  52. FLAGS_TRANSPOSE_RECT = (1 << 10),
  53. FLAGS_NINEPACH_DRAW_CENTER = (1 << 12),
  54. FLAGS_USING_PARTICLES = (1 << 13),
  55. FLAGS_USE_SKELETON = (1 << 15),
  56. FLAGS_NINEPATCH_H_MODE_SHIFT = 16,
  57. FLAGS_NINEPATCH_V_MODE_SHIFT = 18,
  58. FLAGS_LIGHT_COUNT_SHIFT = 20,
  59. FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 26),
  60. FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 27),
  61. FLAGS_USE_MSDF = (1 << 28),
  62. FLAGS_USE_LCD = (1 << 29),
  63. FLAGS_FLIP_H = (1 << 30),
  64. FLAGS_FLIP_V = (1 << 31),
  65. };
  66. enum {
  67. LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF,
  68. LIGHT_FLAGS_BLEND_SHIFT = 16,
  69. LIGHT_FLAGS_BLEND_MASK = (3 << 16),
  70. LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16),
  71. LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16),
  72. LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16),
  73. LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16),
  74. LIGHT_FLAGS_HAS_SHADOW = (1 << 20),
  75. LIGHT_FLAGS_FILTER_SHIFT = 22
  76. };
  77. enum {
  78. MAX_RENDER_ITEMS = 256 * 1024,
  79. MAX_LIGHT_TEXTURES = 1024,
  80. MAX_LIGHTS_PER_ITEM = 16,
  81. DEFAULT_MAX_LIGHTS_PER_RENDER = 256,
  82. };
  83. /******************/
  84. /**** LIGHTING ****/
  85. /******************/
  86. struct CanvasLight {
  87. RID texture;
  88. struct {
  89. bool enabled = false;
  90. float z_far;
  91. float y_offset;
  92. Transform2D directional_xform;
  93. } shadow;
  94. };
  95. RID_Owner<CanvasLight> canvas_light_owner;
  96. struct OccluderPolygon {
  97. RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  98. int line_point_count = 0;
  99. GLuint vertex_buffer = 0;
  100. GLuint vertex_array = 0;
  101. GLuint index_buffer = 0;
  102. int sdf_point_count = 0;
  103. int sdf_index_count = 0;
  104. GLuint sdf_vertex_buffer = 0;
  105. GLuint sdf_vertex_array = 0;
  106. GLuint sdf_index_buffer = 0;
  107. bool sdf_is_lines = false;
  108. };
  109. RID_Owner<OccluderPolygon> occluder_polygon_owner;
  110. void _update_shadow_atlas();
  111. struct {
  112. CanvasOcclusionShaderGLES3 shader;
  113. RID shader_version;
  114. } shadow_render;
  115. struct LightUniform {
  116. float matrix[8]; //light to texture coordinate matrix
  117. float shadow_matrix[8]; //light to shadow coordinate matrix
  118. float color[4];
  119. uint8_t shadow_color[4];
  120. uint32_t flags; //index to light texture
  121. float shadow_pixel_size;
  122. float height;
  123. float position[2];
  124. float shadow_z_far_inv;
  125. float shadow_y_ofs;
  126. float atlas_rect[4];
  127. };
  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. 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;
  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>()) 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 specular_shininess;
  190. uint32_t lights[4];
  191. };
  192. struct Data {
  193. GLuint canvas_quad_vertices;
  194. GLuint canvas_quad_array;
  195. GLuint indexed_quad_buffer;
  196. GLuint indexed_quad_array;
  197. GLuint particle_quad_vertices;
  198. GLuint particle_quad_array;
  199. GLuint ninepatch_vertices;
  200. GLuint ninepatch_elements;
  201. RID canvas_shader_default_version;
  202. uint32_t max_lights_per_render = 256;
  203. uint32_t max_lights_per_item = 16;
  204. uint32_t max_instances_per_buffer = 16384;
  205. uint32_t max_instance_buffer_size = 16384 * 128;
  206. } data;
  207. struct Batch {
  208. // Position in the UBO measured in bytes
  209. uint32_t start = 0;
  210. uint32_t instance_count = 0;
  211. uint32_t instance_buffer_index = 0;
  212. RID tex;
  213. RS::CanvasItemTextureFilter filter = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
  214. RS::CanvasItemTextureRepeat repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
  215. GLES3::CanvasShaderData::BlendMode blend_mode = GLES3::CanvasShaderData::BLEND_MODE_MIX;
  216. Color blend_color = Color(1.0, 1.0, 1.0, 1.0);
  217. Item *clip = nullptr;
  218. RID material;
  219. GLES3::CanvasMaterialData *material_data = nullptr;
  220. CanvasShaderGLES3::ShaderVariant shader_variant = CanvasShaderGLES3::MODE_QUAD;
  221. const Item::Command *command = nullptr;
  222. Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch.
  223. uint32_t primitive_points = 0;
  224. bool lights_disabled = false;
  225. };
  226. // DataBuffer contains our per-frame data. I.e. the resources that are updated each frame.
  227. // We track them and ensure that they don't get reused until at least 2 frames have passed
  228. // to avoid the GPU stalling to wait for a resource to become available.
  229. struct DataBuffer {
  230. Vector<GLuint> instance_buffers;
  231. GLuint light_ubo = 0;
  232. GLuint state_ubo = 0;
  233. uint64_t last_frame_used = -3;
  234. GLsync fence = GLsync();
  235. };
  236. struct State {
  237. LocalVector<DataBuffer> canvas_instance_data_buffers;
  238. LocalVector<Batch> canvas_instance_batches;
  239. uint32_t current_data_buffer_index = 0;
  240. uint32_t current_instance_buffer_index = 0;
  241. uint32_t current_batch_index = 0;
  242. uint32_t last_item_index = 0;
  243. InstanceData *instance_data_array = nullptr;
  244. LightUniform *light_uniforms = nullptr;
  245. GLuint shadow_texture = 0;
  246. GLuint shadow_depth_buffer = 0;
  247. GLuint shadow_fb = 0;
  248. int shadow_texture_size = 2048;
  249. bool using_directional_lights = false;
  250. RID current_tex;
  251. RS::CanvasItemTextureFilter current_filter_mode = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
  252. RS::CanvasItemTextureRepeat current_repeat_mode = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
  253. bool transparent_render_target = false;
  254. double time = 0.0;
  255. RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  256. RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  257. } state;
  258. Item *items[MAX_RENDER_ITEMS];
  259. RID default_canvas_texture;
  260. RID default_canvas_group_material;
  261. RID default_canvas_group_shader;
  262. RID default_clip_children_material;
  263. RID default_clip_children_shader;
  264. typedef void Texture;
  265. void canvas_begin(RID p_to_render_target, bool p_to_backbuffer);
  266. //virtual void draw_window_margins(int *black_margin, RID *black_image) override;
  267. void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
  268. void reset_canvas();
  269. RID light_create() override;
  270. void light_set_texture(RID p_rid, RID p_texture) override;
  271. void light_set_use_shadow(RID p_rid, bool p_enable) override;
  272. 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;
  273. 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;
  274. void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override;
  275. RID occluder_polygon_create() override;
  276. void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override;
  277. void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override;
  278. void set_shadow_texture_size(int p_size) override;
  279. bool free(RID p_rid) override;
  280. void update() override;
  281. void _bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat);
  282. 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);
  283. 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;
  284. 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);
  285. 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);
  286. void _render_batch(Light *p_lights, uint32_t p_index);
  287. bool _bind_material(GLES3::CanvasMaterialData *p_material_data, CanvasShaderGLES3::ShaderVariant p_variant, uint64_t p_specialization);
  288. void _new_batch(bool &r_batch_broken);
  289. void _add_to_batch(uint32_t &r_index, bool &r_batch_broken);
  290. void _allocate_instance_data_buffer();
  291. void _allocate_instance_buffer();
  292. void _enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate = 1);
  293. void set_time(double p_time);
  294. static RasterizerCanvasGLES3 *get_singleton();
  295. RasterizerCanvasGLES3();
  296. ~RasterizerCanvasGLES3();
  297. };
  298. #endif // GLES3_ENABLED
  299. #endif // RASTERIZER_CANVAS_GLES3_H