소스 검색

Some fixes

Krzysztof Krysiński 7 시간 전
부모
커밋
488978357d

+ 62 - 8
src/PixiEditor.ChangeableDocument/Changeables/Graph/Context/ShaderFuncContext.cs

@@ -11,6 +11,7 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Context;
 public class ShaderFuncContext : FuncContext
 {
     public static ShaderFuncContext NoContext { get; } = new();
+
     /// <summary>
     ///     Original position of the pixel in the image. This is the input argument of the main function.
     /// </summary>
@@ -40,10 +41,10 @@ public class ShaderFuncContext : FuncContext
 
     public ShaderFuncContext()
     {
-
     }
 
-     public Half4 SampleSurface(DrawingSurface surface, Expression pos, ColorSampleMode sampleMode, bool normalizedCoordinates)
+    public Half4 SampleSurface(DrawingSurface surface, Expression pos, ColorSampleMode sampleMode,
+        bool normalizedCoordinates)
     {
         SurfaceSampler texName = Builder.AddOrGetSurface(surface, sampleMode);
         return Builder.Sample(texName, pos, normalizedCoordinates);
@@ -199,8 +200,16 @@ public class ShaderFuncContext : FuncContext
             }
         }
 
+        Float1 val;
+        if (getFrom.Value == null)
+        {
+            val = new Float1("") { ConstantValue = 0.0 };
+        }
+        else
+        {
+            val = getFrom.Value(this);
+        }
 
-        var val = getFrom.Value(this);
         _cachedValues[getFrom] = val;
 
         return val;
@@ -226,7 +235,16 @@ public class ShaderFuncContext : FuncContext
             }
         }
 
-        var val = getFrom.Value(this);
+        Int1 val;
+        if (getFrom.Value == null)
+        {
+            val = new Int1("") { ConstantValue = 0 };
+        }
+        else
+        {
+            val = getFrom.Value(this);
+        }
+
         _cachedValues[getFrom] = val;
 
         return val;
@@ -258,7 +276,16 @@ public class ShaderFuncContext : FuncContext
             }
         }
 
-        var val = getFrom.Value(this);
+        Half4 val;
+        if (getFrom.Value == null)
+        {
+            val = new Half4("") { ConstantValue = Colors.Transparent };
+        }
+        else
+        {
+            val = getFrom.Value(this);
+        }
+
         _cachedValues[getFrom] = val;
 
         return val;
@@ -285,7 +312,16 @@ public class ShaderFuncContext : FuncContext
             }
         }
 
-        var val = getFrom.Value(this);
+        Float2 val;
+        if (getFrom.Value == null)
+        {
+            val = new Float2("") { ConstantValue = new VecD(0, 0) };
+        }
+        else
+        {
+            val = getFrom.Value(this);
+        }
+
         _cachedValues[getFrom] = val;
 
         return val;
@@ -312,7 +348,16 @@ public class ShaderFuncContext : FuncContext
             }
         }
 
-        var val = getFrom.Value(this);
+        Int2 val;
+        if (getFrom?.Value == null)
+        {
+            val = new Int2("") { ConstantValue = new VecI(0, 0) };
+        }
+        else
+        {
+            val = getFrom.Value(this);
+        }
+
         _cachedValues[getFrom] = val;
 
         return val;
@@ -339,7 +384,16 @@ public class ShaderFuncContext : FuncContext
             }
         }
 
-        var val = getFrom.Value(this);
+        Float3x3 val;
+        if (getFrom.Value == null)
+        {
+            val = new Float3x3("") { ConstantValue = Matrix3X3.Identity };
+        }
+        else
+        {
+            val = getFrom.Value(this);
+        }
+
         _cachedValues[getFrom] = val;
 
         return val;

+ 46 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Utility/RepeatNodeEnd.cs

@@ -1,4 +1,5 @@
 using Drawie.Backend.Core.Shaders;
+using Drawie.Backend.Core.Shaders.Generation;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Rendering;
@@ -34,7 +35,46 @@ public class RepeatNodeEnd : Node, IPairNode, IExecutionFlowNode
             }
         }
 
-        Output.Value = Input.Value;
+        if(startNode.Iterations.Value <= 0)
+        {
+            Output.Value = DefaultOfType(Input.Value);
+        }
+        else
+        {
+            if (Input.Value is Delegate del)
+            {
+                var result = del.DynamicInvoke(ShaderFuncContext.NoContext);
+                if (result is ShaderExpressionVariable expressionVariable)
+                {
+                    var constant = expressionVariable.GetConstant();
+                    Output.Value = constant;
+                }
+                else
+                {
+                    Output.Value = result;
+                }
+            }
+            else
+            {
+                Output.Value = Input.Value;
+            }
+        }
+    }
+
+    private object DefaultOfType(object? val)
+    {
+        if (val == null) return null;
+        var type = val.GetType();
+        if (type.IsValueType) return Activator.CreateInstance(type)!;
+        if (val is Delegate del)
+        {
+            var result = del.DynamicInvoke(ShaderFuncContext.NoContext);
+            if (result is ShaderExpressionVariable expressionVariable)
+            {
+                return DefaultOfType(expressionVariable.GetConstant());
+            }
+        }
+        return null;
     }
 
     public override Node CreateCopy()
@@ -52,11 +92,13 @@ public class RepeatNodeEnd : Node, IPairNode, IExecutionFlowNode
             {
                 nestingCount++;
             }
+
             if (node is RepeatNodeStart leftNode && nestingCount == 0)
             {
                 startNode = leftNode;
                 return false;
             }
+
             if (node is RepeatNodeStart)
             {
                 nestingCount--;
@@ -85,12 +127,15 @@ public class RepeatNodeEnd : Node, IPairNode, IExecutionFlowNode
             {
                 nestingCount++;
             }
+
             if (node is RepeatNodeEnd leftNode && nestingCount == 0)
             {
                 if (leftNode == this)
                 {
+                    handled.Add(node);
                     break;
                 }
+
                 nestingCount--;
             }
 

+ 3 - 6
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Utility/RepeatNodeStart.cs

@@ -1,4 +1,5 @@
-using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
+using Drawie.Backend.Core.Shaders.Generation;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Rendering;
 
@@ -60,12 +61,8 @@ public class RepeatNodeStart : Node, IPairNode
                 node.Execute(context);
             }
 
-            Output.Value = endNode.Output.Value;
-        }
 
-        if (iterations > 0)
-        {
-            Output.Value = Input.Value;
+            Output.Value = endNode.Output.Value;
         }
 
         iterationInProgress = false;