diff --git a/.gitignore b/.gitignore index 23c39fe0..fd8f3e38 100644 --- a/.gitignore +++ b/.gitignore @@ -329,6 +329,5 @@ ASALocalRun/ # MFractors (Xamarin productivity tool) working folder .mfractor/ -# Sample bokeh blur image -**/*_bokeh.jpg -**/*_gaussian.jpg +# Sample images +**/city_*.jpg diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 066d9426..cccc08d6 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -45,7 +45,7 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.Sample.Shared" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.Internals.SourceGenerators", "tests\ComputeSharp.Tests.Internals.SourceGenerators\ComputeSharp.Tests.Internals.SourceGenerators.csproj", "{BBBD0ADD-F2F9-49A3-BD5E-D223F0735D8B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.Tests.NativeLibrariesResolver", "tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj", "{BF52326F-E415-49E6-8595-62762216762B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Tests.NativeLibrariesResolver", "tests\ComputeSharp.Tests.NativeLibrariesResolver\ComputeSharp.Tests.NativeLibrariesResolver.csproj", "{BF52326F-E415-49E6-8595-62762216762B}" EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution diff --git a/nuget.config b/nuget.config index a06b48ea..e71007c9 100644 --- a/nuget.config +++ b/nuget.config @@ -4,5 +4,6 @@ + diff --git a/samples/ComputeSharp.Benchmark/DnnBenchmark.cs b/samples/ComputeSharp.Benchmark/Blas/BlasBenchmark.cs similarity index 86% rename from samples/ComputeSharp.Benchmark/DnnBenchmark.cs rename to samples/ComputeSharp.Benchmark/Blas/BlasBenchmark.cs index 186d119f..870b1966 100644 --- a/samples/ComputeSharp.Benchmark/DnnBenchmark.cs +++ b/samples/ComputeSharp.Benchmark/Blas/BlasBenchmark.cs @@ -4,13 +4,13 @@ using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Engines; -namespace ComputeSharp.Benchmark +namespace ComputeSharp.Benchmark.Blas { /// - /// A that benchmarks the APIs in the class, on both CPU and GPU. + /// A that benchmarks the APIs in the class, on both CPU and GPU. /// [SimpleJob(RunStrategy.Monitoring)] - public class DnnBenchmark : IDisposable + public class BlasBenchmark : IDisposable { /// /// The number of samples. @@ -105,11 +105,13 @@ namespace ComputeSharp.Benchmark BufferW = Gpu.Default.AllocateReadOnlyBuffer(W); BufferB = Gpu.Default.AllocateReadOnlyBuffer(B); BufferY = Gpu.Default.AllocateReadWriteBuffer(Y); + + Cpu(); + GpuWithNoTemporaryBuffers(); + GpuWithTemporaryBuffers(); } - /// - /// Clears the memory from the current session. - /// + /// [GlobalCleanup] public void Dispose() { @@ -128,13 +130,19 @@ namespace ComputeSharp.Benchmark /// Runs a fully connected forward operation on the CPU. /// [Benchmark(Baseline = true)] - public void Cpu() => Dnn.FullyConnectedForwardCpu(C, N, M, P, X, W, B, Y); + public void Cpu() + { + BlasHelpers.FullyConnectedForwardCpu(C, N, M, P, X, W, B, Y); + } /// /// Runs a fully connected forward operation on the GPU. /// [Benchmark] - public void GpuWithNoTemporaryBuffers() => Dnn.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, BufferX, BufferW, BufferB, BufferY); + public void GpuWithNoTemporaryBuffers() + { + BlasHelpers.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, BufferX, BufferW, BufferB, BufferY); + } /// /// Runs a fully connected forward operation on the GPU, creating temporary GPU buffers to perform the operations. @@ -147,7 +155,7 @@ namespace ComputeSharp.Benchmark using ReadOnlyBuffer b = Gpu.Default.AllocateReadOnlyBuffer(B); using ReadWriteBuffer y = Gpu.Default.AllocateReadWriteBuffer(Y); - Dnn.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, x, w, b, y); + BlasHelpers.FullyConnectedForwardGpu(Gpu.Default, C, N, M, P, x, w, b, y); y.GetData(Y); } diff --git a/samples/ComputeSharp.Benchmark/Dnn.cs b/samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs similarity index 98% rename from samples/ComputeSharp.Benchmark/Dnn.cs rename to samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs index 3f8087ba..6634e708 100644 --- a/samples/ComputeSharp.Benchmark/Dnn.cs +++ b/samples/ComputeSharp.Benchmark/Blas/BlasHelpers.cs @@ -2,12 +2,12 @@ using System.Threading.Tasks; using ComputeSharp.Graphics; -namespace ComputeSharp.Benchmark +namespace ComputeSharp.Benchmark.Blas { /// /// A that contains primitives to run certain operations of a neural network. /// - internal static partial class Dnn + internal static partial class BlasHelpers { /// /// Executes the forward pass on a fully connected layer on the CPU. diff --git a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj index 06bb8291..ccadcf15 100644 --- a/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj +++ b/samples/ComputeSharp.Benchmark/ComputeSharp.Benchmark.csproj @@ -6,7 +6,20 @@ - + + + + + + + + + + + + + + diff --git a/samples/ComputeSharp.Benchmark/Imaging/ImagingBenchmark.cs b/samples/ComputeSharp.Benchmark/Imaging/ImagingBenchmark.cs new file mode 100644 index 00000000..e518da56 --- /dev/null +++ b/samples/ComputeSharp.Benchmark/Imaging/ImagingBenchmark.cs @@ -0,0 +1,91 @@ +using System; +using System.IO; +using System.Reflection; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Engines; +using ComputeSharp.BokehBlur.Processors; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Processing; +using SixLabors.ImageSharp.Processing.Processors.Convolution; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; + +namespace ComputeSharp.Benchmark.Imaging +{ + /// + /// A that benchmarks the image processors against the reference in ImageSharp. + /// + [SimpleJob(RunStrategy.Monitoring)] + [CategoriesColumn] + [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] + public class ImagingBenchmark : IDisposable + { + /// + /// The loaded image. + /// + private Image image; + + /// + /// Initial setup for a benchmarking session. + /// + [GlobalSetup] + public void Setup() + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Imaging", "city.jpg"); + + this.image = Image.Load(path); + + Bokeh_Cpu(); + Bokeh_Gpu(); + Gaussian_Cpu(); + Gaussian_Gpu(); + } + + /// + [GlobalCleanup] + public void Dispose() + { + this.image.Dispose(); + } + + /// + /// Runs a bokeh blur effect on the CPU. + /// + [Benchmark(Baseline = true)] + [BenchmarkCategory("BOKEH")] + public void Bokeh_Cpu() + { + this.image.Mutate(c => c.ApplyProcessor(new BokehBlurProcessor(80, 2, 3))); + } + + /// + /// Runs a bokeh blur effect on the GPU. + /// + [Benchmark] + [BenchmarkCategory("BOKEH")] + public void Bokeh_Gpu() + { + this.image.Mutate(c => c.ApplyProcessor(new HlslBokehBlurProcessor(80, 2))); + } + + /// + /// Runs a gaussian blur effect on the CPU. + /// + [Benchmark(Baseline = true)] + [BenchmarkCategory("GAUSSIAN")] + public void Gaussian_Cpu() + { + this.image.Mutate(c => c.ApplyProcessor(new GaussianBlurProcessor(80))); + } + + /// + /// Runs a gaussian blur effect on the GPU. + /// + [Benchmark] + [BenchmarkCategory("GAUSSIAN")] + public void Gaussian_Gpu() + { + this.image.Mutate(c => c.ApplyProcessor(new HlslGaussianBlurProcessor(80))); + } + } +} diff --git a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs b/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs deleted file mode 100644 index 1c24a7dd..00000000 --- a/samples/ComputeSharp.ImageProcessing/Primitives/ComplexVector4.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -using System.Numerics; - -namespace SixLabors.ImageSharp -{ - /// - /// A vector with a pair of representing a complex color. - /// - internal struct ComplexVector4 - { - /// - /// The real part of the complex vector. - /// - public Vector4 Real; - - /// - /// The imaginary part of the complex number. - /// - public Vector4 Imaginary; - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs index 84e05a55..dd4ecb24 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor.cs @@ -1,7 +1,9 @@ -using System.Numerics; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Diagnostics; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.BokehBlur.Processors { @@ -20,27 +22,23 @@ namespace ComputeSharp.BokehBlur.Processors /// public const int DefaultComponents = 2; - /// - /// The default gamma used by the parameterless constructor. - /// - public const float DefaultGamma = 3f; - /// /// Initializes a new instance of the class. /// - public HlslBokehBlurProcessor() : this(DefaultRadius, DefaultComponents, DefaultGamma) { } + public HlslBokehBlurProcessor() + : this(DefaultRadius, DefaultComponents) + { + } /// /// Initializes a new instance of the class. /// /// The size of the area to sample. /// The number of components to use to approximate the original 2D bokeh blur convolution kernel. - /// The gamma highlight factor to use to further process the image. - public HlslBokehBlurProcessor(int radius, int components, float gamma) + public HlslBokehBlurProcessor(int radius, int components) { Radius = radius; Components = components; - Gamma = gamma; } /// @@ -53,98 +51,18 @@ namespace ComputeSharp.BokehBlur.Processors /// public int Components { get; } - /// - /// Gets the gamma highlight factor to use when applying the effect. - /// - public float Gamma { get; } - /// public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) where TPixel : unmanaged, IPixel { - return new HlslBokehBlurProcessor(this, configuration, source, sourceRectangle); - } - - /// - /// Kernel for the vertical convolution pass. - /// - [AutoConstructor] - internal partial struct VerticalConvolutionProcessor : IComputeShader - { - public int width; - public int maxY; - public int maxX; - public int kernelLength; - - public ReadOnlyBuffer source; - public ReadWriteBuffer target; - public ReadOnlyBuffer kernel; - - /// - public void Execute(ThreadIds ids) + if (typeof(TPixel) != typeof(ImageSharpRgba32)) { - Vector4 real = Vector4.Zero; - Vector4 imaginary = Vector4.Zero; - int radiusY = kernelLength >> 1; - int sourceOffsetColumnBase = ids.X; - - for (int i = 0; i < kernelLength; i++) - { - int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); - int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); - Vector4 color = source[offsetY * width + offsetX]; - Complex64 factors = kernel[i]; - - real += factors.Real * color; - imaginary += factors.Imaginary * color; - } - - int offsetXY = ids.Y * width + ids.X; - - target[offsetXY].Real = real; - target[offsetXY].Imaginary = imaginary; + ThrowHelper.ThrowInvalidOperationException("This processor only supports the RGBA32 pixel format"); } - } - /// - /// Kernel for the horizontal convolution pass. - /// - [AutoConstructor] - internal partial struct HorizontalConvolutionAndAccumulatePartialsProcessor : IComputeShader - { - public int width; - public int maxY; - public int maxX; - public int kernelLength; - public float z; - public float w; + var processor = new Implementation(this, configuration, Unsafe.As>(source), sourceRectangle); - public ReadWriteBuffer source; - public ReadWriteBuffer target; - public ReadOnlyBuffer kernel; - - /// - public void Execute(ThreadIds ids) - { - Vector4 real = Vector4.Zero; - Vector4 imaginary = Vector4.Zero; - int radiusX = kernelLength >> 1; - int sourceOffsetColumnBase = ids.X; - int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); - - for (int i = 0; i < kernelLength; i++) - { - int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); - var offsetXY = offsetY * width + offsetX; - ComplexVector4 source4 = source[offsetXY]; - Complex64 factors = kernel[i]; - - real += factors.Real * source4.Real - factors.Imaginary * source4.Imaginary; - imaginary += factors.Real * source4.Imaginary + factors.Imaginary * source4.Real; - } - - target[ids.Y * width + ids.X] += real * z + imaginary * w; - } + return Unsafe.As>(processor); } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs index bbc41abb..25d0d23b 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslBokehBlurProcessor{TPixel}.cs @@ -1,272 +1,272 @@ using System; -using System.Buffers; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using Microsoft.Toolkit.Diagnostics; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.BokehBlur.Processors { - /// - /// Applies bokeh blur processing to the image. - /// - /// The pixel format. - /// This processor is based on the code from Mike Pound, see github.com/mikepound/convolve. - public sealed partial class HlslBokehBlurProcessor : ImageProcessor - where TPixel : unmanaged, IPixel + /// + public sealed partial class HlslBokehBlurProcessor { /// - /// The kernel radius. + /// Applies bokeh blur processing to the image. /// - private readonly int Radius; - - /// - /// The gamma highlight factor to use when applying the effect. - /// - private readonly float Gamma; - - /// - /// The maximum size of the kernel in either direction. - /// - private readonly int KernelSize; - - /// - /// The number of components to use when applying the bokeh blur. - /// - private readonly int ComponentsCount; - - /// - /// The kernel parameters to use for the current instance (a: X, b: Y, A: Z, B: W) - /// - private readonly Vector4[] KernelParameters; - - /// - /// The kernel components for the current instance. - /// - private readonly Complex64[][] Kernels; - - /// - /// The scaling factor for kernel values. - /// - private readonly float KernelsScale; - - /// - /// The mapping of initialized complex kernels and parameters, to speed up the initialization of new instances. - /// - private static readonly ConcurrentDictionary<(int Radius, int ComponentsCount), (Vector4[] Parameters, float Scale, Complex64[][] Kernels)> Cache = new(); - - /// - /// Initializes a new instance of the class. - /// - /// The defining the processor parameters. - /// The configuration which allows altering default behaviour or extending the library. - /// The source instance to modify. - /// The source that indicates the area to edit. - public HlslBokehBlurProcessor(HlslBokehBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle) - : base(configuration, source, sourceRectangle) + /// This processor is based on the code from Mike Pound, see github.com/mikepound/convolve. + public sealed partial class Implementation : ImageProcessor { - Radius = definition.Radius; - KernelSize = Radius * 2 + 1; - ComponentsCount = definition.Components; - Gamma = definition.Gamma; + /// + /// The kernel radius. + /// + private readonly int Radius; - // Reuse the initialized values from the cache, if possible - var parameters = (Radius, ComponentsCount); - if (Cache.TryGetValue(parameters, out var info)) + /// + /// The maximum size of the kernel in either direction. + /// + private readonly int KernelSize; + + /// + /// The number of components to use when applying the bokeh blur. + /// + private readonly int ComponentsCount; + + /// + /// The kernel parameters to use for the current instance (a: X, b: Y, A: Z, B: W) + /// + private readonly Vector4[] KernelParameters; + + /// + /// The kernel components for the current instance. + /// + private readonly Complex64[][] Kernels; + + /// + /// The scaling factor for kernel values. + /// + private readonly float KernelsScale; + + /// + /// The mapping of initialized complex kernels and parameters, to speed up the initialization of new instances. + /// + private static readonly ConcurrentDictionary<(int Radius, int ComponentsCount), (Vector4[] Parameters, float Scale, Complex64[][] Kernels)> Cache = new(); + + /// + /// Initializes a new instance of the class. + /// + /// The defining the processor parameters. + /// The configuration which allows altering default behaviour or extending the library. + /// The source instance to modify. + /// The source that indicates the area to edit. + public Implementation(HlslBokehBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle) + : base(configuration, source, sourceRectangle) { - KernelParameters = info.Parameters; - KernelsScale = info.Scale; - Kernels = info.Kernels; - } - else - { - // Initialize the complex kernels and parameters with the current arguments - (KernelParameters, KernelsScale) = GetParameters(); - Kernels = CreateComplexKernels(); - NormalizeKernels(); + Radius = definition.Radius; + KernelSize = Radius * 2 + 1; + ComponentsCount = definition.Components; - // Store them in the cache for future use - Cache.TryAdd(parameters, (KernelParameters, KernelsScale, Kernels)); - } - } - - /// - /// Gets the kernel scales to adjust the component values in each kernel. - /// - private static IReadOnlyList KernelScales { get; } = new[] { 1.4f, 1.2f, 1.2f, 1.2f, 1.2f, 1.2f }; - - /// - /// Gets the available bokeh blur kernel parameters. - /// - private static IReadOnlyList KernelComponents { get; } = new[] - { - // 1 component - new[] { new Vector4(0.862325f, 1.624835f, 0.767583f, 1.862321f) }, - - // 2 components - new[] - { - new Vector4(0.886528f, 5.268909f, 0.411259f, -0.548794f), - new Vector4(1.960518f, 1.558213f, 0.513282f, 4.56111f) - }, - - // 3 components - new[] - { - new Vector4(2.17649f, 5.043495f, 1.621035f, -2.105439f), - new Vector4(1.019306f, 9.027613f, -0.28086f, -0.162882f), - new Vector4(2.81511f, 1.597273f, -0.366471f, 10.300301f) - }, - - // 4 components - new[] - { - new Vector4(4.338459f, 1.553635f, -5.767909f, 46.164397f), - new Vector4(3.839993f, 4.693183f, 9.795391f, -15.227561f), - new Vector4(2.791880f, 8.178137f, -3.048324f, 0.302959f), - new Vector4(1.342190f, 12.328289f, 0.010001f, 0.244650f) - }, - - // 5 components - new[] - { - new Vector4(4.892608f, 1.685979f, -22.356787f, 85.91246f), - new Vector4(4.71187f, 4.998496f, 35.918936f, -28.875618f), - new Vector4(4.052795f, 8.244168f, -13.212253f, -1.578428f), - new Vector4(2.929212f, 11.900859f, 0.507991f, 1.816328f), - new Vector4(1.512961f, 16.116382f, 0.138051f, -0.01f) - }, - - // 6 components - new[] - { - new Vector4(5.143778f, 2.079813f, -82.326596f, 111.231024f), - new Vector4(5.612426f, 6.153387f, 113.878661f, 58.004879f), - new Vector4(5.982921f, 9.802895f, 39.479083f, -162.028887f), - new Vector4(6.505167f, 11.059237f, -71.286026f, 95.027069f), - new Vector4(3.869579f, 14.81052f, 1.405746f, -3.704914f), - new Vector4(2.201904f, 19.032909f, -0.152784f, -0.107988f) - } - }; - - /// - /// Gets the kernel parameters and scaling factor for the current count value in the current instance. - /// - private (Vector4[] Parameters, float Scale) GetParameters() - { - // Prepare the kernel components - int index = Math.Max(0, Math.Min(ComponentsCount - 1, KernelComponents.Count)); - return (KernelComponents[index], KernelScales[index]); - } - - /// - /// Creates the collection of complex 1D kernels with the specified parameters. - /// - private Complex64[][] CreateComplexKernels() - { - var kernels = new Complex64[KernelParameters.Length][]; - ref Vector4 baseRef = ref MemoryMarshal.GetReference(KernelParameters.AsSpan()); - for (int i = 0; i < KernelParameters.Length; i++) - { - ref Vector4 paramsRef = ref Unsafe.Add(ref baseRef, i); - kernels[i] = CreateComplex1DKernel(paramsRef.X, paramsRef.Y); - } - - return kernels; - } - - /// - /// Creates a complex 1D kernel with the specified parameters. - /// - /// The exponential parameter for each complex component. - /// The angle component for each complex component. - private Complex64[] CreateComplex1DKernel(float a, float b) - { - var kernel = new Complex64[KernelSize]; - ref Complex64 baseRef = ref MemoryMarshal.GetReference(kernel.AsSpan()); - int r = Radius, n = -r; - - for (int i = 0; i < KernelSize; i++, n++) - { - // Incrementally compute the range values - float value = n * KernelsScale * (1f / r); - value *= value; - - // Fill in the complex kernel values - Unsafe.Add(ref baseRef, i) = new Complex64( - MathF.Exp(-a * value) * MathF.Cos(b * value), - MathF.Exp(-a * value) * MathF.Sin(b * value)); - } - - return kernel; - } - - /// - /// Normalizes the kernels with respect to A * real + B * imaginary. - /// - private void NormalizeKernels() - { - // Calculate the complex weighted sum - float total = 0; - Span kernelsSpan = Kernels.AsSpan(); - ref Complex64[] baseKernelsRef = ref MemoryMarshal.GetReference(kernelsSpan); - ref Vector4 baseParamsRef = ref MemoryMarshal.GetReference(KernelParameters.AsSpan()); - - for (int i = 0; i < KernelParameters.Length; i++) - { - ref Complex64[] kernelRef = ref Unsafe.Add(ref baseKernelsRef, i); - int length = kernelRef.Length; - ref Complex64 valueRef = ref kernelRef[0]; - ref Vector4 paramsRef = ref Unsafe.Add(ref baseParamsRef, i); - - for (int j = 0; j < length; j++) + // Reuse the initialized values from the cache, if possible + var parameters = (Radius, ComponentsCount); + if (Cache.TryGetValue(parameters, out var info)) { - for (int k = 0; k < length; k++) + KernelParameters = info.Parameters; + KernelsScale = info.Scale; + Kernels = info.Kernels; + } + else + { + // Initialize the complex kernels and parameters with the current arguments + (KernelParameters, KernelsScale) = GetParameters(); + Kernels = CreateComplexKernels(); + NormalizeKernels(); + + // Store them in the cache for future use + Cache.TryAdd(parameters, (KernelParameters, KernelsScale, Kernels)); + } + } + + /// + /// Gets the kernel scales to adjust the component values in each kernel. + /// + private static IReadOnlyList KernelScales { get; } = new[] { 1.4f, 1.2f, 1.2f, 1.2f, 1.2f, 1.2f }; + + /// + /// Gets the available bokeh blur kernel parameters. + /// + private static IReadOnlyList KernelComponents { get; } = new[] + { + // 1 component + new[] { new Vector4(0.862325f, 1.624835f, 0.767583f, 1.862321f) }, + + // 2 components + new[] + { + new Vector4(0.886528f, 5.268909f, 0.411259f, -0.548794f), + new Vector4(1.960518f, 1.558213f, 0.513282f, 4.56111f) + }, + + // 3 components + new[] + { + new Vector4(2.17649f, 5.043495f, 1.621035f, -2.105439f), + new Vector4(1.019306f, 9.027613f, -0.28086f, -0.162882f), + new Vector4(2.81511f, 1.597273f, -0.366471f, 10.300301f) + }, + + // 4 components + new[] + { + new Vector4(4.338459f, 1.553635f, -5.767909f, 46.164397f), + new Vector4(3.839993f, 4.693183f, 9.795391f, -15.227561f), + new Vector4(2.791880f, 8.178137f, -3.048324f, 0.302959f), + new Vector4(1.342190f, 12.328289f, 0.010001f, 0.244650f) + }, + + // 5 components + new[] + { + new Vector4(4.892608f, 1.685979f, -22.356787f, 85.91246f), + new Vector4(4.71187f, 4.998496f, 35.918936f, -28.875618f), + new Vector4(4.052795f, 8.244168f, -13.212253f, -1.578428f), + new Vector4(2.929212f, 11.900859f, 0.507991f, 1.816328f), + new Vector4(1.512961f, 16.116382f, 0.138051f, -0.01f) + }, + + // 6 components + new[] + { + new Vector4(5.143778f, 2.079813f, -82.326596f, 111.231024f), + new Vector4(5.612426f, 6.153387f, 113.878661f, 58.004879f), + new Vector4(5.982921f, 9.802895f, 39.479083f, -162.028887f), + new Vector4(6.505167f, 11.059237f, -71.286026f, 95.027069f), + new Vector4(3.869579f, 14.81052f, 1.405746f, -3.704914f), + new Vector4(2.201904f, 19.032909f, -0.152784f, -0.107988f) + } + }; + + /// + /// Gets the kernel parameters and scaling factor for the current count value in the current instance. + /// + private (Vector4[] Parameters, float Scale) GetParameters() + { + // Prepare the kernel components + int index = Math.Max(0, Math.Min(ComponentsCount - 1, KernelComponents.Count)); + return (KernelComponents[index], KernelScales[index]); + } + + /// + /// Creates the collection of complex 1D kernels with the specified parameters. + /// + private Complex64[][] CreateComplexKernels() + { + var kernels = new Complex64[KernelParameters.Length][]; + ref Vector4 baseRef = ref MemoryMarshal.GetReference(KernelParameters.AsSpan()); + for (int i = 0; i < KernelParameters.Length; i++) + { + ref Vector4 paramsRef = ref Unsafe.Add(ref baseRef, i); + kernels[i] = CreateComplex1DKernel(paramsRef.X, paramsRef.Y); + } + + return kernels; + } + + /// + /// Creates a complex 1D kernel with the specified parameters. + /// + /// The exponential parameter for each complex component. + /// The angle component for each complex component. + private Complex64[] CreateComplex1DKernel(float a, float b) + { + var kernel = new Complex64[KernelSize]; + ref Complex64 baseRef = ref MemoryMarshal.GetReference(kernel.AsSpan()); + int r = Radius, n = -r; + + for (int i = 0; i < KernelSize; i++, n++) + { + // Incrementally compute the range values + float value = n * KernelsScale * (1f / r); + value *= value; + + // Fill in the complex kernel values + Unsafe.Add(ref baseRef, i) = new Complex64( + MathF.Exp(-a * value) * MathF.Cos(b * value), + MathF.Exp(-a * value) * MathF.Sin(b * value)); + } + + return kernel; + } + + /// + /// Normalizes the kernels with respect to A * real + B * imaginary. + /// + private void NormalizeKernels() + { + // Calculate the complex weighted sum + float total = 0; + Span kernelsSpan = Kernels.AsSpan(); + ref Complex64[] baseKernelsRef = ref MemoryMarshal.GetReference(kernelsSpan); + ref Vector4 baseParamsRef = ref MemoryMarshal.GetReference(KernelParameters.AsSpan()); + + for (int i = 0; i < KernelParameters.Length; i++) + { + ref Complex64[] kernelRef = ref Unsafe.Add(ref baseKernelsRef, i); + int length = kernelRef.Length; + ref Complex64 valueRef = ref kernelRef[0]; + ref Vector4 paramsRef = ref Unsafe.Add(ref baseParamsRef, i); + + for (int j = 0; j < length; j++) { - ref Complex64 jRef = ref Unsafe.Add(ref valueRef, j); - ref Complex64 kRef = ref Unsafe.Add(ref valueRef, k); - total += - paramsRef.Z * (jRef.Real * kRef.Real - jRef.Imaginary * kRef.Imaginary) - + paramsRef.W * (jRef.Real * kRef.Imaginary + jRef.Imaginary * kRef.Real); + for (int k = 0; k < length; k++) + { + ref Complex64 jRef = ref Unsafe.Add(ref valueRef, j); + ref Complex64 kRef = ref Unsafe.Add(ref valueRef, k); + total += + paramsRef.Z * (jRef.Real * kRef.Real - jRef.Imaginary * kRef.Imaginary) + + paramsRef.W * (jRef.Real * kRef.Imaginary + jRef.Imaginary * kRef.Real); + } + } + } + + // Normalize the kernels + float scalar = 1f / MathF.Sqrt(total); + for (int i = 0; i < kernelsSpan.Length; i++) + { + ref Complex64[] kernelsRef = ref Unsafe.Add(ref baseKernelsRef, i); + int length = kernelsRef.Length; + ref Complex64 valueRef = ref kernelsRef[0]; + + for (int j = 0; j < length; j++) + { + Unsafe.Add(ref valueRef, j) *= scalar; } } } - // Normalize the kernels - float scalar = 1f / MathF.Sqrt(total); - for (int i = 0; i < kernelsSpan.Length; i++) + /// + protected override void OnFrameApply(ImageFrame source) { - ref Complex64[] kernelsRef = ref Unsafe.Add(ref baseKernelsRef, i); - int length = kernelsRef.Length; - ref Complex64 valueRef = ref kernelsRef[0]; - - for (int j = 0; j < length; j++) + if (!source.TryGetSinglePixelSpan(out Span pixelSpan)) { - Unsafe.Add(ref valueRef, j) *= scalar; + ThrowHelper.ThrowInvalidOperationException("Cannot process image frames wrapping discontiguous memory"); } - } - } - /// - protected override void OnFrameApply(ImageFrame source) - { - foreach (Memory memory in source.GetPixelMemoryGroup()) - { - // Preliminary gamma highlight pass - using IMemoryOwner source4 = GetExposedVector4Buffer(memory); + Span span = MemoryMarshal.Cast(pixelSpan); - using ReadOnlyBuffer sourceBuffer = Gpu.Default.AllocateReadOnlyBuffer(source4.Memory.Span); - using ReadWriteBuffer processingBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Length); - using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Length); + using ReadWriteTexture2D sourceTexture = Gpu.Default.AllocateReadWriteTexture2D(span, source.Width, source.Height); + using ReadWriteTexture2D processingTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D realTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); + using ReadWriteTexture2D imaginaryTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(KernelSize); - ref Vector4 param0 = ref KernelParameters[0]; // Avoid bounds check to access the kernel parameters + // Preliminary gamma highlight pass + ApplyGammaHighlight(sourceTexture); + + ref Vector4 param0 = ref MemoryMarshal.GetArrayDataReference(KernelParameters); // Perform two 1D convolutions for each component in the current instance for (int j = 0; j < Kernels.Length; j++) @@ -274,182 +274,231 @@ namespace ComputeSharp.BokehBlur.Processors kernelBuffer.SetData(Kernels[j]); Vector4 parameters = Unsafe.Add(ref param0, j); - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); - ApplyHorizontalConvolutionAndAccumulatePartials(firstPassBuffer, processingBuffer, kernelBuffer, parameters.Z, parameters.W); + ApplyVerticalConvolution(sourceTexture, realTexture, imaginaryTexture, kernelBuffer); + ApplyHorizontalConvolutionAndAccumulatePartials(realTexture, imaginaryTexture, processingTexture, kernelBuffer, parameters.Z, parameters.W); } // Apply the inverse gamma exposure pass - processingBuffer.GetData(source4.Memory.Span); + ApplyInverseGammaExposure(processingTexture, sourceTexture); // Write the final pixel data - ApplyInverseGammaExposure(source4.Memory, memory); + sourceTexture.GetData(span); } - } - /// - /// Performs a vertical 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyVerticalConvolution( - ReadOnlyBuffer source, - ReadWriteBuffer target, - ReadOnlyBuffer kernel) - { - int height = Source.Height; - int width = Source.Width; - - HlslBokehBlurProcessor.VerticalConvolutionProcessor shader = new( - width, - maxY: height - 1, - maxX: width - 1, - kernelLength: kernel.Length, - source, - target, - kernel); - - Gpu.Default.For(width, height, in shader); - } - - /// - /// Performs an horizontal 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - /// The weight factor for the real component of the complex pixel values. - /// The weight factor for the imaginary component of the complex pixel values. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyHorizontalConvolutionAndAccumulatePartials( - ReadWriteBuffer source, - ReadWriteBuffer target, - ReadOnlyBuffer kernel, - float z, - float w) - { - int height = Source.Height; - int width = Source.Width; - - HlslBokehBlurProcessor.HorizontalConvolutionAndAccumulatePartialsProcessor shader = new( - width, - maxY: height - 1, - maxX: width - 1, - kernelLength: kernel.Length, - z, - w, - source, - target, - kernel); - - Gpu.Default.For(width, height, in shader); - } - - /// - /// Applies the gamma correction/highlight to the input pixel buffer and returns an instance with values. - /// - /// The source image. - [Pure] - private IMemoryOwner GetExposedVector4Buffer(Memory source) - { - IMemoryOwner target = Configuration.MemoryAllocator.Allocate(source.Length); - - float exp = Gamma; - int width = Source.Width; - int height = source.Length / width; - - ParallelRowIterator.IterateRows( - Configuration, - new Rectangle(0, 0, width, height), - new GetExposedVector4BufferProcessor(width, exp, source, target.Memory, Configuration)); - - return target; - } - - /// - /// The processor for . - /// - [AutoConstructor] - private readonly partial struct GetExposedVector4BufferProcessor : IRowOperation - { - private readonly int width; - private readonly float exp; - - private readonly Memory source; - private readonly Memory target; - private readonly Configuration configuration; - - /// - public void Invoke(int y) + /// + /// Performs a vertical 1D complex convolution with the specified parameters. + /// + /// The source to read data from. + /// The target to write the real results to. + /// The target to write the imaginary results to. + /// The with the values for the current complex kernel. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ApplyVerticalConvolution( + ReadWriteTexture2D source, + ReadWriteTexture2D reals, + ReadWriteTexture2D imaginaries, + ReadOnlyBuffer kernel) { - int offset = y * width; + int height = Source.Height; + int width = Source.Width; - Span rowTPixel = source.Span.Slice(offset, width); - Span rowVector4 = target.Span.Slice(offset, width); + VerticalConvolutionProcessor shader = new( + maxY: height - 1, + maxX: width - 1, + kernelLength: kernel.Length, + source, + reals, + imaginaries, + kernel); - PixelOperations.Instance.ToVector4(configuration, rowTPixel, rowVector4); + Gpu.Default.For(width, height, in shader); + } - foreach (ref Vector4 v in rowVector4) + /// + /// Kernel for the vertical convolution pass. + /// + [AutoConstructor] + internal partial struct VerticalConvolutionProcessor : IComputeShader + { + public int maxY; + public int maxX; + public int kernelLength; + + public ReadWriteTexture2D source; + public ReadWriteTexture2D reals; + public ReadWriteTexture2D imaginaries; + public ReadOnlyBuffer kernel; + + /// + public void Execute(ThreadIds ids) { - v.X = MathF.Pow(v.X, exp); - v.Y = MathF.Pow(v.Y, exp); - v.Z = MathF.Pow(v.Z, exp); + Vector4 real = Vector4.Zero; + Vector4 imaginary = Vector4.Zero; + int radiusY = kernelLength >> 1; + int sourceOffsetColumnBase = ids.X; + + for (int i = 0; i < kernelLength; i++) + { + int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); + int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); + Vector4 color = source[offsetX, offsetY]; + Complex64 factors = kernel[i]; + + real += factors.Real * color; + imaginary += factors.Imaginary * color; + } + + reals[ids.XY] = real; + imaginaries[ids.XY] = imaginary; } } - } - /// - /// Applies the inverse gamma exposure pass to compute the final pixel values for a target image. - /// - /// The source buffer to read from. - /// The target image. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyInverseGammaExposure(Memory source, Memory target) - { - int width = Source.Width; - int height = source.Length / width; - float expGamma = 1 / Gamma; - - ParallelRowIterator.IterateRows( - Configuration, - new Rectangle(0, 0, width, height), - new ApplyInverseGammaExposureProcessor(width, expGamma, source, target, Configuration)); - } - - /// - /// The processor for . - /// - [AutoConstructor] - private readonly partial struct ApplyInverseGammaExposureProcessor : IRowOperation - { - private readonly int width; - private readonly float exp; - - private readonly Memory source; - private readonly Memory target; - private readonly Configuration configuration; - - /// - public void Invoke(int y) + /// + /// Performs an horizontal 1D complex convolution with the specified parameters. + /// + /// The source to read the real results from. + /// The source to read the imaginary results from. + /// The with the values for the current complex kernel. + /// The weight factor for the real component of the complex pixel values. + /// The weight factor for the imaginary component of the complex pixel values. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ApplyHorizontalConvolutionAndAccumulatePartials( + ReadWriteTexture2D reals, + ReadWriteTexture2D imaginaries, + ReadWriteTexture2D target, + ReadOnlyBuffer kernel, + float z, + float w) { - int offset = y * width; + int height = Source.Height; + int width = Source.Width; - Span rowVector4 = source.Span.Slice(offset, width); - Span rowTPixel = target.Span.Slice(offset, width); + HorizontalConvolutionAndAccumulatePartialsProcessor shader = new( + maxY: height - 1, + maxX: width - 1, + kernelLength: kernel.Length, + z, + w, + reals, + imaginaries, + target, + kernel); - Vector4 low = Vector4.Zero; - var high = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); + Gpu.Default.For(width, height, in shader); + } - foreach (ref Vector4 v in rowVector4) + /// + /// Kernel for the horizontal convolution pass. + /// + [AutoConstructor] + internal partial struct HorizontalConvolutionAndAccumulatePartialsProcessor : IComputeShader + { + public int maxY; + public int maxX; + public int kernelLength; + public float z; + public float w; + + public ReadWriteTexture2D reals; + public ReadWriteTexture2D imaginaries; + public ReadWriteTexture2D target; + public ReadOnlyBuffer kernel; + + /// + public void Execute(ThreadIds ids) { - var clamp = Vector4.Clamp(v, low, high); - v.X = MathF.Pow(clamp.X, exp); - v.Y = MathF.Pow(clamp.Y, exp); - v.Z = MathF.Pow(clamp.Z, exp); - } + Vector4 real = Vector4.Zero; + Vector4 imaginary = Vector4.Zero; + int radiusX = kernelLength >> 1; + int sourceOffsetColumnBase = ids.X; + int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); - PixelOperations.Instance.FromVector4Destructive(configuration, rowVector4, rowTPixel); + for (int i = 0; i < kernelLength; i++) + { + int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); + Vector4 sourceReal = reals[offsetX, offsetY]; + Vector4 sourceImaginary = imaginaries[offsetX, offsetY]; + Complex64 factors = kernel[i]; + + real += factors.Real * sourceReal - factors.Imaginary * sourceImaginary; + imaginary += factors.Real * sourceImaginary + factors.Imaginary * sourceReal; + } + + target[ids.XY] += real * z + imaginary * w; + } + } + + /// + /// Applies the gamma highlight pass to compute the final pixel values for a target image. + /// + /// The source instance to modify. + [Pure] + private void ApplyGammaHighlight(ReadWriteTexture2D source) + { + Gpu.Default.For(source.Height, new GammaHighlightProcessor(source.Width, source)); + } + + /// + /// Kernel for the gamma highlight pass. + /// + [AutoConstructor] + internal readonly partial struct GammaHighlightProcessor : IComputeShader + { + public readonly int width; + + public readonly ReadWriteTexture2D source; + + /// + public void Execute(ThreadIds ids) + { + for (int i = 0; i < width; i++) + { + Float4 v = source[i, ids.X]; + + v.XYZ = v.XYZ * v.XYZ * v.XYZ; + + source[i, ids.X] = v; + } + } + } + + /// + /// Applies the inverse gamma highlight pass to compute the final pixel values for a target image. + /// + /// The source instance to read from. + /// The target instance to write to. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ApplyInverseGammaExposure( + ReadWriteTexture2D source, + ReadWriteTexture2D target) + { + Gpu.Default.For(source.Height, new InverseGammaHighlightProcessor(source.Width, source, target)); + } + + /// + /// Kernel for the inverse gamma highlight pass. + /// + [AutoConstructor] + internal readonly partial struct InverseGammaHighlightProcessor : IComputeShader + { + public readonly int width; + + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; + + /// + public void Execute(ThreadIds ids) + { + for (int i = 0; i < width; i++) + { + Float4 v = source[i, ids.X]; + + v.XYZ = Hlsl.Clamp(v.XYZ, 0, float.MaxValue); + v.XYZ = Hlsl.Pow(v.XYZ, 1 / 3f); + + target[i, ids.X] = v; + } + } } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs index b0d22c81..962db9cf 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor.cs @@ -1,7 +1,9 @@ -using System.Numerics; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Diagnostics; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.BokehBlur.Processors { @@ -51,81 +53,14 @@ namespace ComputeSharp.BokehBlur.Processors public IImageProcessor CreatePixelSpecificProcessor(Configuration configuration, Image source, Rectangle sourceRectangle) where TPixel : unmanaged, IPixel { - return new HlslGaussianBlurProcessor(this, configuration, source, sourceRectangle); - } - - /// - /// Kernel for the vertical convolution pass. - /// - [AutoConstructor] - internal readonly partial struct VerticalConvolutionProcessor : IComputeShader - { - public readonly int width; - public readonly int maxY; - public readonly int maxX; - public readonly int kernelLength; - - public readonly ReadWriteBuffer source; - public readonly ReadWriteBuffer target; - public readonly ReadOnlyBuffer kernel; - - /// - public void Execute(ThreadIds ids) + if (typeof(TPixel) != typeof(ImageSharpRgba32)) { - Vector4 result = Vector4.Zero; - int radiusY = kernelLength >> 1; - int sourceOffsetColumnBase = ids.X; - - for (int i = 0; i < kernelLength; i++) - { - int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); - int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); - Vector4 color = source[offsetY * width + offsetX]; - - result += kernel[i] * color; - } - - int offsetXY = ids.Y * width + ids.X; - target[offsetXY] = result; + ThrowHelper.ThrowInvalidOperationException("This processor only supports the RGBA32 pixel format"); } - } - /// - /// Kernel for the horizontal convolution pass. - /// - [AutoConstructor] - internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader - { - public readonly int width; - public readonly int maxY; - public readonly int maxX; - public readonly int kernelLength; + var processor = new Implementation(this, configuration, Unsafe.As>(source), sourceRectangle); - public readonly ReadWriteBuffer source; - public readonly ReadWriteBuffer target; - public readonly ReadOnlyBuffer kernel; - - /// - public void Execute(ThreadIds ids) - { - Vector4 result = Vector4.Zero; - int radiusX = kernelLength >> 1; - int sourceOffsetColumnBase = ids.X; - int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); - int offsetXY; - - for (int i = 0; i < kernelLength; i++) - { - int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); - offsetXY = offsetY * width + offsetX; - Vector4 color = source[offsetXY]; - - result += kernel[i] * color; - } - - offsetXY = ids.Y * width + ids.X; - target[offsetXY] = result; - } + return Unsafe.As>(processor); } } } diff --git a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs index 4ea8671c..5f1fa2f5 100644 --- a/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs +++ b/samples/ComputeSharp.ImageProcessing/Processors/HlslGaussianBlurProcessor{TPixel}.cs @@ -1,166 +1,228 @@ using System; -using System.Buffers; using System.Numerics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Microsoft.Toolkit.Diagnostics; using SixLabors.ImageSharp; -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.BokehBlur.Processors { - /// - /// Applies Gaussian blur processing to an image. - /// - /// The pixel format. - internal sealed class HlslGaussianBlurProcessor : ImageProcessor - where TPixel : unmanaged, IPixel + /// + public sealed partial class HlslGaussianBlurProcessor { /// - /// The 1D kernel to apply. + /// Applies Gaussian blur processing to an image. /// - private readonly float[] Kernel; - - /// - /// Initializes a new instance of the class. - /// - /// The defining the processor parameters. - /// The configuration which allows altering default behaviour or extending the library. - /// The source for the current processor instance. - /// The source area to process for the current processor instance. - public HlslGaussianBlurProcessor(HlslGaussianBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle) - : base(configuration, source, sourceRectangle) + internal sealed partial class Implementation : ImageProcessor { - int kernelSize = definition.Radius * 2 + 1; - Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma); - } + /// + /// The 1D kernel to apply. + /// + private readonly float[] Kernel; - /// - /// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function. - /// - private static float[] CreateGaussianBlurKernel(int size, float weight) - { - float[] kernel = new float[size]; - ref float rKernel = ref kernel[0]; - - float sum = 0F; - float midpoint = (size - 1) / 2F; - - for (int i = 0; i < size; i++) + /// + /// Initializes a new instance of the class. + /// + /// The defining the processor parameters. + /// The configuration which allows altering default behaviour or extending the library. + /// The source for the current processor instance. + /// The source area to process for the current processor instance. + public Implementation(HlslGaussianBlurProcessor definition, Configuration configuration, Image source, Rectangle sourceRectangle) + : base(configuration, source, sourceRectangle) { - float x = i - midpoint; - float gx = Gaussian(x, weight); - sum += gx; - Unsafe.Add(ref rKernel, i) = gx; + int kernelSize = definition.Radius * 2 + 1; + Kernel = CreateGaussianBlurKernel(kernelSize, definition.Sigma); } - // Normalize kernel so that the sum of all weights equals 1 - for (int i = 0; i < size; i++) + /// + /// Creates a 1 dimensional Gaussian kernel using the Gaussian G(x) function. + /// + private static float[] CreateGaussianBlurKernel(int size, float weight) { - Unsafe.Add(ref rKernel, i) /= sum; + float[] kernel = new float[size]; + ref float rKernel = ref kernel[0]; + + float sum = 0F; + float midpoint = (size - 1) / 2F; + + for (int i = 0; i < size; i++) + { + float x = i - midpoint; + float gx = Gaussian(x, weight); + sum += gx; + Unsafe.Add(ref rKernel, i) = gx; + } + + // Normalize kernel so that the sum of all weights equals 1 + for (int i = 0; i < size; i++) + { + Unsafe.Add(ref rKernel, i) /= sum; + } + + return kernel; } - return kernel; - } - - /// - /// Implementation of 1D Gaussian G(x) function. - /// - /// The x provided to G(x) - /// The spread of the blur. - /// The Gaussian G(x) - private static float Gaussian(float x, float sigma) - { - const float Numerator = 1.0f; - float denominator = MathF.Sqrt(2 * MathF.PI) * sigma; - - float exponentNumerator = -x * x; - float exponentDenominator = 2 * sigma * sigma; - - float left = Numerator / denominator; - float right = MathF.Exp(exponentNumerator / exponentDenominator); - - return left * right; - } - - /// - protected override void OnFrameApply(ImageFrame source) - { - foreach (Memory memory in source.GetPixelMemoryGroup()) + /// + /// Implementation of 1D Gaussian G(x) function. + /// + /// The x provided to G(x) + /// The spread of the blur. + /// The Gaussian G(x) + private static float Gaussian(float x, float sigma) { - Span spanTPixel = memory.Span; + const float Numerator = 1.0f; + float denominator = MathF.Sqrt(2 * MathF.PI) * sigma; - using IMemoryOwner group4 = Configuration.MemoryAllocator.Allocate(spanTPixel.Length); + float exponentNumerator = -x * x; + float exponentDenominator = 2 * sigma * sigma; - Span spanVector4 = group4.Memory.Span; + float left = Numerator / denominator; + float right = MathF.Exp(exponentNumerator / exponentDenominator); - PixelOperations.Instance.ToVector4(Configuration, spanTPixel, spanVector4); + return left * right; + } - using ReadWriteBuffer sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(spanVector4); - using ReadWriteBuffer firstPassBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer.Length); + /// + protected override void OnFrameApply(ImageFrame source) + { + if (!source.TryGetSinglePixelSpan(out Span pixelSpan)) + { + ThrowHelper.ThrowInvalidOperationException("Cannot process image frames wrapping discontiguous memory"); + } + + Span span = MemoryMarshal.Cast(pixelSpan); + + using ReadWriteTexture2D sourceTexture = Gpu.Default.AllocateReadWriteTexture2D(span, source.Width, source.Height); + using ReadWriteTexture2D firstPassTexture = Gpu.Default.AllocateReadWriteTexture2D(source.Width, source.Height); using ReadOnlyBuffer kernelBuffer = Gpu.Default.AllocateReadOnlyBuffer(Kernel); - ApplyVerticalConvolution(sourceBuffer, firstPassBuffer, kernelBuffer); - ApplyHorizontalConvolution(firstPassBuffer, sourceBuffer, kernelBuffer); + ApplyVerticalConvolution(sourceTexture, firstPassTexture, kernelBuffer); + ApplyHorizontalConvolution(firstPassTexture, sourceTexture, kernelBuffer); - sourceBuffer.GetData(spanVector4); - - PixelOperations.Instance.FromVector4Destructive(Configuration, spanVector4, spanTPixel); + sourceTexture.GetData(span); } - } - /// - /// Performs a vertical 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyVerticalConvolution( - ReadWriteBuffer source, - ReadWriteBuffer target, - ReadOnlyBuffer kernel) - { - int height = Source.Height; - int width = Source.Width; + /// + /// Performs a vertical 1D complex convolution with the specified parameters. + /// + /// The source to read data from. + /// The target to write the results to. + /// The with the values for the current complex kernel. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ApplyVerticalConvolution( + ReadWriteTexture2D source, + ReadWriteTexture2D target, + ReadOnlyBuffer kernel) + { + int height = Source.Height; + int width = Source.Width; - HlslGaussianBlurProcessor.VerticalConvolutionProcessor shader = new( - width, - maxY: height - 1, - maxX: width - 1, - kernel.Length, - source, - target, - kernel); + VerticalConvolutionProcessor shader = new( + maxY: height - 1, + maxX: width - 1, + kernel.Length, + source, + target, + kernel); - Gpu.Default.For(width, height, in shader); - } + Gpu.Default.For(width, height, in shader); + } - /// - /// Performs an horizontal 1D complex convolution with the specified parameters. - /// - /// The source to read data from. - /// The target to write the results to. - /// The with the values for the current complex kernel. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyHorizontalConvolution( - ReadWriteBuffer source, - ReadWriteBuffer target, - ReadOnlyBuffer kernel) - { - int height = Source.Height; - int width = Source.Width; + /// + /// Kernel for the vertical convolution pass. + /// + [AutoConstructor] + internal readonly partial struct VerticalConvolutionProcessor : IComputeShader + { + public readonly int maxY; + public readonly int maxX; + public readonly int kernelLength; - HlslGaussianBlurProcessor.HorizontalConvolutionProcessor shader = new( - width, - maxY: height - 1, - maxX: width - 1, - kernel.Length, - source, - target, - kernel); + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; + public readonly ReadOnlyBuffer kernel; - Gpu.Default.For(width, height, in shader); + /// + public void Execute(ThreadIds ids) + { + Vector4 result = Vector4.Zero; + int radiusY = kernelLength >> 1; + int sourceOffsetColumnBase = ids.X; + + for (int i = 0; i < kernelLength; i++) + { + int offsetY = Hlsl.Clamp(ids.Y + i - radiusY, 0, maxY); + int offsetX = Hlsl.Clamp(sourceOffsetColumnBase, 0, maxX); + Vector4 color = source[offsetX, offsetY]; + + result += kernel[i] * color; + } + + target[ids.XY] = result; + } + } + + /// + /// Performs an horizontal 1D complex convolution with the specified parameters. + /// + /// The source to read data from. + /// The target to write the results to. + /// The with the values for the current complex kernel. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void ApplyHorizontalConvolution( + ReadWriteTexture2D source, + ReadWriteTexture2D target, + ReadOnlyBuffer kernel) + { + int height = Source.Height; + int width = Source.Width; + + HorizontalConvolutionProcessor shader = new( + maxY: height - 1, + maxX: width - 1, + kernel.Length, + source, + target, + kernel); + + Gpu.Default.For(width, height, in shader); + } + + /// + /// Kernel for the horizontal convolution pass. + /// + [AutoConstructor] + internal readonly partial struct HorizontalConvolutionProcessor : IComputeShader + { + public readonly int maxY; + public readonly int maxX; + public readonly int kernelLength; + + public readonly ReadWriteTexture2D source; + public readonly ReadWriteTexture2D target; + public readonly ReadOnlyBuffer kernel; + + /// + public void Execute(ThreadIds ids) + { + Vector4 result = Vector4.Zero; + int radiusX = kernelLength >> 1; + int sourceOffsetColumnBase = ids.X; + int offsetY = Hlsl.Clamp(ids.Y, 0, maxY); + + for (int i = 0; i < kernelLength; i++) + { + int offsetX = Hlsl.Clamp(sourceOffsetColumnBase + i - radiusX, 0, maxX); + Vector4 color = source[offsetX, offsetY]; + + result += kernel[i] * color; + } + + target[ids.XY] = result; + } + } } } } diff --git a/samples/ComputeSharp.ImageProcessing/Program.cs b/samples/ComputeSharp.ImageProcessing/Program.cs index 91f52ca5..9924b200 100644 --- a/samples/ComputeSharp.ImageProcessing/Program.cs +++ b/samples/ComputeSharp.ImageProcessing/Program.cs @@ -5,6 +5,7 @@ using ComputeSharp.BokehBlur.Processors; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors; +using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; namespace ComputeSharp.ImageProcessing { @@ -14,12 +15,12 @@ namespace ComputeSharp.ImageProcessing { Console.WriteLine(">> Loading image"); string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "city.jpg"); - using Image image = Image.Load(path); + using Image image = Image.Load(path); // Apply a series of processors and save the results foreach (var effect in new (string Name, IImageProcessor Processor)[] { - ("bokeh", new HlslBokehBlurProcessor(80, 2, 3)), + ("bokeh", new HlslBokehBlurProcessor(80, 2)), ("gaussian", new HlslGaussianBlurProcessor(80)) }) { diff --git a/src/ComputeSharp.Core/ComputeSharp.Core.csproj b/src/ComputeSharp.Core/ComputeSharp.Core.csproj index 85cab145..e7c12eb8 100644 --- a/src/ComputeSharp.Core/ComputeSharp.Core.csproj +++ b/src/ComputeSharp.Core/ComputeSharp.Core.csproj @@ -8,6 +8,7 @@ + diff --git a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs index 7e5e0fcb..c58a3301 100644 --- a/src/ComputeSharp.Core/Intrinsics/Hlsl.cs +++ b/src/ComputeSharp.Core/Intrinsics/Hlsl.cs @@ -1,5 +1,4 @@ using System.Diagnostics.Contracts; -using System.Numerics; using ComputeSharp.Core.Intrinsics.Attributes; using ComputeSharp.Exceptions; @@ -74,30 +73,6 @@ namespace ComputeSharp [Pure] public static Float4 Abs(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Abs)}({nameof(Float4)})"); - /// - /// Returns the absolute value of the specified value. - /// - /// The specified value. - /// The absolute value of the parameter. - [Pure] - public static Vector2 Abs(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Abs)}({nameof(Vector2)})"); - - /// - /// Returns the absolute value of the specified value. - /// - /// The specified value. - /// The absolute value of the parameter. - [Pure] - public static Vector3 Abs(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Abs)}({nameof(Vector3)})"); - - /// - /// Returns the absolute value of the specified value. - /// - /// The specified value. - /// The absolute value of the parameter. - [Pure] - public static Vector4 Abs(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Abs)}({nameof(Vector4)})"); - /// /// Returns the arccosine of the specified value. /// @@ -130,30 +105,6 @@ namespace ComputeSharp [Pure] public static Float3 Acos(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Acos)}({nameof(Float4)})"); - /// - /// Returns the arccosine of the specified value. - /// - /// The specified value, where each component should be within the range [-1 to 1] - /// The arccosine of the parameter. - [Pure] - public static Vector2 Acos(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Acos)}({nameof(Vector2)})"); - - /// - /// Returns the arccosine of the specified value. - /// - /// The specified value, where each component should be within the range [-1 to 1] - /// The arccosine of the parameter. - [Pure] - public static Vector3 Acos(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Acos)}({nameof(Vector3)})"); - - /// - /// Returns the arccosine of the specified value. - /// - /// The specified value, where each component should be within the range [-1 to 1] - /// The arccosine of the parameter. - [Pure] - public static Vector3 Acos(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Acos)}({nameof(Vector4)})"); - /// /// Determines if all components of the specified value are non-zero. /// @@ -202,30 +153,6 @@ namespace ComputeSharp [Pure] public static bool All(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(All)}({nameof(Float4)})"); - /// - /// Determines if all components of the specified value are non-zero. - /// - /// The specified value. - /// if all components of the parameter are non-zero; otherwise, . - [Pure] - public static bool All(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(All)}({nameof(Vector2)})"); - - /// - /// Determines if all components of the specified value are non-zero. - /// - /// The specified value. - /// if all components of the parameter are non-zero; otherwise, . - [Pure] - public static bool All(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(All)}({nameof(Vector3)})"); - - /// - /// Determines if all components of the specified value are non-zero. - /// - /// The specified value. - /// if all components of the parameter are non-zero; otherwise, . - [Pure] - public static bool All(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(All)}({nameof(Vector4)})"); - /// /// Determines if any components of the specified value are non-zero. /// @@ -274,30 +201,6 @@ namespace ComputeSharp [Pure] public static bool Any(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Any)}({nameof(Float4)})"); - /// - /// Determines if any components of the specified value are non-zero. - /// - /// The specified value. - /// if any components of the parameter are non-zero; otherwise, . - [Pure] - public static bool Any(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Any)}({nameof(Vector2)})"); - - /// - /// Determines if any components of the specified value are non-zero. - /// - /// The specified value. - /// if any components of the parameter are non-zero; otherwise, . - [Pure] - public static bool Any(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Any)}({nameof(Vector3)})"); - - /// - /// Determines if any components of the specified value are non-zero. - /// - /// The specified value. - /// if any components of the parameter are non-zero; otherwise, . - [Pure] - public static bool Any(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Any)}({nameof(Vector4)})"); - /// /// Reinterprets a cast value (two 32-bit values) into a . /// @@ -412,30 +315,6 @@ namespace ComputeSharp [Pure] public static Float3 Asin(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Asin)}({nameof(Float4)})"); - /// - /// Returns the arcsine of the specified value. - /// - /// The specified value, where each component should be within the range [-1 to 1] - /// The arcsine of the parameter. - [Pure] - public static Vector2 Asin(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Asin)}({nameof(Vector2)})"); - - /// - /// Returns the arcsine of the specified value. - /// - /// The specified value, where each component should be within the range [-1 to 1] - /// The arcsine of the parameter. - [Pure] - public static Vector3 Asin(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Asin)}({nameof(Vector3)})"); - - /// - /// Returns the arcsine of the specified value. - /// - /// The specified value, where each component should be within the range [-1 to 1] - /// The arcsine of the parameter. - [Pure] - public static Vector3 Asin(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Asin)}({nameof(Vector4)})"); - /// /// Returns the arctangent of the specified value. /// @@ -468,30 +347,6 @@ namespace ComputeSharp [Pure] public static Float3 Atan(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan)}({nameof(Float4)})"); - /// - /// Returns the arctangent of the specified value. - /// - /// The specified value. - /// The arctangent of the parameter. - [Pure] - public static Vector2 Atan(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan)}({nameof(Vector2)})"); - - /// - /// Returns the arctangent of the specified value. - /// - /// The specified value. - /// The arctangent of the parameter. - [Pure] - public static Vector3 Atan(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan)}({nameof(Vector3)})"); - - /// - /// Returns the arctangent of the specified value. - /// - /// The specified value. - /// The arctangent of the parameter. - [Pure] - public static Vector3 Atan(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan)}({nameof(Vector4)})"); - /// /// Returns the arctangent of two values (,) /// @@ -528,33 +383,6 @@ namespace ComputeSharp [Pure] public static Float3 Atan2(Float4 y, Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan2)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns the arctangent of two values (,) - /// - /// The value. - /// The value. - /// The arctangent of (,) - [Pure] - public static Vector2 Atan2(Vector2 y, Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan2)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns the arctangent of two values (,) - /// - /// The value. - /// The value. - /// The arctangent of (,) - [Pure] - public static Vector3 Atan2(Vector3 y, Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan2)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns the arctangent of two values (,) - /// - /// The value. - /// The value. - /// The arctangent of (,) - [Pure] - public static Vector3 Atan2(Vector4 y, Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Atan2)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the smallest integer value that is greater than or equal to the specified value. /// @@ -587,30 +415,6 @@ namespace ComputeSharp [Pure] public static Float3 Ceil(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ceil)}({nameof(Float4)})"); - /// - /// Returns the smallest integer value that is greater than or equal to the specified value. - /// - /// The specified value. - /// The smallest integer value (returned as a floating-point type) that is greater than or equal to the parameter. - [Pure] - public static Vector2 Ceil(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ceil)}({nameof(Vector2)})"); - - /// - /// Returns the smallest integer value that is greater than or equal to the specified value. - /// - /// The specified value. - /// The smallest integer value (returned as a floating-point type) that is greater than or equal to the parameter. - [Pure] - public static Vector3 Ceil(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ceil)}({nameof(Vector3)})"); - - /// - /// Returns the smallest integer value that is greater than or equal to the specified value. - /// - /// The specified value. - /// The smallest integer value (returned as a floating-point type) that is greater than or equal to the parameter. - [Pure] - public static Vector3 Ceil(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ceil)}({nameof(Vector4)})"); - /// /// Clamps the specified value to the specified minimum and maximum range. /// @@ -699,39 +503,6 @@ namespace ComputeSharp [Pure] public static Float4 Clamp(Float4 x, Float4 min, Float4 max) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Clamp)}({nameof(Float4)},{nameof(Float4)},{nameof(Float4)})"); - /// - /// Clamps the specified value to the specified minimum and maximum range. - /// - /// A value to clamp. - /// The specified minimum range. - /// The specified maximum range. - /// The clamped value for the x parameter. - /// For values of -INF or INF, clamp will behave as expected. However for values of NaN, the results are undefined. - [Pure] - public static Vector2 Clamp(Vector2 x, Vector2 min, Vector2 max) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Clamp)}({nameof(Vector2)},{nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Clamps the specified value to the specified minimum and maximum range. - /// - /// A value to clamp. - /// The specified minimum range. - /// The specified maximum range. - /// The clamped value for the x parameter. - /// For values of -INF or INF, clamp will behave as expected. However for values of NaN, the results are undefined. - [Pure] - public static Vector3 Clamp(Vector3 x, Vector3 min, Vector3 max) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Clamp)}({nameof(Vector3)},{nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Clamps the specified value to the specified minimum and maximum range. - /// - /// A value to clamp. - /// The specified minimum range. - /// The specified maximum range. - /// The clamped value for the x parameter. - /// For values of -INF or INF, clamp will behave as expected. However for values of NaN, the results are undefined. - [Pure] - public static Vector4 Clamp(Vector4 x, Vector4 min, Vector4 max) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Clamp)}({nameof(Vector4)},{nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the cosine of the specified value. /// @@ -764,30 +535,6 @@ namespace ComputeSharp [Pure] public static Float3 Cos(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cos)}({nameof(Float4)})"); - /// - /// Returns the cosine of the specified value. - /// - /// The specified value, radians. - /// The cosine of the parameter. - [Pure] - public static Vector2 Cos(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cos)}({nameof(Vector2)})"); - - /// - /// Returns the cosine of the specified value. - /// - /// The specified value, radians. - /// The cosine of the parameter. - [Pure] - public static Vector3 Cos(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cos)}({nameof(Vector3)})"); - - /// - /// Returns the cosine of the specified value. - /// - /// The specified value, radians. - /// The cosine of the parameter. - [Pure] - public static Vector3 Cos(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cos)}({nameof(Vector4)})"); - /// /// Returns the hyperbolic cosine of the specified value. /// @@ -820,30 +567,6 @@ namespace ComputeSharp [Pure] public static Float3 Cosh(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cosh)}({nameof(Float4)})"); - /// - /// Returns the hyperbolic cosine of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic cosine of the parameter. - [Pure] - public static Vector2 Cosh(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cosh)}({nameof(Vector2)})"); - - /// - /// Returns the hyperbolic cosine of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic cosine of the parameter. - [Pure] - public static Vector3 Cosh(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cosh)}({nameof(Vector3)})"); - - /// - /// Returns the hyperbolic cosine of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic cosine of the parameter. - [Pure] - public static Vector3 Cosh(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cosh)}({nameof(Vector4)})"); - /// /// Counts the number of bits in the input value. /// @@ -861,15 +584,6 @@ namespace ComputeSharp [Pure] public static Float3 Cross(Float3 x, Float3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cross)}({nameof(Float3)})"); - /// - /// Returns the cross product of two values. - /// - /// The first vector. - /// The second vector. - /// The cross product of the parameter and the parameter. - [Pure] - public static Vector3 Cross(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Cross)}({nameof(Vector3)})"); - /// /// Converts the specified value from radians to degrees. /// @@ -902,30 +616,6 @@ namespace ComputeSharp [Pure] public static Float3 Degrees(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Degrees)}({nameof(Float4)})"); - /// - /// Converts the specified value from radians to degrees. - /// - /// The specified value. - /// The result of converting the parameter from radians to degrees. - [Pure] - public static Vector2 Degrees(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Degrees)}({nameof(Vector2)})"); - - /// - /// Converts the specified value from radians to degrees. - /// - /// The specified value. - /// The result of converting the parameter from radians to degrees. - [Pure] - public static Vector3 Degrees(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Degrees)}({nameof(Vector3)})"); - - /// - /// Converts the specified value from radians to degrees. - /// - /// The specified value. - /// The result of converting the parameter from radians to degrees. - [Pure] - public static Vector3 Degrees(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Degrees)}({nameof(Vector4)})"); - /// /// Returns a distance scalar between two values. /// @@ -953,33 +643,6 @@ namespace ComputeSharp [Pure] public static float Distance(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Distance)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns a distance scalar between two values. - /// - /// The first value to compare. - /// The second value to compare. - /// A scalar value that represents the distance between the parameter and the parameter. - [Pure] - public static float Distance(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Distance)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns a distance scalar between two values. - /// - /// The first value to compare. - /// The second value to compare. - /// A scalar value that represents the distance between the parameter and the parameter. - [Pure] - public static float Distance(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Distance)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns a distance scalar between two values. - /// - /// The first value to compare. - /// The second value to compare. - /// A scalar value that represents the distance between the parameter and the parameter. - [Pure] - public static float Distance(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Distance)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the dot product of two values. /// @@ -1007,33 +670,6 @@ namespace ComputeSharp [Pure] public static float Dot(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Dot)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns the dot product of two values. - /// - /// The first vector. - /// The second vector. - /// The dot product of the parameter and the parameter. - [Pure] - public static float Dot(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Dot)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns the dot product of two values. - /// - /// The first vector. - /// The second vector. - /// The dot product of the parameter and the parameter. - [Pure] - public static float Dot(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Dot)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns the dot product of two values. - /// - /// The first vector. - /// The second vector. - /// The dot product of the parameter and the parameter. - [Pure] - public static float Dot(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Dot)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the base-e exponential of the specified value. /// @@ -1066,30 +702,6 @@ namespace ComputeSharp [Pure] public static Float4 Exp(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp)}({nameof(Float4)})"); - /// - /// Returns the base-e exponential of the specified value. - /// - /// The specified value. - /// The base-e exponential of the parameter. - [Pure] - public static Vector2 Exp(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp)}({nameof(Vector2)})"); - - /// - /// Returns the base-e exponential of the specified value. - /// - /// The specified value. - /// The base-e exponential of the parameter. - [Pure] - public static Vector3 Exp(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp)}({nameof(Vector3)})"); - - /// - /// Returns the base-e exponential of the specified value. - /// - /// The specified value. - /// The base-e exponential of the parameter. - [Pure] - public static Vector4 Exp(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp)}({nameof(Vector4)})"); - /// /// Returns the base-2 exponential of the specified value. /// @@ -1122,30 +734,6 @@ namespace ComputeSharp [Pure] public static Float4 Exp2(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp2)}({nameof(Float4)})"); - /// - /// Returns the base-2 exponential of the specified value. - /// - /// The specified value. - /// The base-2 exponential of the parameter. - [Pure] - public static Vector2 Exp2(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp2)}({nameof(Vector2)})"); - - /// - /// Returns the base-2 exponential of the specified value. - /// - /// The specified value. - /// The base-2 exponential of the parameter. - [Pure] - public static Vector3 Exp2(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp2)}({nameof(Vector3)})"); - - /// - /// Returns the base-2 exponential of the specified value. - /// - /// The specified value. - /// The base-2 exponential of the parameter. - [Pure] - public static Vector4 Exp2(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Exp2)}({nameof(Vector4)})"); - /// /// Returns the largest integer that is less than or equal to the specified value. /// @@ -1178,30 +766,6 @@ namespace ComputeSharp [Pure] public static Float4 Floor(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Floor)}({nameof(Float4)})"); - /// - /// Returns the largest integer that is less than or equal to the specified value. - /// - /// The specified value. - /// The largest integer value (returned as a floating-point type) that is less than or equal to the parameter. - [Pure] - public static Vector2 Floor(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Floor)}({nameof(Vector2)})"); - - /// - /// Returns the largest integer that is less than or equal to the specified value. - /// - /// The specified value. - /// The largest integer value (returned as a floating-point type) that is less than or equal to the parameter. - [Pure] - public static Vector3 Floor(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Floor)}({nameof(Vector3)})"); - - /// - /// Returns the largest integer that is less than or equal to the specified value. - /// - /// The specified value. - /// The largest integer value (returned as a floating-point type) that is less than or equal to the parameter. - [Pure] - public static Vector4 Floor(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Floor)}({nameof(Vector4)})"); - /// /// Returns the floating-point remainder of /. /// @@ -1238,33 +802,6 @@ namespace ComputeSharp [Pure] public static Float4 Fmod(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Fmod)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns the floating-point remainder of /. - /// - /// The divident. - /// The divisor. - /// The floating-point remainder of the parameter divided by the parameter. - [Pure] - public static Vector2 Fmod(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Fmod)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns the floating-point remainder of /. - /// - /// The divident. - /// The divisor. - /// The floating-point remainder of the parameter divided by the parameter. - [Pure] - public static Vector3 Fmod(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Fmod)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns the floating-point remainder of /. - /// - /// The divident. - /// The divisor. - /// The floating-point remainder of the parameter divided by the parameter. - [Pure] - public static Vector4 Fmod(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Fmod)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the fractional (or decimal) part of , which is greater than or equal to 0 and less than 1. /// @@ -1297,30 +834,6 @@ namespace ComputeSharp [Pure] public static Float4 Frac(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frac)}({nameof(Float4)})"); - /// - /// Returns the fractional (or decimal) part of , which is greater than or equal to 0 and less than 1. - /// - /// The specified value. - /// The fractional part of the parameter. - [Pure] - public static Vector2 Frac(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frac)}({nameof(Vector2)})"); - - /// - /// Returns the fractional (or decimal) part of , which is greater than or equal to 0 and less than 1. - /// - /// The specified value. - /// The fractional part of the parameter. - [Pure] - public static Vector3 Frac(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frac)}({nameof(Vector3)})"); - - /// - /// Returns the fractional (or decimal) part of , which is greater than or equal to 0 and less than 1. - /// - /// The specified value. - /// The fractional part of the parameter. - [Pure] - public static Vector4 Frac(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frac)}({nameof(Vector4)})"); - /// /// Returns the mantissa and exponent of the specified floating-point value. /// @@ -1357,33 +870,6 @@ namespace ComputeSharp [Pure] public static Float4 Frexp(Float4 x, out Float4 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frexp)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns the mantissa and exponent of the specified floating-point value. - /// - /// The specified floating-point value (if the x parameter is 0, this function returns 0 for both the mantissa and the exponent) - /// The returned exponent of the parameter. - /// The fractional part of the parameter. - [Pure] - public static Vector2 Frexp(Vector2 x, out Vector2 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frexp)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns the mantissa and exponent of the specified floating-point value. - /// - /// The specified floating-point value (if the x parameter is 0, this function returns 0 for both the mantissa and the exponent) - /// The returned exponent of the parameter. - /// The fractional part of the parameter. - [Pure] - public static Vector3 Frexp(Vector3 x, out Vector3 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frexp)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns the mantissa and exponent of the specified floating-point value. - /// - /// The specified floating-point value (if the x parameter is 0, this function returns 0 for both the mantissa and the exponent) - /// The returned exponent of the parameter. - /// The fractional part of the parameter. - [Pure] - public static Vector4 Frexp(Vector4 x, out Vector4 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Frexp)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Performs a guaranteed atomic add of a value to a destination. /// @@ -1708,33 +1194,6 @@ namespace ComputeSharp [Pure] public static Float4 Ldexp(Float4 x, Float4 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ldexp)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns the result of multiplying the specified value by two, raised to the power of the specified exponent. - /// - /// The specified value. - /// The specified exponent. - /// The result of multiplying the parameter by two, raised to the power of the parameter. - [Pure] - public static Vector2 Ldexp(Vector2 x, Vector2 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ldexp)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns the result of multiplying the specified value by two, raised to the power of the specified exponent. - /// - /// The specified value. - /// The specified exponent. - /// The result of multiplying the parameter by two, raised to the power of the parameter. - [Pure] - public static Vector3 Ldexp(Vector3 x, Vector3 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ldexp)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns the result of multiplying the specified value by two, raised to the power of the specified exponent. - /// - /// The specified value. - /// The specified exponent. - /// The result of multiplying the parameter by two, raised to the power of the parameter. - [Pure] - public static Vector4 Ldexp(Vector4 x, Vector4 exp) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Ldexp)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Performs a linear interpolation. /// @@ -1775,36 +1234,6 @@ namespace ComputeSharp [Pure] public static Float2 Lerp(Float4 x, Float4 y, Float4 s) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Lerp)}({nameof(Float4)},{nameof(Float4)},{nameof(Float4)})"); - /// - /// Performs a linear interpolation. - /// - /// The first value. - /// The second value. - /// A value that linearly interpolates between the parameter and the parameter. - /// The result of the linear interpolation. - [Pure] - public static Vector2 Lerp(Vector2 x, Vector2 y, Vector2 s) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Lerp)}({nameof(Vector2)},{nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Performs a linear interpolation. - /// - /// The first value. - /// The second value. - /// A value that linearly interpolates between the parameter and the parameter. - /// The result of the linear interpolation. - [Pure] - public static Vector3 Lerp(Vector3 x, Vector3 y, Vector3 s) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Lerp)}({nameof(Vector3)},{nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Performs a linear interpolation. - /// - /// The first value. - /// The second value. - /// A value that linearly interpolates between the parameter and the parameter. - /// The result of the linear interpolation. - [Pure] - public static Vector2 Lerp(Vector4 x, Vector4 y, Vector4 s) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Lerp)}({nameof(Vector4)},{nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the base-e logarithm of the specified value. /// @@ -1837,30 +1266,6 @@ namespace ComputeSharp [Pure] public static Float4 Log(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log)}({nameof(Float4)})"); - /// - /// Returns the base-e logarithm of the specified value. - /// - /// The specified value. - /// The base-e logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector2 Log(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log)}({nameof(Vector2)})"); - - /// - /// Returns the base-e logarithm of the specified value. - /// - /// The specified value. - /// The base-e logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector3 Log(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log)}({nameof(Vector3)})"); - - /// - /// Returns the base-e logarithm of the specified value. - /// - /// The specified value. - /// The base-e logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector4 Log(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log)}({nameof(Vector4)})"); - /// /// Returns the base-10 logarithm of the specified value. /// @@ -1893,30 +1298,6 @@ namespace ComputeSharp [Pure] public static Float4 Log10(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log10)}({nameof(Float4)})"); - /// - /// Returns the base-10 logarithm of the specified value. - /// - /// The specified value. - /// The base-10 logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector2 Log10(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log10)}({nameof(Vector2)})"); - - /// - /// Returns the base-10 logarithm of the specified value. - /// - /// The specified value. - /// The base-10 logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector3 Log10(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log10)}({nameof(Vector3)})"); - - /// - /// Returns the base-10 logarithm of the specified value. - /// - /// The specified value. - /// The base-10 logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector4 Log10(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log10)}({nameof(Vector4)})"); - /// /// Returns the base-2 logarithm of the specified value. /// @@ -1949,30 +1330,6 @@ namespace ComputeSharp [Pure] public static Float4 Log2(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log2)}({nameof(Float4)})"); - /// - /// Returns the base-2 logarithm of the specified value. - /// - /// The specified value. - /// The base-2 logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector2 Log2(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log2)}({nameof(Vector2)})"); - - /// - /// Returns the base-2 logarithm of the specified value. - /// - /// The specified value. - /// The base-2 logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector3 Log2(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log2)}({nameof(Vector3)})"); - - /// - /// Returns the base-2 logarithm of the specified value. - /// - /// The specified value. - /// The base-2 logarithm of the parameter (if parameter is negative this function returns indefinite, and if is 0, this function returns -INF) - [Pure] - public static Vector4 Log2(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Log2)}({nameof(Vector4)})"); - /// /// Selects the greater of and . /// @@ -2023,36 +1380,6 @@ namespace ComputeSharp [Pure] public static Float4 Max(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Max)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Selects the greater of and . - /// - /// The first specified value. - /// The second specified exponent. - /// The or parameter, whichever is the largest value. - /// For values of -INF or INF, will behave as expected, but for for values of , the results are undefined. - [Pure] - public static Vector2 Max(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Max)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Selects the greater of and . - /// - /// The first specified value. - /// The second specified exponent. - /// The or parameter, whichever is the largest value. - /// For values of -INF or INF, will behave as expected, but for for values of , the results are undefined. - [Pure] - public static Vector3 Max(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Max)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Selects the greater of and . - /// - /// The first specified value. - /// The second specified exponent. - /// The or parameter, whichever is the largest value. - /// For values of -INF or INF, will behave as expected, but for for values of , the results are undefined. - [Pure] - public static Vector4 Max(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Max)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Selects the lesser of and . /// @@ -2103,36 +1430,6 @@ namespace ComputeSharp [Pure] public static Float4 Min(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Min)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Selects the lesser of and . - /// - /// The first specified value. - /// The second specified exponent. - /// The or parameter, whichever is the smallest value. - /// For values of -INF or INF, will behave as expected, but for for values of , the results are undefined. - [Pure] - public static Vector2 Min(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Min)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Selects the lesser of and . - /// - /// The first specified value. - /// The second specified exponent. - /// The or parameter, whichever is the smallest value. - /// For values of -INF or INF, will behave as expected, but for for values of , the results are undefined. - [Pure] - public static Vector3 Min(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Min)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Selects the lesser of and . - /// - /// The first specified value. - /// The second specified exponent. - /// The or parameter, whichever is the smallest value. - /// For values of -INF or INF, will behave as expected, but for for values of , the results are undefined. - [Pure] - public static Vector4 Min(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Min)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Splits the value into fractional and integer parts, each of which has the same sign as . /// @@ -2205,33 +1502,6 @@ namespace ComputeSharp [Pure] public static Float4 Modf(Float4 x, out Float4 ip) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Modf)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Splits the value into fractional and integer parts, each of which has the same sign as . - /// - /// The input value. - /// The integer portion of . - /// The signed-fractional portion of . - [Pure] - public static Vector2 Modf(Vector2 x, out Vector2 ip) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Modf)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Splits the value into fractional and integer parts, each of which has the same sign as . - /// - /// The input value. - /// The integer portion of . - /// The signed-fractional portion of . - [Pure] - public static Vector3 Modf(Vector3 x, out Vector3 ip) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Modf)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Splits the value into fractional and integer parts, each of which has the same sign as . - /// - /// The input value. - /// The integer portion of . - /// The signed-fractional portion of . - [Pure] - public static Vector4 Modf(Vector4 x, out Vector4 ip) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Modf)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Multiplies and using matrix math. /// @@ -2259,33 +1529,6 @@ namespace ComputeSharp [Pure] public static Float3 Mul(float x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}(float,{nameof(Float4)})"); - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static Vector2 Mul(float x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}(float,{nameof(Vector2)})"); - - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static Vector3 Mul(float x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}(float,{nameof(Vector3)})"); - - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static Vector3 Mul(float x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}(float,{nameof(Vector4)})"); - /// /// Multiplies and using matrix math. /// @@ -2313,33 +1556,6 @@ namespace ComputeSharp [Pure] public static Float4 Mul(Float4 x, float y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Float4)},float)"); - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static Vector2 Mul(Vector2 x, float y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Vector2)},float)"); - - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static Vector3 Mul(Vector3 x, float y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Vector3)},float)"); - - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static Vector4 Mul(Vector4 x, float y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Vector4)},float)"); - /// /// Multiplies and using matrix math. /// @@ -2367,33 +1583,6 @@ namespace ComputeSharp [Pure] public static float Mul(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static float Mul(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static float Mul(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Multiplies and using matrix math. - /// - /// The first input value (if is a vector, it treated as a row vector) - /// The second input value (if is a vector, it treated as a column vector) - /// The result of times . - [Pure] - public static float Mul(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Mul)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Normalizes the specified floating-point vector according to / length() /// @@ -2418,30 +1607,6 @@ namespace ComputeSharp [Pure] public static Float4 Normalize(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Normalize)}({nameof(Float4)})"); - /// - /// Normalizes the specified floating-point vector according to / length() - /// - /// The specified value. - /// The normalized parameter. - [Pure] - public static Vector2 Normalize(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Normalize)}({nameof(Vector2)})"); - - /// - /// Normalizes the specified floating-point vector according to / length() - /// - /// The specified value. - /// The normalized parameter. - [Pure] - public static Vector3 Normalize(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Normalize)}({nameof(Vector3)})"); - - /// - /// Normalizes the specified floating-point vector according to / length() - /// - /// The specified value. - /// The normalized parameter. - [Pure] - public static Vector4 Normalize(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Normalize)}({nameof(Vector4)})"); - /// /// Returns the specified value raised to the specified power. /// @@ -2482,36 +1647,6 @@ namespace ComputeSharp [Pure] public static Float4 Pow(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Pow)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns the specified value raised to the specified power. - /// - /// The specified value. - /// The specified power. - /// The parameter raised to the power of the parameter. - /// See for details on special cases. - [Pure] - public static Vector2 Pow(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Pow)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns the specified value raised to the specified power. - /// - /// The specified value. - /// The specified power. - /// The parameter raised to the power of the parameter. - /// See for details on special cases. - [Pure] - public static Vector3 Pow(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Pow)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns the specified value raised to the specified power. - /// - /// The specified value. - /// The specified power. - /// The parameter raised to the power of the parameter. - /// See for details on special cases. - [Pure] - public static Vector4 Pow(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Pow)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Converts the specified value from degrees to radians. /// @@ -2544,30 +1679,6 @@ namespace ComputeSharp [Pure] public static Float4 Radians(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Radians)}({nameof(Float4)})"); - /// - /// Converts the specified value from degrees to radians. - /// - /// The specified value. - /// The parameter converted from degrees to radians. - [Pure] - public static Vector2 Radians(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Radians)}({nameof(Vector2)})"); - - /// - /// Converts the specified value from degrees to radians. - /// - /// The specified value. - /// The parameter converted from degrees to radians. - [Pure] - public static Vector3 Radians(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Radians)}({nameof(Vector3)})"); - - /// - /// Converts the specified value from degrees to radians. - /// - /// The specified value. - /// The parameter converted from degrees to radians. - [Pure] - public static Vector4 Radians(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Radians)}({nameof(Vector4)})"); - /// /// Calculates a fast, approximate reciprocal of a given value. /// @@ -2600,30 +1711,6 @@ namespace ComputeSharp [Pure] public static Float4 Rcp(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rcp)}({nameof(Float4)})"); - /// - /// Calculates a fast, approximate, per-component reciprocal. - /// - /// The specified value. - /// The reciprocal of the parameter. - [Pure] - public static Vector2 Rcp(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rcp)}({nameof(Vector2)})"); - - /// - /// Calculates a fast, approximate, per-component reciprocal. - /// - /// The specified value. - /// The reciprocal of the parameter. - [Pure] - public static Vector3 Rcp(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rcp)}({nameof(Vector3)})"); - - /// - /// Calculates a fast, approximate, per-component reciprocal. - /// - /// The specified value. - /// The reciprocal of the parameter. - [Pure] - public static Vector4 Rcp(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rcp)}({nameof(Vector4)})"); - /// /// Rounds the specified value to the nearest integer. /// @@ -2656,30 +1743,6 @@ namespace ComputeSharp [Pure] public static Float4 Round(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Round)}({nameof(Float4)})"); - /// - /// Rounds the specified value to the nearest integer. - /// - /// The specified value. - /// The parameter, rounded to the nearest integer within a floating-point type. - [Pure] - public static Vector2 Round(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Round)}({nameof(Vector2)})"); - - /// - /// Rounds the specified value to the nearest integer. - /// - /// The specified value. - /// The parameter, rounded to the nearest integer within a floating-point type. - [Pure] - public static Vector3 Round(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Round)}({nameof(Vector3)})"); - - /// - /// Rounds the specified value to the nearest integer. - /// - /// The specified value. - /// The parameter, rounded to the nearest integer within a floating-point type. - [Pure] - public static Vector4 Round(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Round)}({nameof(Vector4)})"); - /// /// Returns the reciprocal of the square root of the specified value. /// @@ -2716,33 +1779,6 @@ namespace ComputeSharp [Pure] public static Float4 Rsqrt(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rsqrt)}({nameof(Float4)})"); - /// - /// Returns the reciprocal of the square root of the specified value. - /// - /// The specified value. - /// The reciprocal of the square root of the parameter. - /// This function uses the following formula: 1 / sqrt() - [Pure] - public static Vector2 Rsqrt(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rsqrt)}({nameof(Vector2)})"); - - /// - /// Returns the reciprocal of the square root of the specified value. - /// - /// The specified value. - /// The reciprocal of the square root of the parameter. - /// This function uses the following formula: 1 / sqrt() - [Pure] - public static Vector3 Rsqrt(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rsqrt)}({nameof(Vector3)})"); - - /// - /// Returns the reciprocal of the square root of the specified value. - /// - /// The specified value. - /// The reciprocal of the square root of the parameter. - /// This function uses the following formula: 1 / sqrt() - [Pure] - public static Vector4 Rsqrt(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Rsqrt)}({nameof(Vector4)})"); - /// /// Clamps the specified value within the range [0,1] /// @@ -2775,30 +1811,6 @@ namespace ComputeSharp [Pure] public static Float4 Saturate(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Saturate)}({nameof(Float4)})"); - /// - /// Clamps the specified value within the range [0,1] - /// - /// The specified value. - /// The parameter, clamped within the range [0,1] - [Pure] - public static Vector2 Saturate(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Saturate)}({nameof(Vector2)})"); - - /// - /// Clamps the specified value within the range [0,1] - /// - /// The specified value. - /// The parameter, clamped within the range [0,1] - [Pure] - public static Vector3 Saturate(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Saturate)}({nameof(Vector3)})"); - - /// - /// Clamps the specified value within the range [0,1] - /// - /// The specified value. - /// The parameter, clamped within the range [0,1] - [Pure] - public static Vector4 Saturate(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Saturate)}({nameof(Vector4)})"); - /// /// Returns the sign of . /// @@ -2831,30 +1843,6 @@ namespace ComputeSharp [Pure] public static Float4 Sign(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sign)}({nameof(Float4)})"); - /// - /// Returns the sign of . - /// - /// The input value. - /// Returns -1 if is less than zero, 0 if equals zero and 1 if is greater than zero. - [Pure] - public static Vector2 Sign(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sign)}({nameof(Vector2)})"); - - /// - /// Returns the sign of . - /// - /// The input value. - /// Returns -1 if is less than zero, 0 if equals zero and 1 if is greater than zero. - [Pure] - public static Vector3 Sign(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sign)}({nameof(Vector3)})"); - - /// - /// Returns the sign of . - /// - /// The input value. - /// Returns -1 if is less than zero, 0 if equals zero and 1 if is greater than zero. - [Pure] - public static Vector4 Sign(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sign)}({nameof(Vector4)})"); - /// /// Returns the sine of the specified value. /// @@ -2887,30 +1875,6 @@ namespace ComputeSharp [Pure] public static Float4 Sin(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sin)}({nameof(Float4)})"); - /// - /// Returns the sine of the specified value. - /// - /// The specified value, radians. - /// The sine of the parameter. - [Pure] - public static Vector2 Sin(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sin)}({nameof(Vector2)})"); - - /// - /// Returns the sine of the specified value. - /// - /// The specified value, radians. - /// The sine of the parameter. - [Pure] - public static Vector3 Sin(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sin)}({nameof(Vector3)})"); - - /// - /// Returns the sine of the specified value. - /// - /// The specified value, radians. - /// The sine of the parameter. - [Pure] - public static Vector4 Sin(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sin)}({nameof(Vector4)})"); - /// /// Returns the sine and cosine of . /// @@ -2943,30 +1907,6 @@ namespace ComputeSharp /// Returns the cosine of . public static void SinCos(Float4 x, out Float4 s, out Float4 c) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SinCos)}({nameof(Float4)},{nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns the sine and cosine of . - /// - /// The specified value, radians. - /// Returns the sine of . - /// Returns the cosine of . - public static void SinCos(Vector2 x, out Vector2 s, out Vector2 c) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SinCos)}({nameof(Vector2)},{nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns the sine and cosine of . - /// - /// The specified value, radians. - /// Returns the sine of . - /// Returns the cosine of . - public static void SinCos(Vector3 x, out Vector3 s, out Vector3 c) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SinCos)}({nameof(Vector3)},{nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns the sine and cosine of . - /// - /// The specified value, radians. - /// Returns the sine of . - /// Returns the cosine of . - public static void SinCos(Vector4 x, out Vector4 s, out Vector4 c) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SinCos)}({nameof(Vector4)},{nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the hyperbolic sine of the specified value. /// @@ -2999,30 +1939,6 @@ namespace ComputeSharp [Pure] public static Float4 Sinh(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sinh)}({nameof(Float4)})"); - /// - /// Returns the hyperbolic sine of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic sine of the parameter. - [Pure] - public static Vector2 Sinh(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sinh)}({nameof(Vector2)})"); - - /// - /// Returns the hyperbolic sine of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic sine of the parameter. - [Pure] - public static Vector3 Sinh(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sinh)}({nameof(Vector3)})"); - - /// - /// Returns the hyperbolic sine of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic sine of the parameter. - [Pure] - public static Vector4 Sinh(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sinh)}({nameof(Vector4)})"); - /// /// Returns a smooth Hermite interpolation between 0 and 1, if is in the range [, ] /// @@ -3063,36 +1979,6 @@ namespace ComputeSharp [Pure] public static Float4 SmoothStep(Float4 min, Float4 max, Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SmoothStep)}({nameof(Float4)},{nameof(Float4)},{nameof(Float4)})"); - /// - /// Returns a smooth Hermite interpolation between 0 and 1, if is in the range [, ] - /// - /// The minimum range of the parameter. - /// The maximum range of the parameter. - /// The specified value to be interpolated. - /// 0 if is less than , 1 if is greater than , otherwise a value between 0 and 1 if is in the range [, ] - [Pure] - public static Vector2 SmoothStep(Vector2 min, Vector2 max, Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SmoothStep)}({nameof(Vector2)},{nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Returns a smooth Hermite interpolation between 0 and 1, if is in the range [, ] - /// - /// The minimum range of the parameter. - /// The maximum range of the parameter. - /// The specified value to be interpolated. - /// 0 if is less than , 1 if is greater than , otherwise a value between 0 and 1 if is in the range [, ] - [Pure] - public static Vector3 SmoothStep(Vector3 min, Vector3 max, Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SmoothStep)}({nameof(Vector3)},{nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Returns a smooth Hermite interpolation between 0 and 1, if is in the range [, ] - /// - /// The minimum range of the parameter. - /// The maximum range of the parameter. - /// The specified value to be interpolated. - /// 0 if is less than , 1 if is greater than , otherwise a value between 0 and 1 if is in the range [, ] - [Pure] - public static Vector4 SmoothStep(Vector4 min, Vector4 max, Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(SmoothStep)}({nameof(Vector4)},{nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the square root of the specified floating-point value. /// @@ -3125,30 +2011,6 @@ namespace ComputeSharp [Pure] public static Float4 Sqrt(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sqrt)}({nameof(Float4)})"); - /// - /// Returns the square root of the specified floating-point value, per component. - /// - /// The specified value. - /// The square root of the parameter, per component. - [Pure] - public static Vector2 Sqrt(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sqrt)}({nameof(Vector2)})"); - - /// - /// Returns the square root of the specified floating-point value, per component. - /// - /// The specified value. - /// The square root of the parameter, per component. - [Pure] - public static Vector3 Sqrt(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sqrt)}({nameof(Vector3)})"); - - /// - /// Returns the square root of the specified floating-point value, per component. - /// - /// The specified value. - /// The square root of the parameter, per component. - [Pure] - public static Vector4 Sqrt(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Sqrt)}({nameof(Vector4)})"); - /// /// Compares two values, returning 0 or 1 based on which value is greater. /// @@ -3189,36 +2051,6 @@ namespace ComputeSharp [Pure] public static Float4 Step(Float4 x, Float4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Step)}({nameof(Float4)},{nameof(Float4)})"); - /// - /// Compares two values, returning 0 or 1 based on which value is greater. - /// - /// The first specified value to compare. - /// The second specified value to compare. - /// 1 if the parameter is greater than or equal to the parameter, otherwise, 0. - /// This function uses the following formula: (x >= y) ? 1 : 0. - [Pure] - public static Vector2 Step(Vector2 x, Vector2 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Step)}({nameof(Vector2)},{nameof(Vector2)})"); - - /// - /// Compares two values, returning 0 or 1 based on which value is greater. - /// - /// The first specified value to compare. - /// The second specified value to compare. - /// 1 if the parameter is greater than or equal to the parameter, otherwise, 0. - /// This function uses the following formula: (x >= y) ? 1 : 0. - [Pure] - public static Vector3 Step(Vector3 x, Vector3 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Step)}({nameof(Vector3)},{nameof(Vector3)})"); - - /// - /// Compares two values, returning 0 or 1 based on which value is greater. - /// - /// The first specified value to compare. - /// The second specified value to compare. - /// 1 if the parameter is greater than or equal to the parameter, otherwise, 0. - /// This function uses the following formula: (x >= y) ? 1 : 0. - [Pure] - public static Vector4 Step(Vector4 x, Vector4 y) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Step)}({nameof(Vector4)},{nameof(Vector4)})"); - /// /// Returns the tangent of the specified value. /// @@ -3251,30 +2083,6 @@ namespace ComputeSharp [Pure] public static Float4 Tan(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tan)}({nameof(Float4)})"); - /// - /// Returns the tangent of the specified value. - /// - /// The specified value, radians. - /// The tangent of the parameter. - [Pure] - public static Vector2 Tan(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tan)}({nameof(Vector2)})"); - - /// - /// Returns the tangent of the specified value. - /// - /// The specified value, radians. - /// The tangent of the parameter. - [Pure] - public static Vector3 Tan(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tan)}({nameof(Vector3)})"); - - /// - /// Returns the tangent of the specified value. - /// - /// The specified value, radians. - /// The tangent of the parameter. - [Pure] - public static Vector4 Tan(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tan)}({nameof(Vector4)})"); - /// /// Returns the hyperbolic tangent of the specified value. /// @@ -3307,30 +2115,6 @@ namespace ComputeSharp [Pure] public static Float4 Tanh(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tanh)}({nameof(Float4)})"); - /// - /// Returns the hyperbolic tangent of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic tangent of the parameter. - [Pure] - public static Vector2 Tanh(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tanh)}({nameof(Vector2)})"); - - /// - /// Returns the hyperbolic tangent of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic tangent of the parameter. - [Pure] - public static Vector3 Tanh(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tanh)}({nameof(Vector3)})"); - - /// - /// Returns the hyperbolic tangent of the specified value. - /// - /// The specified value, radians. - /// The hyperbolic tangent of the parameter. - [Pure] - public static Vector4 Tanh(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Tanh)}({nameof(Vector4)})"); - /// /// Truncates a floating-point value to the integer component. /// @@ -3362,29 +2146,5 @@ namespace ComputeSharp /// The parameter truncated to an integer component. [Pure] public static Float4 Trunc(Float4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Trunc)}({nameof(Float4)})"); - - /// - /// Truncates a floating-point value to the integer component. - /// - /// The specified value, radians. - /// The parameter truncated to an integer component. - [Pure] - public static Vector2 Trunc(Vector2 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Trunc)}({nameof(Vector2)})"); - - /// - /// Truncates a floating-point value to the integer component. - /// - /// The specified value, radians. - /// The parameter truncated to an integer component. - [Pure] - public static Vector3 Trunc(Vector3 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Trunc)}({nameof(Vector3)})"); - - /// - /// Truncates a floating-point value to the integer component. - /// - /// The specified value, radians. - /// The parameter truncated to an integer component. - [Pure] - public static Vector4 Trunc(Vector4 x) => throw new InvalidExecutionContextException($"{nameof(Hlsl)}.{nameof(Trunc)}({nameof(Vector4)})"); } } diff --git a/src/ComputeSharp.Core/PixelTypes/Bgra32.cs b/src/ComputeSharp.Core/PixelTypes/Bgra32.cs new file mode 100644 index 00000000..3a9172d2 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Bgra32.cs @@ -0,0 +1,122 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in blue, green, red, and alpha order (least significant to most significant byte). + /// The format is binary compatible with System.Drawing.Imaging.PixelFormat.Format32bppArgb. + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. + /// + /// + /// This struct is fully mutable. + [StructLayout(LayoutKind.Sequential)] + public struct Bgra32 : IEquatable, IUnorm, IUnorm + { + /// + /// The blue component. + /// + public byte B; + + /// + /// The green component. + /// + public byte G; + + /// + /// The red component. + /// + public byte R; + + /// + /// The alpha component. + /// + public byte A; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Bgra32(byte r, byte g, byte b) + { + R = r; + G = g; + B = b; + A = byte.MaxValue; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Bgra32(byte r, byte g, byte b, byte a) + { + R = r; + G = g; + B = b; + A = a; + } + + /// + /// Gets or sets the packed representation of the struct. + /// + public uint PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Bgra32 left, Bgra32 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Bgra32 left, Bgra32 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object? obj) => obj is Bgra32 other && Equals(other); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Bgra32 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Bgra32)}({this.B}, {this.G}, {this.R}, {this.A})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/R16.cs b/src/ComputeSharp.Core/PixelTypes/R16.cs new file mode 100644 index 00000000..8e6a8951 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/R16.cs @@ -0,0 +1,81 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing a single 16-bit unsigned normalized channel value. + /// + /// Ranges from [0, 0, 0, 0] to [1, 0, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct R16 : IEquatable, IUnorm + { + /// + /// The red component. + /// + public ushort R; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + public R16(ushort r) + { + R = r; + } + + /// + /// Gets or sets the packed representation of the struct. + /// + public ushort PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(R16 left, R16 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(R16 left, R16 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object? obj) => obj is R16 R16 && Equals(R16); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(R16 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(R16)}({this.R})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/R8.cs b/src/ComputeSharp.Core/PixelTypes/R8.cs new file mode 100644 index 00000000..cb627b96 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/R8.cs @@ -0,0 +1,81 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing a single 8-bit unsigned normalized channel value. + /// + /// Ranges from [0, 0, 0, 0] to [1, 0, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct R8 : IEquatable, IUnorm + { + /// + /// The red component. + /// + public byte R; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + public R8(byte r) + { + R = r; + } + + /// + /// Gets or sets the packed representation of the struct. + /// + public byte PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(R8 left, R8 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(R8 left, R8 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object? obj) => obj is R8 R8 && Equals(R8); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(R8 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(R8)}({this.R})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/Rg16.cs b/src/ComputeSharp.Core/PixelTypes/Rg16.cs new file mode 100644 index 00000000..cf294abb --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rg16.cs @@ -0,0 +1,89 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing two 8-bit unsigned normalized values ranging from 0 to 255. + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct Rg16 : IEquatable, IUnorm, IUnorm + { + /// + /// The red component. + /// + public byte R; + + /// + /// The green component. + /// + public byte G; + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component + /// The y-component + public Rg16(byte r, byte g) + { + R = r; + G = g; + } + + /// + /// Gets or sets the packed representation of the struct. + /// + public ushort PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rg16 left, Rg16 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rg16 left, Rg16 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object obj) => obj is Rg16 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rg16 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rg16)}({this.R}, {this.G})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/Rg32.cs b/src/ComputeSharp.Core/PixelTypes/Rg32.cs new file mode 100644 index 00000000..10f5a7c1 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rg32.cs @@ -0,0 +1,89 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing two 16-bit unsigned normalized values ranging from 0 to 255. + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct Rg32 : IEquatable, IUnorm, IUnorm + { + /// + /// The red component. + /// + public ushort R; + + /// + /// The green component. + /// + public ushort G; + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component + /// The y-component + public Rg32(ushort r, ushort g) + { + R = r; + G = g; + } + + /// + /// Gets or sets the packed representation of the struct. + /// + public uint PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rg32 left, Rg32 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rg32 left, Rg32 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object? obj) => obj is Rg32 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rg32 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rg32)}({this.R}, {this.G})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/Rgba32.cs b/src/ComputeSharp.Core/PixelTypes/Rgba32.cs new file mode 100644 index 00000000..7e21c856 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rgba32.cs @@ -0,0 +1,121 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in red, green, blue, and alpha order (least significant to most significant byte). + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. + /// + /// + /// This struct is fully mutable. + [StructLayout(LayoutKind.Sequential)] + public struct Rgba32 : IEquatable, IUnorm, IUnorm + { + /// + /// The red component. + /// + public byte R; + + /// + /// The green component. + /// + public byte G; + + /// + /// The blue component. + /// + public byte B; + + /// + /// The alpha component. + /// + public byte A; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba32(byte r, byte g, byte b) + { + this.R = r; + this.G = g; + this.B = b; + this.A = byte.MaxValue; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba32(byte r, byte g, byte b, byte a) + { + this.R = r; + this.G = g; + this.B = b; + this.A = a; + } + + /// + /// Gets or sets the packed representation of the struct. + /// + public uint PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rgba32 left, Rgba32 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rgba32 left, Rgba32 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object? obj) => obj is Rgba32 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rgba32 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rgba32)}({this.R}, {this.G}, {this.B}, {this.A})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/Rgba64.cs b/src/ComputeSharp.Core/PixelTypes/Rgba64.cs new file mode 100644 index 00000000..7d1c7eb1 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rgba64.cs @@ -0,0 +1,121 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing four 16-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in red, green, blue, and alpha order (least significant to most significant byte). + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. + /// + /// + /// This struct is fully mutable. + [StructLayout(LayoutKind.Sequential)] + public struct Rgba64 : IEquatable, IUnorm, IUnorm + { + /// + /// The red component. + /// + public ushort R; + + /// + /// The green component. + /// + public ushort G; + + /// + /// The blue component. + /// + public ushort B; + + /// + /// The alpha component. + /// + public ushort A; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba64(ushort r, ushort g, ushort b) + { + this.R = r; + this.G = g; + this.B = b; + this.A = ushort.MaxValue; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba64(ushort r, ushort g, ushort b, ushort a) + { + this.R = r; + this.G = g; + this.B = b; + this.A = a; + } + + /// + /// Gets or sets the packed representation of the struct. + /// + public ulong PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rgba64 left, Rgba64 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rgba64 left, Rgba64 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object? obj) => obj is Rgba64 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rgba64 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rgba64)}({this.R}, {this.G}, {this.B}, {this.A})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/__Internals/IUnorm{TPixel}.cs b/src/ComputeSharp.Core/PixelTypes/__Internals/IUnorm{TPixel}.cs new file mode 100644 index 00000000..4f138d99 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/__Internals/IUnorm{TPixel}.cs @@ -0,0 +1,16 @@ +using System; +using System.ComponentModel; + +namespace ComputeSharp.__Internals +{ + /// + /// Indicates the supported HLSL pixel types to represent a given normalized pixel type. + /// + /// The type of pixels used on the GPU side. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This interface is not intended to be used directly by user code")] + public interface IUnorm + where TPixel : unmanaged + { + } +} diff --git a/src/ComputeSharp.Core/Primitives/Float/Float2.cs b/src/ComputeSharp.Core/Primitives/Float/Float2.cs index 42067446..36168a1c 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float2.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float2.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Numerics; using System.Runtime.InteropServices; using ComputeSharp.Exceptions; @@ -142,6 +143,18 @@ namespace ComputeSharp /// The value to use for the components of the new instance. public static implicit operator Float2(float x) => new(x, x); + /// + /// Casts a value to a one. + /// + /// The input value to cast. + public static unsafe implicit operator Vector2(Float2 xy) => *(Vector2*)&xy; + + /// + /// Casts a value to a one. + /// + /// The input value to cast. + public static unsafe implicit operator Float2(Vector2 xy) => *(Float2*)&xy; + /// /// Casts a value to a one. /// diff --git a/src/ComputeSharp.Core/Primitives/Float/Float3.cs b/src/ComputeSharp.Core/Primitives/Float/Float3.cs index 30069e33..eeeea995 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float3.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float3.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Numerics; using System.Runtime.InteropServices; using ComputeSharp.Exceptions; @@ -563,6 +564,18 @@ namespace ComputeSharp /// The value to use for the components of the new instance. public static implicit operator Float3(float x) => new(x, x, x); + /// + /// Casts a value to a one. + /// + /// The input value to cast. + public static unsafe implicit operator Vector3(Float3 xyz) => *(Vector3*)&xyz; + + /// + /// Casts a value to a one. + /// + /// The input value to cast. + public static unsafe implicit operator Float3(Vector3 xyz) => *(Float3*)&xyz; + /// /// Casts a value to a one. /// diff --git a/src/ComputeSharp.Core/Primitives/Float/Float4.cs b/src/ComputeSharp.Core/Primitives/Float/Float4.cs index 944ec16e..5b7904a6 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float4.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float4.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Numerics; using System.Runtime.InteropServices; using ComputeSharp.Exceptions; @@ -3968,6 +3969,18 @@ namespace ComputeSharp /// The value to use for the components of the new instance. public static implicit operator Float4(float x) => new(x, x, x, x); + /// + /// Casts a value to a one. + /// + /// The input value to cast. + public static unsafe implicit operator Vector4(Float4 xyzw) => *(Vector4*)&xyzw; + + /// + /// Casts a value to a one. + /// + /// The input value to cast. + public static unsafe implicit operator Float4(Vector4 xyzw) => *(Float4*)&xyzw; + /// /// Casts a value to a one. /// diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T,TPixel}.cs new file mode 100644 index 00000000..ad66a8a2 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture2D{T,TPixel}.cs @@ -0,0 +1,56 @@ +using System.Diagnostics; +using ComputeSharp.__Internals; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// A representing a typed readonly 2D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadOnlyTexture2D : Texture2D + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The height of the texture. + internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height) + : base(device, width, height, ResourceType.ReadOnly) + { + } + + /// + /// Gets a single value from the current readonly texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[int,int]"); + + /// + /// Gets a single value from the current readonly texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture2D)}[Int2]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyTexture2D<{typeof(T)},{typeof(TPixel)}>[{Width}, {Height}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T,TPixel}.cs new file mode 100644 index 00000000..35d64c8e --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadOnlyTexture3D{T,TPixel}.cs @@ -0,0 +1,58 @@ +using System.Diagnostics; +using ComputeSharp.__Internals; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// A representing a typed readonly 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadOnlyTexture3D : Texture3D + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth) + : base(device, width, height, depth, ResourceType.ReadOnly) + { + } + + /// + /// Gets a single value from the current readonly texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// The depthwise offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[int,int,int]"); + + /// + /// Gets a single value from the current readonly texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public TPixel this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyTexture3D)}[Int3]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadOnlyTexture3D<{typeof(T)},{nameof(TPixel)}>[{Width}, {Height}, {Depth}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T,TPixel}.cs new file mode 100644 index 00000000..ca284d79 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture2D{T,TPixel}.cs @@ -0,0 +1,56 @@ +using System.Diagnostics; +using ComputeSharp.__Internals; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// A representing a typed read write 2D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + [DebuggerTypeProxy(typeof(Texture2DDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadWriteTexture2D : Texture2D + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The height of the texture. + internal ReadWriteTexture2D(GraphicsDevice device, int width, int height) + : base(device, width, height, ResourceType.ReadWrite) + { + } + + /// + /// Gets a single value from the current writeable texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[int x, int y] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[int,int]"); + + /// + /// Gets or sets a single value from the current writeable texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[Int2 xy] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture2D)}[Int2]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadWriteTexture2D<{typeof(T)},{typeof(TPixel)}>[{Width}, {Height}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T,TPixel}.cs new file mode 100644 index 00000000..32acd100 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/ReadWriteTexture3D{T,TPixel}.cs @@ -0,0 +1,58 @@ +using System.Diagnostics; +using ComputeSharp.__Internals; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Views; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// A representing a typed readonly 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + [DebuggerTypeProxy(typeof(Texture3DDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] + public sealed class ReadWriteTexture3D : Texture3D + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth) + : base(device, width, height, depth, ResourceType.ReadWrite) + { + } + + /// + /// Gets a single value from the current writeable texture. + /// + /// The horizontal offset of the value to get. + /// The vertical offset of the value to get. + /// The depthwise offset of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[int x, int y, int z] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[int,int,int]"); + + /// + /// Gets a single value from the current writeable texture. + /// + /// The coordinates of the value to get. + /// This API can only be used from a compute shader, and will always throw if used anywhere else. + public ref TPixel this[Int3 xyz] => throw new InvalidExecutionContextException($"{nameof(ReadWriteTexture3D)}[Int3]"); + + /// + public override string ToString() + { + return $"ComputeSharp.ReadWriteTexture3D<{typeof(T)},{nameof(TPixel)}>[{Width}, {Height}, {Depth}]"; + } + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs index be8d5d21..c12698ae 100644 --- a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs @@ -2,8 +2,8 @@ using System.Numerics; using System.Runtime.CompilerServices; using Microsoft.Toolkit.Diagnostics; -using static TerraFX.Interop.DXGI_FORMAT; using TerraFX.Interop; +using static TerraFX.Interop.DXGI_FORMAT; namespace ComputeSharp.Graphics.Helpers { @@ -38,6 +38,13 @@ namespace ComputeSharp.Graphics.Helpers else if (typeof(T) == typeof(Vector2)) return DXGI_FORMAT_R32G32_FLOAT; else if (typeof(T) == typeof(Vector3)) return DXGI_FORMAT_R32G32B32_FLOAT; else if (typeof(T) == typeof(Vector4)) return DXGI_FORMAT_R32G32B32A32_FLOAT; + else if (typeof(T) == typeof(Bgra32)) return DXGI_FORMAT_B8G8R8A8_UNORM; + else if (typeof(T) == typeof(Rgba32)) return DXGI_FORMAT_R8G8B8A8_UNORM; + else if (typeof(T) == typeof(Rgba64)) return DXGI_FORMAT_R16G16B16A16_UNORM; + else if (typeof(T) == typeof(R8)) return DXGI_FORMAT_R8_UNORM; + else if (typeof(T) == typeof(R16)) return DXGI_FORMAT_R16_UNORM; + else if (typeof(T) == typeof(Rg16)) return DXGI_FORMAT_R8G8_UNORM; + else if (typeof(T) == typeof(Rg32)) return DXGI_FORMAT_R16G16_UNORM; else return ThrowHelper.ThrowArgumentException("Invalid texture type"); } } diff --git a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs index b3fe7b73..b2c84d86 100644 --- a/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Translation/Mappings/HlslKnownTypes.cs @@ -77,7 +77,9 @@ namespace ComputeSharp.Shaders.Mappings return genericType == typeof(ReadOnlyBuffer<>) || genericType == typeof(ReadOnlyTexture2D<>) || - genericType == typeof(ReadOnlyTexture3D<>); + genericType == typeof(ReadOnlyTexture2D<,>) || + genericType == typeof(ReadOnlyTexture3D<>) || + genericType == typeof(ReadOnlyTexture3D<,>); } /// @@ -95,7 +97,9 @@ namespace ComputeSharp.Shaders.Mappings return genericType == typeof(ReadWriteBuffer<>) || genericType == typeof(ReadWriteTexture2D<>) || - genericType == typeof(ReadWriteTexture3D<>); + genericType == typeof(ReadWriteTexture2D<,>) || + genericType == typeof(ReadWriteTexture3D<>) || + genericType == typeof(ReadWriteTexture3D<,>); } } } diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 04f729f7..03fb352f 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -2,6 +2,7 @@ netstandard2.0 + true 9.0 @@ -12,7 +13,7 @@ + and to match members based on full name, so the types being in the generator assembly are not an issue.--> diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index a90ba55c..fa51f8ba 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -122,7 +122,8 @@ namespace ComputeSharp.SourceGenerators yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name, typeName }; // Track the type of items in the current buffer - if (HlslKnownTypes.IsTypedResourceType(typeSymbol.GetFullMetadataName())) + if (HlslKnownTypes.IsTypedResourceType(typeSymbol.GetFullMetadataName()) && + typeSymbol.TypeArguments.Length == 1) { types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]); } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs index 7a7df32b..e3751ef4 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownMembers.cs @@ -18,9 +18,13 @@ namespace ComputeSharp.SourceGenerators.Mappings private static readonly IReadOnlyDictionary KnownIndexers = new Dictionary { [$"ComputeSharp.ReadOnlyTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.ReadOnlyTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", [$"ComputeSharp.ReadWriteTexture2D`1.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", + [$"ComputeSharp.ReadWriteTexture2D`2.this[{typeof(int).FullName}, {typeof(int).FullName}]"] = "int2", [$"ComputeSharp.ReadOnlyTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", - [$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" + [$"ComputeSharp.ReadOnlyTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", + [$"ComputeSharp.ReadWriteTexture3D`1.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3", + [$"ComputeSharp.ReadWriteTexture3D`2.this[{typeof(int).FullName}, {typeof(int).FullName}, {typeof(int).FullName}]"] = "int3" }; /// diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 43ba20b4..2bc27d99 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.Contracts; +using System.Linq; using System.Numerics; using ComputeSharp.SourceGenerators.Extensions; using Microsoft.CodeAnalysis; @@ -70,9 +71,13 @@ namespace ComputeSharp.SourceGenerators.Mappings case "ComputeSharp.ReadOnlyBuffer`1": case "ComputeSharp.ReadWriteBuffer`1": case "ComputeSharp.ReadOnlyTexture2D`1": + case "ComputeSharp.ReadOnlyTexture2D`2": case "ComputeSharp.ReadWriteTexture2D`1": + case "ComputeSharp.ReadWriteTexture2D`2": case "ComputeSharp.ReadOnlyTexture3D`1": + case "ComputeSharp.ReadOnlyTexture3D`2": case "ComputeSharp.ReadWriteTexture3D`1": + case "ComputeSharp.ReadWriteTexture3D`2": return true; default: return false; }; @@ -107,7 +112,7 @@ namespace ComputeSharp.SourceGenerators.Mappings // Special case for the structured buffer types if (IsTypedResourceType(typeName)) { - string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments[0]).GetFullMetadataName(); + string genericArgumentName = ((INamedTypeSymbol)typeSymbol.TypeArguments.Last()).GetFullMetadataName(); // If the current type is a custom type, format it as needed if (!KnownTypes.TryGetValue(genericArgumentName, out string? mapped)) @@ -122,9 +127,13 @@ namespace ComputeSharp.SourceGenerators.Mappings "ComputeSharp.ReadOnlyBuffer`1" => $"StructuredBuffer<{mapped}>", "ComputeSharp.ReadWriteBuffer`1" => $"RWStructuredBuffer<{mapped}>", "ComputeSharp.ReadOnlyTexture2D`1" => $"Texture2D<{mapped}>", + "ComputeSharp.ReadOnlyTexture2D`2" => $"Texture2D", "ComputeSharp.ReadWriteTexture2D`1" => $"RWTexture2D<{mapped}>", + "ComputeSharp.ReadWriteTexture2D`2" => $"RWTexture2D", "ComputeSharp.ReadOnlyTexture3D`1" => $"Texture3D<{mapped}>", + "ComputeSharp.ReadOnlyTexture3D`2" => $"Texture3D", "ComputeSharp.ReadWriteTexture3D`1" => $"RWTexture3D<{mapped}>", + "ComputeSharp.ReadWriteTexture3D`2" => $"RWTexture3D", _ => throw new ArgumentException() }; } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 2331d5ea..0f089902 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -218,11 +218,15 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters // in HLSL, we can remove the suffix entirely. As for 64 bit values, we simply use the 'L' suffix. if (type.GetFullMetadataName().Equals(typeof(float).FullName)) { - return updatedNode.WithToken(Literal(updatedNode.Token.ValueText, 0f)); + string literal = updatedNode.Token.ValueText; + + if (!literal.Contains('.')) literal += ".0"; + + return updatedNode.WithToken(Literal(literal, 0f)); } else if (type.GetFullMetadataName().Equals(typeof(double).FullName)) { - return updatedNode.WithToken(Literal($"{updatedNode.Token.ValueText}L", 0d)); + return updatedNode.WithToken(Literal(updatedNode.Token.ValueText + "L", 0d)); } } diff --git a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs index e22ed529..c0533982 100644 --- a/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs +++ b/src/ComputeSharp/Extensions/GraphicsDeviceExtensions.cs @@ -1,9 +1,12 @@ using System; using System.Diagnostics.Contracts; +using ComputeSharp.__Internals; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders; +#pragma warning disable CS0618 + namespace ComputeSharp { /// @@ -225,6 +228,102 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height); + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] array, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture2D(array.AsSpan(), width, height); + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[] array, int offset, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture2D(array.AsSpan(offset), width, height); + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, T[,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new readonly 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadOnlyTexture2D AllocateReadOnlyTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture2D texture = new(device, width, height); + + texture.SetData(span); + + return texture; + } + /// /// Allocates a new readonly 3D texture with the specified parameters. /// @@ -319,6 +418,110 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A zeroed instance of size [, , ]. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] array, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture3D(array.AsSpan(), width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[] array, int offset, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadOnlyTexture3D(array.AsSpan(offset), width, height, depth); + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + /// + /// The source 3D array needs to have each 2D plane stacked on the depth axis. + /// That is, the expected layout of the input array has to be [depth, height, width]. + /// + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, T[,,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new readonly 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadOnlyTexture3D AllocateReadOnlyTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadOnlyTexture3D texture = new(device, width, height, depth); + + texture.SetData(span); + + return texture; + } + /// /// Allocates a new read write buffer with the specified parameters. /// @@ -469,6 +672,102 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture2D(array.AsSpan(), width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[] array, int offset, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture2D(array.AsSpan(offset), width, height); + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, T[,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture2D texture = new(device, array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new writeable 2D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadWriteTexture2D AllocateReadWriteTexture2D(this GraphicsDevice device, ReadOnlySpan span, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture2D texture = new(device, width, height); + + texture.SetData(span); + + return texture; + } + /// /// Allocates a new writeable 3D texture with the specified parameters. /// @@ -563,6 +862,110 @@ namespace ComputeSharp return texture; } + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A zeroed instance of size [, ]. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return new(device, width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] array, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture3D(array.AsSpan(), width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// The starting offset within to read data from. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input array. + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[] array, int offset, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return device.AllocateReadWriteTexture3D(array.AsSpan(offset), width, height, depth); + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input array with the data to copy on the allocated texture. + /// A read write instance with the contents of the input array. + /// + /// The source 3D array needs to have each 2D plane stacked on the depth axis. + /// That is, the expected layout of the input array has to be [depth, height, width]. + /// + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, T[,,] array) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture3D texture = new(device, array.GetLength(2), array.GetLength(1), array.GetLength(0)); + + texture.SetData(array); + + return texture; + } + + /// + /// Allocates a new writeable 3D texture with the specified parameters. + /// + /// The type of items to store in the texture. + /// The type of pixels used on the GPU side. + /// The instance to use to allocate the texture. + /// The input with the data to copy on the allocated texture. + /// The width of the texture. + /// The height of the texture. + /// The depth of the texture. + /// A instance with the contents of the input . + [Pure] + public static ReadWriteTexture3D AllocateReadWriteTexture3D(this GraphicsDevice device, ReadOnlySpan span, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + ReadWriteTexture3D texture = new(device, width, height, depth); + + texture.SetData(span); + + return texture; + } + /// /// Compiles and runs the input shader on a target instance, with the specified parameters. /// diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index d4f1c821..aae52469 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -20,4 +20,32 @@ PrivateAssets="contentfiles;build"/> + + + TextTemplatingFileGenerator + Texture2DTests.Pixels.g.cs + + + TextTemplatingFileGenerator + Texture3DTests.Pixels.g.cs + + + + + + + + + + True + True + Texture2DTests.Pixels.tt + + + True + True + Texture3DTests.Pixels.tt + + + diff --git a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs index ff9ba47e..f4005f85 100644 --- a/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs +++ b/tests/ComputeSharp.Tests/Extensions/GraphicsDeviceExtensions.cs @@ -1,8 +1,11 @@ using System; using System.Diagnostics.Contracts; +using ComputeSharp.__Internals; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; +#pragma warning disable CS0618 + namespace ComputeSharp.Tests.Extensions { /// @@ -52,6 +55,29 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The type of pixels used on the GPU side. + /// The target instance to allocate the texture for. + /// The type of texture to allocate. + /// The width of the texture to create. + /// The height of the texture to create. + /// A instance of the requested size. + [Pure] + public static Texture2D AllocateTexture2D(this GraphicsDevice device, Type type, int width, int height) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture2D<,>) => device.AllocateReadOnlyTexture2D(width, height), + _ when type == typeof(ReadWriteTexture2D<,>) => device.AllocateReadWriteTexture2D(width, height), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// @@ -74,6 +100,30 @@ namespace ComputeSharp.Tests.Extensions }; } + /// + /// Allocates a new instance of the specified type. + /// + /// The type of items in the texture. + /// The type of pixels used on the GPU side. + /// The target instance to allocate the texture for. + /// The type of texture to allocate. + /// The width of the texture to create. + /// The height of the texture to create. + /// The depth of the texture to create. + /// A instance of the requested size. + [Pure] + public static Texture3D AllocateTexture3D(this GraphicsDevice device, Type type, int width, int height, int depth) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + return type switch + { + _ when type == typeof(ReadOnlyTexture3D<,>) => device.AllocateReadOnlyTexture3D(width, height, depth), + _ when type == typeof(ReadWriteTexture3D<,>) => device.AllocateReadWriteTexture3D(width, height, depth), + _ => throw new ArgumentException($"Invalid type: {type}", nameof(type)) + }; + } + /// /// Allocates a new instance of the specified type. /// diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs new file mode 100644 index 00000000..78efd8c9 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.cs @@ -0,0 +1,73 @@ +using System; +using System.Numerics; +using System.Reflection; +using ComputeSharp.__Internals; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Tests +{ + public partial class Texture2DTests + { + [TestMethod] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture2D<,>), typeof(Rgba64), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture2D<,>), typeof(Rgba64), typeof(Float4))] + public void Allocate_Uninitialized_Pixel_Ok(Type textureType, Type t, Type tPixel) + { + static void Test(Type textureType) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using Texture2D texture = Gpu.Default.AllocateTexture2D(textureType, 128, 128); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + + if (textureType == typeof(ReadOnlyTexture2D<,>)) + { + Assert.IsTrue(texture is ReadOnlyTexture2D); + } + else + { + Assert.IsTrue(texture is ReadWriteTexture2D); + } + } + + try + { + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new[] { textureType }); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } + } +} diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs new file mode 100644 index 00000000..08039596 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.g.cs @@ -0,0 +1,254 @@ +using System; +using System.Numerics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + public partial class Texture2DTests + { + [TestMethod] + [DataRow(typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(R16), typeof(float))] + [DataRow(typeof(R8), typeof(float))] + [DataRow(typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(Rg16), typeof(Float2))] + [DataRow(typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(Rg32), typeof(Float2))] + [DataRow(typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(Rgba64), typeof(Float4))] + public void Dispatch_NormalizedTexture2D(Type t, Type tPixel) + { + if (t == typeof(Bgra32) && tPixel == typeof(Vector4)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Bgra32_Vector4(source, destination)); + } + if (t == typeof(Bgra32) && tPixel == typeof(Float4)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Bgra32_Float4(source, destination)); + } + if (t == typeof(R16) && tPixel == typeof(float)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_R16_float(source, destination)); + } + if (t == typeof(R8) && tPixel == typeof(float)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_R8_float(source, destination)); + } + if (t == typeof(Rg16) && tPixel == typeof(Vector2)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rg16_Vector2(source, destination)); + } + if (t == typeof(Rg16) && tPixel == typeof(Float2)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rg16_Float2(source, destination)); + } + if (t == typeof(Rg32) && tPixel == typeof(Vector2)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rg32_Vector2(source, destination)); + } + if (t == typeof(Rg32) && tPixel == typeof(Float2)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rg32_Float2(source, destination)); + } + if (t == typeof(Rgba32) && tPixel == typeof(Vector4)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rgba32_Vector4(source, destination)); + } + if (t == typeof(Rgba32) && tPixel == typeof(Float4)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rgba32_Float4(source, destination)); + } + if (t == typeof(Rgba64) && tPixel == typeof(Vector4)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rgba64_Vector4(source, destination)); + } + if (t == typeof(Rgba64) && tPixel == typeof(Float4)) + { + using ReadOnlyTexture2D source = Gpu.Default.AllocateReadOnlyTexture2D(128, 128); + using ReadWriteTexture2D destination = Gpu.Default.AllocateReadWriteTexture2D(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_Rgba64_Float4(source, destination)); + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Bgra32_Vector4 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Bgra32_Float4 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_R16_float : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_R8_float : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg16_Vector2 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg16_Float2 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg32_Vector2 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg32_Float2 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba32_Vector4 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba32_Float4 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba64_Vector4 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba64_Float4 : IComputeShader + { + public readonly ReadOnlyTexture2D source; + public readonly ReadWriteTexture2D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt new file mode 100644 index 00000000..33775291 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture2DTests.Pixels.tt @@ -0,0 +1,81 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".g.cs"#> +<# +var typesMap = new Dictionary +{ + ["Bgra32"] = new[] { "Vector4", "Float4" }, + ["R16"] = new[] { "float" }, + ["R8"] = new[] { "float" }, + ["Rg16"] = new[] { "Vector2", "Float2" }, + ["Rg32"] = new[] { "Vector2", "Float2" }, + ["Rgba32"] = new[] { "Vector4", "Float4" }, + ["Rgba64"] = new[] { "Vector4", "Float4" } +}; + +IEnumerable<(string T, string TPixel)> EnumerateTypes() +{ + foreach (var pair in typesMap) + foreach (var tPixel in pair.Value) + yield return (pair.Key, tPixel); +} +#> +using System; +using System.Numerics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + public partial class Texture2DTests + { + [TestMethod] +<# +foreach (var (t, tPixel) in EnumerateTypes()) +{ +#> + [DataRow(typeof(<#=t#>), typeof(<#=tPixel#>))] +<# +} +#> + public void Dispatch_NormalizedTexture2D(Type t, Type tPixel) + { +<# +foreach (var (t, tPixel) in EnumerateTypes()) +{ +#> + if (t == typeof(<#=t#>) && tPixel == typeof(<#=tPixel#>)) + { + using ReadOnlyTexture2D<<#=t#>, <#=tPixel#>> source = Gpu.Default.AllocateReadOnlyTexture2D<<#=t#>, <#=tPixel#>>(128, 128); + using ReadWriteTexture2D<<#=t#>, <#=tPixel#>> destination = Gpu.Default.AllocateReadWriteTexture2D<<#=t#>, <#=tPixel#>>(128, 128); + + Gpu.Default.For(128, 128, new Shader_Unorm_<#=t#>_<#=tPixel#>(source, destination)); + } +<# +} +#> + } +<# + +foreach (var (t, tPixel) in EnumerateTypes()) +{ +#> + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_<#=t#>_<#=tPixel#> : IComputeShader + { + public readonly ReadOnlyTexture2D<<#=t#>, <#=tPixel#>> source; + public readonly ReadWriteTexture2D<<#=t#>, <#=tPixel#>> destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XY] = (source[ids.XY] + destination[ids.XY]) / 2; + } + } +<# +} +#> + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs new file mode 100644 index 00000000..673f6f77 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.cs @@ -0,0 +1,74 @@ +using System; +using System.Numerics; +using System.Reflection; +using ComputeSharp.__Internals; +using ComputeSharp.Graphics.Buffers.Abstract; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Tests +{ + public partial class Texture3DTests + { + [TestMethod] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadOnlyTexture3D<,>), typeof(Rgba64), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R16), typeof(float))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(R8), typeof(float))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg16), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rg32), typeof(Float2))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(ReadWriteTexture3D<,>), typeof(Rgba64), typeof(Float4))] + public void Allocate_Uninitialized_Pixel_Ok(Type textureType, Type t, Type tPixel) + { + static void Test(Type textureType) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + using Texture3D texture = Gpu.Default.AllocateTexture3D(textureType, 128, 128, 2); + + Assert.IsNotNull(texture); + Assert.AreEqual(texture.Width, 128); + Assert.AreEqual(texture.Height, 128); + Assert.AreEqual(texture.Depth, 2); + Assert.AreSame(texture.GraphicsDevice, Gpu.Default); + + if (textureType == typeof(ReadOnlyTexture3D<,>)) + { + Assert.IsTrue(texture is ReadOnlyTexture3D); + } + else + { + Assert.IsTrue(texture is ReadWriteTexture3D); + } + } + + try + { + new Action(Test).Method.GetGenericMethodDefinition().MakeGenericMethod(t, tPixel).Invoke(null, new[] { textureType }); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } + } +} diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs new file mode 100644 index 00000000..3c1e4955 --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.g.cs @@ -0,0 +1,254 @@ +using System; +using System.Numerics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + public partial class Texture3DTests + { + [TestMethod] + [DataRow(typeof(Bgra32), typeof(Vector4))] + [DataRow(typeof(Bgra32), typeof(Float4))] + [DataRow(typeof(R16), typeof(float))] + [DataRow(typeof(R8), typeof(float))] + [DataRow(typeof(Rg16), typeof(Vector2))] + [DataRow(typeof(Rg16), typeof(Float2))] + [DataRow(typeof(Rg32), typeof(Vector2))] + [DataRow(typeof(Rg32), typeof(Float2))] + [DataRow(typeof(Rgba32), typeof(Vector4))] + [DataRow(typeof(Rgba32), typeof(Float4))] + [DataRow(typeof(Rgba64), typeof(Vector4))] + [DataRow(typeof(Rgba64), typeof(Float4))] + public void Dispatch_NormalizedTexture3D(Type t, Type tPixel) + { + if (t == typeof(Bgra32) && tPixel == typeof(Vector4)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Bgra32_Vector4(source, destination)); + } + if (t == typeof(Bgra32) && tPixel == typeof(Float4)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Bgra32_Float4(source, destination)); + } + if (t == typeof(R16) && tPixel == typeof(float)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_R16_float(source, destination)); + } + if (t == typeof(R8) && tPixel == typeof(float)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_R8_float(source, destination)); + } + if (t == typeof(Rg16) && tPixel == typeof(Vector2)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg16_Vector2(source, destination)); + } + if (t == typeof(Rg16) && tPixel == typeof(Float2)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg16_Float2(source, destination)); + } + if (t == typeof(Rg32) && tPixel == typeof(Vector2)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg32_Vector2(source, destination)); + } + if (t == typeof(Rg32) && tPixel == typeof(Float2)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rg32_Float2(source, destination)); + } + if (t == typeof(Rgba32) && tPixel == typeof(Vector4)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba32_Vector4(source, destination)); + } + if (t == typeof(Rgba32) && tPixel == typeof(Float4)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba32_Float4(source, destination)); + } + if (t == typeof(Rgba64) && tPixel == typeof(Vector4)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba64_Vector4(source, destination)); + } + if (t == typeof(Rgba64) && tPixel == typeof(Float4)) + { + using ReadOnlyTexture3D source = Gpu.Default.AllocateReadOnlyTexture3D(128, 128, 2); + using ReadWriteTexture3D destination = Gpu.Default.AllocateReadWriteTexture3D(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_Rgba64_Float4(source, destination)); + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Bgra32_Vector4 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Bgra32_Float4 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_R16_float : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_R8_float : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg16_Vector2 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg16_Float2 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg32_Vector2 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rg32_Float2 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba32_Vector4 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba32_Float4 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba64_Vector4 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_Rgba64_Float4 : IComputeShader + { + public readonly ReadOnlyTexture3D source; + public readonly ReadWriteTexture3D destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt new file mode 100644 index 00000000..d72edd3c --- /dev/null +++ b/tests/ComputeSharp.Tests/Texture3DTests.Pixels.tt @@ -0,0 +1,81 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".g.cs"#> +<# +var typesMap = new Dictionary +{ + ["Bgra32"] = new[] { "Vector4", "Float4" }, + ["R16"] = new[] { "float" }, + ["R8"] = new[] { "float" }, + ["Rg16"] = new[] { "Vector2", "Float2" }, + ["Rg32"] = new[] { "Vector2", "Float2" }, + ["Rgba32"] = new[] { "Vector4", "Float4" }, + ["Rgba64"] = new[] { "Vector4", "Float4" } +}; + +IEnumerable<(string T, string TPixel)> EnumerateTypes() +{ + foreach (var pair in typesMap) + foreach (var tPixel in pair.Value) + yield return (pair.Key, tPixel); +} +#> +using System; +using System.Numerics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests +{ + public partial class Texture3DTests + { + [TestMethod] +<# +foreach (var (t, tPixel) in EnumerateTypes()) +{ +#> + [DataRow(typeof(<#=t#>), typeof(<#=tPixel#>))] +<# +} +#> + public void Dispatch_NormalizedTexture3D(Type t, Type tPixel) + { +<# +foreach (var (t, tPixel) in EnumerateTypes()) +{ +#> + if (t == typeof(<#=t#>) && tPixel == typeof(<#=tPixel#>)) + { + using ReadOnlyTexture3D<<#=t#>, <#=tPixel#>> source = Gpu.Default.AllocateReadOnlyTexture3D<<#=t#>, <#=tPixel#>>(128, 128, 2); + using ReadWriteTexture3D<<#=t#>, <#=tPixel#>> destination = Gpu.Default.AllocateReadWriteTexture3D<<#=t#>, <#=tPixel#>>(128, 128, 2); + + Gpu.Default.For(128, 128, 2, new Shader_Unorm_<#=t#>_<#=tPixel#>(source, destination)); + } +<# +} +#> + } +<# + +foreach (var (t, tPixel) in EnumerateTypes()) +{ +#> + + [AutoConstructor] + internal readonly partial struct Shader_Unorm_<#=t#>_<#=tPixel#> : IComputeShader + { + public readonly ReadOnlyTexture3D<<#=t#>, <#=tPixel#>> source; + public readonly ReadWriteTexture3D<<#=t#>, <#=tPixel#>> destination; + + public void Execute(ThreadIds ids) + { + destination[ids.XYZ] = (source[ids.XYZ] + destination[ids.XYZ]) / 2; + } + } +<# +} +#> + } +} \ No newline at end of file