Add public D2D1ShaderCompiler API

このコミットが含まれているのは:
Sergio Pedri committed 2022-05-21 01:40:31 +02:00
1 親 ffcfcb3471
コミット e7bdf19bf2
3ファイル変更
+282 -5

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

+105
ファイルの表示
@@ -0,0 +1,105 @@
using System;
using System.Buffers;
using System.Text;
using ComputeSharp.D2D1.Exceptions;
#if !NET6_0_OR_GREATER
using ComputeSharp.D2D1.NetStandard.System.Text;
#endif
using ComputeSharp.D2D1.Shaders.Translation;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.D2D1.Interop;
/// <summary>
/// Provides methods to manually compile D2D1 shaders.
/// </summary>
public static class D2D1ShaderCompiler
{
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <param name="source">The HLSL source code to compile.</param>
/// <param name="entryPoint">The entry point of the shader being compiled.</param>
/// <param name="shaderProfile">The shader profile to use to compile the shader.</param>
/// <param name="options">The compiler options to use to compile the shader.</param>
/// <returns>The bytecode for the compiled shader.</returns>
/// <exception cref="FxcCompilationException">Thrown if the compilation fails.</exception>
public static ReadOnlyMemory<byte> Compile(
ReadOnlySpan<char> source,
ReadOnlySpan<char> entryPoint,
D2D1ShaderProfile shaderProfile,
D2D1ShaderCompilerOptions options)
{
// Encode the HLSL source to ASCII
int maxSourceLength = Encoding.ASCII.GetMaxByteCount(source.Length);
byte[] sourceBuffer = ArrayPool<byte>.Shared.Rent(maxSourceLength);
int sourceWrittenBytes = Encoding.ASCII.GetBytes(source, sourceBuffer);
// Encode the entry point to ASCII
int maxEntryPointLength = Encoding.ASCII.GetMaxByteCount(entryPoint.Length);
byte[] entryPointBuffer = ArrayPool<byte>.Shared.Rent(maxEntryPointLength);
int entryPointWrittenBytes = Encoding.ASCII.GetBytes(entryPoint, entryPointBuffer);
ReadOnlySpan<byte> sourceAscii = sourceBuffer.AsSpan(0, sourceWrittenBytes);
ReadOnlySpan<byte> entryPointAscii = entryPointBuffer.AsSpan(0, entryPointWrittenBytes);
return Compile(sourceAscii, entryPointAscii, shaderProfile, options);
}
/// <summary>
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <param name="source">The HLSL source code to compile (in ASCII).</param>
/// <param name="entryPoint">The entry point of the shader being compiled (in ASCII).</param>
/// <param name="shaderProfile">The shader profile to use to compile the shader.</param>
/// <param name="options">The compiler options to use to compile the shader.</param>
/// <returns>The bytecode for the compiled shader.</returns>
/// <exception cref="FxcCompilationException">Thrown if the compilation fails.</exception>
public static unsafe ReadOnlyMemory<byte> Compile(
ReadOnlySpan<byte> source,
ReadOnlySpan<byte> entryPoint,
D2D1ShaderProfile shaderProfile,
D2D1ShaderCompilerOptions options)
{
// Check linking support
bool enableLinking = (options & D2D1ShaderCompilerOptions.EnableLinking) == D2D1ShaderCompilerOptions.EnableLinking;
// Remove the linking flag to make the options blittable to flags
options &= ~D2D1ShaderCompilerOptions.EnableLinking;
// Compile the standalone D2D1 full shader
using ComPtr<ID3DBlob> d3DBlobFullShader = D3DCompiler.CompileShader(
source: source,
macro: D3DCompiler.ASCII.D2D_FULL_SHADER,
d2DEntry: entryPoint,
entryPoint: entryPoint,
target: D3DCompiler.ASCII.GetPixelShaderProfile(shaderProfile),
flags: (uint)options);
if (!enableLinking)
{
void* blobFullShaderPtr = d3DBlobFullShader.Get()->GetBufferPointer();
nuint blobFullShaderSize = d3DBlobFullShader.Get()->GetBufferSize();
return new Span<byte>(blobFullShaderPtr, (int)blobFullShaderSize).ToArray();
}
// Compile the export function
using ComPtr<ID3DBlob> d3DBlobFunction = D3DCompiler.CompileShader(
source: source,
macro: D3DCompiler.ASCII.D2D_FUNCTION,
d2DEntry: entryPoint,
entryPoint: default,
target: D3DCompiler.ASCII.GetLibraryProfile(shaderProfile),
flags: (uint)options);
// Embed it as private data if requested
using ComPtr<ID3DBlob> d3DBlobLinked = D3DCompiler.SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
void* blobLinkedPtr = d3DBlobLinked.Get()->GetBufferPointer();
nuint blobLinkedSize = d3DBlobLinked.Get()->GetBufferSize();
return new Span<byte>(blobLinkedPtr, (int)blobLinkedSize).ToArray();
}
}
+172
ファイルの表示
@@ -0,0 +1,172 @@
using System;
using TerraFX.Interop.DirectX;
namespace ComputeSharp.D2D1.Interop;
/// <summary>
/// Flags to control the options that can be used to compile a D2D1 shader.
/// </summary>
/// <remarks>
/// For more info, see For more info, see <see href="https://docs.microsoft.com/windows/win32/direct3dhlsl/d3dcompile-constants"/>.
/// </remarks>
[Flags]
public enum D2D1ShaderCompilerOptions
{
/// <summary>
/// Directs the compiler to insert debug file/line/type/symbol information into the output code.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_DEBUG</c> and <c>/Zi</c>.
/// </remarks>
Debug = D3DCOMPILE.D3DCOMPILE_DEBUG,
/// <summary>
/// Directs the compiler not to validate the generated code against known capabilities and constraints.
/// This option should only be used with shaders that have been successfully compiled in the past.
/// DirectX always validates shaders before it sets them to a device.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_SKIP_VALIDATION</c> and <c>/Vd</c>.
/// </remarks>
SkipValidation = D3DCOMPILE.D3DCOMPILE_SKIP_VALIDATION,
/// <summary>
/// Directs the compiler to skip optimization steps during code generation.
/// This option should only be used for debug purposes.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_SKIP_OPTIMIZATION</c> and <c>/Od</c>.
/// </remarks>
SkipOptimization = D3DCOMPILE.D3DCOMPILE_SKIP_OPTIMIZATION,
/// <summary>
/// Directs the compiler to pack matrices in row-major order on input and output from the shader.
/// </summary>
/// <remarks>
/// <para>This value is needed to have the constant buffer loading from generated shaders map to compiled shaders.</para>
/// <para>This flag maps to <c>D3DCOMPILE_PACK_MATRIX_ROW_MAJOR</c> and <c>/Zpr</c>.</para>
/// </remarks>
PackMatrixRowMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR,
/// <summary>
/// Directs the compiler to pack matrices in column-major order on input and output from the shader.
/// This type of packing is generally more efficient because a series of dot-products can then
/// perform vector-matrix multiplication.
/// </summary>
/// <remarks>
/// <para>Using this flag makes shaders incompatible with the constant buffer loading code produced by ComputeSharp.D2D1.</para>
/// <para>That is, trying to load a constant buffer from a shader processed by the source generator is undefined behavior.</para>
/// <para>This flag maps to <c>D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR</c> and <c>/Zpc</c>.</para>
/// </remarks>
PackMatrixColumnMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR,
/// <summary>
/// Directs the compiler to perform all computations with partial precision.
/// If this flag is set, the compiled code might run faster on some hardware.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_PARTIAL_PRECISION</c> and <c>/Gpp</c>.
/// </remarks>
PartialPrecision = D3DCOMPILE.D3DCOMPILE_PARTIAL_PRECISION,
/// <summary>
/// Directs the compiler to not use flow-control constructs where possible.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_AVOID_FLOW_CONTROL</c> and <c>/Gfa</c>.
/// </remarks>
AvoidFlowControl = D3DCOMPILE.D3DCOMPILE_AVOID_FLOW_CONTROL,
/// <summary>
/// Forces strict compile, which might not allow for legacy syntax.
/// By default, the compiler disables strictness on deprecated syntax.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_ENABLE_STRICTNESS</c> and <c>/Ges</c>.
/// </remarks>
EnableStrictness = D3DCOMPILE.D3DCOMPILE_ENABLE_STRICTNESS,
/// <summary>
/// Forces the IEEE strict compile which avoids optimizations that may break IEEE rules.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_IEEE_STRICTNESS</c> and <c>/Gis</c>.
/// </remarks>
IeeeStrictness = D3DCOMPILE.D3DCOMPILE_IEEE_STRICTNESS,
/// <summary>
/// Directs the compiler to enable older shaders to compile to 5_0 targets.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY</c> and <c>/Gec</c>.
/// </remarks>
EnableBackwardsCompatibility = D3DCOMPILE.D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY,
/// <summary>
/// Directs the compiler to use the lowest optimization level. If this option is set, the compiler
/// might produce slower code but produces the code more quickly. This option should be set when
/// shaders are developed iteratively, as it can reduce compilation times.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_OPTIMIZATION_LEVEL0</c> and <c>/O0</c>.
/// </remarks>
OptimizationLevel0 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL0,
/// <summary>
/// Directs the compiler to use the second lowest optimization level.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_OPTIMIZATION_LEVEL1</c> and <c>/O1</c>.
/// </remarks>
OptimizationLevel1 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL1,
/// <summary>
/// Directs the compiler to use the second highest optimization level.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_OPTIMIZATION_LEVEL2</c> and <c>/O2</c>.
/// </remarks>
OptimizationLevel2 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL2,
/// <summary>
/// Directs the compiler to use the highest optimization level. If this option is set, the compiler
/// produces the best possible code but might take significantly longer to do so. This option should
/// be set for final builds of an application when performance is the most important factor.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_OPTIMIZATION_LEVEL3</c> and <c>/O3</c>.
/// </remarks>
OptimizationLevel3 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3,
/// <summary>
/// Directs the compiler to treat all warnings as errors when it compiles the shader code. This
/// option is recommended for new shader code, so that warnings can be resolved and the
/// number of hard-to-find code defects can be minimized.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILE_WARNINGS_ARE_ERRORS</c> and <c>/WX</c>.
/// </remarks>
WarningsAreErrors = D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS,
/// <summary>
/// This flag enables the support for shader linking. Specifically, when set, this flag causes the APIs from
/// <see cref="D2D1ShaderCompiler"/> to compile the input shader twice: once as a full D2D1 shader, and once
/// as an export function, which is then set as private blob data into the final bytecode that is returned.
/// </summary>
/// <remarks>
/// For more info, see <see href="https://docs.microsoft.com/en-us/windows/win32/direct2d/effect-shader-linking"/>.
/// </remarks>
EnableLinking = 1 << 31,
/// <summary>
/// The default options for shaders compiled by ComputeSharp.D2D1. Specifically, this combination
/// of options is the one used by precompiled shaders (ie. when using <see cref="D2DEmbeddedBytecodeAttribute"/>),
/// and when using <see cref="D2D1PixelShader.LoadBytecode{T}()"/> or any of the overloads.
/// </summary>
/// <remarks>
/// This option does not include <see cref="EnableLinking"/>, which is instead automatically enabled by shaders
/// compiled using any of the APIs mentioned above. When manually compiling shaders from source using the APIs
/// from <see cref="D2D1ShaderCompiler"/> instance, <see cref="EnableLinking"/> should be manually used when needed.
/// </remarks>
Default = OptimizationLevel3 | WarningsAreErrors | PackMatrixRowMajor
}
+5 -5
ファイルの表示
@@ -32,9 +32,9 @@ internal static unsafe partial class D3DCompiler
/// </summary>
/// <param name="source">The HLSL source code to compile.</param>
/// <param name="shaderProfile">The shader profile to use to compile the shader.</param>
/// <param name="enableLinkingSupport">Whether to enable linking support for the shader.</param>
/// <param name="enableLinking">Whether to enable linking for the shader.</param>
/// <returns>The bytecode for the compiled shader.</returns>
public static ComPtr<ID3DBlob> Compile(ReadOnlySpan<char> source, D2D1ShaderProfile shaderProfile, bool enableLinkingSupport)
public static ComPtr<ID3DBlob> Compile(ReadOnlySpan<char> source, D2D1ShaderProfile shaderProfile, bool enableLinking)
{
int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);
byte[] buffer = ArrayPool<byte>.Shared.Rent(maxLength);
@@ -51,7 +51,7 @@ internal static unsafe partial class D3DCompiler
target: ASCII.GetPixelShaderProfile(shaderProfile),
flags: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR);
if (!enableLinkingSupport)
if (!enableLinking)
{
return d3DBlobFullShader.Move();
}
@@ -158,7 +158,7 @@ internal static unsafe partial class D3DCompiler
/// <param name="shaderBlob">The bytecode for the full shader.</param>
/// <param name="exportBlob">The bytecode for the shader function to export.</param>
/// <returns>An <see cref="ID3DBlob"/> instance with the combined data of <paramref name="shaderBlob"/> and <paramref name="exportBlob"/>.</returns>
private static ComPtr<ID3DBlob> SetD3DPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob)
public static ComPtr<ID3DBlob> SetD3DPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob)
{
void* shaderPtr = shaderBlob->GetBufferPointer();
nuint shaderSize = shaderBlob->GetBufferSize();
@@ -209,7 +209,7 @@ internal static unsafe partial class D3DCompiler
/// <summary>
/// A container for ASCII encoded, null-terminated constant strings.
/// </summary>
private static class ASCII
public static class ASCII
{
/// <summary>
/// Gets a <see cref="ReadOnlySpan{T}"/> with the <c>"D2D_FUNCTION"</c> ASCII text.