From 1d9b0a94e7aea9038076fbc2cff022cd7575d7d3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 11 Aug 2019 23:47:32 +0200 Subject: [PATCH] ShaderCompiler class refactored --- src/DirectX12ComputeShaderSample/Gpu.cs | 3 +- .../ShaderCompiler.cs | 87 ++++++++++++------- .../ShaderGenerator.cs | 4 +- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/DirectX12ComputeShaderSample/Gpu.cs b/src/DirectX12ComputeShaderSample/Gpu.cs index c8153509..fd97da90 100644 --- a/src/DirectX12ComputeShaderSample/Gpu.cs +++ b/src/DirectX12ComputeShaderSample/Gpu.cs @@ -7,6 +7,7 @@ using SharpDX.D3DCompiler; using SharpDX.Direct3D12; using CommandList = DirectX12GameEngine.Graphics.CommandList; using PipelineState = DirectX12GameEngine.Graphics.PipelineState; +using ShaderBytecode = SharpDX.Direct3D12.ShaderBytecode; namespace DirectX12ComputeShaderSample { @@ -25,7 +26,7 @@ namespace DirectX12ComputeShaderSample ShaderGenerator shaderGenerator = new ShaderGenerator(shader); ShaderGenerationResult result = shaderGenerator.GenerateShaderForLambda(); - byte[] shaderBytecode = ShaderCompiler.CompileShader(result.ShaderSource, ShaderVersion.ComputeShader, result.ComputeShader); + ShaderBytecode shaderBytecode = ShaderCompiler.Instance.CompileShader(result.ShaderSource); // Create the root signature for the pipeline and the pipeline state RootSignatureDescription rootSignatureDescription = new RootSignatureDescription(RootSignatureFlags.None, shaderGenerator.RootParameters); diff --git a/src/DirectX12GameEngine.Shaders/ShaderCompiler.cs b/src/DirectX12GameEngine.Shaders/ShaderCompiler.cs index ffe9aba0..ea64c675 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderCompiler.cs +++ b/src/DirectX12GameEngine.Shaders/ShaderCompiler.cs @@ -1,33 +1,54 @@ using System; -using System.IO; +using System.Diagnostics.Contracts; using DotNetDxc; -using SharpDX.D3DCompiler; +using SharpDX.Direct3D12; namespace DirectX12GameEngine.Shaders { - public static class ShaderCompiler + /// + /// A that uses the APIs to compile compute shaders + /// + public sealed class ShaderCompiler { - static ShaderCompiler() + /// + /// Gets the singleton instance to use + /// + public static ShaderCompiler Instance { get; } = new ShaderCompiler(); + + /// + /// The instance to use to create new HLSL shaders + /// + private readonly IDxcCompiler Compiler; + + /// + /// The instance to use to create the bytecode for HLSL sources + /// + private readonly IDxcLibrary Library; + + /// + /// Creates a new instance + /// + private ShaderCompiler() { HlslDxcLib.DxcCreateInstanceFn = DefaultDxcLib.GetDxcCreateInstanceFn(); + Compiler = HlslDxcLib.CreateDxcCompiler(); + Library = HlslDxcLib.CreateDxcLibrary(); } - public static byte[] CompileShaderFile(string filePath, ShaderVersion? version = null, string? entryPoint = null) + /// + /// Compiles a new HLSL shader from the input source code + /// + /// The HLSL source code to compile + /// The bytecode for the compiled shader + [Pure] + public ShaderBytecode CompileShader(string source) { - string shaderSource = File.ReadAllText(filePath); - return CompileShader(shaderSource, version, entryPoint, filePath); - } - - public static byte[] CompileShader(string shaderSource, ShaderVersion? version = null, string? entryPoint = null, string filePath = "") - { - IDxcCompiler compiler = HlslDxcLib.CreateDxcCompiler(); - IDxcLibrary library = HlslDxcLib.CreateDxcLibrary(); - const uint CP_UTF16 = 1200; - IDxcBlobEncoding sourceBlob = library.CreateBlobWithEncodingOnHeapCopy(shaderSource, (uint)(shaderSource.Length * 2), CP_UTF16); - IDxcOperationResult result = compiler.Compile(sourceBlob, filePath, entryPoint ?? "CSMain", "cs_6_1", new[] { "-Zpr" }, 1, null, 0, library.CreateIncludeHandler()); + IDxcBlobEncoding sourceBlob = Library.CreateBlobWithEncodingOnHeapCopy(source, (uint)(source.Length * 2), CP_UTF16); + IDxcOperationResult result = Compiler.Compile(sourceBlob, "", "CSMain", "cs_6_1", new[] { "-Zpr" }, 1, null, 0, Library.CreateIncludeHandler()); + // Get the compiled bytecode in case of success if (result.GetStatus() == 0) { IDxcBlob blob = result.GetResult(); @@ -35,31 +56,37 @@ namespace DirectX12GameEngine.Shaders return bytecode; } - else - { - string resultText = GetStringFromBlob(library, result.GetErrors()); - throw new Exception(resultText); - } + + // Compile error + string resultText = GetStringFromBlob(Library, result.GetErrors()); + throw new Exception(resultText); } - public static unsafe byte[] GetBytesFromBlob(IDxcBlob blob) + /// + /// Reads the buffer content from an input instance + /// + /// The input instance to read data from + /// The read bytes buffer, as an array + [Pure] + private static unsafe byte[] GetBytesFromBlob(IDxcBlob blob) { byte* pMem = (byte*)blob.GetBufferPointer(); uint size = blob.GetBufferSize(); byte[] result = new byte[size]; - fixed (byte* pTarget = result) - { - for (uint i = 0; i < size; i++) - { - pTarget[i] = pMem[i]; - } - } + new Span(pMem, (int)size).CopyTo(result.AsSpan()); return result; } - public static unsafe string GetStringFromBlob(IDxcLibrary library, IDxcBlob blob) + /// + /// Reads a value from an input instance + /// + /// The source to use to load the data + /// The input instance to read data from + /// The resulting read from the input data buffer + [Pure] + private static unsafe string GetStringFromBlob(IDxcLibrary library, IDxcBlob blob) { blob = library.GetBlobAstUf16(blob); return new string(blob.GetBufferPointer(), 0, (int)(blob.GetBufferSize() / 2)); diff --git a/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs b/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs index 08e2dde6..6f136d9e 100644 --- a/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs +++ b/src/DirectX12GameEngine.Shaders/ShaderGenerator.cs @@ -99,7 +99,7 @@ namespace DirectX12GameEngine.Shaders stringWriter.GetStringBuilder().TrimEnd(); writer.WriteLine(); - result = new ShaderGenerationResult(stringWriter.ToString(), "Foo"); /* action.Method.Name */ + result = new ShaderGenerationResult(stringWriter.ToString(), "CSMain"); /* action.Method.Name */ return result; } @@ -330,7 +330,7 @@ namespace DirectX12GameEngine.Shaders root = syntaxRewriter.Visit(root); string shaderSource = root.ToFullString(); - shaderSource = shaderSource.Replace("internal void
b__0", "void Foo"); + shaderSource = shaderSource.Replace("internal void
b__0", "void CSMain"); shaderSource = shaderSource.Replace("uint3 id", "uint3 id : SV_DispatchThreadId"); // TODO: See why the System namespace in System.Math is not present in UWP projects.