From c1f4fa2dc5121c784adf4b1aa84fa1c66bbfc08e Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 5 Jul 2026 13:36:03 +0900 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E5=99=A8=E3=82=92Lua/Gpu?= =?UTF-8?q?=E3=81=B8=E6=95=B4=E7=90=86=E3=81=97=E5=9E=8B=E3=82=921?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB1=E3=82=AF=E3=83=A9?= =?UTF-8?q?=E3=82=B9=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Generator/Gpu/GpuShaderClass.cs | 10 ++ LuaScript.Generator/Gpu/GpuShaderFile.cs | 4 + .../Gpu/GpuShaderInvocation.cs | 4 + LuaScript.Generator/Gpu/GpuShaderMethod.cs | 14 +++ .../Gpu/GpuShaderMethodSource.cs | 4 + LuaScript.Generator/Gpu/GpuShaderParameter.cs | 4 + .../Gpu/GpuShaderResolution.cs | 6 + .../{ => Gpu}/GpuShaderSourceGenerator.cs | 119 ------------------ LuaScript.Generator/Gpu/GpuShaderTypePart.cs | 4 + LuaScript.Generator/Gpu/ShaderCompiler.cs | 96 ++++++++++++++ .../{ => Lua}/LuaApiGenerator.cs | 0 .../{ => Lua}/LuaCatalogEntry.cs | 0 .../{ => Lua}/LuaConstantModel.cs | 0 .../{ => Lua}/LuaFunctionModel.cs | 0 .../{ => Lua}/LuaParameterKind.cs | 0 .../{ => Lua}/LuaParameterModel.cs | 0 .../{ => Lua}/LuaReturnKind.cs | 0 .../{ => Lua}/LuaSyntaxRuleGenerator.cs | 0 .../{ => Lua}/LuaSyntaxRuleModel.cs | 0 .../{ => Lua}/LuaTableModel.cs | 0 LuaScript.Generator/{ => Lua}/LuaTypePart.cs | 0 .../{ => Lua}/LuaUpdateKind.cs | 0 .../{ => Lua}/LuaUpdateModel.cs | 0 LuaScript.Generator/{ => Lua}/LuaValueKind.cs | 0 24 files changed, 146 insertions(+), 119 deletions(-) create mode 100644 LuaScript.Generator/Gpu/GpuShaderClass.cs create mode 100644 LuaScript.Generator/Gpu/GpuShaderFile.cs create mode 100644 LuaScript.Generator/Gpu/GpuShaderInvocation.cs create mode 100644 LuaScript.Generator/Gpu/GpuShaderMethod.cs create mode 100644 LuaScript.Generator/Gpu/GpuShaderMethodSource.cs create mode 100644 LuaScript.Generator/Gpu/GpuShaderParameter.cs create mode 100644 LuaScript.Generator/Gpu/GpuShaderResolution.cs rename LuaScript.Generator/{ => Gpu}/GpuShaderSourceGenerator.cs (77%) create mode 100644 LuaScript.Generator/Gpu/GpuShaderTypePart.cs create mode 100644 LuaScript.Generator/Gpu/ShaderCompiler.cs rename LuaScript.Generator/{ => Lua}/LuaApiGenerator.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaCatalogEntry.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaConstantModel.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaFunctionModel.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaParameterKind.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaParameterModel.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaReturnKind.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaSyntaxRuleGenerator.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaSyntaxRuleModel.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaTableModel.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaTypePart.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaUpdateKind.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaUpdateModel.cs (100%) rename LuaScript.Generator/{ => Lua}/LuaValueKind.cs (100%) diff --git a/LuaScript.Generator/Gpu/GpuShaderClass.cs b/LuaScript.Generator/Gpu/GpuShaderClass.cs new file mode 100644 index 0000000..69c3466 --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderClass.cs @@ -0,0 +1,10 @@ +using System.Collections.Immutable; + +namespace LuaScript.Generator +{ + internal sealed record GpuShaderClass( + string Namespace, + ImmutableArray TypeChain, + ImmutableArray Methods, + ImmutableArray Errors); +} diff --git a/LuaScript.Generator/Gpu/GpuShaderFile.cs b/LuaScript.Generator/Gpu/GpuShaderFile.cs new file mode 100644 index 0000000..718d0ec --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderFile.cs @@ -0,0 +1,4 @@ +namespace LuaScript.Generator +{ + internal sealed record GpuShaderFile(string Path, string Source); +} diff --git a/LuaScript.Generator/Gpu/GpuShaderInvocation.cs b/LuaScript.Generator/Gpu/GpuShaderInvocation.cs new file mode 100644 index 0000000..21b644e --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderInvocation.cs @@ -0,0 +1,4 @@ +namespace LuaScript.Generator +{ + internal sealed record GpuShaderInvocation(string Resources, string Constants, string Target, string Width, string Height); +} diff --git a/LuaScript.Generator/Gpu/GpuShaderMethod.cs b/LuaScript.Generator/Gpu/GpuShaderMethod.cs new file mode 100644 index 0000000..7fa2b23 --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderMethod.cs @@ -0,0 +1,14 @@ +using System.Collections.Immutable; + +namespace LuaScript.Generator +{ + internal sealed record GpuShaderMethod( + string Accessibility, + bool IsStatic, + string ReturnType, + string MethodName, + ImmutableArray Parameters, + string FileName, + string EntryPoint, + GpuShaderInvocation Invocation); +} diff --git a/LuaScript.Generator/Gpu/GpuShaderMethodSource.cs b/LuaScript.Generator/Gpu/GpuShaderMethodSource.cs new file mode 100644 index 0000000..9541a11 --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderMethodSource.cs @@ -0,0 +1,4 @@ +namespace LuaScript.Generator +{ + internal sealed record GpuShaderMethodSource(GpuShaderMethod Method, string Bytecode); +} diff --git a/LuaScript.Generator/Gpu/GpuShaderParameter.cs b/LuaScript.Generator/Gpu/GpuShaderParameter.cs new file mode 100644 index 0000000..fbe2283 --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderParameter.cs @@ -0,0 +1,4 @@ +namespace LuaScript.Generator +{ + internal sealed record GpuShaderParameter(string Prefix, string Type, string Name); +} diff --git a/LuaScript.Generator/Gpu/GpuShaderResolution.cs b/LuaScript.Generator/Gpu/GpuShaderResolution.cs new file mode 100644 index 0000000..a7b98e7 --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderResolution.cs @@ -0,0 +1,6 @@ +using System.Collections.Immutable; + +namespace LuaScript.Generator +{ + internal sealed record GpuShaderResolution(ImmutableArray Methods, ImmutableArray Errors); +} diff --git a/LuaScript.Generator/GpuShaderSourceGenerator.cs b/LuaScript.Generator/Gpu/GpuShaderSourceGenerator.cs similarity index 77% rename from LuaScript.Generator/GpuShaderSourceGenerator.cs rename to LuaScript.Generator/Gpu/GpuShaderSourceGenerator.cs index 9d1b887..d13db3e 100644 --- a/LuaScript.Generator/GpuShaderSourceGenerator.cs +++ b/LuaScript.Generator/Gpu/GpuShaderSourceGenerator.cs @@ -1,7 +1,6 @@ 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; @@ -432,123 +431,5 @@ namespace LuaScript.Generator builder.Append('"'); return builder.ToString(); } - - private sealed record GpuShaderClass( - string Namespace, - ImmutableArray TypeChain, - ImmutableArray Methods, - ImmutableArray Errors); - - private sealed record GpuShaderTypePart(string Name, bool IsStatic); - - private sealed record GpuShaderMethod( - string Accessibility, - bool IsStatic, - string ReturnType, - string MethodName, - ImmutableArray 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 GpuShaderResolution(ImmutableArray Methods, ImmutableArray 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(blob, 3)(blob); - int size = checked((int)(long)Invoke(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(blob, 3)(blob); - int size = checked((int)(long)Invoke(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(unknown, 2)(unknown); - } - - private static T Invoke(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); - } } } diff --git a/LuaScript.Generator/Gpu/GpuShaderTypePart.cs b/LuaScript.Generator/Gpu/GpuShaderTypePart.cs new file mode 100644 index 0000000..79068b4 --- /dev/null +++ b/LuaScript.Generator/Gpu/GpuShaderTypePart.cs @@ -0,0 +1,4 @@ +namespace LuaScript.Generator +{ + internal sealed record GpuShaderTypePart(string Name, bool IsStatic); +} diff --git a/LuaScript.Generator/Gpu/ShaderCompiler.cs b/LuaScript.Generator/Gpu/ShaderCompiler.cs new file mode 100644 index 0000000..16711c2 --- /dev/null +++ b/LuaScript.Generator/Gpu/ShaderCompiler.cs @@ -0,0 +1,96 @@ +using System.Globalization; +using System.Runtime.InteropServices; +using System.Text; + +namespace LuaScript.Generator +{ + internal 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(blob, 3)(blob); + int size = checked((int)(long)Invoke(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(blob, 3)(blob); + int size = checked((int)(long)Invoke(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(unknown, 2)(unknown); + } + + private static T Invoke(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); + } +} diff --git a/LuaScript.Generator/LuaApiGenerator.cs b/LuaScript.Generator/Lua/LuaApiGenerator.cs similarity index 100% rename from LuaScript.Generator/LuaApiGenerator.cs rename to LuaScript.Generator/Lua/LuaApiGenerator.cs diff --git a/LuaScript.Generator/LuaCatalogEntry.cs b/LuaScript.Generator/Lua/LuaCatalogEntry.cs similarity index 100% rename from LuaScript.Generator/LuaCatalogEntry.cs rename to LuaScript.Generator/Lua/LuaCatalogEntry.cs diff --git a/LuaScript.Generator/LuaConstantModel.cs b/LuaScript.Generator/Lua/LuaConstantModel.cs similarity index 100% rename from LuaScript.Generator/LuaConstantModel.cs rename to LuaScript.Generator/Lua/LuaConstantModel.cs diff --git a/LuaScript.Generator/LuaFunctionModel.cs b/LuaScript.Generator/Lua/LuaFunctionModel.cs similarity index 100% rename from LuaScript.Generator/LuaFunctionModel.cs rename to LuaScript.Generator/Lua/LuaFunctionModel.cs diff --git a/LuaScript.Generator/LuaParameterKind.cs b/LuaScript.Generator/Lua/LuaParameterKind.cs similarity index 100% rename from LuaScript.Generator/LuaParameterKind.cs rename to LuaScript.Generator/Lua/LuaParameterKind.cs diff --git a/LuaScript.Generator/LuaParameterModel.cs b/LuaScript.Generator/Lua/LuaParameterModel.cs similarity index 100% rename from LuaScript.Generator/LuaParameterModel.cs rename to LuaScript.Generator/Lua/LuaParameterModel.cs diff --git a/LuaScript.Generator/LuaReturnKind.cs b/LuaScript.Generator/Lua/LuaReturnKind.cs similarity index 100% rename from LuaScript.Generator/LuaReturnKind.cs rename to LuaScript.Generator/Lua/LuaReturnKind.cs diff --git a/LuaScript.Generator/LuaSyntaxRuleGenerator.cs b/LuaScript.Generator/Lua/LuaSyntaxRuleGenerator.cs similarity index 100% rename from LuaScript.Generator/LuaSyntaxRuleGenerator.cs rename to LuaScript.Generator/Lua/LuaSyntaxRuleGenerator.cs diff --git a/LuaScript.Generator/LuaSyntaxRuleModel.cs b/LuaScript.Generator/Lua/LuaSyntaxRuleModel.cs similarity index 100% rename from LuaScript.Generator/LuaSyntaxRuleModel.cs rename to LuaScript.Generator/Lua/LuaSyntaxRuleModel.cs diff --git a/LuaScript.Generator/LuaTableModel.cs b/LuaScript.Generator/Lua/LuaTableModel.cs similarity index 100% rename from LuaScript.Generator/LuaTableModel.cs rename to LuaScript.Generator/Lua/LuaTableModel.cs diff --git a/LuaScript.Generator/LuaTypePart.cs b/LuaScript.Generator/Lua/LuaTypePart.cs similarity index 100% rename from LuaScript.Generator/LuaTypePart.cs rename to LuaScript.Generator/Lua/LuaTypePart.cs diff --git a/LuaScript.Generator/LuaUpdateKind.cs b/LuaScript.Generator/Lua/LuaUpdateKind.cs similarity index 100% rename from LuaScript.Generator/LuaUpdateKind.cs rename to LuaScript.Generator/Lua/LuaUpdateKind.cs diff --git a/LuaScript.Generator/LuaUpdateModel.cs b/LuaScript.Generator/Lua/LuaUpdateModel.cs similarity index 100% rename from LuaScript.Generator/LuaUpdateModel.cs rename to LuaScript.Generator/Lua/LuaUpdateModel.cs diff --git a/LuaScript.Generator/LuaValueKind.cs b/LuaScript.Generator/Lua/LuaValueKind.cs similarity index 100% rename from LuaScript.Generator/LuaValueKind.cs rename to LuaScript.Generator/Lua/LuaValueKind.cs