Skip to content
Open
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
18 changes: 9 additions & 9 deletions game/addons/tools/Code/ShaderGraph/Compiler/GraphCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public NodeResult Result( NodeInput input )

var id = ShaderResult.InputResults.Count;
var varName = $"l_{id}";
var localResult = new NodeResult( funcResult.Components, varName );
var localResult = new NodeResult( funcResult.ResultType, varName );

ShaderResult.InputResults.Add( input, localResult );
ShaderResult.Results.Add( (localResult, funcResult) );
Expand Down Expand Up @@ -464,9 +464,9 @@ public NodeResult Result( NodeInput input )
return (resultA, resultB);

if ( resultA.Components < resultB.Components )
return (new( resultB.Components, resultA.Cast( resultB.Components ) ), resultB);
return (new( resultB.ResultType, resultA.Cast( resultB.Components ) ), resultB);

return (resultA, new( resultA.Components, resultB.Cast( resultA.Components ) ));
return (resultA, new( resultA.ResultType, resultB.Cast( resultA.Components ) ));
}

/// <summary>
Expand Down Expand Up @@ -561,12 +561,12 @@ public NodeResult ResultValue<T>( T value, string name = null, bool previewOverr

return value switch
{
float v => isNamed ? new NodeResult( 1, $"{name}" ) : new NodeResult( 1, $"{v}", true ),
Vector2 v => isNamed ? new NodeResult( 2, $"{name}" ) : new NodeResult( 2, $"float2( {v.x}, {v.y} )" ),
Vector3 v => isNamed ? new NodeResult( 3, $"{name}" ) : new NodeResult( 3, $"float3( {v.x}, {v.y}, {v.z} )" ),
Vector4 v => isNamed ? new NodeResult( 4, $"{name}" ) : new NodeResult( 4, $"float4( {v.x}, {v.y}, {v.z}, {v.w} )" ),
Color v => isNamed ? new NodeResult( 4, $"{name}" ) : new NodeResult( 4, $"float4( {v.r}, {v.g}, {v.b}, {v.a} )" ),
bool v => isNamed ? new NodeResult( 0, $"{name}" ) : new NodeResult( 0, $"{v}" ),
float v => isNamed ? new NodeResult( NodeResultType.Float, $"{name}" ) : new NodeResult( NodeResultType.Float, $"{v}", true ),
Vector2 v => isNamed ? new NodeResult( NodeResultType.Vector2, $"{name}" ) : new NodeResult( NodeResultType.Vector2, $"float2( {v.x}, {v.y} )" ),
Vector3 v => isNamed ? new NodeResult( NodeResultType.Vector3, $"{name}" ) : new NodeResult( NodeResultType.Vector3, $"float3( {v.x}, {v.y}, {v.z} )" ),
Vector4 v => isNamed ? new NodeResult( NodeResultType.Vector4, $"{name}" ) : new NodeResult( NodeResultType.Vector4, $"float4( {v.x}, {v.y}, {v.z}, {v.w} )" ),
Color v => isNamed ? new NodeResult( NodeResultType.Vector4, $"{name}" ) : new NodeResult( NodeResultType.Vector4, $"float4( {v.r}, {v.g}, {v.b}, {v.a} )" ),
bool v => isNamed ? new NodeResult( NodeResultType.Bool, $"{name}" ) : new NodeResult( NodeResultType.Bool, $"{v}" ),
_ => throw new ArgumentException( "Unsupported attribute type", nameof( value ) )
};
}
Expand Down
67 changes: 46 additions & 21 deletions game/addons/tools/Code/ShaderGraph/Compiler/NodeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,69 @@ public enum NodeResultType
Float,
Vector2,
Vector3,
Color
Vector4,
[Obsolete( "Use NodeResultType.Vector4 instead", false )]
Color,
Invalid,
}

public struct NodeResult : IValid
{
public delegate NodeResult Func( GraphCompiler compiler );

public NodeResultType ResultType { get; private set; } = NodeResultType.Invalid;
public string Code { get; private set; }
public int Components { get; private set; }
public bool Constant { get; set; }
public string[] Errors { get; private init; }
public readonly bool IsValid => ResultType != NodeResultType.Invalid && !string.IsNullOrWhiteSpace( Code );

public readonly bool IsValid => Components > 0 && !string.IsNullOrWhiteSpace( Code );
public readonly string TypeName => ResultType switch
{
NodeResultType.Bool => "bool",
NodeResultType.Float => "float",
NodeResultType.Vector2 => "float2",
NodeResultType.Vector3 => "float3",
NodeResultType.Vector4 => "float4",
_ => null
};

public readonly string TypeName => Components > 1 ? $"float{Components}" : Components == 0 ? "bool" : "float";
public int Components => ResultType switch
{
NodeResultType.Bool => 1,
NodeResultType.Float => 1,
NodeResultType.Vector2 => 2,
NodeResultType.Vector3 => 3,
NodeResultType.Vector4 => 4,
_ => 1
};

public readonly Type ComponentType => Components switch
public readonly Type ComponentType => ResultType switch
{
1 => typeof( float ),
2 => typeof( Vector2 ),
3 => typeof( Vector3 ),
4 => typeof( Color ),
NodeResultType.Float => typeof( float ),
NodeResultType.Vector2 => typeof( Vector2 ),
NodeResultType.Vector3 => typeof( Vector3 ),
NodeResultType.Vector4 => typeof( Vector4 ),
_ => null,
};

[Obsolete( "Use NodeResult( NodeResultType resultType, string code, bool constant = false ) instead", false )]
public NodeResult( int components, string code, bool constant = false )
{
Components = components;
ResultType = components switch
{
1 => NodeResultType.Float,
2 => NodeResultType.Vector2,
3 => NodeResultType.Vector3,
4 => NodeResultType.Vector4,
_ => NodeResultType.Invalid,
};

Code = code;
Constant = constant;
}

public NodeResult( NodeResultType type, string code, bool constant = false )
public NodeResult( NodeResultType resultType, string code, bool constant = false )
{
Components = type switch
{
NodeResultType.Bool => 1,
NodeResultType.Float => 1,
NodeResultType.Vector2 => 2,
NodeResultType.Vector3 => 3,
NodeResultType.Color => 4,
_ => 1
};
ResultType = resultType;
Code = code;
Constant = constant;
}
Expand All @@ -62,6 +82,11 @@ public NodeResult( NodeResultType type, string code, bool constant = false )
/// </summary>
public string Cast( int components, float defaultValue = 0.0f )
{
if ( ResultType == NodeResultType.Bool || ResultType == NodeResultType.Invalid )
{
throw new Exception( $"ResultType `{ResultType}` cannot be cast." );
}

if ( Components == components )
return Code;

Expand Down
2 changes: 1 addition & 1 deletion game/addons/tools/Code/ShaderGraph/ParameterNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected NodeResult Component( string component, float value, GraphCompiler com
return compiler.ResultValue( value );

var result = compiler.Result( new NodeInput { Identifier = Identifier, Output = nameof( Result ) } );
return new( 1, $"{result}.{component}", true );
return new( NodeResultType.Float, $"{result}.{component}", true );
}

public virtual object GetDefaultValue()
Expand Down
21 changes: 15 additions & 6 deletions game/editor/ShaderGraph/Code/Nodes/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void OnPaint( Rect rect )
public NodeResult.Func Result => ( GraphCompiler compiler ) =>
{
var results = compiler.Result( A, B, DefaultA, DefaultB );
return new NodeResult( results.Item1.Components, $"{results.Item1} {Op} {results.Item2}" );
return new NodeResult( results.Item1.ResultType, $"{results.Item1} {Op} {results.Item2}" );
};

[JsonIgnore, Hide, Browsable( false )]
Expand Down Expand Up @@ -148,7 +148,7 @@ public sealed class Lerp : ShaderNode
var results = compiler.Result( A, B, DefaultA, DefaultB );
var fraction = compiler.Result( C );
var fractionType = fraction.IsValid && fraction.Components > 1 ? Math.Max( results.Item1.Components, results.Item2.Components ) : 1;
return new NodeResult( results.Item1.Components, $"lerp( {results.Item1}, {results.Item2}," +
return new NodeResult( results.Item1.ResultType, $"lerp( {results.Item1}, {results.Item2}," +
$" {(fraction.IsValid ? fraction.Cast( fractionType ) : compiler.ResultValue( Fraction ))} )" );
};
}
Expand Down Expand Up @@ -194,7 +194,7 @@ public sealed class CrossProduct : ShaderNode
{
var a = compiler.ResultOrDefault( A, DefaultA ).Cast( 3 );
var b = compiler.ResultOrDefault( B, DefaultB ).Cast( 3 );
return new NodeResult( 3, $"cross( {a}, {b} )" );
return new NodeResult( NodeResultType.Vector3, $"cross( {a}, {b} )" );
};
}

Expand Down Expand Up @@ -328,7 +328,16 @@ public string GetResult( GraphCompiler compiler, int componentCount = 1 )
// Remap the normalized value to the output range
var remappedOutput = $"{normalizedInValue} * ( {outMaxValue} - {outMinValue} ) + {outMinValue}";

return new NodeResult( maxComponents, remappedOutput );
NodeResultType resultType = maxComponents switch
{
1 => NodeResultType.Float,
2 => NodeResultType.Vector2,
3 => NodeResultType.Vector3,
4 => NodeResultType.Vector4,
_ => NodeResultType.Invalid,
};

return new NodeResult( resultType, remappedOutput );
};
}

Expand Down Expand Up @@ -362,7 +371,7 @@ public Arctan2() : base()
public NodeResult.Func Result => ( GraphCompiler compiler ) =>
{
var results = compiler.Result( Y, X, DefaultY, DefaultX );
return new NodeResult( results.Item1.Components, $"atan2( {results.Item1}, {results.Item2} )" );
return new NodeResult( results.Item1.ResultType, $"atan2( {results.Item1}, {results.Item2} )" );
};
}

Expand Down Expand Up @@ -408,7 +417,7 @@ public override DisplayInfo DisplayInfo
public NodeResult.Func Result => ( GraphCompiler compiler ) =>
{
var results = compiler.Result( A, B, DefaultA, DefaultB );
return new NodeResult( results.Item1.Components, $"pow( {results.Item1}, {results.Item2} )" );
return new NodeResult( results.Item1.ResultType, $"pow( {results.Item1}, {results.Item2} )" );
};

public override void OnPaint( Rect rect )
Expand Down
2 changes: 1 addition & 1 deletion game/editor/ShaderGraph/Code/Nodes/Branch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private string Op
var resultA = useCondition ? compiler.ResultOrDefault( A, 0.0f ) : default;
var resultB = useCondition ? compiler.ResultOrDefault( B, 0.0f ) : default;

return new NodeResult( results.Item1.Components, $"{(useCondition ?
return new NodeResult( results.Item1.ResultType, $"{(useCondition ?
$"{resultA.Cast( 1 )} {Op} {resultB.Cast( 1 )}" : compiler.ResultParameter( Name, Enabled, default, default, false, IsAttribute, UI ))} ?" +
$" {results.Item1} :" +
$" {results.Item2}" );
Expand Down
12 changes: 6 additions & 6 deletions game/editor/ShaderGraph/Code/Nodes/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ public sealed class Camera : ShaderNode
{
[Output( typeof( Vector3 ) ), Title( "Position" )]
[Hide]
public static NodeResult.Func WorldPosition => ( GraphCompiler compiler ) => new( 3, "g_vCameraPositionWs" );
public static NodeResult.Func WorldPosition => ( GraphCompiler compiler ) => new( NodeResultType.Vector3, "g_vCameraPositionWs" );

[Output( typeof( Vector3 ) )]
[Hide]
public static NodeResult.Func Direction => ( GraphCompiler compiler ) => new( 3, "g_vCameraDirWs" );
public static NodeResult.Func Direction => ( GraphCompiler compiler ) => new( NodeResultType.Vector3, "g_vCameraDirWs" );

[Output( typeof( float ) )]
[Hide]
public static NodeResult.Func NearPlane => ( GraphCompiler compiler ) => new( 1, "g_flNearPlane" );
public static NodeResult.Func NearPlane => ( GraphCompiler compiler ) => new( NodeResultType.Float, "g_flNearPlane" );

[Output( typeof( float ) )]
[Hide]
public static NodeResult.Func FarPlane => ( GraphCompiler compiler ) => new( 1, "g_flFarPlane" );
public static NodeResult.Func FarPlane => ( GraphCompiler compiler ) => new( NodeResultType.Float, "g_flFarPlane" );
}

/// <summary>
Expand All @@ -39,7 +39,7 @@ public sealed class Depth : ShaderNode
var result = UV.IsValid() ? compiler.Result( UV ).Cast( 2 ) :
compiler.IsVs ? "i.vPositionPs.xy" : "i.vPositionSs.xy";

return new NodeResult( 1, $"Depth::Get( {result} )" );
return new NodeResult( NodeResultType.Float, $"Depth::Get( {result} )" );
};
}

Expand All @@ -58,6 +58,6 @@ public sealed class LinearDepth : ShaderNode
var result = UV.IsValid() ? compiler.Result( UV ).Cast( 2 ) :
compiler.IsVs ? "i.vPositionPs.xy" : "i.vPositionSs.xy";

return new NodeResult( 1, $"Depth::GetLinear( {result} )" );
return new NodeResult( NodeResultType.Float, $"Depth::GetLinear( {result} )" );
};
}
35 changes: 22 additions & 13 deletions game/editor/ShaderGraph/Code/Nodes/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ public sealed class SplitVector : ShaderNode
public NodeResult.Func X => ( GraphCompiler compiler ) =>
{
var result = compiler.Result( Input );
if ( result.IsValid && result.Components > 0 ) return new NodeResult( 1, $"{result}.x" );
return new NodeResult( 1, "0.0f" );
if ( result.IsValid && result.Components > 0 ) return new NodeResult( NodeResultType.Float, $"{result}.x" );
return new NodeResult( NodeResultType.Float, "0.0f" );
};

[Output( typeof( float ) ), Hide]
public NodeResult.Func Y => ( GraphCompiler compiler ) =>
{
var result = compiler.Result( Input );
if ( result.IsValid && result.Components > 1 ) return new NodeResult( 1, $"{result}.y" );
return new NodeResult( 1, "0.0f" );
if ( result.IsValid && result.Components > 1 ) return new NodeResult( NodeResultType.Float, $"{result}.y" );
return new NodeResult( NodeResultType.Float, "0.0f" );
};

[Output( typeof( float ) ), Hide]
public NodeResult.Func Z => ( GraphCompiler compiler ) =>
{
var result = compiler.Result( Input );
if ( result.IsValid && result.Components > 2 ) return new NodeResult( 1, $"{result}.z" );
return new NodeResult( 1, "0.0f" );
if ( result.IsValid && result.Components > 2 ) return new NodeResult( NodeResultType.Float, $"{result}.z" );
return new NodeResult( NodeResultType.Float, "0.0f" );
};

[Output( typeof( float ) ), Hide]
public NodeResult.Func W => ( GraphCompiler compiler ) =>
{
var result = compiler.Result( Input );
if ( result.IsValid && result.Components > 3 ) return new NodeResult( 1, $"{result}.w" );
return new NodeResult( 1, "0.0f" );
if ( result.IsValid && result.Components > 3 ) return new NodeResult( NodeResultType.Float, $"{result}.w" );
return new NodeResult( NodeResultType.Float, "0.0f" );
};
}

Expand Down Expand Up @@ -92,7 +92,7 @@ public sealed class CombineVector : ShaderNode
var z = compiler.ResultOrDefault( Z, DefaultZ ).Cast( 1 );
var w = compiler.ResultOrDefault( W, DefaultW ).Cast( 1 );

return new NodeResult( 4, $"float4( {x}, {y}, {z}, {w} )" );
return new NodeResult( NodeResultType.Vector4, $"float4( {x}, {y}, {z}, {w} )" );
};

[Output( typeof( Vector3 ) )]
Expand All @@ -103,7 +103,7 @@ public sealed class CombineVector : ShaderNode
var y = compiler.ResultOrDefault( Y, DefaultY ).Cast( 1 );
var z = compiler.ResultOrDefault( Z, DefaultZ ).Cast( 1 );

return new NodeResult( 3, $"float3( {x}, {y}, {z} )" );
return new NodeResult( NodeResultType.Vector3, $"float3( {x}, {y}, {z} )" );
};

[Output( typeof( Vector2 ) )]
Expand All @@ -113,7 +113,7 @@ public sealed class CombineVector : ShaderNode
var x = compiler.ResultOrDefault( X, DefaultX ).Cast( 1 );
var y = compiler.ResultOrDefault( Y, DefaultY ).Cast( 1 );

return new NodeResult( 2, $"float2( {x}, {y})" );
return new NodeResult( NodeResultType.Vector2, $"float2( {x}, {y})" );
};
}

Expand Down Expand Up @@ -155,7 +155,7 @@ private static char SwizzleToChannel( SwizzleChannel channel )
swizzle += SwizzleToChannel( BlueOut );
swizzle += SwizzleToChannel( AlphaOut );

return new NodeResult( 4, $"{input.Cast( 4 )}{swizzle}" );
return new NodeResult( NodeResultType.Vector4, $"{input.Cast( 4 )}{swizzle}" );
};
}

Expand All @@ -181,6 +181,15 @@ public sealed class AppendVector : ShaderNode
if ( components < 1 || components > 4 )
return NodeResult.Error( $"Can't append {resultB.TypeName} to {resultA.TypeName}" );

return new NodeResult( components, $"float{components}( {resultA}, {resultB} )" );
NodeResultType resultType = components switch
{
1 => NodeResultType.Float,
2 => NodeResultType.Vector2,
3 => NodeResultType.Vector3,
4 => NodeResultType.Vector4,
_ => NodeResultType.Invalid,
};

return new NodeResult( resultType, $"float{components}( {resultA}, {resultB} )" );
};
}
2 changes: 1 addition & 1 deletion game/editor/ShaderGraph/Code/Nodes/Effects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public sealed class Fresnel : ShaderNode
var direction = Direction.IsValid ? compiler.Result( Direction ).Cast( 3 ) :
$"CalculatePositionToCameraDirWs( {(compiler.IsVs ? "i.vPositionWs.xyz" : "i.vPositionWithOffsetWs.xyz + g_vHighPrecisionLightingOffsetWs.xyz")} )";

return new( 3, $"pow( 1.0 - dot( normalize( {normal} ), normalize( {direction} ) ), {power} )" );
return new( NodeResultType.Vector3, $"pow( 1.0 - dot( normalize( {normal} ), normalize( {direction} ) ), {power} )" );
};
}
Loading
Loading