rasterizer_canvas_gles3.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*************************************************************************/
  2. /* rasterizer_canvas_gles3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_OPENGL_H
  31. #define RASTERIZER_CANVAS_OPENGL_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 "shaders/canvas.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. FLAGS_INSTANCING_MASK = 0x7F,
  48. FLAGS_INSTANCING_HAS_COLORS = (1 << 7),
  49. FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8),
  50. FLAGS_CLIP_RECT_UV = (1 << 9),
  51. FLAGS_TRANSPOSE_RECT = (1 << 10),
  52. FLAGS_NINEPACH_DRAW_CENTER = (1 << 12),
  53. FLAGS_USING_PARTICLES = (1 << 13),
  54. FLAGS_USE_SKELETON = (1 << 15),
  55. FLAGS_NINEPATCH_H_MODE_SHIFT = 16,
  56. FLAGS_NINEPATCH_V_MODE_SHIFT = 18,
  57. FLAGS_LIGHT_COUNT_SHIFT = 20,
  58. FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 26),
  59. FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 27),
  60. FLAGS_USE_MSDF = (1 << 28),
  61. };
  62. enum {
  63. LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF,
  64. LIGHT_FLAGS_BLEND_SHIFT = 16,
  65. LIGHT_FLAGS_BLEND_MASK = (3 << 16),
  66. LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16),
  67. LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16),
  68. LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16),
  69. LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16),
  70. LIGHT_FLAGS_HAS_SHADOW = (1 << 20),
  71. LIGHT_FLAGS_FILTER_SHIFT = 22
  72. };
  73. enum {
  74. MAX_RENDER_ITEMS = 256 * 1024,
  75. MAX_LIGHT_TEXTURES = 1024,
  76. MAX_LIGHTS_PER_ITEM = 16,
  77. DEFAULT_MAX_LIGHTS_PER_RENDER = 256,
  78. };
  79. public:
  80. enum {
  81. BASE_UNIFORM_LOCATION = 0,
  82. GLOBAL_UNIFORM_LOCATION = 1,
  83. LIGHT_UNIFORM_LOCATION = 2,
  84. INSTANCE_UNIFORM_LOCATION = 3,
  85. MATERIAL_UNIFORM_LOCATION = 4,
  86. };
  87. struct StateBuffer {
  88. float canvas_transform[16];
  89. float screen_transform[16];
  90. float canvas_normal_transform[16];
  91. float canvas_modulate[4];
  92. float screen_pixel_size[2];
  93. float time;
  94. uint32_t use_pixel_snap;
  95. float sdf_to_tex[4];
  96. float sdf_to_screen[2];
  97. float screen_to_sdf[2];
  98. uint32_t directional_light_count;
  99. float tex_to_sdf;
  100. uint32_t pad1;
  101. uint32_t pad2;
  102. };
  103. struct InstanceData {
  104. float world[6];
  105. float color_texture_pixel_size[2];
  106. union {
  107. //rect
  108. struct {
  109. float modulation[4];
  110. union {
  111. float msdf[4];
  112. float ninepatch_margins[4];
  113. };
  114. float dst_rect[4];
  115. float src_rect[4];
  116. float pad[2];
  117. };
  118. //primitive
  119. struct {
  120. float points[6]; // vec2 points[3]
  121. float uvs[6]; // vec2 points[3]
  122. uint32_t colors[6]; // colors encoded as half
  123. };
  124. };
  125. uint32_t flags;
  126. uint32_t specular_shininess;
  127. uint32_t lights[4];
  128. };
  129. struct Data {
  130. GLuint canvas_quad_vertices;
  131. GLuint canvas_quad_array;
  132. GLuint particle_quad_vertices;
  133. GLuint particle_quad_array;
  134. GLuint ninepatch_vertices;
  135. GLuint ninepatch_elements;
  136. } data;
  137. struct State {
  138. GLuint canvas_state_buffer;
  139. LocalVector<GLuint> canvas_instance_data_buffers;
  140. LocalVector<GLsync> fences;
  141. uint32_t current_buffer = 0;
  142. InstanceData *instance_data_array = nullptr;
  143. bool canvas_texscreen_used;
  144. RID canvas_shader_current_version;
  145. RID canvas_shader_default_version;
  146. RID current_tex = RID();
  147. Size2 current_pixel_size = Size2();
  148. RID current_normal = RID();
  149. RID current_specular = RID();
  150. GLES3::Texture *current_tex_ptr;
  151. RID current_shader_version = RID();
  152. RS::PrimitiveType current_primitive = RS::PRIMITIVE_MAX;
  153. uint32_t current_primitive_points = 0;
  154. Item::Command::Type current_command = Item::Command::TYPE_RECT;
  155. bool transparent_render_target = false;
  156. double time = 0.0;
  157. uint32_t max_lights_per_render;
  158. uint32_t max_lights_per_item;
  159. uint32_t max_instances_per_batch;
  160. RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  161. RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  162. } state;
  163. Item *items[MAX_RENDER_ITEMS];
  164. RID default_canvas_texture;
  165. RID default_canvas_group_material;
  166. RID default_canvas_group_shader;
  167. typedef void Texture;
  168. void canvas_begin(RID p_to_render_target, bool p_to_backbuffer);
  169. //virtual void draw_window_margins(int *black_margin, RID *black_image) override;
  170. void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
  171. void reset_canvas();
  172. void canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, Projection *p_xform_cache);
  173. virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) override;
  174. RID light_create() override;
  175. void light_set_texture(RID p_rid, RID p_texture) override;
  176. void light_set_use_shadow(RID p_rid, bool p_enable) override;
  177. 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;
  178. 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;
  179. void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override;
  180. RID occluder_polygon_create() override;
  181. void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override;
  182. void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override;
  183. void set_shadow_texture_size(int p_size) override;
  184. bool free(RID p_rid) override;
  185. void update() override;
  186. void _bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, uint32_t &r_index);
  187. struct PolygonBuffers {
  188. GLuint vertex_buffer;
  189. GLuint vertex_array;
  190. GLuint index_buffer;
  191. int count;
  192. bool color_disabled = false;
  193. Color color;
  194. };
  195. struct {
  196. HashMap<PolygonID, PolygonBuffers> polygons;
  197. PolygonID last_id;
  198. } polygon_buffers;
  199. 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;
  200. void free_polygon(PolygonID p_polygon) override;
  201. 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;
  202. void _render_items(RID p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, bool p_to_backbuffer = false);
  203. void _render_item(RID p_render_target, const Item *p_item, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, Light *p_lights, uint32_t &r_index);
  204. void _render_batch(uint32_t &p_max_index);
  205. void _bind_instance_data_buffer(uint32_t p_max_index);
  206. void _allocate_instance_data_buffer();
  207. void set_time(double p_time);
  208. static RasterizerCanvasGLES3 *get_singleton();
  209. RasterizerCanvasGLES3();
  210. ~RasterizerCanvasGLES3();
  211. };
  212. #endif // GLES3_ENABLED
  213. #endif // RASTERIZER_CANVAS_OPENGL_H