diff --git a/ComputeSharp.sln b/ComputeSharp.sln
index 49cd9704..b8d2e49a 100644
--- a/ComputeSharp.sln
+++ b/ComputeSharp.sln
@@ -90,7 +90,7 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.Core.NetStanda
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Core.SourceGenerators", "src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj", "{BFB003D1-788B-48DE-8065-39479C162CB0}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.D2D1Interop.SourceGenerators", "src\ComputeSharp.D2D1Interop.SourceGenerators\ComputeSharp.D2D1Interop.SourceGenerators.csproj", "{E44053BD-A761-47FB-AA78-087A599672EA}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.D2D1Interop.SourceGenerators", "src\ComputeSharp.D2D1Interop.SourceGenerators\ComputeSharp.D2D1Interop.SourceGenerators.csproj", "{E44053BD-A761-47FB-AA78-087A599672EA}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
@@ -116,6 +116,8 @@ Global
src\ComputeSharp.UI\ComputeSharp.UI.projitems*{afaafdc9-be42-45d4-8c79-0ee8c8edbf52}*SharedItemsImports = 5
src\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems*{bfb003d1-788b-48de-8065-39479c162cb0}*SharedItemsImports = 5
src\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5
+ src\ComputeSharp.D2D1Interop.NetStandard\ComputeSharp.D2D1Interop.NetStandard.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5
+ src\TerraFX.Interop.Windows.D2D1Interop\TerraFX.Interop.Windows.D2D1Interop.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5
samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{e7c9247d-016b-4819-af9f-0e9b3591e04b}*SharedItemsImports = 5
samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{e91f4d24-922a-4921-a8d7-9c45a143c45d}*SharedItemsImports = 5
src\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems*{eb52cb67-4162-4722-874b-44ea8da5450e}*SharedItemsImports = 5
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj b/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj
index feb032ad..e97c6f0a 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ComputeSharp.D2D1Interop.SourceGenerators.csproj
@@ -63,6 +63,7 @@
+
@@ -71,11 +72,17 @@
+
+
+
+
+
+
@@ -90,4 +97,7 @@
+
+
+
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs
new file mode 100644
index 00000000..c89af0ca
--- /dev/null
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.D3DCompile.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Immutable;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+using System.Threading;
+using ComputeSharp.D2D1Interop.Exceptions;
+using ComputeSharp.D2D1Interop.Shaders.Translation;
+using ComputeSharp.D2D1Interop.SourceGenerators.Models;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using TerraFX.Interop.DirectX;
+using TerraFX.Interop.Windows;
+using static ComputeSharp.D2D1Interop.SourceGenerators.Diagnostics.DiagnosticDescriptors;
+
+namespace ComputeSharp.D2D1Interop.SourceGenerators;
+
+///
+partial class ID2D1ShaderGenerator
+{
+ ///
+ internal static partial class LoadBytecode
+ {
+ ///
+ /// Gets a instance with the logic to try to get a compiled shader bytecode.
+ ///
+ /// The generated HLSL source code (ignoring captured delegates, if present).
+ /// The used to cancel the operation, if needed.
+ /// The resulting diagnostic from the processing operation, if any.
+ /// The instance with the compiled shader bytecode.
+ public static unsafe ImmutableArray GetBytecode(
+ string hlslSource,
+ CancellationToken token,
+ out DiagnosticInfo? diagnostic)
+ {
+ ImmutableArray bytecode = ImmutableArray.Empty;
+
+ try
+ {
+ token.ThrowIfCancellationRequested();
+
+ // Compile the shader bytecode
+ using ComPtr dxcBlobBytecode = D2D1ShaderCompiler.Compile(hlslSource.AsSpan(), false);
+
+ token.ThrowIfCancellationRequested();
+
+ byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
+ int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
+
+ byte[] array = new ReadOnlySpan(buffer, length).ToArray();
+
+ bytecode = Unsafe.As>(ref array);
+ diagnostic = null;
+ }
+ catch (Win32Exception e)
+ {
+ diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message);
+ }
+ catch (FxcCompilationException e)
+ {
+ diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message);
+ }
+
+ return bytecode;
+ }
+ }
+}
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs
index f9af6bba..7cd13869 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.HlslSource.cs
@@ -249,7 +249,7 @@ partial class ID2D1ShaderGenerator
if (isShaderEntryPoint)
{
string entryPointDeclaration = processedMethod.NormalizeWhitespace(eol: "\n").ToFullString();
- string adjustedEntryPointDeclaration = entryPointDeclaration.Replace("float4 Execute()", "D2D_ENTRY(Execute)");
+ string adjustedEntryPointDeclaration = entryPointDeclaration.Replace("float4 Execute()", "D2D_PS_ENTRY(Execute)");
entryPoint = adjustedEntryPointDeclaration;
}
@@ -389,6 +389,14 @@ partial class ID2D1ShaderGenerator
AppendLineAndLF("// See: https://github.com/Sergio0694/ComputeSharp.");
AppendLF();
+ // TODO: switch to discovered ones
+ AppendLineAndLF("#define D2D_INPUT_COUNT 1");
+ AppendLineAndLF("#define D2D_INPUT0_COMPLEX");
+ AppendLineAndLF("#define D2D_REQUIRES_SCENE_POSITION");
+
+ // Add the "d2d1effecthelpers.hlsli" header
+ AppendLineAndLF("#include \"d2d1effecthelpers.hlsli\"");
+
// Define declarations
foreach (var (name, value) in definedConstants)
{
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
index 2fdd6b5b..2d8b31c8 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs
@@ -1,4 +1,9 @@
-using ComputeSharp.D2D1Interop.__Internals;
+using System;
+using System.Collections.Immutable;
+using ComputeSharp.__Internals;
+using ComputeSharp.D2D1Interop.__Internals;
+using ComputeSharp.D2D1Interop.SourceGenerators.Models;
+using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@@ -16,11 +21,21 @@ partial class ID2D1ShaderGenerator
///
/// Creates a instance for the TryGetBytecode method.
///
- /// The input HLSL source.
+ /// The input bytecode info.
+ /// An opaque instance to transform the final tree into text.
/// The resulting instance for the BuildHlslString method.
- public static MethodDeclarationSyntax GetSyntax(string hlslSource)
+ public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeInfo bytecodeInfo, out Func fixup)
{
- BlockSyntax block = GetShaderBytecodeBody(hlslSource);
+ BlockSyntax block = GetShaderBytecodeBody(bytecodeInfo, out string? bytecodeLiterals);
+
+ if (bytecodeLiterals is not null)
+ {
+ fixup = tree => tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals);
+ }
+ else
+ {
+ fixup = static tree => tree.ToFullString();
+ }
// This code produces a method declaration as follows:
//
@@ -45,21 +60,128 @@ partial class ID2D1ShaderGenerator
///
/// Gets a instance with the logic to try to get a compiled shader bytecode.
///
- /// The input HLSL source.
+ /// The input bytecode info.
+ /// The resulting bytecode literals to insert into the final source code.
/// The instance trying to retrieve the precompiled shader.
- private static unsafe BlockSyntax GetShaderBytecodeBody(string hlslSource)
+ private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, out string? bytecodeLiterals)
{
+ ImmutableArray.Builder statements = ImmutableArray.CreateBuilder();
+
// This code produces a branch as follows:
//
// hlslSource = ;
- return
- Block(ExpressionStatement(
+ statements.Add(
+ ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("hlslSource"),
LiteralExpression(
SyntaxKind.StringLiteralExpression,
- Literal(hlslSource)))));
+ Literal(bytecodeInfo.HlslSource)))));
+
+ // Load the bytecode literals, if available, otherwise emit the dynamic loading code
+ if (bytecodeInfo.Bytecode.IsEmpty)
+ {
+ bytecodeLiterals = null;
+
+ // This code produces a method declaration as follows:
+ //
+ // ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, hlslSource);
+ statements.Add(
+ ExpressionStatement(
+ InvocationExpression(
+ MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName("ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler"),
+ IdentifierName("LoadDynamicBytecode")))
+ .AddArgumentListArguments(
+ Argument(IdentifierName("loader")).WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
+ Argument(IdentifierName("hlslSource")))));
+ }
+ else
+ {
+ bytecodeLiterals = BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
+
+ // This code produces the following statements:
+ //
+ // global::System.ReadOnlySpan bytecode = new byte[] { __EMBEDDED_SHADER_BYTECODE };
+ //
+ // Note that the __EMBEDDED_SHADER_BYTECODE identifier will be replaced after the string
+ // is created by the generator. This greatly speeds up the code generation, as it avoids
+ // having to create/parse tens of thousands of literal expression nodes for every shader.
+ statements.Add(
+ LocalDeclarationStatement(
+ VariableDeclaration(
+ GenericName(Identifier("global::System.ReadOnlySpan"))
+ .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
+ .AddVariables(
+ VariableDeclarator(Identifier("bytecode"))
+ .WithInitializer(
+ EqualsValueClause(
+ ArrayCreationExpression(
+ ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
+ .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression()))))
+ .WithInitializer(
+ InitializerExpression(
+ SyntaxKind.ArrayInitializerExpression,
+ SingletonSeparatedList(IdentifierName("__EMBEDDED_SHADER_BYTECODE")))))))));
+
+ // loader.LoadEmbeddedBytecode(bytecode);
+ statements.Add(
+ ExpressionStatement(
+ InvocationExpression(
+ MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName("loader"),
+ IdentifierName("LoadEmbeddedBytecode")))
+ .AddArgumentListArguments(Argument(IdentifierName("bytecode")))));
+ }
+
+ return Block(statements);
+ }
+
+ ///
+ /// A mapping of precomputed literals for all values.
+ ///
+ private static readonly string[] formattedBytes =
+ {
+ "0x00", "0x01", "0x02", "0x03", "0x04", "0x05", "0x06", "0x07", "0x08", "0x09", "0x0A", "0x0B", "0x0C", "0x0D", "0x0E", "0x0F",
+ "0x10", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "0x17", "0x18", "0x19", "0x1A", "0x1B", "0x1C", "0x1D", "0x1E", "0x1F",
+ "0x20", "0x21", "0x22", "0x23", "0x24", "0x25", "0x26", "0x27", "0x28", "0x29", "0x2A", "0x2B", "0x2C", "0x2D", "0x2E", "0x2F",
+ "0x30", "0x31", "0x32", "0x33", "0x34", "0x35", "0x36", "0x37", "0x38", "0x39", "0x3A", "0x3B", "0x3C", "0x3D", "0x3E", "0x3F",
+ "0x40", "0x41", "0x42", "0x43", "0x44", "0x45", "0x46", "0x47", "0x48", "0x49", "0x4A", "0x4B", "0x4C", "0x4D", "0x4E", "0x4F",
+ "0x50", "0x51", "0x52", "0x53", "0x54", "0x55", "0x56", "0x57", "0x58", "0x59", "0x5A", "0x5B", "0x5C", "0x5D", "0x5E", "0x5F",
+ "0x60", "0x61", "0x62", "0x63", "0x64", "0x65", "0x66", "0x67", "0x68", "0x69", "0x6A", "0x6B", "0x6C", "0x6D", "0x6E", "0x6F",
+ "0x70", "0x71", "0x72", "0x73", "0x74", "0x75", "0x76", "0x77", "0x78", "0x79", "0x7A", "0x7B", "0x7C", "0x7D", "0x7E", "0x7F",
+ "0x80", "0x81", "0x82", "0x83", "0x84", "0x85", "0x86", "0x87", "0x88", "0x89", "0x8A", "0x8B", "0x8C", "0x8D", "0x8E", "0x8F",
+ "0x90", "0x91", "0x92", "0x93", "0x94", "0x95", "0x96", "0x97", "0x98", "0x99", "0x9A", "0x9B", "0x9C", "0x9D", "0x9E", "0x9F",
+ "0xA0", "0xA1", "0xA2", "0xA3", "0xA4", "0xA5", "0xA6", "0xA7", "0xA8", "0xA9", "0xAA", "0xAB", "0xAC", "0xAD", "0xAE", "0xAF",
+ "0xB0", "0xB1", "0xB2", "0xB3", "0xB4", "0xB5", "0xB6", "0xB7", "0xB8", "0xB9", "0xBA", "0xBB", "0xBC", "0xBD", "0xBE", "0xBF",
+ "0xC0", "0xC1", "0xC2", "0xC3", "0xC4", "0xC5", "0xC6", "0xC7", "0xC8", "0xC9", "0xCA", "0xCB", "0xCC", "0xCD", "0xCE", "0xCF",
+ "0xD0", "0xD1", "0xD2", "0xD3", "0xD4", "0xD5", "0xD6", "0xD7", "0xD8", "0xD9", "0xDA", "0xDB", "0xDC", "0xDD", "0xDE", "0xDF",
+ "0xE0", "0xE1", "0xE2", "0xE3", "0xE4", "0xE5", "0xE6", "0xE7", "0xE8", "0xE9", "0xEA", "0xEB", "0xEC", "0xED", "0xEE", "0xEF",
+ "0xF0", "0xF1", "0xF2", "0xF3", "0xF4", "0xF5", "0xF6", "0xF7", "0xF8", "0xF9", "0xFA", "0xFB", "0xFC", "0xFD", "0xFE", "0xFF"
+ };
+
+ ///
+ /// Creates a formatted expression to initialize a with a given shader data.
+ ///
+ /// The input shader bytecode to serialize.
+ /// A formatted with the serialized data.
+ private static string BuildShaderBytecodeExpressionString(ReadOnlySpan bytecode)
+ {
+ //The estimation is 4 characters per byte (up to "255" in hex), plus ", " to separate sequential values
+ using ArrayPoolStringBuilder builder = ArrayPoolStringBuilder.Create(bytecode.Length * 6);
+
+ builder.Append(formattedBytes[bytecode[0]]);
+
+ foreach (byte b in bytecode.Slice(1))
+ {
+ builder.Append(", ");
+ builder.Append(formattedBytes[b]);
+ }
+
+ return builder.WrittenSpan.ToString();
}
}
}
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs
index 3d048565..1587f2fe 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -1,4 +1,6 @@
-using System.Collections.Immutable;
+using System;
+using System.Collections.Immutable;
+using System.Linq;
using ComputeSharp.D2D1Interop.SourceGenerators.Diagnostics;
using ComputeSharp.D2D1Interop.SourceGenerators.Extensions;
using ComputeSharp.D2D1Interop.SourceGenerators.Models;
@@ -121,13 +123,54 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
.Select(static (item, token) => item.SourceInfo)
.WithComparer(HlslShaderSourceInfo.Comparer.Default);
- // Generate the LoadBytecode() methods
- context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
- {
- MethodDeclarationSyntax loadBytecodeMethod = LoadBytecode.GetSyntax(item.HlslSource);
- CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, loadBytecodeMethod, false);
+ // Compile the requested shader bytecodes
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
+ hlslSourceInfo
+ .Select(static (item, token) =>
+ {
+ ImmutableArray bytecode = LoadBytecode.GetBytecode(item.HlslSource, token, out DiagnosticInfo? diagnostic);
- context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(LoadBytecode)}", compilationUnit.ToFullString());
+ token.ThrowIfCancellationRequested();
+
+ EmbeddedBytecodeInfo bytecodeInfo = new(item.HlslSource, bytecode);
+
+ return (item.Hierarchy, bytecodeInfo, diagnostic);
+ });
+
+ // Gather the diagnostics
+ IncrementalValuesProvider> embeddedBytecodeDiagnostics =
+ embeddedBytecodeWithErrors
+ .Select(static (item, token) => (item.Hierarchy.MetadataName, item.Diagnostic))
+ .Where(static item => item.Diagnostic is not null)
+ .Combine(context.CompilationProvider)
+ .Select(static (item, token) =>
+ {
+ INamedTypeSymbol? typeSymbol = item.Right.GetTypeByMetadataName(item.Left.MetadataName)!;
+ Diagnostic diagnostic = Diagnostic.Create(
+ item.Left.Diagnostic!.Descriptor,
+ typeSymbol.Locations.FirstOrDefault(),
+ new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Args).ToArray());
+
+ return ImmutableArray.Create(diagnostic);
+ });
+
+ // Output the diagnostics
+ context.ReportDiagnostics(embeddedBytecodeDiagnostics);
+
+ // Get the filtered sequence to enable caching
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode =
+ embeddedBytecodeWithErrors
+ .Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo))
+ .WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default);
+
+ // Generate the LoadBytecode() methods
+ context.RegisterSourceOutput(embeddedBytecode, static (context, item) =>
+ {
+ MethodDeclarationSyntax loadBytecodeMethod = LoadBytecode.GetSyntax(item.BytecodeInfo, out Func fixup);
+ CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, loadBytecodeMethod, false);
+ string text = fixup(compilationUnit);
+
+ context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(LoadBytecode)}", text);
});
}
}
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs
index 91a8c2b4..5242cbd0 100644
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs
+++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Models/EmbeddedBytecodeInfo.cs
@@ -6,13 +6,11 @@ using ComputeSharp.D2D1Interop.SourceGenerators.Extensions;
namespace ComputeSharp.D2D1Interop.SourceGenerators.Models;
///
-/// A model representing a compiled shader bytecode, or none.
+/// A model representing a shared and its compiled bytecode, if available.
///
-/// The thread ids value for the X axis.
-/// The thread ids value for the Y axis.
-/// The thread ids value for the Z axis.
+/// The HLSL source for the shader.
/// The compiled shader bytecode, if available.
-internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray Bytecode)
+internal sealed record EmbeddedBytecodeInfo(string HlslSource, ImmutableArray Bytecode)
{
///
/// An implementation for .
@@ -43,9 +41,7 @@ internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray<
}
return
- x.X == y.X &&
- x.Y == y.Y &&
- x.Z == y.Z &&
+ x.HlslSource == y.HlslSource &&
x.Bytecode.SequenceEqual(y.Bytecode);
}
@@ -54,9 +50,7 @@ internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray<
{
HashCode hashCode = default;
- hashCode.Add(obj.X);
- hashCode.Add(obj.Y);
- hashCode.Add(obj.Z);
+ hashCode.Add(obj.HlslSource);
hashCode.AddBytes(obj.Bytecode.AsSpan());
return hashCode.ToHashCode();
diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs
deleted file mode 100644
index a914bf06..00000000
--- a/src/ComputeSharp.D2D1Interop.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-using System.ComponentModel;
-
-namespace System.Runtime.CompilerServices;
-
-///
-/// Reserved to be used by the compiler for tracking metadata.
-/// This class should not be used by developers in source code.
-///
-[EditorBrowsable(EditorBrowsableState.Never)]
-internal static class IsExternalInit
-{
-}
diff --git a/src/ComputeSharp.D2D1Interop/Intrinsics/D2D1.cs b/src/ComputeSharp.D2D1Interop/Intrinsics/D2D1.cs
index a0dca8fb..1054a413 100644
--- a/src/ComputeSharp.D2D1Interop/Intrinsics/D2D1.cs
+++ b/src/ComputeSharp.D2D1Interop/Intrinsics/D2D1.cs
@@ -1,4 +1,5 @@
-using ComputeSharp.Exceptions;
+using ComputeSharp.Core.Intrinsics.Attributes;
+using ComputeSharp.Exceptions;
namespace ComputeSharp.D2D1Interop;
@@ -14,6 +15,7 @@ public static class D2D1
/// The index of the input texture to get the input from.
/// The color from the target input at the current coordinate, in INPUTN format.
/// This method is only available for simple inputs.
+ [HlslIntrinsicName("D2DGetInput")]
public static Float4 GetInput(int index) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(GetInput)}({typeof(int)})");
///
@@ -22,6 +24,7 @@ public static class D2D1
/// The index of the input texture to get the coordinate for.
/// The input coordinate, in TEXCOORDN format.
/// This method is only available for complex inputs.
+ [HlslIntrinsicName("D2DGetInputCoordinate")]
public static Float4 GetInputCoordinate(int index) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(GetInputCoordinate)}({typeof(int)})");
///
@@ -29,6 +32,7 @@ public static class D2D1
///
/// The current scene position, in SCENE_POSITION format.
/// This method is only available when is used on the shader type.
+ [HlslIntrinsicName("D2DGetScenePosition")]
public static Float4 GetScenePosition() => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(GetScenePosition)}()");
///
@@ -38,6 +42,7 @@ public static class D2D1
/// The normalized coordinate to use to sample the texture.
/// The sampled value from the texture, in TEXCOORDN format.
/// This method is only available for complex inputs.
+ [HlslIntrinsicName("D2DSampleInput")]
public static Float4 SampleInput(int index, Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(SampleInput)}({typeof(int)}, {typeof(Float2)})");
///
@@ -47,6 +52,7 @@ public static class D2D1
/// The normalized offset to use to sample the texture.
/// The sampled value from the texture, in TEXCOORDN format.
/// This method is only available for simple inputs.
+ [HlslIntrinsicName("D2DSampleInputAtOffset")]
public static Float4 SampleInputAtOffset(int index, Float2 offset) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(SampleInputAtOffset)}({typeof(int)}, {typeof(Float2)})");
///
@@ -56,5 +62,6 @@ public static class D2D1
/// The normalized coordinate to use to sample the texture.
/// The sampled value from the texture, in TEXCOORDN format.
/// This method is only available for complex inputs.
+ [HlslIntrinsicName("D2DSampleInputAtPosition")]
public static Float4 SampleInputAtPosition(int index, Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(SampleInputAtPosition)}({typeof(int)}, {typeof(Float2)})");
}
diff --git a/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs b/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs
index e1e0ac8c..0d267a74 100644
--- a/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs
+++ b/src/ComputeSharp.D2D1Interop/Shaders/Translation/D2D1ShaderCompiler.ID3DInclude.cs
@@ -128,7 +128,11 @@ partial class D2D1ShaderCompiler
/// A pointer to a buffer with the contents of d2d1effecthelpers.hlsli.
private static void* LoadD2D1EffectHelpersHeader(out int size)
{
+#if SOURCE_GENERATOR
+ const string headerFilename = "ComputeSharp.D2D1Interop.SourceGenerators.ComputeSharp.D2D1Interop.Shaders.Translation.Headers.d2d1effecthelpers.hlsli";
+#else
const string headerFilename = "ComputeSharp.D2D1Interop.Shaders.Translation.Headers.d2d1effecthelpers.hlsli";
+#endif
using Stream source = Assembly.GetExecutingAssembly().GetManifestResourceStream(headerFilename)!;
using MemoryStream destination = new();
diff --git a/src/TerraFX.Interop.Windows.D2D1Interop/DirectX/um/d3dcommon/ID3DInclude.cs b/src/TerraFX.Interop.Windows.D2D1Interop/DirectX/um/d3dcommon/ID3DInclude.cs
index c62f93b2..8fd92f76 100644
--- a/src/TerraFX.Interop.Windows.D2D1Interop/DirectX/um/d3dcommon/ID3DInclude.cs
+++ b/src/TerraFX.Interop.Windows.D2D1Interop/DirectX/um/d3dcommon/ID3DInclude.cs
@@ -6,6 +6,8 @@
using System.Runtime.CompilerServices;
using TerraFX.Interop.Windows;
+#pragma warning disable CS0649
+
namespace TerraFX.Interop.DirectX
{
internal unsafe partial struct ID3DInclude