Refactor: make evaluate_fcurve() take its FCurve as const instead of mutable #121788

Merged
Nathan Vegdahl merged 3 commits from nathanvegdahl/blender:const_fcurve_functions into main 2024-05-16 17:13:53 +02:00
Member

This required making a whole bunch of other functions in the call chain
take const parameters as well. It also required changing some function
pointers in some types to take const parameters, which in turn required
changing all the functions that are pointed to by those function
pointers to take const parameters as well.

Additionally, there was one mutable usage of the FModifier * parameter
in fcm_cycles_time() that had to be removed to make the call chain
const. However, this turned out to be a code path that shouldn't be
reachable, and would represent a bug elsewhere. So it was changed to
an assert.

All in all, the non-constness was deep and tangled.

There's still a lot more that we can make const, but I wanted to keep
this change as narrow and focused as possible.

Notes for Reviewers

  • There are other parameters that can trivially be made const at the function definitions (like floats, ints, etc.). I've intentionally left those alone, to focus this on the minimal set of changes needed to make evaluate_fcurve()'s FCurve * parameter const.
This required making a whole bunch of other functions in the call chain take const parameters as well. It also required changing some function pointers in some types to take const parameters, which in turn required changing all the functions that are pointed to by those function pointers to take const parameters as well. Additionally, there was one mutable usage of the `FModifier *` parameter in `fcm_cycles_time()` that had to be removed to make the call chain const. However, this turned out to be a code path that shouldn't be reachable, and would represent a bug elsewhere. So it was changed to an assert. All in all, the non-constness was deep and tangled. There's still a lot more that we can make const, but I wanted to keep this change as narrow and focused as possible. ## Notes for Reviewers - There are other parameters that can trivially be made const at the function definitions (like floats, ints, etc.). I've intentionally left those alone, to focus this on the minimal set of changes needed to make `evaluate_fcurve()`'s `FCurve *` parameter const.
Nathan Vegdahl added the
Module
Animation & Rigging
label 2024-05-14 15:41:45 +02:00
Nathan Vegdahl added 1 commit 2024-05-14 15:41:54 +02:00
This required making a whole bunch of other functions in the call chain
take const parameters as well.  It also required changing some function
pointers in some types to take const parameters, which in turn required
changing all the functions that are pointed to by those function
pointers to take const parameters as well.

All in all, the non-constness was deep and tangled.

There's still a lot more that we can make const, but I wanted to keep
this change as narrow and focused as possible.
Nathan Vegdahl reviewed 2024-05-14 15:43:00 +02:00
@ -627,3 +636,3 @@
/* FIXME... */
if (fcm->prev) {
fcm->flag |= FMODIFIER_FLAG_DISABLED;
// fcm->flag |= FMODIFIER_FLAG_DISABLED;
Author
Member

Here is the (likely bogus) change that's otherwise blocking evaluate_fcurve() from being const.

Here is the (likely bogus) change that's otherwise blocking `evaluate_fcurve()` from being const.
Member

Looks like this logic would have to be pulled out and put into evaluate_time_fmodifiers directly (that's where this code likely should have been from the start).

Looks like this logic would have to be pulled out and put into `evaluate_time_fmodifiers` directly (that's where this code likely should have been from the start).
Member
diff --git a/source/blender/blenkernel/intern/fmodifier.cc b/source/blender/blenkernel/intern/fmodifier.cc
index 025c4851733..7e49d71edee 100644
--- a/source/blender/blenkernel/intern/fmodifier.cc
+++ b/source/blender/blenkernel/intern/fmodifier.cc
@@ -623,13 +623,6 @@ static float fcm_cycles_time(
   /* Initialize storage. */
   storage->cycyofs = 0;
 
-  /* check if modifier is first in stack, otherwise disable ourself... */
-  /* FIXME... */
-  if (fcm->prev) {
-    fcm->flag |= FMODIFIER_FLAG_DISABLED;
-    return evaltime;
-  }
-
   if (fcu == nullptr || (fcu->bezt == nullptr && fcu->fpt == nullptr)) {
     return evaltime;
   }
@@ -1399,6 +1392,19 @@ float evaluate_time_fmodifiers(FModifiersStackStorage *storage,
     return evaltime;
   }
 
+  /**
+   * Disable all cycles modifiers that are not first in the stack.
+   */
+  LISTBASE_FOREACH (FModifier *, fcm, modifiers) {
+    const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm);
+    if (fmi == nullptr || fmi->type != FMODIFIER_TYPE_CYCLES) {
+      continue;
+    }
+    if (fcm->prev) {
+      fcm->flag |= FMODIFIER_FLAG_DISABLED;
+    }
+  }
+
   /* Starting from the end of the stack, calculate the time effects of various stacked modifiers
    * on the time the F-Curve should be evaluated at.
    *
```diff diff --git a/source/blender/blenkernel/intern/fmodifier.cc b/source/blender/blenkernel/intern/fmodifier.cc index 025c4851733..7e49d71edee 100644 --- a/source/blender/blenkernel/intern/fmodifier.cc +++ b/source/blender/blenkernel/intern/fmodifier.cc @@ -623,13 +623,6 @@ static float fcm_cycles_time( /* Initialize storage. */ storage->cycyofs = 0; - /* check if modifier is first in stack, otherwise disable ourself... */ - /* FIXME... */ - if (fcm->prev) { - fcm->flag |= FMODIFIER_FLAG_DISABLED; - return evaltime; - } - if (fcu == nullptr || (fcu->bezt == nullptr && fcu->fpt == nullptr)) { return evaltime; } @@ -1399,6 +1392,19 @@ float evaluate_time_fmodifiers(FModifiersStackStorage *storage, return evaltime; } + /** + * Disable all cycles modifiers that are not first in the stack. + */ + LISTBASE_FOREACH (FModifier *, fcm, modifiers) { + const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm); + if (fmi == nullptr || fmi->type != FMODIFIER_TYPE_CYCLES) { + continue; + } + if (fcm->prev) { + fcm->flag |= FMODIFIER_FLAG_DISABLED; + } + } + /* Starting from the end of the stack, calculate the time effects of various stacked modifiers * on the time the F-Curve should be evaluated at. * ```
Author
Member

Thanks! That may indeed be the case.

However, that doesn't solve the problem because evaluate_time_fmodifiers() also needs to take those parameters as const for this PR. So it might be a good refactor, but isn't what's needed for this PR.

Thanks! That may indeed be the case. However, that doesn't solve the problem because `evaluate_time_fmodifiers()` *also* needs to take those parameters as const for this PR. So it might be a good refactor, but isn't what's needed for this PR.
Member

Ah you're right indeed.
Looking at add_fmodifier, it seems that it's already not possible to add a FMODIFIER_TYPE_CYCLES after the first one. Maybe this just needs to be an assert instead? Is that code actually ever hit? 😅

Ah you're right indeed. Looking at `add_fmodifier`, it seems that it's already not possible to add a `FMODIFIER_TYPE_CYCLES` after the first one. Maybe this just needs to be an assert instead? Is that code actually ever hit? 😅

I agree with @filedescriptor here. I don't think you can have a cycles modifier at any other place than the first one. Changing that out with an assert is a valid option. And in the long run we can maybe remove that limitation altogether.

I agree with @filedescriptor here. I don't think you can have a cycles modifier at any other place than the first one. Changing that out with an assert is a valid option. And in the long run we can maybe remove that limitation altogether.
Member

Yes, also from what I can tell, all codepaths that add an fcurve modifier go through add_fmodifier. Even the Python API uses it (see rna_FCurve_modifiers_new).

Yes, also from what I can tell, all codepaths that add an fcurve modifier go through `add_fmodifier`. Even the Python API uses it (see `rna_FCurve_modifiers_new`).
nathanvegdahl marked this conversation as resolved
Nathan Vegdahl requested review from Sybren A. Stüvel 2024-05-14 15:43:40 +02:00
Nathan Vegdahl requested review from Christoph Lendenfeld 2024-05-14 15:43:49 +02:00
Nathan Vegdahl changed title from Make `evaluate_fcurve()` take its FCurve as const instead of mutable to WIP: Make `evaluate_fcurve()` take its FCurve as const instead of mutable 2024-05-14 15:51:11 +02:00
Author
Member

Context: this PR was inspired by a comment from @dr.sybren on #120428 (now #121661) about making new_key_needed() take a const FCurve reference.

Context: this PR was inspired by a comment from @dr.sybren on #120428 (now #121661) about making `new_key_needed()` take a const FCurve reference.
Nathan Vegdahl added 2 commits 2024-05-16 15:22:10 +02:00
Change cycle-modifier-is-first check into an assert
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
fe7539e304
Nathan Vegdahl changed title from WIP: Make `evaluate_fcurve()` take its FCurve as const instead of mutable to Make `evaluate_fcurve()` take its FCurve as const instead of mutable 2024-05-16 15:27:46 +02:00
Nathan Vegdahl requested review from Falk David 2024-05-16 15:28:04 +02:00
Author
Member

@ChrisLend @filedescriptor I turned that one mutable usage into an assert as suggested. I think this is ready to land, pending a final review.

@ChrisLend @filedescriptor I turned that one mutable usage into an assert as suggested. I think this is ready to land, pending a final review.
Author
Member

@blender-bot build

@blender-bot build
Christoph Lendenfeld approved these changes 2024-05-16 16:40:01 +02:00
Christoph Lendenfeld left a comment
Member

The title should probably be Refactor:... but else the PR looks good.
Thanks for that, much nicer!

The title should probably be `Refactor:...` but else the PR looks good. Thanks for that, much nicer!
Nathan Vegdahl changed title from Make `evaluate_fcurve()` take its FCurve as const instead of mutable to Refactor: make `evaluate_fcurve()` take its FCurve as const instead of mutable 2024-05-16 17:10:45 +02:00
Nathan Vegdahl merged commit 848bd505b4 into main 2024-05-16 17:13:53 +02:00
Nathan Vegdahl deleted branch const_fcurve_functions 2024-05-16 17:13:56 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
Asset Browser Project
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#121788
No description provided.