GPv3: Material locking #114580

Merged
Falk David merged 34 commits from filedescriptor/blender:gpv3-lock-materials into main 2023-11-21 15:27:13 +01:00
Member

This PR adds the material locking functionality from the current grease pencil.

Material locking allows the user to lock strokes with that material.

The strategy for implementing this functionality is as follows:

  • Note that most (if not all) editing operators act on the selected elements.
  • We replace the retrieval of the selected elements with something like retrieve_editable_strokes/points.
  • These new functions return an IndexMask of elements that are both selected but also not locked.

TODO:

  • Selection operators
  • Transform
This PR adds the material locking functionality from the current grease pencil. Material locking allows the user to lock strokes with that material. The strategy for implementing this functionality is as follows: * Note that most (if not all) editing operators act on the selected elements. * We replace the retrieval of the selected elements with something like `retrieve_editable_strokes/points`. * These new functions return an `IndexMask` of elements that are both selected but also not locked. TODO: - [x] Selection operators - [x] Transform
Falk David added 6 commits 2023-11-07 15:07:01 +01:00
Falk David requested review from Hans Goudey 2023-11-07 15:07:13 +01:00
Hans Goudey requested changes 2023-11-08 09:32:27 +01:00
@ -252,3 +252,2 @@
static void smooth_curve_attribute(const OffsetIndices<int> points_by_curve,
const VArray<bool> &selection,
static void smooth_curve_attribute(const IndexMask &curves_to_smooth,
Member

I find it a bit more consistent to move the index mask argument below the curve data arguments, so below cyclic. That way all of the "constants" come before the "operation specific" things

I find it a bit more consistent to move the index mask argument below the curve data arguments, so below `cyclic`. That way all of the "constants" come before the "operation specific" things
filedescriptor marked this conversation as resolved
@ -260,3 +261,3 @@
GMutableSpan data)
{
threading::parallel_for(points_by_curve.index_range(), 512, [&](const IndexRange range) {
curves_to_smooth.foreach_index([&](const int64_t curve_i) {
Member

Add GrainSize(512) to keep this loop multi-threaded

Add `GrainSize(512)` to keep this loop multi-threaded
filedescriptor marked this conversation as resolved
@ -505,1 +518,3 @@
selection.materialize(points_to_delete);
/* Mark all points in the editable curves to be deleted. */
Array<bool> points_to_delete(curves.points_num(), false);
editable_strokes.foreach_index([&](const int64_t curve_i) {
Member

bke::curves::fill_points

`bke::curves::fill_points`
filedescriptor marked this conversation as resolved
@ -597,2 +611,3 @@
static Array<bool> get_points_to_dissolve(bke::CurvesGeometry &curves, const DissolveMode mode)
static Array<bool> get_points_to_dissolve(bke::CurvesGeometry &curves,
const IndexMask &editable_points,
Member

I find this combination of the use of selection and "editable" in the same function pretty confusing. Better to combine selection and locking outside of here so the function only needs one index mask argument.

I find this combination of the use of selection and "editable" in the same function pretty confusing. Better to combine selection and locking outside of here so the function only needs one index mask argument.
filedescriptor marked this conversation as resolved
@ -836,3 +853,3 @@
curves.attributes_for_write().lookup_or_add_for_write_span<int>("material_index",
ATTR_DOMAIN_CURVE);
selected_curves.foreach_index(
editable_strokes.foreach_index(
Member

Might as well use index_mask::masked_fill

Might as well use `index_mask::masked_fill`
filedescriptor marked this conversation as resolved
@ -149,0 +169,4 @@
const bke::greasepencil::Drawing &drawing,
IndexMaskMemory &memory)
{
using namespace blender;
Member

We're already in the blender namespace here

We're already in the blender namespace here
filedescriptor marked this conversation as resolved
@ -149,0 +189,4 @@
/* Get all the selected strokes. */
const IndexMask selected_strokes = ed::curves::retrieve_selected_curves(curves, memory);
/* The editable strokes are all strokes that have an unlocked material and are selected. */
return IndexMask::from_predicate(
Member

We talked about anding two bit vectors here

We talked about anding two bit vectors here
filedescriptor marked this conversation as resolved
Falk David added 7 commits 2023-11-08 11:43:14 +01:00
Falk David added 1 commit 2023-11-08 11:48:02 +01:00
Falk David added 3 commits 2023-11-08 12:24:41 +01:00
Falk David added 2 commits 2023-11-08 12:53:57 +01:00
Falk David requested review from Hans Goudey 2023-11-08 12:54:07 +01:00
Hans Goudey requested changes 2023-11-09 09:22:15 +01:00
@ -29,3 +27,1 @@
.slice(points_by_curve[curve_i].drop_front(amount_start).drop_back(amount_end))
.fill(inverted ? true : false);
}
curves_mask.foreach_index_optimized<int64_t>([&](const int64_t curve_i) {
Member

Think I'd go with foreach_index rather than foreach_index_optimized, this isn't that performance sensitive and the inside of the loop isn't completely trivial.

Think I'd go with `foreach_index` rather than `foreach_index_optimized`, this isn't *that* performance sensitive and the inside of the loop isn't completely trivial.
filedescriptor marked this conversation as resolved
@ -164,0 +166,4 @@
return init;
}
for (const int i : indices_range) {
if (span[indices_to_check[i]] == value) {
Member

Slice the index mask here first, and keep the span.slice(range).contains(value); in case that slice is a range. IndexMask operator [] is O(logn), and should be avoided wherever possible.

Same below

Slice the index mask here first, and keep the `span.slice(range).contains(value);` in case that slice is a range. IndexMask operator [] is `O(logn)`, and should be avoided wherever possible. Same below
Author
Member

I can check if it's a range and slice, but if it's not a range, I still have to use the operator[] I think. Using foreach_index won't work, because I can't return true from that if I find value. Only thing I could maybe do is write to an std::atomic<bool> or something.

Also I'm not sure if I can do any of the stuff you mentioned in the case below. At this point we know the varray is neither a single nor a span, so we either have to use operator[] or materialize I think.

I can check if it's a range and slice, but if it's not a range, I still have to use the `operator[]` I think. Using `foreach_index` won't work, because I can't `return true` from that if I find `value`. Only thing I could maybe do is write to an `std::atomic<bool>` or something. Also I'm not sure if I can do any of the stuff you mentioned in the case below. At this point we know the `varray` is neither a single nor a span, so we either have to use `operator[]` or `materialize` I think.
filedescriptor marked this conversation as resolved
@ -260,3 +314,1 @@
if (has_anything_selected(selection_curve)) {
fill_selection_true(selection_curve);
}
curves_mask.foreach_index_optimized<int64_t>([&](const int64_t curve_i) {
Member

foreach_index_optimized -> foreach_index(GrainSize(256)

foreach_index_optimized should only be used where there's a clear performance benefit (when the inner loop is very simple). We should be careful about bloating the binary size when it doesn't make a difference. Please double check that the new code isn't removing parallelism in more places. I won't bother repeating this comment elsewhere for now.

`foreach_index_optimized` -> `foreach_index(GrainSize(256)` `foreach_index_optimized` should only be used where there's a clear performance benefit (when the inner loop is very simple). We should be careful about bloating the binary size when it doesn't make a difference. Please double check that the new code isn't removing parallelism in more places. I won't bother repeating this comment elsewhere for now.
filedescriptor marked this conversation as resolved
Falk David added 3 commits 2023-11-10 11:39:07 +01:00
Falk David added 1 commit 2023-11-10 11:42:38 +01:00
Falk David requested review from Hans Goudey 2023-11-16 11:42:45 +01:00
Hans Goudey reviewed 2023-11-16 16:25:08 +01:00
@ -151,2 +151,4 @@
const IndexMask &indices_to_check,
const bool value)
{
const CommonVArrayInfo info = varray.common_info();
Member

Mentioned this in person-- we should avoid using operator[] for IndexMask, it's much slower than the alternative here.
Took a look at optimizing this and finishing the todo mentioned in the code:

  const CommonVArrayInfo info = varray.common_info();
  if (info.type == CommonVArrayInfo::Type::Single) {
    return *static_cast<const bool *>(info.data) == value;
  }
  if (info.type == CommonVArrayInfo::Type::Span) {
    const Span<bool> span(static_cast<const bool *>(info.data), varray.size());
    return threading::parallel_reduce(
        indices_to_check.index_range(),
        4096,
        false,
        [&](const IndexRange range, const bool init) {
          if (init) {
            return init;
          }
          const IndexMask sliced_mask = indices_to_check.slice(range);
          if (std::optional<IndexRange> range = sliced_mask.to_range()) {
            return span.slice(*range).contains(value);
          }
          for (const int64_t segment_i : IndexRange(sliced_mask.segments_num())) {
            const IndexMaskSegment segment = sliced_mask.segment(segment_i);
            for (const int i : segment) {
              if (span[i]) {
                return true;
              }
            }
          }
          return false;
        },
        std::logical_or());
  }
  return threading::parallel_reduce(
      indices_to_check.index_range(),
      2048,
      false,
      [&](const IndexRange range, const bool init) {
        if (init) {
          return init;
        }
        constexpr int64_t MaxChunkSize = 512;
        for (int64_t start = range.start(); start < range.last(); start += MaxChunkSize) {
          const int64_t end = std::min<int64_t>(start + MaxChunkSize, range.last());
          const int64_t size = end - start;
          const IndexMask sliced_mask = indices_to_check.slice(start, size);
          std::array<bool, MaxChunkSize> values;
          varray.materialize_compressed(sliced_mask, values);
          if (std::find(values.begin(), values.end(), true) != values.end()) {
            return true;
          }
        }
        return false;
      },
      std::logical_or());
Mentioned this in person-- we should avoid using `operator[]` for `IndexMask`, it's much slower than the alternative here. Took a look at optimizing this and finishing the todo mentioned in the code: ```cpp const CommonVArrayInfo info = varray.common_info(); if (info.type == CommonVArrayInfo::Type::Single) { return *static_cast<const bool *>(info.data) == value; } if (info.type == CommonVArrayInfo::Type::Span) { const Span<bool> span(static_cast<const bool *>(info.data), varray.size()); return threading::parallel_reduce( indices_to_check.index_range(), 4096, false, [&](const IndexRange range, const bool init) { if (init) { return init; } const IndexMask sliced_mask = indices_to_check.slice(range); if (std::optional<IndexRange> range = sliced_mask.to_range()) { return span.slice(*range).contains(value); } for (const int64_t segment_i : IndexRange(sliced_mask.segments_num())) { const IndexMaskSegment segment = sliced_mask.segment(segment_i); for (const int i : segment) { if (span[i]) { return true; } } } return false; }, std::logical_or()); } return threading::parallel_reduce( indices_to_check.index_range(), 2048, false, [&](const IndexRange range, const bool init) { if (init) { return init; } constexpr int64_t MaxChunkSize = 512; for (int64_t start = range.start(); start < range.last(); start += MaxChunkSize) { const int64_t end = std::min<int64_t>(start + MaxChunkSize, range.last()); const int64_t size = end - start; const IndexMask sliced_mask = indices_to_check.slice(start, size); std::array<bool, MaxChunkSize> values; varray.materialize_compressed(sliced_mask, values); if (std::find(values.begin(), values.end(), true) != values.end()) { return true; } } return false; }, std::logical_or()); ```
filedescriptor marked this conversation as resolved
Falk David added 2 commits 2023-11-16 16:53:30 +01:00
Falk David added 1 commit 2023-11-16 16:55:13 +01:00
Hans Goudey requested changes 2023-11-16 18:25:10 +01:00
@ -231,1 +269,3 @@
void select_all(bke::CurvesGeometry &curves, const eAttrDomain selection_domain, int action)
static void invert_selection(MutableSpan<float> selection, const IndexMask &mask)
{
mask.foreach_index_optimized<int64_t>(
Member

GrainSize(2048)

`GrainSize(2048)`
filedescriptor marked this conversation as resolved
@ -280,3 +355,1 @@
threading::parallel_for(curves.curves_range(), 256, [&](const IndexRange range) {
for (const int curve_i : range) {
const IndexRange points = points_by_curve[curve_i];
curves_mask.foreach_index_optimized<int64_t>([&](const int64_t curve_i) {
Member

foreach_index (not "optimized"). Same below, and probably other places.

`foreach_index` (not "optimized"). Same below, and probably other places.
filedescriptor marked this conversation as resolved
Falk David added 5 commits 2023-11-17 11:01:03 +01:00
Falk David requested review from Hans Goudey 2023-11-17 11:01:16 +01:00
Hans Goudey reviewed 2023-11-19 00:10:04 +01:00
@ -129,0 +134,4 @@
IndexMaskMemory &memory);
IndexMask retrieve_editable_elements(Object &object,
const bke::greasepencil::Drawing &drawing,
const eAttrDomain selection_domain,
Member

const eAttrDomain selection_domain -> eAttrDomain selection_domain

`const eAttrDomain selection_domain` -> `eAttrDomain selection_domain`
filedescriptor marked this conversation as resolved
@ -169,3 +169,2 @@
closest_distances.reinitialize(points.size());
closest_distances.fill(std::numeric_limits<float>::max());
Vector<float> closest_distances(points.size());
Member

Use foreach_segment and a for loop inside to avoid reconstructing this vector from scratch every time. Like the docstring says, foreach_segment should be used whenever processing more than one element in a loop can be faster.

Use `foreach_segment` and a `for` loop inside to avoid reconstructing this vector from scratch every time. Like the docstring says, `foreach_segment` should be used whenever processing more than one element in a loop can be faster.
Author
Member

This seems unrelated to this PR

This seems unrelated to this PR
Member

Why is it unrelated? With the changes in the PR, the Vector that was reused for many curves is now constructed from scratch for every curve.

Why is it unrelated? With the changes in the PR, the `Vector` that was reused for many curves is now constructed from scratch for every curve.
Author
Member

Sorry, I missunderstood you, will make the changes.

Sorry, I missunderstood you, will make the changes.
filedescriptor marked this conversation as resolved
Falk David added 1 commit 2023-11-21 10:13:29 +01:00
Falk David added 2 commits 2023-11-21 14:22:15 +01:00
Hans Goudey approved these changes 2023-11-21 14:25:09 +01:00
Author
Member

@blender-bot build

@blender-bot build
Falk David referenced this issue from a commit 2023-11-21 15:27:13 +01:00
Falk David merged commit 78d9267a56 into main 2023-11-21 15:27:13 +01:00
Falk David deleted branch gpv3-lock-materials 2023-11-21 15:27:15 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#114580
No description provided.