ShaderCompiler class refactored

このコミットが含まれているのは:
Sergio Pedri committed 2019-08-11 23:47:32 +02:00
1 親 0a71c0a30b
コミット 1d9b0a94e7
3ファイル変更
+61 -33

検索条件に一致するファイルはありません

+2 -1
ファイルの表示
@@ -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);
+57 -30
ファイルの表示
@@ -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
/// <summary>
/// A <see langword="class"/> that uses the <see cref="HlslDxcLib"/> APIs to compile compute shaders
/// </summary>
public sealed class ShaderCompiler
{
static ShaderCompiler()
/// <summary>
/// Gets the singleton <see cref="ShaderCompiler"/> instance to use
/// </summary>
public static ShaderCompiler Instance { get; } = new ShaderCompiler();
/// <summary>
/// The <see cref="IDxcCompiler"/> instance to use to create new HLSL shaders
/// </summary>
private readonly IDxcCompiler Compiler;
/// <summary>
/// The <see cref="IDxcLibrary"/> instance to use to create the bytecode for HLSL sources
/// </summary>
private readonly IDxcLibrary Library;
/// <summary>
/// Creates a new <see cref="ShaderCompiler"/> instance
/// </summary>
private ShaderCompiler()
{
HlslDxcLib.DxcCreateInstanceFn = DefaultDxcLib.GetDxcCreateInstanceFn();
Compiler = HlslDxcLib.CreateDxcCompiler();
Library = HlslDxcLib.CreateDxcLibrary();
}
public static byte[] CompileShaderFile(string filePath, ShaderVersion? version = null, string? entryPoint = null)
/// <summary>
/// Compiles a new HLSL shader from the input source code
/// </summary>
/// <param name="source">The HLSL source code to compile</param>
/// <returns>The bytecode for the compiled shader</returns>
[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)
/// <summary>
/// Reads the buffer content from an input <see cref="IDxcBlob"/> instance
/// </summary>
/// <param name="blob">The input <see cref="IDxcBlob"/> instance to read data from</param>
/// <returns>The read bytes buffer, as an array</returns>
[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<byte>(pMem, (int)size).CopyTo(result.AsSpan());
return result;
}
public static unsafe string GetStringFromBlob(IDxcLibrary library, IDxcBlob blob)
/// <summary>
/// Reads a <see cref="string"/> value from an input <see cref="IDxcBlob"/> instance
/// </summary>
/// <param name="library">The source <see cref="IDxcLibrary"/> to use to load the data</param>
/// <param name="blob">The input <see cref="IDxcBlob"/> instance to read data from</param>
/// <returns>The resulting <see cref="string"/> read from the input data buffer</returns>
[Pure]
private static unsafe string GetStringFromBlob(IDxcLibrary library, IDxcBlob blob)
{
blob = library.GetBlobAstUf16(blob);
return new string(blob.GetBufferPointer(), 0, (int)(blob.GetBufferSize() / 2));
+2 -2
ファイルの表示
@@ -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 <Main>b__0", "void Foo");
shaderSource = shaderSource.Replace("internal void <Main>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.