using System;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGenerators.Dxc;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using Windows.Win32;
using Windows.Win32.Graphics.Direct3D.Dxc;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
///
partial class ComputeShaderDescriptorGenerator
{
///
/// A helper with all logic to generate the HlslBytecode property.
///
private static partial class HlslBytecode
{
///
/// The shared cache of values.
///
private static readonly DynamicCache HlslBytecodeCache = new();
///
/// Gets the instance for the input shader info.
///
/// The instance for the shader to compile.
/// The used to cancel the operation, if needed.
/// The instance for the current shader.
public static unsafe HlslBytecodeInfo GetBytecode(ref HlslBytecodeInfoKey key, CancellationToken token)
{
static unsafe HlslBytecodeInfo GetInfo(HlslBytecodeInfoKey key, CancellationToken token)
{
// Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator)
if (!key.IsCompilationEnabled)
{
return HlslBytecodeInfo.Missing.Instance;
}
try
{
token.ThrowIfCancellationRequested();
// Try to load dxcompiler.dll and dxil.dll
DxcLibraryLoader.LoadNativeDxcLibraries();
token.ThrowIfCancellationRequested();
// Compile the shader bytecode
using ComPtr dxcBlob = DxcShaderCompiler.Instance.Compile(
key.HlslSource.AsSpan(),
key.CompileOptions,
token);
token.ThrowIfCancellationRequested();
// Check whether double precision operations are required
bool requiresDoublePrecisionSupport = DxcShaderCompiler.Instance.IsDoublePrecisionSupportRequired(dxcBlob.Get());
token.ThrowIfCancellationRequested();
byte* buffer = (byte*)dxcBlob.Get()->GetBufferPointer();
int length = checked((int)dxcBlob.Get()->GetBufferSize());
byte[] array = new ReadOnlySpan(buffer, length).ToArray();
ImmutableArray bytecode = Unsafe.As>(ref array);
return new HlslBytecodeInfo.Success(bytecode, requiresDoublePrecisionSupport);
}
catch (Win32Exception e)
{
return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, DxcShaderCompiler.FixupExceptionMessage(e.Message));
}
catch (DxcCompilationException e)
{
return new HlslBytecodeInfo.CompilerError(DxcShaderCompiler.FixupExceptionMessage(e.Message));
}
}
return HlslBytecodeCache.GetOrCreate(ref key, GetInfo, token);
}
///
/// Extracts the compile options for the current shader.
///
/// The collection of produced instances.
/// The input instance to process.
/// The requested compile options to use to compile the shader, if present.
public static CompileOptions GetCompileOptions(ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol)
{
// If a [CompileOptions] annotation is present, return the explicit options
if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.CompileOptionsAttribute", out AttributeData? attributeData) ||
structDeclarationSymbol.ContainingAssembly.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.CompileOptionsAttribute", out attributeData))
{
return (CompileOptions)attributeData.ConstructorArguments[0].Value!;
}
return CompileOptions.Default;
}
///
/// Gets any diagnostics from a processed instance.
///
/// The input instance to process.
/// The source instance.
/// The collection of produced instances.
public static void GetInfoDiagnostics(
INamedTypeSymbol structDeclarationSymbol,
HlslBytecodeInfo info,
ImmutableArrayBuilder diagnostics)
{
DiagnosticInfo? diagnostic = null;
if (info is HlslBytecodeInfo.Win32Error win32Error)
{
diagnostic = DiagnosticInfo.Create(
HlslBytecodeFailedWithWin32Exception,
structDeclarationSymbol,
structDeclarationSymbol,
win32Error.HResult,
win32Error.Message);
}
else if (info is HlslBytecodeInfo.CompilerError dxcError)
{
diagnostic = DiagnosticInfo.Create(
HlslBytecodeFailedWithCompilationException,
structDeclarationSymbol,
structDeclarationSymbol,
dxcError.Message);
}
if (diagnostic is not null)
{
diagnostics.Add(diagnostic);
}
}
///
/// Gets the diagnostics for when double precision support is configured incorrectly.
///
/// The input instance to process.
/// The source instance.
/// The collection of produced instances.
public static void GetDoublePrecisionSupportDiagnostics(
INamedTypeSymbol structDeclarationSymbol,
HlslBytecodeInfo info,
ImmutableArrayBuilder diagnostics)
{
// If we have no compiled HLSL bytecode, there is nothing more to do
if (info is not HlslBytecodeInfo.Success success)
{
return;
}
bool hasD2DRequiresDoublePrecisionSupportAttribute = structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName(
"ComputeSharp.RequiresDoublePrecisionSupportAttribute",
out AttributeData? attributeData);
// Check the two cases where diagnostics are necessary (same as the D2D generator)
if (!hasD2DRequiresDoublePrecisionSupportAttribute && success.RequiresDoublePrecisionSupport)
{
diagnostics.Add(DiagnosticInfo.Create(
MissingRequiresDoublePrecisionSupportAttribute,
structDeclarationSymbol,
structDeclarationSymbol));
}
else if (hasD2DRequiresDoublePrecisionSupportAttribute && !success.RequiresDoublePrecisionSupport)
{
diagnostics.Add(DiagnosticInfo.Create(
UnnecessaryRequiresDoublePrecisionSupportAttribute,
attributeData!.GetLocation(),
structDeclarationSymbol));
}
}
}
}