using System; using System.Collections.Immutable; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using ComputeSharp.D2D1Interop.Exceptions; using ComputeSharp.D2D1Interop.Shaders.Translation; using ComputeSharp.D2D1Interop.SourceGenerators.Extensions; using ComputeSharp.D2D1Interop.SourceGenerators.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static ComputeSharp.D2D1Interop.SourceGenerators.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1Interop.SourceGenerators; /// partial class ID2D1ShaderGenerator { /// internal static partial class LoadBytecode { /// /// Extracts the shader profile for the current shader. /// /// The input instance to process. /// The shader profile to use to compile the shader, if present. public static D2D1ShaderProfile? GetShaderProfile(INamedTypeSymbol structDeclarationSymbol) { if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedName("ComputeSharp.D2D1Interop.D2DEmbeddedBytecodeAttribute", out AttributeData? attributeData)) { return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!; } return null; } /// /// Extracts the metadata definition for the current shader. /// /// The input instance to process. /// Whether the shader only has simple inputs. public static bool IsSimpleInputShader(INamedTypeSymbol structDeclarationSymbol) { if (!structDeclarationSymbol.TryGetAttributeWithFullyQualifiedName("ComputeSharp.D2D1Interop.D2DInputCountAttribute", out AttributeData? inputCountAttribute)) { return false; } // Build a map of all simple inputs (unmarked inputs default to being complex) int inputCount = (int)inputCountAttribute!.ConstructorArguments[0].Value!; bool[] simpleInputsMap = new bool[inputCount]; foreach (AttributeData attributeData in structDeclarationSymbol.GetAttributes()) { switch (attributeData.AttributeClass?.GetFullMetadataName()) { case "ComputeSharp.D2D1Interop.D2DInputSimpleAttribute": simpleInputsMap[(int)attributeData.ConstructorArguments[0].Value!] = true; break; } } return simpleInputsMap.All(static x => x); } /// /// Gets a instance with the logic to try to get a compiled shader bytecode. /// /// 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(HlslShaderSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic) { ImmutableArray bytecode = ImmutableArray.Empty; // No embedded shader was requested if (sourceInfo.ShaderProfile is null) { diagnostic = null; goto End; } try { token.ThrowIfCancellationRequested(); // Compile the shader bytecode using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile( sourceInfo.HlslSource.AsSpan(), sourceInfo.ShaderProfile.Value, sourceInfo.IsLinkingSupported); 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 = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message); } catch (FxcCompilationException e) { diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message); } End: return bytecode; } } }