Blender V5.3
lineart_shadow.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MOD_lineart.hh"
10
11#include "lineart_intern.hh"
12
13#include "BKE_global.hh"
14#include "BKE_grease_pencil.hh"
16#include "BKE_object.hh"
17
18#include "BLI_listbase.hh"
19#include "BLI_math_matrix_c.hh"
21#include "BLI_task_c.hh"
22#include "BLI_time.hh"
23
24#include "DNA_light_types.h"
25#include "DNA_modifier_types.h"
26
27#include "MEM_guardedalloc.h"
28
29namespace blender {
30
31/* Shadow loading etc. ================== */
32
33LineartElementLinkNode *lineart_find_matching_eln(ListBaseT<LineartElementLinkNode> *shadow_elns,
34 int obindex)
35{
36 for (LineartElementLinkNode &eln : *shadow_elns) {
37 if (eln.obindex == obindex) {
38 return &eln;
39 }
40 }
41 return nullptr;
42}
43
45 uint64_t edge_identifier)
46{
47 LineartEdge *elist = static_cast<LineartEdge *>(shadow_eln->pointer);
48 for (int i = 0; i < shadow_eln->element_count; i++) {
49 if (elist[i].edge_identifier == edge_identifier) {
50 return &elist[i];
51 }
52 }
53 return nullptr;
54}
55
57{
58
60 return false;
61 }
62 double view_vector[3];
63 double light_vector[3];
64 bool side_1_facing_light = false;
65 bool side_2_facing_light = false;
66 bool side_1_facing_camera = false;
68 sub_v3_v3v3_db(light_vector, ld->conf.camera_pos_secondary, e->v1->gloc);
69 }
70 else {
71 copy_v3_v3_db(light_vector, ld->conf.view_vector_secondary);
72 }
73 double dot_light_1 = dot_v3v3_db(light_vector, e->t1->gn);
74 side_1_facing_light = (dot_light_1 > 0);
75 if (e->t2) {
76 double dot_light_2 = dot_v3v3_db(light_vector, e->t2->gn);
77 side_2_facing_light = (dot_light_2 > 0);
78 }
79 else {
80 side_2_facing_light = !side_1_facing_light;
81 }
82
83 if (ld->conf.cam_is_persp) {
84 sub_v3_v3v3_db(view_vector, ld->conf.camera_pos, e->v1->gloc);
85 }
86 else {
87 copy_v3_v3_db(view_vector, ld->conf.view_vector);
88 }
89 double dot_view_1 = dot_v3v3_db(view_vector, e->t1->gn);
90 side_1_facing_camera = (dot_view_1 > 0);
91
92 if ((side_1_facing_camera && (!side_1_facing_light) && side_2_facing_light) ||
93 ((!side_1_facing_camera) && side_1_facing_light && (!side_2_facing_light)))
94 {
95 return true;
96 }
97 return false;
98}
99
101{
102 for (LineartEdgeSegment &es : shadow_edge->segments) {
103 /* Convert to view space cutting points. */
104 double la1 = es.ratio;
105 double la2 = es.next ? es.next->ratio : 1.0f;
106 la1 = la1 * e->v2->fbcoord[3] /
107 (e->v1->fbcoord[3] - la1 * (e->v1->fbcoord[3] - e->v2->fbcoord[3]));
108 la2 = la2 * e->v2->fbcoord[3] /
109 (e->v1->fbcoord[3] - la2 * (e->v1->fbcoord[3] - e->v2->fbcoord[3]));
111
113 {
114 shadow_bits = LRT_SHADOW_MASK_SHADED;
115 }
116
117 lineart_edge_cut(ld, e, la1, la2, 0, 0, shadow_bits);
118 }
119}
120
122 ListBaseT<LineartElementLinkNode> *shadow_elns)
123{
124 if (!shadow_elns) {
125 return;
126 }
127
128 LineartElementLinkNode *eln_isect_shadow = nullptr;
129 LineartElementLinkNode *eln_isect_original = nullptr;
130
131 for (LineartElementLinkNode &eln : *shadow_elns) {
132 if (eln.flags & LRT_ELEMENT_INTERSECTION_DATA) {
133 eln_isect_shadow = &eln;
134 break;
135 }
136 }
139 eln_isect_original = &eln;
140 break;
141 }
142 }
143 if (!eln_isect_shadow || !eln_isect_original) {
144 return;
145 }
146
147 /* Keeping it single threaded for now because a simple parallel_for could end up getting the same
148 * #shadow_e in different threads. */
149 for (int i = 0; i < eln_isect_original->element_count; i++) {
150 LineartEdge *e = &(static_cast<LineartEdge *>(eln_isect_original->pointer))[i];
151 LineartEdge *shadow_e = lineart_find_matching_edge(eln_isect_shadow, e->edge_identifier);
152 if (shadow_e) {
153 lineart_register_shadow_cuts(ld, e, shadow_e);
154 }
155 }
156}
157
158/* Shadow computation part ================== */
159
161{
163
164 /* See if there is any already allocated memory we can reuse. */
165 if (ld->wasted_shadow_cuts.first()) {
166 LineartShadowSegment *es = static_cast<LineartShadowSegment *>(
169 memset(es, 0, sizeof(LineartShadowSegment));
170 return es;
171 }
173
174 /* Otherwise allocate some new memory. */
175 return static_cast<LineartShadowSegment *>(
177}
178
179static void lineart_shadow_segment_slice_get(double *fb_co_1,
180 double *fb_co_2,
181 double *gloc_1,
182 double *gloc_2,
183 double ratio,
184 double at_1,
185 double at_2,
186 double *r_fb_co,
187 double *r_gloc)
188{
189 double real_at = ((at_2 - at_1) == 0) ? 0 : ((ratio - at_1) / (at_2 - at_1));
190 double ga_div = (fb_co_2[3] * (1.0f - real_at) + fb_co_1[3] * real_at);
191 double ga;
192 if (ga_div == 0) {
193 ga = 0;
194 }
195 else {
196 ga = fb_co_1[3] * real_at / ga_div;
197 }
198 interp_v3_v3v3_db(r_fb_co, fb_co_1, fb_co_2, real_at);
199 r_fb_co[3] = interpd(fb_co_2[3], fb_co_1[3], ga);
200 interp_v3_v3v3_db(r_gloc, gloc_1, gloc_2, ga);
201}
202
226static bool lineart_do_closest_segment(bool is_persp,
227 double *s1_fb_co_1,
228 double *s1_fb_co_2,
229 double *s2_fb_co_1,
230 double *s2_fb_co_2,
231 double *s1_gloc_1,
232 double *s1_gloc_2,
233 double *s2_gloc_1,
234 double *s2_gloc_2,
235 double *r_fb_co_1,
236 double *r_fb_co_2,
237 double *r_gloc_1,
238 double *r_gloc_2,
239 double *r_new_in_the_middle,
240 double *r_new_in_the_middle_global,
241 double *r_new_at,
242 bool *is_side_2r,
243 bool *use_new_ref)
244{
245 int side = 0;
246 int z_index = is_persp ? 3 : 2;
247
248 /* No need to do anything if the segment has no length. */
249 if (s2_fb_co_1[z_index] == s2_fb_co_2[z_index]) {
250 return false;
251 }
252
253 /* Always use the closest point to the light camera. */
254 if (s1_fb_co_1[z_index] >= s2_fb_co_1[z_index]) {
255 copy_v4_v4_db(r_fb_co_1, s2_fb_co_1);
256 copy_v3_v3_db(r_gloc_1, s2_gloc_1);
257 side++;
258 }
259 if (s1_fb_co_2[z_index] >= s2_fb_co_2[z_index]) {
260 copy_v4_v4_db(r_fb_co_2, s2_fb_co_2);
261 copy_v3_v3_db(r_gloc_2, s2_gloc_2);
262 *is_side_2r = true;
263 side++;
264 }
265 if (s1_fb_co_1[z_index] <= s2_fb_co_1[z_index]) {
266 copy_v4_v4_db(r_fb_co_1, s1_fb_co_1);
267 copy_v3_v3_db(r_gloc_1, s1_gloc_1);
268 side--;
269 }
270 if (s1_fb_co_2[z_index] <= s2_fb_co_2[z_index]) {
271 copy_v4_v4_db(r_fb_co_2, s1_fb_co_2);
272 copy_v3_v3_db(r_gloc_2, s1_gloc_2);
273 *is_side_2r = false;
274 side--;
275 }
276
277 /* No need to cut in the middle, because one segment completely overlaps the other. */
278 if (side) {
279 if (side > 0) {
280 *is_side_2r = true;
281 *use_new_ref = true;
282 }
283 else if (side < 0) {
284 *is_side_2r = false;
285 *use_new_ref = false;
286 }
287 return false;
288 }
289
290 /* Else there must be an intersection point in the middle. Use "w" value to linearly plot the
291 * position and get image space "ratio" position. */
292 double dl = s1_fb_co_1[z_index] - s2_fb_co_1[z_index];
293 double dr = s1_fb_co_2[z_index] - s2_fb_co_2[z_index];
294 double ga = ratiod(dl, dr, 0);
295 double ga_div = s2_fb_co_1[3] * (1.0f - ga) + s2_fb_co_2[3] * ga;
296 if (ga_div == 0) {
297 *r_new_at = 0;
298 return false;
299 }
300
301 *r_new_at = is_persp ? s2_fb_co_2[3] * ga / ga_div : ga;
302
303 interp_v3_v3v3_db(r_new_in_the_middle, s2_fb_co_1, s2_fb_co_2, *r_new_at);
304 r_new_in_the_middle[3] = interpd(s2_fb_co_2[3], s2_fb_co_1[3], ga);
305 interp_v3_v3v3_db(r_new_in_the_middle_global, s1_gloc_1, s1_gloc_2, ga);
306 *use_new_ref = true;
307
308 return true;
309}
310
311/* For each visible [segment] of the edge, create 1 shadow edge. Note if the original edge has
312 * multiple visible cuts, multiple shadow edges should be generated. */
314 bool transform_edge_cuts,
315 bool do_light_contour)
316{
317/* If the segment is short enough, we ignore them because it's not prominently visible anyway. */
318#define DISCARD_NONSENSE_SEGMENTS \
319 if (es.occlusion != 0 || \
320 (es.next && LRT_DOUBLE_CLOSE_ENOUGH(es.ratio, ((LineartEdgeSegment *)es.next)->ratio))) \
321 { \
322 LRT_ITER_ALL_LINES_NEXT; \
323 continue; \
324 }
325
326 /* Count and allocate at once to save time. */
327 int segment_count = 0;
329 if (do_light_contour) {
331 }
333 {
334 /* Only contour and loose edges can actually cast shadows. We allow light contour here because
335 * we want to see if it also doubles as a view contour, in that case we also need to project
336 * them. */
337 if (!(e->flags & accept_types)) {
338 continue;
339 }
341 /* Check if the light contour also doubles as a view contour. */
342 LineartEdge *orig_e = reinterpret_cast<LineartEdge *>(e->t1);
343 if (!orig_e->t2) {
345 }
346 else {
347 double vv[3];
348 double *view_vector = vv;
349 double dot_1 = 0, dot_2 = 0;
350 double result;
351 if (ld->conf.cam_is_persp) {
352 sub_v3_v3v3_db(view_vector, orig_e->v1->gloc, ld->conf.camera_pos);
353 }
354 else {
355 view_vector = ld->conf.view_vector;
356 }
357
358 dot_1 = dot_v3v3_db(view_vector, orig_e->t1->gn);
359 dot_2 = dot_v3v3_db(view_vector, orig_e->t2->gn);
360
361 if ((result = dot_1 * dot_2) <= 0 && (dot_1 + dot_2)) {
362 /* If this edge is both a light contour and a view contour, mark it for the convenience
363 * of generating it in the next iteration. */
365 }
366 }
367 if (!(e->flags & MOD_LINEART_EDGE_FLAG_CONTOUR)) {
368 continue;
369 }
370 }
371 for (LineartEdgeSegment &es : e->segments) {
373 segment_count++;
374 }
375 }
377
378 LineartShadowEdge *sedge = static_cast<LineartShadowEdge *>(
379 lineart_mem_acquire(&ld->render_data_pool, sizeof(LineartShadowEdge) * segment_count));
381 &ld->render_data_pool, sizeof(LineartShadowSegment) * segment_count * 2));
382
383 ld->shadow_edges = sedge;
384 ld->shadow_edges_count = segment_count;
385
386 int i = 0;
388 {
390 continue;
391 }
392 for (LineartEdgeSegment &es : e->segments) {
394
395 double next_at = es.next ? (es.next)->ratio : 1.0f;
396 /* Get correct XYZ and W coordinates. */
397 interp_v3_v3v3_db(sedge[i].fbc1, e->v1->fbcoord, e->v2->fbcoord, es.ratio);
398 interp_v3_v3v3_db(sedge[i].fbc2, e->v1->fbcoord, e->v2->fbcoord, next_at);
399
400 /* Global coord for light-shadow separation line (occlusion-corrected light contour). */
401 double ga1 = e->v1->fbcoord[3] * es.ratio /
402 (es.ratio * e->v1->fbcoord[3] + (1 - es.ratio) * e->v2->fbcoord[3]);
403 double ga2 = e->v1->fbcoord[3] * next_at /
404 (next_at * e->v1->fbcoord[3] + (1 - next_at) * e->v2->fbcoord[3]);
405 interp_v3_v3v3_db(sedge[i].g1, e->v1->gloc, e->v2->gloc, ga1);
406 interp_v3_v3v3_db(sedge[i].g2, e->v1->gloc, e->v2->gloc, ga2);
407
408 /* Assign an absurdly big W for initial distance so when triangles show up to catch the
409 * shadow, their w must certainly be smaller than this value so the shadow catches
410 * successfully. */
411 sedge[i].fbc1[3] = 1e30;
412 sedge[i].fbc2[3] = 1e30;
413 sedge[i].fbc1[2] = 1e30;
414 sedge[i].fbc2[2] = 1e30;
415
416 /* Assign to the first segment's right and the last segment's left position */
417 copy_v4_v4_db(sseg[i * 2].fbc2, sedge[i].fbc1);
418 copy_v4_v4_db(sseg[i * 2 + 1].fbc1, sedge[i].fbc2);
419 sseg[i * 2].ratio = 0.0f;
420 sseg[i * 2 + 1].ratio = 1.0f;
421 BLI_addtail(&sedge[i].shadow_segments, &sseg[i * 2]);
422 BLI_addtail(&sedge[i].shadow_segments, &sseg[i * 2 + 1]);
423
425 sedge[i].e_ref = reinterpret_cast<LineartEdge *>(e->t1);
426 sedge[i].e_ref_light_contour = e;
427 /* Restore original edge flag for edges "who is both view and light contour" so we still
428 * have correct edge flags. */
430 }
431 else {
432 sedge[i].e_ref = e;
433 }
434
435 sedge[i].es_ref = &es;
436
437 i++;
438 }
439 }
441
442 /* Transform the cutting position to global space for regular feature lines. This is for
443 * convenience of reusing the shadow cast function for both shadow line generation and silhouette
444 * registration, which the latter one needs view-space coordinates, while cast shadow needs
445 * global-space coordinates. */
446 if (transform_edge_cuts) {
448 {
449 for (LineartEdgeSegment &es : e->segments) {
450 es.ratio = e->v1->fbcoord[3] * es.ratio /
451 (es.ratio * e->v1->fbcoord[3] + (1 - es.ratio) * e->v2->fbcoord[3]);
452 }
453 }
455 }
456
457 if (G.debug_value == 4000) {
458 printf("Shadow: Added %d raw shadow_edges\n", segment_count);
459 }
460}
461
462/* This function does the actual cutting on a given "shadow edge".
463 * #start / #end determines the view(from light camera) space cutting ratio.
464 * #start/end_gloc/fbc are the respective start/end coordinates.
465 * #facing_light is set from the caller which determines if this edge landed on a triangle's light
466 * facing side or not.
467 *
468 * Visually this function does this: (Top is the far side of the camera)
469 * _-end
470 * _-`
471 * l[-------------_-`--------------]r [e] 1) Calls for cut on top of #e.
472 * _-`
473 * _-`
474 * start-`
475 *
476 * _-end
477 * _-`
478 * l[-----][------_-`----][--------]r [e] 2) Add cutting points on #e at #start/#end.
479 * _-`
480 * _-`
481 * start-`
482 *
483 * _-end
484 * _-`
485 * [------_-`----] 3) Call lineart_shadow_segment_slice_get() to
486 * _-` get coordinates of a visually aligned segment on
487 * _-` #e with the incoming segment.
488 * start-`
489 *
490 * _c-----] 4) Call lineart_do_closest_segment() to find out the
491 * _-` actual geometry after cut, add a new cut if needed.
492 * _-`
493 * [`
494 *
495 * l[-----] _][----][--------]r [e] 5) Write coordinates on cuts.
496 * _-`
497 * _-`
498 * [`
499 *
500 * This process is repeated on each existing segments of the shadow edge (#e), which ensures they
501 * all have been tested for closest segments after cutting. And in the diagram it's clear that the
502 * left/right side of cuts are likely to be discontinuous, each cut's left side designates the
503 * right side of the last segment, and vice-versa. */
506 double start,
507 double end,
508 double *start_gloc,
509 double *end_gloc,
510 double *start_fb_co,
511 double *end_fb_co,
512 bool facing_light,
513 uint32_t target_reference)
514{
515 LineartShadowSegment *seg, *i_seg;
516 LineartShadowSegment *cut_start_after = static_cast<LineartShadowSegment *>(
517 e->shadow_segments.first()),
518 *cut_end_before = e->shadow_segments.last();
519 LineartShadowSegment *new_seg_1 = nullptr, *new_seg_2 = nullptr, *seg_1 = nullptr,
520 *seg_2 = nullptr;
521 int untouched = 0;
522
523 /* If for some reason the occlusion function may give a result that has zero length, or
524 * reversed in direction, or NAN, we take care of them here. */
525 if (LRT_DOUBLE_CLOSE_ENOUGH(start, end)) {
526 return;
527 }
528 if (LRT_DOUBLE_CLOSE_ENOUGH(start, 1) || LRT_DOUBLE_CLOSE_ENOUGH(end, 0)) {
529 return;
530 }
531 if (start != start) [[unlikely]] {
532 start = 0;
533 }
534 if (end != end) [[unlikely]] {
535 end = 0;
536 }
537
538 if (start > end) {
539 double t = start;
540 start = end;
541 end = t;
542 }
543
544 /* Begin looking for starting position of the segment. */
545 /* Not using a list iteration macro because of it more clear when using for loops to iterate
546 * through the segments. */
547 for (LineartShadowSegment &seg : e->shadow_segments) {
548 if (LRT_DOUBLE_CLOSE_ENOUGH(seg.ratio, start)) {
549 cut_start_after = &seg;
550 new_seg_1 = cut_start_after;
551 break;
552 }
553 if (seg.next == nullptr) {
554 break;
555 }
556 i_seg = seg.next;
557 if (i_seg->ratio > start + 1e-09 && start > seg.ratio) {
558 cut_start_after = &seg;
559 new_seg_1 = lineart_give_shadow_segment(ld);
560 break;
561 }
562 }
563 if (!cut_start_after && LRT_DOUBLE_CLOSE_ENOUGH(1, end)) {
564 untouched = 1;
565 }
566 for (seg = cut_start_after->next; seg; seg = seg->next) {
567 /* We tried to cut at existing cutting point (e.g. where the line's occluded by a triangle
568 * strip). */
569 if (LRT_DOUBLE_CLOSE_ENOUGH(seg->ratio, end)) {
570 cut_end_before = seg;
571 new_seg_2 = cut_end_before;
572 break;
573 }
574 /* This check is to prevent `es->ratio == 1.0` (where we don't need to cut because we are ratio
575 * the end point). */
576 if (!seg->next && LRT_DOUBLE_CLOSE_ENOUGH(1, end)) {
577 cut_end_before = seg;
578 new_seg_2 = cut_end_before;
579 untouched = 1;
580 break;
581 }
582 /* When an actual cut is needed in the line. */
583 if (seg->ratio > end) {
584 cut_end_before = seg;
585 new_seg_2 = lineart_give_shadow_segment(ld);
586 break;
587 }
588 }
589
590 /* When we still can't find any existing cut in the line, we allocate new ones. */
591 if (new_seg_1 == nullptr) {
592 new_seg_1 = lineart_give_shadow_segment(ld);
593 }
594 if (new_seg_2 == nullptr) {
595 if (untouched) {
596 new_seg_2 = new_seg_1;
597 cut_end_before = new_seg_2;
598 }
599 else {
600 new_seg_2 = lineart_give_shadow_segment(ld);
601 }
602 }
603
604 /* If we touched the cut list, we assign the new cut position based on new cut position,
605 * this way we accommodate precision lost due to multiple cut inserts. */
606 new_seg_1->ratio = start;
607 if (!untouched) {
608 new_seg_2->ratio = end;
609 }
610
611 double r_fb_co_1[4] = {0}, r_fb_co_2[4] = {0}, r_gloc_1[3] = {0}, r_gloc_2[3] = {0};
612 double r_new_in_the_middle[4], r_new_in_the_middle_global[3], r_new_at;
613 double *s1_fb_co_1, *s1_fb_co_2, *s1_gloc_1, *s1_gloc_2;
614
615 /* Temporary coordinate records and "middle" records. */
616 double t_g1[3], t_g2[3], t_fbc1[4], t_fbc2[4], m_g1[3], m_fbc1[4], m_g2[3], m_fbc2[4];
617 bool is_side_2r, has_middle = false, use_new_ref;
618 copy_v4_v4_db(t_fbc1, start_fb_co);
619 copy_v3_v3_db(t_g1, start_gloc);
620
621 /* Do max stuff before insert. */
623 for (seg = cut_start_after; seg != cut_end_before; seg = nes) {
624 nes = seg->next;
625
626 s1_fb_co_1 = seg->fbc2;
627 s1_fb_co_2 = nes->fbc1;
628
629 s1_gloc_1 = seg->g2;
630 s1_gloc_2 = nes->g1;
631
632 seg_1 = seg;
633 seg_2 = nes;
634
635 if (seg == cut_start_after) {
637 nes->fbc1,
638 seg->g2,
639 nes->g1,
640 new_seg_1->ratio,
641 seg->ratio,
642 nes->ratio,
643 m_fbc1,
644 m_g1);
645 s1_fb_co_1 = m_fbc1;
646 s1_gloc_1 = m_g1;
647
648 seg_1 = new_seg_1;
649 if (cut_start_after != new_seg_1) {
650 BLI_insertlinkafter(&e->shadow_segments, cut_start_after, new_seg_1);
651 copy_v4_v4_db(new_seg_1->fbc1, m_fbc1);
652 copy_v3_v3_db(new_seg_1->g1, m_g1);
653 }
654 }
655 if (nes == cut_end_before) {
657 nes->fbc1,
658 seg->g2,
659 nes->g1,
660 new_seg_2->ratio,
661 seg->ratio,
662 nes->ratio,
663 m_fbc2,
664 m_g2);
665 s1_fb_co_2 = m_fbc2;
666 s1_gloc_2 = m_g2;
667
668 seg_2 = new_seg_2;
669 if (cut_end_before != new_seg_2) {
670 BLI_insertlinkbefore(&e->shadow_segments, cut_end_before, new_seg_2);
671 copy_v4_v4_db(new_seg_2->fbc2, m_fbc2);
672 copy_v3_v3_db(new_seg_2->g2, m_g2);
673 /* Need to restore the flag for next segment's reference. */
674 seg_2->flag = seg->flag;
675 seg_2->target_reference = seg->target_reference;
676 }
677 }
678
680 start_fb_co, end_fb_co, start_gloc, end_gloc, seg_2->ratio, start, end, t_fbc2, t_g2);
681
682 if ((has_middle = lineart_do_closest_segment(ld->conf.cam_is_persp,
683 s1_fb_co_1,
684 s1_fb_co_2,
685 t_fbc1,
686 t_fbc2,
687 s1_gloc_1,
688 s1_gloc_2,
689 t_g1,
690 t_g2,
691 r_fb_co_1,
692 r_fb_co_2,
693 r_gloc_1,
694 r_gloc_2,
695 r_new_in_the_middle,
696 r_new_in_the_middle_global,
697 &r_new_at,
698 &is_side_2r,
699 &use_new_ref)))
700 {
702 ss_middle->ratio = interpf(seg_2->ratio, seg_1->ratio, r_new_at);
703 ss_middle->flag = LRT_SHADOW_CASTED |
704 (use_new_ref ? (facing_light ? LRT_SHADOW_FACING_LIGHT : 0) : seg_1->flag);
705 ss_middle->target_reference = (use_new_ref ? (target_reference) : seg_1->target_reference);
706 copy_v3_v3_db(ss_middle->g1, r_new_in_the_middle_global);
707 copy_v3_v3_db(ss_middle->g2, r_new_in_the_middle_global);
708 copy_v4_v4_db(ss_middle->fbc1, r_new_in_the_middle);
709 copy_v4_v4_db(ss_middle->fbc2, r_new_in_the_middle);
710 BLI_insertlinkafter(&e->shadow_segments, seg_1, ss_middle);
711 }
712 /* Always assign the "closest" value to the segment. */
713 copy_v4_v4_db(seg_1->fbc2, r_fb_co_1);
714 copy_v3_v3_db(seg_1->g2, r_gloc_1);
715 copy_v4_v4_db(seg_2->fbc1, r_fb_co_2);
716 copy_v3_v3_db(seg_2->g1, r_gloc_2);
717
718 if (has_middle) {
719 seg_1->flag = LRT_SHADOW_CASTED |
720 (is_side_2r ? seg->flag : (facing_light ? LRT_SHADOW_FACING_LIGHT : 0));
721 seg_1->target_reference = is_side_2r ? seg->target_reference : target_reference;
722 }
723 else {
724 seg_1->flag = LRT_SHADOW_CASTED |
725 (use_new_ref ? (facing_light ? LRT_SHADOW_FACING_LIGHT : 0) : seg->flag);
726 seg_1->target_reference = use_new_ref ? target_reference : seg->target_reference;
727 }
728
729 copy_v4_v4_db(t_fbc1, t_fbc2);
730 copy_v3_v3_db(t_g1, t_g2);
731 }
732}
733
735 LineartTriangle *tri,
736 LineartShadowEdge *sedge,
737 double *r_at_1,
738 double *r_at_2,
739 double *r_fb_co_1,
740 double *r_fb_co_2,
741 double *r_gloc_1,
742 double *r_gloc_2,
743 bool *r_facing_light)
744{
745 double *LFBC = sedge->fbc1, *RFBC = sedge->fbc2, *FBC0 = tri->v[0]->fbcoord,
746 *FBC1 = tri->v[1]->fbcoord, *FBC2 = tri->v[2]->fbcoord;
747
748 /* Bound box check. Because we have already done occlusion in the shadow camera, so any visual
749 * intersection found in this function must mean that the triangle is behind the given line so it
750 * will always project a shadow, hence no need to do depth bound-box check. */
751 if ((std::max({FBC0[0], FBC1[0], FBC2[0]}) < std::min(LFBC[0], RFBC[0])) ||
752 (std::min({FBC0[0], FBC1[0], FBC2[0]}) > std::max(LFBC[0], RFBC[0])) ||
753 (std::max({FBC0[1], FBC1[1], FBC2[1]}) < std::min(LFBC[1], RFBC[1])) ||
754 (std::min({FBC0[1], FBC1[1], FBC2[1]}) > std::max(LFBC[1], RFBC[1])))
755 {
756 return false;
757 }
758
759 bool is_persp = ld->conf.cam_is_persp;
760 double ratio[2];
761 int trie[2];
762 int pi = 0;
763 if (lineart_line_isec_2d_ignore_line2pos(FBC0, FBC1, LFBC, RFBC, &ratio[pi])) {
764 trie[pi] = 0;
765 pi++;
766 }
767 if (lineart_line_isec_2d_ignore_line2pos(FBC1, FBC2, LFBC, RFBC, &ratio[pi])) {
768 /* ratio[0] == 1 && ratio[1] == 0 means we found a intersection at the same point of the
769 * edge (FBC1), ignore this one and try get the intersection point from the other side of
770 * the edge
771 */
772 if (!(pi && LRT_DOUBLE_CLOSE_ENOUGH(ratio[0], 1.0f) &&
773 LRT_DOUBLE_CLOSE_ENOUGH(ratio[1], 0.0f)))
774 {
775 trie[pi] = 1;
776 pi++;
777 }
778 }
779 if (!pi) {
780 return false;
781 }
782 if (pi == 1 && lineart_line_isec_2d_ignore_line2pos(FBC2, FBC0, LFBC, RFBC, &ratio[pi])) {
783
784 if ((trie[0] == 0 && LRT_DOUBLE_CLOSE_ENOUGH(ratio[0], 0.0f) &&
785 LRT_DOUBLE_CLOSE_ENOUGH(ratio[1], 1.0f)) ||
786 (trie[0] == 1 && LRT_DOUBLE_CLOSE_ENOUGH(ratio[0], 1.0f) &&
787 LRT_DOUBLE_CLOSE_ENOUGH(ratio[1], 0.0f)))
788 {
789 return false;
790 }
791 trie[pi] = 2;
792 pi++;
793 }
794
795 if (pi != 2) {
796 return false;
797 }
798
799 /* Get projected global position. */
800
801 double3 gpos1, gpos2;
802 double *v1 = (trie[0] == 0 ? FBC0 : (trie[0] == 1 ? FBC1 : FBC2));
803 double *v2 = (trie[0] == 0 ? FBC1 : (trie[0] == 1 ? FBC2 : FBC0));
804 double *v3 = (trie[1] == 0 ? FBC0 : (trie[1] == 1 ? FBC1 : FBC2));
805 double *v4 = (trie[1] == 0 ? FBC1 : (trie[1] == 1 ? FBC2 : FBC0));
806 double *gv1 = (trie[0] == 0 ? tri->v[0]->gloc :
807 (trie[0] == 1 ? tri->v[1]->gloc : tri->v[2]->gloc));
808 double *gv2 = (trie[0] == 0 ? tri->v[1]->gloc :
809 (trie[0] == 1 ? tri->v[2]->gloc : tri->v[0]->gloc));
810 double *gv3 = (trie[1] == 0 ? tri->v[0]->gloc :
811 (trie[1] == 1 ? tri->v[1]->gloc : tri->v[2]->gloc));
812 double *gv4 = (trie[1] == 0 ? tri->v[1]->gloc :
813 (trie[1] == 1 ? tri->v[2]->gloc : tri->v[0]->gloc));
814 double gr1 = is_persp ? v1[3] * ratio[0] / (ratio[0] * v1[3] + (1 - ratio[0]) * v2[3]) :
815 ratio[0];
816 double gr2 = is_persp ? v3[3] * ratio[1] / (ratio[1] * v3[3] + (1 - ratio[1]) * v4[3]) :
817 ratio[1];
818 interp_v3_v3v3_db(gpos1, gv1, gv2, gr1);
819 interp_v3_v3v3_db(gpos2, gv3, gv4, gr2);
820
821 double4 fbc1, fbc2;
822
823 mul_v4_m4v3_db(fbc1, ld->conf.view_projection, gpos1);
824 mul_v4_m4v3_db(fbc2, ld->conf.view_projection, gpos2);
825 if (is_persp) {
826 mul_v3db_db(fbc1, 1.0f / fbc1[3]);
827 mul_v3db_db(fbc2, 1.0f / fbc2[3]);
828 }
829 fbc1[0] -= ld->conf.shift_x * 2;
830 fbc1[1] -= ld->conf.shift_y * 2;
831 fbc2[0] -= ld->conf.shift_x * 2;
832 fbc2[1] -= ld->conf.shift_y * 2;
833
834 int use = (fabs(LFBC[0] - RFBC[0]) > fabs(LFBC[1] - RFBC[1])) ? 0 : 1;
835 double at1 = ratiod(LFBC[use], RFBC[use], fbc1[use]);
836 double at2 = ratiod(LFBC[use], RFBC[use], fbc2[use]);
837 if (at1 > at2) {
838 std::swap(gpos1, gpos2);
839 std::swap(fbc1, fbc2);
840 std::swap(at1, at2);
841 }
842
843 /* If not effectively projecting anything. */
844 if (at1 > (1.0f - FLT_EPSILON) || at2 < FLT_EPSILON) {
845 return false;
846 }
847
848 /* Trim to edge's end points. */
849
850 double t_fbc1[4], t_fbc2[4], t_gpos1[3], t_gpos2[3];
851 bool trimmed1 = false, trimmed2 = false;
852 if (at1 < 0 || at2 > 1) {
853 double rat1 = (-at1) / (at2 - at1);
854 double rat2 = (1.0f - at1) / (at2 - at1);
855 double gat1 = is_persp ? fbc1[3] * rat1 / (rat1 * fbc1[3] + (1 - rat1) * fbc2[3]) : rat1;
856 double gat2 = is_persp ? fbc1[3] * rat2 / (rat2 * fbc1[3] + (1 - rat2) * fbc2[3]) : rat2;
857 if (at1 < 0) {
858 interp_v3_v3v3_db(t_gpos1, gpos1, gpos2, gat1);
859 interp_v3_v3v3_db(t_fbc1, fbc1, fbc2, rat1);
860 t_fbc1[3] = interpd(fbc2[3], fbc1[3], gat1);
861 at1 = 0;
862 trimmed1 = true;
863 }
864 if (at2 > 1) {
865 interp_v3_v3v3_db(t_gpos2, gpos1, gpos2, gat2);
866 interp_v3_v3v3_db(t_fbc2, fbc1, fbc2, rat2);
867 t_fbc2[3] = interpd(fbc2[3], fbc1[3], gat2);
868 at2 = 1;
869 trimmed2 = true;
870 }
871 }
872 if (trimmed1) {
873 copy_v4_v4_db(fbc1, t_fbc1);
874 copy_v3_v3_db(gpos1, t_gpos1);
875 }
876 if (trimmed2) {
877 copy_v4_v4_db(fbc2, t_fbc2);
878 copy_v3_v3_db(gpos2, t_gpos2);
879 }
880
881 *r_at_1 = at1;
882 *r_at_2 = at2;
883 copy_v4_v4_db(r_fb_co_1, fbc1);
884 copy_v4_v4_db(r_fb_co_2, fbc2);
885 copy_v3_v3_db(r_gloc_1, gpos1);
886 copy_v3_v3_db(r_gloc_2, gpos2);
887
888 double camera_vector[3];
889
890 if (is_persp) {
891 sub_v3_v3v3_db(camera_vector, ld->conf.camera_pos, tri->v[0]->gloc);
892 }
893 else {
894 copy_v3_v3_db(camera_vector, ld->conf.view_vector);
895 }
896
897 double dot_f = dot_v3v3_db(camera_vector, tri->gn);
898 *r_facing_light = (dot_f < 0);
899
900 return true;
901}
902
903/* The one step all to cast all visible edges in light camera back to other geometries behind them,
904 * the result of this step can then be generated as actual LineartEdge's for occlusion test in view
905 * camera. */
906static void lineart_shadow_cast(LineartData *ld, bool transform_edge_cuts, bool do_light_contour)
907{
908
909 lineart_shadow_create_shadow_edge_array(ld, transform_edge_cuts, do_light_contour);
910
911 /* Keep it single threaded for now because the loop will write "done" pointers to triangles. */
912 for (int edge_i = 0; edge_i < ld->shadow_edges_count; edge_i++) {
913 LineartShadowEdge *sedge = &ld->shadow_edges[edge_i];
914
916 double at_1, at_2;
917 double fb_co_1[4], fb_co_2[4];
918 double global_1[3], global_2[3];
919 bool facing_light;
920
921 LRT_EDGE_BA_MARCHING_BEGIN(sedge->fbc1, sedge->fbc2)
922 {
923 for (int i = 0; i < nba->triangle_count; i++) {
924 tri = reinterpret_cast<LineartTriangleThread *>(nba->linked_triangles[i]);
925 if (tri->testing_e[0] == reinterpret_cast<LineartEdge *>(sedge) ||
926 tri->base.mat_occlusion == 0 ||
927 lineart_edge_from_triangle(reinterpret_cast<LineartTriangle *>(tri),
928 sedge->e_ref,
930 {
931 continue;
932 }
933 tri->testing_e[0] = reinterpret_cast<LineartEdge *>(sedge);
934
936 reinterpret_cast<LineartTriangle *>(tri),
937 sedge,
938 &at_1,
939 &at_2,
940 fb_co_1,
941 fb_co_2,
942 global_1,
943 global_2,
944 &facing_light))
945 {
947 sedge,
948 at_1,
949 at_2,
950 global_1,
951 global_2,
952 fb_co_1,
953 fb_co_2,
954 facing_light,
956 }
957 }
958 LRT_EDGE_BA_MARCHING_NEXT(sedge->fbc1, sedge->fbc2);
959 }
961 }
962}
963
964/* For each [segment] on a shadow shadow_edge, 1 LineartEdge will be generated with a cast shadow
965 * edge flag (if that segment failed to cast onto anything then it's not generated). The original
966 * shadow shadow_edge is optionally generated as a light contour. */
968 bool do_original_edges,
969 LineartElementLinkNode **r_veln,
970 LineartElementLinkNode **r_eeln)
971{
972 int tot_edges = 0;
973 int tot_orig_edges = 0;
974 for (int i = 0; i < ld->shadow_edges_count; i++) {
975 LineartShadowEdge *sedge = &ld->shadow_edges[i];
976 for (LineartShadowSegment &sseg : sedge->shadow_segments) {
977 if (!(sseg.flag & LRT_SHADOW_CASTED)) {
978 continue;
979 }
980 if (!sseg.next) {
981 break;
982 }
983 tot_edges++;
984 }
985 tot_orig_edges++;
986 }
987
988 int edge_alloc = tot_edges + (do_original_edges ? tot_orig_edges : 0);
989
990 if (G.debug_value == 4000) {
991 printf("Line art shadow segments total: %d\n", tot_edges);
992 }
993
994 if (!edge_alloc) {
995 return false;
996 }
997 LineartElementLinkNode *veln = static_cast<LineartElementLinkNode *>(
999 LineartElementLinkNode *eeln = static_cast<LineartElementLinkNode *>(
1001 veln->pointer = lineart_mem_acquire(ld->shadow_data_pool, sizeof(LineartVert) * edge_alloc * 2);
1002 eeln->pointer = lineart_mem_acquire(ld->shadow_data_pool, sizeof(LineartEdge) * edge_alloc);
1003 LineartEdgeSegment *es = static_cast<LineartEdgeSegment *>(
1004 lineart_mem_acquire(ld->shadow_data_pool, sizeof(LineartEdgeSegment) * edge_alloc));
1005 *r_veln = veln;
1006 *r_eeln = eeln;
1007
1008 veln->element_count = edge_alloc * 2;
1009 eeln->element_count = edge_alloc;
1010
1011 LineartVert *vlist = static_cast<LineartVert *>(veln->pointer);
1012 LineartEdge *elist = static_cast<LineartEdge *>(eeln->pointer);
1013
1014 int ei = 0;
1015 for (int i = 0; i < ld->shadow_edges_count; i++) {
1016 LineartShadowEdge *sedge = &ld->shadow_edges[i];
1017 for (LineartShadowSegment &sseg : sedge->shadow_segments) {
1018 if (!(sseg.flag & LRT_SHADOW_CASTED)) {
1019 continue;
1020 }
1021 if (!sseg.next) {
1022 break;
1023 }
1024 LineartEdge *e = &elist[ei];
1025 BLI_addtail(&e->segments, &es[ei]);
1026 LineartVert *v1 = &vlist[ei * 2], *v2 = &vlist[ei * 2 + 1];
1027 copy_v3_v3_db(v1->gloc, sseg.g2);
1028 copy_v3_v3_db(v2->gloc, (sseg.next)->g1);
1029 e->v1 = v1;
1030 e->v2 = v2;
1031 e->t1 = reinterpret_cast<LineartTriangle *>(
1032 sedge->e_ref); /* See LineartEdge::t1 for usage. */
1033 e->t2 = reinterpret_cast<LineartTriangle *>(
1034 sedge->e_ref_light_contour ? sedge->e_ref_light_contour : sedge->e_ref);
1035 e->target_reference = sseg.target_reference;
1036 e->edge_identifier = sedge->e_ref->edge_identifier;
1038 ((sseg.flag & LRT_SHADOW_FACING_LIGHT) ?
1040 0));
1041 ei++;
1042 }
1043 if (do_original_edges) {
1044 /* Occlusion-corrected light contour. */
1045 LineartEdge *e = &elist[ei];
1046 BLI_addtail(&e->segments, &es[ei]);
1047 LineartVert *v1 = &vlist[ei * 2], *v2 = &vlist[ei * 2 + 1];
1048 v1->index = sedge->e_ref->v1->index;
1049 v2->index = sedge->e_ref->v2->index;
1050 copy_v3_v3_db(v1->gloc, sedge->g1);
1051 copy_v3_v3_db(v2->gloc, sedge->g2);
1052 uint64_t ref_1 = sedge->e_ref->t1 ? sedge->e_ref->t1->target_reference : 0;
1053 uint64_t ref_2 = sedge->e_ref->t2 ? sedge->e_ref->t2->target_reference : 0;
1054 e->edge_identifier = sedge->e_ref->edge_identifier;
1055 e->target_reference = ((ref_1 << 32) | ref_2);
1056 e->v1 = v1;
1057 e->v2 = v2;
1058 e->t1 = e->t2 = reinterpret_cast<LineartTriangle *>(sedge->e_ref);
1061 lineart_edge_cut(ld, e, 0.0f, 1.0f, 0, 0, LRT_SHADOW_MASK_SHADED);
1062 }
1063 ei++;
1064 }
1065 }
1066 return true;
1067}
1068
1070{
1071 /* Keeping it single threaded for now because a simple parallel_for could end up getting the same
1072 * #sedge->e_ref in different threads. */
1073 for (int i = 0; i < ld->shadow_edges_count; i++) {
1074 LineartShadowEdge *sedge = &ld->shadow_edges[i];
1075
1076 LineartEdge *e = sedge->e_ref;
1077 LineartEdgeSegment *es = sedge->es_ref;
1078 double es_start = es->ratio, es_end = es->next ? es->next->ratio : 1.0f;
1079 for (LineartShadowSegment &sseg : sedge->shadow_segments) {
1080 if (!(sseg.flag & LRT_SHADOW_CASTED)) {
1081 continue;
1082 }
1083 if (!sseg.next) {
1084 break;
1085 }
1086
1087 uint32_t silhouette_flags = (sseg.target_reference & LRT_OBINDEX_HIGHER) |
1089
1090 double at_start = interpd(es_end, es_start, sseg.ratio);
1091 double at_end = interpd(es_end, es_start, sseg.next->ratio);
1092 lineart_edge_cut(ld, e, at_start, at_end, 0, 0, silhouette_flags);
1093 }
1094 }
1095}
1096
1097/* To achieve enclosed shape effect, we need to:
1098 * 1) Show shaded segments against lit background.
1099 * 2) Erase lit segments against lit background. */
1101{
1102 LineartEdge *e;
1103 for (int i = 0; i < shadow_ld->pending_edges.next; i++) {
1104 e = shadow_ld->pending_edges.array[i];
1105
1106 /* Only care about shade-on-light and light-on-light situations, hence we only need
1107 * non-occluded segments in shadow buffer. */
1108 if (e->min_occ > 0) {
1109 continue;
1110 }
1111 for (LineartEdgeSegment &es : e->segments) {
1112 if (es.occlusion > 0) {
1113 continue;
1114 }
1115 double next_at = es.next ? (es.next)->ratio : 1.0f;
1116 LineartEdge *orig_e = reinterpret_cast<LineartEdge *>(e->t2);
1117
1118 /* Shadow view space to global. */
1119 double ga1 = e->v1->fbcoord[3] * es.ratio /
1120 (es.ratio * e->v1->fbcoord[3] + (1 - es.ratio) * e->v2->fbcoord[3]);
1121 double ga2 = e->v1->fbcoord[3] * next_at /
1122 (next_at * e->v1->fbcoord[3] + (1 - next_at) * e->v2->fbcoord[3]);
1123 double g1[3], g2[3], g1v[4], g2v[4];
1124 interp_v3_v3v3_db(g1, e->v1->gloc, e->v2->gloc, ga1);
1125 interp_v3_v3v3_db(g2, e->v1->gloc, e->v2->gloc, ga2);
1126 mul_v4_m4v3_db(g1v, ld->conf.view_projection, g1);
1127 mul_v4_m4v3_db(g2v, ld->conf.view_projection, g2);
1128
1129 if (ld->conf.cam_is_persp) {
1130 mul_v3db_db(g1v, (1 / g1v[3]));
1131 mul_v3db_db(g2v, (1 / g2v[3]));
1132 }
1133
1134 g1v[0] -= ld->conf.shift_x * 2;
1135 g1v[1] -= ld->conf.shift_y * 2;
1136 g2v[0] -= ld->conf.shift_x * 2;
1137 g2v[1] -= ld->conf.shift_y * 2;
1138
1139#define GET_RATIO(n) \
1140 (fabs(orig_e->v2->fbcoord[0] - orig_e->v1->fbcoord[0]) > \
1141 fabs(orig_e->v2->fbcoord[1] - orig_e->v1->fbcoord[1])) ? \
1142 ((g##n##v[0] - orig_e->v1->fbcoord[0]) / \
1143 (orig_e->v2->fbcoord[0] - orig_e->v1->fbcoord[0])) : \
1144 ((g##n##v[1] - orig_e->v1->fbcoord[1]) / (orig_e->v2->fbcoord[1] - orig_e->v1->fbcoord[1]))
1145 double la1, la2;
1146 la1 = GET_RATIO(1);
1147 la2 = GET_RATIO(2);
1148#undef GET_RATIO
1149
1150 lineart_edge_cut(ld, orig_e, la1, la2, 0, 0, LRT_SHADOW_MASK_ENCLOSED_SHAPE);
1151 }
1152 }
1153}
1154
1156 Depsgraph *depsgraph,
1157 Scene *scene,
1158 LineartData *original_ld,
1160 LineartStaticMemPool *shadow_data_pool,
1161 LineartElementLinkNode **r_veln,
1162 LineartElementLinkNode **r_eeln,
1163 ListBaseT<LineartElementLinkNode> *r_calculated_edges_eln_list,
1164 LineartData **r_shadow_ld_if_reproject)
1165{
1166 if ((!original_ld->conf.use_shadow && !original_ld->conf.use_light_contour &&
1167 !original_ld->conf.shadow_selection) ||
1168 (!lmd->light_contour_object))
1169 {
1170 return false;
1171 }
1172
1173 double t_start;
1174 if (G.debug_value == 4000) {
1175 t_start = BLI_time_now_seconds();
1176 }
1177
1178 bool is_persp = true;
1179
1180 if (lmd->light_contour_object->type == OB_LAMP) {
1181 Light *la = id_cast<Light *>(lmd->light_contour_object->data);
1182 if (la->type == LA_SUN) {
1183 is_persp = false;
1184 }
1185 }
1186
1187 LineartData *ld = MEM_new_uninitialized<LineartData>("LineArt render buffer copied");
1188 memcpy(ld, original_ld, sizeof(LineartData));
1189
1193
1194 ld->conf.do_shadow_cast = true;
1195 ld->shadow_data_pool = shadow_data_pool;
1196
1197 /* See LineartData::edge_data_pool for explanation. */
1198 if (ld->conf.shadow_selection) {
1199 ld->edge_data_pool = shadow_data_pool;
1200 }
1201 else {
1203 }
1204
1207
1208 copy_m4_m4(ld->conf.cam_obmat, lmd->light_contour_object->object_to_world().ptr());
1211 ld->conf.cam_is_persp = is_persp;
1212 ld->conf.near_clip = is_persp ? lmd->shadow_camera_near : -lmd->shadow_camera_far;
1213 ld->conf.far_clip = lmd->shadow_camera_far;
1214 ld->w = lmd->shadow_camera_size;
1215 ld->h = lmd->shadow_camera_size;
1216 /* Need to prevent wrong camera configuration so that shadow computation won't stall. */
1217 if (!ld->w || !ld->h) {
1218 ld->w = ld->h = 200;
1219 }
1220 if (!ld->conf.near_clip || !ld->conf.far_clip) {
1221 ld->conf.near_clip = 0.1f;
1222 ld->conf.far_clip = 200.0f;
1223 }
1225
1226 /* Contour and loose edge from light viewing direction will be cast as shadow, so only
1227 * force them on. If we need lit/shaded information for other line types, they are then
1228 * enabled as-is so that cutting positions can also be calculated through shadow projection.
1229 */
1230 if (!ld->conf.shadow_selection) {
1232 ld->conf.use_intersections = ld->conf.use_light_contour = false;
1233 }
1234 else {
1235 ld->conf.use_contour_secondary = true;
1236 ld->conf.allow_duplicated_types = true;
1237 }
1238 ld->conf.use_loose = true;
1239 ld->conf.use_contour = true;
1240
1241 ld->conf.max_occlusion_level = 0; /* No point getting see-through projections there. */
1242 ld->conf.use_back_face_culling = false;
1243
1244 /* Override matrices to light "camera". */
1245 double proj[4][4], view[4][4], result[4][4];
1246 float inv[4][4];
1247 if (is_persp) {
1249 }
1250 else {
1252 proj, -ld->w, ld->w, -ld->h, ld->h, ld->conf.near_clip, ld->conf.far_clip);
1253 }
1254 invert_m4_m4(inv, ld->conf.cam_obmat);
1255 mul_m4db_m4db_m4fl(result, proj, inv);
1256 copy_m4_m4_db(proj, result);
1258 unit_m4_db(view);
1259 copy_m4_m4_db(ld->conf.view, view);
1260
1262
1263 LineartModifierRuntime *runtime = reinterpret_cast<LineartModifierRuntime *>(lmd->runtime);
1264 Set<const Object *> *included_objects = runtime ? &runtime->object_dependencies : nullptr;
1265
1267 scene,
1268 nullptr,
1269 ld,
1270 int(lmd->flags) & int(MOD_LINEART_ALLOW_DUPLI_OBJECTS),
1271 true,
1272 nullptr,
1273 included_objects);
1274
1275 if (!ld->geom.vertex_buffer_pointers.first()) {
1276 /* No geometry loaded, return early. */
1278 MEM_delete(ld);
1279 return false;
1280 }
1281
1282 /* The exact same process as in MOD_lineart_compute_feature_lines() until occlusion finishes.
1283 */
1284
1286 lineart_main_cull_triangles(ld, false);
1295
1296 /* Do shadow cast stuff then get generated vert/edge data. */
1297 lineart_shadow_cast(ld, true, false);
1298 bool any_generated = lineart_shadow_cast_generate_edges(ld, true, r_veln, r_eeln);
1299
1300 if (ld->conf.shadow_selection) {
1301 memcpy(r_calculated_edges_eln_list, &ld->geom.line_buffer_pointers, sizeof(ListBase));
1302 }
1303
1304 if (ld->conf.shadow_enclose_shapes) {
1305 /* Need loaded data for re-projecting the 3rd time to get shape boundary against lit/shaded
1306 * region. */
1307 (*r_shadow_ld_if_reproject) = ld;
1308 }
1309 else {
1311 MEM_delete(ld);
1312 }
1313
1314 if (G.debug_value == 4000) {
1315 double t_elapsed = BLI_time_now_seconds() - t_start;
1316 printf("Line art shadow stage 1 time: %f\n", t_elapsed);
1317 }
1318
1319 return any_generated;
1320}
1321
1323 Depsgraph *depsgraph,
1324 Scene *scene,
1325 LineartData *original_ld,
1326 LineartGpencilModifierData *lmd_legacy,
1327 LineartStaticMemPool *shadow_data_pool,
1328 LineartElementLinkNode **r_veln,
1329 LineartElementLinkNode **r_eeln,
1330 ListBaseT<LineartElementLinkNode> *r_calculated_edges_eln_list,
1331 LineartData **r_shadow_ld_if_reproject)
1332{
1333 bool ret = false;
1337 scene,
1338 original_ld,
1339 &lmd,
1340 shadow_data_pool,
1341 r_veln,
1342 r_eeln,
1343 r_calculated_edges_eln_list,
1344 r_shadow_ld_if_reproject);
1346 return ret;
1347}
1348
1354
1355static void lineart_shadow_transform_task(void *__restrict userdata,
1356 const int element_index,
1357 const TaskParallelTLS *__restrict /*tls*/)
1358{
1359 LineartShadowFinalizeData *data = static_cast<LineartShadowFinalizeData *>(userdata);
1360 LineartData *ld = data->ld;
1361 LineartVert *v = &data->v[element_index];
1362 mul_v4_m4v3_db(v->fbcoord, ld->conf.view_projection, v->gloc);
1363 v->fbcoord[0] -= ld->conf.shift_x * 2;
1364 v->fbcoord[1] -= ld->conf.shift_y * 2;
1365}
1366
1367static void lineart_shadow_finalize_shadow_edges_task(void *__restrict userdata,
1368 const int i,
1369 const TaskParallelTLS *__restrict /*tls*/)
1370{
1371 LineartShadowFinalizeData *data = static_cast<LineartShadowFinalizeData *>(userdata);
1372 LineartData *ld = data->ld;
1373 LineartEdge *e = data->e;
1374
1375 if (e[i].flags & MOD_LINEART_EDGE_FLAG_LIGHT_CONTOUR) {
1377 &ld->geom.vertex_buffer_pointers, e[i].edge_identifier & LRT_OBINDEX_HIGHER);
1378 if (eln) {
1379 int v1i = (((e[i].edge_identifier) >> 32) & LRT_OBINDEX_LOWER);
1380 int v2i = (e[i].edge_identifier & LRT_OBINDEX_LOWER);
1381 LineartVert *v = static_cast<LineartVert *>(eln->pointer);
1382 /* If the global position is close enough, use the original vertex to prevent flickering
1383 * caused by very slim boundary condition in point_triangle_relation(). */
1384 if (LRT_CLOSE_LOOSER_v3(e[i].v1->gloc, v[v1i].gloc)) {
1385 e[i].v1 = &v[v1i];
1386 }
1387 if (LRT_CLOSE_LOOSER_v3(e[i].v2->gloc, v[v2i].gloc)) {
1388 e[i].v2 = &v[v2i];
1389 }
1390 }
1391 }
1392}
1393
1397{
1398
1399 TaskParallelSettings transform_settings;
1400 BLI_parallel_range_settings_defaults(&transform_settings);
1401 /* Set the minimum amount of edges a thread has to process. */
1402 transform_settings.min_iter_per_thread = 8192;
1403
1404 LineartShadowFinalizeData data = {nullptr};
1405 data.ld = ld;
1406 data.v = static_cast<LineartVert *>(veln->pointer);
1407 data.e = static_cast<LineartEdge *>(eeln->pointer);
1408
1410 0, veln->element_count, &data, lineart_shadow_transform_task, &transform_settings);
1412 eeln->element_count,
1413 &data,
1415 &transform_settings);
1416 for (int i = 0; i < eeln->element_count; i++) {
1418 }
1419
1422}
1423
1425{
1426 double t_start;
1427 if (G.debug_value == 4000) {
1428 t_start = BLI_time_now_seconds();
1429 }
1430
1431 if (shadow_ld || ld->conf.shadow_use_silhouette) {
1432 lineart_shadow_cast(ld, false, shadow_ld ? true : false);
1433 if (ld->conf.shadow_use_silhouette) {
1435 }
1436 }
1437
1438 if (G.debug_value == 4000) {
1439 double t_elapsed = BLI_time_now_seconds() - t_start;
1440 printf("Line art shadow stage 2 cast and silhouette time: %f\n", t_elapsed);
1441 }
1442
1443 if (!shadow_ld) {
1444 return;
1445 }
1446
1448
1449 if (shadow_ld->pending_edges.array) {
1450 MEM_delete(shadow_ld->pending_edges.array);
1451 shadow_ld->pending_edges.array = nullptr;
1452 shadow_ld->pending_edges.next = shadow_ld->pending_edges.max = 0;
1453 }
1454
1455 LineartElementLinkNode *shadow_veln, *shadow_eeln;
1456
1457 bool any_generated = lineart_shadow_cast_generate_edges(ld, false, &shadow_veln, &shadow_eeln);
1458
1459 if (!any_generated) {
1460 return;
1461 }
1462
1463 LineartVert *v = static_cast<LineartVert *>(shadow_veln->pointer);
1464 for (int i = 0; i < shadow_veln->element_count; i++) {
1465 mul_v4_m4v3_db(v[i].fbcoord, shadow_ld->conf.view_projection, v[i].gloc);
1466 if (shadow_ld->conf.cam_is_persp) {
1467 mul_v3db_db(v[i].fbcoord, (1 / v[i].fbcoord[3]));
1468 }
1469 v[i].fbcoord[0] -= shadow_ld->conf.shift_x * 2;
1470 v[i].fbcoord[1] -= shadow_ld->conf.shift_y * 2;
1471 }
1472
1474 shadow_eeln->element_count);
1475
1476 LineartEdge *se = static_cast<LineartEdge *>(shadow_eeln->pointer);
1477 for (int i = 0; i < shadow_eeln->element_count; i++) {
1478 lineart_add_edge_to_array(&shadow_ld->pending_edges, &se[i]);
1479 }
1480
1481 shadow_ld->scheduled_count = 0;
1482
1484 lineart_main_link_lines(shadow_ld);
1486
1488
1489 if (G.debug_value == 4000) {
1490 double t_elapsed = BLI_time_now_seconds() - t_start;
1491 printf("Line art shadow stage 2 total time: %f\n", t_elapsed);
1492 }
1493}
1494
1495} // namespace blender
Low-level operations for grease pencil.
General operations, lookup, etc. for blender objects.
#define DEG2RAD(_deg)
Platform independent time functions.
Read Blender memory allocation and freeing.
#define LRT_DOUBLE_CLOSE_ENOUGH(a, b)
#define LRT_OBINDEX_LOWER
#define LRT_SHADOW_MASK_ENCLOSED_SHAPE
#define LRT_OBINDEX_HIGHER
#define LRT_SHADOW_SILHOUETTE_ERASED_GROUP
#define LRT_SHADOW_MASK_ILLUMINATED
#define LRT_CLOSE_LOOSER_v3(a, b)
#define LRT_SHADOW_MASK_SHADED
#define FLT_EPSILON
#define LRT_EDGE_BA_MARCHING_BEGIN(fb1, fb2)
#define LRT_EDGE_BA_MARCHING_NEXT(fb1, fb2)
#define LRT_EDGE_BA_MARCHING_END
#define LRT_ITER_ALL_LINES_END
#define LRT_ITER_ALL_LINES_BEGIN
#define GET_RATIO(n)
#define DISCARD_NONSENSE_SEGMENTS
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
void lineart_unwrap_v3(LineartGpencilModifierData *lmd_legacy, const GreasePencilLineartModifierData *lmd)
void lineart_wrap_v3(const LineartGpencilModifierData *lmd_legacy, GreasePencilLineartModifierData *lmd)
BMesh Iterators.
void lineart_main_discard_out_of_frame_edges(LineartData *ld)
static bool lineart_shadow_cast_generate_edges(LineartData *ld, bool do_original_edges, LineartElementLinkNode **r_veln, LineartElementLinkNode **r_eeln)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool lineart_edge_from_triangle(const LineartTriangle *tri, const LineartEdge *e, bool allow_overlapping_edges)
unsigned __int64 uint64_t
Definition BKE_text.h:91
LineartEdge * lineart_find_matching_edge(struct LineartElementLinkNode *shadow_eln, uint64_t edge_identifier)
@ LRT_ELEMENT_INTERSECTION_DATA
MINLINE double dot_v3v3_db(const double a[3], const double b[3]) ATTR_WARN_UNUSED_RESULT
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
static bool lineart_shadow_cast_onto_triangle(LineartData *ld, LineartTriangle *tri, LineartShadowEdge *sedge, double *r_at_1, double *r_at_2, double *r_fb_co_1, double *r_fb_co_2, double *r_gloc_1, double *r_gloc_2, bool *r_facing_light)
MINLINE void copy_v3db_v3fl(double r[3], const float a[3])
BLI_INLINE int lineart_line_isec_2d_ignore_line2pos(const double a1[2], const double a2[2], const double b1[2], const double b2[2], double *r_a_ratio)
void lineart_main_bounding_areas_connect_post(LineartData *ld)
@ LRT_TILE_RECURSIVE_PERSPECTIVE
@ LRT_TILE_RECURSIVE_ORTHO
void lineart_main_load_geometries(Depsgraph *depsgraph, Scene *scene, Object *camera, LineartData *ld, bool allow_duplicates, bool do_shadow_casting, ListBaseT< LineartElementLinkNode > *shadow_elns, Set< const Object * > *included_objects)
static void lineart_shadow_edge_cut(LineartData *ld, LineartShadowEdge *e, double start, double end, double *start_gloc, double *end_gloc, double *start_fb_co, double *end_fb_co, bool facing_light, uint32_t target_reference)
static void lineart_shadow_register_enclosed_shapes(LineartData *ld, LineartData *shadow_ld)
MINLINE void copy_v4_v4_db(double r[4], const double a[4])
static void lineart_shadow_create_shadow_edge_array(LineartData *ld, bool transform_edge_cuts, bool do_light_contour)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
MINLINE double interpd(double target, double origin, double t)
static void lineart_shadow_transform_task(void *__restrict userdata, const int element_index, const TaskParallelTLS *__restrict)
void lineart_matrix_perspective_44d(double(*mProjection)[4], double fFov_rad, double fAspect, double zMin, double zMax)
static bool lineart_do_closest_segment(bool is_persp, double *s1_fb_co_1, double *s1_fb_co_2, double *s2_fb_co_1, double *s2_fb_co_2, double *s1_gloc_1, double *s1_gloc_2, double *s2_gloc_1, double *s2_gloc_2, double *r_fb_co_1, double *r_fb_co_2, double *r_gloc_1, double *r_gloc_2, double *r_new_in_the_middle, double *r_new_in_the_middle_global, double *r_new_at, bool *is_side_2r, bool *use_new_ref)
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void lineart_destroy_render_data_keep_init(LineartData *ld)
MINLINE void copy_v3_v3_db(double r[3], const double a[3])
MINLINE double ratiod(double min, double max, double pos)
static LineartShadowSegment * lineart_give_shadow_segment(LineartData *ld)
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
Global G
Definition blender.cc:57
unsigned short uint16_t
Definition BKE_text.h:80
static void lineart_shadow_segment_slice_get(double *fb_co_1, double *fb_co_2, double *gloc_1, double *gloc_2, double ratio, double at_1, double at_2, double *r_fb_co, double *r_gloc)
unsigned int uint32_t
Definition BKE_text.h:81
void lineart_add_edge_to_array(LineartPendingEdges *pe, LineartEdge *e)
void lineart_main_clear_linked_edges(LineartData *ld)
unsigned char uchar
Definition BKE_text.h:58
void mul_v4_m4v3_db(double r[4], const double mat[4][4], const double vec[3])
void copy_m4_m4_db(double m1[4][4], const double m2[4][4])
void lineart_register_intersection_shadow_cuts(struct LineartData *ld, ListBaseT< LineartElementLinkNode > *shadow_elns)
void lineart_main_add_triangles(LineartData *ld)
static bool lineart_contour_viewed_from_dark_side(LineartData *ld, LineartEdge *e)
void lineart_matrix_ortho_44d(double(*mProjection)[4], double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
void lineart_main_occlusion_begin(LineartData *ld)
VecBase< double, 3 > double3
void lineart_register_shadow_cuts(struct LineartData *ld, struct LineartEdge *e, struct LineartEdge *shadow_edge)
void lineart_main_get_view_vector(LineartData *ld)
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
static void lineart_shadow_cast(LineartData *ld, bool transform_edge_cuts, bool do_light_contour)
void unit_m4_db(double m[4][4])
@ MOD_LINEART_EDGE_FLAG_PROJECTED_SHADOW
@ MOD_LINEART_EDGE_FLAG_LOOSE
@ MOD_LINEART_EDGE_FLAG_CONTOUR_SECONDARY
@ MOD_LINEART_EDGE_FLAG_SHADOW_FACING_LIGHT
@ MOD_LINEART_EDGE_FLAG_LIGHT_CONTOUR
@ MOD_LINEART_EDGE_FLAG_CONTOUR
void BLI_spin_unlock(SpinLock *spin)
Definition threads.cc:433
void lineart_main_cull_triangles(LineartData *ld, bool clip_far)
MINLINE void mul_v3db_db(double r[3], double f)
void interp_v3_v3v3_db(double target[3], const double a[3], const double b[3], double t)
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:394
void * lineart_mem_acquire(struct LineartStaticMemPool *smp, size_t size)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void lineart_shadow_finalize_shadow_edges_task(void *__restrict userdata, const int i, const TaskParallelTLS *__restrict)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void lineart_main_link_lines(LineartData *ld)
MINLINE void sub_v3_v3v3_db(double r[3], const double a[3], const double b[3])
void lineart_edge_cut(LineartData *ld, LineartEdge *e, double start, double end, uchar material_mask_bits, uchar mat_occlusion, uint32_t shadow_bits)
@ LRT_SHADOW_FACING_LIGHT
@ LRT_SHADOW_CASTED
void lineart_main_transform_and_add_shadow(struct LineartData *ld, struct LineartElementLinkNode *veln, struct LineartElementLinkNode *eeln)
void lineart_main_make_enclosed_shapes(struct LineartData *ld, struct LineartData *shadow_ld)
static void lineart_shadow_register_silhouette(LineartData *ld)
void lineart_finalize_object_edge_array_reserve(LineartPendingEdges *pe, int count)
MINLINE float interpf(float target, float origin, float t)
double BLI_time_now_seconds()
Definition time.cc:30
void * lineart_mem_acquire_thread(struct LineartStaticMemPool *smp, size_t size)
void mul_m4db_m4db_m4fl(double R[4][4], const double A[4][4], const float B[4][4])
bool lineart_main_try_generate_shadow(struct Depsgraph *depsgraph, struct Scene *scene, struct LineartData *original_ld, struct LineartGpencilModifierData *lmd_legacy, struct LineartStaticMemPool *shadow_data_pool, struct LineartElementLinkNode **r_veln, struct LineartElementLinkNode **r_eeln, ListBaseT< LineartElementLinkNode > *r_calculated_edges_eln_list, struct LineartData **r_shadow_ld_if_reproject)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
@ MOD_LINEART_ALLOW_DUPLI_OBJECTS
void BLI_spin_lock(SpinLock *spin)
Definition threads.cc:408
VecBase< double, 4 > double4
void lineart_main_perspective_division(LineartData *ld)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void lineart_main_free_adjacent_data(LineartData *ld)
bool lineart_main_try_generate_shadow_v3(struct Depsgraph *depsgraph, struct Scene *scene, struct LineartData *original_ld, struct GreasePencilLineartModifierData *lmd, struct LineartStaticMemPool *shadow_data_pool, struct LineartElementLinkNode **r_veln, struct LineartElementLinkNode **r_eeln, ListBaseT< LineartElementLinkNode > *r_calculated_edges_eln_list, struct LineartData **r_shadow_ld_if_reproject)
void lineart_main_bounding_area_make_initial(LineartData *ld)
LineartElementLinkNode * lineart_find_matching_eln(ListBaseT< LineartElementLinkNode > *shadow_elns, int obindex)
struct LineartModifierRuntime * runtime
ListBaseT< LineartElementLinkNode > vertex_buffer_pointers
ListBaseT< LineartElementLinkNode > line_buffer_pointers
LineartShadowEdge * shadow_edges
struct blender::LineartData::_geom geom
LineartStaticMemPool * edge_data_pool
struct blender::LineartData::_qtree qtree
struct blender::LineartData::_conf conf
struct LineartPendingEdges pending_edges
LineartStaticMemPool render_data_pool
LineartStaticMemPool * shadow_data_pool
ListBaseT< LineartShadowSegment > wasted_shadow_cuts
LineartEdgeSegment * next
ListBaseT< LineartEdgeSegment > segments
LineartTriangle * t1
LineartTriangle * t2
eLineArtElementNodeFlag flags
Set< const Object * > object_dependencies
ListBaseT< LineartShadowSegment > shadow_segments
LineartEdge * e_ref_light_contour
LineartEdgeSegment * es_ref
LineartShadowSegment * next