Fixed multithread issue, minor performance improvement

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-15 19:26:40 +02:00
1 親 b5c13451b0
コミット 381f99dd3e
2ファイル変更
+40 -44

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

+28 -24
ファイルの表示
@@ -40,7 +40,7 @@ namespace ComputeSharp.Shaders
/// <summary>
/// The mapping used to cache and reuse compiled shaders
/// </summary>
private static readonly Dictionary<(int Id, int ThreadsX, int ThreadsY, int ThreadsZ), (ShaderLoader Loader, ShaderBytecode Bytecode)> ShadersCache = new Dictionary<(int, int, int, int), (ShaderLoader, ShaderBytecode)>();
private static readonly Dictionary<(int Id, int ThreadsX, int ThreadsY, int ThreadsZ), (ShaderLoader, ShaderBytecode)> ShadersCache = new Dictionary<(int, int, int, int), (ShaderLoader, ShaderBytecode)>();
/// <summary>
/// Compiles and runs the input shader on a target <see cref="GraphicsDevice"/> instance, with the specified parameters
@@ -60,33 +60,37 @@ namespace ComputeSharp.Shaders
Action<ThreadIds> action)
{
// Try to get the cache shader
var key = (ShaderLoader.GetHashCode(action), threadsX, threadsY, threadsZ);
if (!ShadersCache.TryGetValue(key, out var shaderData))
(ShaderLoader Loader, ShaderBytecode Bytecode) shaderData;
lock (ShadersCache)
{
// Load the input shader
ShaderLoader shaderLoader = ShaderLoader.Load(action);
// Render the loaded shader
ShaderInfo shaderInfo = new ShaderInfo
var key = (ShaderLoader.GetHashCode(action), threadsX, threadsY, threadsZ);
if (!ShadersCache.TryGetValue(key, out shaderData))
{
BuffersList = shaderLoader.BuffersList,
FieldsList = shaderLoader.FieldsList,
NumThreadsX = threadsX,
NumThreadsY = threadsY,
NumThreadsZ = threadsZ,
ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName,
ShaderBody = shaderLoader.MethodBody,
FunctionsList = shaderLoader.FunctionsList,
LocalFunctionsList = shaderLoader.LocalFunctionsList
};
string shaderSource = ShaderRenderer.Instance.Render(shaderInfo);
// Load the input shader
ShaderLoader shaderLoader = ShaderLoader.Load(action);
// Compile the loaded shader to HLSL bytecode
ShaderBytecode shaderBytecode = ShaderCompiler.CompileShader(shaderSource);
// Render the loaded shader
ShaderInfo shaderInfo = new ShaderInfo
{
BuffersList = shaderLoader.BuffersList,
FieldsList = shaderLoader.FieldsList,
NumThreadsX = threadsX,
NumThreadsY = threadsY,
NumThreadsZ = threadsZ,
ThreadsIdsVariableName = shaderLoader.ThreadsIdsVariableName,
ShaderBody = shaderLoader.MethodBody,
FunctionsList = shaderLoader.FunctionsList,
LocalFunctionsList = shaderLoader.LocalFunctionsList
};
string shaderSource = ShaderRenderer.Instance.Render(shaderInfo);
// Cache for later use
shaderData = (shaderLoader, shaderBytecode);
ShadersCache.Add(key, shaderData);
// Compile the loaded shader to HLSL bytecode
ShaderBytecode shaderBytecode = ShaderCompiler.CompileShader(shaderSource);
// Cache for later use
shaderData = (shaderLoader, shaderBytecode);
ShadersCache.Add(key, shaderData);
}
}
// Create the root signature for the pipeline and get the pipeline state
+12 -20
ファイルの表示
@@ -29,11 +29,6 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
public static MethodDecompiler Instance { get; } = new MethodDecompiler();
/// <summary>
/// The dummy object used to handle concurrent decompilation requests
/// </summary>
private readonly object Lock = new object();
/// <summary>
/// The mapping of available <see cref="CSharpDecompiler"/> instances targeting different assemblies
/// </summary>
@@ -141,25 +136,22 @@ namespace ComputeSharp.Shaders.Translation
/// <param name="semanticModel">The semantic model for the input method</param>
public void GetSyntaxTree(MethodInfo methodInfo, MethodType methodType, out MethodDeclarationSyntax rootNode, out SemanticModel semanticModel)
{
lock (Lock)
string sourceCode = methodType switch
{
string sourceCode = methodType switch
{
MethodType.Closure => GetSyntaxTreeForClosureMethod(methodInfo),
MethodType.Static => GetSyntaxTreeForStaticMethod(methodInfo),
_ => throw new ArgumentOutOfRangeException(nameof(methodType), $"Invalid method type: {methodType}")
};
MethodType.Closure => GetSyntaxTreeForClosureMethod(methodInfo),
MethodType.Static => GetSyntaxTreeForStaticMethod(methodInfo),
_ => throw new ArgumentOutOfRangeException(nameof(methodType), $"Invalid method type: {methodType}")
};
// Load the type syntax tree
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);
// Load the type syntax tree
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);
// Get the root node to return
rootNode = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name));
// Get the root node to return
rootNode = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name));
// Update the incremental compilation and retrieve the syntax tree for the method
_Compilation = _Compilation.AddSyntaxTrees(syntaxTree);
semanticModel = _Compilation.GetSemanticModel(syntaxTree);
}
// Update the incremental compilation and retrieve the syntax tree for the method
_Compilation = _Compilation.AddSyntaxTrees(syntaxTree);
semanticModel = _Compilation.GetSemanticModel(syntaxTree);
}
/// <summary>