Files
UnrealEngineUWP/Engine/Source/Editor/AnimGraph/Private/AnimBlueprintExtension_Attributes.cpp
Thomas Sarkanen 16eee0289d Anim node data/compiler refactor
Per-node constant data is now held on a generated struct as part of sparse class data.
Per-node mutable data (i.e. pin links/property access mappings) is now held on a generated 'mutable data' struct that is compiled as part of the generated class.

The anim BP compiler is now extended more conventionally using UAnimBlueprintExtension, derived from UBlueprintExtension. This directly replaces the older 'compiler handler' pattern that was added in an emergency fashion for 4.26. Anim graph nodes now request their required extensions and these are held on the UAnimBlueprint in the UBlueprint::Extensions array. The Extensions array is potentially refreshed with any node addition or removal. The Extensions array is force-refreshed each time an anim BP is compiled for the first time to deal with newly added or removed requirements.

Const-corrected a bunch of UAnimInstance/FAnimInstanceProxy APIs that rely on (now truly) const data.
Added a split state/constant version of FInputScaleBiasClamp to allow some of its data to be split into constants.
Tweaked alignment/ordering of FPoseLinkBase to save a few bytes per pose link.
Deprecated FAnimNode_Base::OverrideAsset in favor of a more UAnimGraphNode_Base-based approach. Individual nodes can still have runtime overrides via specific accessors. The new approach will also give us the oppurtunity to override multiple assets per node if required in the future.

Moved property access into Engine module & removed event support from it - this was never used.
Reworked property access compilation API a little - construction/lifetime was a bit confusing previously.

Optimized path used to create UK2Node_StructMemberSet nodes in per-node custom events. When using mutable data, the structure used is large and very sparsely connected (i.e. only a few properties are written) so we only create pins that are actually going to be used, rather than creating all of them and conly connecting a few.

Patched the following nodes to use the new data approach:

- Asset players (sequences, blendspaces, aim offsets)
- Blend lists
- Ref poses
- Roots

#rb Jurre.deBaare, Martin.Wilson, Keith.Yerex

[CL 16090510 by Thomas Sarkanen in ue5-main branch]
2021-04-22 04:57:09 -04:00

256 lines
10 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "AnimBlueprintExtension_Attributes.h"
#include "IAnimBlueprintCompilerCreationContext.h"
#include "IAnimBlueprintCompilationContext.h"
#include "AnimationGraphSchema.h"
#include "AnimGraphNode_Base.h"
#include "AnimGraphNode_SaveCachedPose.h"
#include "AnimGraphNode_UseCachedPose.h"
#include "AnimGraphNode_StateResult.h"
#include "AnimationStateGraph.h"
#include "AnimationStateMachineGraph.h"
#include "AnimGraphNode_StateMachineBase.h"
#include "AnimStateNode.h"
#include "AnimGraphNode_Root.h"
#include "AnimGraphAttributes.h"
#include "AnimGraphNode_TransitionResult.h"
#include "AnimGraphNode_CustomTransitionResult.h"
#include "AnimationCustomTransitionGraph.h"
#include "AnimStateTransitionNode.h"
#include "AnimGraphNode_BlendSpaceSampleResult.h"
#include "BlendSpaceGraph.h"
#include "AnimGraphNode_BlendSpaceGraphBase.h"
#include "AnimationBlendSpaceSampleGraph.h"
void UAnimBlueprintExtension_Attributes::HandlePostProcessAnimationNodes(TArrayView<UAnimGraphNode_Base*> InAnimNodes, IAnimBlueprintCompilationContext& InCompilationContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
using FNodeAttributeMap = TMap<UAnimGraphNode_Base*, UAnimGraphNode_Base::FNodeAttributeArray>;
using FCachedPoseMap = TMultiMap<UAnimGraphNode_SaveCachedPose*, UAnimGraphNode_UseCachedPose*>;
FNodeAttributeMap AttributeInputNodes;
FNodeAttributeMap AttributeOutputNodes;
FCachedPoseMap SaveCachedPoseMap;
UAnimGraphNode_Base::FNodeAttributeArray Attributes;
for(UAnimGraphNode_Base* Node : InAnimNodes)
{
// Establish links between save/used cached pose nodes
if(UAnimGraphNode_UseCachedPose* UseCachedPoseNode = Cast<UAnimGraphNode_UseCachedPose>(Node))
{
if(UseCachedPoseNode->SaveCachedPoseNode.IsValid())
{
SaveCachedPoseMap.Add(UseCachedPoseNode->SaveCachedPoseNode.Get(), UseCachedPoseNode);
}
}
// Get I/O attributes from all nodes
Node->GetInputLinkAttributes(Attributes);
if(Attributes.Num() > 0)
{
AttributeInputNodes.Add(Node, Attributes);
}
Attributes.Reset();
Node->GetOutputLinkAttributes(Attributes);
if(Attributes.Num() > 0)
{
AttributeOutputNodes.Add(Node, Attributes);
}
Attributes.Reset();
}
// Utility struct to check whether nodes with outputs reach nodes with inputs further towards the root
struct FCheckOutputNodes
{
private:
IAnimBlueprintCompilationContext& CompilationContext;
const UAnimGraphNode_Base::FNodeAttributeArray& BaseAttributes;
const FNodeAttributeMap& AttributeInputNodes;
const FCachedPoseMap& SaveCachedPoseMap;
UAnimGraphNode_Base* OutputNode;
TSet<UEdGraphNode*> VisitedNodes;
public:
FCheckOutputNodes(IAnimBlueprintCompilationContext& InCompilationContext, const UAnimGraphNode_Base::FNodeAttributeArray& InAttributes, const FNodeAttributeMap& InAttributeInputNodes, const FCachedPoseMap& InSaveCachedPoseMap, UAnimGraphNode_Base* InNode)
: CompilationContext(InCompilationContext)
, BaseAttributes(InAttributes)
, AttributeInputNodes(InAttributeInputNodes)
, SaveCachedPoseMap(InSaveCachedPoseMap)
, OutputNode(InNode)
{
UAnimGraphNode_Base::FNodeAttributeArray AbsorbedAttributes = TraverseNodes_Recursive(OutputNode, BaseAttributes);
if(AbsorbedAttributes.Num() > 0)
{
// Push only absorbed attributes to the source node's internal set
CompilationContext.AddAttributesToNode(OutputNode, AbsorbedAttributes);
}
}
private:
UAnimGraphNode_Base::FNodeAttributeArray TraverseNodes_Recursive_PerNode(UAnimGraphNode_Base* InLinkedNode, const UAnimGraphNode_Base::FNodeAttributeArray& InAttributes)
{
UAnimGraphNode_Base::FNodeAttributeArray AbsorbedAttributes;
if (InLinkedNode)
{
if(!VisitedNodes.Contains(InLinkedNode))
{
// See if this node absorbs this attribute
const UAnimGraphNode_Base::FNodeAttributeArray* AbsorbedAttributesPtr = AttributeInputNodes.Find(InLinkedNode);
auto HasAttribute = [AbsorbedAttributesPtr, InLinkedNode](FName InAttribute)
{
return (AbsorbedAttributesPtr && AbsorbedAttributesPtr->Contains(InAttribute));
};
if(InLinkedNode->IsA<UAnimGraphNode_Root>() || InLinkedNode->IsA<UAnimGraphNode_TransitionResult>() || InAttributes.ContainsByPredicate(HasAttribute))
{
UAnimGraphNode_Base::FNodeAttributeArray ReducedAttributes;
if(InLinkedNode->IsA<UAnimGraphNode_Root>() || InLinkedNode->IsA<UAnimGraphNode_TransitionResult>())
{
const UAnimGraphAttributes* AnimGraphAttributes = GetDefault<UAnimGraphAttributes>();
auto IsAttributeBlendable = [AnimGraphAttributes](const FName& InAttribute)
{
const FAnimGraphAttributeDesc* Desc = AnimGraphAttributes->FindAttributeDesc(InAttribute);
return Desc && Desc->Blend == EAnimGraphAttributeBlend::Blendable;
};
// Absorb all blendables at the root
for(const FName& Attribute : InAttributes)
{
if(IsAttributeBlendable(Attribute))
{
AbsorbedAttributes.Add(Attribute);
}
else
{
ReducedAttributes.Add(Attribute);
}
}
}
else
{
// Reduce the set of attributes that we are using in this traversal
for(const FName& Attribute : InAttributes)
{
if(HasAttribute(Attribute))
{
AbsorbedAttributes.Add(Attribute);
}
else
{
ReducedAttributes.Add(Attribute);
}
}
}
if(ReducedAttributes.Num() > 0)
{
UAnimGraphNode_Base::FNodeAttributeArray AbsorbedAttributesBelow = TraverseNodes_Recursive(InLinkedNode, ReducedAttributes);
AbsorbedAttributes.Append(AbsorbedAttributesBelow);
}
}
else
{
UAnimGraphNode_Base::FNodeAttributeArray AbsorbedAttributesBelow = TraverseNodes_Recursive(InLinkedNode, InAttributes);
AbsorbedAttributes.Append(AbsorbedAttributesBelow);
}
}
else
{
// already visited by another branch of a cached pose, see if any attributes got absorbed the last time we took this branch
TArrayView<const FName> PreviouslyAbsorbedAttributes = CompilationContext.GetAttributesFromNode(InLinkedNode);
AbsorbedAttributes.Append(PreviouslyAbsorbedAttributes.GetData(), PreviouslyAbsorbedAttributes.Num());
}
}
// Post-recursion, we add any pass through attributes that got absorbed
CompilationContext.AddAttributesToNode(InLinkedNode, AbsorbedAttributes);
return AbsorbedAttributes;
}
UAnimGraphNode_Base::FNodeAttributeArray TraverseNodes_Recursive(UEdGraphNode* InNode, const UAnimGraphNode_Base::FNodeAttributeArray& InAttributes)
{
VisitedNodes.Add(InNode);
for (UEdGraphPin* Pin : InNode->Pins)
{
if (UAnimationGraphSchema::IsPosePin(Pin->PinType) && Pin->Direction == EGPD_Output)
{
// Traverse pins
for (UEdGraphPin* LinkedPin : Pin->LinkedTo)
{
UEdGraphNode* OwningNode = LinkedPin->GetOwningNode();
if(UAnimGraphNode_Base* LinkedNode = Cast<UAnimGraphNode_Base>(OwningNode))
{
return TraverseNodes_Recursive_PerNode(LinkedNode, InAttributes);
}
else if(OwningNode != nullptr)
{
// Its a pose link, but not on an anim node, likely a knot
return TraverseNodes_Recursive(OwningNode, InAttributes);
}
}
}
}
// Traverse saved cached pose->all use cached pose nodes
if(UAnimGraphNode_SaveCachedPose* SaveCachedPoseNode = Cast<UAnimGraphNode_SaveCachedPose>(InNode))
{
TArray<UAnimGraphNode_UseCachedPose*> UseCachedPoseNodes;
SaveCachedPoseMap.MultiFind(SaveCachedPoseNode, UseCachedPoseNodes);
for(int32 UseCachedPoseIndex = 0; UseCachedPoseIndex < UseCachedPoseNodes.Num(); ++UseCachedPoseIndex)
{
UAnimGraphNode_UseCachedPose* UsedCachedPoseNode = UseCachedPoseNodes[UseCachedPoseIndex];
UAnimGraphNode_Base::FNodeAttributeArray AbsorbedAttributesBelow = TraverseNodes_Recursive_PerNode(UsedCachedPoseNode, InAttributes);
if(UseCachedPoseIndex == UseCachedPoseNodes.Num() - 1)
{
return AbsorbedAttributesBelow;
}
}
}
// Traverse out of custom transitions
else if(UAnimGraphNode_CustomTransitionResult* TransitionResultNode = Cast<UAnimGraphNode_CustomTransitionResult>(InNode))
{
UAnimationCustomTransitionGraph* TransitionGraph = CastChecked<UAnimationCustomTransitionGraph>(TransitionResultNode->GetGraph());
UAnimStateTransitionNode* TransitionNode = CastChecked<UAnimStateTransitionNode>(TransitionGraph->GetOuter());
UAnimationStateMachineGraph* StateMachineGraph = CastChecked<UAnimationStateMachineGraph>(TransitionNode->GetOuter());
UAnimGraphNode_StateMachineBase* StateMachineNode = CastChecked<UAnimGraphNode_StateMachineBase>(StateMachineGraph->GetOuter());
return TraverseNodes_Recursive_PerNode(StateMachineNode, InAttributes);
}
// Traverse out of state machines
else if(UAnimGraphNode_StateResult* StateResultNode = Cast<UAnimGraphNode_StateResult>(InNode))
{
UAnimationStateGraph* StateGraph = CastChecked<UAnimationStateGraph>(StateResultNode->GetGraph());
UAnimStateNode* StateNode = CastChecked<UAnimStateNode>(StateGraph->GetOuter());
UAnimationStateMachineGraph* StateMachineGraph = CastChecked<UAnimationStateMachineGraph>(StateNode->GetOuter());
UAnimGraphNode_StateMachineBase* StateMachineNode = CastChecked<UAnimGraphNode_StateMachineBase>(StateMachineGraph->GetOuter());
return TraverseNodes_Recursive_PerNode(StateMachineNode, InAttributes);
}
else if(UAnimGraphNode_BlendSpaceSampleResult* SampleResultNode = Cast<UAnimGraphNode_BlendSpaceSampleResult>(InNode))
{
UAnimationBlendSpaceSampleGraph* SampleGraph = CastChecked<UAnimationBlendSpaceSampleGraph>(SampleResultNode->GetGraph());
UBlendSpaceGraph* BlendSpaceGraph = CastChecked<UBlendSpaceGraph>(SampleGraph->GetOuter());
UAnimGraphNode_BlendSpaceGraphBase* BlendSpaceGraphNode = CastChecked<UAnimGraphNode_BlendSpaceGraphBase>(BlendSpaceGraph->GetOuter());
return TraverseNodes_Recursive_PerNode(BlendSpaceGraphNode, InAttributes);
}
return UAnimGraphNode_Base::FNodeAttributeArray();
}
};
// Color connected nodes root-wise from output nodes
for(auto& NodeAttributesPair : AttributeOutputNodes)
{
FCheckOutputNodes Checker(InCompilationContext, NodeAttributesPair.Value, AttributeInputNodes, SaveCachedPoseMap, NodeAttributesPair.Key);
}
}