GPencil: Undo System #95450

Open
opened 2022-02-02 18:46:30 +01:00 by Falk David · 4 comments
Member

Overall design

The gpencil undo system is a relative differential undo system (see https://wiki.blender.org/wiki/Source/Architecture/Undo). Steps do not encode the whole object data, but rather only what changed between steps. We make use of the update cache (#95401) to retrieve what changed.

How undoing works

We only save “one-way” diffs. That means we can get from the current state into the future, but we cannot go into the past. E.g. when drawing a stroke and then deleting a layer, the system will save the change with the stroke and then the change with the layer. When we undo now we need to restore the deleted layer, but the previous step just has the data with the change of the stroke.
In this case we need to go back further in time until we find a step that changed what we want to restore. Then we need to re-apply all the diffs (going back to the future) until we reach the desired step.
In order to guarantee that we always find a step that has enough data in it, we force the first step of the “grease pencil undo chain” to be a full copy (initial full-copy). When a step is freed, we also ensure that the full copy is kept in the first position.
In the case that the previous step already encodes the element we are looking for, we just apply those changes to the current state.

How redoing works

Redoing is more trivial: we just need to apply the step's diff onto the current grease pencil data.

Limitations

Going into object mode in an undo step
Right now, when going into any gpencil mode from object mode and then undoing will not work. This is probably because the global undo system is not able to restore the object mode for gpencil objects.

Undoing changes outside of the active grease pencil data
The gpencil undo system only encodes a step if:

  • the active gpencil data was modified
  • the scene's frame changed
  • the step before was not part of the same undo system (initial full-copy)
    All other modifications are not taken into account, and therefore not undo-able (similarly to how the mesh undo system works).

Editing multiple gpencil objects
Because of how steps are encoded, the current system only supports undo/redo on the active GP object. If the dopesheet displays keyframes of several objects, only the modifications applied on the active object's one will be undo-able.

How to free the update cache

Without the undo system, the update cache was always freed after the update-on-write happened, e.g. after the changes were written to the eval object. With the undo system, we need to make sure that the cache is still populated when we encode a step. This can fail for example when we run a modal operator. Every modal execution might trigger an update-on-write, but only after the operator has finished do we encode the undo step.
So we need to make sure that we only free the cache after the encode step was run. This is done by tagging the gpencil data with a flag GP_DATA_UPDATE_CACHE_DISPOSABLE after the encode, and clearing it after a free.

Memory usage

Steps that encode full copies or layer copies might become very large. In the worst case, the entire object might be duplicated multiple times in memory. This applies mostly to operations that modify layers (e.g. reordering layers, deleting a layer or adding a new one).

Additionally, we need to make a decision on what happens when the update cache is not populated. Right now, this will fall back to a full-copy of the data block. Again this will increase the memory usage substantially. An alternative would be to only encode steps when the cache is populated. Ideally this would be the case once the cache is used everywhere in grease pencil.

Implementation

Undo system patch: D13996

Co-authored-by: @yann-lty

## Overall design The gpencil undo system is a relative differential undo system (see https://wiki.blender.org/wiki/Source/Architecture/Undo). Steps do not encode the whole object data, but rather only what changed between steps. We make use of the update cache (#95401) to retrieve what changed. ## How undoing works We only save “one-way” diffs. That means we can get from the current state into the future, but we cannot go into the past. E.g. when drawing a stroke and then deleting a layer, the system will save the change with the stroke and then the change with the layer. When we undo now we need to restore the deleted layer, but the previous step just has the data with the change of the stroke. In this case we need to go back further in time until we find a step that changed what we want to restore. Then we need to re-apply all the diffs (going back to the future) until we reach the desired step. In order to guarantee that we always find a step that has enough data in it, we force the first step of the “grease pencil undo chain” to be a full copy (initial full-copy). When a step is freed, we also ensure that the full copy is kept in the first position. In the case that the previous step already encodes the element we are looking for, we just apply those changes to the current state. ## How redoing works Redoing is more trivial: we just need to apply the step's diff onto the current grease pencil data. ## Limitations ~~**Going into object mode in an undo step** Right now, when going into any gpencil mode from object mode and then undoing will not work. This is probably because the global undo system is not able to restore the object mode for gpencil objects.~~ **Undoing changes outside of the active grease pencil data** The gpencil undo system only encodes a step if: - the active gpencil data was modified - the scene's frame changed - the step before was not part of the same undo system (initial full-copy) All other modifications are not taken into account, and therefore not undo-able (similarly to how the mesh undo system works). **Editing multiple gpencil objects** Because of how steps are encoded, the current system only supports undo/redo on the active GP object. If the dopesheet displays keyframes of several objects, only the modifications applied on the active object's one will be undo-able. ## How to free the update cache Without the undo system, the update cache was always freed after the update-on-write happened, e.g. after the changes were written to the eval object. With the undo system, we need to make sure that the cache is still populated when we encode a step. This can fail for example when we run a modal operator. Every modal execution might trigger an update-on-write, but only after the operator has finished do we encode the undo step. So we need to make sure that we only free the cache after the encode step was run. This is done by tagging the gpencil data with a flag `GP_DATA_UPDATE_CACHE_DISPOSABLE` after the encode, and clearing it after a free. ## Memory usage Steps that encode full copies or layer copies might become very large. In the worst case, the entire object might be duplicated multiple times in memory. This applies mostly to operations that modify layers (e.g. reordering layers, deleting a layer or adding a new one). Additionally, we need to make a decision on what happens when the update cache is not populated. Right now, this will fall back to a full-copy of the data block. Again this will increase the memory usage substantially. An alternative would be to only encode steps when the cache is populated. Ideally this would be the case once the cache is used everywhere in grease pencil. ## Implementation Undo system patch: [D13996](https://archive.blender.org/developer/D13996) Co-authored-by: @yann-lty
Author
Member

Added subscribers: @yann-lty, @filedescriptor

Added subscribers: @yann-lty, @filedescriptor

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Falk David was assigned by Antonio Vazquez 2022-02-03 12:55:30 +01:00

Added subscriber: @hamza-el-barmaki

Added subscriber: @hamza-el-barmaki

HI, reallly nice work @filedescriptor
any news on this in near future maybe blender v3.4 or 3.5 or after rewrite of eevee-next?

  • a found that for primitive tools 'like arc, circle tools..' are lagging in heavy GP, also ,sculpt mode every tool works smouthly except strenght tools which is lagging in heavy GP ,is it a a knowns issue?
HI, reallly nice work @filedescriptor any news on this in near future maybe blender v3.4 or 3.5 or after rewrite of eevee-next? - a found that for primitive tools 'like arc, circle tools..' are lagging in heavy GP, also ,sculpt mode every tool works smouthly except strenght tools which is lagging in heavy GP ,is it a a knowns issue?
Philipp Oeser removed the
Interest
Grease Pencil
label 2023-02-09 15:19:13 +01:00
Bastien Montagne added this to the Core project 2023-02-09 15:42:54 +01:00
Bastien Montagne removed this from the Core project 2023-02-09 18:20:37 +01:00
Sign in to join this conversation.
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
3 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#95450
No description provided.