diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj
index a430c4ca..ef124a54 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj
@@ -83,8 +83,8 @@
-
-
+
+
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
index 2c24a603..a9454d6b 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs
@@ -92,7 +92,7 @@ partial class ID2D1ShaderGenerator
token.ThrowIfCancellationRequested();
// Compile the shader bytecode
- using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile(
+ using ComPtr dxcBlobBytecode = D3DCompiler.Compile(
sourceInfo.HlslSource.AsSpan(),
sourceInfo.ShaderProfile.Value,
sourceInfo.IsLinkingSupported);
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs
new file mode 100644
index 00000000..c52b142a
--- /dev/null
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompiler.cs
@@ -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;
+
+///
+/// Provides methods to manually compile D2D1 shaders.
+///
+public static class D2D1ShaderCompiler
+{
+ ///
+ /// Compiles a new HLSL shader from the input source code.
+ ///
+ /// The HLSL source code to compile.
+ /// The entry point of the shader being compiled.
+ /// The shader profile to use to compile the shader.
+ /// The compiler options to use to compile the shader.
+ /// The bytecode for the compiled shader.
+ /// Thrown if the compilation fails.
+ public static ReadOnlyMemory Compile(
+ ReadOnlySpan hlslSource,
+ ReadOnlySpan entryPoint,
+ D2D1ShaderProfile shaderProfile,
+ D2D1ShaderCompilerOptions options)
+ {
+ // Encode the HLSL source to ASCII
+ int maxSourceLength = Encoding.ASCII.GetMaxByteCount(hlslSource.Length);
+ byte[] sourceBuffer = ArrayPool.Shared.Rent(maxSourceLength);
+ int sourceWrittenBytes = Encoding.ASCII.GetBytes(hlslSource, sourceBuffer);
+
+ // Encode the entry point to ASCII
+ int maxEntryPointLength = Encoding.ASCII.GetMaxByteCount(entryPoint.Length);
+ byte[] entryPointBuffer = ArrayPool.Shared.Rent(maxEntryPointLength);
+ int entryPointWrittenBytes = Encoding.ASCII.GetBytes(entryPoint, entryPointBuffer);
+
+ ReadOnlySpan sourceAscii = sourceBuffer.AsSpan(0, sourceWrittenBytes);
+ ReadOnlySpan entryPointAscii = entryPointBuffer.AsSpan(0, entryPointWrittenBytes);
+
+ return Compile(sourceAscii, entryPointAscii, shaderProfile, options);
+ }
+
+ ///
+ /// Compiles a new HLSL shader from the input source code.
+ ///
+ /// The HLSL source code to compile (in ASCII).
+ /// The entry point of the shader being compiled (in ASCII).
+ /// The shader profile to use to compile the shader.
+ /// The compiler options to use to compile the shader.
+ /// The bytecode for the compiled shader.
+ /// Thrown if the compilation fails.
+ public static unsafe ReadOnlyMemory Compile(
+ ReadOnlySpan hlslSourceAscii,
+ ReadOnlySpan entryPointAscii,
+ 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 d3DBlobFullShader = D3DCompiler.CompileShader(
+ source: hlslSourceAscii,
+ macro: D3DCompiler.ASCII.D2D_FULL_SHADER,
+ d2DEntry: entryPointAscii,
+ entryPoint: entryPointAscii,
+ target: D3DCompiler.ASCII.GetPixelShaderProfile(shaderProfile),
+ flags: (uint)options);
+
+ if (!enableLinking)
+ {
+ void* blobFullShaderPtr = d3DBlobFullShader.Get()->GetBufferPointer();
+ nuint blobFullShaderSize = d3DBlobFullShader.Get()->GetBufferSize();
+
+ return new Span(blobFullShaderPtr, (int)blobFullShaderSize).ToArray();
+ }
+
+ // Compile the export function
+ using ComPtr d3DBlobFunction = D3DCompiler.CompileShader(
+ source: hlslSourceAscii,
+ macro: D3DCompiler.ASCII.D2D_FUNCTION,
+ d2DEntry: entryPointAscii,
+ entryPoint: default,
+ target: D3DCompiler.ASCII.GetLibraryProfile(shaderProfile),
+ flags: (uint)options);
+
+ // Embed it as private data if requested
+ using ComPtr d3DBlobLinked = D3DCompiler.SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
+
+ void* blobLinkedPtr = d3DBlobLinked.Get()->GetBufferPointer();
+ nuint blobLinkedSize = d3DBlobLinked.Get()->GetBufferSize();
+
+ return new Span(blobLinkedPtr, (int)blobLinkedSize).ToArray();
+ }
+}
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs
new file mode 100644
index 00000000..a1cacc59
--- /dev/null
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ShaderCompilerOptions.cs
@@ -0,0 +1,172 @@
+using System;
+using TerraFX.Interop.DirectX;
+
+namespace ComputeSharp.D2D1.Interop;
+
+///
+/// Flags to control the options that can be used to compile a D2D1 shader.
+///
+///
+/// For more info, see For more info, see .
+///
+[Flags]
+public enum D2D1ShaderCompilerOptions
+{
+ ///
+ /// Directs the compiler to insert debug file/line/type/symbol information into the output code.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_DEBUG and /Zi.
+ ///
+ Debug = D3DCOMPILE.D3DCOMPILE_DEBUG,
+
+ ///
+ /// 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.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_SKIP_VALIDATION and /Vd.
+ ///
+ SkipValidation = D3DCOMPILE.D3DCOMPILE_SKIP_VALIDATION,
+
+ ///
+ /// Directs the compiler to skip optimization steps during code generation.
+ /// This option should only be used for debug purposes.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_SKIP_OPTIMIZATION and /Od.
+ ///
+ SkipOptimization = D3DCOMPILE.D3DCOMPILE_SKIP_OPTIMIZATION,
+
+ ///
+ /// Directs the compiler to pack matrices in row-major order on input and output from the shader.
+ ///
+ ///
+ /// This value is needed to have the constant buffer loading from generated shaders map to compiled shaders.
+ /// This flag maps to D3DCOMPILE_PACK_MATRIX_ROW_MAJOR and /Zpr.
+ ///
+ PackMatrixRowMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR,
+
+ ///
+ /// 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.
+ ///
+ ///
+ /// Using this flag makes shaders incompatible with the constant buffer loading code produced by ComputeSharp.D2D1.
+ /// That is, trying to load a constant buffer from a shader processed by the source generator is undefined behavior.
+ /// This flag maps to D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR and /Zpc.
+ ///
+ PackMatrixColumnMajor = D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR,
+
+ ///
+ /// Directs the compiler to perform all computations with partial precision.
+ /// If this flag is set, the compiled code might run faster on some hardware.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_PARTIAL_PRECISION and /Gpp.
+ ///
+ PartialPrecision = D3DCOMPILE.D3DCOMPILE_PARTIAL_PRECISION,
+
+ ///
+ /// Directs the compiler to not use flow-control constructs where possible.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_AVOID_FLOW_CONTROL and /Gfa.
+ ///
+ AvoidFlowControl = D3DCOMPILE.D3DCOMPILE_AVOID_FLOW_CONTROL,
+
+ ///
+ /// Forces strict compile, which might not allow for legacy syntax.
+ /// By default, the compiler disables strictness on deprecated syntax.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_ENABLE_STRICTNESS and /Ges.
+ ///
+ EnableStrictness = D3DCOMPILE.D3DCOMPILE_ENABLE_STRICTNESS,
+
+ ///
+ /// Forces the IEEE strict compile which avoids optimizations that may break IEEE rules.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_IEEE_STRICTNESS and /Gis.
+ ///
+ IeeeStrictness = D3DCOMPILE.D3DCOMPILE_IEEE_STRICTNESS,
+
+ ///
+ /// Directs the compiler to enable older shaders to compile to 5_0 targets.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY and /Gec.
+ ///
+ EnableBackwardsCompatibility = D3DCOMPILE.D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY,
+
+ ///
+ /// 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.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL0 and /O0.
+ ///
+ OptimizationLevel0 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL0,
+
+ ///
+ /// Directs the compiler to use the second lowest optimization level.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL1 and /O1.
+ ///
+ OptimizationLevel1 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL1,
+
+ ///
+ /// Directs the compiler to use the second highest optimization level.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL2 and /O2.
+ ///
+ OptimizationLevel2 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL2,
+
+ ///
+ /// 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.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_OPTIMIZATION_LEVEL3 and /O3.
+ ///
+ OptimizationLevel3 = D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3,
+
+ ///
+ /// 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.
+ ///
+ ///
+ /// This flag maps to D3DCOMPILE_WARNINGS_ARE_ERRORS and /WX.
+ ///
+ WarningsAreErrors = D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS,
+
+ ///
+ /// This flag enables the support for shader linking. Specifically, when set, this flag causes the APIs from
+ /// 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.
+ ///
+ ///
+ /// For more info, see .
+ ///
+ EnableLinking = 1 << 31,
+
+ ///
+ /// The default options for shaders compiled by ComputeSharp.D2D1. Specifically, this combination
+ /// of options is the one used by precompiled shaders (ie. when using ),
+ /// and when using or any of the overloads.
+ ///
+ ///
+ /// This option does not include , 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 instance, should be manually used when needed.
+ ///
+ Default = OptimizationLevel3 | WarningsAreErrors | PackMatrixRowMajor
+}
diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs
similarity index 99%
rename from src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs
rename to src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs
index 7e59f3ab..23455d02 100644
--- a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.ID3DInclude.cs
@@ -14,7 +14,7 @@ using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.In
namespace ComputeSharp.D2D1.Shaders.Translation;
///
-partial class D2D1ShaderCompiler
+partial class D3DCompiler
{
///
/// A custom fallback implementation to use on systems with no support for it.
diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
similarity index 69%
rename from src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs
rename to src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
index 9b879275..e9d9617d 100644
--- a/src/ComputeSharp.D2D1/Shaders/Translation/D2D1ShaderCompiler.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Translation/D3DCompiler.cs
@@ -20,7 +20,7 @@ namespace ComputeSharp.D2D1.Shaders.Translation;
///
/// A that uses the FXC APIs to compile D2D1 pixel shaders.
///
-internal static unsafe partial class D2D1ShaderCompiler
+internal static unsafe partial class D3DCompiler
{
///
/// The instance to load d2d1effecthelpers.hlsli.
@@ -32,81 +32,115 @@ internal static unsafe partial class D2D1ShaderCompiler
///
/// The HLSL source code to compile.
/// The shader profile to use to compile the shader.
- /// Whether to enable linking support for the shader.
+ /// Whether to enable linking for the shader.
/// The bytecode for the compiled shader.
- public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, bool enableLinkingSupport)
+ public static ComPtr Compile(ReadOnlySpan source, D2D1ShaderProfile shaderProfile, bool enableLinking)
{
- // Compile the standalone D2D1 full shader
- using ComPtr d3DBlobFullShader = CompileD2DFullShader(source, shaderProfile);
-
- if (!enableLinkingSupport)
- {
- return d3DBlobFullShader.Move();
- }
-
- // Compile the export function and embed it as private data if requested
- using ComPtr d3DBlobFunction = CompileD2DFunction(source, shaderProfile);
- using ComPtr d3DBlobLinked = EmbedD2DFunctionPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
-
- return d3DBlobLinked.Move();
- }
-
- ///
- /// Compiles a D2D1 pixel shader with D2D_FULL_SHADER.
- ///
- /// The HLSL source code to compile.
- /// The shader profile to use to compile the shader.
- /// The bytecode for the compiled shader.
- private static ComPtr CompileD2DFullShader(ReadOnlySpan source, D2D1ShaderProfile shaderProfile)
- {
- // Encode the HLSL source to ASCII
int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);
byte[] buffer = ArrayPool.Shared.Rent(maxLength);
int writtenBytes = Encoding.ASCII.GetBytes(source, buffer);
+ try
+ {
+ // Compile the standalone D2D1 full shader
+ using ComPtr d3DBlobFullShader = CompileShader(
+ source: buffer.AsSpan(0, writtenBytes),
+ macro: ASCII.D2D_FULL_SHADER,
+ d2DEntry: ASCII.Execute,
+ entryPoint: ASCII.Execute,
+ target: ASCII.GetPixelShaderProfile(shaderProfile),
+ flags: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR);
+
+ if (!enableLinking)
+ {
+ return d3DBlobFullShader.Move();
+ }
+
+ // Compile the export function
+ using ComPtr d3DBlobFunction = CompileShader(
+ source: buffer.AsSpan(0, writtenBytes),
+ macro: ASCII.D2D_FUNCTION,
+ d2DEntry: ASCII.Execute,
+ entryPoint: default,
+ target: ASCII.GetLibraryProfile(shaderProfile),
+ flags: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR);
+
+ // Embed it as private data if requested
+ using ComPtr d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
+
+ return d3DBlobLinked.Move();
+ }
+ finally
+ {
+ ArrayPool.Shared.Return(buffer);
+ }
+ }
+
+ ///
+ /// Compiles a D2D1 pixel shader with the specified parameters.
+ ///
+ /// The HLSL source code to compile (in ASCII).
+ /// The target macro to define for the shader (either D2D_FULL_SHADER or D2D_FUNCTION).
+ /// The D2D entry to specify when compiling the shader.
+ /// The entry point for the shader.
+ /// The shader target to use to compile the input source.
+ /// The compilationm flag to use.
+ /// The bytecode for the compiled shader.
+ /// Thrown if the compilation fails.
+ public static ComPtr CompileShader(
+ ReadOnlySpan source,
+ ReadOnlySpan macro,
+ ReadOnlySpan d2DEntry,
+ ReadOnlySpan entryPoint,
+ ReadOnlySpan target,
+ uint flags)
+ {
using ComPtr d3DBlobBytecode = default;
using ComPtr d3DBlobErrors = default;
int hResult;
- fixed (byte* bufferPtr = buffer)
+ fixed (byte* sourcePtr = source)
+ fixed (byte* macroPtr = macro)
+ fixed (byte* d2DEntryPtr = d2DEntry)
+ fixed (byte* entryPointPtr = entryPoint)
+ fixed (byte* targetPtr = target)
{
// Prepare the macros for full shader compilation:
//
- // -D D2D_FULL_SHADER
- // -D D2D_ENTRY=Execute
+ // -D
+ // -D D2D_ENTRY=
+ //
D3D_SHADER_MACRO* macros = stackalloc D3D_SHADER_MACRO[]
{
new()
{
- Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_FULL_SHADER)),
+ Name = (sbyte*)macroPtr,
Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.NullTerminator))
},
new()
{
Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_ENTRY)),
- Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute))
+ Definition = (sbyte*)d2DEntryPtr
},
new()
};
- // Compile the shader with -ps_5_0 -O3 -We -Zpr
+ // Compile the shader
hResult = DirectX.D3DCompile(
- pSrcData: bufferPtr,
- SrcDataSize: (nuint)writtenBytes,
+ pSrcData: sourcePtr,
+ SrcDataSize: (nuint)source.Length,
pSourceName: null,
pDefines: macros,
pInclude: D3DIncludeForD2D1EffectHelpers,
- pEntrypoint: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute)),
- pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.GetPixelShaderProfile(shaderProfile))),
- Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR,
+ pEntrypoint: (sbyte*)entryPointPtr,
+ pTarget: (sbyte*)targetPtr,
+ Flags1: flags,
Flags2: 0,
ppCode: d3DBlobBytecode.GetAddressOf(),
ppErrorMsgs: d3DBlobErrors.GetAddressOf());
}
- ArrayPool.Shared.Return(buffer);
-
// Throw if an error was retrieved, then also double check the HRESULT
if (d3DBlobErrors.Get() is not null)
{
@@ -118,79 +152,13 @@ internal static unsafe partial class D2D1ShaderCompiler
return d3DBlobBytecode.Move();
}
- ///
- /// Compiles a D2D1 pixel shader with D2D_FUNCTION.
- ///
- /// The HLSL source code to compile.
- /// The shader profile to use to compile the shader.
- /// The bytecode for the compiled shader.
- private static ComPtr CompileD2DFunction(ReadOnlySpan source, D2D1ShaderProfile shaderProfile)
- {
- int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);
- byte[] buffer = ArrayPool.Shared.Rent(maxLength);
- int writtenBytes = Encoding.ASCII.GetBytes(source, buffer);
-
- using ComPtr d3DBlobBytecode = default;
- using ComPtr d3DBlobErrors = default;
-
- int hResult;
-
- fixed (byte* bufferPtr = buffer)
- {
- // Prepare the macros for full shader compilation:
- //
- // -D D2D_FUNCTION
- // -D D2D_ENTRY=Execute
- D3D_SHADER_MACRO* macros = stackalloc D3D_SHADER_MACRO[]
- {
- new()
- {
- Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_FUNCTION)),
- Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.NullTerminator))
- },
- new()
- {
- Name = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_ENTRY)),
- Definition = (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute))
- },
- new()
- };
-
- // Compile the shader with -lib_5_0 -O3 -We -Zpr
- hResult = DirectX.D3DCompile(
- pSrcData: bufferPtr,
- SrcDataSize: (nuint)writtenBytes,
- pSourceName: null,
- pDefines: macros,
- pInclude: D3DIncludeForD2D1EffectHelpers,
- pEntrypoint: null,
- pTarget: (sbyte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.GetLibraryProfile(shaderProfile))),
- Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR,
- Flags2: 0,
- ppCode: d3DBlobBytecode.GetAddressOf(),
- ppErrorMsgs: d3DBlobErrors.GetAddressOf());
- }
-
- ArrayPool.Shared.Return(buffer);
-
- // Check for errors and double check like above
- if (d3DBlobErrors.Get() is not null)
- {
- ThrowHslsCompilationException(d3DBlobErrors.Get());
- }
-
- hResult.Assert();
-
- return d3DBlobBytecode.Move();
- }
-
///
/// Embeds the bytecode for an exported shader as private data into another shader bytecode.
///
- /// The bytecode produced by .
- /// The bytecode produced by .
+ /// The bytecode for the full shader.
+ /// The bytecode for the shader function to export.
/// An instance with the combined data of and .
- private static ComPtr EmbedD2DFunctionPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob)
+ public static ComPtr SetD3DPrivateData(ID3DBlob* shaderBlob, ID3DBlob* exportBlob)
{
void* shaderPtr = shaderBlob->GetBufferPointer();
nuint shaderSize = shaderBlob->GetBufferSize();
@@ -241,7 +209,7 @@ internal static unsafe partial class D2D1ShaderCompiler
///
/// A container for ASCII encoded, null-terminated constant strings.
///
- private static class ASCII
+ public static class ASCII
{
///
/// Gets a with the "D2D_FUNCTION" ASCII text.
diff --git a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
index 47867a3a..deae7cd7 100644
--- a/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Translation/__Internals/D2D1ShaderCompiler.cs
@@ -28,7 +28,7 @@ public static class D2D1ShaderCompiler
{
Unsafe.AsRef(in shader).BuildHlslSource(out string hlslSource);
- using ComPtr d3DBlobBytecode = Shaders.Translation.D2D1ShaderCompiler.Compile(
+ using ComPtr d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile(
hlslSource.AsSpan(),
shaderProfile ?? D2D1ShaderProfile.PixelShader50,
enableLinkingSupport);
diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs
new file mode 100644
index 00000000..06c208bf
--- /dev/null
+++ b/tests/ComputeSharp.D2D1.Tests/D2D1ShaderCompilerTests.cs
@@ -0,0 +1,165 @@
+using System;
+using ComputeSharp.D2D1.Exceptions;
+using ComputeSharp.D2D1.Interop;
+using ComputeSharp.D2D1.Tests.Effects;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace ComputeSharp.D2D1.Tests;
+
+[TestClass]
+[TestCategory("D2D1ShaderCompilerTests")]
+public partial class D2D1ShaderCompilerTests
+{
+ [TestMethod]
+ public unsafe void CompileInvertEffectWithDefaultOptions()
+ {
+ const string source = @"
+ #define D2D_INPUT_COUNT 1
+ #define D2D_INPUT0_SIMPLE
+
+ #include ""d2d1effecthelpers.hlsli""
+
+
+ D2D_PS_ENTRY(PSMain)
+ {
+ float4 color = D2DGetInput(0);
+ float3 rgb = saturate(1.0 - color.rgb);
+ return float4(rgb, 1);
+ }";
+
+ ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile(
+ source,
+ "PSMain",
+ D2D1ShaderProfile.PixelShader40Level93,
+ D2D1ShaderCompilerOptions.Default);
+
+ Assert.IsTrue(bytecode.Length > 0);
+ }
+
+ [TestMethod]
+ public unsafe void CompileInvertEffectWithDefaultOptionsAndLinking()
+ {
+ const string source = @"
+ #define D2D_INPUT_COUNT 1
+ #define D2D_INPUT0_SIMPLE
+
+ #include ""d2d1effecthelpers.hlsli""
+
+
+ D2D_PS_ENTRY(PSMain)
+ {
+ float4 color = D2DGetInput(0);
+ float3 rgb = saturate(1.0 - color.rgb);
+ return float4(rgb, 1);
+ }";
+
+ ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile(
+ source,
+ "PSMain",
+ D2D1ShaderProfile.PixelShader40Level93,
+ D2D1ShaderCompilerOptions.Default | D2D1ShaderCompilerOptions.EnableLinking);
+
+ Assert.IsTrue(bytecode.Length > 0);
+ }
+
+ [TestMethod]
+ [ExpectedException(typeof(FxcCompilationException))]
+ public unsafe void CompileInvertEffectWithInvalidEntryPoint()
+ {
+ const string source = @"
+ #define D2D_INPUT_COUNT 1
+ #define D2D_INPUT0_SIMPLE
+
+ #include ""d2d1effecthelpers.hlsli""
+
+
+ D2D_PS_ENTRY(PSMain)
+ {
+ float4 color = D2DGetInput(0);
+ float3 rgb = saturate(1.0 - color.rgb);
+ return float4(rgb, 1);
+ }";
+
+ ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile(
+ source,
+ "Execute",
+ D2D1ShaderProfile.PixelShader40Level93,
+ D2D1ShaderCompilerOptions.Default);
+
+ Assert.IsTrue(bytecode.Length > 0);
+ }
+
+ [TestMethod]
+ [ExpectedException(typeof(FxcCompilationException))]
+ public unsafe void CompileInvertEffectWithErrors()
+ {
+ const string source = @"
+ #define D2D_INPUT_COUNT 1
+ #define D2D_INPUT0_SIMPLE
+
+ #include ""d2d1effecthelpers.hlsli""
+
+
+ D2D_PS_ENTRY(PSMain)
+ {
+ float4 color = D2DGetInput(99);
+ float3 rgb2 = saturate(1.0 - color.rgb);
+ return float4(rgb, 1, 4.0);
+ }";
+
+ ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile(
+ source,
+ "PSMain",
+ D2D1ShaderProfile.PixelShader40Level93,
+ D2D1ShaderCompilerOptions.Default);
+
+ Assert.IsTrue(bytecode.Length > 0);
+ }
+
+ [TestMethod]
+ public unsafe void CompilePixelateEffectWithDefaultOptions()
+ {
+ const string source = @"
+ #define D2D_INPUT_COUNT 1
+ #define D2D_INPUT0_COMPLEX
+ #define D2D_REQUIRES_SCENE_POSITION
+
+ #include ""d2d1effecthelpers.hlsli""
+
+ struct ComputeSharp_D2D1_Tests_Effects_PixelateEffect_Shader_Constants
+ {
+ int inputWidth;
+ int inputHeight;
+ int cellSize;
+ };
+
+ ComputeSharp_D2D1_Tests_Effects_PixelateEffect_Shader_Constants constants;
+
+ D2D_PS_ENTRY(PSMain)
+ {
+ float2 scenePos = D2DGetScenePosition().xy;
+ uint x = (uint)floor(scenePos.x);
+ uint y = (uint)floor(scenePos.y);
+ int cellX = (int)floor(x / constants.cellSize);
+ int cellY = (int)floor(y / constants.cellSize);
+ int x0 = cellX * constants.cellSize;
+ int y0 = cellY * constants.cellSize;
+ int x1 = min(constants.inputWidth, x0 + constants.cellSize) - 1;
+ int y1 = min(constants.inputHeight, y0 + constants.cellSize) - 1;
+ float4 sample0 = D2DSampleInputAtPosition(0, int2(x0, y0));
+ float4 sample1 = D2DSampleInputAtPosition(0, int2(x1, y0));
+ float4 sample2 = D2DSampleInputAtPosition(0, int2(x0, y1));
+ float4 sample3 = D2DSampleInputAtPosition(0, int2(x1, y1));
+ float4 color = (sample0 + sample1 + sample2 + sample3) / 4;
+ return color;
+ }";
+
+ ReadOnlyMemory bytecode = D2D1ShaderCompiler.Compile(
+ source,
+ "PSMain",
+ D2D1ShaderProfile.PixelShader40,
+ D2D1ShaderCompilerOptions.Default);
+
+ Assert.IsTrue(bytecode.Length > 0);
+ }
+}
\ No newline at end of file