Files
UnrealEngineUWP/Engine/Plugins/Runtime/Metasound/Source/MetasoundFrontend/Private/MetasoundFrontendGraph.h
rob gay 4b67969932 - Fix for sticky MetaSound literals when more than one external node attempts to set literals for a vertex with the same ID
- Fix for not serializing MetaSound EdGraph (again...)
#rb aaron.mcleran
#jira UE-117041
#preflight 60b71e4f5719ca00015b604c

#ROBOMERGE-SOURCE: CL 16534985 in //UE5/Private-Frosty/...
#ROBOMERGE-BOT: STARSHIP (Private-Frosty -> Main) (v828-16531559)

[CL 16535029 by rob gay in ue5-main branch]
2021-06-02 11:30:20 -04:00

171 lines
6.7 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "MetasoundFrontend.h"
#include "MetasoundGraph.h"
#include "MetasoundNodeInterface.h"
namespace Metasound
{
/** FFrontendGraph is a utility graph for use in the frontend. It can own nodes
* that live within the graph and provides query interfaces for finding nodes
* by dependency ID or input/output index.
*/
class FFrontendGraph : public FGraph
{
public:
/** FFrontendGraph constructor.
*
* @parma InInstanceName - Name of this graph.
* @parma InInstanceID - ID of this graph.
*/
FFrontendGraph(const FString& InInstanceName, const FGuid& InInstanceID);
virtual ~FFrontendGraph() = default;
/** Add an input node to this graph.
*
* @param InNodeID - The NodeID related to the parent FMetasoundFrontendClass.
* @param InIndex - The positional index for the input.
* @param InVertexKey - The key for the graph input vertex.
* @param InNode - A shared pointer to an input node.
*/
void AddInputNode(FGuid InNodeID, int32 InIndex, const FVertexKey& InVertexKey, TSharedPtr<const INode> InNode);
/** Add an output node to this graph.
*
* @param InNodeID - The NodeID related to the parent FMetasoundFrontendClass.
* @param InIndex - The positional index for the output.
* @param InVertexKey - The key for the graph output vertex.
* @param InNode - A shared pointer to an output node.
*/
void AddOutputNode(FGuid InNodeID, int32 InIndex, const FVertexKey& InVertexKey, TSharedPtr<const INode> InNode);
/** Store a node on this graph.
*
* @param InNodeID - The NodeID related to the parent FMetasoundFrontendClass.
* @param InNode - A shared pointer to a node.
*/
void AddNode(FGuid InNodeID, TSharedPtr<const INode> InNode);
/** Retrieve node by node ID.
*
* @param InNodeID - The NodeID of the requested Node.
*
* @return Pointer to the Node if it is stored on this graph. nullptr otherwise.
*/
const INode* FindNode(FGuid InNodeID) const;
/** Retrieve node by input index.
*
* @param InIndex - The index of the requested input.
*
* @return Pointer to the Node if it is stored on this graph. nullptr otherwise.
*/
const INode* FindInputNode(int32 InIndex) const;
/** Retrieve node by output index.
*
* @param InIndex - The index of the requested output.
*
* @return Pointer to the Node if it is stored on this graph. nullptr otherwise.
*/
const INode* FindOutputNode(int32 InIndex) const;
/** Returns true if all edges, destinations and sources refer to
* nodes stored in this graph. */
bool OwnsAllReferencedNodes() const;
private:
void StoreNode(TSharedPtr<const INode> InNode);
TMap<int32, const INode*> InputNodes;
TMap<int32, const INode*> OutputNodes;
TMap<FGuid, const INode*> NodeMap;
TSet<const INode*> StoredNodes;
TArray<TSharedPtr<const INode>> NodeStorage;
};
/** FFrontendGraphBuilder builds a FFrontendGraph from a FMetasoundDoucment
* or FMetasoundFrontendClass.
*/
class FFrontendGraphBuilder
{
public:
/** Check that all dependencies are C++ class dependencies.
*
* @param InDocument - Document containing dependencies.
*
* @return True if all dependencies are C++ classes. False otherwise.
*/
static bool IsFlat(const FMetasoundFrontendDocument& InDocument);
static bool IsFlat(const FMetasoundFrontendGraphClass& InRoot, const TArray<FMetasoundFrontendClass>& InDependencies);
/* Metasound document should be in order to create this graph. */
static TUniquePtr<FFrontendGraph> CreateGraph(const FMetasoundFrontendDocument& InDocument);
/* Metasound document should be in order to create this graph. */
static TUniquePtr<FFrontendGraph> CreateGraph(const FMetasoundFrontendGraphClass& InGraphClass, const TArray<FMetasoundFrontendGraphClass>& InSubgraphs, const TArray<FMetasoundFrontendClass>& InDependencies);
private:
struct FDefaultVariableData
{
FGuid DestinationNodeID;
FGuid DestinationVertexID;
FVertexKey DestinationVertexKey;
FName TypeName;
FVariableNodeConstructorParams InitParams;
};
// Map of Input VertexID to variable data required to construct and connect default variable
using FNodeIDVertexID = TTuple<FGuid, FGuid>;
using FDependencyByIDMap = TMap<FGuid, const FMetasoundFrontendClass*>;
using FSharedNodeByIDMap = TMap<FGuid, TSharedPtr<const INode>>;
using FDefaultInputByIDMap = TMap<FNodeIDVertexID, FDefaultVariableData>;
// Context used throughout entire graph build process
// (for both a root and nested subgraphs)
struct FBuildContext
{
FDependencyByIDMap FrontendClasses;
FSharedNodeByIDMap Graphs;
};
// Transient context used for building a specific graph
struct FBuildGraphContext
{
TUniquePtr<FFrontendGraph> Graph;
const FMetasoundFrontendGraphClass& GraphClass;
FBuildContext& BuildContext;
FDefaultInputByIDMap DefaultInputs;
};
static TArray<FDefaultVariableData> GetInputDefaultVariableData(const FMetasoundFrontendNode& InNode, const FNodeInitData& InInitData);
static bool SortSubgraphDependencies(TArray<const FMetasoundFrontendGraphClass*>& Subgraphs);
static TUniquePtr<FFrontendGraph> CreateGraph(FBuildContext& InContext, const FMetasoundFrontendGraphClass& InSubgraph);
static const FMetasoundFrontendClassInput* FindClassInputForInputNode(const FMetasoundFrontendGraphClass& InOwningGraph, const FMetasoundFrontendNode& InInputNode, int32& OutClassInputIndex);
static const FMetasoundFrontendClassOutput* FindClassOutputForOutputNode(const FMetasoundFrontendGraphClass& InOwningGraph, const FMetasoundFrontendNode& InOutputNode, int32& OutClassOutputIndex);
static const FMetasoundFrontendLiteral* FindInputLiteralForInputNode(const FMetasoundFrontendNode& InInputNode, const FMetasoundFrontendClass& InInputNodeClass, const FMetasoundFrontendClassInput& InOwningGraphClassInput);
static TUniquePtr<INode> CreateInputNode(const FMetasoundFrontendNode& InNode, const FMetasoundFrontendClass& InClass, const FMetasoundFrontendClassInput& InOwningGraphClassInput);
static TUniquePtr<INode> CreateOutputNode(const FMetasoundFrontendNode& InNode, const FMetasoundFrontendClass& InClass, FBuildGraphContext& InGraphContext);
static TUniquePtr<INode> CreateExternalNode(const FMetasoundFrontendNode& InNode, const FMetasoundFrontendClass& InClass, FBuildGraphContext& InGraphContext);
// TODO: add errors here. Most will be a "PromptIfMissing"...
static void AddNodesToGraph(FBuildGraphContext& InGraphContext);
static void AddEdgesToGraph(FBuildGraphContext& InGraphContext);
static void AddDefaultInputVariables(FBuildGraphContext& InGraphContext);
};
}