From 0be404c9b32356a501b6c8fe2204109e58d3dbca Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 16 Sep 2019 03:20:21 +0200 Subject: [PATCH] Minor code refactoring --- src/ComputeSharp.Shaders/ShaderRunner.cs | 7 +++-- .../Translation/Models/CachedShader.cs | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Translation/Models/CachedShader.cs diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 2b0ac27f..40da9b5e 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -7,6 +7,7 @@ using ComputeSharp.Graphics.Buffers.Extensions; using ComputeSharp.Shaders.Renderer; using ComputeSharp.Shaders.Renderer.Models; using ComputeSharp.Shaders.Translation; +using ComputeSharp.Shaders.Translation.Models; using Vortice.Direct3D12; using CommandList = ComputeSharp.Graphics.Commands.CommandList; using PipelineState = ComputeSharp.Graphics.Commands.PipelineState; @@ -40,7 +41,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, ShaderBytecode)> ShadersCache = new Dictionary<(int, int, int, int), (ShaderLoader, ShaderBytecode)>(); + private static readonly Dictionary<(int Id, int ThreadsX, int ThreadsY, int ThreadsZ), CachedShader> ShadersCache = new Dictionary<(int, int, int, int), CachedShader>(); /// /// Compiles and runs the input shader on a target instance, with the specified parameters @@ -60,7 +61,7 @@ namespace ComputeSharp.Shaders Action action) { // Try to get the cache shader - (ShaderLoader Loader, ShaderBytecode Bytecode) shaderData; + CachedShader shaderData; lock (ShadersCache) { var key = (ShaderLoader.GetHashCode(action), threadsX, threadsY, threadsZ); @@ -88,7 +89,7 @@ namespace ComputeSharp.Shaders ShaderBytecode shaderBytecode = ShaderCompiler.CompileShader(shaderSource); // Cache for later use - shaderData = (shaderLoader, shaderBytecode); + shaderData = new CachedShader(shaderLoader, shaderBytecode); ShadersCache.Add(key, shaderData); } } diff --git a/src/ComputeSharp.Shaders/Translation/Models/CachedShader.cs b/src/ComputeSharp.Shaders/Translation/Models/CachedShader.cs new file mode 100644 index 00000000..8c5cf20c --- /dev/null +++ b/src/ComputeSharp.Shaders/Translation/Models/CachedShader.cs @@ -0,0 +1,31 @@ +using Vortice.Direct3D12; + +namespace ComputeSharp.Shaders.Translation.Models +{ + /// + /// A that contains info on a cached shader + /// + internal readonly struct CachedShader + { + /// + /// The instance with the shader metadata + /// + public readonly ShaderLoader Loader; + + /// + /// The compiled shader bytecode + /// + public readonly ShaderBytecode Bytecode; + + /// + /// 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; + } + } +}