Browse Source

Added shader access optimization

flabbet 8 months ago
parent
commit
a1f8aa8ac6

+ 59 - 52
src/PixiEditor.ChangeableDocument/Changeables/Graph/Context/FuncContext.cs

@@ -20,17 +20,18 @@ public class FuncContext
     ///     Original position of the pixel in the image. This is the input argument of the main function.
     ///     Original position of the pixel in the image. This is the input argument of the main function.
     /// </summary>
     /// </summary>
     public Float2 OriginalPosition { get; private set; }
     public Float2 OriginalPosition { get; private set; }
-    
+
     /// <summary>
     /// <summary>
     ///     Modified position of the pixel. This should be used to sample the texture, unless you want to sample the texture at the original position only.
     ///     Modified position of the pixel. This should be used to sample the texture, unless you want to sample the texture at the original position only.
     /// </summary>
     /// </summary>
     public Float2 SamplePosition { get; private set; }
     public Float2 SamplePosition { get; private set; }
+
     public VecI Size { get; private set; }
     public VecI Size { get; private set; }
     public bool HasContext { get; private set; }
     public bool HasContext { get; private set; }
     public RenderContext RenderContext { get; set; }
     public RenderContext RenderContext { get; set; }
 
 
     public ShaderBuilder Builder { get; set; }
     public ShaderBuilder Builder { get; set; }
-    
+
     private Dictionary<IFuncInputProperty, ShaderExpressionVariable> _cachedValues = new();
     private Dictionary<IFuncInputProperty, ShaderExpressionVariable> _cachedValues = new();
 
 
     public void ThrowOnMissingContext()
     public void ThrowOnMissingContext()
@@ -51,7 +52,7 @@ public class FuncContext
         Builder = builder;
         Builder = builder;
         HasContext = true;
         HasContext = true;
         OriginalPosition = new Float2("coords"); // input argument 'half4 main(float2 coords)'
         OriginalPosition = new Float2("coords"); // input argument 'half4 main(float2 coords)'
-        SamplePosition = Builder.ConstructFloat2(OriginalPosition.X, OriginalPosition.Y); 
+        SamplePosition = Builder.ConstructFloat2(OriginalPosition.X, OriginalPosition.Y);
     }
     }
 
 
     public Half4 SampleSurface(DrawingSurface surface, Expression pos)
     public Half4 SampleSurface(DrawingSurface surface, Expression pos)
@@ -68,7 +69,7 @@ public class FuncContext
             constantFloat.ConstantValue = new VecD(firstFloat.ConstantValue, secondFloat.ConstantValue);
             constantFloat.ConstantValue = new VecD(firstFloat.ConstantValue, secondFloat.ConstantValue);
             return constantFloat;
             return constantFloat;
         }
         }
-        
+
         return Builder.ConstructFloat2(x, y);
         return Builder.ConstructFloat2(x, y);
     }
     }
 
 
@@ -80,7 +81,7 @@ public class FuncContext
             constantFloat.ConstantValue = float1.ConstantValue;
             constantFloat.ConstantValue = float1.ConstantValue;
             return constantFloat;
             return constantFloat;
         }
         }
-        
+
         return Builder.ConstructFloat1(result);
         return Builder.ConstructFloat1(result);
     }
     }
 
 
@@ -93,13 +94,14 @@ public class FuncContext
             constantInt.ConstantValue = new VecI(firstInt.ConstantValue, secondInt.ConstantValue);
             constantInt.ConstantValue = new VecI(firstInt.ConstantValue, secondInt.ConstantValue);
             return constantInt;
             return constantInt;
         }
         }
-        
+
         return Builder.ConstructInt2(first, second);
         return Builder.ConstructInt2(first, second);
     }
     }
 
 
     public Half4 NewHalf4(Expression r, Expression g, Expression b, Expression a)
     public Half4 NewHalf4(Expression r, Expression g, Expression b, Expression a)
     {
     {
-        if (!HasContext && r is Float1 firstFloat && g is Float1 secondFloat && b is Float1 thirdFloat && a is Float1 fourthFloat)
+        if (!HasContext && r is Float1 firstFloat && g is Float1 secondFloat && b is Float1 thirdFloat &&
+            a is Float1 fourthFloat)
         {
         {
             Half4 constantHalf4 = new Half4("");
             Half4 constantHalf4 = new Half4("");
             byte rByte = firstFloat.AsConstantColorByte();
             byte rByte = firstFloat.AsConstantColorByte();
@@ -109,13 +111,14 @@ public class FuncContext
             constantHalf4.ConstantValue = new Color(rByte, gByte, bByte, aByte);
             constantHalf4.ConstantValue = new Color(rByte, gByte, bByte, aByte);
             return constantHalf4;
             return constantHalf4;
         }
         }
-        
+
         return Builder.ConstructHalf4(r, g, b, a);
         return Builder.ConstructHalf4(r, g, b, a);
     }
     }
 
 
     public Half4 HsvaToRgba(Expression h, Expression s, Expression v, Expression a)
     public Half4 HsvaToRgba(Expression h, Expression s, Expression v, Expression a)
     {
     {
-        if (!HasContext && h is Float1 firstFloat && s is Float1 secondFloat && v is Float1 thirdFloat && a is Float1 fourthFloat)
+        if (!HasContext && h is Float1 firstFloat && s is Float1 secondFloat && v is Float1 thirdFloat &&
+            a is Float1 fourthFloat)
         {
         {
             Half4 constantHalf4 = new Half4("");
             Half4 constantHalf4 = new Half4("");
             var hValue = firstFloat.ConstantValue * 360;
             var hValue = firstFloat.ConstantValue * 360;
@@ -131,7 +134,8 @@ public class FuncContext
 
 
     public Half4 HslaToRgba(Expression h, Expression s, Expression l, Expression a)
     public Half4 HslaToRgba(Expression h, Expression s, Expression l, Expression a)
     {
     {
-        if (!HasContext && h is Float1 firstFloat && s is Float1 secondFloat && l is Float1 thirdFloat && a is Float1 fourthFloat)
+        if (!HasContext && h is Float1 firstFloat && s is Float1 secondFloat && l is Float1 thirdFloat &&
+            a is Float1 fourthFloat)
         {
         {
             Half4 constantHalf4 = new Half4("");
             Half4 constantHalf4 = new Half4("");
             var hValue = firstFloat.ConstantValue * 360;
             var hValue = firstFloat.ConstantValue * 360;
@@ -144,29 +148,31 @@ public class FuncContext
 
 
         return Builder.AssignNewHalf4(Builder.Functions.GetHslToRgb(h, s, l, a));
         return Builder.AssignNewHalf4(Builder.Functions.GetHslToRgb(h, s, l, a));
     }
     }
-    
+
     public Half4 RgbaToHsva(Expression color)
     public Half4 RgbaToHsva(Expression color)
     {
     {
         if (!HasContext && color is Half4 constantColor)
         if (!HasContext && color is Half4 constantColor)
         {
         {
             var variable = new Half4(string.Empty);
             var variable = new Half4(string.Empty);
             constantColor.ConstantValue.ToHsv(out float h, out float s, out float l);
             constantColor.ConstantValue.ToHsv(out float h, out float s, out float l);
-            variable.ConstantValue = new Color((byte)(h * 255), (byte)(s * 255), (byte)(l * 255), constantColor.ConstantValue.A);
-            
+            variable.ConstantValue = new Color((byte)(h * 255), (byte)(s * 255), (byte)(l * 255),
+                constantColor.ConstantValue.A);
+
             return variable;
             return variable;
         }
         }
 
 
         return Builder.AssignNewHalf4(Builder.Functions.GetRgbToHsv(color));
         return Builder.AssignNewHalf4(Builder.Functions.GetRgbToHsv(color));
     }
     }
-    
+
     public Half4 RgbaToHsla(Expression color)
     public Half4 RgbaToHsla(Expression color)
     {
     {
         if (!HasContext && color is Half4 constantColor)
         if (!HasContext && color is Half4 constantColor)
         {
         {
             var variable = new Half4(string.Empty);
             var variable = new Half4(string.Empty);
             constantColor.ConstantValue.ToHsl(out float h, out float s, out float l);
             constantColor.ConstantValue.ToHsl(out float h, out float s, out float l);
-            variable.ConstantValue = new Color((byte)(h * 255), (byte)(s * 255), (byte)(l * 255), constantColor.ConstantValue.A);
-            
+            variable.ConstantValue = new Color((byte)(h * 255), (byte)(s * 255), (byte)(l * 255),
+                constantColor.ConstantValue.A);
+
             return variable;
             return variable;
         }
         }
 
 
@@ -181,7 +187,7 @@ public class FuncContext
             constantHalf4.ConstantValue = half4.ConstantValue;
             constantHalf4.ConstantValue = half4.ConstantValue;
             return constantHalf4;
             return constantHalf4;
         }
         }
-        
+
         return Builder.AssignNewHalf4(assignment);
         return Builder.AssignNewHalf4(assignment);
     }
     }
 
 
@@ -195,19 +201,20 @@ public class FuncContext
                 Builder.AddUniform(uniformName, (float)getFrom.Value(this).ConstantValue);
                 Builder.AddUniform(uniformName, (float)getFrom.Value(this).ConstantValue);
                 return new Float1(uniformName);
                 return new Float1(uniformName);
             }
             }
-        }
-        
-        if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
-        {
-            if (cachedValue is Float1 float1)
+
+            if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
             {
             {
-                return float1;
+                if (cachedValue is Float1 float1)
+                {
+                    return float1;
+                }
             }
             }
         }
         }
 
 
+
         var val = getFrom.Value(this);
         var val = getFrom.Value(this);
         _cachedValues[getFrom] = val;
         _cachedValues[getFrom] = val;
-        
+
         return val;
         return val;
     }
     }
 
 
@@ -221,19 +228,19 @@ public class FuncContext
                 Builder.AddUniform(uniformName, (int)getFrom.Value(this).ConstantValue);
                 Builder.AddUniform(uniformName, (int)getFrom.Value(this).ConstantValue);
                 return new Int1(uniformName);
                 return new Int1(uniformName);
             }
             }
-        }
-        
-        if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
-        {
-            if (cachedValue is Int1 int1)
+
+            if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
             {
             {
-                return int1;
+                if (cachedValue is Int1 int1)
+                {
+                    return int1;
+                }
             }
             }
         }
         }
 
 
         var val = getFrom.Value(this);
         var val = getFrom.Value(this);
         _cachedValues[getFrom] = val;
         _cachedValues[getFrom] = val;
-        
+
         return val;
         return val;
     }
     }
 
 
@@ -253,19 +260,19 @@ public class FuncContext
                 Builder.AddUniform(color.VariableName, color.ConstantValue);
                 Builder.AddUniform(color.VariableName, color.ConstantValue);
                 return color;
                 return color;
             }
             }
-        }
-        
-        if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
-        {
-            if (cachedValue is Half4 half4)
+
+            if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
             {
             {
-                return half4;
+                if (cachedValue is Half4 half4)
+                {
+                    return half4;
+                }
             }
             }
         }
         }
 
 
         var val = getFrom.Value(this);
         var val = getFrom.Value(this);
         _cachedValues[getFrom] = val;
         _cachedValues[getFrom] = val;
-        
+
         return val;
         return val;
     }
     }
 
 
@@ -280,19 +287,19 @@ public class FuncContext
                 Builder.AddUniform(value.VariableName, value.ConstantValue);
                 Builder.AddUniform(value.VariableName, value.ConstantValue);
                 return value;
                 return value;
             }
             }
-        }
-        
-        if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
-        {
-            if (cachedValue is Float2 float2)
+
+            if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
             {
             {
-                return float2;
+                if (cachedValue is Float2 float2)
+                {
+                    return float2;
+                }
             }
             }
         }
         }
 
 
         var val = getFrom.Value(this);
         var val = getFrom.Value(this);
         _cachedValues[getFrom] = val;
         _cachedValues[getFrom] = val;
-        
+
         return val;
         return val;
     }
     }
 
 
@@ -307,19 +314,19 @@ public class FuncContext
                 Builder.AddUniform(value.VariableName, value.ConstantValue);
                 Builder.AddUniform(value.VariableName, value.ConstantValue);
                 return value;
                 return value;
             }
             }
-        }
-        
-        if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
-        {
-            if (cachedValue is Int2 int2)
+
+            if (_cachedValues.TryGetValue(getFrom, out ShaderExpressionVariable cachedValue))
             {
             {
-                return int2;
+                if (cachedValue is Int2 int2)
+                {
+                    return int2;
+                }
             }
             }
         }
         }
 
 
         var val = getFrom.Value(this);
         var val = getFrom.Value(this);
         _cachedValues[getFrom] = val;
         _cachedValues[getFrom] = val;
-        
+
         return val;
         return val;
     }
     }
 }
 }

+ 3 - 3
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/MathNode.cs

@@ -56,8 +56,8 @@ public class MathNode : Node
             return context.NewFloat1(result);
             return context.NewFloat1(result);
         }
         }
 
 
-        var xConst = (x.GetConstant() as Float1)?.ConstantValue ?? 0;
-        var yConst = (y.GetConstant() as Float1)?.ConstantValue ?? 0;
+        var xConst = x.ConstantValue;
+        var yConst = y.ConstantValue;
             
             
         var constValue = Mode.Value switch
         var constValue = Mode.Value switch
         {
         {
@@ -73,7 +73,7 @@ public class MathNode : Node
         return new Float1(string.Empty) { ConstantValue = constValue };
         return new Float1(string.Empty) { ConstantValue = constValue };
     }
     }
 
 
-    private (ShaderExpressionVariable xConst, ShaderExpressionVariable y) GetValues(FuncContext context)
+    private (Float1 xConst, Float1 y) GetValues(FuncContext context)
     {
     {
         return (context.GetValue(X), context.GetValue(Y));
         return (context.GetValue(X), context.GetValue(Y));
     }
     }