using System; using System.Collections.Immutable; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Threading; using ComputeSharp.D2D1.Exceptions; using ComputeSharp.D2D1.Shaders.Translation; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; /// partial class D2DPixelShaderSourceGenerator { /// /// A container for all the logic for . /// private static partial class Execute { /// /// Validates that the return type of the annotated method is valid and returns the type name. /// /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. public static string? GetInvalidReturnType(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { if (!(methodSymbol.ReturnType is INamedTypeSymbol { Name: "ReadOnlySpan", ContainingNamespace.Name: "System", IsGenericType: true, TypeParameters.Length: 1 } returnType && returnType.TypeArguments[0].SpecialType == SpecialType.System_Byte)) { diagnostics.Add( InvalidD2DPixelShaderSourceMethodReturnType, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType, methodSymbol.ReturnType); return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); } return null; } /// /// Extracts the HLSL source from a method with the annotation. /// /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. public static string GetHlslSource(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { _ = methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData); if (!attributeData!.TryGetConstructorArgument(0, out string? hlslSource)) { diagnostics.Add( InvalidD2DPixelShaderSource, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType); } return hlslSource ?? ""; } /// /// Extracts the shader profile for a target method, if present. /// /// The collection of produced instances. /// The input instance to process. /// The shader profile to use to compile the shader, if present. public static D2D1ShaderProfile GetShaderProfile(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData)) { return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; } if (methodSymbol.ContainingAssembly.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out attributeData)) { return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; } diagnostics.Add( MissingShaderProfileForD2DPixelShaderSource, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType); return D2D1ShaderProfile.PixelShader50; } /// /// Extracts the compile options for the current shader. /// /// The collection of produced instances. /// The input instance to process. /// The compile options to use to compile the shader, if present. public static D2D1CompileOptions GetCompileOptions(ImmutableArrayBuilder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { return (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; } if (methodSymbol.ContainingAssembly.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out attributeData)) { return (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!; } diagnostics.Add( MissingCompileOptionsForD2DPixelShaderSource, methodSymbol, methodSymbol.Name, methodSymbol.ContainingType); return D2D1CompileOptions.Default; } /// /// Gets an instance with the compiled bytecode for the current shader. /// /// The source info for the shader to compile. /// The used to cancel the operation, if needed. /// The resulting diagnostic from the processing operation, if any. /// The instance with the compiled shader bytecode. public static unsafe ImmutableArray GetBytecode(HlslShaderMethodSourceInfo sourceInfo, CancellationToken token, out DeferredDiagnosticInfo? diagnostic) { ImmutableArray bytecode = ImmutableArray.Empty; // Skip compilation if there are any errors if (sourceInfo is { HasErrors: true }) { diagnostic = null; goto End; } try { token.ThrowIfCancellationRequested(); // Compile the shader bytecode using the requested parameters using ComPtr dxcBlobBytecode = D3DCompiler.Compile( sourceInfo.HlslSource.AsSpan(), sourceInfo.ShaderProfile, sourceInfo.CompileOptions); token.ThrowIfCancellationRequested(); byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); byte[] array = new ReadOnlySpan(buffer, length).ToArray(); bytecode = Unsafe.As>(ref array); diagnostic = null; } catch (Win32Exception e) { diagnostic = DeferredDiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithWin32Exception, sourceInfo.MethodName, e.HResult, ID2D1ShaderGenerator.LoadBytecode.FixupExceptionMessage(e.Message)); } catch (FxcCompilationException e) { diagnostic = DeferredDiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithFxcCompilationException, sourceInfo.MethodName, ID2D1ShaderGenerator.LoadBytecode.FixupExceptionMessage(e.Message)); } End: return bytecode; } } }