1 | /**************************************************************************/ |
2 | /* renderer_canvas_render_rd.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 RENDERER_CANVAS_RENDER_RD_H |
32 | #define RENDERER_CANVAS_RENDER_RD_H |
33 | |
34 | #include "servers/rendering/renderer_canvas_render.h" |
35 | #include "servers/rendering/renderer_compositor.h" |
36 | #include "servers/rendering/renderer_rd/pipeline_cache_rd.h" |
37 | #include "servers/rendering/renderer_rd/shaders/canvas.glsl.gen.h" |
38 | #include "servers/rendering/renderer_rd/shaders/canvas_occlusion.glsl.gen.h" |
39 | #include "servers/rendering/renderer_rd/storage_rd/material_storage.h" |
40 | #include "servers/rendering/rendering_device.h" |
41 | #include "servers/rendering/shader_compiler.h" |
42 | |
43 | class RendererCanvasRenderRD : public RendererCanvasRender { |
44 | enum { |
45 | BASE_UNIFORM_SET = 0, |
46 | MATERIAL_UNIFORM_SET = 1, |
47 | TRANSFORMS_UNIFORM_SET = 2, |
48 | CANVAS_TEXTURE_UNIFORM_SET = 3, |
49 | }; |
50 | |
51 | const int SAMPLERS_BINDING_FIRST_INDEX = 10; |
52 | |
53 | enum ShaderVariant { |
54 | SHADER_VARIANT_QUAD, |
55 | SHADER_VARIANT_NINEPATCH, |
56 | SHADER_VARIANT_PRIMITIVE, |
57 | SHADER_VARIANT_PRIMITIVE_POINTS, |
58 | SHADER_VARIANT_ATTRIBUTES, |
59 | SHADER_VARIANT_ATTRIBUTES_POINTS, |
60 | SHADER_VARIANT_QUAD_LIGHT, |
61 | SHADER_VARIANT_NINEPATCH_LIGHT, |
62 | SHADER_VARIANT_PRIMITIVE_LIGHT, |
63 | SHADER_VARIANT_PRIMITIVE_POINTS_LIGHT, |
64 | SHADER_VARIANT_ATTRIBUTES_LIGHT, |
65 | SHADER_VARIANT_ATTRIBUTES_POINTS_LIGHT, |
66 | SHADER_VARIANT_MAX |
67 | }; |
68 | |
69 | enum { |
70 | |
71 | FLAGS_INSTANCING_MASK = 0x7F, |
72 | FLAGS_INSTANCING_HAS_COLORS = (1 << 7), |
73 | FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8), |
74 | |
75 | FLAGS_CLIP_RECT_UV = (1 << 9), |
76 | FLAGS_TRANSPOSE_RECT = (1 << 10), |
77 | |
78 | FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR = (1 << 11), |
79 | |
80 | FLAGS_NINEPACH_DRAW_CENTER = (1 << 12), |
81 | FLAGS_USING_PARTICLES = (1 << 13), |
82 | |
83 | FLAGS_USE_SKELETON = (1 << 15), |
84 | FLAGS_NINEPATCH_H_MODE_SHIFT = 16, |
85 | FLAGS_NINEPATCH_V_MODE_SHIFT = 18, |
86 | FLAGS_LIGHT_COUNT_SHIFT = 20, |
87 | |
88 | FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 26), |
89 | FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 27), |
90 | |
91 | FLAGS_USE_MSDF = (1 << 28), |
92 | FLAGS_USE_LCD = (1 << 29), |
93 | |
94 | FLAGS_FLIP_H = (1 << 30), |
95 | FLAGS_FLIP_V = (1 << 31), |
96 | }; |
97 | |
98 | enum { |
99 | LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF, |
100 | LIGHT_FLAGS_BLEND_SHIFT = 16, |
101 | LIGHT_FLAGS_BLEND_MASK = (3 << 16), |
102 | LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16), |
103 | LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16), |
104 | LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16), |
105 | LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16), |
106 | LIGHT_FLAGS_HAS_SHADOW = (1 << 20), |
107 | LIGHT_FLAGS_FILTER_SHIFT = 22 |
108 | |
109 | }; |
110 | |
111 | enum { |
112 | MAX_RENDER_ITEMS = 256 * 1024, |
113 | MAX_LIGHT_TEXTURES = 1024, |
114 | MAX_LIGHTS_PER_ITEM = 16, |
115 | DEFAULT_MAX_LIGHTS_PER_RENDER = 256 |
116 | }; |
117 | |
118 | /****************/ |
119 | /**** SHADER ****/ |
120 | /****************/ |
121 | |
122 | enum PipelineVariant { |
123 | PIPELINE_VARIANT_QUAD, |
124 | PIPELINE_VARIANT_NINEPATCH, |
125 | PIPELINE_VARIANT_PRIMITIVE_TRIANGLES, |
126 | PIPELINE_VARIANT_PRIMITIVE_LINES, |
127 | PIPELINE_VARIANT_PRIMITIVE_POINTS, |
128 | PIPELINE_VARIANT_ATTRIBUTE_TRIANGLES, |
129 | PIPELINE_VARIANT_ATTRIBUTE_TRIANGLE_STRIP, |
130 | PIPELINE_VARIANT_ATTRIBUTE_LINES, |
131 | PIPELINE_VARIANT_ATTRIBUTE_LINES_STRIP, |
132 | PIPELINE_VARIANT_ATTRIBUTE_POINTS, |
133 | PIPELINE_VARIANT_QUAD_LCD_BLEND, |
134 | PIPELINE_VARIANT_MAX |
135 | }; |
136 | enum PipelineLightMode { |
137 | PIPELINE_LIGHT_MODE_DISABLED, |
138 | PIPELINE_LIGHT_MODE_ENABLED, |
139 | PIPELINE_LIGHT_MODE_MAX |
140 | }; |
141 | |
142 | struct PipelineVariants { |
143 | PipelineCacheRD variants[PIPELINE_LIGHT_MODE_MAX][PIPELINE_VARIANT_MAX]; |
144 | }; |
145 | |
146 | struct { |
147 | CanvasShaderRD canvas_shader; |
148 | RID default_version; |
149 | RID default_version_rd_shader; |
150 | RID quad_index_buffer; |
151 | RID quad_index_array; |
152 | PipelineVariants pipeline_variants; |
153 | |
154 | ShaderCompiler compiler; |
155 | } shader; |
156 | |
157 | struct CanvasShaderData : public RendererRD::MaterialStorage::ShaderData { |
158 | enum BlendMode { //used internally |
159 | BLEND_MODE_MIX, |
160 | BLEND_MODE_ADD, |
161 | BLEND_MODE_SUB, |
162 | BLEND_MODE_MUL, |
163 | BLEND_MODE_PMALPHA, |
164 | BLEND_MODE_DISABLED, |
165 | }; |
166 | |
167 | bool valid = false; |
168 | RID version; |
169 | PipelineVariants pipeline_variants; |
170 | |
171 | Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms; |
172 | |
173 | Vector<uint32_t> ubo_offsets; |
174 | uint32_t ubo_size = 0; |
175 | |
176 | String code; |
177 | |
178 | bool uses_screen_texture = false; |
179 | bool uses_screen_texture_mipmaps = false; |
180 | bool uses_sdf = false; |
181 | bool uses_time = false; |
182 | |
183 | virtual void set_code(const String &p_Code); |
184 | virtual bool is_animated() const; |
185 | virtual bool casts_shadows() const; |
186 | virtual RS::ShaderNativeSourceCode get_native_source_code() const; |
187 | |
188 | CanvasShaderData() {} |
189 | virtual ~CanvasShaderData(); |
190 | }; |
191 | |
192 | RendererRD::MaterialStorage::ShaderData *_create_shader_func(); |
193 | static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() { |
194 | return static_cast<RendererCanvasRenderRD *>(singleton)->_create_shader_func(); |
195 | } |
196 | |
197 | struct CanvasMaterialData : public RendererRD::MaterialStorage::MaterialData { |
198 | CanvasShaderData *shader_data = nullptr; |
199 | RID uniform_set; |
200 | RID uniform_set_srgb; |
201 | |
202 | virtual void set_render_priority(int p_priority) {} |
203 | virtual void set_next_pass(RID p_pass) {} |
204 | virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty); |
205 | virtual ~CanvasMaterialData(); |
206 | }; |
207 | |
208 | RendererRD::MaterialStorage::MaterialData *_create_material_func(CanvasShaderData *p_shader); |
209 | static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) { |
210 | return static_cast<RendererCanvasRenderRD *>(singleton)->_create_material_func(static_cast<CanvasShaderData *>(p_shader)); |
211 | } |
212 | |
213 | /**************************/ |
214 | /**** CANVAS TEXTURES *****/ |
215 | /**************************/ |
216 | |
217 | struct { |
218 | RS::CanvasItemTextureFilter default_filter; |
219 | RS::CanvasItemTextureRepeat default_repeat; |
220 | } default_samplers; |
221 | |
222 | /******************/ |
223 | /**** POLYGONS ****/ |
224 | /******************/ |
225 | |
226 | struct PolygonBuffers { |
227 | RD::VertexFormatID vertex_format_id; |
228 | RID vertex_buffer; |
229 | RID vertex_array; |
230 | RID index_buffer; |
231 | RID indices; |
232 | }; |
233 | |
234 | struct { |
235 | HashMap<PolygonID, PolygonBuffers> polygons; |
236 | PolygonID last_id; |
237 | } polygon_buffers; |
238 | |
239 | /********************/ |
240 | /**** PRIMITIVES ****/ |
241 | /********************/ |
242 | |
243 | struct { |
244 | RID index_array[4]; |
245 | } primitive_arrays; |
246 | |
247 | /*******************/ |
248 | /**** MATERIALS ****/ |
249 | /*******************/ |
250 | |
251 | /******************/ |
252 | /**** LIGHTING ****/ |
253 | /******************/ |
254 | |
255 | struct CanvasLight { |
256 | RID texture; |
257 | struct { |
258 | bool enabled = false; |
259 | float z_far; |
260 | float y_offset; |
261 | Transform2D directional_xform; |
262 | } shadow; |
263 | }; |
264 | |
265 | RID_Owner<CanvasLight> canvas_light_owner; |
266 | |
267 | struct ShadowRenderPushConstant { |
268 | float projection[16]; |
269 | float modelview[8]; |
270 | float direction[2]; |
271 | float z_far; |
272 | float pad; |
273 | }; |
274 | |
275 | struct OccluderPolygon { |
276 | RS::CanvasOccluderPolygonCullMode cull_mode; |
277 | int line_point_count; |
278 | RID vertex_buffer; |
279 | RID vertex_array; |
280 | RID index_buffer; |
281 | RID index_array; |
282 | |
283 | int sdf_point_count; |
284 | int sdf_index_count; |
285 | RID sdf_vertex_buffer; |
286 | RID sdf_vertex_array; |
287 | RID sdf_index_buffer; |
288 | RID sdf_index_array; |
289 | bool sdf_is_lines; |
290 | }; |
291 | |
292 | struct LightUniform { |
293 | float matrix[8]; //light to texture coordinate matrix |
294 | float shadow_matrix[8]; //light to shadow coordinate matrix |
295 | float color[4]; |
296 | |
297 | uint8_t shadow_color[4]; |
298 | uint32_t flags; //index to light texture |
299 | float shadow_pixel_size; |
300 | float height; |
301 | |
302 | float position[2]; |
303 | float shadow_z_far_inv; |
304 | float shadow_y_ofs; |
305 | |
306 | float atlas_rect[4]; |
307 | }; |
308 | |
309 | RID_Owner<OccluderPolygon> occluder_polygon_owner; |
310 | |
311 | enum ShadowRenderMode { |
312 | SHADOW_RENDER_MODE_SHADOW, |
313 | SHADOW_RENDER_MODE_SDF, |
314 | }; |
315 | |
316 | enum { |
317 | SHADOW_RENDER_SDF_TRIANGLES, |
318 | SHADOW_RENDER_SDF_LINES, |
319 | }; |
320 | |
321 | struct { |
322 | CanvasOcclusionShaderRD shader; |
323 | RID shader_version; |
324 | RID render_pipelines[3]; |
325 | RID sdf_render_pipelines[2]; |
326 | RD::VertexFormatID vertex_format; |
327 | RD::VertexFormatID sdf_vertex_format; |
328 | RD::FramebufferFormatID framebuffer_format; |
329 | RD::FramebufferFormatID sdf_framebuffer_format; |
330 | } shadow_render; |
331 | |
332 | /***************/ |
333 | /**** STATE ****/ |
334 | /***************/ |
335 | |
336 | //state that does not vary across rendering all items |
337 | |
338 | struct State { |
339 | //state buffer |
340 | struct Buffer { |
341 | float canvas_transform[16]; |
342 | float screen_transform[16]; |
343 | float canvas_normal_transform[16]; |
344 | float canvas_modulate[4]; |
345 | |
346 | float screen_pixel_size[2]; |
347 | float time; |
348 | uint32_t use_pixel_snap; |
349 | |
350 | float sdf_to_tex[4]; |
351 | float sdf_to_screen[2]; |
352 | float screen_to_sdf[2]; |
353 | |
354 | uint32_t directional_light_count; |
355 | float tex_to_sdf; |
356 | uint32_t pad1; |
357 | uint32_t pad2; |
358 | }; |
359 | |
360 | LightUniform *light_uniforms = nullptr; |
361 | |
362 | RID lights_uniform_buffer; |
363 | RID canvas_state_buffer; |
364 | RID shadow_sampler; |
365 | RID shadow_texture; |
366 | RID shadow_depth_texture; |
367 | RID shadow_fb; |
368 | int shadow_texture_size = 2048; |
369 | |
370 | RID default_transforms_uniform_set; |
371 | |
372 | uint32_t max_lights_per_render; |
373 | uint32_t max_lights_per_item; |
374 | |
375 | double time; |
376 | |
377 | } state; |
378 | |
379 | struct PushConstant { |
380 | float world[6]; |
381 | uint32_t flags; |
382 | uint32_t specular_shininess; |
383 | union { |
384 | //rect |
385 | struct { |
386 | float modulation[4]; |
387 | union { |
388 | float msdf[4]; |
389 | float ninepatch_margins[4]; |
390 | }; |
391 | float dst_rect[4]; |
392 | float src_rect[4]; |
393 | float pad[2]; |
394 | }; |
395 | //primitive |
396 | struct { |
397 | float points[6]; // vec2 points[3] |
398 | float uvs[6]; // vec2 points[3] |
399 | uint32_t colors[6]; // colors encoded as half |
400 | }; |
401 | }; |
402 | float color_texture_pixel_size[2]; |
403 | uint32_t lights[4]; |
404 | }; |
405 | |
406 | Item *items[MAX_RENDER_ITEMS]; |
407 | |
408 | bool using_directional_lights = false; |
409 | RID default_canvas_texture; |
410 | |
411 | RID default_canvas_group_shader; |
412 | RID default_canvas_group_material; |
413 | RID default_clip_children_material; |
414 | RID default_clip_children_shader; |
415 | |
416 | RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR; |
417 | RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED; |
418 | |
419 | RID _create_base_uniform_set(RID p_to_render_target, bool p_backbuffer); |
420 | |
421 | inline void _bind_canvas_texture(RD::DrawListID p_draw_list, RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, RID &r_last_texture, PushConstant &push_constant, Size2 &r_texpixel_size, bool p_texture_is_data = false); //recursive, so regular inline used instead. |
422 | void _render_item(RenderingDevice::DrawListID p_draw_list, RID p_render_target, const Item *p_item, RenderingDevice::FramebufferFormatID p_framebuffer_format, const Transform2D &p_canvas_transform_inverse, Item *¤t_clip, Light *p_lights, PipelineVariants *p_pipeline_variants, bool &r_sdf_used); |
423 | 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); |
424 | |
425 | _FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4); |
426 | _FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3); |
427 | |
428 | _FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4); |
429 | _FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4); |
430 | |
431 | void _update_shadow_atlas(); |
432 | |
433 | public: |
434 | 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>()); |
435 | void free_polygon(PolygonID p_polygon); |
436 | |
437 | RID light_create(); |
438 | void light_set_texture(RID p_rid, RID p_texture); |
439 | void light_set_use_shadow(RID p_rid, bool p_enable); |
440 | 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); |
441 | 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); |
442 | |
443 | virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders); |
444 | |
445 | RID occluder_polygon_create(); |
446 | void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed); |
447 | void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode); |
448 | |
449 | void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_light_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); |
450 | |
451 | virtual void set_shadow_texture_size(int p_size); |
452 | |
453 | void set_time(double p_time); |
454 | void update(); |
455 | bool free(RID p_rid); |
456 | RendererCanvasRenderRD(); |
457 | ~RendererCanvasRenderRD(); |
458 | }; |
459 | |
460 | #endif // RENDERER_CANVAS_RENDER_RD_H |
461 | |