Merge pull request #281 from Sergio0694/dev/feature-d2dcompileoptions-attribute
Add [D2D1CompileOptions] attribute to control bytecode generation
このコミットが含まれているのは:
21ファイル変更
+319
-118
検索条件に一致するファイルはありません
@@ -50,3 +50,4 @@ CMPSD2D0040 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
|
||||
CMPSD2D0041 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2D0042 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2D0043 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
CMPSD2D0044 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
|
||||
@@ -67,8 +67,10 @@
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1ShaderProfile.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1ShaderProfile.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1BufferPrecision.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1BufferPrecision.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1ChannelDepth.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1ChannelDepth.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1CompileOptions.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1CompileOptions.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1Filter.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1Filter.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\Enums\D2D1PixelOptions.cs" Link="ComputeSharp.D2D1\Attributes\Enums\D2D1PixelOptions.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DCompileOptionsAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DCompileOptionsAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DEmbeddedBytecodeAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DEmbeddedBytecodeAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DInputComplexAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DInputComplexAttribute.cs" />
|
||||
<Compile Include="..\ComputeSharp.D2D1\Attributes\D2DInputCountAttribute.cs" Link="ComputeSharp.D2D1\Attributes\D2DInputCountAttribute.cs" />
|
||||
|
||||
@@ -618,4 +618,20 @@ internal static class DiagnosticDescriptors
|
||||
isEnabledByDefault: true,
|
||||
description: "A D2D1 shader must only have unique indices for all of its [D2DInputDescription] attributes.",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DiagnosticDescriptor"/> for the PackMatrixColumnMajor option being used.
|
||||
/// <para>
|
||||
/// Format: <c>"The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute"</c>.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static readonly DiagnosticDescriptor InvalidPackMatrixColumnMajorOption = new DiagnosticDescriptor(
|
||||
id: "CMPSD2D0044",
|
||||
title: "Invalid PackMatrixColumnMajor compile option",
|
||||
messageFormat: "The D2D1 shader of type {0} is using the PackMatrixColumnMajor option in its [D2DCompileOptions] attribute",
|
||||
category: "ComputeSharp.D2D1.Shaders",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true,
|
||||
description: "A D2D1 shader generated with ComputeSharp.D2D1 cannot use the PackMatrixColumnMajor option, as that is not compatible with the generated code used to load shader constant buffers.",
|
||||
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
|
||||
}
|
||||
+8
-12
@@ -28,27 +28,25 @@ partial class ID2D1ShaderGenerator
|
||||
/// <summary>
|
||||
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> node to process.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="structDeclaration"/>.</param>
|
||||
/// <param name="inputCount">The number of inputs for the shader.</param>
|
||||
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
|
||||
/// <param name="inputComplexIndices">The indicess of the complex shader inputs.</param>
|
||||
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
|
||||
/// <returns>The HLSL source for the shader.</returns>
|
||||
public static string GetHlslSource(
|
||||
ImmutableArray<Diagnostic>.Builder diagnostics,
|
||||
Compilation compilation,
|
||||
StructDeclarationSyntax structDeclaration,
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
int inputCount,
|
||||
ImmutableArray<int> inputSimpleIndices,
|
||||
ImmutableArray<int> inputComplexIndices,
|
||||
out ImmutableArray<Diagnostic> diagnostics)
|
||||
ImmutableArray<int> inputComplexIndices)
|
||||
{
|
||||
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
|
||||
// Properties are not supported
|
||||
DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol);
|
||||
DetectAndReportInvalidPropertyDeclarations(diagnostics, structDeclarationSymbol);
|
||||
|
||||
// We need to sets to track all discovered custom types and static methods
|
||||
HashSet<INamedTypeSymbol> discoveredTypes = new(SymbolEqualityComparer.Default);
|
||||
@@ -57,19 +55,17 @@ partial class ID2D1ShaderGenerator
|
||||
|
||||
// Explore the syntax tree and extract the processed info
|
||||
var semanticModelProvider = new SemanticModelProvider(compilation);
|
||||
var valueFields = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes);
|
||||
var (entryPoint, processedMethods) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
|
||||
var staticFields = GetStaticFields(builder, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
|
||||
var valueFields = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes);
|
||||
var (entryPoint, processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions);
|
||||
var staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
|
||||
|
||||
// Process the discovered types and constants
|
||||
var declaredTypes = GetDeclaredTypes(builder, structDeclarationSymbol, discoveredTypes);
|
||||
var declaredTypes = GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes);
|
||||
var definedConstants = GetDefinedConstants(constantDefinitions);
|
||||
|
||||
// Check whether the scene position is required
|
||||
bool requiresScenePosition = GetRequiresScenePositionInfo(structDeclarationSymbol);
|
||||
|
||||
diagnostics = builder.ToImmutable();
|
||||
|
||||
// Get the HLSL source
|
||||
return GetHlslSource(
|
||||
definedConstants,
|
||||
|
||||
+13
-18
@@ -19,22 +19,20 @@ partial class ID2D1ShaderGenerator
|
||||
/// <summary>
|
||||
/// Extracts the input info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
|
||||
/// <param name="inputCount">The number of shader inputs to declare.</param>
|
||||
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
|
||||
/// <param name="inputComplexIndices">The indices of the complex shader inputs.</param>
|
||||
/// <param name="combinedInputTypes">The combined and serialized input types for each available input.</param>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
public static void GetInfo(
|
||||
ImmutableArray<Diagnostic>.Builder diagnostics,
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
out int inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out ImmutableArray<uint> combinedInputTypes,
|
||||
out ImmutableArray<Diagnostic> diagnostics)
|
||||
out ImmutableArray<uint> combinedInputTypes)
|
||||
{
|
||||
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
|
||||
// We need a separate local here so we can keep the original value to apply
|
||||
// diagnostics to, but without returning invalid values to the caller which
|
||||
// might cause generator errors (eg. -1 would cause other code to just throw).
|
||||
@@ -71,17 +69,17 @@ partial class ID2D1ShaderGenerator
|
||||
// Validate the input count
|
||||
if (rawInputCount is not (>= 0 and <= 8))
|
||||
{
|
||||
builder.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
|
||||
diagnostics.Add(InvalidD2DInputCount, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
return;
|
||||
}
|
||||
|
||||
// All simple indices must be in range
|
||||
if (inputSimpleIndicesBuilder.Concat(inputComplexIndicesBuilder).Any(i => (uint)i >= rawInputCount))
|
||||
{
|
||||
builder.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
|
||||
diagnostics.Add(OutOfRangeInputIndex, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<int> inputSimpleIndicesAsSet = new(inputSimpleIndicesBuilder);
|
||||
@@ -90,25 +88,25 @@ partial class ID2D1ShaderGenerator
|
||||
// All simple indices must be unique
|
||||
if (inputSimpleIndicesAsSet.Count != inputSimpleIndicesBuilder.Count)
|
||||
{
|
||||
builder.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
diagnostics.Add(RepeatedD2DInputSimpleIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
return;
|
||||
}
|
||||
|
||||
// All complex indices must be unique
|
||||
if (inputComplexIndicesAsSet.Count != inputComplexIndicesBuilder.Count)
|
||||
{
|
||||
builder.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
diagnostics.Add(RepeatedD2DInputComplexIndices, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
return;
|
||||
}
|
||||
|
||||
// Simple and complex indices can't have indices in common
|
||||
if (inputSimpleIndicesAsSet.Intersect(inputComplexIndicesAsSet).Any())
|
||||
{
|
||||
builder.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
|
||||
diagnostics.Add(InvalidSimpleAndComplexIndicesCombination, structDeclarationSymbol, structDeclarationSymbol);
|
||||
|
||||
goto End;
|
||||
return;
|
||||
}
|
||||
|
||||
// Build the combined input types list now that inputs have been validated
|
||||
@@ -125,9 +123,6 @@ partial class ID2D1ShaderGenerator
|
||||
}
|
||||
|
||||
combinedInputTypes = combinedBuilder.MoveToImmutable();
|
||||
|
||||
End:
|
||||
diagnostics = builder.ToImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
-17
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.D2D1.__Internals;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
@@ -39,7 +40,7 @@ partial class ID2D1ShaderGenerator
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadBytecode<TLoader>(ref TLoader loader, global::ComputeSharp.D2D1.D2D1ShaderProfile? shaderProfile)
|
||||
// readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadBytecode<TLoader>(ref TLoader loader, global::ComputeSharp.D2D1.D2D1ShaderProfile? shaderProfile, global::ComputeSharp.D2D1.D2D1CompileOptions? options)
|
||||
// {
|
||||
// <BODY>
|
||||
// }
|
||||
@@ -50,7 +51,8 @@ partial class ID2D1ShaderGenerator
|
||||
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
|
||||
.AddParameterListParameters(
|
||||
Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader")),
|
||||
Parameter(Identifier("shaderProfile")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"))))
|
||||
Parameter(Identifier("shaderProfile")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"))),
|
||||
Parameter(Identifier("options")).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1CompileOptions"))))
|
||||
.WithBody(block);
|
||||
}
|
||||
|
||||
@@ -62,21 +64,56 @@ partial class ID2D1ShaderGenerator
|
||||
/// <returns>The <see cref="BlockSyntax"/> instance trying to retrieve the precompiled shader.</returns>
|
||||
private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, out string? bytecodeLiterals)
|
||||
{
|
||||
// This method needs to handle several different scenarions, which are as follows:
|
||||
// 1) If [EmbeddedBytecode] is used, then the shader is precompiled and stored as a ReadOnlySpan<byte>. In this case,
|
||||
// this bytecode would have been generated with some specific D2D1ShaderProfile and D2D1CompileOptions values. So,
|
||||
// a branch is generated to try to retrieve this precompiled bytecode, which can be returned if and only if the
|
||||
// requested shader profile and compile options are either null, or either matches the values that have been used
|
||||
// to generate that bytecode. If this is the case, then no further work is done and that binary data is returned.
|
||||
// 2) If either the requested shader profile or options do not match the values used to create the shader bytecode,
|
||||
// then a call to the internal D2D1ShaderCompiler.LoadDynamicBytecode API is used, to create the shader on the fly.
|
||||
// 3) If [EmbeddedBytecode] is not used, then regardless of the requested profile and options, a new shader is compiled.
|
||||
// 4) If this is the case and either the shader profile or the options are null, then this method will manually set the
|
||||
// value being null to the default one to use. That is, D2D1ShaderProfile.PixelShader50 will be used as the target,
|
||||
// and the options will be set to D2D1CompileOptions.Default (and optionally, D2D1CompileOptions.EnableLinking too).
|
||||
|
||||
// Get a formatted representation of the compile options being used
|
||||
ExpressionSyntax optionsExpression =
|
||||
ParseExpression(
|
||||
bytecodeInfo.CompileOptions
|
||||
.GetValueOrDefault()
|
||||
.ToString(CultureInfo.InvariantCulture)
|
||||
.Split(',')
|
||||
.Select(static name => $"global::ComputeSharp.D2D1.D2D1CompileOptions.{name.Trim()}")
|
||||
.Aggregate("", static (left, right) => left.Length > 0 ? $"{left} | {right}" : right));
|
||||
|
||||
// Add parantheses if needed
|
||||
if (optionsExpression is BinaryExpressionSyntax)
|
||||
{
|
||||
optionsExpression = ParenthesizedExpression(optionsExpression);
|
||||
}
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, in this, shaderProfile, <ENABLE_LINKING_SUPPORT>);
|
||||
// global::ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, in this, shaderProfile ?? <DEFAULT_PROFILE>, options ?? <REQUESTED_OPTIONS>);
|
||||
ExpressionStatementSyntax dynamicLoadingStatement =
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler"),
|
||||
IdentifierName("global::ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler"),
|
||||
IdentifierName("LoadDynamicBytecode")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName("loader")).WithRefKindKeyword(Token(SyntaxKind.RefKeyword)),
|
||||
Argument(ThisExpression()).WithRefKindKeyword(Token(SyntaxKind.InKeyword)),
|
||||
Argument(IdentifierName("shaderProfile")),
|
||||
Argument(LiteralExpression(bytecodeInfo.IsLinkingSupported ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression))));
|
||||
Argument(BinaryExpression(
|
||||
SyntaxKind.CoalesceExpression,
|
||||
IdentifierName("shaderProfile"),
|
||||
IdentifierName($"global::ComputeSharp.D2D1.D2D1ShaderProfile.{D2D1ShaderProfile.PixelShader50}"))),
|
||||
Argument(BinaryExpression(
|
||||
SyntaxKind.CoalesceExpression,
|
||||
IdentifierName("options"),
|
||||
optionsExpression))));
|
||||
|
||||
// If there is no precompiled bytecode, just return the dynamic path
|
||||
if (bytecodeInfo.Bytecode.IsDefaultOrEmpty)
|
||||
@@ -90,7 +127,7 @@ partial class ID2D1ShaderGenerator
|
||||
|
||||
// This code produces a branch as follows:
|
||||
//
|
||||
// if (shaderProfile is null or <SHADER_PROFILE>)
|
||||
// if (shaderProfile is null or <SHADER_PROFILE> && options is null or <OPTIONS>)
|
||||
// {
|
||||
// global::System.ReadOnlySpan<byte> bytecode = new byte[] { __EMBEDDED_SHADER_BYTECODE };
|
||||
// loader.LoadEmbeddedBytecode(bytecode);
|
||||
@@ -101,16 +138,24 @@ partial class ID2D1ShaderGenerator
|
||||
// having to create/parse tens of thousands of literal expression nodes for every shader.
|
||||
IfStatementSyntax embeddedBranch =
|
||||
IfStatement(
|
||||
IsPatternExpression(
|
||||
IdentifierName("shaderProfile"),
|
||||
BinaryPattern(
|
||||
SyntaxKind.OrPattern,
|
||||
ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
|
||||
ConstantPattern(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"),
|
||||
IdentifierName(bytecodeInfo.ShaderProfile.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)))))),
|
||||
BinaryExpression(
|
||||
SyntaxKind.LogicalAndExpression,
|
||||
IsPatternExpression(
|
||||
IdentifierName("shaderProfile"),
|
||||
BinaryPattern(
|
||||
SyntaxKind.OrPattern,
|
||||
ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
|
||||
ConstantPattern(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"),
|
||||
IdentifierName(bytecodeInfo.ShaderProfile.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)))))),
|
||||
IsPatternExpression(
|
||||
IdentifierName("options"),
|
||||
BinaryPattern(
|
||||
SyntaxKind.OrPattern,
|
||||
ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
|
||||
ConstantPattern(optionsExpression)))),
|
||||
Block(
|
||||
LocalDeclarationStatement(
|
||||
VariableDeclaration(
|
||||
|
||||
+43
-5
@@ -41,6 +41,33 @@ partial class ID2D1ShaderGenerator
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the compile options for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
|
||||
/// <returns>The compile options to use to compile the shader, if present.</returns>
|
||||
public static D2D1CompileOptions? GetCompileOptions(ImmutableArray<Diagnostic>.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
if (structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData))
|
||||
{
|
||||
D2D1CompileOptions options = (D2D1CompileOptions)attributeData!.ConstructorArguments[0].Value!;
|
||||
|
||||
if ((options & D2D1CompileOptions.PackMatrixColumnMajor) != 0)
|
||||
{
|
||||
diagnostics.Add(
|
||||
InvalidPackMatrixColumnMajorOption,
|
||||
structDeclarationSymbol,
|
||||
structDeclarationSymbol);
|
||||
}
|
||||
|
||||
// PackMatrixRowMajor is always automatically enabled
|
||||
return options | D2D1CompileOptions.PackMatrixRowMajor;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the metadata definition for the current shader.
|
||||
/// </summary>
|
||||
@@ -70,18 +97,23 @@ partial class ID2D1ShaderGenerator
|
||||
/// </summary>
|
||||
/// <param name="sourceInfo">The source info for the shader to compile.</param>
|
||||
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
|
||||
/// <param name="options">The effective compile options used to create the shader bytecode.</param>
|
||||
/// <param name="diagnostic">The resulting diagnostic from the processing operation, if any.</param>
|
||||
/// <returns>The <see cref="ImmutableArray{T}"/> instance with the compiled shader bytecode.</returns>
|
||||
public static unsafe ImmutableArray<byte> GetBytecode(HlslShaderSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic)
|
||||
public static unsafe ImmutableArray<byte> GetBytecode(
|
||||
HlslShaderSourceInfo sourceInfo,
|
||||
CancellationToken token,
|
||||
out D2D1CompileOptions options,
|
||||
out DiagnosticInfo? diagnostic)
|
||||
{
|
||||
ImmutableArray<byte> bytecode = ImmutableArray<byte>.Empty;
|
||||
|
||||
// No embedded shader was requested, or there were some errors earlier in the pipeline.
|
||||
// In this case, skip the compilation, as diagnostic will be emitted for those anyway.
|
||||
// Compiling would just add overhead and result in more errors, as the HLSL would be invalid.
|
||||
if (sourceInfo.HasErrors ||
|
||||
sourceInfo.ShaderProfile is null)
|
||||
if (sourceInfo is { HasErrors: true } or { ShaderProfile: null })
|
||||
{
|
||||
options = default;
|
||||
diagnostic = null;
|
||||
|
||||
goto End;
|
||||
@@ -91,11 +123,15 @@ partial class ID2D1ShaderGenerator
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Compile the shader bytecode
|
||||
// If an explicit set of compile options is provided, use that directly. Otherwise, use the default
|
||||
// options plus the option to enable linking only if the shader can potentially support it.
|
||||
options = sourceInfo.CompileOptions ?? (D2D1CompileOptions.Default | (sourceInfo.IsLinkingSupported ? D2D1CompileOptions.EnableLinking : 0));
|
||||
|
||||
// Compile the shader bytecode using the requested parameters
|
||||
using ComPtr<ID3DBlob> dxcBlobBytecode = D3DCompiler.Compile(
|
||||
sourceInfo.HlslSource.AsSpan(),
|
||||
sourceInfo.ShaderProfile.Value,
|
||||
sourceInfo.IsLinkingSupported);
|
||||
options);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
@@ -109,10 +145,12 @@ partial class ID2D1ShaderGenerator
|
||||
}
|
||||
catch (Win32Exception e)
|
||||
{
|
||||
options = default;
|
||||
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message));
|
||||
}
|
||||
catch (FxcCompilationException e)
|
||||
{
|
||||
options = default;
|
||||
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message));
|
||||
}
|
||||
|
||||
|
||||
+4
-8
@@ -23,14 +23,14 @@ partial class ID2D1ShaderGenerator
|
||||
/// <summary>
|
||||
/// Explores a given type hierarchy and generates statements to load fields.
|
||||
/// </summary>
|
||||
/// <param name="diagnostics">The collection of produced <see cref="Diagnostic"/> instances.</param>
|
||||
/// <param name="structDeclarationSymbol">The current shader type being explored.</param>
|
||||
/// <param name="root32BitConstantCount">The total number of needed 32 bit constants in the shader root signature.</param>
|
||||
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
|
||||
/// <returns>The sequence of <see cref="FieldInfo"/> instances for all captured resources and values.</returns>
|
||||
public static ImmutableArray<FieldInfo> GetInfo(
|
||||
ImmutableArray<Diagnostic>.Builder diagnostics,
|
||||
ITypeSymbol structDeclarationSymbol,
|
||||
out int root32BitConstantCount,
|
||||
out ImmutableArray<Diagnostic> diagnostics)
|
||||
out int root32BitConstantCount)
|
||||
{
|
||||
// Helper method that uses boxes instead of ref-s (illegal in enumerators)
|
||||
static IEnumerable<FieldInfo> GetCapturedFieldInfos(
|
||||
@@ -78,8 +78,6 @@ partial class ID2D1ShaderGenerator
|
||||
}
|
||||
}
|
||||
|
||||
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
|
||||
// Setup the resource and byte offsets for tracking
|
||||
StrongBox<int> rawDataOffsetAsBox = new();
|
||||
|
||||
@@ -107,11 +105,9 @@ partial class ID2D1ShaderGenerator
|
||||
|
||||
if (rootSignatureDwordSize > 64)
|
||||
{
|
||||
builder.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
|
||||
diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
|
||||
}
|
||||
|
||||
diagnostics = builder.ToImmutable();
|
||||
|
||||
return fieldInfos;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,16 +51,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
|
||||
// Get the dispatch data, input types, HLSL source and embedded bytecode info. This info is computed
|
||||
// on the same step as parts are shared in following sub-branches in the incremental generator pipeline.
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<DispatchDataInfo> Dispatch, Result<InputTypesInfo> InputTypes, Result<HlslShaderSourceInfo> Source)> shaderInfoWithErrors =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source, ImmutableArray<Diagnostic> Diagnostics)> shaderInfoWithErrors =
|
||||
shaderDeclarations
|
||||
.Combine(context.CompilationProvider)
|
||||
.Select(static (item, token) =>
|
||||
{
|
||||
ImmutableArray<Diagnostic>.Builder diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
|
||||
// LoadDispatchData() info
|
||||
ImmutableArray<FieldInfo> fieldInfos = LoadDispatchData.GetInfo(
|
||||
diagnostics,
|
||||
item.Left.Symbol,
|
||||
out int root32BitConstantCount,
|
||||
out ImmutableArray<Diagnostic> dispatchDataDiagnostics);
|
||||
out int root32BitConstantCount);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
@@ -68,56 +70,62 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
|
||||
// Get the input info for GetInputInfo()
|
||||
GetInputType.GetInfo(
|
||||
diagnostics,
|
||||
item.Left.Symbol,
|
||||
out int inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out ImmutableArray<uint> inputTypes,
|
||||
out ImmutableArray<Diagnostic> inputTypesDiagnostics);
|
||||
out ImmutableArray<uint> inputTypes);
|
||||
|
||||
InputTypesInfo inputTypesInfo = new(inputTypes);
|
||||
|
||||
// Get HLSL source for BuildHlslSource()
|
||||
string hlslSource = BuildHlslSource.GetHlslSource(
|
||||
diagnostics,
|
||||
item.Right,
|
||||
item.Left.Syntax,
|
||||
item.Left.Symbol,
|
||||
inputCount,
|
||||
inputSimpleIndices,
|
||||
inputComplexIndices,
|
||||
out ImmutableArray<Diagnostic> hlslSourceDiagnostics);
|
||||
inputComplexIndices);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Get the shader profile and linking info for LoadBytecode()
|
||||
D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol);
|
||||
bool hasErrors = !dispatchDataDiagnostics.IsDefaultOrEmpty || !hlslSourceDiagnostics.IsDefaultOrEmpty;
|
||||
bool isLinkingSupported = !hasErrors && LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
|
||||
D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, item.Left.Symbol);
|
||||
bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(item.Left.Symbol, inputCount);
|
||||
|
||||
HlslShaderSourceInfo sourceInfo = new(
|
||||
hlslSource,
|
||||
shaderProfile,
|
||||
compileOptions,
|
||||
isLinkingSupported,
|
||||
hasErrors);
|
||||
HasErrors: diagnostics.Count > 0);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
return (
|
||||
item.Left.Hierarchy,
|
||||
new Result<DispatchDataInfo>(dispatchDataInfo, dispatchDataDiagnostics),
|
||||
new Result<InputTypesInfo>(inputTypesInfo, inputTypesDiagnostics),
|
||||
new Result<HlslShaderSourceInfo>(sourceInfo, hlslSourceDiagnostics));
|
||||
dispatchDataInfo,
|
||||
inputTypesInfo,
|
||||
sourceInfo,
|
||||
diagnostics.ToImmutableArray());
|
||||
});
|
||||
|
||||
// Output the diagnostics
|
||||
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Dispatch.Errors));
|
||||
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.InputTypes.Errors));
|
||||
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Source.Errors));
|
||||
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.Diagnostics));
|
||||
|
||||
// Filter all items to enable caching at a coarse level, and remove diagnostics
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.Dispatch, item.InputTypes, item.Source))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer<HlslShaderSourceInfo>.Default);
|
||||
|
||||
// Filter items to enable caching for the input count methods
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.InputTypes.Value.InputTypes.Length))
|
||||
shaderInfo
|
||||
.Select(static (item, token) => (item.Hierarchy, item.InputTypes.InputTypes.Length))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<int>.Default);
|
||||
|
||||
// Generate the GetInputCount() methods
|
||||
@@ -129,12 +137,6 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(GetInputCount)}", compilationUnit.ToFullString());
|
||||
});
|
||||
|
||||
// Filter all items to enable caching at a coarse level, and remove diagnostics
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchDataInfo Dispatch, InputTypesInfo InputTypes, HlslShaderSourceInfo Source)> shaderInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.Dispatch.Value, item.InputTypes.Value, item.Source.Value))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, DispatchDataInfo.Comparer.Default, InputTypesInfo.Comparer.Default, EqualityComparer<HlslShaderSourceInfo>.Default);
|
||||
|
||||
// Get a filtered sequence to enable caching
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, InputTypesInfo InputTypes)> inputTypesInfo =
|
||||
shaderInfo
|
||||
@@ -200,11 +202,19 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
shaderBytecodeInfo
|
||||
.Select(static (item, token) =>
|
||||
{
|
||||
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(item.Source, token, out DiagnosticInfo? diagnostic);
|
||||
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(
|
||||
item.Source,
|
||||
token,
|
||||
out D2D1CompileOptions options,
|
||||
out DiagnosticInfo? diagnostic);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
EmbeddedBytecodeInfo bytecodeInfo = new(item.Source.HlslSource, item.Source.ShaderProfile, item.Source.IsLinkingSupported, bytecode);
|
||||
EmbeddedBytecodeInfo bytecodeInfo = new(
|
||||
item.Source.HlslSource,
|
||||
item.Source.ShaderProfile,
|
||||
options,
|
||||
bytecode);
|
||||
|
||||
return (item.Hierarchy, bytecodeInfo, diagnostic);
|
||||
});
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
/// </summary>
|
||||
/// <param name="HlslSource">The HLSL source for the shader.</param>
|
||||
/// <param name="ShaderProfile">The shader profile to use to compile the shader, if requested.</param>
|
||||
/// <param name="IsLinkingSupported">Whether linking is supported for the current shader.</param>
|
||||
/// <param name="CompileOptions">The compile options to use to compile the shader.</param>
|
||||
/// <param name="Bytecode">The compiled shader bytecode, if available.</param>
|
||||
internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported, ImmutableArray<byte> Bytecode)
|
||||
internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, D2D1CompileOptions? CompileOptions, ImmutableArray<byte> Bytecode)
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="EmbeddedBytecodeInfo"/>.
|
||||
@@ -25,7 +25,7 @@ internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile
|
||||
{
|
||||
hashCode.Add(obj.HlslSource);
|
||||
hashCode.Add(obj.ShaderProfile);
|
||||
hashCode.Add(obj.IsLinkingSupported);
|
||||
hashCode.Add(obj.CompileOptions);
|
||||
hashCode.AddBytes(obj.Bytecode.AsSpan());
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ internal sealed record EmbeddedBytecodeInfo(string HlslSource, D2D1ShaderProfile
|
||||
return
|
||||
x.HlslSource == y.HlslSource &&
|
||||
x.ShaderProfile == y.ShaderProfile &&
|
||||
x.IsLinkingSupported == y.IsLinkingSupported &&
|
||||
x.CompileOptions == y.CompileOptions &&
|
||||
x.Bytecode.SequenceEqual(y.Bytecode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,12 @@
|
||||
/// </summary>
|
||||
/// <param name="HlslSource">The HLSL source.</param>
|
||||
/// <param name="ShaderProfile">The shader profile to use to compile the shader, if requested.</param>
|
||||
/// <param name="CompileOptions">The compile options to use to compile the shader.</param>
|
||||
/// <param name="IsLinkingSupported">Whether the shader supports linking.</param>
|
||||
/// <param name="HasErrors">Whether any errors have been detected, and therefore the shader compilation should be skipped.</param>
|
||||
internal sealed record HlslShaderSourceInfo(string HlslSource, D2D1ShaderProfile? ShaderProfile, bool IsLinkingSupported, bool HasErrors);
|
||||
internal sealed record HlslShaderSourceInfo(
|
||||
string HlslSource,
|
||||
D2D1ShaderProfile? ShaderProfile,
|
||||
D2D1CompileOptions? CompileOptions,
|
||||
bool IsLinkingSupported,
|
||||
bool HasErrors);
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace ComputeSharp.D2D1;
|
||||
|
||||
/// <summary>
|
||||
/// An attribute that indicates the compile options that should be used when compiling a given D2D1 shader.
|
||||
/// This applies when the shader is precompiled (through <see cref="D2DEmbeddedBytecodeAttribute"/>) as well.
|
||||
/// <para>
|
||||
/// This attribute can be used to annotate shader types as follows:
|
||||
/// <code>
|
||||
/// [D2DCompileOptions(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel3)]
|
||||
/// struct MyShader : ID2D1PixelShader
|
||||
/// {
|
||||
/// }
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// </para>
|
||||
/// Note that the <see cref="D2D1CompileOptions.PackMatrixRowMajor"/> is always enabled automatically and does not need to be
|
||||
/// specified. This option is mandatory, as the generated code to load the constant buffer from a shader assumes the layout
|
||||
/// for matrix types is row major. For the same reason, using <see cref="D2D1CompileOptions.PackMatrixColumnMajor"/> is not
|
||||
/// allowed, as it would cause the constant buffer retrieved from a shader to be potentially incorrect.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
|
||||
public sealed class D2DCompileOptionsAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2DCompileOptionsAttribute"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="options">The compiler options to use to compile the shader.</param>
|
||||
public D2DCompileOptionsAttribute(D2D1CompileOptions options)
|
||||
{
|
||||
Options = options;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of threads in each thread group for the X axis
|
||||
/// </summary>
|
||||
public D2D1CompileOptions Options { get; }
|
||||
}
|
||||
@@ -8,7 +8,6 @@ namespace ComputeSharp.D2D1;
|
||||
/// <para>
|
||||
/// This attribute can be used to annotate shader types as follows:
|
||||
/// <code>
|
||||
/// // A compute shader that is dispatched on a target buffer
|
||||
/// [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)]
|
||||
/// struct MyShader : ID2D1PixelShader
|
||||
/// {
|
||||
|
||||
+5
-2
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
#if !SOURCE_GENERATOR
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
#endif
|
||||
using TerraFX.Interop.DirectX;
|
||||
|
||||
namespace ComputeSharp.D2D1.Interop;
|
||||
namespace ComputeSharp.D2D1;
|
||||
|
||||
/// <summary>
|
||||
/// Flags to control the options that can be used to compile a D2D1 shader.
|
||||
@@ -10,7 +13,7 @@ namespace ComputeSharp.D2D1.Interop;
|
||||
/// For more info, see For more info, see <see href="https://docs.microsoft.com/windows/win32/direct3dhlsl/d3dcompile-constants"/>.
|
||||
/// </remarks>
|
||||
[Flags]
|
||||
public enum D2D1ShaderCompilerOptions
|
||||
public enum D2D1CompileOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Directs the compiler to insert debug file/line/type/symbol information into the output code.
|
||||
@@ -88,9 +88,10 @@ public interface ID2D1Shader
|
||||
/// <typeparam name="TLoader">The type of bytecode loader being used.</typeparam>
|
||||
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the bytecode.</param>
|
||||
/// <param name="shaderProfile">The shader profile to use to get the shader bytecode (if <see langword="null"/>, the precompiled shader will be used).</param>
|
||||
/// <param name="options">The compile options to use to get the shader bytecode (if <see langword="null"/>, the precompiled shader will be used).</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if a precompiled bytecode was requested (<paramref name="shaderProfile"/> is <see langword="null"/>), but it wasn't availablle.</exception>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This method is not intended to be used directly by user code")]
|
||||
void LoadBytecode<TLoader>(ref TLoader loader, D2D1ShaderProfile? shaderProfile)
|
||||
void LoadBytecode<TLoader>(ref TLoader loader, D2D1ShaderProfile? shaderProfile, D2D1CompileOptions? options)
|
||||
where TLoader : struct, ID2D1BytecodeLoader;
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public static class D2D1PixelShader
|
||||
public static ReadOnlyMemory<byte> LoadBytecode<T>()
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
return LoadOrCompileBytecode<T>(null);
|
||||
return LoadOrCompileBytecode<T>(null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,13 +40,65 @@ public static class D2D1PixelShader
|
||||
/// <param name="shaderProfile">The shader profile to use to get the shader bytecode.</param>
|
||||
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// If precompiled shader for the profile does not exist, the shader will be compiled with either the custom compile options specified on the shader
|
||||
/// type <typeparamref name="T"/> (through <see cref="D2DCompileOptionsAttribute"/>), or using <see cref="D2D1CompileOptions.Default"/> otherwise.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Additionally, in case no custom compile options are specified and the the shader type <typeparamref name="T"/>
|
||||
/// supports linking, <see cref="D2D1CompileOptions.EnableLinking"/> will also be automatically added.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the input shader was precompiled, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a pinned memory buffer (from the PE section).
|
||||
/// If the shader was compiled at runtime, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a <see cref="byte"/> array with the bytecode.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static ReadOnlyMemory<byte> LoadBytecode<T>(D2D1ShaderProfile shaderProfile)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
return LoadOrCompileBytecode<T>(shaderProfile);
|
||||
return LoadOrCompileBytecode<T>(shaderProfile, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the bytecode from an input D2D1 pixel shader.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to load the bytecode for.</typeparam>
|
||||
/// <param name="options">The compile options to use to get the shader bytecode.</param>
|
||||
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// If precompiled shader with the requested options does not exist, the shader will be compiled with the input options. If additional compile
|
||||
/// options have been specified on the shader type <typeparamref name="T"/> (through <see cref="D2DCompileOptionsAttribute"/>), they will be ignored.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the shader needs to be recompiled, the shader profile that will be used is <see cref="D2D1ShaderProfile.PixelShader50"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the input shader was precompiled, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a pinned memory buffer (from the PE section).
|
||||
/// If the shader was compiled at runtime, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a <see cref="byte"/> array with the bytecode.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static ReadOnlyMemory<byte> LoadBytecode<T>(D2D1CompileOptions options)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
return LoadOrCompileBytecode<T>(null, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the bytecode from an input D2D1 pixel shader.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to load the bytecode for.</typeparam>
|
||||
/// <param name="shaderProfile">The shader profile to use to get the shader bytecode.</param>
|
||||
/// <param name="options">The compile options to use to get the shader bytecode.</param>
|
||||
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
|
||||
/// <remarks>
|
||||
/// If the input shader was precompiled, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a pinned memory buffer (from the PE section).
|
||||
/// If the shader was compiled at runtime, the returned <see cref="ReadOnlyMemory{T}"/> instance will wrap a <see cref="byte"/> array with the bytecode.
|
||||
/// </remarks>
|
||||
public static ReadOnlyMemory<byte> LoadBytecode<T>(D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
return LoadOrCompileBytecode<T>(shaderProfile, options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -54,16 +106,17 @@ public static class D2D1PixelShader
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of D2D1 pixel shader to load the bytecode for.</typeparam>
|
||||
/// <param name="shaderProfile">The shader profile to use to get the shader bytecode.</param>
|
||||
/// <param name="options">The compile options to use to get the shader bytecode.</param>
|
||||
/// <returns>A <see cref="ReadOnlyMemory{T}"/> instance with the resulting shader bytecode.</returns>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the input shader has not been precompiled.</exception>
|
||||
private static unsafe ReadOnlyMemory<byte> LoadOrCompileBytecode<T>(D2D1ShaderProfile? shaderProfile)
|
||||
private static unsafe ReadOnlyMemory<byte> LoadOrCompileBytecode<T>(D2D1ShaderProfile? shaderProfile, D2D1CompileOptions? options)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
D2D1ShaderBytecodeLoader bytecodeLoader = default;
|
||||
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.LoadBytecode(ref bytecodeLoader, shaderProfile);
|
||||
shader.LoadBytecode(ref bytecodeLoader, shaderProfile, options);
|
||||
|
||||
using ComPtr<ID3DBlob> dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode);
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public static class D2D1ReflectionServices
|
||||
|
||||
D2D1ShaderBytecodeLoader bytecodeLoader = default;
|
||||
|
||||
shader.LoadBytecode(ref bytecodeLoader, D2D1ShaderProfile.PixelShader50);
|
||||
shader.LoadBytecode(ref bytecodeLoader, D2D1ShaderProfile.PixelShader50, D2D1CompileOptions.Default);
|
||||
|
||||
using ComPtr<ID3DBlob> dynamicBytecode = bytecodeLoader.GetResultingShaderBytecode(out ReadOnlySpan<byte> precompiledBytecode);
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public static class D2D1ShaderCompiler
|
||||
ReadOnlySpan<char> hlslSource,
|
||||
ReadOnlySpan<char> entryPoint,
|
||||
D2D1ShaderProfile shaderProfile,
|
||||
D2D1ShaderCompilerOptions options)
|
||||
D2D1CompileOptions options)
|
||||
{
|
||||
// Encode the HLSL source to ASCII
|
||||
int maxSourceLength = Encoding.ASCII.GetMaxByteCount(hlslSource.Length);
|
||||
@@ -60,13 +60,13 @@ public static class D2D1ShaderCompiler
|
||||
ReadOnlySpan<byte> hlslSourceAscii,
|
||||
ReadOnlySpan<byte> entryPointAscii,
|
||||
D2D1ShaderProfile shaderProfile,
|
||||
D2D1ShaderCompilerOptions options)
|
||||
D2D1CompileOptions options)
|
||||
{
|
||||
// Check linking support
|
||||
bool enableLinking = (options & D2D1ShaderCompilerOptions.EnableLinking) == D2D1ShaderCompilerOptions.EnableLinking;
|
||||
bool enableLinking = (options & D2D1CompileOptions.EnableLinking) == D2D1CompileOptions.EnableLinking;
|
||||
|
||||
// Remove the linking flag to make the options blittable to flags
|
||||
options &= ~D2D1ShaderCompilerOptions.EnableLinking;
|
||||
options &= ~D2D1CompileOptions.EnableLinking;
|
||||
|
||||
// Compile the standalone D2D1 full shader
|
||||
using ComPtr<ID3DBlob> d3DBlobFullShader = D3DCompiler.CompileShader(
|
||||
|
||||
@@ -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="enableLinking">Whether to enable linking for the shader.</param>
|
||||
/// <param name="options">The options to use to compile the shader.</param>
|
||||
/// <returns>The bytecode for the compiled shader.</returns>
|
||||
public static ComPtr<ID3DBlob> Compile(ReadOnlySpan<char> source, D2D1ShaderProfile shaderProfile, bool enableLinking)
|
||||
public static ComPtr<ID3DBlob> Compile(ReadOnlySpan<char> source, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
|
||||
{
|
||||
int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);
|
||||
byte[] buffer = ArrayPool<byte>.Shared.Rent(maxLength);
|
||||
@@ -49,9 +49,9 @@ internal static unsafe partial class D3DCompiler
|
||||
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);
|
||||
flags: (uint)(options & ~D2D1CompileOptions.EnableLinking));
|
||||
|
||||
if (!enableLinking)
|
||||
if ((options & D2D1CompileOptions.EnableLinking) == 0)
|
||||
{
|
||||
return d3DBlobFullShader.Move();
|
||||
}
|
||||
@@ -63,7 +63,7 @@ internal static unsafe partial class D3DCompiler
|
||||
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);
|
||||
flags: (uint)(options & ~D2D1CompileOptions.EnableLinking));
|
||||
|
||||
// Embed it as private data if requested
|
||||
using ComPtr<ID3DBlob> d3DBlobLinked = SetD3DPrivateData(d3DBlobFullShader.Get(), d3DBlobFunction.Get());
|
||||
|
||||
@@ -21,8 +21,8 @@ public static class D2D1ShaderCompiler
|
||||
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the bytecode.</param>
|
||||
/// <param name="shader">The input <typeparamref name="T"/> instance representing the D2D1 shader to run.</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>
|
||||
public static unsafe void LoadDynamicBytecode<TLoader, T>(ref TLoader loader, in T shader, D2D1ShaderProfile? shaderProfile, bool enableLinkingSupport)
|
||||
/// <param name="options">The compiler options to use to compile the shader.</param>
|
||||
public static unsafe void LoadDynamicBytecode<TLoader, T>(ref TLoader loader, in T shader, D2D1ShaderProfile shaderProfile, D2D1CompileOptions options)
|
||||
where TLoader : struct, ID2D1BytecodeLoader
|
||||
where T : struct, ID2D1Shader
|
||||
{
|
||||
@@ -30,8 +30,8 @@ public static class D2D1ShaderCompiler
|
||||
|
||||
using ComPtr<ID3DBlob> d3DBlobBytecode = Shaders.Translation.D3DCompiler.Compile(
|
||||
hlslSource.AsSpan(),
|
||||
shaderProfile ?? D2D1ShaderProfile.PixelShader50,
|
||||
enableLinkingSupport);
|
||||
shaderProfile,
|
||||
options);
|
||||
|
||||
loader.LoadDynamicBytecode((IntPtr)d3DBlobBytecode.Get());
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public partial class D2D1ShaderCompilerTests
|
||||
source,
|
||||
"PSMain",
|
||||
D2D1ShaderProfile.PixelShader40Level93,
|
||||
D2D1ShaderCompilerOptions.Default);
|
||||
D2D1CompileOptions.Default);
|
||||
|
||||
Assert.IsTrue(bytecode.Length > 0);
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public partial class D2D1ShaderCompilerTests
|
||||
source,
|
||||
"PSMain",
|
||||
D2D1ShaderProfile.PixelShader40Level93,
|
||||
D2D1ShaderCompilerOptions.Default | D2D1ShaderCompilerOptions.EnableLinking);
|
||||
D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking);
|
||||
|
||||
Assert.IsTrue(bytecode.Length > 0);
|
||||
}
|
||||
@@ -84,7 +84,7 @@ public partial class D2D1ShaderCompilerTests
|
||||
source,
|
||||
"Execute",
|
||||
D2D1ShaderProfile.PixelShader40Level93,
|
||||
D2D1ShaderCompilerOptions.Default);
|
||||
D2D1CompileOptions.Default);
|
||||
|
||||
Assert.IsTrue(bytecode.Length > 0);
|
||||
}
|
||||
@@ -111,7 +111,7 @@ public partial class D2D1ShaderCompilerTests
|
||||
source,
|
||||
"PSMain",
|
||||
D2D1ShaderProfile.PixelShader40Level93,
|
||||
D2D1ShaderCompilerOptions.Default);
|
||||
D2D1CompileOptions.Default);
|
||||
|
||||
Assert.IsTrue(bytecode.Length > 0);
|
||||
}
|
||||
@@ -158,7 +158,7 @@ public partial class D2D1ShaderCompilerTests
|
||||
source,
|
||||
"PSMain",
|
||||
D2D1ShaderProfile.PixelShader40,
|
||||
D2D1ShaderCompilerOptions.Default);
|
||||
D2D1CompileOptions.Default);
|
||||
|
||||
Assert.IsTrue(bytecode.Length > 0);
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする