GPU内部シェーダーをビルド時DXBC化

このコミットが含まれているのは:
routersys committed 2026-07-05 13:22:06 +09:00
1 親 406a4c1a1b
コミット af9c5af173
9ファイル変更
+868 -447

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

+414 -80
ファイルの表示
@@ -1,8 +1,10 @@
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace LuaScript.Generator
@@ -14,83 +16,171 @@ namespace LuaScript.Generator
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var registrations = context.CompilationProvider.Select(static (compilation, _) => CollectRegistrations(compilation));
var classes = context.SyntaxProvider
.CreateSyntaxProvider(
static (node, _) => node is ClassDeclarationSyntax { AttributeLists.Count: > 0 },
static (ctx, _) => CreateClass(ctx))
.Where(static model => model is not null)
.Select(static (model, _) => model!);
var files = context.AdditionalTextsProvider
.Where(static file => file.Path.EndsWith(".hlsl", StringComparison.OrdinalIgnoreCase))
.Select(static (file, token) => new GpuShaderFile(Normalize(file.Path), file.GetText(token)?.ToString() ?? string.Empty))
.Collect();
context.RegisterSourceOutput(registrations.Combine(files), static (context, source) =>
context.RegisterSourceOutput(classes.Combine(files), static (context, source) =>
{
var (registrations, files) = source;
if (registrations.Length == 0)
return;
var result = Resolve(registrations, files);
if (result.Errors.Length > 0)
var (model, files) = source;
var resolved = Resolve(model, files);
if (resolved.Errors.Length > 0)
{
context.AddSource("GpuPixelOperationShaderRegistry.Errors.g.cs", SourceText.From(EmitErrors(result.Errors), Encoding.UTF8));
context.AddSource(ErrorHintName(model), SourceText.From(EmitErrors(resolved.Errors), Encoding.UTF8));
return;
}
if (result.Sources.IsDefaultOrEmpty)
return;
context.AddSource("GpuPixelOperationShaderRegistry.g.cs", SourceText.From(Emit(result.Sources), Encoding.UTF8));
context.AddSource(HintName(model), SourceText.From(Emit(model, resolved.Methods), Encoding.UTF8));
});
}
private static ImmutableArray<GpuShaderRegistration> CollectRegistrations(Compilation compilation)
private static GpuShaderClass? CreateClass(GeneratorSyntaxContext context)
{
var registrations = ImmutableArray.CreateBuilder<GpuShaderRegistration>();
foreach (var attribute in compilation.Assembly.GetAttributes())
{
if (attribute.AttributeClass?.ToDisplayString() != AttributeName || attribute.ConstructorArguments.Length < 2)
continue;
if (attribute.ConstructorArguments[0].Value is not string apiName || attribute.ConstructorArguments[1].Value is not string fileName)
continue;
registrations.Add(new GpuShaderRegistration(apiName, Normalize(fileName), attribute.ApplicationSyntaxReference?.Span.Start ?? 0));
}
return registrations.ToImmutable();
}
if (context.Node is not ClassDeclarationSyntax declaration)
return null;
if (context.SemanticModel.GetDeclaredSymbol(declaration) is not INamedTypeSymbol type)
return null;
private static GpuShaderResolution Resolve(ImmutableArray<GpuShaderRegistration> registrations, ImmutableArray<GpuShaderFile> files)
{
var apiNames = new HashSet<string>(StringComparer.Ordinal);
var identifiers = new HashSet<string>(StringComparer.Ordinal);
var sources = ImmutableArray.CreateBuilder<GpuShaderSource>();
var attributes = type.GetAttributes()
.Where(static attribute => attribute.AttributeClass?.ToDisplayString() == AttributeName)
.ToArray();
if (attributes.Length == 0)
return null;
var chain = new List<GpuShaderTypePart>();
for (var current = type; current is not null; current = current.ContainingType)
chain.Insert(0, new GpuShaderTypePart(current.Name, current.IsStatic));
var methods = ImmutableArray.CreateBuilder<GpuShaderMethod>();
var errors = ImmutableArray.CreateBuilder<string>();
foreach (var registration in registrations.OrderBy(static r => r.Position).ThenBy(static r => r.ApiName, StringComparer.Ordinal))
foreach (var attribute in attributes)
{
if (!apiNames.Add(registration.ApiName))
if (attribute.ConstructorArguments.Length < 3 ||
attribute.ConstructorArguments[0].Value is not string methodName ||
attribute.ConstructorArguments[1].Value is not string fileName ||
attribute.ConstructorArguments[2].Value is not string entryPoint)
{
errors.Add($"GPU shader API '{registration.ApiName}' is registered multiple times");
errors.Add("GPU shader attribute arguments are invalid");
continue;
}
var matched = files.Where(file => Matches(file.Path, registration.FileName)).ToArray();
var candidates = type.GetMembers(methodName).OfType<IMethodSymbol>().ToArray();
if (candidates.Length == 0)
{
errors.Add($"GPU shader method '{methodName}' was not found");
continue;
}
if (candidates.Length > 1)
{
errors.Add($"GPU shader method '{methodName}' is ambiguous");
continue;
}
var method = candidates[0];
string? error = Validate(method, fileName, entryPoint, out var invocation);
if (error is not null)
{
errors.Add($"GPU shader method '{methodName}': {error}");
continue;
}
var parameters = ImmutableArray.CreateBuilder<GpuShaderParameter>();
foreach (var parameter in method.Parameters)
parameters.Add(new GpuShaderParameter(ParameterPrefix(parameter), parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), parameter.Name));
methods.Add(new GpuShaderMethod(
AccessibilityName(method.DeclaredAccessibility),
method.IsStatic,
method.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
method.Name,
[.. parameters],
Normalize(fileName),
entryPoint,
invocation));
}
return new GpuShaderClass(
type.ContainingNamespace.IsGlobalNamespace ? string.Empty : type.ContainingNamespace.ToDisplayString(),
[.. chain],
[.. methods],
[.. errors]);
}
private static string? Validate(IMethodSymbol method, string fileName, string entryPoint, out GpuShaderInvocation invocation)
{
invocation = new GpuShaderInvocation(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty);
if (string.IsNullOrWhiteSpace(fileName))
return "file name is empty";
if (string.IsNullOrWhiteSpace(entryPoint))
return "entry point is empty";
if (method.ReturnType.SpecialType != SpecialType.System_Boolean)
return "return type must be bool";
var parameters = method.Parameters;
if (parameters.Length == 4 &&
IsReadOnlySpanSingle(parameters[0].Type) &&
IsByteArray(parameters[1].Type) &&
IsInt32(parameters[2].Type) &&
IsInt32(parameters[3].Type))
{
invocation = new GpuShaderInvocation("[]", parameters[0].Name, parameters[1].Name, parameters[2].Name, parameters[3].Name);
return null;
}
if (parameters.Length == 5 &&
IsReadOnlySpanPixelShaderInput(parameters[0].Type) &&
IsReadOnlySpanSingle(parameters[1].Type) &&
IsByteArray(parameters[2].Type) &&
IsInt32(parameters[3].Type) &&
IsInt32(parameters[4].Type))
{
invocation = new GpuShaderInvocation(parameters[0].Name, parameters[1].Name, parameters[2].Name, parameters[3].Name, parameters[4].Name);
return null;
}
return "signature is invalid";
}
private static GpuShaderResolution Resolve(GpuShaderClass model, ImmutableArray<GpuShaderFile> files)
{
var methods = ImmutableArray.CreateBuilder<GpuShaderMethodSource>();
var errors = ImmutableArray.CreateBuilder<string>();
errors.AddRange(model.Errors);
foreach (var method in model.Methods)
{
var matched = files.Where(file => Matches(file.Path, method.FileName)).ToArray();
if (matched.Length == 0)
{
errors.Add($"GPU shader file '{registration.FileName}' for API '{registration.ApiName}' was not found");
errors.Add($"GPU shader file '{method.FileName}' for method '{method.MethodName}' was not found");
continue;
}
if (matched.Length > 1)
{
errors.Add($"GPU shader file '{registration.FileName}' for API '{registration.ApiName}' matched multiple additional files");
errors.Add($"GPU shader file '{method.FileName}' for method '{method.MethodName}' matched multiple additional files");
continue;
}
string identifier = ToIdentifier(registration.ApiName);
if (!identifiers.Add(identifier))
string? failure = ShaderCompiler.Compile(matched[0].Source, method.EntryPoint, out string bytecode);
if (failure is not null)
{
errors.Add($"GPU shader API '{registration.ApiName}' resolves to a duplicate identifier");
errors.Add($"GPU shader method '{method.MethodName}' failed to compile: {failure}");
continue;
}
sources.Add(new GpuShaderSource(registration.ApiName, identifier, matched[0].Source));
methods.Add(new GpuShaderMethodSource(method, bytecode));
}
return new GpuShaderResolution(sources.ToImmutable(), errors.ToImmutable());
return new GpuShaderResolution([.. methods], [.. errors]);
}
private static bool Matches(string filePath, string registrationPath)
@@ -100,52 +190,156 @@ namespace LuaScript.Generator
return filePath.EndsWith("/" + registrationPath, StringComparison.OrdinalIgnoreCase);
}
private static string Normalize(string path) => path.Replace(Path.DirectorySeparatorChar, '/').Replace(Path.AltDirectorySeparatorChar, '/');
private static string ToIdentifier(string apiName)
private static bool IsReadOnlySpanSingle(ITypeSymbol type)
{
var builder = new StringBuilder();
bool upper = true;
foreach (char c in apiName)
{
if (!char.IsLetterOrDigit(c))
{
upper = true;
continue;
}
builder.Append(upper ? char.ToUpperInvariant(c) : c);
upper = false;
}
if (builder.Length == 0 || char.IsDigit(builder[0]))
builder.Insert(0, '_');
return builder.ToString();
return type is INamedTypeSymbol named &&
named.OriginalDefinition.ToDisplayString() == "System.ReadOnlySpan<T>" &&
named.TypeArguments.Length == 1 &&
named.TypeArguments[0].SpecialType == SpecialType.System_Single;
}
private static string Emit(ImmutableArray<GpuShaderSource> sources)
private static bool IsReadOnlySpanPixelShaderInput(ITypeSymbol type)
{
string display = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
return display.Contains("ReadOnlySpan", StringComparison.Ordinal) &&
display.Contains("PixelShaderInput", StringComparison.Ordinal);
}
private static bool IsByteArray(ITypeSymbol type)
{
return type is IArrayTypeSymbol array &&
array.Rank == 1 &&
array.ElementType.SpecialType == SpecialType.System_Byte;
}
private static bool IsInt32(ITypeSymbol type) => type.SpecialType == SpecialType.System_Int32;
private static string Normalize(string path) => path.Replace(Path.DirectorySeparatorChar, '/').Replace(Path.AltDirectorySeparatorChar, '/');
private static string AccessibilityName(Accessibility accessibility)
{
return accessibility switch
{
Accessibility.Public => "public",
Accessibility.Internal => "internal",
Accessibility.Protected => "protected",
Accessibility.ProtectedAndInternal => "private protected",
Accessibility.ProtectedOrInternal => "protected internal",
_ => "private",
};
}
private static string ParameterPrefix(IParameterSymbol parameter)
{
return parameter.RefKind switch
{
RefKind.In => "in ",
RefKind.Ref => "ref ",
RefKind.Out => "out ",
_ => string.Empty,
};
}
private static string Emit(GpuShaderClass model, ImmutableArray<GpuShaderMethodSource> sources)
{
var builder = new StringBuilder();
builder.AppendLine("#nullable enable");
builder.AppendLine("namespace LuaScript.Engine.Processing");
builder.AppendLine("{");
builder.AppendLine(" internal static class GpuPixelOperationShaderRegistry");
builder.AppendLine(" {");
builder.AppendLine(" internal static bool TryGet(string apiName, out string source)");
builder.AppendLine(" {");
builder.AppendLine(" switch (apiName)");
builder.AppendLine(" {");
foreach (var source in sources)
builder.AppendLine($" case {Literal(source.ApiName)}: source = {source.Identifier}; return true;");
builder.AppendLine(" default: source = string.Empty; return false;");
builder.AppendLine(" }");
builder.AppendLine(" }");
builder.AppendLine();
foreach (var source in sources)
builder.AppendLine($" internal const string {source.Identifier} = {Literal(source.Source)};");
builder.AppendLine(" }");
builder.AppendLine("}");
if (model.Namespace.Length > 0)
{
builder.AppendLine("namespace " + model.Namespace);
builder.AppendLine("{");
}
int indent = model.Namespace.Length > 0 ? 1 : 0;
foreach (var type in model.TypeChain)
{
AppendIndent(builder, indent);
builder.Append(type.IsStatic ? "static partial class " : "partial class ");
builder.AppendLine(type.Name);
AppendIndent(builder, indent);
builder.AppendLine("{");
indent++;
}
for (int i = 0; i < sources.Length; i++)
{
if (i > 0)
builder.AppendLine();
EmitMethod(builder, indent, sources[i]);
}
for (int i = model.TypeChain.Length - 1; i >= 0; i--)
{
indent--;
AppendIndent(builder, indent);
builder.AppendLine("}");
}
if (model.Namespace.Length > 0)
builder.AppendLine("}");
return builder.ToString();
}
private static void EmitMethod(StringBuilder builder, int indent, GpuShaderMethodSource source)
{
var method = source.Method;
string field = "s_shader_" + method.MethodName;
AppendIndent(builder, indent);
builder.Append("private static readonly byte[] ");
builder.Append(field);
builder.Append(" = global::System.Convert.FromBase64String(");
builder.Append(Literal(source.Bytecode));
builder.AppendLine(");");
builder.AppendLine();
AppendIndent(builder, indent);
builder.Append(method.Accessibility);
builder.Append(' ');
if (method.IsStatic)
builder.Append("static ");
builder.Append("partial ");
builder.Append(method.ReturnType);
builder.Append(' ');
builder.Append(method.MethodName);
builder.Append('(');
for (int i = 0; i < method.Parameters.Length; i++)
{
if (i > 0)
builder.Append(", ");
var parameter = method.Parameters[i];
builder.Append(parameter.Prefix);
builder.Append(parameter.Type);
builder.Append(' ');
builder.Append(parameter.Name);
}
builder.AppendLine(")");
AppendIndent(builder, indent);
builder.AppendLine("{");
AppendIndent(builder, indent + 1);
builder.Append("return Run(");
builder.Append(field);
builder.Append(", ");
builder.Append(method.Invocation.Resources);
builder.Append(", ");
builder.Append(method.Invocation.Constants);
builder.Append(", ");
builder.Append(method.Invocation.Target);
builder.Append(", ");
builder.Append(method.Invocation.Width);
builder.Append(", ");
builder.Append(method.Invocation.Height);
builder.AppendLine(");");
AppendIndent(builder, indent);
builder.AppendLine("}");
}
private static void AppendIndent(StringBuilder builder, int indent)
{
for (int i = 0; i < indent; i++)
builder.Append(" ");
}
private static string EmitErrors(ImmutableArray<string> errors)
{
var builder = new StringBuilder();
@@ -154,6 +348,36 @@ namespace LuaScript.Generator
return builder.ToString();
}
private static string HintName(GpuShaderClass model)
{
var builder = new StringBuilder("GpuPixelOperationShader");
if (model.Namespace.Length > 0)
{
builder.Append('.');
builder.Append(HintPart(model.Namespace));
}
foreach (var type in model.TypeChain)
{
builder.Append('.');
builder.Append(HintPart(type.Name));
}
builder.Append(".g.cs");
return builder.ToString();
}
private static string ErrorHintName(GpuShaderClass model)
{
return Path.ChangeExtension(HintName(model), ".Errors.g.cs");
}
private static string HintPart(string value)
{
var builder = new StringBuilder(value.Length);
foreach (char c in value)
builder.Append(char.IsLetterOrDigit(c) ? c : '_');
return builder.ToString();
}
private static string Literal(string value)
{
var builder = new StringBuilder(value.Length + 2);
@@ -209,12 +433,122 @@ namespace LuaScript.Generator
return builder.ToString();
}
private sealed record GpuShaderRegistration(string ApiName, string FileName, int Position);
private sealed record GpuShaderClass(
string Namespace,
ImmutableArray<GpuShaderTypePart> TypeChain,
ImmutableArray<GpuShaderMethod> Methods,
ImmutableArray<string> Errors);
private sealed record GpuShaderTypePart(string Name, bool IsStatic);
private sealed record GpuShaderMethod(
string Accessibility,
bool IsStatic,
string ReturnType,
string MethodName,
ImmutableArray<GpuShaderParameter> Parameters,
string FileName,
string EntryPoint,
GpuShaderInvocation Invocation);
private sealed record GpuShaderParameter(string Prefix, string Type, string Name);
private sealed record GpuShaderInvocation(string Resources, string Constants, string Target, string Width, string Height);
private sealed record GpuShaderMethodSource(GpuShaderMethod Method, string Bytecode);
private sealed record GpuShaderFile(string Path, string Source);
private sealed record GpuShaderSource(string ApiName, string Identifier, string Source);
private sealed record GpuShaderResolution(ImmutableArray<GpuShaderMethodSource> Methods, ImmutableArray<string> Errors);
private sealed record GpuShaderResolution(ImmutableArray<GpuShaderSource> Sources, ImmutableArray<string> Errors);
private static class ShaderCompiler
{
private const string Profile = "ps_5_0";
public static string? Compile(string source, string entryPoint, out string bytecode)
{
bytecode = string.Empty;
byte[] data = Encoding.UTF8.GetBytes(source);
IntPtr code = IntPtr.Zero;
IntPtr errors = IntPtr.Zero;
try
{
int hr = D3DCompile(data, (IntPtr)data.Length, null, IntPtr.Zero, IntPtr.Zero, entryPoint, Profile, 0u, 0u, out code, out errors);
if (hr < 0 || code == IntPtr.Zero)
return ReadError(errors, hr);
bytecode = Convert.ToBase64String(ReadBlob(code));
return null;
}
catch (DllNotFoundException)
{
return "the Direct3D shader compiler is unavailable on this build host";
}
catch (EntryPointNotFoundException)
{
return "the Direct3D shader compiler is unavailable on this build host";
}
finally
{
Release(errors);
Release(code);
}
}
private static byte[] ReadBlob(IntPtr blob)
{
IntPtr pointer = Invoke<GetBufferPointerDelegate>(blob, 3)(blob);
int size = checked((int)(long)Invoke<GetBufferSizeDelegate>(blob, 4)(blob));
var buffer = new byte[size];
Marshal.Copy(pointer, buffer, 0, size);
return buffer;
}
private static string ReadError(IntPtr blob, int hr)
{
string fallback = "HRESULT 0x" + hr.ToString("X8", CultureInfo.InvariantCulture);
if (blob == IntPtr.Zero)
return fallback;
IntPtr pointer = Invoke<GetBufferPointerDelegate>(blob, 3)(blob);
int size = checked((int)(long)Invoke<GetBufferSizeDelegate>(blob, 4)(blob));
string message = (Marshal.PtrToStringAnsi(pointer, size) ?? string.Empty).Trim();
return message.Length > 0 ? message : fallback;
}
private static void Release(IntPtr unknown)
{
if (unknown != IntPtr.Zero)
Invoke<ReleaseDelegate>(unknown, 2)(unknown);
}
private static T Invoke<T>(IntPtr instance, int slot) where T : Delegate
{
IntPtr table = Marshal.ReadIntPtr(instance);
IntPtr function = Marshal.ReadIntPtr(table, slot * IntPtr.Size);
return (T)Marshal.GetDelegateForFunctionPointer(function, typeof(T));
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate IntPtr GetBufferPointerDelegate(IntPtr self);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate IntPtr GetBufferSizeDelegate(IntPtr self);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate uint ReleaseDelegate(IntPtr self);
[DllImport("d3dcompiler_47.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int D3DCompile(
byte[] srcData,
IntPtr srcDataSize,
[MarshalAs(UnmanagedType.LPStr)] string? sourceName,
IntPtr defines,
IntPtr include,
[MarshalAs(UnmanagedType.LPStr)] string entryPoint,
[MarshalAs(UnmanagedType.LPStr)] string target,
uint flags1,
uint flags2,
out IntPtr code,
out IntPtr errorMsgs);
}
}
}