diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 1b86abe1..6443dc70 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Renderer; @@ -60,44 +61,29 @@ namespace ComputeSharp.Shaders int threadsX, int threadsY, int threadsZ, in T shader) { - // Try to get the cache shader + // Create the shader key + ShaderKey key = new ShaderKey(ShaderHashCodeProvider.GetHashCode(shader), threadsX, threadsY, threadsZ); CachedShader shaderData; + PipelineState pipelineState; + lock (ShadersCache) { - var key = new ShaderKey(ShaderHashCodeProvider.GetHashCode(shader), threadsX, threadsY, threadsZ); + // Get or preload the shader if (!ShadersCache.TryGetValue(key, out shaderData)) { - // Load the input shader - ShaderLoader shaderLoader = ShaderLoader.Load(shader); - - // 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); - - // Compile the loaded shader to HLSL bytecode - ShaderBytecode shaderBytecode = ShaderCompiler.CompileShader(shaderSource); + LoadShader(threadsX, threadsY, threadsZ, shader, out shaderData); // Cache for later use - ShadersCache.Add(key, shaderData = new CachedShader(shaderLoader, shaderBytecode)); + ShadersCache.Add(key, shaderData); + } + + // Create the reusable pipeline state + if (!shaderData.CachedPipelines.TryGetValue(device, out pipelineState)) + { + CreatePipelineState(device, shaderData, out pipelineState); } } - // Create the root signature for the pipeline and get the pipeline state - VersionedRootSignatureDescription rootSignatureDescription = new VersionedRootSignatureDescription(new RootSignatureDescription1(RootSignatureFlags.None, shaderData.Loader.RootParameters)); - ID3D12RootSignature rootSignature = device.CreateRootSignature(rootSignatureDescription); - PipelineState pipelineState = new PipelineState(device, rootSignature, shaderData.Bytecode); - // Create the commands list and set the pipeline state using CommandList commandList = new CommandList(device, CommandListType.Compute); commandList.SetPipelineState(pipelineState); @@ -127,5 +113,59 @@ namespace ComputeSharp.Shaders commandList.Dispatch(groupsX, groupsY, groupsZ); commandList.ExecuteAndWaitForCompletion(); } + + /// + /// Loads a shader with the specified parameters + /// + /// The number of threads in each thread group for the X axis + /// The number of threads in each thread group for the Y axis + /// The number of threads in each thread group for the Z axis + /// The input instance representing the compute shader to run + /// The instance to return with the cached shader data + [MethodImpl(MethodImplOptions.NoInlining)] + private static void LoadShader(int threadsX, int threadsY, int threadsZ, in T shader, out CachedShader shaderData) + { + // Load the input shader + ShaderLoader shaderLoader = ShaderLoader.Load(shader); + + // Render the loaded shader + string shaderSource = ShaderRenderer.Instance.Render(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 + }); + + // Compile the loaded shader to HLSL bytecode + ShaderBytecode shaderBytecode = ShaderCompiler.CompileShader(shaderSource); + + // Get the cached shader data + shaderData = new CachedShader(shaderLoader, shaderBytecode); + } + + /// + /// Creates and caches a instance for a given shader + /// + /// The to use to run the shader + /// The instance with the data on the loaded shader + /// The resulting instance to use to run the shader + [MethodImpl(MethodImplOptions.NoInlining)] + private static void CreatePipelineState(GraphicsDevice device, in CachedShader shaderData, out PipelineState pipelineState) + { + // Create the root signature for the pipeline and get the pipeline state + RootSignatureDescription1 rootSignatureDescription = new RootSignatureDescription1(RootSignatureFlags.None, shaderData.Loader.RootParameters); + VersionedRootSignatureDescription versionedRootSignatureDescription = new VersionedRootSignatureDescription(rootSignatureDescription); + ID3D12RootSignature rootSignature = device.CreateRootSignature(versionedRootSignatureDescription); + pipelineState = new PipelineState(device, rootSignature, shaderData.Bytecode); + + // Cache the pipeline state for the target device + shaderData.CachedPipelines.Add(device, pipelineState); + } } } diff --git a/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs b/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs index 3b0005e0..c92a96e4 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/CachedShader{T}.cs @@ -1,4 +1,7 @@ -using Vortice.Direct3D12; +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics; +using ComputeSharp.Graphics.Commands; +using Vortice.Direct3D12; namespace ComputeSharp.Shaders.Translation.Models { @@ -10,7 +13,7 @@ namespace ComputeSharp.Shaders.Translation.Models where T : struct, IComputeShader { /// - /// The instance with the shader metadata + /// The instance with the shader metadata /// public readonly ShaderLoader Loader; @@ -20,14 +23,20 @@ namespace ComputeSharp.Shaders.Translation.Models public readonly ShaderBytecode Bytecode; /// - /// Creates a new instance with the specified parameters + /// The map of cached instances for each GPU in use. /// - /// The instance with the shader metadata + public readonly ConditionalWeakTable CachedPipelines; + + /// + /// Creates a new instance with the specified parameters + /// + /// The instance with the shader metadata /// The compiled shader bytecode public CachedShader(ShaderLoader loader, ShaderBytecode bytecode) { Loader = loader; Bytecode = bytecode; + CachedPipelines = new ConditionalWeakTable(); } } }