Ported DirectX12GameEngine.Shaders project

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-10 20:07:30 +02:00
1 親 b3faa3ddd2
コミット 8e0931c46b
18ファイル変更
+1794

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

+9
ファイルの表示
@@ -7,6 +7,15 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetDxc" Version="1.2.0-alpha">
<ExcludeAssets>native</ExcludeAssets>
</PackageReference>
<PackageReference Include="ICSharpCode.Decompiler" Version="5.0.0.4861-preview3" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.3.0-beta1-final" />
<PackageReference Include="SharpDX.D3DCompiler" Version="4.2.1-beta0-g3060495c2a" />
</ItemGroup>
<ItemGroup>
<None Remove="Dlls\win-x64\dxcompiler.dll" />
<None Remove="Dlls\win-x64\dxil.dll" />
+122
ファイルの表示
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace DirectX12GameEngine.Shaders
{
public static class MemberInfoExtensions
{
public static Type GetElementOrDeclaredType(this Type type) => type.IsArray ? type.GetElementType() : type;
public static bool IsOverride(this MethodInfo methodInfo)
{
return methodInfo != methodInfo.GetBaseDefinition();
}
public static IList<MethodInfo> GetBaseMethods(this MethodInfo methodInfo)
{
List<MethodInfo> methodInfos = new List<MethodInfo>() { methodInfo };
if (methodInfo.IsOverride())
{
MethodInfo? currentMethodInfo = methodInfo.DeclaringType.BaseType?.GetMethod(methodInfo.Name);
while (currentMethodInfo != null)
{
methodInfos.Add(currentMethodInfo);
currentMethodInfo = currentMethodInfo.DeclaringType.BaseType?.GetMethod(currentMethodInfo.Name);
}
}
return methodInfos;
}
public static IEnumerable<Type> GetNestedTypesInTypeHierarchy(this Type type, BindingFlags bindingAttr)
{
IEnumerable<Type> nestedTypes = type.GetNestedTypes(bindingAttr);
Type parent = type.BaseType;
while (parent != null)
{
nestedTypes = parent.GetNestedTypes(bindingAttr).Concat(nestedTypes);
parent = parent.BaseType;
}
return nestedTypes;
}
public static IEnumerable<MemberInfo> GetMembersInTypeHierarchy(this Type type, BindingFlags bindingAttr)
{
Dictionary<Type, int> lookup = new Dictionary<Type, int>();
int index = 0;
lookup[type] = index++;
Type parent = type.BaseType;
while (parent != null)
{
lookup[parent] = index;
index++;
parent = parent.BaseType;
}
return type.GetMembers(bindingAttr).OrderByDescending(prop => lookup[prop.DeclaringType]);
}
public static IEnumerable<MemberInfo> GetMembersInTypeHierarchyInOrder(this Type type, BindingFlags bindingAttr)
{
return type.GetMembersInTypeHierarchy(bindingAttr)
.GroupBy(m => m.DeclaringType)
.Select(g => g.OrderBy(m => m.GetCustomAttribute<ShaderResourceAttribute>()?.Order))
.SelectMany(m => m);
}
public static IEnumerable<MemberInfo> GetMembersInOrder(this Type type, BindingFlags bindingAttr)
{
return type.GetMembers(bindingAttr)
.OrderBy(m => m.GetCustomAttribute<ShaderResourceAttribute>()?.Order);
}
public static object? GetMemberValue(this MemberInfo memberInfo, object? obj) => memberInfo switch
{
FieldInfo fieldInfo => obj is null ? null : fieldInfo.GetValue(obj),
PropertyInfo propertyInfo => obj is null ? null : propertyInfo.GetValue(obj),
_ => null
};
public static Type? GetMemberType(this MemberInfo memberInfo, object? obj) => memberInfo switch
{
FieldInfo fieldInfo => obj is null ? fieldInfo.FieldType : fieldInfo.GetValue(obj)?.GetType() ?? fieldInfo.FieldType,
PropertyInfo propertyInfo => obj is null ? propertyInfo.PropertyType : propertyInfo.GetValue(obj)?.GetType() ?? propertyInfo.PropertyType,
_ => null
};
public static Type? GetMemberType(this MemberInfo memberInfo) => memberInfo switch
{
FieldInfo fieldInfo => fieldInfo.FieldType,
PropertyInfo propertyInfo => propertyInfo.PropertyType,
_ => null
};
public static ShaderResourceAttribute? GetResourceAttribute(this MemberInfo memberInfo, Type? memberType)
{
ShaderResourceAttribute? resourceType = memberInfo.GetCustomAttribute<ShaderResourceAttribute>();
if (resourceType is null) return null;
return resourceType.Override
? resourceType
: memberType?.GetCustomAttribute<ShaderResourceAttribute>() ?? resourceType;
}
public static bool IsStatic(this MemberInfo memberInfo) => memberInfo switch
{
FieldInfo fieldInfo => fieldInfo.IsStatic,
PropertyInfo propertyInfo => propertyInfo.GetAccessors(true)[0].IsStatic,
_ => false
};
}
}
+9
ファイルの表示
@@ -0,0 +1,9 @@
namespace DirectX12GameEngine.Shaders.Numerics
{
public struct UInt2
{
public uint X;
public uint Y;
}
}
+13
ファイルの表示
@@ -0,0 +1,13 @@
namespace DirectX12GameEngine.Shaders.Numerics
{
public struct UInt3
{
public uint X;
public uint Y;
public uint Z;
public UInt2 XY => new UInt2 { X = X, Y = Y };
}
}
+7
ファイルの表示
@@ -0,0 +1,7 @@
namespace DirectX12GameEngine.Shaders.Numerics
{
public struct Vector2
{
public static float Length(System.Numerics.Vector2 value) => value.Length();
}
}
+28
ファイルの表示
@@ -0,0 +1,28 @@
namespace DirectX12GameEngine.Shaders.Numerics
{
public struct Vector4
{
private readonly float[] memory;
public Vector4(float x, float y, float z, float w)
{
memory = new float[] { x, y, z, w };
}
public float this[int i]
{
get => memory[i];
set => memory[i] = value;
}
public static implicit operator System.Numerics.Vector4(Vector4 value)
{
return new System.Numerics.Vector4(value[0], value[1], value[2], value[3]);
}
public static implicit operator Vector4(System.Numerics.Vector4 value)
{
return new Vector4(value.X, value.Y, value.Z, value.W);
}
}
}
+64
ファイルの表示
@@ -0,0 +1,64 @@
using System.Numerics;
namespace DirectX12GameEngine.Shaders
{
public abstract class ShaderBase
{
[ShaderMethod]
[Shader("vertex")]
public virtual VSOutput VSMain(VSInput input)
{
return default;
}
[ShaderMethod]
[Shader("pixel")]
public virtual PSOutput PSMain(PSInput input)
{
return default;
}
}
public struct VSInput
{
[ShaderResource] [PositionSemantic] public Vector3 Position;
[ShaderResource] [NormalSemantic] public Vector3 Normal;
[ShaderResource] [TangentSemantic] public Vector4 Tangent;
[ShaderResource] [TextureCoordinateSemantic] public Vector2 TexCoord;
[ShaderResource] [SystemInstanceIdSemantic] public uint InstanceId;
}
public struct VSOutput
{
[ShaderResource] [PositionSemantic] public Vector4 PositionWS;
[ShaderResource] [NormalSemantic(0)] public Vector3 Normal;
[ShaderResource] [NormalSemantic(1)] public Vector3 NormalWS;
[ShaderResource] [TangentSemantic] public Vector4 Tangent;
[ShaderResource] [TextureCoordinateSemantic] public Vector2 TexCoord;
[ShaderResource] [SystemPositionSemantic] public Vector4 ShadingPosition;
[ShaderResource] [SystemInstanceIdSemantic] public uint InstanceId;
[ShaderResource] [SystemRenderTargetArrayIndexSemantic] public uint TargetId;
}
public struct PSInput
{
[ShaderResource] [PositionSemantic] public Vector4 PositionWS;
[ShaderResource] [NormalSemantic(0)] public Vector3 Normal;
[ShaderResource] [NormalSemantic(1)] public Vector3 NormalWS;
[ShaderResource] [TangentSemantic] public Vector4 Tangent;
[ShaderResource] [TextureCoordinateSemantic] public Vector2 TexCoord;
[ShaderResource] [SystemPositionSemantic] public Vector4 ShadingPosition;
[ShaderResource] [SystemInstanceIdSemantic] public uint InstanceId;
[ShaderResource] [SystemRenderTargetArrayIndexSemantic] public uint TargetId;
[ShaderResource] [SystemIsFrontFaceSemantic] public bool IsFrontFace;
}
public struct PSOutput
{
[ShaderResource] [SystemTargetSemantic] public Vector4 ColorTarget;
}
}
+95
ファイルの表示
@@ -0,0 +1,95 @@
using System;
using System.IO;
using DotNetDxc;
using SharpDX.D3DCompiler;
namespace DirectX12GameEngine.Shaders
{
public static class ShaderCompiler
{
static ShaderCompiler()
{
HlslDxcLib.DxcCreateInstanceFn = DefaultDxcLib.GetDxcCreateInstanceFn();
}
public static byte[] CompileShaderFile(string filePath, ShaderVersion? version = null, string? entryPoint = null)
{
string shaderSource = File.ReadAllText(filePath);
return CompileShader(shaderSource, version, entryPoint, filePath);
}
public static byte[] CompileShader(string shaderSource, ShaderVersion? version = null, string? entryPoint = null, string filePath = "")
{
IDxcCompiler compiler = HlslDxcLib.CreateDxcCompiler();
IDxcLibrary library = HlslDxcLib.CreateDxcLibrary();
const uint CP_UTF16 = 1200;
IDxcBlobEncoding sourceBlob = library.CreateBlobWithEncodingOnHeapCopy(shaderSource, (uint)(shaderSource.Length * 2), CP_UTF16);
IDxcOperationResult result = compiler.Compile(sourceBlob, filePath, entryPoint ?? GetDefaultEntryPoint(version), $"{GetShaderProfile(version)}_6_1", new[] { "-Zpr" }, 1, null, 0, library.CreateIncludeHandler());
if (result.GetStatus() == 0)
{
IDxcBlob blob = result.GetResult();
byte[] bytecode = GetBytesFromBlob(blob);
return bytecode;
}
else
{
string resultText = GetStringFromBlob(library, result.GetErrors());
throw new Exception(resultText);
}
}
public static byte[] CompileShaderLegacy(string shaderSource, ShaderVersion? version = null, string? entryPoint = null)
{
return ShaderBytecode.Compile(shaderSource, entryPoint ?? GetDefaultEntryPoint(version), $"{GetShaderProfile(version)}_5_1", ShaderFlags.PackMatrixRowMajor);
}
public static unsafe byte[] GetBytesFromBlob(IDxcBlob blob)
{
byte* pMem = (byte*)blob.GetBufferPointer();
uint size = blob.GetBufferSize();
byte[] result = new byte[size];
fixed (byte* pTarget = result)
{
for (uint i = 0; i < size; i++)
{
pTarget[i] = pMem[i];
}
}
return result;
}
public static unsafe string GetStringFromBlob(IDxcLibrary library, IDxcBlob blob)
{
blob = library.GetBlobAstUf16(blob);
return new string(blob.GetBufferPointer(), 0, (int)(blob.GetBufferSize() / 2));
}
private static string GetDefaultEntryPoint(ShaderVersion? version) => version switch
{
ShaderVersion.ComputeShader => "CSMain",
ShaderVersion.VertexShader => "VSMain",
ShaderVersion.PixelShader => "PSMain",
ShaderVersion.HullShader => "HSMain",
ShaderVersion.DomainShader => "DSMain",
ShaderVersion.GeometryShader => "GSMain",
_ => ""
};
private static string GetShaderProfile(ShaderVersion? version) => version switch
{
ShaderVersion.ComputeShader => "cs",
ShaderVersion.VertexShader => "vs",
ShaderVersion.PixelShader => "ps",
ShaderVersion.HullShader => "hs",
ShaderVersion.DomainShader => "ds",
ShaderVersion.GeometryShader => "gs",
_ => "lib"
};
}
}
+53
ファイルの表示
@@ -0,0 +1,53 @@
using System;
using System.Reflection;
namespace DirectX12GameEngine.Shaders
{
public class ShaderGenerationResult
{
public ShaderGenerationResult(string shaderSource)
{
ShaderSource = shaderSource;
}
public string ShaderSource { get; }
public string? ComputeShader { get; set; }
public string? VertexShader { get; set; }
public string? PixelShader { get; set; }
public string? HullShader { get; set; }
public string? DomainShader { get; set; }
public string? GeometryShader { get; set; }
public string? RayGenerationShader { get; set; }
public string? IntersectionShader { get; set; }
public string? AnyHitShader { get; set; }
public string? ClosestHitShader { get; set; }
public string? MissShader { get; set; }
public string? CallableShader { get; set; }
internal void SetShader(string shaderName, string methodName)
{
foreach (PropertyInfo propertyInfo in GetType().GetProperties())
{
if (shaderName == propertyInfo.Name.Replace("Shader", "").ToLower())
{
propertyInfo.SetValue(this, methodName);
return;
}
}
throw new NotSupportedException("Attribute 'Shader' must have one of these values: compute, vertex, pixel, hull, domain, geometry, raygeneration, intersection, anyhit, closesthit, miss, callable.");
}
}
}
+884
ファイルの表示
@@ -0,0 +1,884 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text.RegularExpressions;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace DirectX12GameEngine.Shaders
{
public class ShaderGenerator
{
private static readonly object compilationLock = new object();
private static readonly Dictionary<string, CSharpDecompiler> decompilers = new Dictionary<string, CSharpDecompiler>();
private static readonly IEnumerable<PEFile> peFiles;
private static readonly Dictionary<Type, (PEFile PEFile, TypeDefinition TypeDefinition)> typeDefinitions = new Dictionary<Type, (PEFile, TypeDefinition)>();
private static Compilation compilation;
private readonly List<ShaderTypeDefinition> collectedTypes = new List<ShaderTypeDefinition>();
private readonly BindingFlags bindingAttr;
private readonly HlslBindingTracker bindingTracker = new HlslBindingTracker();
private readonly object shader;
private readonly StringWriter stringWriter = new StringWriter();
private readonly IndentedTextWriter writer;
private ShaderGenerationResult? result;
static ShaderGenerator()
{
IEnumerable<string> assemblyPaths;
if (!string.IsNullOrEmpty(Assembly.GetEntryAssembly().Location))
{
assemblyPaths = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic).Select(a => a.Location);
}
else
{
assemblyPaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll").Where(p =>
{
try
{
PEFile peFile = new PEFile(p);
return true;
}
catch
{
return false;
}
});
}
var metadataReferences = assemblyPaths.Select(p => MetadataReference.CreateFromFile(p));
peFiles = assemblyPaths.Select(p => new PEFile(p));
compilation = CSharpCompilation.Create("ShaderAssembly").WithReferences(metadataReferences);
AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad;
}
private static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs e)
{
if (!e.LoadedAssembly.IsDynamic)
{
PortableExecutableReference metadataReference = MetadataReference.CreateFromFile(e.LoadedAssembly.Location);
compilation = compilation.AddReferences(metadataReference);
}
}
public ShaderGenerator(object shader, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
{
this.shader = shader;
this.bindingAttr = bindingAttr;
writer = new IndentedTextWriter(stringWriter);
}
public bool IsGenerated => result != null;
public void AddType(Type type)
{
CollectStructure(type, null);
}
public ShaderGenerationResult GenerateShader()
{
if (result != null) return result;
Type shaderType = shader.GetType();
var memberInfos = shaderType.GetMembersInTypeHierarchyInOrder(bindingAttr).Where(m => m.IsDefined(typeof(ShaderResourceAttribute)));
// Collecting stage
foreach (MemberInfo memberInfo in memberInfos)
{
Type? memberType = memberInfo.GetMemberType(shader);
if (memberType != null)
{
CollectStructure(memberType, memberInfo.GetMemberValue(shader));
}
ShaderResourceAttribute? resourceType = memberInfo.GetResourceAttribute(memberType);
if (resourceType is ShaderMethodAttribute && memberInfo is MethodInfo methodInfo)
{
CollectTopLevelMethod(methodInfo);
}
}
// Writing stage
foreach (ShaderTypeDefinition type in collectedTypes)
{
WriteStructure(type.Type, type.Instance);
}
foreach (MemberInfo memberInfo in memberInfos)
{
Type? memberType = memberInfo.GetMemberType(shader);
ShaderResourceAttribute? resourceType = memberInfo.GetResourceAttribute(memberType);
if (resourceType is ShaderMethodAttribute && memberInfo is MethodInfo methodInfo)
{
WriteTopLevelMethod(methodInfo);
}
else if (memberType != null && resourceType != null)
{
WriteResource(memberInfo, memberType, resourceType);
}
}
stringWriter.GetStringBuilder().TrimEnd();
writer.WriteLine();
result = new ShaderGenerationResult(stringWriter.ToString());
GetEntryPoints(result, shaderType, bindingAttr);
return result;
}
public static ShaderGenerationResult GetEntryPoints(ShaderGenerationResult result, Type shaderType, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
{
foreach (MethodInfo shaderMethodInfo in shaderType.GetMethods(bindingAttr).Where(m => m.IsDefined(typeof(ShaderAttribute))))
{
ShaderAttribute shaderAttribute = shaderMethodInfo.GetCustomAttribute<ShaderAttribute>();
result.SetShader(shaderAttribute.Name, shaderMethodInfo.Name);
}
return result;
}
private void CollectStructure(Type type, object? obj)
{
type = type.GetElementOrDeclaredType();
if (HlslKnownTypes.ContainsKey(type) || collectedTypes.Any(d => d.Type == type)) return;
ShaderTypeDefinition shaderTypeDefinition = new ShaderTypeDefinition(type, obj);
if (type.IsEnum)
{
collectedTypes.Add(shaderTypeDefinition);
return;
}
Type parentType = type.BaseType;
while (parentType != null)
{
CollectStructure(parentType, obj);
parentType = parentType.BaseType;
}
foreach (Type interfaceType in type.GetInterfaces())
{
CollectStructure(interfaceType, obj);
}
var memberInfos = type.GetMembersInOrder(bindingAttr);
if (!type.IsInterface)
{
memberInfos = memberInfos.Where(m => m.IsDefined(typeof(ShaderResourceAttribute)));
}
foreach (MemberInfo memberInfo in memberInfos)
{
Type? memberType = memberInfo.GetMemberType(obj);
ShaderResourceAttribute? resourceType = memberInfo.GetResourceAttribute(memberType);
if (memberInfo is MethodInfo methodInfo)
{
if (type.IsInterface || resourceType is ShaderMethodAttribute)
{
CollectMethod(methodInfo);
}
}
else if (memberType != null && memberType != type)
{
CollectStructure(memberType, memberInfo.GetMemberValue(obj));
if (resourceType != null)
{
shaderTypeDefinition.ResourceDefinitions.Add(new ResourceDefinition(memberType, resourceType));
}
}
}
collectedTypes.Add(shaderTypeDefinition);
}
private void WriteStructure(Type type, object? obj)
{
if (type.IsEnum)
{
writer.WriteLine($"enum class {type.Name}");
}
else if (type.IsInterface)
{
writer.WriteLine($"interface {type.Name}");
}
else
{
writer.Write($"struct {type.Name}");
if (type.BaseType != null && type.BaseType != typeof(object) && type.BaseType != typeof(ValueType))
{
writer.Write($" : {type.BaseType.Name}, ");
// NOTE: Types might no expose every interface method.
//foreach (Type interfaceType in type.GetInterfaces())
//{
// writer.Write(interfaceType.Name + ", ");
//}
stringWriter.GetStringBuilder().Length -= 2;
}
writer.WriteLine();
}
writer.WriteLine("{");
writer.Indent++;
var fieldAndPropertyInfos = type.GetMembersInOrder(bindingAttr | BindingFlags.DeclaredOnly).Where(m => m is FieldInfo || m is PropertyInfo);
var methodInfos = type.GetMembersInTypeHierarchyInOrder(bindingAttr).Where(m => m is MethodInfo);
var memberInfos = fieldAndPropertyInfos.Concat(methodInfos);
if (!type.IsInterface && !type.IsEnum)
{
memberInfos = memberInfos.Where(m => m.IsDefined(typeof(ShaderResourceAttribute)));
}
foreach (MemberInfo memberInfo in memberInfos)
{
Type? memberType = memberInfo.GetMemberType(obj);
ShaderResourceAttribute? resourceType = memberInfo.GetResourceAttribute(memberType);
if (memberInfo is MethodInfo methodInfo)
{
if (type.IsInterface || resourceType is ShaderMethodAttribute)
{
WriteMethod(methodInfo);
}
}
else if (memberType != null && resourceType != null)
{
if (type.IsEnum)
{
writer.Write(memberInfo.Name);
writer.WriteLine(",");
}
else
{
WriteStructureField(memberInfo, memberType);
}
}
}
stringWriter.GetStringBuilder().TrimEnd();
writer.Indent--;
writer.WriteLine();
writer.WriteLine("};");
writer.WriteLine();
if (type.IsEnum) return;
foreach (MemberInfo memberInfo in memberInfos.Where(m => m.IsStatic()))
{
Type? memberType = memberInfo.GetMemberType(obj);
if (memberType != null)
{
WriteStaticStructureField(memberInfo, memberType);
}
}
}
private void WriteStructureField(MemberInfo memberInfo, Type memberType)
{
if (memberInfo.IsStatic())
{
writer.Write("static");
writer.Write(" ");
}
writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}");
int arrayCount = memberType.IsArray ? 2 : 0;
writer.Write(GetArrayString(arrayCount));
writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute<ShaderSemanticAttribute>()));
writer.WriteLine(";");
writer.WriteLine();
}
private void WriteStaticStructureField(MemberInfo memberInfo, Type memberType)
{
string declaringType = HlslKnownTypes.GetMappedName(memberInfo.DeclaringType);
writer.WriteLine($"static {HlslKnownTypes.GetMappedName(memberType)} {declaringType}::{memberInfo.Name};");
writer.WriteLine();
}
private void WriteResource(MemberInfo memberInfo, Type memberType, ShaderResourceAttribute resourceType)
{
switch (resourceType)
{
case ConstantBufferResourceAttribute _:
WriteConstantBuffer(memberInfo, memberType, bindingTracker.ConstantBuffer++);
break;
case SamplerResourceAttribute _:
WriteSampler(memberInfo, memberType, bindingTracker.Sampler++);
break;
case TextureResourceAttribute _:
WriteTexture(memberInfo, memberType, bindingTracker.Texture++);
break;
case UnorderedAccessViewResourceAttribute _:
WriteUnorderedAccessView(memberInfo, memberType, bindingTracker.UnorderedAccessView++);
break;
case StaticResourceAttribute _:
WriteStaticResource(memberInfo, memberType);
break;
default:
throw new NotSupportedException("This shader resource type is not supported.");
}
}
private void WriteConstantBuffer(MemberInfo memberInfo, Type memberType, int binding)
{
int arrayCount = memberType.IsArray ? 2 : 0;
writer.Write($"cbuffer {memberInfo.Name}Buffer");
writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute<ShaderSemanticAttribute>()));
writer.WriteLine($" : register(b{binding})");
writer.WriteLine("{");
writer.Indent++;
writer.WriteLine($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}{GetArrayString(arrayCount)};");
writer.Indent--;
writer.WriteLine("}");
writer.WriteLine();
}
private void WriteSampler(MemberInfo memberInfo, Type memberType, int binding)
{
writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}");
writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute<ShaderSemanticAttribute>()));
writer.Write($" : register(s{binding})");
writer.WriteLine(";");
writer.WriteLine();
}
private void WriteTexture(MemberInfo memberInfo, Type memberType, int binding)
{
writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}");
writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute<ShaderSemanticAttribute>()));
writer.Write($" : register(t{binding})");
writer.WriteLine(";");
writer.WriteLine();
}
private void WriteUnorderedAccessView(MemberInfo memberInfo, Type memberType, int binding)
{
writer.Write($"{HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}");
writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute<ShaderSemanticAttribute>()));
writer.Write($" : register(u{binding})");
writer.WriteLine(";");
writer.WriteLine();
}
private void WriteStaticResource(MemberInfo memberInfo, Type memberType)
{
List<MemberInfo> generatedMemberInfos = new List<MemberInfo>();
foreach (ResourceDefinition resourceDefinition in collectedTypes.First(d => d.Type == memberType).ResourceDefinitions)
{
MemberInfo generatedMemberInfo = new FakeMemberInfo($"__Generated__{bindingTracker.StaticResource++}__");
generatedMemberInfos.Add(generatedMemberInfo);
WriteResource(generatedMemberInfo, resourceDefinition.MemberType, resourceDefinition.ResourceType);
}
writer.Write($"static {HlslKnownTypes.GetMappedName(memberType)} {memberInfo.Name}");
writer.Write(GetHlslSemantic(memberInfo.GetCustomAttribute<ShaderSemanticAttribute>()));
if (generatedMemberInfos.Count != 0)
{
writer.Write(" = { ");
foreach (MemberInfo generatedMemberInfo in generatedMemberInfos)
{
writer.Write(generatedMemberInfo.Name);
writer.Write(", ");
}
writer.Write("}");
}
writer.WriteLine(";");
writer.WriteLine();
}
private static string GetArrayString(int arrayCount) => arrayCount > 0 ? $"[{arrayCount}]" : "";
private static string GetHlslSemantic(ShaderSemanticAttribute? semanticAttribute)
{
if (semanticAttribute is null) return "";
Type semanticType = semanticAttribute.GetType();
if (HlslKnownSemantics.ContainsKey(semanticType))
{
string semanticName = HlslKnownSemantics.GetMappedName(semanticType);
return semanticAttribute is ShaderSemanticWithIndexAttribute semanticAttributeWithIndex
? " : " + semanticName + semanticAttributeWithIndex.Index
: " : " + semanticName;
}
throw new NotSupportedException();
}
private void CollectTopLevelMethod(MethodInfo methodInfo)
{
IList<MethodInfo> methodInfos = methodInfo.GetBaseMethods();
for (int depth = methodInfos.Count - 1; depth >= 0; depth--)
{
MethodInfo currentMethodInfo = methodInfos[depth];
CollectMethod(currentMethodInfo);
}
}
private void CollectMethod(MethodInfo methodInfo)
{
GetSyntaxTree(methodInfo, out SyntaxNode root, out SemanticModel semanticModel);
ShaderSyntaxCollector syntaxCollector = new ShaderSyntaxCollector(this, semanticModel);
syntaxCollector.Visit(root);
}
private void WriteTopLevelMethod(MethodInfo methodInfo)
{
IList<MethodInfo> methodInfos = methodInfo.GetBaseMethods();
for (int depth = methodInfos.Count - 1; depth >= 0; depth--)
{
MethodInfo currentMethodInfo = methodInfos[depth];
WriteMethod(currentMethodInfo, depth);
}
}
private void WriteMethod(MethodInfo methodInfo, int depth = 0)
{
GetSyntaxTree(methodInfo, out SyntaxNode root, out SemanticModel semanticModel);
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(this, semanticModel, true, depth);
root = syntaxRewriter.Visit(root);
string shaderSource = root.ToFullString();
// TODO: See why the System namespace in System.Math is not present in UWP projects.
shaderSource = shaderSource.Replace("Math.Max", "max");
shaderSource = shaderSource.Replace("Math.Pow", "pow");
shaderSource = shaderSource.Replace("Math.Sin", "sin");
shaderSource = shaderSource.Replace("vector", "vec");
shaderSource = Regex.Replace(shaderSource, @"\d+[fF]", m => m.Value.Replace("f", ""));
// Indent every line
string indent = "";
for (int i = 0; i < writer.Indent; i++)
{
indent += IndentedTextWriter.DefaultTabString;
}
shaderSource = shaderSource.Replace(Environment.NewLine, Environment.NewLine + indent).TrimEnd(' ');
writer.WriteLine(shaderSource);
}
private static void GetSyntaxTree(MethodInfo methodInfo, out SyntaxNode root, out SemanticModel semanticModel)
{
lock (compilationLock)
{
GetMethodHandle(methodInfo, out string assemblyPath, out EntityHandle methodHandle);
if (!decompilers.TryGetValue(assemblyPath, out CSharpDecompiler decompiler))
{
decompiler = CreateDecompiler(assemblyPath);
decompilers.Add(assemblyPath, decompiler);
}
string sourceCode = decompiler.DecompileAsString(methodHandle);
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);
root = syntaxTree.GetRoot();
compilation = compilation.AddSyntaxTrees(syntaxTree);
semanticModel = compilation.GetSemanticModel(syntaxTree);
}
}
private static void GetMethodHandle(MethodInfo methodInfo, out string assemblyPath, out EntityHandle methodHandle)
{
assemblyPath = methodInfo.DeclaringType.Assembly.Location;
if (!string.IsNullOrEmpty(assemblyPath))
{
methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException();
}
else
{
if (!typeDefinitions.TryGetValue(methodInfo.DeclaringType, out var tuple))
{
foreach (PEFile peFile in peFiles)
{
TypeDefinitionHandle typeDefinitionHandle = peFile.Metadata.TypeDefinitions.FirstOrDefault(t => t.GetFullTypeName(peFile.Metadata).ToString() == methodInfo.DeclaringType.FullName);
if (!typeDefinitionHandle.IsNil)
{
tuple = (peFile, peFile.Metadata.GetTypeDefinition(typeDefinitionHandle));
typeDefinitions.Add(methodInfo.DeclaringType, tuple);
break;
}
}
if (tuple.PEFile is null) throw new InvalidOperationException();
}
PEFile peFileForMethod = tuple.PEFile;
TypeDefinition typeDefinition = tuple.TypeDefinition;
assemblyPath = peFileForMethod.FileName;
methodHandle = typeDefinition.GetMethods()
.Where(m => peFileForMethod.Metadata.StringComparer.Equals(peFileForMethod.Metadata.GetMethodDefinition(m).Name, methodInfo.Name))
.First(m => peFileForMethod.Metadata.GetMethodDefinition(m).GetParameters().Count == methodInfo.GetParameters().Length);
}
}
private static CSharpDecompiler CreateDecompiler(string assemblyPath)
{
UniversalAssemblyResolver resolver = new UniversalAssemblyResolver(assemblyPath, false, "netstandard");
DecompilerSettings decompilerSettings = new DecompilerSettings(ICSharpCode.Decompiler.CSharp.LanguageVersion.Latest)
{
ObjectOrCollectionInitializers = false,
UsingDeclarations = false
};
decompilerSettings.CSharpFormattingOptions.IndentationString = IndentedTextWriter.DefaultTabString;
return new CSharpDecompiler(assemblyPath, resolver, decompilerSettings);
}
internal static class HlslKnownAttributes
{
private static readonly HashSet<string> allowedAttributes = new HashSet<string>()
{
typeof(NumThreadsAttribute).FullName,
typeof(ShaderAttribute).FullName
};
public static bool Contains(string name)
{
return HlslKnownSemantics.ContainsKey(name) || allowedAttributes.Contains(name);
}
}
internal static class HlslKnownSemantics
{
private static readonly Dictionary<string, string> knownSemantics = new Dictionary<string, string>()
{
{ typeof(PositionSemanticAttribute).FullName, "Position" },
{ typeof(NormalSemanticAttribute).FullName, "Normal" },
{ typeof(TextureCoordinateSemanticAttribute).FullName, "TexCoord" },
{ typeof(ColorSemanticAttribute).FullName, "Color" },
{ typeof(TangentSemanticAttribute).FullName, "Tangent" },
{ typeof(SystemTargetSemanticAttribute).FullName, "SV_Target" },
{ typeof(SystemDispatchThreadIdSemanticAttribute).FullName, "SV_DispatchThreadId" },
{ typeof(SystemIsFrontFaceSemanticAttribute).FullName, "SV_IsFrontFace" },
{ typeof(SystemInstanceIdSemanticAttribute).FullName, "SV_InstanceId" },
{ typeof(SystemPositionSemanticAttribute).FullName, "SV_Position" },
{ typeof(SystemRenderTargetArrayIndexSemanticAttribute).FullName, "SV_RenderTargetArrayIndex" }
};
public static bool ContainsKey(Type type)
{
return knownSemantics.ContainsKey(type.GetElementOrDeclaredType().FullName);
}
public static bool ContainsKey(string name)
{
return knownSemantics.ContainsKey(name);
}
public static string GetMappedName(Type type)
{
return knownSemantics[type.GetElementOrDeclaredType().FullName];
}
public static string GetMappedName(string name)
{
return knownSemantics[name];
}
}
internal static class HlslKnownTypes
{
private static readonly Dictionary<string, string> knownTypes = new Dictionary<string, string>()
{
{ typeof(void).FullName, "void" },
{ typeof(bool).FullName, "bool" },
{ typeof(uint).FullName, "uint" },
{ typeof(int).FullName, "int" },
{ typeof(double).FullName, "double" },
{ typeof(float).FullName, "float" },
{ typeof(Vector2).FullName, "float2" },
{ typeof(Vector3).FullName, "float3" },
{ typeof(Vector4).FullName, "float4" },
{ typeof(Numerics.Vector4).FullName, "float4" },
{ typeof(Numerics.UInt2).FullName, "uint2" },
{ typeof(Numerics.UInt3).FullName, "uint3" },
{ typeof(Matrix4x4).FullName, "float4x4" },
{ typeof(SamplerResource).FullName, "SamplerState" },
{ typeof(SamplerComparisonResource).FullName, "SamplerComparisonState" },
{ typeof(Texture2DResource).FullName, "Texture2D" },
{ typeof(Texture2DArrayResource).FullName, "Texture2DArray" },
{ typeof(TextureCubeResource).FullName, "TextureCube" },
{ typeof(RWBufferResource<>).FullName, "RWBuffer" },
{ typeof(RWTexture2DResource<>).FullName, "RWTexture2D" },
};
public static bool ContainsKey(Type type)
{
type = type.GetElementOrDeclaredType();
string typeFullName = type.Namespace + Type.Delimiter + type.Name;
return knownTypes.ContainsKey(typeFullName);
}
public static bool ContainsKey(string name)
{
int indexOfOpenBracket = name.IndexOf('<');
name = indexOfOpenBracket >= 0 ? name.Remove(indexOfOpenBracket) + "`1" : name;
return knownTypes.ContainsKey(name);
}
public static string GetMappedName(Type type)
{
type = type.GetElementOrDeclaredType();
string typeFullName = type.Namespace + Type.Delimiter + type.Name;
string mappedName = knownTypes.TryGetValue(typeFullName, out string mapped) ? mapped : type.Name;
return type.IsGenericType ? mappedName + $"<{GetMappedName(type.GetGenericArguments()[0])}>" : mappedName;
}
public static string GetMappedName(string name)
{
int indexOfOpenBracket = name.IndexOf('<');
string genericArguments = indexOfOpenBracket >= 0 ? name.Substring(indexOfOpenBracket) : "";
name = indexOfOpenBracket >= 0 ? name.Remove(indexOfOpenBracket) + "`1" : name;
string mappedName = knownTypes.TryGetValue(name, out string mapped) ? mapped : Regex.Match(name, @"[^\.]+$").Value;
return mappedName + genericArguments;
}
}
internal static class HlslKnownMethods
{
private static readonly Dictionary<string, string> knownMethods = new Dictionary<string, string>()
{
{ "System.Math.Cos", "cos" },
{ "System.MathF.Cos", "cos" },
{ "System.Math.Max", "max" },
{ "System.Math.Pow", "pow" },
{ "System.MathF.Pow", "pow" },
{ "System.Math.Sin", "sin" },
{ "System.MathF.Sin", "sin" },
{ "System.Math.PI", "3.1415926535897931" },
{ "System.MathF.PI", "3.14159274f" },
{ "DirectX12GameEngine.Shaders.Numerics.Vector2.Length", "length" },
{ "DirectX12GameEngine.Shaders.Numerics.UInt2.X", ".x" },
{ "DirectX12GameEngine.Shaders.Numerics.UInt2.Y", ".y" },
{ "DirectX12GameEngine.Shaders.Numerics.UInt3.X", ".x" },
{ "DirectX12GameEngine.Shaders.Numerics.UInt3.Y", ".y" },
{ "DirectX12GameEngine.Shaders.Numerics.UInt3.Z", ".z" },
{ "DirectX12GameEngine.Shaders.Numerics.UInt3.XY", ".xy" },
{ "System.Numerics.Vector3.X", ".x" },
{ "System.Numerics.Vector3.Y", ".y" },
{ "System.Numerics.Vector3.Z", ".z" },
{ "System.Numerics.Vector3.Cross", "cross" },
{ "System.Numerics.Vector3.Dot", "dot" },
{ "System.Numerics.Vector3.Lerp", "lerp" },
{ "System.Numerics.Vector3.Transform", "mul" },
{ "System.Numerics.Vector3.TransformNormal", "mul" },
{ "System.Numerics.Vector3.Normalize", "normalize" },
{ "System.Numerics.Vector3.Zero", "(float3)0" },
{ "System.Numerics.Vector3.One", "float3(1.0f, 1.0f, 1.0f)" },
{ "System.Numerics.Vector3.UnitX", "float3(1.0f, 0.0f, 0.0f)" },
{ "System.Numerics.Vector3.UnitY", "float3(0.0f, 1.0f, 0.0f)" },
{ "System.Numerics.Vector3.UnitZ", "float3(0.0f, 0.0f, 1.0f)" },
{ "System.Numerics.Vector4.X", ".x" },
{ "System.Numerics.Vector4.Y", ".y" },
{ "System.Numerics.Vector4.Z", ".z" },
{ "System.Numerics.Vector4.W", ".w" },
{ "System.Numerics.Vector4.Lerp", "lerp" },
{ "System.Numerics.Vector4.Transform", "mul" },
{ "System.Numerics.Vector4.Normalize", "normalize" },
{ "System.Numerics.Vector4.Zero", "(float4)0" },
{ "System.Numerics.Vector4.One", "float4(1.0f, 1.0f, 1.0f, 1.0f)" },
{ "System.Numerics.Matrix4x4.Multiply", "mul" },
{ "System.Numerics.Matrix4x4.Transpose", "transpose" },
{ "System.Numerics.Matrix4x4.Translation", "[3].xyz" },
{ "System.Numerics.Matrix4x4.M11", "[0][0]" },
{ "System.Numerics.Matrix4x4.M12", "[0][1]" },
{ "System.Numerics.Matrix4x4.M13", "[0][2]" },
{ "System.Numerics.Matrix4x4.M14", "[0][3]" },
{ "System.Numerics.Matrix4x4.M21", "[1][0]" },
{ "System.Numerics.Matrix4x4.M22", "[1][1]" },
{ "System.Numerics.Matrix4x4.M23", "[1][2]" },
{ "System.Numerics.Matrix4x4.M24", "[1][3]" },
{ "System.Numerics.Matrix4x4.M31", "[2][0]" },
{ "System.Numerics.Matrix4x4.M32", "[2][1]" },
{ "System.Numerics.Matrix4x4.M33", "[2][2]" },
{ "System.Numerics.Matrix4x4.M34", "[2][3]" },
{ "System.Numerics.Matrix4x4.M41", "[3][0]" },
{ "System.Numerics.Matrix4x4.M42", "[3][1]" },
{ "System.Numerics.Matrix4x4.M43", "[3][2]" },
{ "System.Numerics.Matrix4x4.M44", "[3][3]" }
};
public static bool Contains(ISymbol containingMemberSymbol, ISymbol memberSymbol)
{
string fullTypeName = containingMemberSymbol.IsStatic ? containingMemberSymbol.ToString() : memberSymbol.ContainingType.ToString();
if (knownMethods.ContainsKey(fullTypeName + Type.Delimiter + memberSymbol.Name))
{
return true;
}
return false;
}
public static string? GetMappedName(ISymbol containingMemberSymbol, ISymbol memberSymbol)
{
string fullTypeName = containingMemberSymbol.IsStatic ? containingMemberSymbol.ToString() : memberSymbol.ContainingType.ToString();
if (knownMethods.TryGetValue(fullTypeName + Type.Delimiter + memberSymbol.Name, out string mapped))
{
if (!memberSymbol.IsStatic)
{
return containingMemberSymbol.Name + mapped;
}
return mapped;
}
if (memberSymbol.IsStatic)
{
return containingMemberSymbol.Name + "::" + memberSymbol.Name;
}
return null;
}
}
private class HlslBindingTracker
{
public int ConstantBuffer { get; set; }
public int Sampler { get; set; }
public int Texture { get; set; }
public int UnorderedAccessView { get; set; }
public int StaticResource { get; set; }
}
private class ShaderTypeDefinition
{
public ShaderTypeDefinition(Type type, object? instance)
{
Type = type;
Instance = instance;
}
public object? Instance { get; }
public Type Type { get; }
public List<ResourceDefinition> ResourceDefinitions { get; } = new List<ResourceDefinition>();
}
private class ResourceDefinition
{
public ResourceDefinition(Type memberType, ShaderResourceAttribute resourceType)
{
MemberType = memberType;
ResourceType = resourceType;
}
public Type MemberType { get; }
public ShaderResourceAttribute ResourceType { get; }
}
private class FakeMemberInfo : MemberInfo
{
public FakeMemberInfo(string name)
{
Name = name;
}
public override Type DeclaringType => throw new NotImplementedException();
public override MemberTypes MemberType => MemberTypes.Field;
public override string Name { get; }
public override Type ReflectedType => throw new NotImplementedException();
public override object[] GetCustomAttributes(bool inherit)
{
return Array.Empty<object>();
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
return Array.Empty<object>();
}
public override bool IsDefined(Type attributeType, bool inherit)
{
return false;
}
}
}
}
+24
ファイルの表示
@@ -0,0 +1,24 @@
using System;
using System.Runtime.CompilerServices;
namespace DirectX12GameEngine.Shaders
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class ShaderMethodAttribute : ShaderResourceAttribute
{
public ShaderMethodAttribute([CallerLineNumber] int order = 0) : base(order)
{
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class ShaderAttribute : Attribute
{
public ShaderAttribute(string name)
{
Name = name;
}
public string Name { get; }
}
}
+53
ファイルの表示
@@ -0,0 +1,53 @@
using System;
using System.Numerics;
namespace DirectX12GameEngine.Shaders
{
public abstract class ShaderResource
{
}
[SamplerResource]
public class SamplerResource : ShaderResource
{
}
[SamplerResource]
public class SamplerComparisonResource : ShaderResource
{
}
[TextureResource]
public class Texture2DResource : ShaderResource
{
public Vector4 Sample(SamplerResource sampler, Vector2 texCoord) => throw new NotImplementedException();
}
[TextureResource]
public class Texture2DResource<T> : ShaderResource where T : unmanaged
{
public T Sample(SamplerResource sampler, Vector2 texCoord) => throw new NotImplementedException();
}
[TextureResource]
public class Texture2DArrayResource : ShaderResource
{
}
[TextureResource]
public class TextureCubeResource : ShaderResource
{
}
[UnorderedAccessViewResource]
public class RWBufferResource<T> : ShaderResource
{
public T this[uint index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
[UnorderedAccessViewResource]
public class RWTexture2DResource<T> : ShaderResource where T : unmanaged
{
public T this[Numerics.UInt2 index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
}
+70
ファイルの表示
@@ -0,0 +1,70 @@
using System;
using System.Runtime.CompilerServices;
namespace DirectX12GameEngine.Shaders
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class ShaderResourceAttribute : Attribute
{
public ShaderResourceAttribute([CallerLineNumber] int order = 0)
{
Order = order;
}
public int Order { get; }
public bool Override { get; set; }
}
public class ConstantBufferResourceAttribute : ShaderResourceAttribute
{
public ConstantBufferResourceAttribute([CallerLineNumber] int order = 0) : base(order)
{
}
}
public class SamplerResourceAttribute : ShaderResourceAttribute
{
public SamplerResourceAttribute([CallerLineNumber] int order = 0) : base(order)
{
}
}
public class TextureResourceAttribute : ShaderResourceAttribute
{
public TextureResourceAttribute([CallerLineNumber] int order = 0) : base(order)
{
}
}
public class UnorderedAccessViewResourceAttribute : ShaderResourceAttribute
{
public UnorderedAccessViewResourceAttribute([CallerLineNumber] int order = 0) : base(order)
{
}
}
public class StaticResourceAttribute : ShaderResourceAttribute
{
public StaticResourceAttribute([CallerLineNumber] int order = 0) : base(order)
{
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class NumThreadsAttribute : Attribute
{
public NumThreadsAttribute(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
public int X { get; }
public int Y { get; }
public int Z { get; }
}
}
+81
ファイルの表示
@@ -0,0 +1,81 @@
using System;
namespace DirectX12GameEngine.Shaders
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)]
public abstract class ShaderSemanticAttribute : Attribute
{
}
public abstract class ShaderSemanticWithIndexAttribute : ShaderSemanticAttribute
{
public ShaderSemanticWithIndexAttribute(int index = 0)
{
Index = index;
}
public int Index { get; }
}
public class PositionSemanticAttribute : ShaderSemanticWithIndexAttribute
{
public PositionSemanticAttribute(int index = 0) : base(index)
{
}
}
public class NormalSemanticAttribute : ShaderSemanticWithIndexAttribute
{
public NormalSemanticAttribute(int index = 0) : base(index)
{
}
}
public class TextureCoordinateSemanticAttribute : ShaderSemanticWithIndexAttribute
{
public TextureCoordinateSemanticAttribute(int index = 0) : base(index)
{
}
}
public class ColorSemanticAttribute : ShaderSemanticWithIndexAttribute
{
public ColorSemanticAttribute(int index = 0) : base(index)
{
}
}
public class TangentSemanticAttribute : ShaderSemanticWithIndexAttribute
{
public TangentSemanticAttribute(int index = 0) : base(index)
{
}
}
public class SystemTargetSemanticAttribute : ShaderSemanticWithIndexAttribute
{
public SystemTargetSemanticAttribute(int index = 0) : base(index)
{
}
}
public class SystemDispatchThreadIdSemanticAttribute : ShaderSemanticAttribute
{
}
public class SystemInstanceIdSemanticAttribute : ShaderSemanticAttribute
{
}
public class SystemIsFrontFaceSemanticAttribute : ShaderSemanticAttribute
{
}
public class SystemPositionSemanticAttribute : ShaderSemanticAttribute
{
}
public class SystemRenderTargetArrayIndexSemanticAttribute : ShaderSemanticAttribute
{
}
}
+61
ファイルの表示
@@ -0,0 +1,61 @@
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace DirectX12GameEngine.Shaders
{
public class ShaderSyntaxCollector : CSharpSyntaxWalker
{
private readonly SemanticModel semanticModel;
private readonly ShaderGenerator shaderGenerator;
public ShaderSyntaxCollector(ShaderGenerator shaderGenerator, SemanticModel semanticModel)
{
this.shaderGenerator = shaderGenerator;
this.semanticModel = semanticModel;
}
public override void VisitParameter(ParameterSyntax node)
{
base.VisitParameter(node);
TypeInfo typeInfo = semanticModel.GetTypeInfo(node.Type);
string fullTypeName = typeInfo.Type.ToDisplayString(
new SymbolDisplayFormat(typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces))
+ ", " + typeInfo.Type.ContainingAssembly.Identity.ToString();
shaderGenerator.AddType(Type.GetType(fullTypeName));
}
public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
base.VisitMemberAccessExpression(node);
SymbolInfo containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression);
SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name);
ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault();
if (memberSymbol is null || (memberSymbol.Kind != SymbolKind.Field && memberSymbol.Kind != SymbolKind.Property && memberSymbol.Kind != SymbolKind.Method))
{
return;
}
ISymbol containingMemberSymbol = containingMemberSymbolInfo.Symbol;
if (!ShaderGenerator.HlslKnownMethods.Contains(containingMemberSymbol, memberSymbol))
{
ISymbol symbol = containingMemberSymbol.IsStatic ? containingMemberSymbol : memberSymbol.ContainingType;
string fullTypeName = symbol.ToDisplayString(
new SymbolDisplayFormat(typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces))
+ ", " + symbol.ContainingAssembly.Identity.ToString();
shaderGenerator.AddType(Type.GetType(fullTypeName));
}
}
}
}
+144
ファイルの表示
@@ -0,0 +1,144 @@
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace DirectX12GameEngine.Shaders
{
public class ShaderSyntaxRewriter : CSharpSyntaxRewriter
{
private readonly int depth;
private readonly bool isTopLevel;
private readonly SemanticModel semanticModel;
private readonly ShaderGenerator shaderGenerator;
public ShaderSyntaxRewriter(ShaderGenerator shaderGenerator, SemanticModel semanticModel, bool isTopLevel = false, int depth = 0)
{
this.shaderGenerator = shaderGenerator;
this.semanticModel = semanticModel;
this.isTopLevel = isTopLevel;
this.depth = depth;
}
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
{
node = (MethodDeclarationSyntax)base.VisitMethodDeclaration(node);
if (isTopLevel && depth > 0)
{
node = node.ReplaceToken(node.Identifier, SyntaxFactory.Identifier($"Base_{depth}_{node.Identifier.ValueText}"));
}
SyntaxTokenList modifiers = new SyntaxTokenList();
if (node.Modifiers.Any(SyntaxKind.StaticKeyword))
{
modifiers = modifiers.Add(SyntaxFactory.Token(default, SyntaxKind.StaticKeyword, SyntaxFactory.TriviaList(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "))));
}
return node.ReplaceType(node.ReturnType).WithModifiers(modifiers);
}
public override SyntaxNode VisitParameter(ParameterSyntax node)
{
string? attributeName = node.AttributeLists.FirstOrDefault()?.Attributes.FirstOrDefault()?.Name.ToString();
node = (ParameterSyntax)base.VisitParameter(node);
node = node.WithAttributeLists(default);
node = node.ReplaceType(node.Type);
if (attributeName != null)
{
node = node.ReplaceToken(node.Identifier, SyntaxFactory.Identifier($"{node.Identifier.ValueText} : {ShaderGenerator.HlslKnownSemantics.GetMappedName(attributeName + "Attribute")}"));
}
return node;
}
public override SyntaxNode VisitAttribute(AttributeSyntax node)
{
node = (AttributeSyntax)base.VisitAttribute(node);
return node.ReplaceType(node.Name);
}
public override SyntaxNode VisitAttributeList(AttributeListSyntax node)
{
var knownAttributes = node.Attributes.Where(n => ShaderGenerator.HlslKnownAttributes.Contains(n.Name + "Attribute"));
if (knownAttributes.Count() == 0) return null;
var list = new SeparatedSyntaxList<AttributeSyntax>();
list = list.AddRange(knownAttributes);
node = node.WithAttributes(list);
return base.VisitAttributeList(node);
}
public override SyntaxNode VisitCastExpression(CastExpressionSyntax node)
{
node = (CastExpressionSyntax)base.VisitCastExpression(node);
return node.ReplaceType(node.Type);
}
public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
{
node = (LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node);
return node.ReplaceType(node.Declaration.Type);
}
public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
node = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node);
node = node.ReplaceType(node.Type);
if (node.ArgumentList.Arguments.Count == 0)
{
return SyntaxFactory.CastExpression(node.Type, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0)));
}
else
{
return SyntaxFactory.InvocationExpression(node.Type, node.ArgumentList);
}
}
public override SyntaxNode VisitDefaultExpression(DefaultExpressionSyntax node)
{
node = (DefaultExpressionSyntax)base.VisitDefaultExpression(node);
node = node.ReplaceType(node.Type);
return SyntaxFactory.CastExpression(node.Type, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0)));
}
public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
node = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node);
if (node.Expression is BaseExpressionSyntax)
{
if (isTopLevel)
{
return SyntaxFactory.IdentifierName($"Base_{depth + 1}_{node.Name}");
}
else
{
SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name);
ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault();
if (memberSymbol != null)
{
return SyntaxFactory.IdentifierName($"{memberSymbol.ContainingType.Name}::{node.Name}");
}
else
{
throw new InvalidOperationException();
}
}
}
else
{
return node.ReplaceMember(semanticModel);
}
}
}
}
+29
ファイルの表示
@@ -0,0 +1,29 @@
using System.Text;
namespace DirectX12GameEngine.Shaders
{
public static class StringBuilderExtensions
{
public static StringBuilder TrimEnd(this StringBuilder sb)
{
if (sb.Length == 0) return sb;
int i;
for (i = sb.Length - 1; i >= 0; i--)
{
if (!char.IsWhiteSpace(sb[i]))
{
break;
}
}
if (i < sb.Length - 1)
{
sb.Length = i + 1;
}
return sb;
}
}
}
+48
ファイルの表示
@@ -0,0 +1,48 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace DirectX12GameEngine.Shaders
{
public static class SyntaxNodeExtensions
{
public static TRoot ReplaceType<TRoot>(this TRoot node, TypeSyntax type) where TRoot : SyntaxNode
{
string value = ShaderGenerator.HlslKnownTypes.GetMappedName(type.ToString());
if (value == type.ToString())
{
return node;
}
else
{
return node.ReplaceNode(type, SyntaxFactory.ParseTypeName(value).WithLeadingTrivia(type.GetLeadingTrivia()).WithTrailingTrivia(type.GetTrailingTrivia()));
}
}
public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel)
{
SymbolInfo containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression);
SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name);
ISymbol? memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault();
if (memberSymbol is null || (memberSymbol.Kind != SymbolKind.Field && memberSymbol.Kind != SymbolKind.Property && memberSymbol.Kind != SymbolKind.Method))
{
return node;
}
string? value = ShaderGenerator.HlslKnownMethods.GetMappedName(containingMemberSymbolInfo.Symbol, memberSymbol);
if (value is null)
{
return node;
}
else
{
return SyntaxFactory.IdentifierName(value).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
}
}
}
}