#if WINDOWS_UWP using System.IO; using CommunityToolkit.Diagnostics; using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Uwp; #endif using ComputeSharp.SwapChain.Shaders.D2D1; #if WINDOWS_UWP using Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas.UI.Xaml; using Windows.ApplicationModel; #endif #nullable enable namespace ComputeSharp.SwapChain.Core.Shaders.Runners; /// /// A specialized for . /// public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner { #if WINDOWS_UWP /// /// The reusable instance to use to render frames. /// private PixelShaderEffect? pixelShaderEffect; /// public unsafe void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) { if (this.pixelShaderEffect is null) { D2D1ResourceTextureManager resourceTextureManager; string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.1024x1024rgba32"); // Open a file to read from the file (we're doing synchronous IO here by design, doesn't really matter) using (FileStream fileStream = new(filename, FileMode.Open, FileAccess.Read)) { byte[] pixelData = new byte[(int)fileStream.Length]; int totalBytesRead = 0; // Read the raw image data while (totalBytesRead < pixelData.Length) { int bytesRead = fileStream.Read(pixelData, 0, pixelData.Length); Guard.IsGreaterThan(bytesRead, 0); totalBytesRead += bytesRead; } // Create the resource texture manager to use in the shader resourceTextureManager = new D2D1ResourceTextureManager( extents: stackalloc uint[] { 1024, 1024 }, bufferPrecision: D2D1BufferPrecision.UInt8Normalized, channelDepth: D2D1ChannelDepth.Four, filter: D2D1Filter.MinMagMipLinear, extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror }, data: pixelData, strides: stackalloc uint[] { (uint)(1024 * sizeof(Bgra32)) }); } // Create the new pixel shader effect this.pixelShaderEffect = new PixelShaderEffect() { ResourceTextureManagers = { [0] = resourceTextureManager } }; } int widthInPixels = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round); int heightInPixels = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round); // Set the constant buffer this.pixelShaderEffect.ConstantBuffer = new ContouredLayers((float)args.Timing.TotalTime.TotalSeconds, new int2(widthInPixels, heightInPixels)); // Draw the shader args.DrawingSession.DrawImage(this.pixelShaderEffect); } #endif }