diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index d7af2136..a15f04c8 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -122,26 +122,7 @@ internal static class ShaderRunner Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); Guard.IsBetweenOrEqualTo(groupsZ, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); - ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, threadsZ); - PipelineData? pipelineData; - - lock (ShadersCache) - { - // Get or preload the shader - if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData)) - { - LoadShader(threadsX, threadsY, threadsZ, ref shader, out shaderData); - - // Cache for later use - ShadersCache.Add(key, shaderData); - } - - // Create the reusable pipeline state - if (!shaderData.CachedPipelines.TryGetValue(device, out pipelineData)) - { - CreatePipelineData(device, shaderData, out pipelineData); - } - } + PipelineData pipelineData = GetPipelineData(device, threadsX, threadsY, threadsZ, ref shader); using CommandList commandList = new(device, pipelineData.D3D12PipelineState); @@ -168,34 +149,16 @@ internal static class ShaderRunner int x = texture.Width; int y = texture.Height; - int threadsX = 8; - int threadsY = 8; + const int threadsX = 8; + const int threadsY = 8; + const int threadsZ = 1; int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1); int groupsY = Math.DivRem(y, threadsY, out int modY) + (modY == 0 ? 0 : 1); Guard.IsBetweenOrEqualTo(groupsX, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); Guard.IsBetweenOrEqualTo(groupsY, 1, D3D11.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); - ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, 1); - PipelineData? pipelineData; - - lock (ShadersCache) - { - // Get or preload the shader - if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData)) - { - LoadShader(threadsX, threadsY, 1, ref shader, out shaderData); - - // Cache for later use - ShadersCache.Add(key, shaderData); - } - - // Create the reusable pipeline state - if (!shaderData.CachedPipelines.TryGetValue(device, out pipelineData)) - { - CreatePipelineData(device, shaderData, out pipelineData); - } - } + PipelineData pipelineData = GetPipelineData(device, threadsX, threadsY, threadsZ, ref shader); using CommandList commandList = new(device, pipelineData.D3D12PipelineState); @@ -214,6 +177,40 @@ internal static class ShaderRunner commandList.ExecuteAndWaitForCompletion(); } + /// + /// Gets the instance for a given shader. + /// + /// The to use to run the shader. + /// 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 for a given shader. + public static unsafe PipelineData GetPipelineData(GraphicsDevice device, int threadsX, int threadsY, int threadsZ, ref T shader) + { + ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, threadsZ); + + lock (ShadersCache) + { + // Get or preload the shader + if (!ShadersCache.TryGetValue(key, out ICachedShader? shaderData)) + { + LoadShader(threadsX, threadsY, threadsZ, ref shader, out shaderData); + + // Cache for later use + ShadersCache.Add(key, shaderData); + } + + // Create the reusable pipeline state + if (!shaderData.CachedPipelines.TryGetValue(device, out PipelineData? pipelineData)) + { + CreatePipelineData(device, shaderData, out pipelineData); + } + + return pipelineData; + } + } + /// /// Loads a shader with the specified parameters. ///