Browse Source

Fixed color values in shader builder

flabbet 1 year ago
parent
commit
95b3cfe8a0

+ 2 - 5
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ModifyImageRightNode.cs

@@ -30,7 +30,6 @@ public class ModifyImageRightNode : Node, IPairNodeEnd
 
 
     private string _lastSksl;
     private string _lastSksl;
     
     
-    private TextureCache _textureCache = new(); 
 
 
     public ModifyImageRightNode()
     public ModifyImageRightNode()
     {
     {
@@ -59,7 +58,7 @@ public class ModifyImageRightNode : Node, IPairNodeEnd
         var width = size.X;
         var width = size.X;
         var height = size.Y;
         var height = size.Y;
 
 
-        Texture surface = _textureCache.GetTexture(renderingContext.ChunkResolution, size); 
+        Texture surface = RequestTexture(0, size);
 
 
         if (!surface.IsHardwareAccelerated)
         if (!surface.IsHardwareAccelerated)
         {
         {
@@ -123,9 +122,8 @@ public class ModifyImageRightNode : Node, IPairNodeEnd
             surface.DrawingSurface.Canvas.DrawPaint(drawingPaint);
             surface.DrawingSurface.Canvas.DrawPaint(drawingPaint);
             builder.Dispose();
             builder.Dispose();
         }
         }
-
+        
         Output.Value = surface;
         Output.Value = surface;
-
         return Output.Value;
         return Output.Value;
     }
     }
 
 
@@ -170,7 +168,6 @@ public class ModifyImageRightNode : Node, IPairNodeEnd
     public override void Dispose()
     public override void Dispose()
     {
     {
         base.Dispose();
         base.Dispose();
-        _textureCache.Dispose(); 
         drawingPaint?.Dispose();
         drawingPaint?.Dispose();
     }
     }
 
 

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

@@ -6,6 +6,7 @@ using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 using PixiEditor.ChangeableDocument.Rendering;
 using PixiEditor.ChangeableDocument.Rendering;
 using PixiEditor.DrawingApi.Core;
 using PixiEditor.DrawingApi.Core;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.Shaders;
 using PixiEditor.DrawingApi.Core.Shaders;
 using PixiEditor.Numerics;
 using PixiEditor.Numerics;
 
 
@@ -130,7 +131,7 @@ public abstract class Node : IReadOnlyNode, IDisposable
             
             
             if (clear)
             if (clear)
             {
             {
-                texture.DrawingSurface.Canvas.Clear();
+                texture.DrawingSurface.Canvas.Clear(Colors.Transparent);
             }
             }
             
             
             return texture;
             return texture;
@@ -309,6 +310,11 @@ public abstract class Node : IReadOnlyNode, IDisposable
                 keyFrame.Dispose();
                 keyFrame.Dispose();
             }
             }
         }
         }
+        
+        foreach (var texture in _managedTextures)
+        {
+            texture.Value.Dispose();
+        }
     }
     }
 
 
     public void DisconnectAll()
     public void DisconnectAll()

+ 1 - 1
src/PixiEditor.ChangeableDocument/Changes/Drawing/CombineStructureMembersOnto_Change.cs

@@ -79,7 +79,7 @@ internal class CombineStructureMembersOnto_Change : Change
         DocumentRenderer renderer = new(target);
         DocumentRenderer renderer = new(target);
 
 
         AffectedArea affArea = new();
         AffectedArea affArea = new();
-        DrawingBackendApi.Current.RenderDispatch(() =>
+        DrawingBackendApi.Current.RenderingServer.Invoke(() =>
         {
         {
             RectI? globalClippingRect = new RectI(0, 0, target.Size.X, target.Size.Y);
             RectI? globalClippingRect = new RectI(0, 0, target.Size.X, target.Size.Y);
             foreach (var chunk in chunksToCombine)
             foreach (var chunk in chunksToCombine)

+ 2 - 2
src/PixiEditor.DrawingApi.Core/Bridge/DrawingBackendApi.cs

@@ -20,7 +20,7 @@ namespace PixiEditor.DrawingApi.Core.Bridge
         
         
         public static bool HasBackend => _current != null;
         public static bool HasBackend => _current != null;
         
         
-        public static void SetupBackend(IDrawingBackend backend, Action<Action> dispatcher)
+        public static void SetupBackend(IDrawingBackend backend, IRenderingServer server)
         {
         {
             if (_current != null)
             if (_current != null)
             {
             {
@@ -28,7 +28,7 @@ namespace PixiEditor.DrawingApi.Core.Bridge
             }
             }
             
             
             _current = backend;
             _current = backend;
-            _current.RenderDispatch = dispatcher;
+            _current.RenderingServer = server;
             backend.Setup();
             backend.Setup();
         }
         }
     }
     }

+ 3 - 1
src/PixiEditor.DrawingApi.Core/Bridge/IDrawingBackend.cs

@@ -1,6 +1,8 @@
 using System;
 using System;
+using System.Threading.Tasks;
 using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
 using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
 using PixiEditor.DrawingApi.Core.Bridge.Operations;
 using PixiEditor.DrawingApi.Core.Bridge.Operations;
+using PixiEditor.DrawingApi.Core.Surfaces;
 
 
 namespace PixiEditor.DrawingApi.Core.Bridge
 namespace PixiEditor.DrawingApi.Core.Bridge
 {
 {
@@ -22,6 +24,6 @@ namespace PixiEditor.DrawingApi.Core.Bridge
         public IImageFilterImplementation ImageFilterImplementation { get; }
         public IImageFilterImplementation ImageFilterImplementation { get; }
         public IShaderImplementation ShaderImplementation { get; set; }
         public IShaderImplementation ShaderImplementation { get; set; }
         public bool IsHardwareAccelerated { get; }
         public bool IsHardwareAccelerated { get; }
-        public Action<Action> RenderDispatch { get; set; }
+        public IRenderingServer RenderingServer { get; set; }
     }
     }
 }
 }

+ 10 - 0
src/PixiEditor.DrawingApi.Core/IRenderingServer.cs

@@ -0,0 +1,10 @@
+using System;
+using System.Threading.Tasks;
+using PixiEditor.DrawingApi.Core.Surfaces;
+
+namespace PixiEditor.DrawingApi.Core;
+
+public interface IRenderingServer
+{
+    public Action<Action> Invoke { get; }
+}

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

@@ -38,7 +38,8 @@ public class ShaderBuilder
     {
     {
         foreach (var uniform in Uniforms)
         foreach (var uniform in Uniforms)
         {
         {
-            sb.AppendLine($"uniform {uniform.Value.UniformName} {uniform.Value.Name};");
+            string layout = string.IsNullOrEmpty(uniform.Value.LayoutOf) ? string.Empty : $"layout({uniform.Value.LayoutOf}) ";
+            sb.AppendLine($"{layout}uniform {uniform.Value.UniformName} {uniform.Value.Name};");
         }
         }
     }
     }
 
 

+ 4 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Uniform.cs

@@ -11,9 +11,10 @@ public struct Uniform
     public float FloatValue { get; }
     public float FloatValue { get; }
     public float[] FloatArrayValue { get; }
     public float[] FloatArrayValue { get; }
     public Shader ShaderValue { get; }
     public Shader ShaderValue { get; }
-    
     public string UniformName { get; }
     public string UniformName { get; }
 
 
+    public string LayoutOf { get; } = string.Empty;
+
     public UniformValueType DataType { get; }
     public UniformValueType DataType { get; }
 
 
     public Uniform(string name, float value)
     public Uniform(string name, float value)
@@ -50,9 +51,10 @@ public struct Uniform
     {
     {
         Name = name;
         Name = name;
         FloatValue = default;
         FloatValue = default;
-        FloatArrayValue = new float[] { color.R, color.G, color.B, color.A };
+        FloatArrayValue = new float[] { color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f };
         ShaderValue = default;
         ShaderValue = default;
         DataType = UniformValueType.FloatArray;
         DataType = UniformValueType.FloatArray;
+        //LayoutOf = "color"; TODO: Doesn't work with SkiaSharp 2.8x, check with 3.0 when it releases
         UniformName = "half4";
         UniformName = "half4";
     }
     }
 
 

+ 5 - 1
src/PixiEditor.DrawingApi.Skia/SkiaDrawingBackend.cs

@@ -1,7 +1,10 @@
 using System;
 using System;
+using System.Threading.Tasks;
+using PixiEditor.DrawingApi.Core;
 using PixiEditor.DrawingApi.Core.Bridge;
 using PixiEditor.DrawingApi.Core.Bridge;
 using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
 using PixiEditor.DrawingApi.Core.Bridge.NativeObjectsImpl;
 using PixiEditor.DrawingApi.Core.Bridge.Operations;
 using PixiEditor.DrawingApi.Core.Bridge.Operations;
+using PixiEditor.DrawingApi.Core.Surfaces;
 using PixiEditor.DrawingApi.Skia.Exceptions;
 using PixiEditor.DrawingApi.Skia.Exceptions;
 using PixiEditor.DrawingApi.Skia.Implementations;
 using PixiEditor.DrawingApi.Skia.Implementations;
 using SkiaSharp;
 using SkiaSharp;
@@ -25,7 +28,8 @@ namespace PixiEditor.DrawingApi.Skia
         }
         }
         
         
         public bool IsHardwareAccelerated => GraphicsContext != null;
         public bool IsHardwareAccelerated => GraphicsContext != null;
-        public Action<Action> RenderDispatch { get; set; }
+        
+        public IRenderingServer RenderingServer { get; set; }
 
 
         public IColorImplementation ColorImplementation { get; }
         public IColorImplementation ColorImplementation { get; }
         public IImageImplementation ImageImplementation { get; }
         public IImageImplementation ImageImplementation { get; }

+ 2 - 1
src/PixiEditor/Models/Files/ImageFileType.cs

@@ -2,6 +2,7 @@
 using ChunkyImageLib;
 using ChunkyImageLib;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
 using PixiEditor.DrawingApi.Core;
 using PixiEditor.DrawingApi.Core;
+using PixiEditor.DrawingApi.Core.Bridge;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.Surfaces;
 using PixiEditor.DrawingApi.Core.Surfaces;
 using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Extensions.Common.Localization;
@@ -35,7 +36,7 @@ internal abstract class ImageFileType : IoFileType
             var maybeBitmap = document.TryRenderWholeImage(0);
             var maybeBitmap = document.TryRenderWholeImage(0);
             if (maybeBitmap.IsT0)
             if (maybeBitmap.IsT0)
                 return SaveResult.ConcurrencyError;
                 return SaveResult.ConcurrencyError;
-            
+
             finalSurface = maybeBitmap.AsT1;
             finalSurface = maybeBitmap.AsT1;
             if (maybeBitmap.AsT1.Size != exportConfig.ExportSize)
             if (maybeBitmap.AsT1.Size != exportConfig.ExportSize)
             {
             {

+ 7 - 9
src/PixiEditor/ViewModels/Document/DocumentViewModel.cs

@@ -504,24 +504,22 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
         {
         {
             Surface finalSurface = new Surface(SizeBindable);
             Surface finalSurface = new Surface(SizeBindable);
             VecI sizeInChunks = (VecI)((VecD)SizeBindable / ChunkyImage.FullChunkSize).Ceiling();
             VecI sizeInChunks = (VecI)((VecD)SizeBindable / ChunkyImage.FullChunkSize).Ceiling();
-            for (int i = 0; i < sizeInChunks.X; i++)
+            DrawingBackendApi.Current.RenderingServer.Invoke(() =>
             {
             {
-                for (int j = 0; j < sizeInChunks.Y; j++)
+                for (int i = 0; i < sizeInChunks.X; i++)
                 {
                 {
-                    var i1 = i;
-                    var j1 = j;
-                    DrawingBackendApi.Current.RenderDispatch(() =>
+                    for (int j = 0; j < sizeInChunks.Y; j++)
                     {
                     {
-                        var maybeChunk = Renderer.RenderChunk(new(i1, j1), ChunkResolution.Full, frameTime);
+                        var maybeChunk = Renderer.RenderChunk(new(i, j), ChunkResolution.Full, frameTime);
                         if (maybeChunk.IsT1)
                         if (maybeChunk.IsT1)
                             return;
                             return;
                         using Chunk chunk = maybeChunk.AsT0;
                         using Chunk chunk = maybeChunk.AsT0;
                         finalSurface.DrawingSurface.Canvas.DrawSurface(
                         finalSurface.DrawingSurface.Canvas.DrawSurface(
                             chunk.Surface.DrawingSurface,
                             chunk.Surface.DrawingSurface,
-                            i1 * ChunkyImage.FullChunkSize, j1 * ChunkyImage.FullChunkSize);
-                    });
+                            i * ChunkyImage.FullChunkSize, j * ChunkyImage.FullChunkSize);
+                    }
                 }
                 }
-            }
+            });
 
 
             return finalSurface;
             return finalSurface;
         }
         }

+ 1 - 1
src/PixiEditor/Views/MainWindow.axaml

@@ -18,4 +18,4 @@
         ui:Translator.UseLanguageFlowDirection="True"
         ui:Translator.UseLanguageFlowDirection="True"
         Title="PixiEditor">
         Title="PixiEditor">
     <views1:MainView />
     <views1:MainView />
-</Window>
+</Window>

+ 4 - 1
src/PixiEditor/Views/MainWindow.axaml.cs

@@ -22,6 +22,7 @@ using PixiEditor.Models.AnalyticsAPI;
 using PixiEditor.Models.ExceptionHandling;
 using PixiEditor.Models.ExceptionHandling;
 using PixiEditor.Platform;
 using PixiEditor.Platform;
 using PixiEditor.ViewModels.SubViewModels;
 using PixiEditor.ViewModels.SubViewModels;
+using PixiEditor.Views.Rendering;
 using ViewModelMain = PixiEditor.ViewModels.ViewModelMain;
 using ViewModelMain = PixiEditor.ViewModels.ViewModelMain;
 using ViewModels_ViewModelMain = PixiEditor.ViewModels.ViewModelMain;
 using ViewModels_ViewModelMain = PixiEditor.ViewModels.ViewModelMain;
 
 
@@ -71,7 +72,9 @@ internal partial class MainWindow : Window
 
 
         SkiaDrawingBackend skiaDrawingBackend = new SkiaDrawingBackend();
         SkiaDrawingBackend skiaDrawingBackend = new SkiaDrawingBackend();
         skiaDrawingBackend.GraphicsContext = GetOpenGlGrContext();
         skiaDrawingBackend.GraphicsContext = GetOpenGlGrContext();
-        DrawingBackendApi.SetupBackend(skiaDrawingBackend, (a) => Dispatcher.UIThread.Invoke(a));
+        
+        AvaloniaRenderingServer renderingServer = new AvaloniaRenderingServer();
+        DrawingBackendApi.SetupBackend(skiaDrawingBackend, renderingServer);
 
 
         preferences = services.GetRequiredService<IPreferences>();
         preferences = services.GetRequiredService<IPreferences>();
         platform = services.GetRequiredService<IPlatform>();
         platform = services.GetRequiredService<IPlatform>();

+ 18 - 0
src/PixiEditor/Views/Rendering/AvaloniaRenderingServer.cs

@@ -0,0 +1,18 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Media;
+using Avalonia.Rendering.SceneGraph;
+using Avalonia.Skia;
+using Avalonia.Threading;
+using PixiEditor.DrawingApi.Core;
+using PixiEditor.DrawingApi.Core.Bridge;
+using PixiEditor.DrawingApi.Core.Surfaces;
+using PixiEditor.DrawingApi.Skia.Extensions;
+using PixiEditor.Views.Visuals;
+
+namespace PixiEditor.Views.Rendering;
+
+public class AvaloniaRenderingServer : IRenderingServer
+{
+    public Action<Action> Invoke { get; } = action => Dispatcher.UIThread.Invoke(action);
+}