Removed Roslyn dependency from main library
このコミットが含まれているのは:
1 親
59d69e35af
コミット
9224485334
9ファイル変更
+9
-623
検索条件に一致するファイルはありません
@@ -6,10 +6,6 @@
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ComputeSharp.Graphics\ComputeSharp.Graphics.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,271 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ComputeSharp;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
|
||||
namespace Microsoft.CodeAnalysis.CSharp.Syntax
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with some extension methods for C# syntax nodes
|
||||
/// </summary>
|
||||
internal static class SyntaxNodeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks a <see cref="SyntaxNode"/> value and replaces the value type to be HLSL compatible, if needed
|
||||
/// </summary>
|
||||
/// <typeparam name="TRoot">The type of the input <see cref="SyntaxNode"/> instance</typeparam>
|
||||
/// <param name="node">The input <see cref="SyntaxNode"/> to check and modify if needed</param>
|
||||
/// <param name="type">The <see cref="TypeSyntax"/> to use for the input node</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL</returns>
|
||||
[Pure]
|
||||
public static TRoot ReplaceType<TRoot>(this TRoot node, TypeSyntax type) where TRoot : SyntaxNode
|
||||
{
|
||||
string value = HlslKnownTypes.GetMappedName(type.ToString());
|
||||
|
||||
// If the HLSL mapped full type name equals the original type, just return the input node
|
||||
if (value == type.ToString()) return node;
|
||||
|
||||
// Process and return the type name
|
||||
TypeSyntax newType = SyntaxFactory.ParseTypeName(value).WithLeadingTrivia(type.GetLeadingTrivia()).WithTrailingTrivia(type.GetTrailingTrivia());
|
||||
return node.ReplaceNode(type, newType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks a <see cref="MemberAccessExpressionSyntax"/> instance and replaces it to be HLSL compatible, if needed
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="MemberAccessExpressionSyntax"/> to check and modify if needed</param>
|
||||
/// <param name="semanticModel">The <see cref="SemanticModel"/> to use to load symbols for the input node</param>
|
||||
/// <param name="variable">The info on parsed static members, if any</param>
|
||||
/// <param name="method">The info on parsed static methods, if any</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
|
||||
[Pure]
|
||||
public static SyntaxNode ReplaceMember(this MemberAccessExpressionSyntax node, SemanticModel semanticModel, out (string Name, ReadableMember MemberInfo)? variable, out (string Name, MethodInfo MethodInfo)? method)
|
||||
{
|
||||
// Set the out parameters to null, replace them later on if needed
|
||||
variable = null;
|
||||
method = null;
|
||||
|
||||
SymbolInfo containingMemberSymbolInfo;
|
||||
ISymbol? memberSymbol;
|
||||
try
|
||||
{
|
||||
containingMemberSymbolInfo = semanticModel.GetSymbolInfo(node.Expression);
|
||||
SymbolInfo memberSymbolInfo = semanticModel.GetSymbolInfo(node.Name);
|
||||
memberSymbol = memberSymbolInfo.Symbol ?? memberSymbolInfo.CandidateSymbols.FirstOrDefault();
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
// Member access on a captured HLSL-compatible field or property
|
||||
string name = node.Name.ToFullString();
|
||||
if (name == "X" || name == "Y" || name == "Z" || name == "W") return node.WithName(SyntaxFactory.IdentifierName(name.ToLowerInvariant()));
|
||||
return node;
|
||||
}
|
||||
|
||||
// If the input member has no symbol, try to load it manually
|
||||
if (memberSymbol is null)
|
||||
{
|
||||
string expression = node.WithoutTrivia().ToFullString();
|
||||
int index = expression.LastIndexOf(Type.Delimiter);
|
||||
string fullname = expression.Substring(0, index);
|
||||
if (!(AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType(fullname)).FirstOrDefault(t => t != null) is Type type))
|
||||
{
|
||||
// The current node can't possibly represent a field or a property, so just return it
|
||||
return node;
|
||||
}
|
||||
|
||||
// Try to get the target static field or property, if present
|
||||
string name = expression.Substring(index + 1, expression.Length - fullname.Length - 1);
|
||||
MemberInfo[] memberInfos = type.GetMember(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
if (memberInfos.Length == 0) return node;
|
||||
bool isReadonly;
|
||||
ReadableMember memberInfo;
|
||||
switch (memberInfos.First())
|
||||
{
|
||||
case FieldInfo fieldInfo:
|
||||
isReadonly = fieldInfo.IsInitOnly;
|
||||
memberInfo = fieldInfo;
|
||||
break;
|
||||
case PropertyInfo propertyInfo:
|
||||
isReadonly = !propertyInfo.CanWrite;
|
||||
memberInfo = propertyInfo;
|
||||
break;
|
||||
case MethodInfo methodInfo:
|
||||
string methodName = $"{methodInfo.DeclaringType!.Name}_{methodInfo.Name}";
|
||||
method = (methodName, methodInfo);
|
||||
return SyntaxFactory.IdentifierName(methodName).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!;
|
||||
}
|
||||
|
||||
// Handle the loaded info
|
||||
return ProcessStaticMember(node, memberInfo, isReadonly, ref variable);
|
||||
}
|
||||
|
||||
// If the input member is not a field, property or method, just return it
|
||||
if (memberSymbol.Kind != SymbolKind.Field &&
|
||||
memberSymbol.Kind != SymbolKind.Property &&
|
||||
memberSymbol.Kind != SymbolKind.Method)
|
||||
{
|
||||
return node;
|
||||
}
|
||||
|
||||
// Handle static members as a special case
|
||||
if (memberSymbol.IsStatic && (
|
||||
memberSymbol.Kind == SymbolKind.Field ||
|
||||
memberSymbol.Kind == SymbolKind.Property ||
|
||||
memberSymbol.Kind == SymbolKind.Method))
|
||||
{
|
||||
// Get the containing type
|
||||
string
|
||||
typeFullname = memberSymbol.ContainingType.ToString()!,
|
||||
assemblyFullname = memberSymbol.ContainingAssembly.ToString()!;
|
||||
Type memberDeclaringType = Type.GetType($"{typeFullname}, {assemblyFullname}")!;
|
||||
|
||||
// Static field or property
|
||||
if (memberSymbol.Kind == SymbolKind.Field || memberSymbol.Kind == SymbolKind.Property)
|
||||
{
|
||||
bool isReadonly;
|
||||
ReadableMember memberInfo;
|
||||
switch (memberSymbol.Kind)
|
||||
{
|
||||
case SymbolKind.Field:
|
||||
FieldInfo fieldInfo = memberDeclaringType.GetField(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!;
|
||||
isReadonly = fieldInfo.IsInitOnly;
|
||||
memberInfo = fieldInfo;
|
||||
break;
|
||||
case SymbolKind.Property:
|
||||
PropertyInfo propertyInfo = memberDeclaringType.GetProperty(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!;
|
||||
isReadonly = !propertyInfo.CanWrite;
|
||||
memberInfo = propertyInfo;
|
||||
break;
|
||||
default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!;
|
||||
}
|
||||
|
||||
// Handle the loaded info
|
||||
return ProcessStaticMember(node, memberInfo, isReadonly, ref variable);
|
||||
}
|
||||
|
||||
// Static method
|
||||
MethodInfo methodInfo = memberDeclaringType.GetMethod(memberSymbol.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!;
|
||||
string name = $"{methodInfo.DeclaringType!.Name}_{methodInfo.Name}";
|
||||
method = (name, methodInfo);
|
||||
|
||||
return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks a <see cref="InvocationExpressionSyntax"/> instance and replaces it to be HLSL compatible, if needed
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="InvocationExpressionSyntax"/> to check and modify if needed</param>
|
||||
/// <param name="declaringType">The <see cref="Type"/> in which the input syntax tree is declared in</param>
|
||||
/// <param name="variable">The info on parsed static members, if any</param>
|
||||
/// <param name="method">The info on parsed static methods, if any</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
|
||||
[Pure]
|
||||
public static InvocationExpressionSyntax ReplaceInvocation(this InvocationExpressionSyntax node, Type declaringType, out (string Name, ReadableMember MemberInfo)? variable, out (string Name, MethodInfo MethodInfo)? method)
|
||||
{
|
||||
// Set the variable and method to null
|
||||
variable = null;
|
||||
method = null;
|
||||
|
||||
// Explore the nested classes from bottom to top
|
||||
if (declaringType.TryFindAncestorMember(node.Expression.ToString(), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, out var result))
|
||||
{
|
||||
// If a static method or delegate is found, track it
|
||||
string name = $"{result.Value.DeclaringType.Name}_{result.Value.MemberInfo.Name}";
|
||||
switch (result.Value.MemberInfo)
|
||||
{
|
||||
case FieldInfo fieldInfo:
|
||||
variable = (name, fieldInfo);
|
||||
break;
|
||||
case PropertyInfo propertyInfo:
|
||||
variable = (name, propertyInfo);
|
||||
break;
|
||||
case MethodInfo methodInfo:
|
||||
method = (name, methodInfo);
|
||||
return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!;
|
||||
}
|
||||
|
||||
// Return the mapped expression
|
||||
return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks a <see cref="IdentifierNameSyntax"/> instance and replaces it to be HLSL compatible, if needed
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="IdentifierNameSyntax"/> to check and modify if needed</param>
|
||||
/// <param name="declaringType">The <see cref="Type"/> in which the input syntax tree is declared in</param>
|
||||
/// <param name="variable">The info on parsed static members, if any</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
|
||||
[Pure]
|
||||
public static IdentifierNameSyntax ReplaceIdentifierName(this IdentifierNameSyntax node, Type declaringType, out (string Name, ReadableMember MemberInfo)? variable)
|
||||
{
|
||||
// Set the variable to null
|
||||
variable = null;
|
||||
|
||||
// Explore the nested classes from bottom to top
|
||||
if (declaringType.TryFindAncestorMember(node.Identifier.ToString(), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, out var result))
|
||||
{
|
||||
// If a static scalar or vector member is found, track it
|
||||
string name = $"{result.Value.DeclaringType.Name}_{result.Value.MemberInfo.Name}";
|
||||
switch (result.Value.MemberInfo)
|
||||
{
|
||||
case FieldInfo fieldInfo when HlslKnownTypes.IsKnownType(fieldInfo.FieldType):
|
||||
variable = (name, fieldInfo);
|
||||
break;
|
||||
case PropertyInfo propertyInfo when HlslKnownTypes.IsKnownType(propertyInfo.PropertyType):
|
||||
variable = (name, propertyInfo);
|
||||
break;
|
||||
default: return node;
|
||||
}
|
||||
|
||||
// Return the mapped expression
|
||||
return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes a loaded static member, either a field or a property
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="MemberAccessExpressionSyntax"/> to check and modify if needed</param>
|
||||
/// <param name="memberInfo">The wrapped member that needs to be processed</param>
|
||||
/// <param name="isReadonly">Indicates whether or not the target member is readonly</param>
|
||||
/// <param name="variable">The info on parsed static members, if any</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
|
||||
private static SyntaxNode ProcessStaticMember(SyntaxNode node, ReadableMember memberInfo, bool isReadonly, ref (string Name, ReadableMember MemberInfo)? variable)
|
||||
{
|
||||
// Constant replacement if the value is a readonly scalar value
|
||||
if (isReadonly && HlslKnownTypes.IsKnownScalarType(memberInfo.MemberType))
|
||||
{
|
||||
LiteralExpressionSyntax expression = memberInfo.GetValue(null) switch
|
||||
{
|
||||
true => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)),
|
||||
false => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.FalseKeyword)),
|
||||
Bool b when b => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)),
|
||||
Bool b when !b => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.FalseKeyword)),
|
||||
IFormattable scalar => SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(scalar.ToString(null, CultureInfo.InvariantCulture))),
|
||||
_ => ThrowHelper.ThrowArgumentException<LiteralExpressionSyntax>("Invalid field type")
|
||||
};
|
||||
return expression.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
// Captured member, treat it like any other captured variable in the closure
|
||||
string name = $"{memberInfo.DeclaringType.Name}_{memberInfo.Name}";
|
||||
variable = (name, memberInfo);
|
||||
return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ComputeSharp.Shaders.Renderer.Models.Functions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a shader function
|
||||
/// </summary>
|
||||
internal sealed class FunctionInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the return type of the current function in the C# source
|
||||
/// </summary>
|
||||
public string FunctionCsharpReturnType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fullname of the current function in the C# source
|
||||
/// </summary>
|
||||
public string FunctionFullName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters of the function in the C# source
|
||||
/// </summary>
|
||||
public string FunctionCsharpParameters { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the return type of the current function
|
||||
/// </summary>
|
||||
public string ReturnType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the current function
|
||||
/// </summary>
|
||||
public string FunctionName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of parameters for the current function
|
||||
/// </summary>
|
||||
public IReadOnlyList<ParameterInfo> ParametersList { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the body of the current function
|
||||
/// </summary>
|
||||
public string FunctionBody { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="FunctionInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="functionCsharpType">The concrete return type for the function</param>
|
||||
/// <param name="functionFullname">The fullname of the function in the C# source</param>
|
||||
/// <param name="functionCsharpParameters">The parameters of the function in the C# source</param>
|
||||
/// <param name="returnType">The return type of the current function</param>
|
||||
/// <param name="functionName">The name of the current function</param>
|
||||
/// <param name="parameters">The function parameters, if any</param>
|
||||
/// <param name="functionBody">The current function</param>
|
||||
public FunctionInfo(
|
||||
Type functionCsharpType,
|
||||
string functionFullname,
|
||||
string functionCsharpParameters,
|
||||
string returnType,
|
||||
string functionName,
|
||||
IReadOnlyList<ParameterInfo> parameters,
|
||||
string functionBody)
|
||||
{
|
||||
FunctionCsharpReturnType = functionCsharpType.ToFriendlyString();
|
||||
FunctionFullName = functionFullname;
|
||||
FunctionCsharpParameters = functionCsharpParameters;
|
||||
ReturnType = returnType;
|
||||
FunctionName = functionName;
|
||||
ParametersList = parameters;
|
||||
FunctionBody = functionBody;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.Shaders.Renderer.Models.Functions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a local shader function
|
||||
/// </summary>
|
||||
internal sealed class LocalFunctionInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the prototype for the current function
|
||||
/// </summary>
|
||||
public string FunctionPrototype => FunctionDeclaration.Split(Environment.NewLine)[0];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the declaration of the current function
|
||||
/// </summary>
|
||||
public string FunctionDeclaration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="LocalFunctionInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="functionDeclaration">The declaration of the current function</param>
|
||||
public LocalFunctionInfo(string functionDeclaration)
|
||||
{
|
||||
FunctionDeclaration = functionDeclaration;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
|
||||
namespace ComputeSharp.Shaders.Renderer.Models.Functions
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that contains info on a function parameter
|
||||
/// </summary>
|
||||
internal sealed class ParameterInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the modifier to use for the current parameter
|
||||
/// </summary>
|
||||
public string ParameterModifier { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the current parameter
|
||||
/// </summary>
|
||||
public string ParameterType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name to use for the current parameter
|
||||
/// </summary>
|
||||
public string ParameterName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not the current parameter is the last one for the parent function
|
||||
/// </summary>
|
||||
public bool IsLastParameter { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ParameterInfo"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="parameterModifiers">The modifiers used in the current parameter</param>
|
||||
/// <param name="parameterType">The type of the current parameter</param>
|
||||
/// <param name="parameterName">The name of the current parameter</param>
|
||||
/// <param name="last">Indicates whether or not the current parameter is the last one</param>
|
||||
public ParameterInfo(IReadOnlyList<SyntaxToken> parameterModifiers, string parameterType, string parameterName, bool last)
|
||||
{
|
||||
if (parameterModifiers.Count == 0) ParameterModifier = "in";
|
||||
else if (parameterModifiers.First().IsKind(SyntaxKind.OutKeyword)) ParameterModifier = "out";
|
||||
else if (parameterModifiers.Any(m => m.IsKind(SyntaxKind.RefKeyword)) && !parameterModifiers.Any(m => m.IsKind(SyntaxKind.ReadOnlyKeyword))) ParameterModifier = "inout";
|
||||
else ParameterModifier = "in";
|
||||
|
||||
ParameterType = parameterType;
|
||||
ParameterName = parameterName;
|
||||
IsLastParameter = last;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Functions;
|
||||
|
||||
#pragma warning disable CS8618
|
||||
|
||||
@@ -50,11 +49,11 @@ namespace ComputeSharp.Shaders.Renderer.Models
|
||||
/// <summary>
|
||||
/// Gets or sets the list of static functions used by the shader.
|
||||
/// </summary>
|
||||
public IReadOnlyList<FunctionInfo> FunctionsList { get; init; }
|
||||
public IReadOnlyList<object> FunctionsList { get; init; } // TODO
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the list of local (implicit) functions used by the shader.
|
||||
/// Gets or sets the list of local functions used by the shader.
|
||||
/// </summary>
|
||||
public IReadOnlyList<LocalFunctionInfo> LocalFunctionsList { get; init; }
|
||||
public IReadOnlyList<string> LocalFunctionsList { get; init; }
|
||||
}
|
||||
}
|
||||
@@ -168,7 +168,7 @@ namespace ComputeSharp.Shaders
|
||||
ShaderLoader<T> shaderLoader = ShaderLoader<T>.Load(in shader);
|
||||
|
||||
// Render the loaded shader
|
||||
string shaderSource = ShaderRenderer.Instance.Render(new ShaderInfo
|
||||
using var shaderSource = HlslShaderRenderer.Render(new ShaderInfo
|
||||
{
|
||||
BuffersList = shaderLoader.HslsBuffersInfo,
|
||||
FieldsList = shaderLoader.FieldsInfo,
|
||||
@@ -182,7 +182,7 @@ namespace ComputeSharp.Shaders
|
||||
});
|
||||
|
||||
// Compile the loaded shader to HLSL bytecode
|
||||
IDxcBlobObject shaderBytecode = new(ShaderCompiler.CompileShader(shaderSource));
|
||||
IDxcBlobObject shaderBytecode = new(ShaderCompiler.CompileShader(shaderSource.WrittenSpan));
|
||||
|
||||
// Get the cached shader data
|
||||
shaderData = new CachedShader<T>(shaderLoader, shaderBytecode);
|
||||
|
||||
@@ -7,9 +7,7 @@ using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Functions;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
using TerraFX.Interop;
|
||||
using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_TYPE;
|
||||
@@ -63,12 +61,12 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> with the <see cref="FunctionInfo"/> items for the shader functions.
|
||||
/// </summary>
|
||||
private readonly List<FunctionInfo> functionsInfo = new();
|
||||
private readonly List<object> functionsInfo = new();
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> with the <see cref="FunctionInfo"/> items for the shader local functions.
|
||||
/// </summary>
|
||||
private readonly List<LocalFunctionInfo> localFunctionsInfo = new();
|
||||
private readonly List<string> localFunctionsInfo = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderLoader{T}"/> instance.
|
||||
@@ -108,12 +106,12 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// Gets the collection of <see cref="FunctionInfo"/> items for the shader.
|
||||
/// </summary>
|
||||
public IReadOnlyList<FunctionInfo> FunctionsInfo => this.functionsInfo;
|
||||
public IReadOnlyList<object> FunctionsInfo => this.functionsInfo;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of <see cref="LocalFunctionInfo"/> items for the shader.
|
||||
/// </summary>
|
||||
public IReadOnlyList<LocalFunctionInfo> LocalFunctionsInfo => this.localFunctionsInfo;
|
||||
public IReadOnlyList<string> LocalFunctionsInfo => this.localFunctionsInfo;
|
||||
|
||||
/// <summary>
|
||||
/// Loads and processes an input<typeparamref name="T"/> shadeer
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
/// <summary>
|
||||
/// A custom <see cref="CSharpSyntaxRewriter"/> <see langword="class"/> that processes C# methods to convert to HLSL
|
||||
/// </summary>
|
||||
internal class ShaderSyntaxRewriter : CSharpSyntaxRewriter
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="Microsoft.CodeAnalysis.SemanticModel"/> instance to use to rewrite the decompiled code
|
||||
/// </summary>
|
||||
private readonly SemanticModel SemanticModel;
|
||||
|
||||
/// <summary>
|
||||
/// The declaring type that hosts the root from which the current syntax tree is inspected
|
||||
/// </summary>
|
||||
private readonly Type DeclaringType;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderSyntaxRewriter"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="semanticModel">The <see cref="Microsoft.CodeAnalysis.SemanticModel"/> instance to use to rewrite the decompiled code</param>
|
||||
/// <param name="declaringType">The declaring type that hosts the root from which the current syntax tree is inspected</param>
|
||||
public ShaderSyntaxRewriter(SemanticModel semanticModel, Type declaringType)
|
||||
{
|
||||
SemanticModel = semanticModel;
|
||||
DeclaringType = declaringType;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, ReadableMember> _StaticMembers = new Dictionary<string, ReadableMember>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapping of captured static members used by the target code
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, ReadableMember> StaticMembers => _StaticMembers;
|
||||
|
||||
private readonly Dictionary<string, MethodInfo> _StaticMethods = new Dictionary<string, MethodInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mapping of captured static methods used by the target code
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, MethodInfo> StaticMethods => _StaticMethods;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
|
||||
{
|
||||
node = (IdentifierNameSyntax)base.VisitIdentifierName(node)!;
|
||||
node = node.ReplaceIdentifierName(DeclaringType, out var variable);
|
||||
|
||||
// Register the captured member, if any
|
||||
if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name))
|
||||
{
|
||||
_StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitParameter(ParameterSyntax node)
|
||||
{
|
||||
node = (ParameterSyntax)base.VisitParameter(node)!;
|
||||
node = node.WithAttributeLists(default);
|
||||
node = node.ReplaceType(node.Type!);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitArgument(ArgumentSyntax node)
|
||||
{
|
||||
node = (ArgumentSyntax)base.VisitArgument(node)!;
|
||||
|
||||
if (node.RefKindKeyword.IsKind(SyntaxKind.RefKeyword))
|
||||
{
|
||||
node = node.WithRefKindKeyword(SyntaxFactory.Token(SyntaxKind.None));
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitCastExpression(CastExpressionSyntax node)
|
||||
{
|
||||
node = (CastExpressionSyntax)base.VisitCastExpression(node)!;
|
||||
|
||||
return node.ReplaceType(node.Type);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
|
||||
{
|
||||
node = (LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node)!;
|
||||
|
||||
return node.ReplaceType(node.Declaration.Type);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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)));
|
||||
}
|
||||
|
||||
return SyntaxFactory.InvocationExpression(node.Type, node.ArgumentList);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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)));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
|
||||
{
|
||||
node = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!;
|
||||
SyntaxNode syntaxNode = node.ReplaceMember(SemanticModel, out var variable, out var method);
|
||||
|
||||
// Register the captured members, if any
|
||||
if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name))
|
||||
{
|
||||
_StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo);
|
||||
}
|
||||
if (method.HasValue && !_StaticMethods.ContainsKey(method.Value.Name))
|
||||
{
|
||||
_StaticMethods.Add(method.Value.Name, method.Value.MethodInfo);
|
||||
}
|
||||
|
||||
return syntaxNode;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
|
||||
{
|
||||
node = (InvocationExpressionSyntax)base.VisitInvocationExpression(node)!;
|
||||
node = node.ReplaceInvocation(DeclaringType, out var variable, out var method);
|
||||
|
||||
// Register the captured members, if any
|
||||
if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name))
|
||||
{
|
||||
_StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo);
|
||||
}
|
||||
if (method.HasValue && !_StaticMethods.ContainsKey(method.Value.Name))
|
||||
{
|
||||
_StaticMethods.Add(method.Value.Name, method.Value.MethodInfo);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
|
||||
{
|
||||
node = (MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!;
|
||||
|
||||
// Replace the return type node, if needed
|
||||
if (!node.ReturnType.ToString().Equals("void"))
|
||||
{
|
||||
return node.ReplaceType(node.ReturnType);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする