生成器をLua/Gpuへ整理し型を1ファイル1クラス化
このコミットが含まれているのは:
24ファイル変更
+146
-119
検索条件に一致するファイルはありません
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderClass(
|
||||
string Namespace,
|
||||
ImmutableArray<GpuShaderTypePart> TypeChain,
|
||||
ImmutableArray<GpuShaderMethod> Methods,
|
||||
ImmutableArray<string> Errors);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderFile(string Path, string Source);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderInvocation(string Resources, string Constants, string Target, string Width, string Height);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderMethod(
|
||||
string Accessibility,
|
||||
bool IsStatic,
|
||||
string ReturnType,
|
||||
string MethodName,
|
||||
ImmutableArray<GpuShaderParameter> Parameters,
|
||||
string FileName,
|
||||
string EntryPoint,
|
||||
GpuShaderInvocation Invocation);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderMethodSource(GpuShaderMethod Method, string Bytecode);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderParameter(string Prefix, string Type, string Name);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderResolution(ImmutableArray<GpuShaderMethodSource> Methods, ImmutableArray<string> Errors);
|
||||
}
|
||||
-119
@@ -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<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 GpuShaderResolution(ImmutableArray<GpuShaderMethodSource> Methods, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace LuaScript.Generator
|
||||
{
|
||||
internal sealed record GpuShaderTypePart(string Name, bool IsStatic);
|
||||
}
|
||||
@@ -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<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);
|
||||
}
|
||||
}
|
||||
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
差分なしでファイル名を変更。
新しいイシューから参照
ユーザーをブロックする