Merge pull request #707 from Sergio0694/dev/d2d-strip-reflection

Add D2D1CompileOptions.StripReflectionData flag
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-12-08 02:04:38 +01:00
コミット 2993efecb2
16ファイル変更
+210 -40

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

+2
ファイルの表示
@@ -1,5 +1,6 @@
D3DCompile
D3DSetBlobPart
D3DStripShader
D2D1_BUFFER_PRECISION
D2D1_CHANNEL_DEPTH
D2D1_FILTER
@@ -19,6 +20,7 @@ D3DCOMPILE_OPTIMIZATION_LEVEL1
D3DCOMPILE_OPTIMIZATION_LEVEL2
D3DCOMPILE_OPTIMIZATION_LEVEL3
D3DCOMPILE_WARNINGS_ARE_ERRORS
D3DCOMPILER_STRIP_FLAGS
IUnknown
S_OK
S_FALSE
+10
ファイルの表示
@@ -155,6 +155,16 @@ public enum D2D1CompileOptions
/// </remarks>
WarningsAreErrors = (int)D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS,
/// <summary>
/// Strips the reflection data from the generated shader bytecode. The bytecode size will be smaller, but trying
/// to perform reflection on the shader will return inaccurate results. It is recommend if not reflection is used.
/// </summary>
/// <remarks>
/// This flag maps to <c>D3DCOMPILER_STRIP_REFLECTION_DATA</c>, when passed as input to <c>D3DStripShader</c>.
/// For more info, see <see href="https://learn.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dstripshader"/>.
/// </remarks>
StripReflectionData = 1 << 30,
/// <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
+8 -4
ファイルの表示
@@ -65,11 +65,13 @@ public static class D2D1ShaderCompiler
D2D1ShaderProfile shaderProfile,
D2D1CompileOptions options)
{
// Check linking support
// Check the additional compile options (not provided by FXC directly)
bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking;
bool stripReflectionData = (options & D2D1CompileOptions.StripReflectionData) == D2D1CompileOptions.StripReflectionData;
// Remove the linking flag to make the options blittable to flags
// Remove the additional options to make them blittable to flags
options &= ~D2D1CompileOptions.EnableLinking;
options &= ~D2D1CompileOptions.StripReflectionData;
// Compile the standalone D2D1 full shader
using ComPtr<ID3DBlob> d3DBlobFullShader = D3DCompiler.Compile(
@@ -78,7 +80,8 @@ public static class D2D1ShaderCompiler
d2DEntry: entryPointAscii,
entryPoint: entryPointAscii,
target: ASCII.GetPixelShaderProfile(shaderProfile),
flags: (uint)options);
flags: (uint)options,
stripReflectionData: stripReflectionData);
if (!enableLinking)
{
@@ -95,7 +98,8 @@ public static class D2D1ShaderCompiler
d2DEntry: entryPointAscii,
entryPoint: default,
target: ASCII.GetLibraryProfile(shaderProfile),
flags: (uint)options);
flags: (uint)options,
stripReflectionData: stripReflectionData);
// Embed it as private data if requested
using ComPtr<ID3DBlob> d3DBlobLinked = D3DCompiler.SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
+28 -4
ファイルの表示
@@ -50,6 +50,12 @@ internal static unsafe partial class D3DCompiler
writtenBytes = Encoding.ASCII.GetBytes(source, buffer);
#endif
bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking;
bool stripReflectionData = (options & D2D1CompileOptions.StripReflectionData) == D2D1CompileOptions.StripReflectionData;
options &= ~D2D1CompileOptions.EnableLinking;
options &= ~D2D1CompileOptions.StripReflectionData;
try
{
// Compile the standalone D2D1 full shader
@@ -59,9 +65,10 @@ internal static unsafe partial class D3DCompiler
d2DEntry: ASCII.Execute,
entryPoint: ASCII.Execute,
target: ASCII.GetPixelShaderProfile(shaderProfile),
flags: (uint)(options & ~D2D1CompileOptions.EnableLinking));
flags: (uint)options,
stripReflectionData: stripReflectionData);
if ((options & D2D1CompileOptions.EnableLinking) == 0)
if (!enableLinking)
{
return d3DBlobFullShader.Move();
}
@@ -73,7 +80,8 @@ internal static unsafe partial class D3DCompiler
d2DEntry: ASCII.Execute,
entryPoint: default,
target: ASCII.GetLibraryProfile(shaderProfile),
flags: (uint)(options & ~D2D1CompileOptions.EnableLinking));
flags: (uint)options,
stripReflectionData: stripReflectionData);
// Embed it as private data if requested
using ComPtr<ID3DBlob> d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
@@ -95,6 +103,7 @@ internal static unsafe partial class D3DCompiler
/// <param name="entryPoint">The entry point for the shader.</param>
/// <param name="target">The shader target to use to compile the input source.</param>
/// <param name="flags">The compilationm flag to use.</param>
/// <param name="stripReflectionData">Whether to strip reflection data from the shader bytecode.</param>
/// <returns>The bytecode for the compiled shader.</returns>
/// <exception cref="FxcCompilationException">Thrown if the compilation fails.</exception>
public static ComPtr<ID3DBlob> Compile(
@@ -103,7 +112,8 @@ internal static unsafe partial class D3DCompiler
ReadOnlySpan<byte> d2DEntry,
ReadOnlySpan<byte> entryPoint,
ReadOnlySpan<byte> target,
uint flags)
uint flags,
bool stripReflectionData)
{
using ComPtr<ID3DBlob> d3DBlobBytecode = default;
using ComPtr<ID3DBlob> d3DBlobErrors = default;
@@ -181,6 +191,20 @@ internal static unsafe partial class D3DCompiler
// there would always be a message), but better safe than sorry.
hResult.Assert();
// If requested, strip the reflection data and return that bytecode blob
if (stripReflectionData)
{
using ComPtr<ID3DBlob> d3DBlobBytecodeNoReflection = default;
DirectX.D3DStripShader(
pShaderBytecode: d3DBlobBytecode.Get()->GetBufferPointer(),
BytecodeLength: d3DBlobBytecode.Get()->GetBufferSize(),
uStripFlags: (uint)D3DCOMPILER_STRIP_FLAGS.D3DCOMPILER_STRIP_REFLECTION_DATA,
ppStrippedBlob: d3DBlobBytecodeNoReflection.GetAddressOf()).Assert();
return d3DBlobBytecodeNoReflection.Move();
}
return d3DBlobBytecode.Move();
}
+1
ファイルの表示
@@ -48,6 +48,7 @@
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3d11shader\D3D11_SHADER_DESC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3dcommon\ID3DInclude.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3dcompiler\D3DCOMPILE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3dcompiler\D3DCOMPILER_STRIP_FLAGS.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3dcompiler\D3D_BLOB_PART.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3dcompiler\DirectX.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\ComputeSharp.Win32.D3D12\DirectX\um\d3dcommon\D3D_PRIMITIVE_TOPOLOGY.cs" Link="DirectX\um\d3dcommon\D3D_PRIMITIVE_TOPOLOGY.cs" />
-3
ファイルの表示
@@ -8,10 +8,7 @@ namespace ComputeSharp.Win32;
internal enum D2D1_EXTEND_MODE : uint
{
D2D1_EXTEND_MODE_CLAMP = 0,
D2D1_EXTEND_MODE_WRAP = 1,
D2D1_EXTEND_MODE_MIRROR = 2,
D2D1_EXTEND_MODE_FORCE_DWORD = 0xffffffff,
}
+19
ファイルの表示
@@ -0,0 +1,19 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from um/d3dcompiler.h in the Windows SDK for Windows 10.0.22621.0
// Original source is Copyright © Microsoft. All rights reserved.
using System;
namespace ComputeSharp.Win32;
[Flags]
internal enum D3DCOMPILER_STRIP_FLAGS
{
D3DCOMPILER_STRIP_REFLECTION_DATA = 0x00000001,
D3DCOMPILER_STRIP_DEBUG_INFO = 0x00000002,
D3DCOMPILER_STRIP_TEST_BLOBS = 0x00000004,
D3DCOMPILER_STRIP_PRIVATE_DATA = 0x00000008,
D3DCOMPILER_STRIP_ROOT_SIGNATURE = 0x00000010,
D3DCOMPILER_STRIP_FORCE_DWORD = 0x7fffffff,
}
-16
ファイルの表示
@@ -8,36 +8,20 @@ namespace ComputeSharp.Win32;
internal enum D3D_BLOB_PART
{
D3D_BLOB_INPUT_SIGNATURE_BLOB,
D3D_BLOB_OUTPUT_SIGNATURE_BLOB,
D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB,
D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB,
D3D_BLOB_ALL_SIGNATURE_BLOB,
D3D_BLOB_DEBUG_INFO,
D3D_BLOB_LEGACY_SHADER,
D3D_BLOB_XNA_PREPASS_SHADER,
D3D_BLOB_XNA_SHADER,
D3D_BLOB_PDB,
D3D_BLOB_PRIVATE_DATA,
D3D_BLOB_ROOT_SIGNATURE,
D3D_BLOB_DEBUG_NAME,
D3D_BLOB_TEST_ALTERNATE_SHADER = 0x8000,
D3D_BLOB_TEST_COMPILE_DETAILS,
D3D_BLOB_TEST_COMPILE_PERF,
D3D_BLOB_TEST_COMPILE_REPORT,
}
+3
ファイルの表示
@@ -18,4 +18,7 @@ internal static unsafe partial class DirectX
[DllImport("d3dcompiler_47", ExactSpelling = true)]
public static extern HRESULT D3DSetBlobPart([NativeTypeName("LPCVOID")] void* pSrcData, [NativeTypeName("SIZE_T")] nuint SrcDataSize, D3D_BLOB_PART Part, uint Flags, [NativeTypeName("LPCVOID")] void* pPart, [NativeTypeName("SIZE_T")] nuint PartSize, ID3DBlob** ppNewShader);
[DllImport("d3dcompiler_47", ExactSpelling = true)]
public static extern HRESULT D3DStripShader([NativeTypeName("LPCVOID")] void* pShaderBytecode, [NativeTypeName("SIZE_T")] nuint BytecodeLength, uint uStripFlags, ID3DBlob** ppStrippedBlob);
}
-2
ファイルの表示
@@ -8,8 +8,6 @@ namespace ComputeSharp.Win32;
internal enum DXGI_MODE_SCALING
{
DXGI_MODE_SCALING_UNSPECIFIED = 0,
DXGI_MODE_SCALING_CENTERED = 1,
DXGI_MODE_SCALING_STRETCHED = 2,
}
-3
ファイルの表示
@@ -8,10 +8,7 @@ namespace ComputeSharp.Win32;
internal enum DXGI_MODE_SCANLINE_ORDER
{
DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED = 0,
DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE = 1,
DXGI_MODE_SCANLINE_ORDER_UPPER_FIELD_FIRST = 2,
DXGI_MODE_SCANLINE_ORDER_LOWER_FIELD_FIRST = 3,
}
-2
ファイルの表示
@@ -8,8 +8,6 @@ namespace ComputeSharp.Win32;
internal enum STATFLAG
{
STATFLAG_DEFAULT = 0,
STATFLAG_NONAME = 1,
STATFLAG_NOOPEN = 2,
}
-3
ファイルの表示
@@ -8,10 +8,7 @@ namespace ComputeSharp.Win32;
internal enum STGTY
{
STGTY_STORAGE = 1,
STGTY_STREAM = 2,
STGTY_LOCKBYTES = 3,
STGTY_PROPERTY = 4,
}
+2 -2
ファイルの表示
@@ -131,8 +131,8 @@ public enum CompileOptions
ResourcesMayAlias = 1 << 13,
/// <summary>
/// Strips the reflection data from the generated shader bytecode. The bytecode size will be significantly lower, but
/// trying to perform reflection on the shader will return inaccurate results. Recommend if not reflection is done.
/// Strips the reflection data from the generated shader bytecode. The bytecode size will be significantly smaller, but
/// trying to perform reflection on the shader will return inaccurate results. It is recommend if not reflection is used.
/// </summary>
/// <remarks>
/// This flag maps to <c>-Qstrip_reflect</c>.
+92
ファイルの表示
@@ -616,6 +616,98 @@ namespace ComputeSharp.D2D1.Tests
}
}
[TestMethod]
public void LoadBytecode_EnableLinkingIsAppliedCorrectly()
{
ReadOnlyMemory<byte> hlslBytecode1 = D2D1PixelShader.LoadBytecode<ReferenceShaderWithDefaultCompileOptions>(out _, out D2D1CompileOptions compileOptions1);
ReadOnlyMemory<byte> hlslBytecode2 = D2D1PixelShader.LoadBytecode<ReferenceShaderWithEnableLinking>(out _, out D2D1CompileOptions compileOptions2);
Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixRowMajor, compileOptions1);
Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking | D2D1CompileOptions.PackMatrixRowMajor, compileOptions2);
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(hlslBytecode1, out MemoryManager<byte>? manager1));
Assert.IsTrue(manager1!.GetType().Name.Contains("HlslBytecodeMemoryManager"));
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(hlslBytecode2, out MemoryManager<byte>? manager2));
Assert.IsTrue(manager2!.GetType().Name.Contains("HlslBytecodeMemoryManager"));
// Same checks as in D2D1ShaderCompilerTests.CompileInvertEffectWithDefaultOptionsAndLinking
Assert.IsTrue(hlslBytecode1.Length > 700);
Assert.IsTrue(hlslBytecode2.Length > 1400);
Assert.IsTrue(hlslBytecode2.Length > hlslBytecode1.Length);
Assert.IsTrue((hlslBytecode2.Length - hlslBytecode1.Length) > 700);
}
[D2DInputCount(1)]
[D2DInputSimple(0)]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[D2DCompileOptions(D2D1CompileOptions.Default)]
[D2DGeneratedPixelShaderDescriptor]
public readonly partial struct ReferenceShaderWithDefaultCompileOptions : ID2D1PixelShader
{
public float4 Execute()
{
float4 color = D2D.GetInput(0);
float3 rgb = Hlsl.Saturate(1.0f - color.RGB);
return new(rgb, 1);
}
}
[D2DInputCount(1)]
[D2DInputSimple(0)]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[D2DCompileOptions(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking)]
[D2DGeneratedPixelShaderDescriptor]
public readonly partial struct ReferenceShaderWithEnableLinking : ID2D1PixelShader
{
public float4 Execute()
{
float4 color = D2D.GetInput(0);
float3 rgb = Hlsl.Saturate(1.0f - color.RGB);
return new(rgb, 1);
}
}
[TestMethod]
public void LoadBytecode_StripReflectionDataIsAppliedCorrectly()
{
ReadOnlyMemory<byte> hlslBytecode1 = D2D1PixelShader.LoadBytecode<ReferenceShaderWithDefaultCompileOptions>(out _, out D2D1CompileOptions compileOptions1);
ReadOnlyMemory<byte> hlslBytecode2 = D2D1PixelShader.LoadBytecode<ReferenceShaderWithStripReflectionData>(out _, out D2D1CompileOptions compileOptions2);
Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixRowMajor, compileOptions1);
Assert.AreEqual(D2D1CompileOptions.Default | D2D1CompileOptions.StripReflectionData | D2D1CompileOptions.PackMatrixRowMajor, compileOptions2);
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(hlslBytecode1, out MemoryManager<byte>? manager1));
Assert.IsTrue(manager1!.GetType().Name.Contains("HlslBytecodeMemoryManager"));
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(hlslBytecode2, out MemoryManager<byte>? manager2));
Assert.IsTrue(manager2!.GetType().Name.Contains("HlslBytecodeMemoryManager"));
// Same checks as in D2D1ShaderCompilerTests.CompileInvertEffectWithDefaultOptionsAndStripReflectionData
Assert.IsTrue(hlslBytecode1.Length > 700);
Assert.IsTrue(hlslBytecode2.Length > 400);
Assert.IsTrue(hlslBytecode1.Length > hlslBytecode2.Length);
Assert.IsTrue((hlslBytecode1.Length - hlslBytecode2.Length) > 300);
}
[D2DInputCount(1)]
[D2DInputSimple(0)]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[D2DCompileOptions(D2D1CompileOptions.Default | D2D1CompileOptions.StripReflectionData)]
[D2DGeneratedPixelShaderDescriptor]
public readonly partial struct ReferenceShaderWithStripReflectionData : ID2D1PixelShader
{
public float4 Execute()
{
float4 color = D2D.GetInput(0);
float3 rgb = Hlsl.Saturate(1.0f - color.RGB);
return new(rgb, 1);
}
}
[TestMethod]
public void GetConstantBufferSize_Empty()
{
+45 -1
ファイルの表示
@@ -78,12 +78,56 @@ public partial class D2D1ShaderCompilerTests
""";
ReadOnlyMemory<byte> bytecode = D2D1ShaderCompiler.Compile(
source.AsSpan(),
"PSMain".AsSpan(),
D2D1ShaderProfile.PixelShader40Level93,
D2D1CompileOptions.Default);
ReadOnlyMemory<byte> bytecodeWithLinking = D2D1ShaderCompiler.Compile(
source.AsSpan(),
"PSMain".AsSpan(),
D2D1ShaderProfile.PixelShader40Level93,
D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking);
Assert.IsTrue(bytecode.Length > 0);
Assert.IsTrue(bytecode.Length > 800);
Assert.IsTrue(bytecodeWithLinking.Length > 1600);
Assert.IsTrue(bytecodeWithLinking.Length > bytecode.Length);
Assert.IsTrue((bytecodeWithLinking.Length - bytecode.Length) > 800);
}
[TestMethod]
public void CompileInvertEffectWithDefaultOptionsAndStripReflectionData()
{
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<byte> bytecode = D2D1ShaderCompiler.Compile(
source.AsSpan(),
"PSMain".AsSpan(),
D2D1ShaderProfile.PixelShader40Level93,
D2D1CompileOptions.Default);
ReadOnlyMemory<byte> bytecodeWithStripReflectionData = D2D1ShaderCompiler.Compile(
source.AsSpan(),
"PSMain".AsSpan(),
D2D1ShaderProfile.PixelShader40Level93,
D2D1CompileOptions.Default | D2D1CompileOptions.StripReflectionData);
Assert.IsTrue(bytecode.Length > 800);
Assert.IsTrue(bytecodeWithStripReflectionData.Length > 500);
Assert.IsTrue(bytecode.Length > bytecodeWithStripReflectionData.Length);
Assert.IsTrue((bytecode.Length - bytecodeWithStripReflectionData.Length) > 200);
}
[TestMethod]