From b096c70d23d9e43d0562ff39b905e465c7d80ecf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Jul 2021 13:21:20 +0200 Subject: [PATCH] Removed IDxcBlobObject type, code refactoring --- .../Interop/__Internals/ResourceDescriptor.cs | 2 +- src/ComputeSharp/Shaders/ShaderRunner{T}.cs | 21 ++++----- .../Translation/Interop/IDxcBlobObject.cs | 38 ---------------- .../Translation/Models/CachedShader.cs | 45 +++++++++++++++++++ .../Translation/Models/CachedShader{T}.cs | 44 ------------------ 5 files changed, 55 insertions(+), 95 deletions(-) delete mode 100644 src/ComputeSharp/Shaders/Translation/Interop/IDxcBlobObject.cs create mode 100644 src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs delete mode 100644 src/ComputeSharp/Shaders/Translation/Models/CachedShader{T}.cs diff --git a/src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs b/src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs index e97fe2ab..6b2d9422 100644 --- a/src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs +++ b/src/ComputeSharp/Graphics/Resources/Interop/__Internals/ResourceDescriptor.cs @@ -4,7 +4,7 @@ using TerraFX.Interop; using FX = TerraFX.Interop.Windows; using static TerraFX.Interop.D3D12_DESCRIPTOR_RANGE_FLAGS; -#pragma warning disable IDE0052 +#pragma warning disable IDE0052, CS0414 namespace ComputeSharp.__Internals { diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index 5293e0f4..15a0c699 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -5,7 +5,6 @@ using ComputeSharp.Graphics.Commands; using ComputeSharp.Shaders.Dispatching; using ComputeSharp.Shaders.Extensions; using ComputeSharp.Shaders.Translation; -using ComputeSharp.Shaders.Translation.Interop; using ComputeSharp.Shaders.Translation.Models; using ComputeSharp.__Internals; using Microsoft.Toolkit.Diagnostics; @@ -27,7 +26,7 @@ namespace ComputeSharp.Shaders /// /// The mapping used to cache and reuse compiled shaders. /// - private static readonly Dictionary> ShadersCache = new(); + private static readonly Dictionary ShadersCache = new(); /// /// Compiles and runs the input shader on a target instance, with the specified parameters. @@ -134,7 +133,7 @@ namespace ComputeSharp.Shaders lock (ShadersCache) { // Get or preload the shader - if (!ShadersCache.TryGetValue(key, out CachedShader shaderData)) + if (!ShadersCache.TryGetValue(key, out CachedShader? shaderData)) { LoadShader(threadsX, threadsY, threadsZ, in shader, out shaderData); @@ -192,7 +191,7 @@ namespace ComputeSharp.Shaders lock (ShadersCache) { // Get or preload the shader - if (!ShadersCache.TryGetValue(key, out CachedShader shaderData)) + if (!ShadersCache.TryGetValue(key, out CachedShader? shaderData)) { LoadShader(threadsX, threadsY, 1, in shader, out shaderData); @@ -234,9 +233,9 @@ namespace ComputeSharp.Shaders /// 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. + /// The instance to return with the cached shader data. [MethodImpl(MethodImplOptions.NoInlining)] - private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, in T shader, out CachedShader shaderData) + private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, in T shader, out CachedShader shaderData) { ArrayPoolStringBuilder builder = ArrayPoolStringBuilder.Create(); @@ -246,19 +245,17 @@ namespace ComputeSharp.Shaders builder.Dispose(); - IDxcBlobObject shaderBytecode = new(dxcBlobBytecode.Get()); - - shaderData = new CachedShader(null!, shaderBytecode); // TODO + shaderData = new CachedShader(dxcBlobBytecode.Get()); } /// /// 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 instance with the data on the loaded shader. /// The resulting instance to use to run the shader. [MethodImpl(MethodImplOptions.NoInlining)] - private static unsafe void CreatePipelineData(GraphicsDevice device, in CachedShader shaderData, out PipelineData pipelineData) + private static unsafe void CreatePipelineData(GraphicsDevice device, CachedShader shaderData, out PipelineData pipelineData) { using ComPtr d3D12RootSignature = default; @@ -266,7 +263,7 @@ namespace ComputeSharp.Shaders default(T).LoadDispatchMetadata(ref metadataLoader, out *(IntPtr*)&d3D12RootSignature); - using ComPtr d3D12PipelineState = device.D3D12Device->CreateComputePipelineState(d3D12RootSignature.Get(), shaderData.Bytecode.D3D12ShaderBytecode); + using ComPtr d3D12PipelineState = device.D3D12Device->CreateComputePipelineState(d3D12RootSignature.Get(), shaderData.D3D12ShaderBytecode); pipelineData = new PipelineData(d3D12RootSignature.Get(), d3D12PipelineState.Get()); diff --git a/src/ComputeSharp/Shaders/Translation/Interop/IDxcBlobObject.cs b/src/ComputeSharp/Shaders/Translation/Interop/IDxcBlobObject.cs deleted file mode 100644 index 9f7e9135..00000000 --- a/src/ComputeSharp/Shaders/Translation/Interop/IDxcBlobObject.cs +++ /dev/null @@ -1,38 +0,0 @@ -using ComputeSharp.Interop; -using TerraFX.Interop; - -namespace ComputeSharp.Shaders.Translation.Interop -{ - /// - /// An object wrapping an instance. - /// - internal sealed unsafe class IDxcBlobObject : NativeObject - { - /// - /// The instance currently in use. - /// - private ComPtr dxcBlob; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The instance to wrap. - public IDxcBlobObject(IDxcBlob* dxcBlob) - { - this.dxcBlob = new ComPtr(dxcBlob); - } - - /// - /// Gets a raw pointer to the instance in use. - /// - public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlob.Get()); - - /// - protected override bool OnDispose() - { - this.dxcBlob.Dispose(); - - return true; - } - } -} diff --git a/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs b/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs new file mode 100644 index 00000000..8bf11804 --- /dev/null +++ b/src/ComputeSharp/Shaders/Translation/Models/CachedShader.cs @@ -0,0 +1,45 @@ +using System.Runtime.CompilerServices; +using ComputeSharp.Graphics.Commands; +using ComputeSharp.Interop; +using TerraFX.Interop; + +namespace ComputeSharp.Shaders.Translation.Models +{ + /// + /// A class that contains info on a cached shader. + /// + internal sealed unsafe class CachedShader : NativeObject + { + /// + /// The instance currently in use. + /// + private ComPtr dxcBlobBytecode; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The bytecode instance to wrap. + public CachedShader(IDxcBlob* dxcBlobBytecode) + { + this.dxcBlobBytecode = new ComPtr(dxcBlobBytecode); + } + + /// + /// Gets the map of cached instances for each GPU in use. + /// + public ConditionalWeakTable CachedPipelines { get; } = new(); + + /// + /// Gets a raw pointer to the instance in use. + /// + public D3D12_SHADER_BYTECODE D3D12ShaderBytecode => new((ID3DBlob*)this.dxcBlobBytecode.Get()); + + /// + protected override bool OnDispose() + { + this.dxcBlobBytecode.Dispose(); + + return true; + } + } +} diff --git a/src/ComputeSharp/Shaders/Translation/Models/CachedShader{T}.cs b/src/ComputeSharp/Shaders/Translation/Models/CachedShader{T}.cs deleted file mode 100644 index 8a616cb5..00000000 --- a/src/ComputeSharp/Shaders/Translation/Models/CachedShader{T}.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Runtime.CompilerServices; -using ComputeSharp.__Internals; -using ComputeSharp.Graphics.Commands; -using ComputeSharp.Shaders.Translation.Interop; - -#pragma warning disable CS0618 - -namespace ComputeSharp.Shaders.Translation.Models -{ - /// - /// A that contains info on a cached shader. - /// - /// The type of compute shader in use. - internal readonly struct CachedShader - where T : struct, IShader - { - /// - /// The instance with the shader metadata. - /// - public readonly ShaderLoader Loader; - - /// - /// The compiled shader bytecode. - /// - public readonly IDxcBlobObject Bytecode; - - /// - /// The map of cached instances for each GPU in use. - /// - 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, IDxcBlobObject bytecode) - { - Loader = loader; - Bytecode = bytecode; - CachedPipelines = new(); - } - } -}