From 0be83e10899ab3b84232de62299381b32cd8099b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 1 Apr 2022 03:29:56 +0200 Subject: [PATCH] Add CompileShader overload with custom parameters --- .../Shaders/Translation/ShaderCompiler.cs | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs index 4ecb1c10..0216f60c 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs @@ -84,6 +84,29 @@ internal sealed unsafe partial class ShaderCompiler /// The HLSL source code to compile. /// The bytecode for the compiled shader. public ComPtr CompileShader(ReadOnlySpan source) + { + fixed (char* optimization = "-O3") + fixed (char* rowMajor = "-Zpr") + fixed (char* warningsAsErrors = "-Werror") + { + const string profile = "cs_6_0"; + const string entryPoint = nameof(IComputeShader.Execute); + + ReadOnlySpan arguments = stackalloc IntPtr[] { (IntPtr)optimization, (IntPtr)rowMajor, (IntPtr)warningsAsErrors }; + + return CompileShader(source, profile.AsSpan(), entryPoint.AsSpan(), arguments); + } + } + + /// + /// Compiles a new HLSL shader from the input source code. + /// + /// The HLSL source code to compile. + /// The profile to use for compilation. + /// The entry point for the shader. + /// The arguments to use for compilation (each item must be a * to an individual argument). + /// The bytecode for the compiled shader. + public ComPtr CompileShader(ReadOnlySpan source, ReadOnlySpan profile, ReadOnlySpan entryPoint, ReadOnlySpan arguments) { using ComPtr dxcBlobEncoding = default; using ComPtr dxcOperationResult = default; @@ -99,23 +122,19 @@ internal sealed unsafe partial class ShaderCompiler dxcBlobEncoding.GetAddressOf()).Assert(); } - // Try to compile the new compute shader - fixed (char* shaderName = "") - fixed (char* entryPoint = nameof(IComputeShader.Execute)) - fixed (char* shaderProfile = "cs_6_0") - fixed (char* optimization = "-O3") - fixed (char* rowMajor = "-Zpr") - fixed (char* warningsAsErrors = "-Werror") + // Try to compile the new shader + fixed (char* shaderNamePtr = "") + fixed (char* entryPointPtr = entryPoint) + fixed (char* shaderProfilePtr = profile) + fixed (void* argumentsPtr = arguments) { - char** arguments = stackalloc char*[3] { optimization, rowMajor, warningsAsErrors }; - DxcCompiler.Get()->Compile( (IDxcBlob*)dxcBlobEncoding.Get(), - (ushort*)shaderName, - (ushort*)entryPoint, - (ushort*)shaderProfile, - (ushort**)arguments, - 3, + (ushort*)shaderNamePtr, + (ushort*)entryPointPtr, + (ushort*)shaderProfilePtr, + (ushort**)argumentsPtr, + (uint)arguments.Length, null, 0, DxcIncludeHandler.Get(),