Browse Source

Fixed cross shader type conversions

flabbet 1 year ago
parent
commit
c777f8326e

+ 7 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/FuncInputProperty.cs

@@ -4,6 +4,7 @@ using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 using PixiEditor.ChangeableDocument.Changes.NodeGraph;
 using PixiEditor.ChangeableDocument.Changes.NodeGraph;
 using PixiEditor.DrawingApi.Core.Shaders;
 using PixiEditor.DrawingApi.Core.Shaders;
 using PixiEditor.DrawingApi.Core.Shaders.Generation;
 using PixiEditor.DrawingApi.Core.Shaders.Generation;
+using PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
 
 namespace PixiEditor.ChangeableDocument.Changeables.Graph;
 namespace PixiEditor.ChangeableDocument.Changeables.Graph;
 
 
@@ -45,7 +46,8 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
                 isShaderExpression = true;
                 isShaderExpression = true;
             }
             }
             
             
-            ConversionTable.TryConvert(delegateToCast.DynamicInvoke(f), targetType, out var result);
+            var sourceObj = delegateToCast.DynamicInvoke(f);
+            ConversionTable.TryConvert(sourceObj, targetType, out var result);
             if (isShaderExpression)
             if (isShaderExpression)
             {
             {
                 var toReturn = Activator.CreateInstance(typeof(T), "");
                 var toReturn = Activator.CreateInstance(typeof(T), "");
@@ -53,6 +55,10 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
                 {
                 {
                     ((ShaderExpressionVariable)toReturn).SetConstantValue(result, ConversionTable.Convert);
                     ((ShaderExpressionVariable)toReturn).SetConstantValue(result, ConversionTable.Convert);
                 }
                 }
+                else if (sourceObj is Expression expression)
+                {
+                    ((ShaderExpressionVariable)toReturn).OverrideExpression = expression;
+                }
 
 
                 return (T)toReturn;
                 return (T)toReturn;
             }
             }

+ 7 - 3
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Float1.cs

@@ -7,9 +7,13 @@
 /// <param name="constant">Constant value of the variable.</param>
 /// <param name="constant">Constant value of the variable.</param>
 public class Float1(string name) : ShaderExpressionVariable<double>(name)
 public class Float1(string name) : ShaderExpressionVariable<double>(name)
 {
 {
-    public override string ConstantValueString => ConstantValue.ToString(System.Globalization.CultureInfo.InvariantCulture);
-    
+    public override string ConstantValueString =>
+        ConstantValue.ToString(System.Globalization.CultureInfo.InvariantCulture);
+
+    public override Expression? OverrideExpression { get; set; }
+
     public static implicit operator Float1(double value) => new Float1("") { ConstantValue = value };
     public static implicit operator Float1(double value) => new Float1("") { ConstantValue = value };
-    
+
     public static explicit operator double(Float1 value) => value.ConstantValue;
     public static explicit operator double(Float1 value) => value.ConstantValue;
+    
 }
 }

+ 14 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Float2.cs

@@ -4,6 +4,7 @@ namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
 
 public class Float2(string name) : ShaderExpressionVariable<VecD>(name)
 public class Float2(string name) : ShaderExpressionVariable<VecD>(name)
 {
 {
+    private Expression? _overrideExpression;
     public override string ConstantValueString
     public override string ConstantValueString
     {
     {
         get
         get
@@ -14,11 +15,22 @@ public class Float2(string name) : ShaderExpressionVariable<VecD>(name)
         }
         }
     }
     }
 
 
+    public override Expression? OverrideExpression
+    {
+        get => _overrideExpression;
+        set
+        {
+            _overrideExpression = value;
+            X.OverrideExpression = value;
+            Y.OverrideExpression = value;
+        }
+    }
+
     public Float1 X
     public Float1 X
     {
     {
         get
         get
         {
         {
-            return new Float1($"{VariableName}.x") { ConstantValue = ConstantValue.X };
+            return new Float1($"{VariableName}.x") { ConstantValue = ConstantValue.X, OverrideExpression = _overrideExpression };
         }
         }
     }
     }
     
     
@@ -26,7 +38,7 @@ public class Float2(string name) : ShaderExpressionVariable<VecD>(name)
     {
     {
         get
         get
         {
         {
-            return new Float1($"{VariableName}.y") { ConstantValue = ConstantValue.Y };
+            return new Float1($"{VariableName}.y") { ConstantValue = ConstantValue.Y, OverrideExpression = _overrideExpression };
         }
         }
     }
     }
     
     

+ 14 - 4
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Half4.cs

@@ -4,13 +4,23 @@ namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
 
 public class Half4(string name) : ShaderExpressionVariable<Color>(name)
 public class Half4(string name) : ShaderExpressionVariable<Color>(name)
 {
 {
+    private Expression? _overrideExpression;
     public override string ConstantValueString => $"half4({ConstantValue.R}, {ConstantValue.G}, {ConstantValue.B}, {ConstantValue.A})";
     public override string ConstantValueString => $"half4({ConstantValue.R}, {ConstantValue.G}, {ConstantValue.B}, {ConstantValue.A})";
     
     
-    public Float1 R => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.r") { ConstantValue = ConstantValue.R };
-    public Float1 G => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.g") { ConstantValue = ConstantValue.G };
-    public Float1 B => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.b") { ConstantValue = ConstantValue.B };
-    public Float1 A => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.a") { ConstantValue = ConstantValue.A };
+    public Float1 R => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.r") { ConstantValue = ConstantValue.R, OverrideExpression = _overrideExpression};
+    public Float1 G => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.g") { ConstantValue = ConstantValue.G, OverrideExpression = _overrideExpression};
+    public Float1 B => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.b") { ConstantValue = ConstantValue.B, OverrideExpression = _overrideExpression};
+    public Float1 A => new Float1(string.IsNullOrEmpty(VariableName) ? string.Empty : $"{VariableName}.a") { ConstantValue = ConstantValue.A, OverrideExpression = _overrideExpression};
     
     
     public static implicit operator Half4(Color value) => new Half4("") { ConstantValue = value };
     public static implicit operator Half4(Color value) => new Half4("") { ConstantValue = value };
     public static explicit operator Color(Half4 value) => value.ConstantValue;
     public static explicit operator Color(Half4 value) => value.ConstantValue;
+
+    public override Expression? OverrideExpression
+    {
+        get => _overrideExpression;
+        set
+        {
+            _overrideExpression = value;
+        }
+    }
 }
 }

+ 3 - 1
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Int1.cs

@@ -3,7 +3,9 @@
 public class Int1(string name) : ShaderExpressionVariable<int>(name)
 public class Int1(string name) : ShaderExpressionVariable<int>(name)
 {
 {
     public override string ConstantValueString => ConstantValue.ToString(System.Globalization.CultureInfo.InvariantCulture);
     public override string ConstantValueString => ConstantValue.ToString(System.Globalization.CultureInfo.InvariantCulture);
-    
+
+    public override Expression? OverrideExpression { get; set; }
+
     public static implicit operator Int1(int value) => new Int1("") { ConstantValue = value };
     public static implicit operator Int1(int value) => new Int1("") { ConstantValue = value };
     public static explicit operator int(Int1 value) => value.ConstantValue;
     public static explicit operator int(Int1 value) => value.ConstantValue;
 }
 }

+ 14 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Int2.cs

@@ -4,18 +4,30 @@ namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
 
 public class Int2(string name) : ShaderExpressionVariable<VecI>(name)
 public class Int2(string name) : ShaderExpressionVariable<VecI>(name)
 {
 {
+    private Expression? _overrideExpression;
     public override string ConstantValueString => $"int2({ConstantValue.X}, {ConstantValue.Y})";
     public override string ConstantValueString => $"int2({ConstantValue.X}, {ConstantValue.Y})";
 
 
     public Int1 X
     public Int1 X
     {
     {
-        get => new Int1($"{VariableName}.x");
+        get => new Int1($"{VariableName}.x") { ConstantValue = ConstantValue.X, OverrideExpression = _overrideExpression };
     }
     }
 
 
     public Int1 Y
     public Int1 Y
     {
     {
-        get => new Int1($"{VariableName}.y");
+        get => new Int1($"{VariableName}.y") { ConstantValue = ConstantValue.Y, OverrideExpression = _overrideExpression };
     }
     }
 
 
     public static implicit operator Int2(VecI value) => new Int2("") { ConstantValue = value };
     public static implicit operator Int2(VecI value) => new Int2("") { ConstantValue = value };
     public static explicit operator VecI(Int2 value) => value.ConstantValue;
     public static explicit operator VecI(Int2 value) => value.ConstantValue;
+    
+    public override Expression? OverrideExpression
+    {
+        get => _overrideExpression;
+        set
+        {
+            _overrideExpression = value;
+            X.OverrideExpression = value;
+            Y.OverrideExpression = value;
+        }
+    }
 }
 }

+ 1 - 0
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/TextureSampler.cs

@@ -7,4 +7,5 @@ public class TextureSampler : ShaderExpressionVariable<Texture>
     }
     }
 
 
     public override string ConstantValueString { get; } = "";
     public override string ConstantValueString { get; } = "";
+    public override Expression? OverrideExpression { get; set; }
 }
 }

+ 2 - 1
src/PixiEditor.DrawingApi.Core/Shaders/Generation/ShaderBuilder.cs

@@ -70,7 +70,8 @@ public class ShaderBuilder
 
 
     public void ReturnVar(Half4 colorValue)
     public void ReturnVar(Half4 colorValue)
     {
     {
-        _bodyBuilder.AppendLine($"half4 premultiplied = half4({colorValue.VariableName}.r * {colorValue.VariableName}.a, {colorValue.VariableName}.g * {colorValue.VariableName}.a, {colorValue.VariableName}.b * {colorValue.VariableName}.a, {colorValue.VariableName}.a);");
+        string alphaExpression = colorValue.A.ExpressionValue;
+        _bodyBuilder.AppendLine($"half4 premultiplied = half4({colorValue.R.ExpressionValue} * {alphaExpression}, {colorValue.G.ExpressionValue} * {alphaExpression}, {colorValue.B.ExpressionValue} * {alphaExpression}, {alphaExpression});");
         _bodyBuilder.AppendLine($"return premultiplied;"); 
         _bodyBuilder.AppendLine($"return premultiplied;"); 
     }
     }
 
 

+ 2 - 1
src/PixiEditor.DrawingApi.Core/Shaders/Generation/ShaderExpressionVariable.cs

@@ -13,7 +13,8 @@ public abstract class ShaderExpressionVariable(string name) : Expression
         return VariableName;
         return VariableName;
     }
     }
 
 
-    public override string ExpressionValue => VarOrConst();
+    public override string ExpressionValue => OverrideExpression?.ExpressionValue ?? VarOrConst();
+    public abstract Expression? OverrideExpression { get; set; }
 
 
     public abstract void SetConstantValue(object? value, Func<object, Type, object> convertFunc);
     public abstract void SetConstantValue(object? value, Func<object, Type, object> convertFunc);