Merge pull request #36 from Sergio0694/feature_ref-intrinsics

HLSL intrinsics with reference parameters
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-24 23:58:24 +02:00
コミット 6cf5c5bb94
5ファイル変更
+386 -200

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

+2 -2
ファイルの表示
@@ -18,9 +18,9 @@ namespace ComputeSharp.Graphics.Buffers
internal ReadOnlyBuffer(GraphicsDevice device, int size) : base(device, size, BufferType.ReadOnly) { }
/// <summary>
/// Gets or sets a single <typeparamref name="T"/> value from the current read write buffer
/// Gets a single <typeparamref name="T"/> value from the current read write buffer
/// </summary>
/// <param name="i">The index of the value to get or set</param>
/// <param name="i">The index of the value to get</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
public T this[uint i] => throw new InvalidExecutionContextException($"{nameof(ReadOnlyBuffer<T>)}<T>[uint i]");
}
+1 -5
ファイルの表示
@@ -22,10 +22,6 @@ namespace ComputeSharp.Graphics.Buffers
/// </summary>
/// <param name="i">The index of the value to get or set</param>
/// <remarks>This API can only be used from a compute shader, and will always throw if used anywhere else</remarks>
public T this[uint i]
{
get => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer<T>)}<T>[uint i]");
set => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer<T>)}<T>[uint i]");
}
public ref T this[uint i] => throw new InvalidExecutionContextException($"{nameof(ReadWriteBuffer<T>)}<T>[uint i]");
}
}
ファイル差分が大きすぎるため表示されません。 差分を読み込み
+47 -2
ファイルの表示
@@ -8,6 +8,7 @@ using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Text.RegularExpressions;
using ComputeSharp.Shaders.Mappings;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
@@ -170,10 +171,16 @@ namespace ComputeSharp.Shaders.Translation
}
// Unwrap the nested fields
string unwrappedCode = UnwrapSyntaxTree(methodFixedCode);
string unwrappedSourceCode = UnwrapSyntaxTree(methodFixedCode);
// Remove the in keyword from the source
string inFixedSourceCode = Regex.Replace(unwrappedSourceCode, @"(?<!\w)in ", string.Empty);
// Tweak the out declarations
string outFixedSourceCode = RefactorInlineOutDeclarations(inFixedSourceCode, methodInfo.Name);
// Load the type syntax tree
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(unwrappedCode);
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(outFixedSourceCode);
// Get the root node to return
rootNode = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name));
@@ -255,5 +262,43 @@ namespace ComputeSharp.Shaders.Translation
return source;
}
/// <summary>
/// Refactors all the inline out expressions with a variable declaration
/// </summary>
/// <param name="source">The input source code to process</param>
/// <param name="entryPoint">The name of the shader method being processed</param>
[Pure]
private string RefactorInlineOutDeclarations(string source, string entryPoint)
{
// Replace the discards
source = Regex.Replace(source, @"(?<!\w)out ([\w.]+) _(?!_)", m => $"out {m.Groups[1].Value} {new string(Guid.NewGuid().ToByteArray().Select(b => (char)('a' + b % 26)).ToArray())}");
// Load the syntax tree and the entry node
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
MethodDeclarationSyntax rootNode = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First(node => node.GetLeadingTrivia().ToFullString().Contains(entryPoint));
// Get the out declarations to replace
var outs = (
from argument in rootNode.DescendantNodes().OfType<ArgumentSyntax>()
where argument.RefKindKeyword.IsKind(SyntaxKind.OutKeyword) &&
argument.Expression.IsKind(SyntaxKind.DeclarationExpression)
let match = Regex.Match(argument.Expression.ToFullString(), @"([\w.]+) ([\w_]+)")
let mappedType = HlslKnownTypes.GetMappedName(match.Groups[1].Value)
let declatation = $"{mappedType} {match.Groups[2].Value} = ({mappedType})0;"
select (match.Groups[1].Value, match.Groups[2].Value, declatation)).ToArray();
// Insert the explicit declarations at the start of the method
int start = rootNode.Body.ChildNodes().First().SpanStart;
foreach (var item in outs.Reverse())
{
source = source.Insert(start, $"{item.declatation}{Environment.NewLine} ");
}
// Remove the out keyword from the source
source = Regex.Replace(source, @"(?<!\w)out ", string.Empty);
return source;
}
}
}
+32
ファイルの表示
@@ -0,0 +1,32 @@
using System;
using ComputeSharp.Graphics.Buffers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("IntrinsicTests")]
public class IntrinsicTests
{
[TestMethod]
public void LocalScalarAssignToBuffer()
{
float angle = 80;
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(4);
Action<ThreadIds> action = id =>
{
buffer[0] = Hlsl.Sin(angle);
buffer[1] = Hlsl.Cos(angle);
Hlsl.SinCos(angle, out buffer[2], out buffer[3]);
};
Gpu.Default.For(1, action);
float[] result = buffer.GetData();
Assert.IsTrue(MathF.Abs(result[0] - result[2]) < 0.0001f);
Assert.IsTrue(MathF.Abs(result[1] - result[3]) < 0.0001f);
}
}
}