Browse Source

Shader changes

flabbet 3 years ago
parent
commit
4693d2b6f1

+ 3 - 1
src/ChunkyImageLib/ChunkyImage.cs

@@ -1,4 +1,5 @@
-using System.Runtime.CompilerServices;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
 using ChunkyImageLib.DataHolders;
 using ChunkyImageLib.Operations;
 using OneOf;
@@ -693,6 +694,7 @@ public class ChunkyImage : IReadOnlyChunkyImage, IDisposable
         {
             ThrowIfDisposed();
             var affectedChunks = FindAffectedChunks();
+
             foreach (var chunk in affectedChunks)
             {
                 MaybeCreateAndProcessQueueForChunk(chunk, ChunkResolution.Full);

+ 3 - 36
src/ChunkyImageLib/Operations/ReplaceColorOperation.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 using System.Runtime.InteropServices;
 using System.Text;
@@ -42,49 +43,15 @@ internal class ReplaceColorOperation : IDrawOperation
             .AllocateReadWriteTexture2D<uint2>(chunk.PixelSize.X, chunk.PixelSize.Y);
 
         texture.CopyFrom(span);
-        
-        uint convR = (BitConverter.HalfToUInt16Bits((Half)(newColor.Red / 255f)));
-        uint convG = (BitConverter.HalfToUInt16Bits((Half)(newColor.Green / 255f)));
-        uint convB = (BitConverter.HalfToUInt16Bits((Half)(newColor.Blue / 255f)));
-        uint convA = (BitConverter.HalfToUInt16Bits((Half)(newColor.Alpha / 255f)));
 
-        UInt2 newCol = new UInt2(convG << 16 | convR, convB | convA << 16);
+        UInt2 packedColor = ShaderUtils.PackPixel(newColor);
         
         GraphicsDevice.GetDefault().For(texture.Width, texture.Height, 
             new ReplaceColorShader(
                 texture,
                 oldColorBounds,
-                newCol));
+                packedColor));
         texture.CopyTo(span);
-        //SKImage processedImage = SKBitmap.
-        
-        /*int maxThreads = Environment.ProcessorCount;
-        VecI imageSize = chunk.PixelSize;
-        int rowsPerThread = imageSize.Y / maxThreads;
-
-        using SKPixmap pixmap = chunk.Surface.SkiaSurface.PeekPixels();
-        IntPtr pixels = pixmap.GetPixels();
-
-        Half* endOffset = (Half*)(pixels + pixmap.BytesSize);
-        for (Half* i = (Half*)pixels; i < endOffset; i += 4)
-        {
-            if (oldColorBounds.IsWithinBounds(i))
-                *(ulong*)i = newColorBits;
-        }*/
-    }
-
-    private static Float4 ToFloat4(UInt2 pixel)
-    {
-        return new Float4(
-            Hlsl.Float16ToFloat32(pixel.X),
-            Hlsl.Float16ToFloat32(pixel.X >> 16),
-            Hlsl.Float16ToFloat32(pixel.Y),
-            Hlsl.Float16ToFloat32(pixel.Y >> 16));
-    }
-
-    private static ulong PackPixel(Float4 pixel)
-    {
-        return (ulong)pixel.R << 0 | (ulong)pixel.G << 8 | (ulong)pixel.B << 16 | (ulong)pixel.A << 24;
     }
 
     public HashSet<VecI> FindAffectedChunks(VecI imageSize)

+ 1 - 10
src/ChunkyImageLib/Shaders/ReplaceColorShader.cs

@@ -12,21 +12,12 @@ public readonly partial struct ReplaceColorShader : IComputeShader
     public void Execute()
     {
         uint2 rgba = texture[ThreadIds.XY];
-        Float4 rgbaFloat = new Float4(
-            Hlsl.Float16ToFloat32(rgba.X),
-            Hlsl.Float16ToFloat32(rgba.X >> 16),
-            Hlsl.Float16ToFloat32(rgba.Y),
-            Hlsl.Float16ToFloat32(rgba.Y >> 16)
-        );
+        Float4 rgbaFloat = ShaderUtils.UnpackPixel(rgba);
         
         if(IsWithinBounds(rgbaFloat))
         {
             texture[ThreadIds.XY] = newColor;
         }
-        else
-        {
-            texture[ThreadIds.XY] = rgba;
-        }
     }
 
     private bool IsWithinBounds(float4 color)

+ 27 - 0
src/ChunkyImageLib/Shaders/ShaderUtils.cs

@@ -0,0 +1,27 @@
+using ComputeSharp;
+using SkiaSharp;
+
+namespace ChunkyImageLib.Shaders;
+
+public static class ShaderUtils
+{
+    public static float4 UnpackPixel(uint2 packedPixel)
+    {
+        return new float4(
+            Hlsl.Float16ToFloat32(packedPixel.X),
+            Hlsl.Float16ToFloat32(packedPixel.X >> 16),
+            Hlsl.Float16ToFloat32(packedPixel.Y),
+            Hlsl.Float16ToFloat32(packedPixel.Y >> 16)
+        );
+    }
+
+    public static uint2 PackPixel(SKColor color)
+    {
+        uint convR = (BitConverter.HalfToUInt16Bits((Half)(color.Red / 255f)));
+        uint convG = (BitConverter.HalfToUInt16Bits((Half)(color.Green / 255f)));
+        uint convB = (BitConverter.HalfToUInt16Bits((Half)(color.Blue / 255f)));
+        uint convA = (BitConverter.HalfToUInt16Bits((Half)(color.Alpha / 255f)));
+
+        return new UInt2(convG << 16 | convR, convB | convA << 16);
+    }
+}