diff --git a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs index c6c501f3..26f4f0cf 100644 --- a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs +++ b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs @@ -1,15 +1,12 @@ using System; -using ComputeSharp.__Internals; using ComputeSharp.Interop; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; -#pragma warning disable CS0618 - namespace ComputeSharp.SwapChain.Backend { internal sealed class SwapChainApplication : Win32Application - where T : struct, IPixelShader, IShader + where T : struct, IPixelShader { /// /// The instance used to create shaders to run. diff --git a/src/ComputeSharp.SourceGenerators/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp.SourceGenerators/Core/Interfaces/__Internals/IShader.cs new file mode 100644 index 00000000..d2b7a4c8 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Core/Interfaces/__Internals/IShader.cs @@ -0,0 +1,14 @@ +using System; +using System.ComponentModel; + +namespace ComputeSharp.__Internals +{ + /// + /// A base representing a given shader that can be dispatched. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This interface is not intended to be used directly by user code")] + public interface IShader + { + } +} diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 085f2734..2035fd0e 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using ComputeSharp.__Internals; using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Extensions; using Microsoft.CodeAnalysis; @@ -11,6 +12,8 @@ using static ComputeSharp.SourceGenerators.Diagnostics.DiagnosticDescriptors; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle; +#pragma warning disable CS0618 + namespace ComputeSharp.SourceGenerators { /// @@ -97,16 +100,13 @@ namespace ComputeSharp.SourceGenerators // Create the partial shader type declaration with the hashcode interface method implementation. // This code produces a struct declaration as follows: // - // public struct ShaderType : IShader + // public struct ShaderType // { // // } var structDeclarationSyntax = StructDeclaration(structName) .WithModifiers(structModifiers) - .AddBaseListTypes(SimpleBaseType( - GenericName(Identifier("IShader")) - .AddTypeArgumentListArguments(IdentifierName(structName)))) .AddMembers(methods.Select(m => m.AddAttributeLists(attributes)).ToArray()); TypeDeclarationSyntax typeDeclarationSyntax = structDeclarationSyntax; diff --git a/src/ComputeSharp/Core/Interfaces/IComputeShader.cs b/src/ComputeSharp/Core/Interfaces/IComputeShader.cs index 37f5396d..d69191ad 100644 --- a/src/ComputeSharp/Core/Interfaces/IComputeShader.cs +++ b/src/ComputeSharp/Core/Interfaces/IComputeShader.cs @@ -1,9 +1,13 @@ -namespace ComputeSharp +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp { /// /// An representing a compute shader. /// - public interface IComputeShader + public interface IComputeShader : IShader { /// /// Executes the current compute shader. diff --git a/src/ComputeSharp/Core/Interfaces/IPixelShader{TPixel}.cs b/src/ComputeSharp/Core/Interfaces/IPixelShader{TPixel}.cs index d89d6187..bf15cf6c 100644 --- a/src/ComputeSharp/Core/Interfaces/IPixelShader{TPixel}.cs +++ b/src/ComputeSharp/Core/Interfaces/IPixelShader{TPixel}.cs @@ -1,9 +1,13 @@ -namespace ComputeSharp +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp { /// /// An representing a pixel shader. /// - public interface IPixelShader + public interface IPixelShader : IShader where TPixel : unmanaged { /// diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs similarity index 87% rename from src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs rename to src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index fee6f8aa..df2e5523 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -6,11 +6,9 @@ namespace ComputeSharp.__Internals /// /// A base representing a given shader that can be dispatched. /// - /// The type of shader. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This interface is not intended to be used directly by user code")] - public interface IShader - where TSelf : struct, IShader + public interface IShader { /// /// Gets a unique dispatch identifier for the shader. @@ -18,7 +16,10 @@ namespace ComputeSharp.__Internals /// The unique dispatch identifier for the shader. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be used directly by user code")] - int GetDispatchId(); + int GetDispatchId() + { + throw new NotImplementedException(); + } /// /// Loads the dispatch data for the shader. @@ -32,7 +33,10 @@ namespace ComputeSharp.__Internals [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] void LoadDispatchData(ref TDataLoader loader, GraphicsDevice device, int x, int y, int z) - where TDataLoader : struct, IDispatchDataLoader; + where TDataLoader : struct, IDispatchDataLoader + { + throw new NotImplementedException(); + } /// /// Loads an opaque metadata handle from the metadata of the current shader. @@ -43,7 +47,10 @@ namespace ComputeSharp.__Internals [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] void LoadDispatchMetadata(ref TMetadataLoader loader, out IntPtr result) - where TMetadataLoader : struct, IDispatchMetadataLoader; + where TMetadataLoader : struct, IDispatchMetadataLoader + { + throw new NotImplementedException(); + } /// /// Builds the HLSL source code for the current shader instance. @@ -54,6 +61,9 @@ namespace ComputeSharp.__Internals /// The number of threads in each thread group for the Z axis. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] - void BuildHlslString(out ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ); + void BuildHlslString(out ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ) + { + throw new NotImplementedException(); + } } } diff --git a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs index 5bfb1ac4..ee6990d9 100644 --- a/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs +++ b/src/ComputeSharp/Graphics/Extensions/GraphicsDeviceExtensions.Dispatching.cs @@ -1,9 +1,6 @@ using ComputeSharp.Shaders; -using ComputeSharp.__Internals; using System.Runtime.CompilerServices; -#pragma warning disable CS0618 - namespace ComputeSharp { /// @@ -19,7 +16,7 @@ namespace ComputeSharp /// The number of iterations to run on the X axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, in T shader) - where T : struct, IComputeShader, IShader + where T : struct, IComputeShader { ShaderRunner.Run(device, x, 1, 1, ref Unsafe.AsRef(in shader)); } @@ -33,7 +30,7 @@ namespace ComputeSharp /// The number of iterations to run on the Y axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, int y, in T shader) - where T : struct, IComputeShader, IShader + where T : struct, IComputeShader { ShaderRunner.Run(device, x, y, 1, ref Unsafe.AsRef(in shader)); } @@ -48,7 +45,7 @@ namespace ComputeSharp /// The number of iterations to run on the Z axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, int y, int z, in T shader) - where T : struct, IComputeShader, IShader + where T : struct, IComputeShader { ShaderRunner.Run(device, x, y, z, ref Unsafe.AsRef(in shader)); } @@ -66,7 +63,7 @@ namespace ComputeSharp /// The number of threads in each thread group for the Z axis. /// The input instance representing the compute shader to run. public static void For(this GraphicsDevice device, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader) - where T : struct, IComputeShader, IShader + where T : struct, IComputeShader { ShaderRunner.Run(device, x, y, z, threadsX, threadsY, threadsZ, ref Unsafe.AsRef(in shader)); } @@ -79,7 +76,7 @@ namespace ComputeSharp /// The to use to run the shader. /// The target texture to apply the pixel shader to. public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture) - where T : struct, IPixelShader, IShader + where T : struct, IPixelShader where TPixel : unmanaged { ShaderRunner.Run(device, texture, ref Unsafe.AsRef(default(T))); @@ -94,7 +91,7 @@ namespace ComputeSharp /// The target texture to apply the pixel shader to. /// The input instance representing the pixel shader to run. public static void ForEach(this GraphicsDevice device, IReadWriteTexture2D texture, in T shader) - where T : struct, IPixelShader, IShader + where T : struct, IPixelShader where TPixel : unmanaged { ShaderRunner.Run(device, texture, ref Unsafe.AsRef(in shader)); diff --git a/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs index 4b8e7194..3a385798 100644 --- a/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs +++ b/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs @@ -30,7 +30,7 @@ namespace ComputeSharp.Interop /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. /// public static void GetShaderInfo(out ShaderInfo shaderInfo) - where T : struct, IComputeShader, IShader + where T : struct, IComputeShader { GetNonGenericShaderInfo(default(T), out shaderInfo); } @@ -47,7 +47,7 @@ namespace ComputeSharp.Interop /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. /// public static unsafe void GetShaderInfo(in T shader, out ShaderInfo shaderInfo) - where T : struct, IComputeShader, IShader + where T : struct, IComputeShader { GetNonGenericShaderInfo(in shader, out shaderInfo); } @@ -68,7 +68,7 @@ namespace ComputeSharp.Interop /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. /// public static void GetShaderInfo(out ShaderInfo shaderInfo) - where T : struct, IPixelShader, IShader + where T : struct, IPixelShader where TPixel : unmanaged { GetNonGenericShaderInfo(default(T), out shaderInfo); @@ -87,7 +87,7 @@ namespace ComputeSharp.Interop /// the APIs to dispatch a shader, the thread sizes would actually be set to a proper value insead. /// public static unsafe void GetShaderInfo(in T shader, out ShaderInfo shaderInfo) - where T : struct, IPixelShader, IShader + where T : struct, IPixelShader where TPixel : unmanaged { GetNonGenericShaderInfo(in shader, out shaderInfo); @@ -100,9 +100,9 @@ namespace ComputeSharp.Interop /// The input shader to retrieve info for. /// The resulting instance. private static unsafe void GetNonGenericShaderInfo(in T shader, out ShaderInfo shaderInfo) - where T : struct, IShader + where T : struct, IShader { - shader.BuildHlslString(out ArrayPoolStringBuilder shaderSource, 1, 1, 1); + Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder shaderSource, 1, 1, 1); using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan); diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index 6400b654..f56a7a25 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -20,7 +20,7 @@ namespace ComputeSharp.Shaders /// /// The type of compute shader to run. internal static class ShaderRunner - where T : struct, IShader + where T : struct, IShader { /// /// The mapping used to cache and reuse compiled shaders. diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index 1021eb9e..e5333572 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -140,7 +140,7 @@ namespace ComputeSharp.Tests if (shaderType.IsAssignableTo(typeof(IComputeShader))) { static void RunComputeShader(ReadWriteTexture2D texture) - where T : struct, IComputeShader, IShader + where T : struct, IComputeShader { texture.GraphicsDevice.For(texture.Width, texture.Height, (T)Activator.CreateInstance(typeof(T), texture, 0f)!); } @@ -152,7 +152,7 @@ namespace ComputeSharp.Tests else { static void RunPixelShader(ReadWriteTexture2D texture) - where T : struct, IPixelShader, IShader + where T : struct, IPixelShader { texture.GraphicsDevice.ForEach(texture, (T)Activator.CreateInstance(typeof(T), 0f)!); } @@ -191,8 +191,8 @@ namespace ComputeSharp.Tests Func, TCompute> computeFactory, Func, TPixel> pixelFactory, float delta) - where TCompute : struct, IComputeShader, IShader - where TPixel : struct, IPixelShader, IShader + where TCompute : struct, IComputeShader + where TPixel : struct, IPixelShader { _ = device.Get();