From 459517a181170bf2730f69c97aab67fdc822575b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 28 Apr 2022 01:04:09 +0200 Subject: [PATCH] Enable shaders to inherit shader interfaces --- .../ID2D1ShaderGenerator.Helpers.cs | 2 +- .../IShaderGenerator.Helpers.cs | 2 +- .../ComputeSharp.Tests/ShaderCompilerTests.cs | 64 +++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs index 9232e403..79a971ae 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs @@ -18,7 +18,7 @@ partial class ID2D1ShaderGenerator /// Whether or not implements . private static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, INamedTypeSymbol d2D1PixelShaderSymbol) { - foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.Interfaces) + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { if (interfaceSymbol.Name == nameof(ID2D1PixelShader) && SymbolEqualityComparer.Default.Equals(interfaceSymbol, d2D1PixelShaderSymbol)) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 88d0550c..77462b98 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -23,7 +23,7 @@ partial class IShaderGenerator INamedTypeSymbol iComputeShaderSymbol, INamedTypeSymbol iPixelShaderSymbol) { - foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.Interfaces) + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { if (interfaceSymbol.Name == nameof(IComputeShader) && SymbolEqualityComparer.Default.Equals(interfaceSymbol, iComputeShaderSymbol)) diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 0c497090..3fd445c3 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -272,6 +272,70 @@ namespace ComputeSharp.Tests this.buffer[ThreadIds.X] *= this.number; } } + + [TestMethod] + public void ComputeShaderWithInheritedShaderInterface() + { + _ = ReflectionServices.GetShaderInfo(); + } + + public interface IMyBaseShader : IComputeShader + { + public int A { get; } + + public void B(); + } + + [AutoConstructor] + internal readonly partial struct ComputeShaderWithInheritedShaderInterfaceShader : IMyBaseShader + { + int IMyBaseShader.A { get; } + + void IMyBaseShader.B() + { + } + + public readonly ReadWriteBuffer buffer; + public readonly float number; + + /// + public void Execute() + { + this.buffer[ThreadIds.X] *= this.number; + } + } + + [TestMethod] + public void PixelShaderWithInheritedShaderInterface() + { + _ = ReflectionServices.GetShaderInfo(); + } + + public interface IMyBaseShader : IPixelShader + where T : unmanaged + { + public int A { get; } + + public void B(); + } + + [AutoConstructor] + internal readonly partial struct PixelShaderWithInheritedShaderInterfaceShader : IMyBaseShader + { + int IMyBaseShader.A { get; } + + void IMyBaseShader.B() + { + } + + public readonly float number; + + /// + public float4 Execute() + { + return default; + } + } } }