Browse Source

Texture allocation changes

flabbet 1 year ago
parent
commit
183ed04c1b

+ 7 - 8
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/CombineSeparate/SeparateChannelsNode.cs

@@ -43,7 +43,6 @@ public class SeparateChannelsNode : Node
         Grayscale = CreateInput(nameof(Grayscale), "GRAYSCALE", false);
     }
 
-
     public override string DisplayName { get; set; } = "SEPARATE_CHANNELS_NODE";
     
     protected override Texture? OnExecute(RenderingContext context)
@@ -60,12 +59,12 @@ public class SeparateChannelsNode : Node
         var blue = !grayscale ? _blueFilter : _blueGrayscaleFilter;
         var alpha = !grayscale ? _alphaFilter : _alphaGrayscaleFilter;
 
-        Red.Value = GetImage(image, red);
-        Green.Value = GetImage(image, green);
-        Blue.Value = GetImage(image, blue);
-        Alpha.Value = GetImage(image, alpha);
+        Red.Value = GetImage(image, red, 0);
+        Green.Value = GetImage(image, green, 1);
+        Blue.Value = GetImage(image, blue, 2);
+        Alpha.Value = GetImage(image, alpha, 3);
 
-        var previewSurface = new Texture(image.Size * 2);
+        var previewSurface = RequestTexture(4, image.Size * 2);
 
         var size = image.Size;
         
@@ -82,9 +81,9 @@ public class SeparateChannelsNode : Node
         return previewSurface;
     }
 
-    private Texture GetImage(Texture image, ColorFilter filter)
+    private Texture GetImage(Texture image, ColorFilter filter, int id)
     {
-        var imageSurface = new Texture(image.Size);
+        var imageSurface = RequestTexture(id, image.Size);
 
         _paint.ColorFilter = filter;
         imageSurface.DrawingSurface.Canvas.DrawSurface(image.DrawingSurface, 0, 0, _paint);

+ 26 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Node.cs

@@ -17,7 +17,6 @@ public abstract class Node : IReadOnlyNode, IDisposable
     private List<InputProperty> inputs = new();
     private List<OutputProperty> outputs = new();
     protected List<KeyFrameData> keyFrames = new();
-
     public Guid Id { get; internal set; } = Guid.NewGuid();
 
     public IReadOnlyList<InputProperty> InputProperties => inputs;
@@ -59,6 +58,8 @@ public abstract class Node : IReadOnlyNode, IDisposable
     private bool _keyFramesDirty;
     private Texture? _lastCachedResult;
     private bool _isDisposed;
+    
+    private Dictionary<int, Texture> _managedTextures = new();
 
     public Texture? Execute(RenderingContext context)
     {
@@ -108,6 +109,30 @@ public abstract class Node : IReadOnlyNode, IDisposable
         _keyFramesDirty = false;
     }
 
+    protected Texture RequestTexture(int id, VecI size, bool clear = false)
+    {
+        if (_managedTextures.TryGetValue(id, out var texture))
+        {
+            if (texture.Size != size)
+            {
+                texture.Dispose();
+                texture = new Texture(size);
+                _managedTextures[id] = texture;
+                return texture;
+            }
+            
+            if (clear)
+            {
+                texture.DrawingSurface.Canvas.Clear();
+            }
+            
+            return texture;
+        }
+        
+        _managedTextures[id] = new Texture(size);
+        return _managedTextures[id];
+    }
+
     public void TraverseBackwards(Func<IReadOnlyNode, bool> action)
     {
         var visited = new HashSet<IReadOnlyNode>();

+ 5 - 0
src/PixiEditor/Views/Nodes/Properties/NodePropertyView.cs

@@ -25,6 +25,11 @@ public abstract class NodePropertyView : UserControl
         base.OnApplyTemplate(e);
         InputSocket = e.NameScope.Find<NodeSocket>("PART_InputSocket");
         OutputSocket = e.NameScope.Find<NodeSocket>("PART_OutputSocket");
+        
+        if(InputSocket is null || OutputSocket is null)
+        {
+            return;
+        }
 
         INodePropertyHandler propertyHandler = DataContext as INodePropertyHandler;
 

+ 0 - 1
src/PixiEditor/Views/Rendering/Scene.cs

@@ -147,7 +147,6 @@ internal class Scene : Zoombox.Zoombox, ICustomHitTest
         RectD dirtyBounds = new RectD(0, 0, Document.Width / resolutionScale, Document.Height / resolutionScale);
         Rect dirtyRect = new Rect(0, 0, Document.Width / resolutionScale, Document.Height / resolutionScale);
 
-        Surface.DrawingSurface.Flush();
         using var operation = new DrawSceneOperation(Surface, Document, CanvasPos, Scale * resolutionScale, angle,
             FlipX, FlipY,
             dirtyRect,

+ 1 - 1
src/PixiEditor/Views/Visuals/TextureImage.cs

@@ -20,7 +20,7 @@ public class TextureImage : IImage
 
     public void Draw(DrawingContext context, Rect sourceRect, Rect destRect)
     {
-        Texture.DrawingSurface.Flush();
+        if(Texture.IsDisposed) return; 
         context.Custom(new DrawTextureOperation(destRect, Stretch, Texture));
     }
 }