1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
/*************************************************************************/
/* renderer_canvas_render.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef RENDERINGSERVERCANVASRENDER_H
#define RENDERINGSERVERCANVASRENDER_H
#include "servers/rendering/renderer_rd/storage_rd/mesh_storage.h"
#include "servers/rendering/renderer_storage.h"
class RendererCanvasRender {
public:
static RendererCanvasRender *singleton;
enum CanvasRectFlags {
CANVAS_RECT_REGION = 1,
CANVAS_RECT_TILE = 2,
CANVAS_RECT_FLIP_H = 4,
CANVAS_RECT_FLIP_V = 8,
CANVAS_RECT_TRANSPOSE = 16,
CANVAS_RECT_CLIP_UV = 32,
CANVAS_RECT_IS_GROUP = 64,
CANVAS_RECT_MSDF = 128,
};
struct Light {
bool enabled;
Color color;
Transform2D xform;
float height;
float energy;
float scale;
int z_min;
int z_max;
int layer_min;
int layer_max;
int item_mask;
int item_shadow_mask;
float directional_distance;
RS::CanvasLightMode mode;
RS::CanvasLightBlendMode blend_mode;
RID texture;
Vector2 texture_offset;
RID canvas;
bool use_shadow;
int shadow_buffer_size;
RS::CanvasLightShadowFilter shadow_filter;
Color shadow_color;
float shadow_smooth;
//void *texture_cache; // implementation dependent
Rect2 rect_cache;
Transform2D xform_cache;
float radius_cache; //used for shadow far plane
//CameraMatrix shadow_matrix_cache;
Transform2D light_shader_xform;
//Vector2 light_shader_pos;
Light *shadows_next_ptr = nullptr;
Light *filter_next_ptr = nullptr;
Light *next_ptr = nullptr;
Light *directional_next_ptr = nullptr;
RID light_internal;
uint64_t version;
int32_t render_index_cache;
Light() {
version = 0;
enabled = true;
color = Color(1, 1, 1);
shadow_color = Color(0, 0, 0, 0);
height = 0;
z_min = -1024;
z_max = 1024;
layer_min = 0;
layer_max = 0;
item_mask = 1;
scale = 1.0;
energy = 1.0;
item_shadow_mask = 1;
mode = RS::CANVAS_LIGHT_MODE_POINT;
blend_mode = RS::CANVAS_LIGHT_BLEND_MODE_ADD;
// texture_cache = nullptr;
next_ptr = nullptr;
directional_next_ptr = nullptr;
filter_next_ptr = nullptr;
use_shadow = false;
shadow_buffer_size = 2048;
shadow_filter = RS::CANVAS_LIGHT_FILTER_NONE;
shadow_smooth = 0.0;
render_index_cache = -1;
directional_distance = 10000.0;
}
};
//easier wrap to avoid mistakes
struct Item;
typedef uint64_t PolygonID;
virtual 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>()) = 0;
virtual void free_polygon(PolygonID p_polygon) = 0;
//also easier to wrap to avoid mistakes
struct Polygon {
PolygonID polygon_id;
Rect2 rect_cache;
_FORCE_INLINE_ void create(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>()) {
ERR_FAIL_COND(polygon_id != 0);
{
uint32_t pc = p_points.size();
const Vector2 *v2 = p_points.ptr();
rect_cache.position = *v2;
for (uint32_t i = 1; i < pc; i++) {
rect_cache.expand_to(v2[i]);
}
}
polygon_id = singleton->request_polygon(p_indices, p_points, p_colors, p_uvs, p_bones, p_weights);
}
_FORCE_INLINE_ Polygon() { polygon_id = 0; }
_FORCE_INLINE_ ~Polygon() {
if (polygon_id) {
singleton->free_polygon(polygon_id);
}
}
};
//item
struct Item {
//commands are allocated in blocks of 4k to improve performance
//and cache coherence.
//blocks always grow but never shrink.
struct CommandBlock {
enum {
MAX_SIZE = 4096
};
uint32_t usage;
uint8_t *memory = nullptr;
};
struct Command {
enum Type {
TYPE_RECT,
TYPE_NINEPATCH,
TYPE_POLYGON,
TYPE_PRIMITIVE,
TYPE_MESH,
TYPE_MULTIMESH,
TYPE_PARTICLES,
TYPE_TRANSFORM,
TYPE_CLIP_IGNORE,
TYPE_ANIMATION_SLICE,
};
Command *next = nullptr;
Type type;
virtual ~Command() {}
};
struct CommandRect : public Command {
Rect2 rect;
Color modulate;
Rect2 source;
uint8_t flags;
float outline;
float px_range;
RID texture;
CommandRect() {
flags = 0;
outline = 0;
px_range = 1;
type = TYPE_RECT;
}
};
struct CommandNinePatch : public Command {
Rect2 rect;
Rect2 source;
float margin[4];
bool draw_center;
Color color;
RS::NinePatchAxisMode axis_x;
RS::NinePatchAxisMode axis_y;
RID texture;
CommandNinePatch() {
draw_center = true;
type = TYPE_NINEPATCH;
}
};
struct CommandPolygon : public Command {
RS::PrimitiveType primitive;
Polygon polygon;
RID texture;
CommandPolygon() {
type = TYPE_POLYGON;
}
};
struct CommandPrimitive : public Command {
uint32_t point_count;
Vector2 points[4];
Vector2 uvs[4];
Color colors[4];
RID texture;
CommandPrimitive() {
type = TYPE_PRIMITIVE;
}
};
struct CommandMesh : public Command {
RID mesh;
Transform2D transform;
Color modulate;
RID mesh_instance;
RID texture;
CommandMesh() { type = TYPE_MESH; }
~CommandMesh() {
if (mesh_instance.is_valid()) {
RendererStorage::base_singleton->free(mesh_instance);
}
}
};
struct CommandMultiMesh : public Command {
RID multimesh;
RID texture;
CommandMultiMesh() { type = TYPE_MULTIMESH; }
};
struct CommandParticles : public Command {
RID particles;
RID texture;
CommandParticles() { type = TYPE_PARTICLES; }
};
struct CommandTransform : public Command {
Transform2D xform;
CommandTransform() { type = TYPE_TRANSFORM; }
};
struct CommandClipIgnore : public Command {
bool ignore;
CommandClipIgnore() {
type = TYPE_CLIP_IGNORE;
ignore = false;
}
};
struct CommandAnimationSlice : public Command {
double animation_length = 0;
double slice_begin = 0;
double slice_end = 0;
double offset = 0;
CommandAnimationSlice() {
type = TYPE_ANIMATION_SLICE;
}
};
struct ViewportRender {
RenderingServer *owner = nullptr;
void *udata = nullptr;
Rect2 rect;
};
Transform2D xform;
bool clip;
bool visible;
bool behind;
bool update_when_visible;
struct CanvasGroup {
RS::CanvasGroupMode mode;
bool fit_empty;
float fit_margin;
bool blur_mipmaps;
float clear_margin;
};
CanvasGroup *canvas_group = nullptr;
int light_mask;
int z_final;
mutable bool custom_rect;
mutable bool rect_dirty;
mutable Rect2 rect;
RID material;
RID skeleton;
Item *next = nullptr;
struct CopyBackBuffer {
Rect2 rect;
Rect2 screen_rect;
bool full;
};
CopyBackBuffer *copy_back_buffer = nullptr;
Color final_modulate;
Transform2D final_transform;
Rect2 final_clip_rect;
Item *final_clip_owner = nullptr;
Item *material_owner = nullptr;
Item *canvas_group_owner = nullptr;
ViewportRender *vp_render = nullptr;
bool distance_field;
bool light_masked;
Rect2 global_rect_cache;
const Rect2 &get_rect() const {
if (custom_rect || (!rect_dirty && !update_when_visible)) {
return rect;
}
//must update rect
if (commands == nullptr) {
rect = Rect2();
rect_dirty = false;
return rect;
}
Transform2D xf;
bool found_xform = false;
bool first = true;
const Item::Command *c = commands;
while (c) {
Rect2 r;
switch (c->type) {
case Item::Command::TYPE_RECT: {
const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);
r = crect->rect;
} break;
case Item::Command::TYPE_NINEPATCH: {
const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);
r = style->rect;
} break;
case Item::Command::TYPE_POLYGON: {
const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
r = polygon->polygon.rect_cache;
} break;
case Item::Command::TYPE_PRIMITIVE: {
const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
for (uint32_t j = 0; j < primitive->point_count; j++) {
if (j == 0) {
r.position = primitive->points[0];
} else {
r.expand_to(primitive->points[j]);
}
}
} break;
case Item::Command::TYPE_MESH: {
const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
AABB aabb = RendererRD::MeshStorage::get_singleton()->mesh_get_aabb(mesh->mesh, RID());
r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
} break;
case Item::Command::TYPE_MULTIMESH: {
const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);
AABB aabb = RendererRD::MeshStorage::get_singleton()->multimesh_get_aabb(multimesh->multimesh);
r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
} break;
case Item::Command::TYPE_PARTICLES: {
const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);
if (particles_cmd->particles.is_valid()) {
AABB aabb = RendererStorage::base_singleton->particles_get_aabb(particles_cmd->particles);
r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
}
} break;
case Item::Command::TYPE_TRANSFORM: {
const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
xf = transform->xform;
found_xform = true;
[[fallthrough]];
}
default: {
c = c->next;
continue;
}
}
if (found_xform) {
r = xf.xform(r);
}
if (first) {
rect = r;
first = false;
} else {
rect = rect.merge(r);
}
c = c->next;
}
rect_dirty = false;
return rect;
}
Command *commands = nullptr;
Command *last_command = nullptr;
Vector<CommandBlock> blocks;
uint32_t current_block;
template <class T>
T *alloc_command() {
T *command = nullptr;
if (commands == nullptr) {
// As the most common use case of canvas items is to
// use only one command, the first is done with it's
// own allocation. The rest of them use blocks.
command = memnew(T);
command->next = nullptr;
commands = command;
last_command = command;
} else {
//Subsequent commands go into a block.
while (true) {
if (unlikely(current_block == (uint32_t)blocks.size())) {
// If we need more blocks, we allocate them
// (they won't be freed until this CanvasItem is
// deleted, though).
CommandBlock cb;
cb.memory = (uint8_t *)memalloc(CommandBlock::MAX_SIZE);
cb.usage = 0;
blocks.push_back(cb);
}
CommandBlock *c = &blocks.write[current_block];
size_t space_left = CommandBlock::MAX_SIZE - c->usage;
if (space_left < sizeof(T)) {
current_block++;
continue;
}
//allocate block and add to the linked list
void *memory = c->memory + c->usage;
command = memnew_placement(memory, T);
command->next = nullptr;
last_command->next = command;
last_command = command;
c->usage += sizeof(T);
break;
}
}
rect_dirty = true;
return command;
}
void clear() {
// The first one is always allocated on heap
// the rest go in the blocks
Command *c = commands;
while (c) {
Command *n = c->next;
if (c == commands) {
memdelete(commands);
commands = nullptr;
} else {
c->~Command();
}
c = n;
}
{
uint32_t cbc = MIN((current_block + 1), (uint32_t)blocks.size());
CommandBlock *blockptr = blocks.ptrw();
for (uint32_t i = 0; i < cbc; i++) {
blockptr[i].usage = 0;
}
}
last_command = nullptr;
commands = nullptr;
current_block = 0;
clip = false;
rect_dirty = true;
final_clip_owner = nullptr;
material_owner = nullptr;
light_masked = false;
}
RS::CanvasItemTextureFilter texture_filter;
RS::CanvasItemTextureRepeat texture_repeat;
Item() {
commands = nullptr;
last_command = nullptr;
current_block = 0;
light_mask = 1;
vp_render = nullptr;
next = nullptr;
final_clip_owner = nullptr;
canvas_group_owner = nullptr;
clip = false;
final_modulate = Color(1, 1, 1, 1);
visible = true;
rect_dirty = true;
custom_rect = false;
behind = false;
material_owner = nullptr;
copy_back_buffer = nullptr;
distance_field = false;
light_masked = false;
update_when_visible = false;
z_final = 0;
texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
}
virtual ~Item() {
clear();
for (int i = 0; i < blocks.size(); i++) {
memfree(blocks[i].memory);
}
if (copy_back_buffer) {
memdelete(copy_back_buffer);
}
}
};
virtual 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) = 0;
virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) = 0;
struct LightOccluderInstance {
bool enabled;
RID canvas;
RID polygon;
RID occluder;
Rect2 aabb_cache;
Transform2D xform;
Transform2D xform_cache;
int light_mask;
bool sdf_collision;
RS::CanvasOccluderPolygonCullMode cull_cache;
LightOccluderInstance *next = nullptr;
LightOccluderInstance() {
enabled = true;
sdf_collision = false;
next = nullptr;
light_mask = 1;
cull_cache = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
}
};
virtual RID light_create() = 0;
virtual void light_set_texture(RID p_rid, RID p_texture) = 0;
virtual void light_set_use_shadow(RID p_rid, bool p_enable) = 0;
virtual 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) = 0;
virtual 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) = 0;
virtual void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) = 0;
virtual RID occluder_polygon_create() = 0;
virtual void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) = 0;
virtual void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) = 0;
virtual void set_shadow_texture_size(int p_size) = 0;
virtual bool free(RID p_rid) = 0;
virtual void update() = 0;
RendererCanvasRender() { singleton = this; }
virtual ~RendererCanvasRender() {}
};
#endif // RENDERINGSERVERCANVASRENDER_H
|