diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 038d4b8a..2b0ac27f 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -40,7 +40,7 @@ namespace ComputeSharp.Shaders /// /// The mapping used to cache and reuse compiled shaders /// - 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)>(); /// /// Compiles and runs the input shader on a target instance, with the specified parameters @@ -60,33 +60,37 @@ namespace ComputeSharp.Shaders Action 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 diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index 0a543835..cd48a6bf 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -29,11 +29,6 @@ namespace ComputeSharp.Shaders.Translation /// public static MethodDecompiler Instance { get; } = new MethodDecompiler(); - /// - /// The dummy object used to handle concurrent decompilation requests - /// - private readonly object Lock = new object(); - /// /// The mapping of available instances targeting different assemblies /// @@ -141,25 +136,22 @@ namespace ComputeSharp.Shaders.Translation /// The semantic model for the input method 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().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name)); + // Get the root node to return + rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().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); } ///