Refactored internal IShader<TSelf> interface

このコミットが含まれているのは:
Sergio Pedri committed 2021-07-21 14:09:22 +02:00
1 親 ed20c56fd7
コミット 56b326a6e4
10ファイル変更
+65 -39

検索条件に一致するファイルはありません

+1 -4
ファイルの表示
@@ -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<T> : Win32Application
where T : struct, IPixelShader<Float4>, IShader<T>
where T : struct, IPixelShader<Float4>
{
/// <summary>
/// The <see cref="Func{T1, TResult}"/> instance used to create shaders to run.
+14
ファイルの表示
@@ -0,0 +1,14 @@
using System;
using System.ComponentModel;
namespace ComputeSharp.__Internals
{
/// <summary>
/// A base <see langword="interface"/> representing a given shader that can be dispatched.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
public interface IShader
{
}
}
+4 -4
ファイルの表示
@@ -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
{
/// <summary>
@@ -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<ShaderType>
// public <MODIFIERS> struct ShaderType
// {
// <METHODS_LIST>
// }
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;
+6 -2
ファイルの表示
@@ -1,9 +1,13 @@
namespace ComputeSharp
using ComputeSharp.__Internals;
#pragma warning disable CS0618
namespace ComputeSharp
{
/// <summary>
/// An <see langword="interface"/> representing a compute shader.
/// </summary>
public interface IComputeShader
public interface IComputeShader : IShader
{
/// <summary>
/// Executes the current compute shader.
+6 -2
ファイルの表示
@@ -1,9 +1,13 @@
namespace ComputeSharp
using ComputeSharp.__Internals;
#pragma warning disable CS0618
namespace ComputeSharp
{
/// <summary>
/// An <see langword="interface"/> representing a pixel shader.
/// </summary>
public interface IPixelShader<TPixel>
public interface IPixelShader<TPixel> : IShader
where TPixel : unmanaged
{
/// <summary>
@@ -6,11 +6,9 @@ namespace ComputeSharp.__Internals
/// <summary>
/// A base <see langword="interface"/> representing a given shader that can be dispatched.
/// </summary>
/// <typeparam name="TSelf">The type of shader.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
public interface IShader<TSelf>
where TSelf : struct, IShader<TSelf>
public interface IShader
{
/// <summary>
/// Gets a unique dispatch identifier for the shader.
@@ -18,7 +16,10 @@ namespace ComputeSharp.__Internals
/// <returns>The unique dispatch identifier for the shader.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be used directly by user code")]
int GetDispatchId();
int GetDispatchId()
{
throw new NotImplementedException();
}
/// <summary>
/// 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<TDataLoader>(ref TDataLoader loader, GraphicsDevice device, int x, int y, int z)
where TDataLoader : struct, IDispatchDataLoader;
where TDataLoader : struct, IDispatchDataLoader
{
throw new NotImplementedException();
}
/// <summary>
/// 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<TMetadataLoader>(ref TMetadataLoader loader, out IntPtr result)
where TMetadataLoader : struct, IDispatchMetadataLoader;
where TMetadataLoader : struct, IDispatchMetadataLoader
{
throw new NotImplementedException();
}
/// <summary>
/// Builds the HLSL source code for the current shader instance.
@@ -54,6 +61,9 @@ namespace ComputeSharp.__Internals
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
[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();
}
}
}
+6 -9
ファイルの表示
@@ -1,9 +1,6 @@
using ComputeSharp.Shaders;
using ComputeSharp.__Internals;
using System.Runtime.CompilerServices;
#pragma warning disable CS0618
namespace ComputeSharp
{
/// <summary>
@@ -19,7 +16,7 @@ namespace ComputeSharp
/// <param name="x">The number of iterations to run on the X axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this GraphicsDevice device, int x, in T shader)
where T : struct, IComputeShader, IShader<T>
where T : struct, IComputeShader
{
ShaderRunner<T>.Run(device, x, 1, 1, ref Unsafe.AsRef(in shader));
}
@@ -33,7 +30,7 @@ namespace ComputeSharp
/// <param name="y">The number of iterations to run on the Y axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this GraphicsDevice device, int x, int y, in T shader)
where T : struct, IComputeShader, IShader<T>
where T : struct, IComputeShader
{
ShaderRunner<T>.Run(device, x, y, 1, ref Unsafe.AsRef(in shader));
}
@@ -48,7 +45,7 @@ namespace ComputeSharp
/// <param name="z">The number of iterations to run on the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this GraphicsDevice device, int x, int y, int z, in T shader)
where T : struct, IComputeShader, IShader<T>
where T : struct, IComputeShader
{
ShaderRunner<T>.Run(device, x, y, z, ref Unsafe.AsRef(in shader));
}
@@ -66,7 +63,7 @@ namespace ComputeSharp
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the compute shader to run.</param>
public static void For<T>(this GraphicsDevice device, int x, int y, int z, int threadsX, int threadsY, int threadsZ, in T shader)
where T : struct, IComputeShader, IShader<T>
where T : struct, IComputeShader
{
ShaderRunner<T>.Run(device, x, y, z, threadsX, threadsY, threadsZ, ref Unsafe.AsRef(in shader));
}
@@ -79,7 +76,7 @@ namespace ComputeSharp
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
/// <param name="texture">The target texture to apply the pixel shader to.</param>
public static void ForEach<T, TPixel>(this GraphicsDevice device, IReadWriteTexture2D<TPixel> texture)
where T : struct, IPixelShader<TPixel>, IShader<T>
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
ShaderRunner<T>.Run(device, texture, ref Unsafe.AsRef(default(T)));
@@ -94,7 +91,7 @@ namespace ComputeSharp
/// <param name="texture">The target texture to apply the pixel shader to.</param>
/// <param name="shader">The input <typeparamref name="T"/> instance representing the pixel shader to run.</param>
public static void ForEach<T, TPixel>(this GraphicsDevice device, IReadWriteTexture2D<TPixel> texture, in T shader)
where T : struct, IPixelShader<TPixel>, IShader<T>
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
ShaderRunner<T>.Run(device, texture, ref Unsafe.AsRef(in shader));
+6 -6
ファイルの表示
@@ -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.
/// </remarks>
public static void GetShaderInfo<T>(out ShaderInfo shaderInfo)
where T : struct, IComputeShader, IShader<T>
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.
/// </remarks>
public static unsafe void GetShaderInfo<T>(in T shader, out ShaderInfo shaderInfo)
where T : struct, IComputeShader, IShader<T>
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.
/// </remarks>
public static void GetShaderInfo<T, TPixel>(out ShaderInfo shaderInfo)
where T : struct, IPixelShader<TPixel>, IShader<T>
where T : struct, IPixelShader<TPixel>
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.
/// </remarks>
public static unsafe void GetShaderInfo<T, TPixel>(in T shader, out ShaderInfo shaderInfo)
where T : struct, IPixelShader<TPixel>, IShader<T>
where T : struct, IPixelShader<TPixel>
where TPixel : unmanaged
{
GetNonGenericShaderInfo(in shader, out shaderInfo);
@@ -100,9 +100,9 @@ namespace ComputeSharp.Interop
/// <param name="shader">The input shader to retrieve info for.</param>
/// <param name="shaderInfo">The resulting <see cref="ShaderInfo"/> instance.</param>
private static unsafe void GetNonGenericShaderInfo<T>(in T shader, out ShaderInfo shaderInfo)
where T : struct, IShader<T>
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<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan);
+1 -1
ファイルの表示
@@ -20,7 +20,7 @@ namespace ComputeSharp.Shaders
/// </summary>
/// <typeparam name="T">The type of compute shader to run.</typeparam>
internal static class ShaderRunner<T>
where T : struct, IShader<T>
where T : struct, IShader
{
/// <summary>
/// The mapping used to cache and reuse compiled shaders.
+4 -4
ファイルの表示
@@ -140,7 +140,7 @@ namespace ComputeSharp.Tests
if (shaderType.IsAssignableTo(typeof(IComputeShader)))
{
static void RunComputeShader<T>(ReadWriteTexture2D<Rgba32, Float4> texture)
where T : struct, IComputeShader, IShader<T>
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<T>(ReadWriteTexture2D<Rgba32, Float4> texture)
where T : struct, IPixelShader<Float4>, IShader<T>
where T : struct, IPixelShader<Float4>
{
texture.GraphicsDevice.ForEach(texture, (T)Activator.CreateInstance(typeof(T), 0f)!);
}
@@ -191,8 +191,8 @@ namespace ComputeSharp.Tests
Func<ReadWriteTexture2D<Rgba32, Float4>, TCompute> computeFactory,
Func<ReadWriteTexture2D<Rgba32, Float4>, TPixel> pixelFactory,
float delta)
where TCompute : struct, IComputeShader, IShader<TCompute>
where TPixel : struct, IPixelShader<Float4>, IShader<TPixel>
where TCompute : struct, IComputeShader
where TPixel : struct, IPixelShader<Float4>
{
_ = device.Get();