Blender V5.3
draw_cache_impl_grease_pencil.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
10
11#include "BKE_attribute.hh"
12#include "BKE_curves.hh"
13#include "BKE_curves_utils.hh"
14#include "BKE_grease_pencil.h"
15#include "BKE_grease_pencil.hh"
17#include "BKE_material.hh"
18
19#include "BLI_array_utils.hh"
20#include "BLI_listbase.hh"
21#include "BLI_offset_indices.hh"
22#include "BLI_task.hh"
24
26#include "DNA_material_types.h"
27
28#include "DRW_engine.hh"
29#include "DRW_render.hh"
30
31#include "ED_curves.hh"
32#include "ED_grease_pencil.hh"
33
34#include "GPU_batch.hh"
35
36#include "draw_cache.hh"
37#include "draw_cache_impl.hh"
38
41
42namespace blender::draw {
43
44#define EDIT_CURVES_NURBS_CONTROL_POINT (1u)
45#define EDIT_CURVES_BEZIER_HANDLE (1u << 1)
46#define EDIT_CURVES_ACTIVE_HANDLE (1u << 2)
47/* Bezier curve control point lying on the curve.
48 * The one between left and right handles. */
49#define EDIT_CURVES_BEZIER_KNOT (1u << 3)
50#define EDIT_CURVES_HANDLE_TYPES_SHIFT (4u)
51
59 gpu::Batch *geom_batch;
60 gpu::Batch *lines_batch;
61 gpu::Batch *edit_points;
62 gpu::Batch *edit_lines;
63 gpu::Batch *edit_handles;
64
65 /* Crazy-space point positions for original points. */
68 /* Selection of original points. */
70 /* vflag of original points. */
72 /* Indices of visible points. */
74
75 /* Crazy-space point positions for all line points. */
77 /* Selection of line points. */
79 /* Indices for lines segments. */
81
82 /* Additional data needed for shader to choose color for each point in edit_points_pos.
83 * If first bit is set, then point is NURBS control point. EDIT_CURVES_NURBS_CONTROL_POINT is
84 * used to set and test. If second, then point is Bezier handle point. Set and tested with
85 * EDIT_CURVES_BEZIER_HANDLE.
86 * In Bezier case two handle types of HandleType are also encoded.
87 * Byte structure for Bezier knot point (handle middle point):
88 * | left handle type | right handle type | | BEZIER| NURBS|
89 * | 7 6 | 5 4 | 3 2 | 1 | 0 |
90 *
91 * If it is left or right handle point, then same handle type is repeated in both slots.
92 */
94
96
101};
102
103/* -------------------------------------------------------------------- */
106
107/* MUST match the format below. */
116
118{
119 static const GPUVertFormat format = []() {
120 GPUVertFormat format{};
121 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32_32);
122 GPU_vertformat_attr_add(&format, "ma", gpu::VertAttrType::SINT_32_32_32_32);
123 GPU_vertformat_attr_add(&format, "uv", gpu::VertAttrType::SFLOAT_32_32_32_32);
124 return format;
125 }();
126 return &format;
127}
128
129/* MUST match the format below. */
131 float vcol[4]; /* Vertex color */
132 float fcol[4]; /* Fill color */
133};
134
136{
137 static const GPUVertFormat format = []() {
138 GPUVertFormat format{};
139 GPU_vertformat_attr_add(&format, "col", gpu::VertAttrType::SFLOAT_32_32_32_32);
140 GPU_vertformat_attr_add(&format, "fcol", gpu::VertAttrType::SFLOAT_32_32_32_32);
141 return format;
142 }();
143 return &format;
144}
145
147
148/* -------------------------------------------------------------------- */
151
153{
154 BLI_assert(grease_pencil.runtime != nullptr);
155 const GreasePencilBatchCache *cache = grease_pencil.runtime->batch_cache;
156 return (cache && cache->is_dirty == false &&
157 cache->cache_frame == grease_pencil.runtime->eval_frame);
158}
159
161{
162 BLI_assert(grease_pencil.runtime != nullptr);
163 GreasePencilBatchCache *cache = grease_pencil.runtime->batch_cache;
164 if (cache == nullptr) {
165 cache = MEM_new<GreasePencilBatchCache>(__func__);
166 grease_pencil.runtime->batch_cache = cache;
167 }
168 else {
169 *cache = {};
170 }
171
172 cache->is_dirty = false;
173 cache->cache_frame = grease_pencil.runtime->eval_frame;
174
175 return cache;
176}
177
210
222
224
225/* -------------------------------------------------------------------- */
228
230 const float asp,
231 const float softness,
232 const float miter_angle)
233{
234 int32_t packed = 0;
235 /* Aspect uses 9 bits */
236 float asp_normalized = (asp > 1.0f) ? (1.0f / asp) : asp;
237 /* Use the default aspect ratio of 1 when the value is outside of the valid range. */
238 if (asp_normalized <= 0.0f) {
239 asp_normalized = 1.0f;
240 }
241 packed |= int32_t(unit_float_to_uchar_clamp(asp_normalized));
242 /* Store if inverted in the 9th bit. */
243 if (asp > 1.0f) {
244 packed |= 1 << 8;
245 }
246 /* Rotation uses 9 bits */
247 /* Rotation are in [-90..90] degree range, so we can encode the sign of the angle + the cosine
248 * because the cosine will always be positive. */
249 packed |= int32_t(unit_float_to_uchar_clamp(cosf(rot))) << 9;
250 /* Store sine sign in 9th bit. */
251 if (rot < 0.0f) {
252 packed |= 1 << 17;
253 }
254 /* Hardness uses 8 bits */
255 packed |= int32_t(unit_float_to_uchar_clamp(1.0f - softness)) << 18;
256
257 /* Miter Angle uses the last 6 bits */
258 if (miter_angle <= GP_STROKE_MITER_ANGLE_ROUND) {
259 packed |= GP_CORNER_TYPE_ROUND_BITS << 26;
260 }
261 else if (miter_angle >= GP_STROKE_MITER_ANGLE_BEVEL) {
262 packed |= GP_CORNER_TYPE_BEVEL_BITS << 26;
263 }
264 else {
265 const float miter_norm = (miter_angle / M_PI);
266 packed |= int32_t(clamp_i(
268 << 26;
269 }
270
271 return packed;
272}
273
276{
277 return cache->edit_points_pos == nullptr && cache->edit_line_indices == nullptr &&
278 cache->edit_points_indices == nullptr && cache->edit_points == nullptr &&
279 cache->edit_lines == nullptr;
280}
281
283 Object &object,
284 const bke::greasepencil::Drawing &drawing,
285 const int layer_index,
286 IndexMaskMemory &memory)
287{
288 const bke::CurvesGeometry &curves = drawing.strokes();
290 object, drawing, layer_index, memory);
291 if (!curves.has_curve_with_type(CURVE_TYPE_NURBS)) {
292 return editable_strokes;
293 }
294
295 const VArray<int8_t> types = curves.curve_types();
296 return IndexMask::from_predicate(editable_strokes, memory, [&](const int64_t curve) {
297 return types[curve] != CURVE_TYPE_NURBS;
298 });
299}
300
302 Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
303{
304 const bke::CurvesGeometry &curves = drawing.strokes();
305
307 object, drawing, memory);
308 if (!curves.has_curve_with_type(CURVE_TYPE_NURBS)) {
309 return visible_strokes;
310 }
311
312 const VArray<int8_t> types = curves.curve_types();
313 return IndexMask::from_predicate(visible_strokes, memory, [&](const int64_t curve) {
314 return types[curve] != CURVE_TYPE_NURBS;
315 });
316}
317
319 const IndexMask &curves_mask,
320 MutableSpan<uint> lines_data,
321 int *r_drawing_line_index,
322 int *r_drawing_line_start_offset)
323{
324 const bke::CurvesGeometry &curves = drawing.strokes();
325 const VArray<bool> cyclic = curves.cyclic();
326 const OffsetIndices<int> points_by_curve_eval = curves.evaluated_points_by_curve();
327
328 const int offset = *r_drawing_line_start_offset;
329 int line_index = *r_drawing_line_index;
330
331 /* Fill line indices. */
332 curves_mask.foreach_index([&](const int curve_i) {
333 const IndexRange points = points_by_curve_eval[curve_i];
334 const bool is_cyclic = cyclic[curve_i];
335
336 for (const int point : points) {
337 lines_data[line_index++] = point + offset;
338 }
339
340 if (is_cyclic) {
341 lines_data[line_index++] = points.first() + offset;
342 }
343
344 lines_data[line_index++] = gpu::RESTART_INDEX;
345 });
346
347 *r_drawing_line_index = line_index;
348 *r_drawing_line_start_offset += curves.evaluated_points_num();
349}
350
352 const IndexMask &curves_mask,
353 MutableSpan<uint> points_data,
354 int *r_drawing_point_index,
355 int *r_drawing_start_offset)
356{
357 const bke::CurvesGeometry &curves = drawing.strokes();
358 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
359
360 const int offset = *r_drawing_start_offset;
361 int ibo_index = *r_drawing_point_index;
362
363 /* Fill point indices. */
364 curves_mask.foreach_index([&](const int curve_i) {
365 const IndexRange points = points_by_curve[curve_i];
366 for (const int point : points) {
367 points_data[ibo_index++] = point + offset;
368 }
369 });
370
371 *r_drawing_point_index = ibo_index;
372 *r_drawing_start_offset += curves.points_num();
373}
374
376 Object &object,
377 const bke::greasepencil::Drawing &drawing,
378 int layer_index,
379 IndexMaskMemory &memory)
380{
381 const bke::CurvesGeometry &curves = drawing.strokes();
382
383 if (!curves.has_curve_with_type(CURVE_TYPE_NURBS)) {
384 return IndexMask(0);
385 }
386
387 const IndexMask selected_editable_strokes =
389 object, drawing, layer_index, memory);
390
391 return bke::curves::indices_for_type(curves.curve_types(),
392 curves.curve_type_counts(),
394 selected_editable_strokes,
395 memory);
396}
397
399 const bke::greasepencil::Drawing &drawing,
400 int layer_index,
401 IndexMaskMemory &memory)
402{
403 const bke::CurvesGeometry &curves = drawing.strokes();
404
405 if (!curves.has_curve_with_type(CURVE_TYPE_NURBS)) {
406 return IndexMask(0);
407 }
408
409 const IndexMask selected_editable_strokes = ed::greasepencil::retrieve_editable_strokes(
410 object, drawing, layer_index, memory);
411
412 return bke::curves::indices_for_type(curves.curve_types(),
413 curves.curve_type_counts(),
415 selected_editable_strokes,
416 memory);
417}
418
420 const IndexMask &nurbs_curves,
421 const VArray<float> &selected_point,
422 const float4x4 &layer_space_to_object_space,
423 MutableSpan<float3> edit_line_points,
424 MutableSpan<float> edit_line_selection,
425 int *r_drawing_line_start_offset,
426 int *r_total_line_ids_num)
427{
428 if (nurbs_curves.is_empty()) {
429 return;
430 }
431
432 const bke::CurvesGeometry &curves = drawing.strokes();
433 const Span<float3> positions = curves.positions();
434
435 IndexMaskMemory memory;
437 curves.points_by_curve(), nurbs_curves, memory);
438 const IndexRange eval_slice = IndexRange(*r_drawing_line_start_offset, nurbs_points.size());
439
440 MutableSpan<float3> positions_eval_slice = edit_line_points.slice(eval_slice);
441
442 array_utils::gather(positions, nurbs_points, positions_eval_slice);
443 math::transform_points(layer_space_to_object_space, positions_eval_slice);
444
445 MutableSpan<float> selection_eval_slice = edit_line_selection.slice(eval_slice);
446
447 array_utils::gather(selected_point, nurbs_points, selection_eval_slice);
448
449 /* Add one point for each NURBS point. */
450 *r_drawing_line_start_offset += nurbs_points.size();
451 *r_total_line_ids_num += nurbs_points.size();
452
453 /* Add one id for the restart after every NURBS. */
454 *r_total_line_ids_num += nurbs_curves.size();
455}
456
458 const IndexMask &nurbs_curves,
459 MutableSpan<uint> lines_data,
460 int *r_drawing_line_index,
461 int *r_drawing_line_start_offset)
462{
463 const bke::CurvesGeometry &curves = drawing.strokes();
464 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
465 if (nurbs_curves.is_empty()) {
466 return;
467 }
468
469 int line_index = *r_drawing_line_index;
470
471 /* Add all NURBS points. */
472 nurbs_curves.foreach_index([&](const int curve_i) {
473 const IndexRange points = points_by_curve[curve_i];
474
475 for (const int point : points.index_range()) {
476 lines_data[line_index++] = point + *r_drawing_line_start_offset;
477 }
478
479 lines_data[line_index++] = gpu::RESTART_INDEX;
480
481 *r_drawing_line_start_offset += points.size();
482 });
483
484 *r_drawing_line_index = line_index;
485}
486
489 const Scene &scene)
490{
491 using namespace blender::bke::greasepencil;
492
493 constexpr float no_active_weight = 666.0f;
494
495 BLI_assert(grease_pencil.runtime != nullptr);
496 GreasePencilBatchCache *cache = grease_pencil.runtime->batch_cache;
497
498 if (cache->edit_points_pos != nullptr) {
499 return;
500 }
501
502 /* Should be discarded together. */
504
505 /* Get active vertex group. */
506 const bDeformGroup *active_defgroup = static_cast<bDeformGroup *>(BLI_findlink(
507 &grease_pencil.vertex_group_names, grease_pencil.vertex_group_active_index - 1));
508 const char *active_defgroup_name = (active_defgroup == nullptr) ? "" : active_defgroup->name;
509
510 /* Get the visible drawings. */
513
514 const Span<const Layer *> layers = grease_pencil.layers();
515
516 static const GPUVertFormat format_points_pos = GPU_vertformat_from_attribute(
517 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
518
519 static const GPUVertFormat format_points_weight = GPU_vertformat_from_attribute(
520 "selection", gpu::VertAttrType::SFLOAT_32);
521
522 static const GPUVertFormat format_edit_line_pos = GPU_vertformat_from_attribute(
523 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
524
525 static const GPUVertFormat format_edit_line_selection = GPU_vertformat_from_attribute(
526 "selection", gpu::VertAttrType::SFLOAT_32);
527
529 cache->edit_points_pos = GPU_vertbuf_create_with_format_ex(format_points_pos, vbo_flag);
530 cache->edit_points_selection = GPU_vertbuf_create_with_format_ex(format_points_weight, vbo_flag);
531 cache->edit_line_pos = GPU_vertbuf_create_with_format_ex(format_edit_line_pos, vbo_flag);
532 cache->edit_line_selection = GPU_vertbuf_create_with_format_ex(format_edit_line_selection,
533 vbo_flag);
534
535 int visible_points_num = 0;
536 int total_line_ids_num = 0;
537 int total_points_num = 0;
538 int total_line_points_num = 0;
539 for (const ed::greasepencil::DrawingInfo &info : drawings) {
540 const Layer &layer = *layers[info.layer_index];
541 const bke::CurvesGeometry &curves = info.drawing.strokes();
542 total_line_points_num += curves.evaluated_points_num();
543
544 /* Do not show points for locked layers. */
545 if (layer.is_locked()) {
546 continue;
547 }
548
549 total_points_num += curves.points_num();
550
551 IndexMaskMemory memory;
553 object, info.drawing, info.layer_index, memory);
555 curves.points_by_curve(), nurbs_curves, memory);
556
557 /* Add one point for each NURBS point. */
558 total_line_points_num += nurbs_points.size();
559 }
560
561 if (total_points_num == 0) {
562 return;
563 }
564
565 GPU_vertbuf_data_alloc(*cache->edit_points_pos, total_points_num);
566 GPU_vertbuf_data_alloc(*cache->edit_points_selection, total_points_num);
567 GPU_vertbuf_data_alloc(*cache->edit_line_pos, total_line_points_num);
568 GPU_vertbuf_data_alloc(*cache->edit_line_selection, total_line_points_num);
569
570 MutableSpan<float3> points_pos = cache->edit_points_pos->data<float3>();
571 MutableSpan<float> points_weight = cache->edit_points_selection->data<float>();
572 MutableSpan<float3> edit_line_points = cache->edit_line_pos->data<float3>();
573 MutableSpan<float> edit_line_weight = cache->edit_line_selection->data<float>();
574
575 int drawing_start_offset = 0;
576 int drawing_line_start_offset = 0;
577 for (const ed::greasepencil::DrawingInfo &info : drawings) {
578 const Layer &layer = *layers[info.layer_index];
579 const float4x4 layer_space_to_object_space = layer.to_object_space(object);
580 const bke::CurvesGeometry &curves = info.drawing.strokes();
581 const OffsetIndices<int> points_by_curve_eval = curves.evaluated_points_by_curve();
582 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
583 const VArray<bool> cyclic = curves.cyclic();
584 IndexMaskMemory memory;
585
586 const IndexMask visible_strokes_for_points = ed::greasepencil::retrieve_editable_strokes(
587 object, info.drawing, info.layer_index, memory);
588 const IndexMask visible_strokes_for_lines = grease_pencil_get_visible_non_nurbs_curves(
589 object, info.drawing, memory);
590
591 const IndexRange points(drawing_start_offset, curves.points_num());
592 const IndexRange points_eval(drawing_line_start_offset, curves.evaluated_points_num());
593
594 math::transform_points(curves.evaluated_positions(),
595 layer_space_to_object_space,
596 edit_line_points.slice(points_eval));
597
598 drawing_line_start_offset += curves.evaluated_points_num();
599
600 /* Add one id for the restart after every curve. */
601 total_line_ids_num += visible_strokes_for_lines.size();
602 /* Add one id for every non-cyclic segment. */
603 total_line_ids_num += offset_indices::sum_group_sizes(points_by_curve_eval,
604 visible_strokes_for_lines);
605 /* Add one id for the last segment of every cyclic curve. */
606 total_line_ids_num += array_utils::count_booleans(curves.cyclic(), visible_strokes_for_lines);
607
608 /* Get vertex weights of the active vertex group in this drawing. */
609 const VArray<float> weights = *curves.attributes().lookup_or_default<float>(
610 active_defgroup_name, bke::AttrDomain::Point, no_active_weight);
611
612 MutableSpan<float> line_weights_slice = edit_line_weight.slice(points_eval);
613
614 /* Poly curves evaluated points match the curve points, no need to interpolate. */
615 if (curves.is_single_type(CURVE_TYPE_POLY)) {
616 array_utils::copy(weights, line_weights_slice);
617 }
618 else {
619 curves.ensure_can_interpolate_to_evaluated();
620 Array<float> weights_array(curves.points_num());
621 array_utils::copy(weights, weights_array.as_mutable_span());
622 curves.interpolate_to_evaluated(weights_array.as_span(), line_weights_slice);
623
624 /* Interpolated attributes from Catmull Rom can go outside of the original range.
625 * So clamp the weight to ensure the range. */
626 if (curves.has_curve_with_type(CURVE_TYPE_CATMULL_ROM)) {
627 for (const int point : line_weights_slice.index_range()) {
628 if (line_weights_slice[point] == no_active_weight) {
629 continue;
630 }
631 line_weights_slice[point] = math::clamp(line_weights_slice[point], 0.0f, 1.0f);
632 }
633 }
634 }
635
636 /* Do not show points for locked layers. */
637 if (layer.is_locked()) {
638 continue;
639 }
640
642 curves.positions(), layer_space_to_object_space, points_pos.slice(points));
643 MutableSpan<float> weights_slice = points_weight.slice(points);
644 array_utils::copy(weights, weights_slice);
645
646 drawing_start_offset += curves.points_num();
647
649 object, info.drawing, info.layer_index, memory);
651 nurbs_curves,
652 weights,
653 layer_space_to_object_space,
654 edit_line_points,
655 edit_line_weight,
656 &drawing_line_start_offset,
657 &total_line_ids_num);
658
659 const int drawing_visible_points_num = offset_indices::sum_group_sizes(
660 points_by_curve, visible_strokes_for_points);
661 visible_points_num += drawing_visible_points_num;
662 }
663
664 GPUIndexBufBuilder lines_builder;
665 GPU_indexbuf_init_ex(&lines_builder, GPU_PRIM_LINE_STRIP, total_line_ids_num, total_points_num);
666 MutableSpan<uint> lines_data = GPU_indexbuf_get_data(&lines_builder);
667 int lines_ibo_index = 0;
668
669 GPUIndexBufBuilder points_builder;
670 GPU_indexbuf_init(&points_builder, GPU_PRIM_POINTS, visible_points_num, total_points_num);
671 MutableSpan<uint> points_data = GPU_indexbuf_get_data(&points_builder);
672 int points_ibo_index = 0;
673
674 /* Fill point index buffer with data. */
675 drawing_start_offset = 0;
676 drawing_line_start_offset = 0;
677 for (const ed::greasepencil::DrawingInfo &info : drawings) {
678 const Layer &layer = *layers[info.layer_index];
679 IndexMaskMemory memory;
680
681 const IndexMask visible_strokes_for_lines = grease_pencil_get_visible_non_nurbs_curves(
682 object, info.drawing, memory);
683 const IndexMask visible_strokes_for_points = ed::greasepencil::retrieve_editable_strokes(
684 object, info.drawing, info.layer_index, memory);
685
686 index_buf_add_line_points(info.drawing,
687 visible_strokes_for_lines,
688 lines_data,
689 &lines_ibo_index,
690 &drawing_line_start_offset);
691
692 /* Do not show points for locked layers. */
693 if (layer.is_locked()) {
694 continue;
695 }
696
698 object, info.drawing, info.layer_index, memory);
700 info.drawing, nurbs_curves, lines_data, &lines_ibo_index, &drawing_line_start_offset);
701 index_buf_add_points(info.drawing,
702 visible_strokes_for_points,
703 points_data,
704 &points_ibo_index,
705 &drawing_start_offset);
706 }
707
708 cache->edit_line_indices = GPU_indexbuf_build_ex(&lines_builder, 0, INT_MAX, true);
709 cache->edit_points_indices = GPU_indexbuf_build_ex(&points_builder, 0, INT_MAX, false);
710
711 /* Create the batches. */
715
719
720 /* Allow creation of buffer texture. */
725
726 cache->is_dirty = false;
727}
728
729static void index_buf_add_bezier_handle_lines(const IndexMask bezier_points,
730 const int all_points,
731 MutableSpan<uint2> handle_lines,
732 int *r_drawing_line_index,
733 const int *r_drawing_line_start_offset)
734{
735 if (bezier_points.is_empty()) {
736 return;
737 }
738
739 const int offset = *r_drawing_line_start_offset;
740 int line_index = *r_drawing_line_index;
741
742 /* Add all bezier handle lines. */
743 bezier_points.foreach_index([&](const int point_i, const int pos) {
744 handle_lines[line_index++] = uint2(offset + all_points + pos + bezier_points.size() * 0,
745 offset + point_i);
746 handle_lines[line_index++] = uint2(offset + all_points + pos + bezier_points.size() * 1,
747 offset + point_i);
748 });
749
750 *r_drawing_line_index = line_index;
751}
752
753static uint32_t bezier_data_value(int8_t handle_type, bool is_active)
754{
756 (is_active ? EDIT_CURVES_ACTIVE_HANDLE : 0);
757}
758
759static void index_buf_add_bezier_line_points(const IndexMask bezier_points,
760 MutableSpan<uint> points_data,
761 int *r_drawing_point_index,
762 int *r_drawing_start_offset)
763{
764 if (bezier_points.is_empty()) {
765 return;
766 }
767
768 const int offset = *r_drawing_start_offset;
769 int ibo_index = *r_drawing_point_index;
770
771 /* Add all bezier points. */
772 for (const int point : IndexRange(bezier_points.size() * 2)) {
773 points_data[ibo_index++] = point + offset;
774 }
775
776 *r_drawing_point_index = ibo_index;
777 *r_drawing_start_offset += bezier_points.size() * 2;
778}
779
780/* Still use legacy vflag for GPv3 for now due to common shader defines. */
781#define GREASE_PENCIL_EDIT_POINT_SELECTED (1 << 0)
782#define GREASE_PENCIL_EDIT_STROKE_SELECTED (1 << 1)
783#define GREASE_PENCIL_EDIT_MULTIFRAME (1 << 2)
784#define GREASE_PENCIL_EDIT_STROKE_START (1 << 3)
785#define GREASE_PENCIL_EDIT_STROKE_END (1 << 4)
786#define GREASE_PENCIL_EDIT_POINT_DIMMED (1 << 5)
787
790 const Scene &scene)
791{
792 using namespace blender::bke::greasepencil;
793 BLI_assert(grease_pencil.runtime != nullptr);
794 GreasePencilBatchCache *cache = grease_pencil.runtime->batch_cache;
795
796 if (cache->edit_points_pos != nullptr) {
797 return;
798 }
799
800 /* Should be discarded together. */
802
803 /* Get the visible drawings. */
806
807 const Span<const Layer *> layers = grease_pencil.layers();
808
809 static const GPUVertFormat format_edit_points_pos = GPU_vertformat_from_attribute(
810 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
811
812 static const GPUVertFormat format_edit_points_rad = GPU_vertformat_from_attribute(
813 "rad", gpu::VertAttrType::SFLOAT_32);
814
815 static const GPUVertFormat format_edit_line_pos = GPU_vertformat_from_attribute(
816 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
817
818 static const GPUVertFormat format_edit_points_selection = GPU_vertformat_from_attribute(
819 "selection", gpu::VertAttrType::SFLOAT_32);
820
821 static const GPUVertFormat format_edit_points_vflag = GPU_vertformat_from_attribute(
822 "vflag", gpu::VertAttrType::UINT_32);
823
824 static const GPUVertFormat format_edit_line_selection = GPU_vertformat_from_attribute(
825 "selection", gpu::VertAttrType::SFLOAT_32);
826
827 static const GPUVertFormat format_edit_points_info = GPU_vertformat_from_attribute(
828 "data", gpu::VertAttrType::UINT_32);
829
831 cache->edit_points_pos = GPU_vertbuf_create_with_format_ex(format_edit_points_pos, vbo_flag);
832 cache->edit_points_rad = GPU_vertbuf_create_with_format_ex(format_edit_points_rad, vbo_flag);
833 cache->edit_points_selection = GPU_vertbuf_create_with_format_ex(format_edit_points_selection,
834 vbo_flag);
835 cache->edit_points_vflag = GPU_vertbuf_create_with_format_ex(format_edit_points_vflag, vbo_flag);
836 cache->edit_line_pos = GPU_vertbuf_create_with_format_ex(format_edit_line_pos, vbo_flag);
837 cache->edit_line_selection = GPU_vertbuf_create_with_format_ex(format_edit_line_selection,
838 vbo_flag);
839 cache->edit_points_info = GPU_vertbuf_create_with_format_ex(format_edit_points_info, vbo_flag);
840
841 int total_points_num = 0;
842 for (const ed::greasepencil::DrawingInfo &info : drawings) {
843 const Layer &layer = *layers[info.layer_index];
844 /* Do not show points for locked layers. */
845 if (layer.is_locked()) {
846 continue;
847 }
848
849 const bke::CurvesGeometry &curves = info.drawing.strokes();
850 total_points_num += curves.points_num();
851 }
852
853 int total_line_points_num = 0;
854 for (const ed::greasepencil::DrawingInfo &info : drawings) {
855 const bke::CurvesGeometry &curves = info.drawing.strokes();
856 total_line_points_num += curves.evaluated_points_num();
857 }
858
859 int total_bezier_point_num = 0;
860 for (const ed::greasepencil::DrawingInfo &info : drawings) {
861 IndexMaskMemory memory;
863 object, info.drawing, info.layer_index, CURVE_HANDLE_ALL, memory);
864
865 total_bezier_point_num += bezier_points.size();
866 }
867
868 for (const ed::greasepencil::DrawingInfo &info : drawings) {
869 const bke::CurvesGeometry &curves = info.drawing.strokes();
870
871 IndexMaskMemory memory;
873 object, info.drawing, info.layer_index, memory);
875 curves.points_by_curve(), nurbs_curves, memory);
876
877 /* Add one point for each NURBS point. */
878 total_line_points_num += nurbs_points.size();
879 }
880
881 /* Add two for each bezier point, (one left, one right). */
882 total_points_num += total_bezier_point_num * 2;
883
884 if (total_points_num == 0) {
885 return;
886 }
887
888 GPU_vertbuf_data_alloc(*cache->edit_points_pos, total_points_num);
889 GPU_vertbuf_data_alloc(*cache->edit_points_rad, total_points_num);
890 GPU_vertbuf_data_alloc(*cache->edit_points_selection, total_points_num);
891 GPU_vertbuf_data_alloc(*cache->edit_points_vflag, total_points_num);
892 GPU_vertbuf_data_alloc(*cache->edit_line_pos, total_line_points_num);
893 GPU_vertbuf_data_alloc(*cache->edit_line_selection, total_line_points_num);
894 GPU_vertbuf_data_alloc(*cache->edit_points_info, total_points_num);
895
896 MutableSpan<float3> edit_points = cache->edit_points_pos->data<float3>();
897 MutableSpan<float> edit_rad = cache->edit_points_rad->data<float>();
898 MutableSpan<float> edit_points_selection = cache->edit_points_selection->data<float>();
899 MutableSpan<uint32_t> edit_points_vflag = cache->edit_points_vflag->data<uint32_t>();
900 MutableSpan<float3> edit_line_points = cache->edit_line_pos->data<float3>();
901 MutableSpan<float> edit_line_selection = cache->edit_line_selection->data<float>();
902 MutableSpan<uint32_t> edit_points_info = cache->edit_points_info->data<uint32_t>();
903 edit_points_selection.fill(0.0f);
904 edit_points_vflag.fill(0);
905 edit_points_info.fill(0);
906 edit_line_selection.fill(0.0f);
907 edit_rad.fill(0.0f);
908
909 int visible_points_num = 0;
910 int total_line_ids_num = 0;
911 int total_bezier_num = 0;
912 int drawing_start_offset = 0;
913 int drawing_line_start_offset = 0;
914 for (const ed::greasepencil::DrawingInfo &info : drawings) {
915 const Layer &layer = *layers[info.layer_index];
916 const float4x4 layer_space_to_object_space = layer.to_object_space(object);
917 const bke::CurvesGeometry &curves = info.drawing.strokes();
918 const OffsetIndices<int> points_by_curve_eval = curves.evaluated_points_by_curve();
919 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
920
921 const VArraySpan<float> radii = curves.radius();
922
923 IndexMaskMemory memory;
924 const IndexMask visible_strokes_for_lines = grease_pencil_get_editable_non_nurbs_curves(
925 object, info.drawing, info.layer_index, memory);
926
927 const IndexRange points(drawing_start_offset, curves.points_num());
928 const IndexRange points_eval(drawing_line_start_offset, curves.evaluated_points_num());
929
930 if (!layer.is_locked()) {
932 curves.positions(), layer_space_to_object_space, edit_points.slice(points));
933 edit_rad.slice(points).copy_from(radii);
934 }
935
936 math::transform_points(curves.evaluated_positions(),
937 layer_space_to_object_space,
938 edit_line_points.slice(points_eval));
939
940 /* Do not show selection for locked layers. */
941 if (!layer.is_locked()) {
942
943 /* Flag the start and end points. */
944 for (const int curve_i : curves.curves_range()) {
945 const IndexRange sub_points = points_by_curve[curve_i].shift(drawing_start_offset);
946 edit_points_vflag[sub_points.first()] |= GREASE_PENCIL_EDIT_STROKE_START;
947 edit_points_vflag[sub_points.last()] |= GREASE_PENCIL_EDIT_STROKE_END;
948 }
949
950 const IndexMask selected_editable_points =
952 object, info.drawing, info.layer_index, memory);
953
954 MutableSpan<float> selection_slice = edit_points_selection.slice(points);
955 index_mask::masked_fill(selection_slice, 1.0f, selected_editable_points);
956
957 const IndexMask selected_editable_points_with_bezier =
959 object, info.drawing, info.layer_index, CURVE_HANDLE_ALL, memory);
960
961 MutableSpan<float> line_selection_slice = edit_line_selection.slice(points_eval);
962
963 /* Poly curves evaluated points match the curve points, no need to interpolate. */
964 if (curves.is_single_type(CURVE_TYPE_POLY)) {
965 index_mask::masked_fill(line_selection_slice, 1.0f, selected_editable_points_with_bezier);
966 }
967 else {
968 curves.ensure_can_interpolate_to_evaluated();
969 Array<float> selected_points(curves.points_num(), 0.0f);
971 selected_points.as_mutable_span(), 1.0f, selected_editable_points_with_bezier);
972 curves.interpolate_to_evaluated(selected_points.as_span(), line_selection_slice);
973 }
974 }
975
976 drawing_line_start_offset += curves.evaluated_points_num();
977
978 /* Add one id for the restart after every curve. */
979 total_line_ids_num += visible_strokes_for_lines.size();
980 /* Add one id for every non-cyclic segment. */
981 total_line_ids_num += offset_indices::sum_group_sizes(points_by_curve_eval,
982 visible_strokes_for_lines);
983 /* Add one id for the last segment of every cyclic curve. */
984 total_line_ids_num += array_utils::count_booleans(curves.cyclic(), visible_strokes_for_lines);
985
986 /* Do not show points for locked layers. */
987 if (layer.is_locked()) {
988 continue;
989 }
990
991 drawing_start_offset += curves.points_num();
992 const IndexMask selected_editable_strokes =
994 object, info.drawing, info.layer_index, memory);
995
996 const IndexMask selected_editable_fill_strokes = bke::greasepencil::selected_mask_to_fills(
997 selected_editable_strokes, curves, bke::AttrDomain::Curve, memory);
998
999 /* Add one id for every point in a selected curve. */
1000 visible_points_num += offset_indices::sum_group_sizes(points_by_curve,
1001 selected_editable_fill_strokes);
1002
1003 const VArray<float> selected_point = *curves.attributes().lookup_or_default<float>(
1004 ".selection", bke::AttrDomain::Point, true);
1005
1007 object, info.drawing, info.layer_index, memory);
1008 grease_pencil_cache_add_nurbs(info.drawing,
1009 nurbs_curves,
1010 selected_point,
1011 layer_space_to_object_space,
1012 edit_line_points,
1013 edit_line_selection,
1014 &drawing_line_start_offset,
1015 &total_line_ids_num);
1016
1018 object, info.drawing, info.layer_index, CURVE_HANDLE_ALL, memory);
1019 if (bezier_points.is_empty()) {
1020 continue;
1021 }
1022
1023 const IndexRange left_slice = IndexRange(drawing_start_offset, bezier_points.size());
1024 const IndexRange right_slice = IndexRange(drawing_start_offset + bezier_points.size(),
1025 bezier_points.size());
1026
1027 MutableSpan<float3> positions_slice_left = edit_points.slice(left_slice);
1028 MutableSpan<float3> positions_slice_right = edit_points.slice(right_slice);
1029
1030 MutableSpan<float> radius_slice_left = edit_rad.slice(left_slice);
1031 MutableSpan<float> radius_slice_right = edit_rad.slice(right_slice);
1032
1033 if (const std::optional<Span<float3>> handles_left = curves.handle_positions_left()) {
1034 array_utils::gather(*handles_left, bezier_points, positions_slice_left);
1035 }
1036 else {
1037 positions_slice_left.fill(float3(0.0f));
1038 }
1039 if (const std::optional<Span<float3>> handles_right = curves.handle_positions_right()) {
1040 array_utils::gather(*handles_right, bezier_points, positions_slice_right);
1041 }
1042 else {
1043 positions_slice_right.fill(float3(0.0f));
1044 }
1045
1046 math::transform_points(layer_space_to_object_space, positions_slice_left);
1047 math::transform_points(layer_space_to_object_space, positions_slice_right);
1048
1049 const VArray<float> selected_left = *curves.attributes().lookup_or_default<float>(
1050 ".selection_handle_left", bke::AttrDomain::Point, true);
1051 const VArray<float> selected_right = *curves.attributes().lookup_or_default<float>(
1052 ".selection_handle_right", bke::AttrDomain::Point, true);
1053
1054 MutableSpan<float> selection_slice_left = edit_points_selection.slice(left_slice);
1055 MutableSpan<float> selection_slice_right = edit_points_selection.slice(right_slice);
1056 array_utils::gather(selected_left, bezier_points, selection_slice_left);
1057 array_utils::gather(selected_right, bezier_points, selection_slice_right);
1058
1059 const VArray<int8_t> types_left = curves.handle_types_left();
1060 const VArray<int8_t> types_right = curves.handle_types_right();
1061
1062 bezier_points.foreach_index([&](const int point_i, const int pos) {
1063 const bool selected = selected_point[point_i] || selected_left[point_i] ||
1064 selected_right[point_i];
1065 edit_points_info.slice(left_slice)[pos] = bezier_data_value(types_left[point_i], selected);
1066 edit_points_info.slice(right_slice)[pos] = bezier_data_value(types_right[point_i], selected);
1067
1068 radius_slice_left[pos] = radii[point_i];
1069 radius_slice_right[pos] = radii[point_i];
1070
1071 edit_points_info.slice(points)[point_i] = EDIT_CURVES_BEZIER_KNOT;
1072 });
1073
1074 /* Add two for each bezier point, (one left, one right). */
1075 visible_points_num += bezier_points.size() * 2;
1076 drawing_start_offset += bezier_points.size() * 2;
1077
1078 total_bezier_num += bezier_points.size();
1079 }
1080
1081 GPUIndexBufBuilder lines_builder;
1083 &lines_builder, GPU_PRIM_LINE_STRIP, total_line_ids_num, total_line_points_num);
1084 MutableSpan<uint> lines_data = GPU_indexbuf_get_data(&lines_builder);
1085 int lines_ibo_index = 0;
1086
1087 GPUIndexBufBuilder points_builder;
1088 GPU_indexbuf_init(&points_builder, GPU_PRIM_POINTS, visible_points_num, total_points_num);
1089 MutableSpan<uint> points_data = GPU_indexbuf_get_data(&points_builder);
1090 int points_ibo_index = 0;
1091
1092 GPUIndexBufBuilder handles_builder;
1093 GPU_indexbuf_init(&handles_builder, GPU_PRIM_LINES, total_bezier_num * 2, total_points_num);
1094 MutableSpan<uint2> handle_lines = GPU_indexbuf_get_data(&handles_builder).cast<uint2>();
1095
1096 int handle_lines_id = 0;
1097 /* Fill line index and point index buffers with data. */
1098 drawing_start_offset = 0;
1099 drawing_line_start_offset = 0;
1100 for (const ed::greasepencil::DrawingInfo &info : drawings) {
1101 const Layer *layer = layers[info.layer_index];
1102 IndexMaskMemory memory;
1103
1104 const IndexMask visible_strokes_for_lines = grease_pencil_get_editable_non_nurbs_curves(
1105 object, info.drawing, info.layer_index, memory);
1106
1107 index_buf_add_line_points(info.drawing,
1108 visible_strokes_for_lines,
1109 lines_data,
1110 &lines_ibo_index,
1111 &drawing_line_start_offset);
1112
1113 if (!layer->is_locked()) {
1115 object, info.drawing, info.layer_index, CURVE_HANDLE_ALL, memory);
1116 const IndexMask selected_editable_strokes =
1118 object, info.drawing, info.layer_index, memory);
1120 object, info.drawing, info.layer_index, memory);
1121 const IndexMask selected_editable_fill_strokes = bke::greasepencil::selected_mask_to_fills(
1122 selected_editable_strokes, info.drawing.strokes(), bke::AttrDomain::Curve, memory);
1123
1125 info.drawing, nurbs_curves, lines_data, &lines_ibo_index, &drawing_line_start_offset);
1127 info.drawing.strokes().points_num(),
1128 handle_lines,
1129 &handle_lines_id,
1130 &drawing_start_offset);
1131 index_buf_add_points(info.drawing,
1132 selected_editable_fill_strokes,
1133 points_data,
1134 &points_ibo_index,
1135 &drawing_start_offset);
1137 bezier_points, points_data, &points_ibo_index, &drawing_start_offset);
1138 }
1139 }
1140
1141 cache->edit_line_indices = GPU_indexbuf_build_ex(&lines_builder, 0, INT_MAX, true);
1142 cache->edit_points_indices = GPU_indexbuf_build_ex(&points_builder, 0, INT_MAX, false);
1143 cache->edit_handles_ibo = GPU_indexbuf_build_ex(&handles_builder, 0, INT_MAX, false);
1144
1145 /* Create the batches */
1150 GPU_batch_vertbuf_add(cache->edit_points, cache->edit_points_info, false);
1151
1155
1159 GPU_batch_vertbuf_add(cache->edit_handles, cache->edit_points_rad, false);
1161
1162 /* Allow creation of buffer texture. */
1169
1170 cache->is_dirty = false;
1171}
1172
1173template<typename T>
1175{
1176 if (curves.is_single_type(CURVE_TYPE_POLY)) {
1177 return input;
1178 }
1179
1180 Array<T> out(curves.evaluated_points_num());
1181 curves.interpolate_to_evaluated(VArraySpan(input), out.as_mutable_span());
1182 return VArray<T>::from_container(std::move(out));
1183};
1184
1186{
1187 const VArray<float> miter_angles = *curves.attributes().lookup_or_default<float>(
1189
1190 if (curves.is_single_type(CURVE_TYPE_POLY)) {
1191 return miter_angles;
1192 }
1193
1194 if (miter_angles.is_single() &&
1196 {
1197 return VArray<float>::from_single(GP_STROKE_MITER_ANGLE_ROUND, curves.evaluated_points_num());
1198 }
1199
1200 /* Default all the evaluated points to be round.
1201 * This is done so that the added points look as smooth as possible. */
1202 Array<float> eval_corners(curves.evaluated_points_num(), GP_STROKE_MITER_ANGLE_ROUND);
1203
1204 const VArray<int8_t> types = curves.curve_types();
1205 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1206 const OffsetIndices<int> evaluated_points_by_curve = curves.evaluated_points_by_curve();
1207
1208 threading::parallel_for(curves.curves_range(), 128, [&](IndexRange range) {
1209 for (const int curve_i : range) {
1210 const IndexRange eval_points = evaluated_points_by_curve[curve_i];
1211 const IndexRange points = points_by_curve[curve_i];
1212 MutableSpan<float> eval_corners_range = eval_corners.as_mutable_span().slice(eval_points);
1213
1214 switch (types[curve_i]) {
1215 case CURVE_TYPE_POLY:
1216 for (const int i : points.index_range()) {
1217 eval_corners_range[i] = miter_angles[points[i]];
1218 }
1219 break;
1220 case CURVE_TYPE_BEZIER: {
1221 const Span<int> offsets = curves.bezier_evaluated_offsets_for_curve(curve_i);
1222 for (const int i : points.index_range()) {
1223 eval_corners_range[offsets[i]] = miter_angles[points[i]];
1224 }
1225 break;
1226 }
1227 case CURVE_TYPE_NURBS:
1228 case CURVE_TYPE_CATMULL_ROM: {
1229 /* NURBS and Catmull-Rom are continuous and don't have corners. */
1230 break;
1231 }
1232 }
1233 }
1234 });
1235 return VArray<float>::from_container(std::move(eval_corners));
1236}
1237
1244static float segment_radius_length(const float l, const float r1, const float r2)
1245{
1246 const float a = r2 - r1;
1247
1248 /* Avoid division by zero. */
1249 if (r1 <= 0.0f || l <= 0.0f || l == a) {
1250 return 0.0f;
1251 }
1252
1253 /* If the two radii are close to being the same, calculate as if they were. */
1254 if (abs(a) < 0.001f * l) {
1255 return l / r1;
1256 }
1257
1258 const float E = (l + a) / (l - a);
1259 const float E_i = a / r1 + 1.0f;
1260
1261 /* Return zero if one dot is inside the other. */
1262 if (E <= 0.0f || E_i <= 0.0f) {
1263 return 0.0f;
1264 }
1265
1266 return 2.0f * log(E_i) / log(E);
1267}
1268
1270 const VArray<float> &radii,
1271 const IndexRange &points)
1272{
1273 Array<float> radii_lengths(lengths.size());
1274
1275 float radii_length = 0.0f;
1276 for (const int i : lengths.index_range()) {
1277 const float l = lengths[i] - (i > 0 ? lengths[i - 1] : 0.0f);
1278 const float r1 = radii[points[i]];
1279 const float r2 = radii[points[(i + 1) % points.size()]];
1280 radii_length += segment_radius_length(l, r1, r2);
1281 radii_lengths[i] = radii_length;
1282 }
1283
1284 return radii_lengths;
1285}
1286
1289 const Scene &scene)
1290{
1291 using namespace blender::bke::greasepencil;
1292 BLI_assert(grease_pencil.runtime != nullptr);
1293 GreasePencilBatchCache *cache = grease_pencil.runtime->batch_cache;
1294
1295 if (cache->vbo != nullptr) {
1296 return;
1297 }
1298
1299 /* Should be discarded together. */
1300 BLI_assert(cache->vbo == nullptr && cache->ibo == nullptr);
1301 BLI_assert(cache->geom_batch == nullptr);
1302
1303 /* Get the visible drawings. */
1306
1307 /* First, count how many vertices and triangles are needed for the whole object. Also record the
1308 * offsets into the curves for the vertices. */
1309 int total_verts_num = 0;
1310 int total_triangles_num = 0;
1311 Vector<Array<int>> verts_start_offsets_per_visible_drawing;
1312 for (const ed::greasepencil::DrawingInfo &info : drawings) {
1313 const bke::CurvesGeometry &curves = info.drawing.strokes();
1314 const OffsetIndices<int> points_by_curve = curves.evaluated_points_by_curve();
1315 const VArray<bool> cyclic = curves.cyclic();
1316 IndexMaskMemory memory;
1318 object, info.drawing, memory);
1320 object, info.drawing, memory);
1321 const std::optional<GroupedSpan<int3>> triangles = info.drawing.triangles();
1322 const GroupedSpan<float3> intersection_points = info.drawing.intersection_points();
1323
1324 Array<int> verts_start_offsets(curves.curves_num() + 1, 0);
1325
1326 int num_cyclic = 0;
1327 int num_points = 0;
1328
1329 total_triangles_num += triangles ?
1330 offset_indices::sum_group_sizes(triangles->offsets, visible_fills) :
1331 0;
1332
1333 /* Calculate the vertex offsets for all the visible curves. */
1334 visible_strokes.foreach_index([&](const int curve_i) {
1335 IndexRange points = points_by_curve[curve_i];
1336 const bool is_cyclic = cyclic[curve_i] && (points.size() > 2);
1337
1338 if (is_cyclic) {
1339 num_cyclic++;
1340 }
1341
1342 verts_start_offsets[curve_i] = total_verts_num;
1343 /* One vertex is stored before and after as padding. */
1344 total_verts_num += 1 + points.size() + 1;
1345 /* Cyclic strokes have one extra vertex. */
1346 total_verts_num += (is_cyclic ? 1 : 0);
1347 num_points += points.size();
1348 });
1349
1350 verts_start_offsets.last() = total_verts_num;
1351
1352 for (const int fill_index : intersection_points.index_range()) {
1353 const Span<float3> inter_points = intersection_points[fill_index];
1354
1355 total_verts_num += inter_points.size();
1356 }
1357
1358 total_triangles_num += (num_points + num_cyclic) * 2;
1359
1360 verts_start_offsets_per_visible_drawing.append(std::move(verts_start_offsets));
1361 }
1362
1364 /* Create VBOs. */
1366 const GPUVertFormat *format_col = grease_pencil_color_format();
1367 cache->vbo = GPU_vertbuf_create_with_format_ex(*format, vbo_flag);
1368 cache->vbo_col = GPU_vertbuf_create_with_format_ex(*format_col, vbo_flag);
1369 /* Add extra space at the end of the buffer because of quad load. */
1370 GPU_vertbuf_data_alloc(*cache->vbo, total_verts_num + 2);
1371 GPU_vertbuf_data_alloc(*cache->vbo_col, total_verts_num + 2);
1372
1375 /* Create IBO. */
1377 GPU_indexbuf_init(&ibo, GPU_PRIM_TRIS, total_triangles_num, INT_MAX);
1378 MutableSpan<uint3> triangle_ibo_data = GPU_indexbuf_get_data(&ibo).cast<uint3>();
1379 int triangle_ibo_index = 0;
1380
1381 /* Fill buffers with data. */
1382 for (const int drawing_i : drawings.index_range()) {
1383 const ed::greasepencil::DrawingInfo &info = drawings[drawing_i];
1384 const Layer &layer = grease_pencil.layer(info.layer_index);
1385 const float4x4 layer_space_to_object_space = layer.to_object_space(object);
1386 const float4x4 object_space_to_layer_space = math::invert(layer_space_to_object_space);
1387 const bke::CurvesGeometry &curves = info.drawing.strokes();
1388 if (curves.evaluated_points_num() == 0) {
1389 continue;
1390 }
1391
1392 const bke::AttributeAccessor attributes = curves.attributes();
1393 const OffsetIndices<int> points_by_curve = curves.evaluated_points_by_curve();
1394 const Span<float3> positions = curves.evaluated_positions();
1395 const VArray<bool> cyclic = curves.cyclic();
1396
1397 curves.ensure_can_interpolate_to_evaluated();
1398
1402 *attributes.lookup_or_default<float>("rotation", bke::AttrDomain::Point, 0.0f), curves);
1405 "vertex_color", bke::AttrDomain::Point, ColorGeometry4f(0.0f, 0.0f, 0.0f, 0.0f)),
1406 curves);
1407 const VArray<float> miter_angles = interpolate_corners(curves);
1408
1409 /* Assumes that if the ".selection" attribute does not exist, all points are selected. */
1410 const VArray<float> selection_float = *attributes.lookup_or_default<float>(
1411 ".selection", bke::AttrDomain::Point, true);
1412 const VArray<int8_t> start_caps = *attributes.lookup_or_default<int8_t>(
1414 const VArray<int8_t> end_caps = *attributes.lookup_or_default<int8_t>(
1415 "end_cap", bke::AttrDomain::Curve, 0);
1416 const VArray<float> stroke_softness = *attributes.lookup_or_default<float>(
1417 "softness", bke::AttrDomain::Curve, 0.0f);
1418 const VArray<float> stroke_point_aspect_ratios = *attributes.lookup_or_default<float>(
1419 "aspect_ratio", bke::AttrDomain::Curve, 1.0f);
1420 const VArray<ColorGeometry4f> stroke_fill_colors = info.drawing.fill_colors();
1421 const VArray<int> materials = *attributes.lookup_or_default<int>(
1422 "material_index", bke::AttrDomain::Curve, 0);
1423 const VArray<float> u_translations = *attributes.lookup_or_default<float>(
1424 "u_translation", bke::AttrDomain::Curve, 0.0f);
1425 const VArray<float> u_scales = *attributes.lookup_or_default<float>(
1426 "u_scale", bke::AttrDomain::Curve, 1.0f);
1427 const VArray<float> fill_opacities = *attributes.lookup_or_default<float>(
1428 "fill_opacity", bke::AttrDomain::Curve, 1.0f);
1429 const VArray<int> fill_ids = *attributes.lookup_or_default<int>(
1430 "fill_id", bke::AttrDomain::Curve, 0);
1431
1432 const std::optional<GroupedSpan<int3>> triangles = info.drawing.triangles();
1433 const std::optional<GroupedSpan<int>> fills = info.drawing.fills();
1434 const GroupedSpan<float3> intersection_points = info.drawing.intersection_points();
1435 const OffsetIndices<int> intersection_offsets = intersection_points.offsets;
1436 const Span<float4x2> texture_matrices = info.drawing.texture_matrices();
1437 const Span<int> verts_start_offsets = verts_start_offsets_per_visible_drawing[drawing_i];
1438 IndexMaskMemory memory;
1440 object, info.drawing, memory);
1441
1442 curves.ensure_evaluated_lengths();
1443
1444 auto populate_point = [&](IndexRange verts_range,
1445 int curve_i,
1446 int8_t start_cap,
1447 int8_t end_cap,
1448 int point_i,
1449 int idx,
1450 float u_stroke,
1451 float first_curve,
1452 float first_vert,
1453 bool cyclic,
1454 const float4x2 &texture_matrix,
1455 GreasePencilStrokeVert &s_vert,
1456 GreasePencilColorVert &c_vert) {
1457 const float3 pos = math::transform_point(layer_space_to_object_space, positions[point_i]);
1458 copy_v3_v3(s_vert.pos, pos);
1459 /* GP data itself does not constrain radii to be positive, but drawing code expects it, and
1460 * use negative values as a special 'flag' to get rounded caps. */
1461 s_vert.radius = math::max(radii[point_i], 0.0f) *
1462 ((end_cap == GP_STROKE_CAP_TYPE_ROUND) ? 1.0f : -1.0f);
1463 s_vert.opacity = opacities[point_i] *
1464 ((start_cap == GP_STROKE_CAP_TYPE_ROUND) ? 1.0f : -1.0f);
1465
1466 /* Store if the curve is cyclic in the sign of the point index. */
1467 s_vert.point_id = cyclic ? -verts_range[idx] : verts_range[idx];
1468 s_vert.stroke_id = first_vert;
1469
1470 /* The material index is allowed to be negative as it's stored as a generic attribute. To
1471 * ensure the material used by the shader is valid this needs to be clamped to zero. */
1472 s_vert.mat = std::max(materials[first_curve], 0) % GPENCIL_MATERIAL_BUFFER_LEN;
1473
1474 s_vert.packed_asp_hard_rot = pack_rotation_aspect_hardness_miter(
1475 rotations[point_i],
1476 stroke_point_aspect_ratios[curve_i],
1477 stroke_softness[curve_i],
1478 miter_angles[point_i]);
1479 s_vert.u_stroke = u_stroke;
1480 copy_v2_v2(s_vert.uv_fill, texture_matrix * float4(pos, 1.0f));
1481
1482 copy_v4_v4(c_vert.vcol, vertex_colors[point_i]);
1483 copy_v4_v4(c_vert.fcol, stroke_fill_colors[first_curve]);
1484 c_vert.fcol[3] = (int(c_vert.fcol[3] * 10000.0f) * 10.0f) + fill_opacities[first_curve];
1485 };
1486
1487 auto populate_curve = [&](const int curve_i,
1488 const int first_curve,
1489 const int first_vert,
1490 const float4x2 &texture_matrix) {
1491 const IndexRange points = points_by_curve[curve_i];
1492 const bool is_cyclic = cyclic[curve_i] && (points.size() > 2);
1493 const int verts_start_offset = verts_start_offsets[curve_i];
1494 const int num_verts = 1 + points.size() + (is_cyclic ? 1 : 0) + 1;
1495 const IndexRange verts_range = IndexRange(verts_start_offset, num_verts);
1496 MutableSpan<GreasePencilStrokeVert> verts_slice = verts.slice(verts_range);
1497 MutableSpan<GreasePencilColorVert> cols_slice = cols.slice(verts_range);
1498
1499 const Span<float> lengths = curves.evaluated_lengths_for_curve(curve_i, cyclic[curve_i]);
1500 const float u_translation = u_translations[curve_i];
1501 const float u_scale = u_scales[curve_i];
1502 const int mat_id = materials[curve_i];
1503
1504 MaterialGPencilStyle *gp_style = BKE_gpencil_material_settings(&object, mat_id + 1);
1505
1506 Array<float> radii_lengths;
1507 const bool is_line = gp_style->mode == GP_MATERIAL_MODE_LINE;
1508
1509 if (gp_style->placement_mode == GP_MATERIAL_PLACEMENT_RADIUS && !is_line) {
1510 radii_lengths = get_radii_lengths(lengths, radii, points);
1511 }
1512
1513 auto get_u_stroke = [&](const int i) {
1514 if (is_line) {
1515 const float u = i > 0 ? lengths[i - 1] : 0.0f;
1516 return u_scale * u + u_translation;
1517 }
1518 switch (gp_style->placement_mode) {
1520 if (gp_style->placement_count == 1) {
1521 return float(i + int(u_translation));
1522 }
1523 return u_scale * float(i) + u_translation;
1524 }
1526 const float u = i > 0 ? radii_lengths[i - 1] : 0.0f;
1527 return u + u_translation;
1528 }
1530 const float u = i > 0 ? lengths[i - 1] : 0.0f;
1531 return u_scale * u + u_translation;
1532 }
1533 }
1534 /* Fallback to single dot per point. */
1535 return float(i + int(u_translation));
1536 };
1537
1538 /* First vertex is not drawn. */
1539 verts_slice.first().mat = -1;
1540 /* The first vertex will have the index of the last vertex. */
1541 verts_slice.first().stroke_id = verts_range.last();
1542
1543 /* Write all the point attributes to the vertex buffers. Create a quad for each point. */
1544 for (const int i : IndexRange(points.size())) {
1545 const int idx = i + 1;
1546 const float u_stroke = get_u_stroke(i);
1547 populate_point(verts_range,
1548 curve_i,
1549 start_caps[curve_i],
1550 end_caps[curve_i],
1551 points[i],
1552 idx,
1553 u_stroke,
1554 first_curve,
1555 first_vert,
1556 is_cyclic,
1557 texture_matrix,
1558 verts_slice[idx],
1559 cols_slice[idx]);
1560 }
1561
1562 if (is_cyclic) {
1563 const int idx = points.size() + 1;
1564 const float u_stroke = get_u_stroke(points.size());
1565 populate_point(verts_range,
1566 curve_i,
1567 start_caps[curve_i],
1568 end_caps[curve_i],
1569 points[0],
1570 idx,
1571 u_stroke,
1572 first_curve,
1573 first_vert,
1574 is_cyclic,
1575 texture_matrix,
1576 verts_slice[idx],
1577 cols_slice[idx]);
1578 }
1579
1580 /* Last vertex is not drawn. */
1581 verts_slice.last().mat = -1;
1582 /* The last vertex will have the index of the first vertex. */
1583 verts_slice.last().stroke_id = verts_range.first();
1584 };
1585
1586 if (fills) {
1587 Array<int> first_curves(curves.curves_num());
1589
1590 int fill_index = 0;
1591 Array<bool> added_fill_drawcalls(curves.curves_num(), false);
1592 for (const int curve_i : curves.curves_range()) {
1593 const bool is_filled = fill_ids[curve_i] != 0;
1594 const bool active_filled = is_filled && !added_fill_drawcalls[curve_i];
1595
1596 /* Keep track of already rendered fills. */
1597 if (active_filled) {
1598 const Span<int> fill = (*fills)[fill_index];
1599 const int first_curve = fill.first();
1600 for (const int pos : fill.index_range()) {
1601 const int curve_i = fill[pos];
1602 added_fill_drawcalls[curve_i] = true;
1603 first_curves[curve_i] = first_curve;
1604 }
1605
1606 fill_index++;
1607 }
1608 }
1609
1611 visible_strokes.index_range(),
1612 1024,
1613 [&](const IndexRange range) {
1614 visible_strokes.slice(range).foreach_index([&](const int64_t curve_i) {
1615 const int first_curve = first_curves[curve_i];
1616 const int first_vert = verts_start_offsets[first_curve];
1617 const float4x2 texture_matrix = texture_matrices[first_curve] *
1618 object_space_to_layer_space;
1619
1620 populate_curve(curve_i, first_curve, first_vert, texture_matrix);
1621 });
1622 },
1624 return offset_indices::sum_group_sizes(points_by_curve, visible_strokes.slice(range));
1625 }));
1626
1627 /* Add all of the intersection points at the end. */
1628 for (const int fill_index : intersection_points.index_range()) {
1629 const Span<float3> inter_points = intersection_points[fill_index];
1630
1631 if (inter_points.is_empty()) {
1632 continue;
1633 }
1634
1635 const int first_curve = (*fills)[fill_index].first();
1636
1637 const float4x2 texture_matrix = texture_matrices[first_curve] *
1638 object_space_to_layer_space;
1639
1640 const int fill_intersect_offset = intersection_offsets[fill_index].first();
1641
1642 for (const int inter_i : inter_points.index_range()) {
1643 const float3 &pos_3d = inter_points[inter_i];
1644
1645 const int verts_start_offset = verts_start_offsets.last() + inter_i +
1646 fill_intersect_offset;
1647
1648 GreasePencilStrokeVert &s_vert = verts[verts_start_offset];
1649 GreasePencilColorVert &c_vert = cols[verts_start_offset];
1650
1651 const float3 pos = math::transform_point(layer_space_to_object_space, pos_3d);
1652 copy_v3_v3(s_vert.pos, pos);
1653
1654 s_vert.point_id = verts_start_offset;
1655 s_vert.stroke_id = verts_start_offsets[first_curve];
1656
1657 /* The material index is allowed to be negative as it's stored as a generic attribute. To
1658 * ensure the material used by the shader is valid this needs to be clamped to zero. */
1659 s_vert.mat = std::max(materials[first_curve], 0) % GPENCIL_MATERIAL_BUFFER_LEN;
1660
1661 s_vert.u_stroke = 0.0f;
1662 copy_v2_v2(s_vert.uv_fill, texture_matrix * float4(pos, 1.0f));
1663
1664 copy_v4_v4(c_vert.fcol, stroke_fill_colors[first_curve]);
1665 c_vert.fcol[3] = (int(c_vert.fcol[3] * 10000.0f) * 10.0f) + fill_opacities[first_curve];
1666 }
1667 }
1668 }
1669 else {
1671 visible_strokes.index_range(),
1672 1024,
1673 [&](const IndexRange range) {
1674 visible_strokes.slice(range).foreach_index([&](const int64_t curve_i) {
1675 const int first_curve = curve_i;
1676 const int first_vert = verts_start_offsets[first_curve];
1677 const float4x2 texture_matrix = texture_matrices[first_curve] *
1678 object_space_to_layer_space;
1679
1680 populate_curve(curve_i, first_curve, first_vert, texture_matrix);
1681 });
1682 },
1684 return offset_indices::sum_group_sizes(points_by_curve, visible_strokes.slice(range));
1685 }));
1686 }
1687
1688 /* Fill in IBO in series. */
1689 if (fills) {
1690 int fill_index = 0;
1691
1692 Array<int> fill_index_by_curves(curves.curves_num(), -1);
1693 Array<int> first_curves(curves.curves_num());
1695
1696 for (const int curve_i : curves.curves_range()) {
1697 const bool is_filled = fill_ids[curve_i] != 0;
1698 const bool active_filled = is_filled && (fill_index_by_curves[curve_i] == -1);
1699
1700 /* Keep track of already rendered fills. */
1701 if (active_filled) {
1702 const Span<int> fill = (*fills)[fill_index];
1703 const int first_curve = fill.first();
1704 for (const int pos : fill.index_range()) {
1705 const int curve_i = fill[pos];
1706 fill_index_by_curves[curve_i] = fill_index;
1707 first_curves[curve_i] = first_curve;
1708 }
1709
1710 fill_index++;
1711 }
1712 }
1713
1714 visible_strokes.foreach_index([&](const int curve_i) {
1715 /* Will be `-1` if not a fill. */
1716 const int fill_index = fill_index_by_curves[curve_i];
1717
1718 const bool is_filled = fill_index != -1;
1719 const bool active_filled = triangles && is_filled && (first_curves[curve_i] == curve_i);
1720
1721 if (active_filled) {
1722 const int fill_index = fill_index_by_curves[curve_i];
1723 const Span<int3> tris_slice = (*triangles)[fill_index];
1724 const Span<int> fill = (*fills)[fill_index];
1725
1726 IndexMaskMemory memory;
1727 Array<int> fill_point_offset_data(fill.size() + 1);
1729 points_by_curve,
1730 IndexMask::from_indices(fill, memory),
1731 fill_point_offset_data.as_mutable_span());
1732
1733 Array<int> fill_point_to_pos_map(fill_point_offset_data.last());
1734 threading::parallel_for(fill.index_range(), 1024, [&](const IndexRange range) {
1735 for (const int i : range) {
1736 fill_point_to_pos_map.as_mutable_span().slice(fill_point_offset[i]).fill(i);
1737 }
1738 });
1739
1740 const int fill_intersect_offset = intersection_points.is_empty() ?
1741 0 :
1742 intersection_offsets[fill_index].start();
1743
1744 auto point_to_id = [&](int32_t p) {
1745 if (p < 0) {
1746 return (-(p + 1) + fill_intersect_offset + verts_start_offsets.last())
1748 }
1749 const int pos_ = fill_point_to_pos_map[p];
1750 const int curve_ = fill[pos_];
1751 const int fill_offset = fill_point_offset[pos_].first();
1752 return (1 + p - fill_offset + verts_start_offsets[curve_]) << GP_VERTEX_ID_SHIFT;
1753 };
1754
1755 /* Add all triangle indices to the index buffer. */
1756 for (const int3 tri : tris_slice) {
1757 triangle_ibo_data[triangle_ibo_index] = uint3(
1758 point_to_id(tri.x), point_to_id(tri.y), point_to_id(tri.z));
1759 triangle_ibo_index++;
1760 }
1761 }
1762
1763 const IndexRange points = points_by_curve[curve_i];
1764 const bool is_cyclic = cyclic[curve_i] && (points.size() > 2);
1765 const int verts_start_offset = verts_start_offsets[curve_i];
1766 const int num_verts = 1 + points.size() + (is_cyclic ? 1 : 0) + 1;
1767 const IndexRange verts_range = IndexRange(verts_start_offset, num_verts);
1768
1769 for (const int i : points.index_range()) {
1770 const int idx = i + 1;
1771 int v_mat = (verts_range[idx] << GP_VERTEX_ID_SHIFT) | GP_IS_STROKE_VERTEX_BIT;
1772 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 0, v_mat + 1, v_mat + 2);
1773 triangle_ibo_index++;
1774 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 2, v_mat + 1, v_mat + 3);
1775 triangle_ibo_index++;
1776 }
1777
1778 if (is_cyclic) {
1779 const int idx = points.size() + 1;
1780
1781 int v_mat = (verts_range[idx] << GP_VERTEX_ID_SHIFT) | GP_IS_STROKE_VERTEX_BIT;
1782 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 0, v_mat + 1, v_mat + 2);
1783 triangle_ibo_index++;
1784 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 2, v_mat + 1, v_mat + 3);
1785 triangle_ibo_index++;
1786 }
1787 });
1788 }
1789 else {
1790 visible_strokes.foreach_index([&](const int curve_i) {
1791 const IndexRange points = points_by_curve[curve_i];
1792 const bool is_cyclic = cyclic[curve_i] && (points.size() > 2);
1793 const int verts_start_offset = verts_start_offsets[curve_i];
1794 const int num_verts = 1 + points.size() + (is_cyclic ? 1 : 0) + 1;
1795 const IndexRange verts_range = IndexRange(verts_start_offset, num_verts);
1796
1797 for (const int i : points.index_range()) {
1798 const int idx = i + 1;
1799 int v_mat = (verts_range[idx] << GP_VERTEX_ID_SHIFT) | GP_IS_STROKE_VERTEX_BIT;
1800 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 0, v_mat + 1, v_mat + 2);
1801 triangle_ibo_index++;
1802 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 2, v_mat + 1, v_mat + 3);
1803 triangle_ibo_index++;
1804 }
1805
1806 if (is_cyclic) {
1807 const int idx = points.size() + 1;
1808
1809 int v_mat = (verts_range[idx] << GP_VERTEX_ID_SHIFT) | GP_IS_STROKE_VERTEX_BIT;
1810 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 0, v_mat + 1, v_mat + 2);
1811 triangle_ibo_index++;
1812 triangle_ibo_data[triangle_ibo_index] = uint3(v_mat + 2, v_mat + 1, v_mat + 3);
1813 triangle_ibo_index++;
1814 }
1815 });
1816 }
1817 };
1818
1819 /* Mark last 2 verts as invalid. */
1820 verts[total_verts_num + 0].mat = -1;
1821 verts[total_verts_num + 1].mat = -1;
1822 /* Also mark first vert as invalid. */
1823 verts[0].mat = -1;
1824
1825 /* Finish the IBO. */
1826 cache->ibo = GPU_indexbuf_build_ex(&ibo, 0, INT_MAX, false);
1827 /* Create the batches */
1828 cache->geom_batch = GPU_batch_create(GPU_PRIM_TRIS, cache->vbo, cache->ibo);
1829 /* Allow creation of buffer texture. */
1830 GPU_vertbuf_use(cache->vbo);
1831 GPU_vertbuf_use(cache->vbo_col);
1832
1833 cache->is_dirty = false;
1834}
1835
1838 const Scene &scene)
1839{
1840 using namespace blender::bke::greasepencil;
1841
1842 BLI_assert(grease_pencil.runtime != nullptr);
1843 GreasePencilBatchCache *cache = grease_pencil.runtime->batch_cache;
1844
1845 if (cache->lines_batch != nullptr) {
1846 return;
1847 }
1848
1850 uint32_t max_index = GPU_vertbuf_get_vertex_len(cache->vbo);
1851
1852 /* Get the visible drawings. */
1855
1856 Vector<int> index_start_per_curve;
1857 Vector<bool> cyclic_per_curve;
1858 Vector<bool> is_onion_per_curve;
1859 Vector<int> curve_intersections_start_offsets;
1860
1861 int index_len = 0;
1862 int curve_inter_offset = 0;
1863 for (const ed::greasepencil::DrawingInfo &info : drawings) {
1864 const bke::CurvesGeometry &curves = info.drawing.strokes();
1865 const OffsetIndices<int> points_by_curve = curves.evaluated_points_by_curve();
1866 const GroupedSpan<float3> intersection_points = info.drawing.intersection_points();
1867 const VArray<bool> cyclic = curves.cyclic();
1868 IndexMaskMemory memory;
1870 object, info.drawing, memory);
1871
1872 visible_strokes.foreach_index([&](const int curve_i) {
1873 const IndexRange points = points_by_curve[curve_i];
1874 const int point_len = points.size();
1875 const int point_start = index_len;
1876 const bool is_cyclic = cyclic[curve_i] && (point_len > 2);
1877 /* Count the primitive restart. */
1878 index_len += point_len + (is_cyclic ? 1 : 0) + 1;
1879 /* Don't draw the onion frames in wireframe mode. */
1880 index_start_per_curve.append(point_start);
1881 cyclic_per_curve.append(is_cyclic);
1882 is_onion_per_curve.append(info.onion_id != 0);
1883 });
1884
1885 curve_intersections_start_offsets.append_n_times(curve_inter_offset, visible_strokes.size());
1886 curve_inter_offset += intersection_points.offsets.total_size();
1887 }
1888 index_start_per_curve.append(index_len);
1889 const OffsetIndices<int> range_per_curve(index_start_per_curve, offset_indices::NoSortCheck{});
1890
1892 GPU_indexbuf_init_ex(&elb, GPU_PRIM_LINE_STRIP, index_len, max_index);
1893
1895
1896 threading::parallel_for(cyclic_per_curve.index_range(), 1024, [&](const IndexRange range) {
1897 for (const int curve : range) {
1898 /* Drop the trailing restart index. */
1899 const IndexRange offset_range = range_per_curve[curve].drop_back(1);
1900 /* Shift the range by `curve` to account for the second padding vertices.
1901 * The first one is already accounted for during counting (as primitive restart).
1902 * Shift by the number of intersection points that are at the end of the point indices in the
1903 * drawing. */
1904 const IndexRange index_range = offset_range.shift(curve + 1 +
1905 curve_intersections_start_offsets[curve]);
1906 if (is_onion_per_curve[curve]) {
1907 for (const int i : offset_range.index_range()) {
1908 indices[offset_range[i]] = gpu::RESTART_INDEX;
1909 }
1910 if (cyclic_per_curve[curve]) {
1911 indices[offset_range.last()] = gpu::RESTART_INDEX;
1912 }
1913 }
1914 else {
1915 for (const int i : offset_range.index_range()) {
1916 indices[offset_range[i]] = index_range[i];
1917 }
1918 if (cyclic_per_curve[curve]) {
1919 indices[offset_range.last()] = index_range.first();
1920 }
1921 }
1922 indices[offset_range.one_after_last()] = gpu::RESTART_INDEX;
1923 }
1924 });
1925
1926 gpu::IndexBuf *ibo = GPU_indexbuf_build_ex(&elb, 0, max_index, true);
1927
1928 cache->lines_batch = GPU_batch_create_ex(
1929 GPU_PRIM_LINE_STRIP, cache->vbo, ibo, GPU_BATCH_OWNS_INDEX);
1930
1931 cache->is_dirty = false;
1932}
1933
1935
1937{
1938 BLI_assert(grease_pencil->runtime != nullptr);
1939 GreasePencilBatchCache *cache = grease_pencil->runtime->batch_cache;
1940 if (cache == nullptr) {
1941 return;
1942 }
1943 switch (mode) {
1945 cache->is_dirty = true;
1946 break;
1947 default:
1949 }
1950}
1951
1960
1962{
1964 MEM_delete(grease_pencil->runtime->batch_cache);
1965 grease_pencil->runtime->batch_cache = nullptr;
1966}
1967
1976
1978{
1982
1983 /* Can be `nullptr` when there's no Grease Pencil drawing visible. */
1984 return cache->edit_points;
1985}
1986
1988{
1992
1993 /* Can be `nullptr` when there's no Grease Pencil drawing visible. */
1994 return cache->edit_lines;
1995}
1996
1998{
2002
2003 /* Can be `nullptr` when there's no Grease Pencil drawing visible. */
2004 return cache->edit_handles;
2005}
2006
2015
2024
2026{
2030
2031 /* Can be `nullptr` when there's no Grease Pencil drawing visible. */
2032 return cache->edit_points;
2033}
2034
2036{
2040
2041 /* Can be `nullptr` when there's no Grease Pencil drawing visible. */
2042 return cache->edit_lines;
2043}
2044
2053
2054} // namespace blender::draw
Low-level operations for curves.
Low-level operations for curves.
Low-level operations for grease pencil that cannot be defined in the C++ header yet.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define M_PI
#define GP_STROKE_MITER_ANGLE_BEVEL
#define GP_STROKE_MITER_ANGLE_ROUND
#define GPU_batch_create(primitive_type, vertex_buf, index_buf)
Definition GPU_batch.hh:143
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
#define GPU_INDEXBUF_DISCARD_SAFE(elem)
#define GPU_VERTBUF_DISCARD_SAFE(verts)
#define BLI_assert_unreachable()
#define BLI_INLINE
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
constexpr int64_t first() const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr IndexRange index_range() const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr T & first() const
Definition BLI_span.hh:685
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
static VArray from_single(T value, const int64_t size)
static VArray from_container(ContainerT container)
void append(const T &value)
IndexRange index_range() const
void append_n_times(const T &value, const int64_t n)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
std::optional< GroupedSpan< int > > fills() const
Span< float4x2 > texture_matrices() const
const bke::CurvesGeometry & strokes() const
std::optional< GroupedSpan< int3 > > triangles() const
VArray< ColorGeometry4f > fill_colors() const
GroupedSpan< float3 > intersection_points() const
constexpr Span slice(int64_t start, int64_t size) const
MutableSpan< T > data()
IndexMask slice(IndexRange range) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define EDIT_CURVES_ACTIVE_HANDLE
#define EDIT_CURVES_HANDLE_TYPES_SHIFT
#define EDIT_CURVES_BEZIER_KNOT
#define EDIT_CURVES_BEZIER_HANDLE
#define GREASE_PENCIL_EDIT_STROKE_START
#define GREASE_PENCIL_EDIT_STROKE_END
#define rot(x, k)
#define GPENCIL_MATERIAL_BUFFER_LEN
#define GP_IS_STROKE_VERTEX_BIT
#define GP_CORNER_TYPE_BEVEL_BITS
#define GP_CORNER_TYPE_ROUND_BITS
#define GP_VERTEX_ID_SHIFT
#define GP_CORNER_TYPE_MITER_NUMBER
#define INT_MAX
#define layer
#define log
#define abs
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void fill_index_range(MutableSpan< T > span, const T start=0)
int64_t count_booleans(const VArray< bool > &varray)
IndexMask indices_for_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const CurveType type, const IndexMask &selection, LinearAllocator<> &memory)
IndexMask curve_to_point_selection(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, LinearAllocator<> &memory)
IndexMask selected_mask_to_fills(const IndexMask &selected_mask, const CurvesGeometry &curves, AttrDomain selection_domain, IndexMaskMemory &memory)
static bool grease_pencil_batch_cache_valid(const GreasePencil &grease_pencil)
gpu::Batch * DRW_cache_grease_pencil_get(const Scene *scene, Object *ob)
void DRW_grease_pencil_batch_cache_validate(GreasePencil *grease_pencil)
static const GPUVertFormat * grease_pencil_stroke_format()
static Array< float > get_radii_lengths(const Span< float > lengths, const VArray< float > &radii, const IndexRange &points)
static void grease_pencil_cache_add_nurbs(const bke::greasepencil::Drawing &drawing, const IndexMask &nurbs_curves, const VArray< float > &selected_point, const float4x4 &layer_space_to_object_space, MutableSpan< float3 > edit_line_points, MutableSpan< float > edit_line_selection, int *r_drawing_line_start_offset, int *r_total_line_ids_num)
static void grease_pencil_batch_cache_clear(GreasePencil &grease_pencil)
static VArray< float > interpolate_corners(const bke::CurvesGeometry &curves)
static void index_buf_add_nurbs_lines(const bke::greasepencil::Drawing &drawing, const IndexMask &nurbs_curves, MutableSpan< uint > lines_data, int *r_drawing_line_index, int *r_drawing_line_start_offset)
static void index_buf_add_line_points(const bke::greasepencil::Drawing &drawing, const IndexMask &curves_mask, MutableSpan< uint > lines_data, int *r_drawing_line_index, int *r_drawing_line_start_offset)
void DRW_grease_pencil_batch_cache_free(GreasePencil *grease_pencil)
gpu::Batch * DRW_cache_grease_pencil_face_wireframe_get(const Scene *scene, Object *ob)
static void index_buf_add_bezier_line_points(const IndexMask bezier_points, MutableSpan< uint > points_data, int *r_drawing_point_index, int *r_drawing_start_offset)
gpu::Batch * DRW_cache_grease_pencil_edit_handles_get(const Scene *scene, Object *ob)
static GreasePencilBatchCache * grease_pencil_batch_cache_init(GreasePencil &grease_pencil)
static void grease_pencil_edit_batch_ensure(Object &object, const GreasePencil &grease_pencil, const Scene &scene)
void DRW_grease_pencil_batch_cache_dirty_tag(GreasePencil *grease_pencil, int mode)
static float segment_radius_length(const float l, const float r1, const float r2)
static IndexMask grease_pencil_get_editable_selected_nurbs_curves(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static void grease_pencil_weight_batch_ensure(Object &object, const GreasePencil &grease_pencil, const Scene &scene)
static void index_buf_add_points(const bke::greasepencil::Drawing &drawing, const IndexMask &curves_mask, MutableSpan< uint > points_data, int *r_drawing_point_index, int *r_drawing_start_offset)
static VArray< T > attribute_interpolate(const VArray< T > &input, const bke::CurvesGeometry &curves)
static GreasePencilBatchCache * grease_pencil_batch_cache_get(GreasePencil &grease_pencil)
gpu::VertBuf * DRW_cache_grease_pencil_position_buffer_get(const Scene *scene, Object *ob)
gpu::Batch * DRW_cache_grease_pencil_edit_lines_get(const Scene *scene, Object *ob)
static IndexMask grease_pencil_get_visible_non_nurbs_curves(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
static IndexMask grease_pencil_get_visible_nurbs_curves(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static const GPUVertFormat * grease_pencil_color_format()
gpu::Batch * DRW_cache_grease_pencil_weight_lines_get(const Scene *scene, Object *ob)
static bool grease_pencil_batch_cache_is_edit_discarded(GreasePencilBatchCache *cache)
gpu::Batch * DRW_cache_grease_pencil_edit_points_get(const Scene *scene, Object *ob)
static uint32_t bezier_data_value(int8_t handle_type, bool is_active)
static void index_buf_add_bezier_handle_lines(const IndexMask bezier_points, const int all_points, MutableSpan< uint2 > handle_lines, int *r_drawing_line_index, const int *r_drawing_line_start_offset)
BLI_INLINE int32_t pack_rotation_aspect_hardness_miter(const float rot, const float asp, const float softness, const float miter_angle)
gpu::Batch * DRW_cache_grease_pencil_weight_points_get(const Scene *scene, Object *ob)
static void grease_pencil_wire_batch_ensure(Object &object, const GreasePencil &grease_pencil, const Scene &scene)
static void grease_pencil_geom_batch_ensure(Object &object, const GreasePencil &grease_pencil, const Scene &scene)
gpu::VertBuf * DRW_cache_grease_pencil_color_buffer_get(const Scene *scene, Object *ob)
static IndexMask grease_pencil_get_editable_non_nurbs_curves(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, IndexMaskMemory &memory)
IndexMask retrieve_editable_and_selected_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
Vector< DrawingInfo > retrieve_visible_drawings(const Scene &scene, const GreasePencil &grease_pencil, const bool do_onion_skinning)
IndexMask retrieve_editable_and_all_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, const eHandleDisplay handle_display, IndexMaskMemory &memory)
IndexMask retrieve_editable_and_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
IndexMask retrieve_visible_bezier_handle_points(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, const eHandleDisplay handle_display, IndexMaskMemory &memory)
IndexMask retrieve_visible_strokes(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
IndexMask retrieve_visible_fills(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
IndexMask retrieve_editable_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
constexpr uint32_t RESTART_INDEX
BLI_NOINLINE void fill(T *__restrict data, const T &value, const IndexMask &mask)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
T clamp(const T &a, const T &min, const T &max)
CartesianBasis invert(const CartesianBasis &basis)
T max(const T &a, const T &b)
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
int sum_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask)
OffsetIndices< int > gather_selected_offsets(OffsetIndices< int > src_offsets, const IndexMask &selection, int start_offset, MutableSpan< int > dst_offsets)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
auto accumulated_task_sizes(Fn &&fn)
VecBase< uint32_t, 2 > uint2
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
void GPU_indexbuf_init_ex(GPUIndexBufBuilder *, GPUPrimType, uint index_len, uint vertex_len)
MINLINE void copy_v3_v3(float r[3], const float a[3])
@ GP_MATERIAL_PLACEMENT_RADIUS
@ GP_MATERIAL_PLACEMENT_COUNT
@ GP_MATERIAL_PLACEMENT_DENSITY
ATTR_WARN_UNUSED_RESULT const BMLoop * l
MINLINE void copy_v2_v2(float r[2], const float a[2])
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
@ GPU_PRIM_LINE_STRIP
void GPU_vertbuf_use(gpu::VertBuf *)
VecBase< uint32_t, 3 > uint3
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
MaterialGPencilStyle * BKE_gpencil_material_settings(Object *ob, short act)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
unsigned int uint32_t
Definition BKE_text.h:81
T & DRW_object_get_data_for_drawing(const Object &object)
MatBase< float, 4, 2 > float4x2
@ BKE_GREASEPENCIL_BATCH_DIRTY_ALL
MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
@ GPU_BATCH_OWNS_INDEX
Definition GPU_batch.hh:48
gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
@ CURVE_TYPE_CATMULL_ROM
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
gpu::IndexBuf * GPU_indexbuf_build_ex(GPUIndexBufBuilder *builder, uint index_min, uint index_max, bool uses_restart_indices)
signed int int32_t
Definition BKE_text.h:78
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
MINLINE void copy_v4_v4(float r[4], const float a[4])
VecBase< float, 3 > float3
MINLINE int clamp_i(int value, int min, int max)
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
int GPU_batch_vertbuf_add(gpu::Batch *batch, gpu::VertBuf *vertex_buf, bool own_vbo)
signed char int8_t
Definition BKE_text.h:76
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eMaterialGPencilStyle_Mode mode
eMaterialGPencilPlacementMode placement_mode
const bke::greasepencil::Drawing & drawing