Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/renderer_canvas_render_rd.h
10278 views
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
#pragma once
32
33
#include "core/templates/lru.h"
34
#include "servers/rendering/renderer_canvas_render.h"
35
#include "servers/rendering/renderer_rd/pipeline_hash_map_rd.h"
36
#include "servers/rendering/renderer_rd/shaders/canvas.glsl.gen.h"
37
#include "servers/rendering/renderer_rd/shaders/canvas_occlusion.glsl.gen.h"
38
#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
39
#include "servers/rendering/rendering_device.h"
40
#include "servers/rendering/shader_compiler.h"
41
42
class RendererCanvasRenderRD : public RendererCanvasRender {
43
enum {
44
BASE_UNIFORM_SET = 0,
45
MATERIAL_UNIFORM_SET = 1,
46
TRANSFORMS_UNIFORM_SET = 2,
47
BATCH_UNIFORM_SET = 3,
48
};
49
50
const int SAMPLERS_BINDING_FIRST_INDEX = 10;
51
// The size of the ring buffer to store GPU buffers. Triple-buffering the max expected frames in flight.
52
static const uint32_t BATCH_DATA_BUFFER_COUNT = 3;
53
54
enum ShaderVariant {
55
SHADER_VARIANT_QUAD,
56
SHADER_VARIANT_NINEPATCH,
57
SHADER_VARIANT_PRIMITIVE,
58
SHADER_VARIANT_PRIMITIVE_POINTS,
59
SHADER_VARIANT_ATTRIBUTES,
60
SHADER_VARIANT_ATTRIBUTES_POINTS,
61
SHADER_VARIANT_MAX
62
};
63
64
enum {
65
INSTANCE_FLAGS_LIGHT_COUNT_SHIFT = 0, // 4 bits for light count.
66
67
INSTANCE_FLAGS_CLIP_RECT_UV = (1 << 4),
68
INSTANCE_FLAGS_TRANSPOSE_RECT = (1 << 5),
69
INSTANCE_FLAGS_USE_MSDF = (1 << 6),
70
INSTANCE_FLAGS_USE_LCD = (1 << 7),
71
72
INSTANCE_FLAGS_NINEPACH_DRAW_CENTER = (1 << 8),
73
INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT = 9,
74
INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT = 11,
75
76
INSTANCE_FLAGS_SHADOW_MASKED_SHIFT = 13, // 16 bits.
77
};
78
79
enum {
80
BATCH_FLAGS_INSTANCING_MASK = 0x7F,
81
BATCH_FLAGS_INSTANCING_HAS_COLORS = (1 << 7),
82
BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8),
83
84
BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 9),
85
BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 10),
86
};
87
88
enum {
89
CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR = (1 << 0),
90
};
91
92
enum {
93
LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF,
94
LIGHT_FLAGS_BLEND_SHIFT = 16,
95
LIGHT_FLAGS_BLEND_MASK = (3 << 16),
96
LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16),
97
LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16),
98
LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16),
99
LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16),
100
LIGHT_FLAGS_HAS_SHADOW = (1 << 20),
101
LIGHT_FLAGS_FILTER_SHIFT = 22
102
103
};
104
105
enum {
106
MAX_RENDER_ITEMS = 256 * 1024,
107
MAX_LIGHT_TEXTURES = 1024,
108
MAX_LIGHTS_PER_ITEM = 16,
109
MAX_LIGHTS_PER_RENDER = 256,
110
};
111
112
/****************/
113
/**** SHADER ****/
114
/****************/
115
116
struct ShaderSpecialization {
117
union {
118
uint32_t packed_0;
119
120
struct {
121
uint32_t use_lighting : 1;
122
};
123
};
124
};
125
126
struct PipelineKey {
127
ShaderVariant variant = SHADER_VARIANT_MAX;
128
RD::FramebufferFormatID framebuffer_format_id = RD::INVALID_FORMAT_ID;
129
RD::VertexFormatID vertex_format_id = RD::INVALID_ID;
130
RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_MAX;
131
ShaderSpecialization shader_specialization = {};
132
uint32_t lcd_blend = 0;
133
uint32_t ubershader = 0;
134
135
uint32_t hash() const {
136
uint32_t h = hash_murmur3_one_32(variant);
137
h = hash_murmur3_one_32(framebuffer_format_id, h);
138
h = hash_murmur3_one_64((uint64_t)vertex_format_id, h);
139
h = hash_murmur3_one_32(render_primitive, h);
140
h = hash_murmur3_one_32(shader_specialization.packed_0, h);
141
h = hash_murmur3_one_32(lcd_blend, h);
142
h = hash_murmur3_one_32(ubershader, h);
143
return hash_fmix32(h);
144
}
145
};
146
147
struct CanvasShaderData : public RendererRD::MaterialStorage::ShaderData {
148
Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
149
int blend_mode = 0;
150
151
Vector<uint32_t> ubo_offsets;
152
uint32_t ubo_size = 0;
153
154
String code;
155
RID version;
156
PipelineHashMapRD<PipelineKey, CanvasShaderData, void (CanvasShaderData::*)(PipelineKey)> pipeline_hash_map;
157
158
static const uint32_t VERTEX_INPUT_MASKS_SIZE = SHADER_VARIANT_MAX * 2;
159
std::atomic<uint64_t> vertex_input_masks[VERTEX_INPUT_MASKS_SIZE] = {};
160
161
bool uses_screen_texture = false;
162
bool uses_screen_texture_mipmaps = false;
163
bool uses_sdf = false;
164
bool uses_time = false;
165
166
void _clear_vertex_input_mask_cache();
167
void _create_pipeline(PipelineKey p_pipeline_key);
168
virtual void set_code(const String &p_Code);
169
virtual bool is_animated() const;
170
virtual bool casts_shadows() const;
171
virtual RS::ShaderNativeSourceCode get_native_source_code() const;
172
virtual Pair<ShaderRD *, RID> get_native_shader_and_version() const;
173
RID get_shader(ShaderVariant p_shader_variant, bool p_ubershader) const;
174
uint64_t get_vertex_input_mask(ShaderVariant p_shader_variant, bool p_ubershader);
175
bool is_valid() const;
176
177
CanvasShaderData();
178
virtual ~CanvasShaderData();
179
};
180
181
struct {
182
// Data must be guaranteed to be erased before the rest on the destructor.
183
CanvasShaderData *default_version_data = nullptr;
184
CanvasShaderRD canvas_shader;
185
RID default_version_rd_shader;
186
RID quad_index_buffer;
187
RID quad_index_array;
188
ShaderCompiler compiler;
189
uint32_t pipeline_compilations[RS::PIPELINE_SOURCE_MAX] = {};
190
Mutex mutex;
191
} shader;
192
193
RendererRD::MaterialStorage::ShaderData *_create_shader_func();
194
static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() {
195
return static_cast<RendererCanvasRenderRD *>(singleton)->_create_shader_func();
196
}
197
198
struct CanvasMaterialData : public RendererRD::MaterialStorage::MaterialData {
199
CanvasShaderData *shader_data = nullptr;
200
RID uniform_set;
201
RID uniform_set_srgb;
202
203
virtual void set_render_priority(int p_priority) {}
204
virtual void set_next_pass(RID p_pass) {}
205
virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
206
virtual ~CanvasMaterialData();
207
};
208
209
RendererRD::MaterialStorage::MaterialData *_create_material_func(CanvasShaderData *p_shader);
210
static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
211
return static_cast<RendererCanvasRenderRD *>(singleton)->_create_material_func(static_cast<CanvasShaderData *>(p_shader));
212
}
213
214
/**************************/
215
/**** CANVAS TEXTURES *****/
216
/**************************/
217
218
struct {
219
RS::CanvasItemTextureFilter default_filter;
220
RS::CanvasItemTextureRepeat default_repeat;
221
} default_samplers;
222
223
/******************/
224
/**** POLYGONS ****/
225
/******************/
226
227
struct PolygonBuffers {
228
RD::VertexFormatID vertex_format_id;
229
RID vertex_buffer;
230
RID vertex_array;
231
RID index_buffer;
232
RID indices;
233
uint32_t primitive_count = 0;
234
};
235
236
struct {
237
HashMap<PolygonID, PolygonBuffers> polygons;
238
PolygonID last_id;
239
} polygon_buffers;
240
241
/********************/
242
/**** PRIMITIVES ****/
243
/********************/
244
245
struct {
246
RID index_array[4];
247
} primitive_arrays;
248
249
/*******************/
250
/**** MATERIALS ****/
251
/*******************/
252
253
/******************/
254
/**** LIGHTING ****/
255
/******************/
256
257
struct CanvasLight {
258
RID texture;
259
struct {
260
bool enabled = false;
261
float z_far;
262
float y_offset;
263
Transform2D directional_xform;
264
} shadow;
265
};
266
267
RID_Owner<CanvasLight> canvas_light_owner;
268
269
struct PositionalShadowRenderPushConstant {
270
float modelview[8];
271
float rotation[4];
272
float direction[2];
273
float z_far;
274
uint32_t pad;
275
float z_near;
276
uint32_t cull_mode;
277
float pad2[2];
278
};
279
280
struct ShadowRenderPushConstant {
281
float projection[16];
282
float modelview[8];
283
float direction[2];
284
float z_far;
285
uint32_t cull_mode;
286
};
287
288
struct OccluderPolygon {
289
RS::CanvasOccluderPolygonCullMode cull_mode;
290
int line_point_count;
291
RID vertex_buffer;
292
RID vertex_array;
293
RID index_buffer;
294
RID index_array;
295
296
int sdf_point_count;
297
int sdf_index_count;
298
RID sdf_vertex_buffer;
299
RID sdf_vertex_array;
300
RID sdf_index_buffer;
301
RID sdf_index_array;
302
bool sdf_is_lines;
303
};
304
305
struct LightUniform {
306
float matrix[8]; //light to texture coordinate matrix
307
float shadow_matrix[8]; //light to shadow coordinate matrix
308
float color[4];
309
310
uint8_t shadow_color[4];
311
uint32_t flags; //index to light texture
312
float shadow_pixel_size;
313
float height;
314
315
float position[2];
316
float shadow_z_far_inv;
317
float shadow_y_ofs;
318
319
float atlas_rect[4];
320
};
321
322
RID_Owner<OccluderPolygon> occluder_polygon_owner;
323
324
enum ShadowRenderMode {
325
SHADOW_RENDER_MODE_DIRECTIONAL_SHADOW,
326
SHADOW_RENDER_MODE_POSITIONAL_SHADOW,
327
SHADOW_RENDER_MODE_SDF,
328
};
329
330
enum {
331
SHADOW_RENDER_SDF_TRIANGLES,
332
SHADOW_RENDER_SDF_LINES,
333
};
334
335
struct {
336
CanvasOcclusionShaderRD shader;
337
RID shader_version;
338
RID render_pipelines[2];
339
RID sdf_render_pipelines[2];
340
RD::VertexFormatID vertex_format;
341
RD::VertexFormatID sdf_vertex_format;
342
RD::FramebufferFormatID framebuffer_format;
343
RD::FramebufferFormatID sdf_framebuffer_format;
344
} shadow_render;
345
346
/***************/
347
/**** STATE ****/
348
/***************/
349
350
//state that does not vary across rendering all items
351
352
struct InstanceData {
353
float world[6];
354
uint32_t flags;
355
uint32_t instance_uniforms_ofs;
356
union {
357
//rect
358
struct {
359
float modulation[4];
360
union {
361
float msdf[4];
362
float ninepatch_margins[4];
363
};
364
float dst_rect[4];
365
float src_rect[4];
366
float pad[2];
367
};
368
//primitive
369
struct {
370
float points[6]; // vec2 points[3]
371
float uvs[6]; // vec2 points[3]
372
uint32_t colors[6]; // colors encoded as half
373
};
374
};
375
float color_texture_pixel_size[2];
376
uint32_t lights[4];
377
};
378
379
struct PushConstant {
380
uint32_t base_instance_index;
381
ShaderSpecialization shader_specialization;
382
uint32_t specular_shininess;
383
uint32_t batch_flags;
384
};
385
386
// TextureState is used to determine when a new batch is required due to a change of texture state.
387
struct TextureState {
388
static const uint32_t FILTER_SHIFT = 0;
389
static const uint32_t FILTER_BITS = 3;
390
static const uint32_t FILTER_MASK = (1 << FILTER_BITS) - 1;
391
static const uint32_t REPEAT_SHIFT = FILTER_BITS;
392
static const uint32_t REPEAT_BITS = 2;
393
static const uint32_t REPEAT_MASK = (1 << REPEAT_BITS) - 1;
394
static const uint32_t TEXTURE_IS_DATA_SHIFT = REPEAT_SHIFT + REPEAT_BITS;
395
static const uint32_t TEXTURE_IS_DATA_BITS = 1;
396
static const uint32_t TEXTURE_IS_DATA_MASK = (1 << TEXTURE_IS_DATA_BITS) - 1;
397
static const uint32_t LINEAR_COLORS_SHIFT = TEXTURE_IS_DATA_SHIFT + TEXTURE_IS_DATA_BITS;
398
static const uint32_t LINEAR_COLORS_BITS = 1;
399
static const uint32_t LINEAR_COLORS_MASK = (1 << LINEAR_COLORS_BITS) - 1;
400
401
RID texture;
402
uint32_t other = 0;
403
404
TextureState() {}
405
406
TextureState(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, bool p_texture_is_data, bool p_use_linear_colors) {
407
texture = p_texture;
408
other = (((uint32_t)p_base_filter & FILTER_MASK) << FILTER_SHIFT) |
409
(((uint32_t)p_base_repeat & REPEAT_MASK) << REPEAT_SHIFT) |
410
(((uint32_t)p_texture_is_data & TEXTURE_IS_DATA_MASK) << TEXTURE_IS_DATA_SHIFT) |
411
(((uint32_t)p_use_linear_colors & LINEAR_COLORS_MASK) << LINEAR_COLORS_SHIFT);
412
}
413
414
_ALWAYS_INLINE_ RS::CanvasItemTextureFilter texture_filter() const {
415
return (RS::CanvasItemTextureFilter)((other >> FILTER_SHIFT) & FILTER_MASK);
416
}
417
418
_ALWAYS_INLINE_ RS::CanvasItemTextureRepeat texture_repeat() const {
419
return (RS::CanvasItemTextureRepeat)((other >> REPEAT_SHIFT) & REPEAT_MASK);
420
}
421
422
_ALWAYS_INLINE_ bool linear_colors() const {
423
return (other >> LINEAR_COLORS_SHIFT) & LINEAR_COLORS_MASK;
424
}
425
426
_ALWAYS_INLINE_ bool texture_is_data() const {
427
return (other >> TEXTURE_IS_DATA_SHIFT) & TEXTURE_IS_DATA_MASK;
428
}
429
430
_ALWAYS_INLINE_ bool operator==(const TextureState &p_val) const {
431
return (texture == p_val.texture) && (other == p_val.other);
432
}
433
434
_ALWAYS_INLINE_ bool operator!=(const TextureState &p_val) const {
435
return (texture != p_val.texture) || (other != p_val.other);
436
}
437
438
_ALWAYS_INLINE_ bool is_valid() const { return texture.is_valid(); }
439
_ALWAYS_INLINE_ bool is_null() const { return texture.is_null(); }
440
441
uint32_t hash() const {
442
uint32_t hash = hash_murmur3_one_64(texture.get_id());
443
return hash_murmur3_one_32(other, hash);
444
}
445
};
446
447
struct TextureInfo {
448
TextureState state;
449
RID diffuse;
450
RID normal;
451
RID specular;
452
RID sampler;
453
Vector2 texpixel_size;
454
uint32_t specular_shininess = 0;
455
uint32_t flags = 0;
456
};
457
458
/// A key used to uniquely identify a distinct BATCH_UNIFORM_SET
459
struct RIDSetKey {
460
TextureState state;
461
RID instance_data;
462
463
RIDSetKey() {
464
}
465
466
RIDSetKey(TextureState p_state, RID p_instance_data) :
467
state(p_state),
468
instance_data(p_instance_data) {
469
}
470
471
_ALWAYS_INLINE_ bool operator==(const RIDSetKey &p_val) const {
472
return state == p_val.state && instance_data == p_val.instance_data;
473
}
474
475
_ALWAYS_INLINE_ bool operator!=(const RIDSetKey &p_val) const {
476
return !(*this == p_val);
477
}
478
479
_ALWAYS_INLINE_ uint32_t hash() const {
480
uint32_t h = state.hash();
481
h = hash_murmur3_one_64(instance_data.get_id(), h);
482
return hash_fmix32(h);
483
}
484
};
485
486
static void _before_evict(RendererCanvasRenderRD::RIDSetKey &p_key, RID &p_rid);
487
static void _uniform_set_invalidation_callback(void *p_userdata);
488
static void _canvas_texture_invalidation_callback(bool p_deleted, void *p_userdata);
489
490
typedef LRUCache<RIDSetKey, RID, HashableHasher<RIDSetKey>, HashMapComparatorDefault<RIDSetKey>, _before_evict> RIDCache;
491
RIDCache rid_set_to_uniform_set;
492
/// Maps a CanvasTexture to its associated uniform sets, which must
493
/// be invalidated when the CanvasTexture is updated, such as changing the
494
/// diffuse texture.
495
HashMap<RID, TightLocalVector<RID>> canvas_texture_to_uniform_set;
496
497
struct Batch {
498
// Position in the UBO measured in bytes
499
uint32_t start = 0;
500
uint32_t instance_count = 0;
501
uint32_t instance_buffer_index = 0;
502
503
TextureInfo *tex_info;
504
505
Color modulate = Color(1.0, 1.0, 1.0, 1.0);
506
507
Item *clip = nullptr;
508
509
RID material;
510
CanvasMaterialData *material_data = nullptr;
511
512
const Item::Command *command = nullptr;
513
Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch.
514
ShaderVariant shader_variant = SHADER_VARIANT_QUAD;
515
RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES;
516
bool use_lighting = false;
517
518
// batch-specific data
519
union {
520
// TYPE_PRIMITIVE
521
uint32_t primitive_points = 0;
522
// TYPE_PARTICLES
523
uint32_t mesh_instance_count;
524
};
525
bool has_blend = false;
526
uint32_t flags = 0;
527
};
528
529
HashMap<TextureState, TextureInfo, HashableHasher<TextureState>, HashMapComparatorDefault<TextureState>, PagedAllocator<HashMapElement<TextureState, TextureInfo>>> texture_info_map;
530
531
// per-frame buffers
532
struct DataBuffer {
533
LocalVector<RID> instance_buffers;
534
};
535
536
struct State {
537
//state buffer
538
struct Buffer {
539
float canvas_transform[16];
540
float screen_transform[16];
541
float canvas_normal_transform[16];
542
float canvas_modulate[4];
543
544
float screen_pixel_size[2];
545
float time;
546
uint32_t use_pixel_snap;
547
548
float sdf_to_tex[4];
549
float sdf_to_screen[2];
550
float screen_to_sdf[2];
551
552
uint32_t directional_light_count;
553
float tex_to_sdf;
554
float shadow_pixel_size;
555
uint32_t flags;
556
};
557
558
DataBuffer canvas_instance_data_buffers[BATCH_DATA_BUFFER_COUNT];
559
LocalVector<Batch> canvas_instance_batches;
560
uint32_t current_data_buffer_index = 0;
561
uint32_t current_instance_buffer_index = 0;
562
uint32_t current_batch_index = 0;
563
uint32_t last_instance_index = 0;
564
InstanceData *instance_data_array = nullptr;
565
566
uint32_t max_instances_per_buffer = 16384;
567
uint32_t max_instance_buffer_size = 16384 * sizeof(InstanceData);
568
569
Vector<RD::Uniform> batch_texture_uniforms;
570
RID current_batch_uniform_set;
571
572
LightUniform *light_uniforms = nullptr;
573
574
RID lights_storage_buffer;
575
RID canvas_state_buffer;
576
RID shadow_sampler;
577
RID shadow_texture;
578
RID shadow_depth_texture;
579
RID shadow_fb;
580
int shadow_texture_size = 2048;
581
582
RID shadow_occluder_buffer;
583
uint32_t shadow_occluder_buffer_size;
584
RID shadow_ocluder_uniform_set;
585
586
RID default_transforms_uniform_set;
587
588
double time;
589
590
} state;
591
592
Item *items[MAX_RENDER_ITEMS];
593
594
TextureInfo default_texture_info;
595
596
bool using_directional_lights = false;
597
RID default_canvas_texture;
598
599
RID default_canvas_group_shader;
600
RID default_canvas_group_material;
601
RID default_clip_children_material;
602
RID default_clip_children_shader;
603
604
RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR;
605
RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;
606
607
RID _create_base_uniform_set(RID p_to_render_target, bool p_backbuffer);
608
609
bool debug_redraw = false;
610
Color debug_redraw_color;
611
double debug_redraw_time = 1.0;
612
613
// A structure to store cached render target information
614
struct RenderTarget {
615
// Current render target for the canvas.
616
RID render_target;
617
bool use_linear_colors = false;
618
};
619
620
inline RID _get_pipeline_specialization_or_ubershader(CanvasShaderData *p_shader_data, PipelineKey &r_pipeline_key, PushConstant &r_push_constant, RID p_mesh_instance = RID(), void *p_surface = nullptr, uint32_t p_surface_index = 0, RID *r_vertex_array = nullptr);
621
void _render_batch_items(RenderTarget 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);
622
void _record_item_commands(const Item *p_item, RenderTarget p_render_target, const Transform2D &p_base_transform, Item *&r_current_clip, Light *p_lights, uint32_t &r_index, bool &r_batch_broken, bool &r_sdf_used, Batch *&r_current_batch);
623
void _render_batch(RD::DrawListID p_draw_list, CanvasShaderData *p_shader_data, RenderingDevice::FramebufferFormatID p_framebuffer_format, Light *p_lights, Batch const *p_batch, RenderingMethod::RenderInfo *r_render_info = nullptr);
624
void _prepare_batch_texture_info(RID p_texture, TextureState &p_state, TextureInfo *p_info);
625
InstanceData *new_instance_data(float *p_world, uint32_t *p_lights, uint32_t p_base_flags, uint32_t p_index, uint32_t p_uniforms_ofs, TextureInfo *p_info);
626
[[nodiscard]] Batch *_new_batch(bool &r_batch_broken);
627
void _add_to_batch(uint32_t &r_index, bool &r_batch_broken, Batch *&r_current_batch);
628
void _allocate_instance_buffer();
629
630
_FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4);
631
_FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3);
632
633
_FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4);
634
_FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4);
635
636
void _update_shadow_atlas();
637
void _update_occluder_buffer(uint32_t p_size);
638
639
public:
640
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;
641
void free_polygon(PolygonID p_polygon) override;
642
643
RID light_create() override;
644
void light_set_texture(RID p_rid, RID p_texture) override;
645
void light_set_use_shadow(RID p_rid, bool p_enable) override;
646
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;
647
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;
648
649
virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override;
650
651
RID occluder_polygon_create() override;
652
void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override;
653
void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override;
654
655
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, RenderingMethod::RenderInfo *r_render_info = nullptr) override;
656
657
virtual void set_shadow_texture_size(int p_size) override;
658
659
void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) override;
660
uint32_t get_pipeline_compilations(RS::PipelineSource p_source) override;
661
662
void set_time(double p_time);
663
void update() override;
664
bool free(RID p_rid) override;
665
RendererCanvasRenderRD();
666
~RendererCanvasRenderRD();
667
};
668
669