Skip to content

Commit

Permalink
change how port dimensions are evaluated
Browse files Browse the repository at this point in the history
  • Loading branch information
z3y committed Nov 2, 2024
1 parent 5c0fc62 commit 44f5b47
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 189 deletions.
75 changes: 20 additions & 55 deletions Editor/Generation/ShaderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,9 @@ public void Build(ShaderNode shaderNode)
const string VertexPreview = "Packages/com.z3y.graphlit/Editor/Targets/Preview/Vertex.hlsl";
const string FragmentPreview = "Packages/com.z3y.graphlit/Editor/Targets/Preview/Fragment.hlsl";

public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode node, List<ShaderNode> rightNodes, bool log = false)
public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode node, List<ShaderNode> rightNodes, bool log = false, bool allNodes = false)
{
var affectedNodes = (new List<ShaderNode> { node }).Union(rightNodes);

bool anyPreview = false;
foreach (var x in affectedNodes)
{
x.EvaluateDimensionsForGraphView();
if (x._previewDisabled || x.DisablePreview)
{
continue;
}
anyPreview = true;
}
if (!anyPreview)
{
return;
}
var affectedNodes = (new List<ShaderNode> { node }).Union(rightNodes).ToList();

var shaderBuilder = new ShaderBuilder(GenerationMode.Preview, graphView)
{
Expand Down Expand Up @@ -226,7 +211,6 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode
shaderBuilder.TraverseGraph(node, fragmentVisitor);
node.BuilderVisit(fragmentVisitor);
shaderBuilder.visitedNodes.Add(node.viewDataKey);
node.EvaluateDimensionsForGraphView();

foreach (var rightNode in rightNodes)
{
Expand All @@ -236,8 +220,19 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode
shaderBuilder.visitedNodes.Add(rightNode.viewDataKey);
rightNode.BuilderVisit(fragmentVisitor);
}
}

rightNode.EvaluateDimensionsForGraphView();
if (allNodes)
{
var nodes = graphView.GetElementsGuidDictionary<ShaderNode>();
foreach (var guid in shaderBuilder.visitedNodes)
{
var n = nodes[guid];
if (!affectedNodes.Contains(n))
{
affectedNodes.Add(n);
}
}
}


Expand Down Expand Up @@ -285,6 +280,11 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode
sb.Add("}");


if (previewId <= 0)
{
return;
}

string result = shaderBuilder.ToString();

//Debug.Log(result);
Expand All @@ -304,26 +304,6 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode
}
}

static void EvaluateDimensionsRecursive(HashSet<string> visitedNodes, ShaderNode node)
{
if (visitedNodes.Contains(node.viewDataKey))
{
return;
}

foreach (var input in node.Inputs)
{
foreach (var edge in input.connections)
{
EvaluateDimensionsRecursive(visitedNodes, (ShaderNode)edge.output.node);
break;
}
}

node.EvaluateDimensionsForGraphView();
visitedNodes.Add(node.viewDataKey);
}

public static void GenerateAllPreviews(ShaderGraphView graphView)
{
graphView.UpdateCachedNodesForBuilder();
Expand All @@ -346,24 +326,9 @@ public static void GenerateAllPreviews(ShaderGraphView graphView)
}
}

var visitedNodes = new HashSet<string>();
foreach (var node in endNodes)
{
EvaluateDimensionsRecursive(visitedNodes, node);
}

var generatedNodes = new HashSet<string>();

foreach (var node in nodes)
{
if (!generatedNodes.Contains(node.viewDataKey))
{
if (!(node._previewDisabled || node.DisablePreview))
{
generatedNodes.UnionWith(node.GeneratePreviewForAffectedNodes());
}
generatedNodes.Add(node.viewDataKey);
}
GenerateUnifiedPreview(graphView, node, new List<ShaderNode>(), false, true);
}
}

Expand Down
6 changes: 3 additions & 3 deletions Editor/ShaderGraphView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public ShaderGraphView(ShaderGraphWindow editorWindow, string assetPath)
RegisterCallback<DragUpdatedEvent>(OnDragUpdated);
RegisterCallback<DragPerformEvent>(OnDragPerform);

if (!string.IsNullOrEmpty(assetPath))
/*if (!string.IsNullOrEmpty(assetPath))
{
var assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath);
//i//mportedMaterial = assets.OfType<Material>().FirstOrDefault();
//Debug.Log(importedMaterial);
}
}*/
}

private GraphViewChange OnGraphViewChanged(GraphViewChange change)
Expand Down Expand Up @@ -532,7 +532,7 @@ void OnDragPerform(DragPerformEvent evt)
evt.StopPropagation();
}

public Dictionary<string, T> GetElementsGuidDictionary<T>() where T : GraphElement, new()
public Dictionary<string, T> GetElementsGuidDictionary<T>() where T : GraphElement
{
var acceleratedGetNode = new Dictionary<string, T>();
foreach (var item in graphElements.OfType<T>())
Expand Down
143 changes: 12 additions & 131 deletions Editor/ShaderNode/ShaderNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,117 +354,6 @@ internal set
public virtual Precision DefaultPrecisionOverride => Precision.Inherit;
protected internal bool _inheritedPrecision;

public Dictionary<int, int> evaluatedOutputDimensions = new Dictionary<int, int>();
public void EvaluateDimensionsForGraphView()
{
var inputDimensions = new Dictionary<int, int>();

foreach (var port in Inputs)
{
int id = port.GetPortID();
if (!port.connected)
{
if (portDescriptors[id].Type is Float @float)
{
inputDimensions[id] = @float.dimensions;
SetPortColor(port, Float.GetPortColor(@float.dimensions));
}
continue;
}

foreach (var edge in port.connections)
{
var node = (ShaderNode)edge.output.node;
if (node is null)
{
continue;
}

if (node.evaluatedOutputDimensions.TryGetValue(edge.output.GetPortID(), out int value))
{
inputDimensions[id] = value;
//SetPortColor(port, Float.GetPortColor(value));
}
break;
}
}


int trunc = 4;
int max = 1;

foreach (var dimension in inputDimensions.Values)
{
if (dimension == 1)
{
continue;
}
max = Mathf.Max(max, dimension);
trunc = Mathf.Min(trunc, dimension);
}
trunc = Mathf.Min(trunc, max);
//Debug.Log(trunc);

foreach (var desc in portDescriptors.Values)
{
if (desc.Direction == PortDirection.Input)
{
if (desc.Type is Float @float)
{
if (@float.dynamic)
{
var port = Inputs.First(x => x.GetPortID() == desc.ID);
SetPortColor(port, Float.GetPortColor(trunc));
}
}
}
else
{

if (desc.Type is Float @float)
{
if (!@float.dynamic)
{
evaluatedOutputDimensions[desc.ID] = @float.dimensions;
}
else
{
evaluatedOutputDimensions[desc.ID] = trunc;
SetPortColor(Outputs.First(x => x.GetPortID() == desc.ID), Float.GetPortColor(trunc));
}
}
}
}


if (this is SwizzleNode swizzle)
{
evaluatedOutputDimensions[1] = Mathf.Clamp(swizzle.swizzle.Length, 1, 4);
SetPortColor(Outputs.First(), Float.GetPortColor(evaluatedOutputDimensions[1]));
return;
}

if (this is FetchVariableNode fv)
{
var registers = GraphView.cachedRegisterVariablesForBuilder.Where(x => x._name == fv._name);
foreach (var regs in registers)
{
regs.evaluatedOutputDimensions[1] = trunc;
SetPortColor(regs.inputContainer.Children().OfType<Port>().First(), Float.GetPortColor(trunc));
}
}
if (this is RegisterVariableNode reg)
{
var fetches = GraphView.cachedNodesForBuilder.OfType<FetchVariableNode>().Where(x => x._name == reg._name);
//Debug.Log(trunc);
foreach (var fetch in fetches)
{
fetch.evaluatedOutputDimensions[1] = trunc;
SetPortColor(fetch.outputContainer.Children().OfType<Port>().First(), Float.GetPortColor(trunc));
}
}
}

public void InheritPreviewAndPrecision()
{
int is3D = 0;
Expand Down Expand Up @@ -666,24 +555,6 @@ GeneratedPortData GetDefaultInput(int portID, NodeVisitor visitor)
var descriptor = portDescriptors[portID];
string value = SetDefaultBinding(descriptor, visitor);

//var autoWireNodes = GraphView.graphElements.OfType<RegisterVariableNode>().Where(x => x._autoWire).ToArray();

/*foreach (var node in autoWireNodes)
{
if (node._name.ToLower() == descriptor.Name.ToLower())
{
var input = node.Inputs.First();
if (!input.connected)
{
break;
}
var incomingPort = input.connections.First().output;
var incomingNode = (ShaderNode)incomingPort.node;
value = incomingNode.PortData[incomingPort.GetPortID()].Name;
break;
}
}*/

UpdateDefaultValueTooltip(portID, value);

return new GeneratedPortData(descriptor.Type, value);
Expand Down Expand Up @@ -817,6 +688,7 @@ internal void BuilderVisit(NodeVisitor visitor, int[] portsMask = null)
}

Generate(visitor);
UpdateGraphViewDimensions();
}
string DisplayName => Info.name.Split("/")[^1];
public string UniqueVariable => DisplayName.Replace(" ", "") + UniqueVariableID;
Expand Down Expand Up @@ -943,7 +815,7 @@ public GeneratedPortData Cast(int portID, int targetDimensions, bool updatePort
return newData;
}

/*public void UpdateGraphView()
public void UpdateGraphViewDimensions()
{
foreach (var data in PortData)
{
Expand All @@ -961,7 +833,16 @@ public GeneratedPortData Cast(int portID, int targetDimensions, bool updatePort
SetPortColor(port, color);
}
}
}*/

if (this is FetchVariableNode fetch)
{
var reg = GraphView.cachedRegisterVariablesForBuilder.FirstOrDefault(x => x._name == fetch._name);
if (reg is not null)
{
SetPortColor(reg.Inputs.First(), Outputs.First().portColor);
}
}
}

public static void SetPortColor(Port port, Color color)
{
Expand Down

0 comments on commit 44f5b47

Please sign in to comment.