Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace UnityEditor.ShaderGraph
{
interface IGeneratesVariables
{
void GenerateNodeVariables(VariableRegistry registry, GenerationMode generationMode);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Internal;
using System;

namespace UnityEditor.ShaderGraph
{
[FormerName("UnityEngine.MaterialGraph.ViewDirectionNode")]
[Title("Input", "Geometry", "View Direction")]
class ViewDirectionNode : GeometryNode, IMayRequireViewDirection
class ViewDirectionNode : GeometryNode, IMayRequireViewDirection, IGeneratesFunction, IGeneratesVariables
{
private const int kOutputSlotId = 0;
public const string kOutputSlotName = "Out";
public override int latestVersion => 1;

public ViewDirectionNode()
{
Expand All @@ -31,12 +33,78 @@ public sealed override void UpdateNodeAfterDeserialization()

public override string GetVariableNameForSlot(int slotId)
{
return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.ViewDirection));
switch(m_SGVersion)
{
case 0:
return string.Format("IN.{0}", space.ToVariableName(InterpolatorType.ViewDirection));
case 1:
return GetVariableName();
default:
return null;
}
}

public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)
{
return space.ToNeededCoordinateSpace();
}

private readonly string functionName = "NormalizeIfURP";

public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
switch (sgVersion)
{
case 0:
break;
case 1:
default:
registry.ProvideFunction(functionName, s =>
{
s.AppendLine("$precision3 {0} ($precision3 In)", functionName);
using (s.BlockScope())
{
s.AppendLine("#ifdef UNIVERSAL_PIPELINE_CORE_INCLUDED");
s.AppendLine("\treturn normalize(In);");
s.AppendLine("#else");
s.AppendLine("\treturn In;");
s.AppendLine("#endif");
}
});
break;
}
}

public void GenerateNodeVariables(VariableRegistry registry, GenerationMode generationMode)
{
switch (sgVersion)
{
case 0:
break;
case 1:
default:
if (generationMode == GenerationMode.Preview)
{
registry.ProvideVariable(GetVariableName(), s => s.AppendLine("$precision3 {0} = normalize(IN.{1});", GetVariableName(), space.ToVariableName(InterpolatorType.ViewDirection)));
}
else
{
registry.ProvideVariable(GetVariableName(), s =>
{
s.AppendLine("$precision3 {0} = {1}(IN.{2});",
GetVariableName(),
functionName,
space.ToVariableName(InterpolatorType.ViewDirection)
);
});
}
break;
}
}

private string GetVariableName()
{
return "normalizedViewDirection";
}
}
}
60 changes: 60 additions & 0 deletions com.unity.shadergraph/Editor/Data/Util/VariableRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace UnityEditor.ShaderGraph
{
struct VariableSource
{
public string code;
public HashSet<AbstractMaterialNode> nodes;
}

class VariableRegistry
{
Dictionary<string, VariableSource> m_Sources = new Dictionary<string, VariableSource>();
bool m_Validate = false;
ShaderStringBuilder m_Builder;

public VariableRegistry(ShaderStringBuilder builder, bool validate = false)
{
m_Builder = builder;
m_Validate = validate;
}

internal ShaderStringBuilder builder => m_Builder;

public Dictionary<string, VariableSource> sources => m_Sources;

public List<string> names { get; } = new List<string>();

public void ProvideVariable(string name, Action<ShaderStringBuilder> generator)
{
VariableSource existingSource;
if (m_Sources.TryGetValue(name, out existingSource))
{
existingSource.nodes.Add(builder.currentNode);
if (m_Validate)
{
var startIndex = builder.length;
generator(builder);
var length = builder.length - startIndex;
var code = builder.ToString(startIndex, length);
builder.length -= length;
if (code != existingSource.code)
Debug.LogErrorFormat(@"Variable `{0}` has varying implementations:{1}{1}{2}{1}{1}{3}", name, Environment.NewLine, code, existingSource);
}
}
else
{
builder.AppendNewLine();
var startIndex = builder.length;
generator(builder);
var length = builder.length - startIndex;
var code = m_Validate ? builder.ToString(startIndex, length) : string.Empty;
m_Sources.Add(name, new VariableSource { code = code, nodes = new HashSet<AbstractMaterialNode> {builder.currentNode} });
names.Add(name);
}
}
}
}
11 changes: 11 additions & 0 deletions com.unity.shadergraph/Editor/Data/Util/VariableRegistry.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -796,15 +796,19 @@ internal static void GenerateSurfaceDescriptionFunction(
using (surfaceDescriptionFunction.BlockScope())
{
surfaceDescriptionFunction.AppendLine("{0} surface = ({0})0;", surfaceDescriptionName);
surfaceDescriptionFunction.AppendLine("$variables");
VariableRegistry variableRegistry = new VariableRegistry(new ShaderStringBuilder(1));
for(int i = 0; i < nodes.Count; i++)
{
GenerateDescriptionForNode(nodes[i], keywordPermutationsPerNode[i], functionRegistry, surfaceDescriptionFunction,
GenerateDescriptionForNode(nodes[i], keywordPermutationsPerNode[i], functionRegistry, variableRegistry, surfaceDescriptionFunction,
shaderProperties, shaderKeywords,
graph, mode);
}

variableRegistry.builder.currentNode = null;
functionRegistry.builder.currentNode = null;
surfaceDescriptionFunction.currentNode = null;
surfaceDescriptionFunction.ReplaceEverywhere("$variables", variableRegistry.builder.ToString());

GenerateSurfaceDescriptionRemap(graph, rootNode, slots,
surfaceDescriptionFunction, mode);
Expand All @@ -826,12 +830,27 @@ static void GenerateDescriptionForNode(
AbstractMaterialNode activeNode,
List<int> keywordPermutations,
FunctionRegistry functionRegistry,
VariableRegistry variableRegistry,
ShaderStringBuilder descriptionFunction,
PropertyCollector shaderProperties,
KeywordCollector shaderKeywords,
GraphData graph,
GenerationMode mode)
{

if(activeNode is IGeneratesVariables variablesNode)
{
if(keywordPermutations != null)
descriptionFunction.AppendLine(KeywordUtil.GetKeywordPermutationSetConditional(keywordPermutations));

variableRegistry.builder.currentNode = activeNode;
variablesNode.GenerateNodeVariables(variableRegistry, mode);
variableRegistry.builder.ReplaceInCurrentMapping(PrecisionUtil.Token, activeNode.concretePrecision.ToShaderString());

if(keywordPermutations != null)
descriptionFunction.AppendLine("#endif");
}

if (activeNode is IGeneratesFunction functionNode)
{
functionRegistry.builder.currentNode = activeNode;
Expand Down Expand Up @@ -963,15 +982,19 @@ internal static void GenerateVertexDescriptionFunction(
using (builder.BlockScope())
{
builder.AppendLine("{0} description = ({0})0;", graphOutputStructName);
builder.AppendLine("$variables");
VariableRegistry variableRegistry = new VariableRegistry(new ShaderStringBuilder(1));
for(int i = 0; i < nodes.Count; i++)
{
GenerateDescriptionForNode(nodes[i], keywordPermutationsPerNode[i], functionRegistry, builder,
GenerateDescriptionForNode(nodes[i], keywordPermutationsPerNode[i], functionRegistry, variableRegistry, builder,
shaderProperties, shaderKeywords,
graph, mode);
}

variableRegistry.builder.currentNode = null;
functionRegistry.builder.currentNode = null;
builder.currentNode = null;
builder.ReplaceEverywhere("$variables", variableRegistry.builder.ToString());

if(slots.Count != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ public void ReplaceInCurrentMapping(string oldValue, string newValue)
m_StringBuilder.Replace(oldValue, newValue, start, end );
}

public void ReplaceEverywhere(string oldValue, string newValue)
{
m_StringBuilder.Replace(oldValue, newValue);
}

public string ToCodeBlock()
{
// Remove new line
Expand Down