From 35a73051d7bba081106bb8c1fd88c01cd9aff2b1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Oct 2023 13:53:46 +0200 Subject: [PATCH] Fix marshalling code for global types, add tests --- .../Models/HierarchyInfo.cs | 11 +- .../D2D1PixelShaderTests.cs | 1936 +++++++++-------- 2 files changed, 1037 insertions(+), 910 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs index 50c8d05f..5127fbcd 100644 --- a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs @@ -118,9 +118,16 @@ internal sealed partial record HierarchyInfo(string FullyQualifiedMetadataName, using ImmutableArrayBuilder fullyQualifiedTypeName = new(); fullyQualifiedTypeName.AddRange("global::".AsSpan()); - fullyQualifiedTypeName.AddRange(Namespace.AsSpan()); - for (int i = Hierarchy.Length - 1; i >= 0; i--) + if (Namespace.Length > 0) + { + fullyQualifiedTypeName.AddRange(Namespace.AsSpan()); + fullyQualifiedTypeName.Add('.'); + } + + fullyQualifiedTypeName.AddRange(Hierarchy[^1].QualifiedName.AsSpan()); + + for (int i = Hierarchy.Length - 2; i >= 0; i--) { fullyQualifiedTypeName.Add('.'); fullyQualifiedTypeName.AddRange(Hierarchy[i].QualifiedName.AsSpan()); diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index bf40275e..2f2a43ff 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -1,41 +1,108 @@ using System; using System.Buffers; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using ComputeSharp; +using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using Microsoft.VisualStudio.TestTools.UnitTesting; -#pragma warning disable IDE0059, IDE0044 +#pragma warning disable IDE0059, IDE0161, IDE0044 -namespace ComputeSharp.D2D1.Tests; - -[TestClass] -[TestCategory("D2D1PixelShader")] -public partial class D2D1PixelShaderTests +[D2DInputCount(0)] +[D2DGeneratedPixelShaderDescriptor] +[AutoConstructor] +internal readonly partial struct ShaderWithScalarVectorAndMatrixTypes_Global : ID2D1PixelShader { - [TestMethod] - public void GetInputCount() + public readonly int x; + public readonly int y; + public readonly int z; + public readonly float2x3 f2x3; + public readonly int a; + public readonly int1x3 i1x3; + public readonly double2 d2; + public readonly int c; + public readonly int1x2 i1x2; + public readonly int2x2 i2x2; + public readonly int d; + + public float4 Execute() { - Assert.AreEqual(D2D1PixelShader.GetInputCount(), 1); - Assert.AreEqual(D2D1PixelShader.GetInputCount(), 1); - Assert.AreEqual(D2D1PixelShader.GetInputCount(), 0); - Assert.AreEqual(D2D1PixelShader.GetInputCount(), 7); + return 0; } +} - [TestMethod] - public void GetInputTypes_NoInputs() +[AutoConstructor] +public readonly partial struct FieldsContainer1_Global +{ + public readonly int1x2 i1x2; + public readonly int2x2 i2x2; + public readonly int d; +} + +[AutoConstructor] +public readonly partial struct FieldsContainer2_Global +{ + public readonly int z; + public readonly float2x3 f2x3; + public readonly int a; + public readonly FieldsContainer3_Global container3; +} + +[AutoConstructor] +public readonly partial struct FieldsContainer3_Global +{ + public readonly int1x3 i1x3; + public readonly double2 d2; +} + +[D2DInputCount(0)] +[D2DGeneratedPixelShaderDescriptor] +[AutoConstructor] +internal readonly partial struct ShaderWithScalarVectorAndMatrixTypesInNestedStructs_Global : ID2D1PixelShader +{ + public readonly int x; + public readonly int y; + public readonly FieldsContainer2_Global container2; + public readonly int c; + public readonly FieldsContainer1_Global container1; + + public float4 Execute() { - ReadOnlyMemory inputTypes = D2D1PixelShader.GetInputTypes(); - - Assert.AreEqual(0, inputTypes.Length); - Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(inputTypes, out MemoryManager? _)); + return 0; } +} - [TestMethod] - public void GetInputTypes_MultipleInputs() +namespace ComputeSharp.D2D1.Tests +{ + [TestClass] + [TestCategory("D2D1PixelShader")] + public partial class D2D1PixelShaderTests { - CollectionAssert.AreEqual(new[] + [TestMethod] + public void GetInputCount() { + Assert.AreEqual(D2D1PixelShader.GetInputCount(), 1); + Assert.AreEqual(D2D1PixelShader.GetInputCount(), 1); + Assert.AreEqual(D2D1PixelShader.GetInputCount(), 0); + Assert.AreEqual(D2D1PixelShader.GetInputCount(), 7); + } + + [TestMethod] + public void GetInputTypes_NoInputs() + { + ReadOnlyMemory inputTypes = D2D1PixelShader.GetInputTypes(); + + Assert.AreEqual(0, inputTypes.Length); + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(inputTypes, out MemoryManager? _)); + } + + [TestMethod] + public void GetInputTypes_MultipleInputs() + { + CollectionAssert.AreEqual(new[] + { D2D1PixelShaderInputType.Simple, D2D1PixelShaderInputType.Complex, D2D1PixelShaderInputType.Simple, @@ -44,969 +111,1022 @@ public partial class D2D1PixelShaderTests D2D1PixelShaderInputType.Complex, D2D1PixelShaderInputType.Simple }, D2D1PixelShader.GetInputTypes().ToArray()); - } - - [TestMethod] - public void GetInputTypes_SameManager() - { - ReadOnlyMemory inputTypes1 = D2D1PixelShader.GetInputTypes(); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(inputTypes1, out MemoryManager? memoryManager1)); - - ReadOnlyMemory inputTypes2 = D2D1PixelShader.GetInputTypes(); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(inputTypes2, out MemoryManager? memoryManager2)); - Assert.AreSame(memoryManager1, memoryManager2); - } - - [D2DInputCount(7)] - [D2DInputSimple(0)] - [D2DInputSimple(2)] - [D2DInputComplex(1)] - [D2DInputComplex(3)] - [D2DInputComplex(5)] - [D2DInputSimple(6)] - [D2DGeneratedPixelShaderDescriptor] - internal partial struct ShaderWithMultipleInputs : ID2D1PixelShader - { - public Float4 Execute() - { - return 0; } - } - [TestMethod] - public void GetOutputBufferPrecision() - { - Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.UInt16Normalized); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.UInt8NormalizedSrgb); - } - - [TestMethod] - public void GetOutputBufferChannelDepth() - { - Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Default); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Default); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Four); - Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.One); - } - - [D2DInputCount(0)] - [D2DGeneratedPixelShaderDescriptor] - internal partial struct EmptyShader : ID2D1PixelShader - { - public Float4 Execute() + [TestMethod] + public void GetInputTypes_SameManager() { - return 0; - } - } + ReadOnlyMemory inputTypes1 = D2D1PixelShader.GetInputTypes(); - [D2DInputCount(0)] - [D2DOutputBuffer(D2D1BufferPrecision.UInt16Normalized)] - [D2DGeneratedPixelShaderDescriptor] - internal partial struct OnlyBufferPrecisionShader : ID2D1PixelShader - { - public Float4 Execute() + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(inputTypes1, out MemoryManager? memoryManager1)); + + ReadOnlyMemory inputTypes2 = D2D1PixelShader.GetInputTypes(); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(inputTypes2, out MemoryManager? memoryManager2)); + Assert.AreSame(memoryManager1, memoryManager2); + } + + [D2DInputCount(7)] + [D2DInputSimple(0)] + [D2DInputSimple(2)] + [D2DInputComplex(1)] + [D2DInputComplex(3)] + [D2DInputComplex(5)] + [D2DInputSimple(6)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct ShaderWithMultipleInputs : ID2D1PixelShader { - return 0; + public Float4 Execute() + { + return 0; + } } - } - [D2DInputCount(0)] - [D2DOutputBuffer(D2D1ChannelDepth.Four)] - [D2DGeneratedPixelShaderDescriptor] - internal partial struct OnlyChannelDepthShader : ID2D1PixelShader - { - public Float4 Execute() + [TestMethod] + public void GetOutputBufferPrecision() { - return 0; + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.UInt16Normalized); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.Unknown); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision(), D2D1BufferPrecision.UInt8NormalizedSrgb); } - } - [D2DInputCount(0)] - [D2DOutputBuffer(D2D1BufferPrecision.UInt8NormalizedSrgb, D2D1ChannelDepth.One)] - [D2DGeneratedPixelShaderDescriptor] - internal partial struct CustomBufferOutputShader : ID2D1PixelShader - { - public Float4 Execute() + [TestMethod] + public void GetOutputBufferChannelDepth() { - return 0; + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Default); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Default); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.Four); + Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth(), D2D1ChannelDepth.One); } - } - [TestMethod] - public void GetInputDescriptions_Empty() - { - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); - } - - [TestMethod] - public void GetInputDescriptions_Custom() - { - ReadOnlyMemory inputDescriptions = D2D1PixelShader.GetInputDescriptions(); - - Assert.AreEqual(inputDescriptions.Length, 5); - - ReadOnlySpan span = inputDescriptions.Span; - - Assert.AreEqual(span[0].Index, 0); - Assert.AreEqual(span[0].Filter, D2D1Filter.MinPointMagLinearMipPoint); - Assert.AreEqual(span[0].LevelOfDetailCount, 0); - - Assert.AreEqual(span[1].Index, 1); - Assert.AreEqual(span[1].Filter, D2D1Filter.Anisotropic); - Assert.AreEqual(span[1].LevelOfDetailCount, 0); - - Assert.AreEqual(span[2].Index, 2); - Assert.AreEqual(span[2].Filter, D2D1Filter.MinLinearMagPointMinLinear); - Assert.AreEqual(span[2].LevelOfDetailCount, 4); - - Assert.AreEqual(span[3].Index, 5); - Assert.AreEqual(span[3].Filter, D2D1Filter.MinMagPointMipLinear); - Assert.AreEqual(span[3].LevelOfDetailCount, 0); - - Assert.AreEqual(span[4].Index, 6); - Assert.AreEqual(span[4].Filter, D2D1Filter.MinPointMagMipLinear); - Assert.AreEqual(span[4].LevelOfDetailCount, 3); - } - - [D2DInputCount(7)] - [D2DInputSimple(0)] - [D2DInputSimple(2)] - [D2DInputComplex(1)] - [D2DInputComplex(3)] - [D2DInputComplex(5)] - [D2DInputSimple(6)] - [D2DInputDescription(0, D2D1Filter.MinPointMagLinearMipPoint)] - [D2DInputDescription(1, D2D1Filter.Anisotropic)] - [D2DInputDescription(2, D2D1Filter.MinLinearMagPointMinLinear, LevelOfDetailCount = 4)] - [D2DInputDescription(5, D2D1Filter.MinMagPointMipLinear)] - [D2DInputDescription(6, D2D1Filter.MinPointMagMipLinear, LevelOfDetailCount = 3)] - [D2DGeneratedPixelShaderDescriptor] - internal partial struct ShaderWithInputDescriptions : ID2D1PixelShader - { - public Float4 Execute() + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct EmptyShader : ID2D1PixelShader { - return 0; + public Float4 Execute() + { + return 0; + } } - } - [TestMethod] - public void GetResourceTextureDescriptions_Empty() - { - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - } - - [TestMethod] - public void GetResourceTextureDescriptions_Custom() - { - ReadOnlyMemory resourceTextureDescriptions = D2D1PixelShader.GetResourceTextureDescriptions(); - - Assert.AreEqual(resourceTextureDescriptions.Length, 4); - - ReadOnlySpan span = resourceTextureDescriptions.Span; - - Assert.AreEqual(span[0].Index, 4); - Assert.AreEqual(span[0].Dimensions, 1); - - Assert.AreEqual(span[1].Index, 5); - Assert.AreEqual(span[1].Dimensions, 2); - - Assert.AreEqual(span[2].Index, 6); - Assert.AreEqual(span[2].Dimensions, 3); - - Assert.AreEqual(span[3].Index, 7); - Assert.AreEqual(span[3].Dimensions, 2); - } - - [TestMethod] - public void GetResourceTextureCount_IsCorrect() - { - Assert.AreEqual(0, D2D1PixelShader.GetResourceTextureCount()); - Assert.AreEqual(1, D2D1PixelShader.GetResourceTextureCount()); - Assert.AreEqual(4, D2D1PixelShader.GetResourceTextureCount()); - } - - [D2DInputCount(4)] - [D2DInputSimple(0)] - [D2DInputSimple(2)] - [D2DInputComplex(1)] - [D2DInputComplex(3)] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] - [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal partial struct ShaderWithoutResourceTextures : ID2D1PixelShader - { - public Float4 Execute() + [D2DInputCount(0)] + [D2DOutputBuffer(D2D1BufferPrecision.UInt16Normalized)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct OnlyBufferPrecisionShader : ID2D1PixelShader { - return 0; + public Float4 Execute() + { + return 0; + } } - } - [D2DInputCount(0)] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] - [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader - { - [D2DResourceTextureIndex(0)] - D2D1ResourceTexture1D myTexture; - - public Float4 Execute() + [D2DInputCount(0)] + [D2DOutputBuffer(D2D1ChannelDepth.Four)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct OnlyChannelDepthShader : ID2D1PixelShader { - return this.myTexture.Sample(0); + public Float4 Execute() + { + return 0; + } } - } - [D2DInputCount(4)] - [D2DInputSimple(0)] - [D2DInputSimple(2)] - [D2DInputComplex(1)] - [D2DInputComplex(3)] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] - [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal partial struct ShaderWithResourceTextures : ID2D1PixelShader - { - float number; - - [D2DResourceTextureIndex(4)] - D2D1ResourceTexture1D myTexture1; - - [D2DResourceTextureIndex(5)] - D2D1ResourceTexture2D myTexture2; - - [D2DResourceTextureIndex(6)] - D2D1ResourceTexture3D myTexture3; - - [D2DResourceTextureIndex(7)] - D2D1ResourceTexture2D myTexture4; - - public Float4 Execute() + [D2DInputCount(0)] + [D2DOutputBuffer(D2D1BufferPrecision.UInt8NormalizedSrgb, D2D1ChannelDepth.One)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct CustomBufferOutputShader : ID2D1PixelShader { - float4 pixel1 = this.myTexture1[0]; - float4 pixel2 = this.myTexture1.Sample(0.4f); - float4 pixel3 = this.myTexture2[0, 1]; - float4 pixel4 = this.myTexture2.Sample(0.4f, 0.3f); - float4 pixel5 = this.myTexture3[0, 1, 2]; - float4 pixel6 = this.myTexture3.Sample(0.4f, 0.5f, 0.6f); - float number1 = this.myTexture4[0, 2]; - float number2 = this.myTexture4.Sample(0.4f, 0.6f); - float number3 = this.number; - - return 0; + public Float4 Execute() + { + return 0; + } } - } - [TestMethod] - public void GetBytecode_FromDynamicBytecode() - { - ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); - - Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? _)); - Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment segment)); - Assert.AreEqual(0, segment.Offset); - Assert.IsTrue(segment.Count > 0); - } - - [D2DInputCount(0)] - [D2DGeneratedPixelShaderDescriptor] - internal readonly partial struct ShaderWithoutEmbeddedBytecode : ID2D1PixelShader - { - public float4 Execute() + [TestMethod] + public void GetInputDescriptions_Empty() { - return 0; + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetInputDescriptions().Length, 0); } - } - [TestMethod] - public void GetBytecode_FromEmbeddedBytecode() - { - // Bytecode with no parameters - ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); - Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - - // Matching shader profile - bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - - // Matching compile options - bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - - // Matching shader profile and compile options - bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - - // Incorrect profile - bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader50); - - Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? _)); - Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment segment)); - Assert.AreEqual(0, segment.Offset); - Assert.IsTrue(segment.Count > 0); - - // Incorrect options - bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableStrictness); - - Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? _)); - Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out segment)); - Assert.AreEqual(0, segment.Offset); - Assert.IsTrue(segment.Count > 0); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void GetBytecode_FromEmbeddedBytecode_WithPackMatrixColumnMajor() - { - _ = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void GetBytecode_FromEmbeddedBytecode_WithTargetProfileAndPackMatrixColumnMajor() - { - _ = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor); - } - - [D2DInputCount(3)] - [D2DInputSimple(0)] - [D2DInputSimple(1)] - [D2DInputSimple(2)] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] - [D2DGeneratedPixelShaderDescriptor] - internal readonly partial struct ShaderWithEmbeddedBytecode : ID2D1PixelShader - { - public float4 Execute() + [TestMethod] + public void GetInputDescriptions_Custom() { - float4 dst = D2D.GetInput(0); - float4 src = D2D.GetInput(1); - float4 srcMask = D2D.GetInput(2); - float4 result = ((1 - srcMask) * dst) + (srcMask * src); + ReadOnlyMemory inputDescriptions = D2D1PixelShader.GetInputDescriptions(); - return result; + Assert.AreEqual(inputDescriptions.Length, 5); + + ReadOnlySpan span = inputDescriptions.Span; + + Assert.AreEqual(span[0].Index, 0); + Assert.AreEqual(span[0].Filter, D2D1Filter.MinPointMagLinearMipPoint); + Assert.AreEqual(span[0].LevelOfDetailCount, 0); + + Assert.AreEqual(span[1].Index, 1); + Assert.AreEqual(span[1].Filter, D2D1Filter.Anisotropic); + Assert.AreEqual(span[1].LevelOfDetailCount, 0); + + Assert.AreEqual(span[2].Index, 2); + Assert.AreEqual(span[2].Filter, D2D1Filter.MinLinearMagPointMinLinear); + Assert.AreEqual(span[2].LevelOfDetailCount, 4); + + Assert.AreEqual(span[3].Index, 5); + Assert.AreEqual(span[3].Filter, D2D1Filter.MinMagPointMipLinear); + Assert.AreEqual(span[3].LevelOfDetailCount, 0); + + Assert.AreEqual(span[4].Index, 6); + Assert.AreEqual(span[4].Filter, D2D1Filter.MinPointMagMipLinear); + Assert.AreEqual(span[4].LevelOfDetailCount, 3); } - } - [TestMethod] - public void GetBytecode_FromEmbeddedBytecode_WithCompileOptions() - { - // Bytecode with no parameters - ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); - Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - - // Bytecode with all output parameters - ReadOnlyMemory bytecode2 = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); - - // The bytecode is the same - Assert.IsTrue(bytecode.Span == bytecode2.Span); - Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader40Level91); - Assert.AreEqual(compileOptions, D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PackMatrixRowMajor); - - // Matching compile options - bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - - // Matching shader profile and compile options - bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); - - Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); - Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); - } - - [D2DInputCount(3)] - [D2DInputSimple(0)] - [D2DInputSimple(1)] - [D2DInputSimple(2)] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] - [D2DCompileOptions(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2)] - [D2DGeneratedPixelShaderDescriptor] - internal readonly partial struct ShaderWithEmbeddedBytecodeAndCompileOptions : ID2D1PixelShader - { - public float4 Execute() + [D2DInputCount(7)] + [D2DInputSimple(0)] + [D2DInputSimple(2)] + [D2DInputComplex(1)] + [D2DInputComplex(3)] + [D2DInputComplex(5)] + [D2DInputSimple(6)] + [D2DInputDescription(0, D2D1Filter.MinPointMagLinearMipPoint)] + [D2DInputDescription(1, D2D1Filter.Anisotropic)] + [D2DInputDescription(2, D2D1Filter.MinLinearMagPointMinLinear, LevelOfDetailCount = 4)] + [D2DInputDescription(5, D2D1Filter.MinMagPointMipLinear)] + [D2DInputDescription(6, D2D1Filter.MinPointMagMipLinear, LevelOfDetailCount = 3)] + [D2DGeneratedPixelShaderDescriptor] + internal partial struct ShaderWithInputDescriptions : ID2D1PixelShader { - float4 dst = D2D.GetInput(0); - float4 src = D2D.GetInput(1); - float4 srcMask = D2D.GetInput(2); - float4 result = ((1 - srcMask) * dst) + (srcMask * src); - - return result; + public Float4 Execute() + { + return 0; + } } - } - [TestMethod] - public void LoadBytecode_VerifyEffectiveValues_ExplicitShaderProfile() - { - _ = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); - - Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader40Level91); - Assert.AreEqual(compileOptions, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); - - _ = D2D1PixelShader.LoadBytecode(out shaderProfile, out compileOptions); - - // This shader has a complex input, so it can't support shader linking - Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader40Level91); - Assert.AreEqual(compileOptions, D2D1CompileOptions.Default); - } - - [D2DInputCount(1)] - [D2DInputSimple(0)] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] - [D2DGeneratedPixelShaderDescriptor] - internal readonly partial struct SimpleShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader - { - public float4 Execute() + [TestMethod] + public void GetResourceTextureDescriptions_Empty() { - return D2D.GetInput(0); - } - } + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); - [D2DInputCount(1)] - [D2DInputComplex(0)] - [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] - [D2DGeneratedPixelShaderDescriptor] - internal readonly partial struct ComplexShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader - { - public float4 Execute() + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + Assert.AreEqual(D2D1PixelShader.GetResourceTextureDescriptions().Length, 0); + } + + [TestMethod] + public void GetResourceTextureDescriptions_Custom() { - return D2D.GetInput(0); + ReadOnlyMemory resourceTextureDescriptions = D2D1PixelShader.GetResourceTextureDescriptions(); + + Assert.AreEqual(resourceTextureDescriptions.Length, 4); + + ReadOnlySpan span = resourceTextureDescriptions.Span; + + Assert.AreEqual(span[0].Index, 4); + Assert.AreEqual(span[0].Dimensions, 1); + + Assert.AreEqual(span[1].Index, 5); + Assert.AreEqual(span[1].Dimensions, 2); + + Assert.AreEqual(span[2].Index, 6); + Assert.AreEqual(span[2].Dimensions, 3); + + Assert.AreEqual(span[3].Index, 7); + Assert.AreEqual(span[3].Dimensions, 2); } - } - [TestMethod] - public void LoadBytecode_VerifyEffectiveValues_ExplicitCompileOptions() - { - _ = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); - - // PS5.0 is the default, when no value is specified - Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader50); - Assert.AreEqual(compileOptions, D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.PackMatrixRowMajor); - - _ = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40, out compileOptions); - - Assert.AreEqual(compileOptions, D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.PackMatrixRowMajor); - } - - [D2DInputCount(1)] - [D2DInputSimple(0)] - [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.IeeeStrictness)] - [D2DGeneratedPixelShaderDescriptor] - internal readonly partial struct ShaderWithExplicitCompileOptionsAndNoShaderProfile : ID2D1PixelShader - { - public float4 Execute() + [TestMethod] + public void GetResourceTextureCount_IsCorrect() { - return D2D.GetInput(0); + Assert.AreEqual(0, D2D1PixelShader.GetResourceTextureCount()); + Assert.AreEqual(1, D2D1PixelShader.GetResourceTextureCount()); + Assert.AreEqual(4, D2D1PixelShader.GetResourceTextureCount()); } - } - [TestMethod] - public void GetConstantBufferSize_Empty() - { - int size = D2D1PixelShader.GetConstantBufferSize(); - - Assert.AreEqual(size, 0); - - // Repeated calls always return the same result - size = D2D1PixelShader.GetConstantBufferSize(); - - Assert.AreEqual(size, 0); - } - - [TestMethod] - public void GetConstantBuffer_Empty_ReadOnlyMemory() - { - ShaderWithNoCapturedValues shader = default; - - ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); - - Assert.AreEqual(constantBuffer.Length, 0); - } - - // See https://github.com/Sergio0694/ComputeSharp/issues/323 - [TestMethod] - public void GetConstantBuffer_Empty_Span() - { - ShaderWithNoCapturedValues shader = default; - - byte[] buffer = Array.Empty(); - - int bytesWritten = D2D1PixelShader.GetConstantBuffer(in shader, buffer); - - Assert.AreEqual(bytesWritten, 0); - } - - [D2DInputCount(1)] - [D2DInputSimple(0)] - [D2DInputDescription(0, D2D1Filter.MinMagMipPoint)] - [D2DPixelOptions(D2D1PixelOptions.TrivialSampling)] - [D2DGeneratedPixelShaderDescriptor] - internal readonly partial struct ShaderWithNoCapturedValues : ID2D1PixelShader - { - public float4 Execute() + [D2DInputCount(4)] + [D2DInputSimple(0)] + [D2DInputSimple(2)] + [D2DInputComplex(1)] + [D2DInputComplex(3)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] + internal partial struct ShaderWithoutResourceTextures : ID2D1PixelShader { - float4 color = D2D.GetInput(0); - - return color * color; + public Float4 Execute() + { + return 0; + } } - } - [TestMethod] - public void GetConstantBufferSize() - { - int size = D2D1PixelShader.GetConstantBufferSize(); - - Assert.AreEqual(size, 124); - - // Same as above: repeated calls always return the same result - size = D2D1PixelShader.GetConstantBufferSize(); - - Assert.AreEqual(size, 124); - } - - [TestMethod] - public void GetConstantBuffer_RoundTrip() - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); - - ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); - - ShaderWithScalarVectorAndMatrixTypes copy = D2D1PixelShader.CreateFromConstantBuffer(constantBuffer.Span); - - ValidateShadersAreEqual(in shader, in copy); - } - - [TestMethod] - public void GetConstantBuffer_WithNestedStructs_RoundTrip() - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); - - ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); - - ShaderWithScalarVectorAndMatrixTypesInNestedStructs copy = D2D1PixelShader.CreateFromConstantBuffer(constantBuffer.Span); - - ValidateShadersAreEqual(in shader, in copy); - } - - [TestMethod] - public void GetConstantBuffer_TryCreate_RoundTrip() - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); - - ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); - - Assert.IsTrue(D2D1PixelShader.TryCreateFromConstantBuffer(constantBuffer.Span, out ShaderWithScalarVectorAndMatrixTypes copy)); - - ValidateShadersAreEqual(in shader, in copy); - } - - [TestMethod] - public void GetConstantBuffer_WithNestedStructs_TryCreate_RoundTrip() - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); - - ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); - - Assert.IsTrue(D2D1PixelShader.TryCreateFromConstantBuffer(constantBuffer.Span, out ShaderWithScalarVectorAndMatrixTypesInNestedStructs copy)); - - ValidateShadersAreEqual(in shader, in copy); - } - - [TestMethod] - [DataRow(0)] - [DataRow(4)] - [DataRow(27)] - [ExpectedException(typeof(ArgumentException))] - public void GetConstantBuffer_BufferTooShort(int size) - { - byte[] buffer = new byte[size]; - - _ = D2D1PixelShader.CreateFromConstantBuffer(buffer); - - Assert.Fail(); - } - - [TestMethod] - [DataRow(0)] - [DataRow(4)] - [DataRow(27)] - public void GetConstantBuffer_TryCreate_BufferTooShort(int size) - { - byte[] buffer = new byte[size]; - - Assert.IsFalse(D2D1PixelShader.TryCreateFromConstantBuffer(buffer, out ShaderWithScalarVectorAndMatrixTypes _)); - } - - [TestMethod] - public void GetConstantBuffer_ReadOnlyMemory() - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); - - ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); - - ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(constantBuffer.Span); - } - - [TestMethod] - public void GetConstantBuffer_Span() - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); - - byte[] buffer = new byte[1024]; - - int bytesWritten = D2D1PixelShader.GetConstantBuffer(in shader, buffer); - - Assert.IsTrue(bytesWritten > 0); - Assert.IsTrue(bytesWritten <= buffer.Length); - - ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten)); - } - - [TestMethod] - [DataRow(0)] - [DataRow(4)] - [DataRow(27)] - [ExpectedException(typeof(ArgumentException))] - public void GetConstantBuffer_Span_DestinationTooShort(int size) - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); - - byte[] buffer = new byte[size]; - - _ = D2D1PixelShader.GetConstantBuffer(in shader, buffer); - - Assert.Fail(); - } - - [TestMethod] - public void GetConstantBuffer_TryGetSpan() - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); - - byte[] buffer = new byte[1024]; - - bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten); - - Assert.IsTrue(success); - Assert.IsTrue(bytesWritten > 0); - Assert.IsTrue(bytesWritten <= buffer.Length); - - ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten)); - } - - [TestMethod] - [DataRow(0)] - [DataRow(4)] - [DataRow(27)] - public void GetConstantBuffer_TryGetSpan_DestinationTooShort(int size) - { - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); - - byte[] buffer = new byte[size]; - - bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten); - - Assert.IsFalse(success); - Assert.AreEqual(bytesWritten, 0); - } - - // Gets a ShaderWithScalarVectorAndMatrixTypes instance with test values - private static void GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader) - { - shader = new ShaderWithScalarVectorAndMatrixTypes( - x: 111, - y: 222, - z: 333, - f2x3: new(55, 44, 888, 111, 222, 333), - a: 22, - i1x3: new(1, 2, 3), - d2: new(3.14, 6.28), - c: 42, - i1x2: new(111, 222), - i2x2: new(11, 22, 33, 44), - d: 9999); - } - - // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypes instance - private unsafe void ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(ReadOnlySpan span) - { - Assert.AreEqual(31 * sizeof(uint), span.Length); - - fixed (byte* buffer = span) + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] + internal partial struct ShaderWithJustOneResourceTextures : ID2D1PixelShader { - Assert.AreEqual(111, *(int*)&buffer[0]); - Assert.AreEqual(222, *(int*)&buffer[4]); - Assert.AreEqual(333, *(int*)&buffer[8]); - Assert.AreEqual(55, *(float*)&buffer[16]); - Assert.AreEqual(44, *(float*)&buffer[20]); - Assert.AreEqual(888, *(float*)&buffer[24]); - Assert.AreEqual(111, *(float*)&buffer[32]); - Assert.AreEqual(222, *(float*)&buffer[36]); - Assert.AreEqual(333, *(float*)&buffer[40]); - Assert.AreEqual(22, *(int*)&buffer[44]); - Assert.AreEqual(1, *(int*)&buffer[48]); - Assert.AreEqual(2, *(int*)&buffer[52]); - Assert.AreEqual(3, *(int*)&buffer[56]); - Assert.AreEqual(3.14, *(double*)&buffer[64]); - Assert.AreEqual(6.28, *(double*)&buffer[72]); - Assert.AreEqual(42, *(int*)&buffer[80]); - Assert.AreEqual(111, *(int*)&buffer[84]); - Assert.AreEqual(222, *(int*)&buffer[88]); - Assert.AreEqual(11, *(int*)&buffer[96]); - Assert.AreEqual(22, *(int*)&buffer[100]); - Assert.AreEqual(33, *(int*)&buffer[112]); - Assert.AreEqual(44, *(int*)&buffer[116]); - Assert.AreEqual(9999, *(int*)&buffer[120]); + [D2DResourceTextureIndex(0)] + D2D1ResourceTexture1D myTexture; + + public Float4 Execute() + { + return this.myTexture.Sample(0); + } } - } - // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypes instance - private static void ValidateShadersAreEqual(in ShaderWithScalarVectorAndMatrixTypes left, in ShaderWithScalarVectorAndMatrixTypes right) - { - Assert.AreEqual(left.x, right.x); - Assert.AreEqual(left.y, right.y); - Assert.AreEqual(left.z, right.z); - Assert.AreEqual(left.f2x3.M11, right.f2x3.M11); - Assert.AreEqual(left.f2x3.M12, right.f2x3.M12); - Assert.AreEqual(left.f2x3.M13, right.f2x3.M13); - Assert.AreEqual(left.f2x3.M21, right.f2x3.M21); - Assert.AreEqual(left.f2x3.M22, right.f2x3.M22); - Assert.AreEqual(left.f2x3.M23, right.f2x3.M23); - Assert.AreEqual(left.i1x3.M11, right.i1x3.M11); - Assert.AreEqual(left.i1x3.M12, right.i1x3.M12); - Assert.AreEqual(left.i1x3.M13, right.i1x3.M13); - Assert.AreEqual(left.d2.X, right.d2.X); - Assert.AreEqual(left.d2.Y, right.d2.Y); - Assert.AreEqual(left.c, right.c); - Assert.AreEqual(left.i1x2.M11, right.i1x2.M11); - Assert.AreEqual(left.i1x2.M12, right.i1x2.M12); - Assert.AreEqual(left.i2x2.M11, right.i2x2.M11); - Assert.AreEqual(left.i2x2.M12, right.i2x2.M12); - Assert.AreEqual(left.i2x2.M21, right.i2x2.M21); - Assert.AreEqual(left.i2x2.M22, right.i2x2.M22); - Assert.AreEqual(left.d, right.d); - } - - [D2DInputCount(0)] - [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal readonly partial struct ShaderWithScalarVectorAndMatrixTypes : ID2D1PixelShader - { - public readonly int x; - public readonly int y; - public readonly int z; - public readonly float2x3 f2x3; - public readonly int a; - public readonly Int1x3 i1x3; - public readonly double2 d2; - public readonly int c; - public readonly Int1x2 i1x2; - public readonly int2x2 i2x2; - public readonly int d; - - public float4 Execute() + [D2DInputCount(4)] + [D2DInputSimple(0)] + [D2DInputSimple(2)] + [D2DInputComplex(1)] + [D2DInputComplex(3)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] + internal partial struct ShaderWithResourceTextures : ID2D1PixelShader { - return 0; + float number; + + [D2DResourceTextureIndex(4)] + D2D1ResourceTexture1D myTexture1; + + [D2DResourceTextureIndex(5)] + D2D1ResourceTexture2D myTexture2; + + [D2DResourceTextureIndex(6)] + D2D1ResourceTexture3D myTexture3; + + [D2DResourceTextureIndex(7)] + D2D1ResourceTexture2D myTexture4; + + public Float4 Execute() + { + float4 pixel1 = this.myTexture1[0]; + float4 pixel2 = this.myTexture1.Sample(0.4f); + float4 pixel3 = this.myTexture2[0, 1]; + float4 pixel4 = this.myTexture2.Sample(0.4f, 0.3f); + float4 pixel5 = this.myTexture3[0, 1, 2]; + float4 pixel6 = this.myTexture3.Sample(0.4f, 0.5f, 0.6f); + float number1 = this.myTexture4[0, 2]; + float number2 = this.myTexture4.Sample(0.4f, 0.6f); + float number3 = this.number; + + return 0; + } } - } - [TestMethod] - [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] - public unsafe void SetConstantBufferForD2D1DrawInfo_NullD2D1DrawInfo() - { - D2D1PixelShader.SetConstantBufferForD2D1DrawInfo(null, default); - } - - [TestMethod] - public unsafe void GetConstantBufferSize_WithNestedStructs() - { - Assert.AreEqual(156, D2D1PixelShader.GetConstantBufferSize()); - - GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); - - ReadOnlyMemory memory = D2D1PixelShader.GetConstantBuffer(in shader); - - Assert.AreEqual(156, memory.Length); - - fixed (byte* buffer = memory.Span) + [TestMethod] + public void GetBytecode_FromDynamicBytecode() { - Assert.AreEqual(111, *(int*)&buffer[0]); - Assert.AreEqual(222, *(int*)&buffer[4]); - Assert.AreEqual(333, *(int*)&buffer[16]); - Assert.AreEqual(55, *(float*)&buffer[32]); - Assert.AreEqual(44, *(float*)&buffer[36]); - Assert.AreEqual(888, *(float*)&buffer[40]); - Assert.AreEqual(111, *(float*)&buffer[48]); - Assert.AreEqual(222, *(float*)&buffer[52]); - Assert.AreEqual(333, *(float*)&buffer[56]); - Assert.AreEqual(22, *(int*)&buffer[60]); - Assert.AreEqual(1, *(int*)&buffer[64]); - Assert.AreEqual(2, *(int*)&buffer[68]); - Assert.AreEqual(3, *(int*)&buffer[72]); - Assert.AreEqual(3.14, *(double*)&buffer[80]); - Assert.AreEqual(6.28, *(double*)&buffer[88]); - Assert.AreEqual(42, *(int*)&buffer[96]); - Assert.AreEqual(111, *(int*)&buffer[112]); - Assert.AreEqual(222, *(int*)&buffer[116]); - Assert.AreEqual(11, *(int*)&buffer[128]); - Assert.AreEqual(22, *(int*)&buffer[132]); - Assert.AreEqual(33, *(int*)&buffer[144]); - Assert.AreEqual(44, *(int*)&buffer[148]); - Assert.AreEqual(9999, *(int*)&buffer[152]); + ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); + + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? _)); + Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment segment)); + Assert.AreEqual(0, segment.Offset); + Assert.IsTrue(segment.Count > 0); } - } - [AutoConstructor] - public readonly partial struct FieldsContainer1 - { - public readonly Int1x2 i1x2; - public readonly int2x2 i2x2; - public readonly int d; - } - - [AutoConstructor] - public readonly partial struct FieldsContainer2 - { - public readonly int z; - public readonly float2x3 f2x3; - public readonly int a; - public readonly FieldsContainer3 container3; - } - - [AutoConstructor] - public readonly partial struct FieldsContainer3 - { - public readonly Int1x3 i1x3; - public readonly double2 d2; - } - - [D2DInputCount(0)] - [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - internal readonly partial struct ShaderWithScalarVectorAndMatrixTypesInNestedStructs : ID2D1PixelShader - { - public readonly int x; - public readonly int y; - public readonly FieldsContainer2 container2; - public readonly int c; - public readonly FieldsContainer1 container1; - - public float4 Execute() + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ShaderWithoutEmbeddedBytecode : ID2D1PixelShader { - return 0; + public float4 Execute() + { + return 0; + } } - } - // Gets a ShaderWithScalarVectorAndMatrixTypesInNestedStructs instance with test values - private static void GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader) - { - shader = new ShaderWithScalarVectorAndMatrixTypesInNestedStructs( - x: 111, - y: 222, - container2: new FieldsContainer2( + [TestMethod] + public void GetBytecode_FromEmbeddedBytecode() + { + // Bytecode with no parameters + ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + + // Matching shader profile + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + + // Matching compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + + // Matching shader profile and compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + + // Incorrect profile + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader50); + + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? _)); + Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out ArraySegment segment)); + Assert.AreEqual(0, segment.Offset); + Assert.IsTrue(segment.Count > 0); + + // Incorrect options + bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.EnableStrictness); + + Assert.IsFalse(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? _)); + Assert.IsTrue(MemoryMarshal.TryGetArray(bytecode, out segment)); + Assert.AreEqual(0, segment.Offset); + Assert.IsTrue(segment.Count > 0); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void GetBytecode_FromEmbeddedBytecode_WithPackMatrixColumnMajor() + { + _ = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void GetBytecode_FromEmbeddedBytecode_WithTargetProfileAndPackMatrixColumnMajor() + { + _ = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor); + } + + [D2DInputCount(3)] + [D2DInputSimple(0)] + [D2DInputSimple(1)] + [D2DInputSimple(2)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ShaderWithEmbeddedBytecode : ID2D1PixelShader + { + public float4 Execute() + { + float4 dst = D2D.GetInput(0); + float4 src = D2D.GetInput(1); + float4 srcMask = D2D.GetInput(2); + float4 result = ((1 - srcMask) * dst) + (srcMask * src); + + return result; + } + } + + [TestMethod] + public void GetBytecode_FromEmbeddedBytecode_WithCompileOptions() + { + // Bytecode with no parameters + ReadOnlyMemory bytecode = D2D1PixelShader.LoadBytecode(); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager? manager)); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + + // Bytecode with all output parameters + ReadOnlyMemory bytecode2 = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); + + // The bytecode is the same + Assert.IsTrue(bytecode.Span == bytecode2.Span); + Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader40Level91); + Assert.AreEqual(compileOptions, D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PackMatrixRowMajor); + + // Matching compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + + // Matching shader profile and compile options + bytecode = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2); + + Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager)); + Assert.IsTrue(manager!.GetType().Name.Contains("HlslBytecodeMemoryManager")); + } + + [D2DInputCount(3)] + [D2DInputSimple(0)] + [D2DInputSimple(1)] + [D2DInputSimple(2)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] + [D2DCompileOptions(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ShaderWithEmbeddedBytecodeAndCompileOptions : ID2D1PixelShader + { + public float4 Execute() + { + float4 dst = D2D.GetInput(0); + float4 src = D2D.GetInput(1); + float4 srcMask = D2D.GetInput(2); + float4 result = ((1 - srcMask) * dst) + (srcMask * src); + + return result; + } + } + + [TestMethod] + public void LoadBytecode_VerifyEffectiveValues_ExplicitShaderProfile() + { + _ = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); + + Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader40Level91); + Assert.AreEqual(compileOptions, D2D1CompileOptions.Default | D2D1CompileOptions.EnableLinking); + + _ = D2D1PixelShader.LoadBytecode(out shaderProfile, out compileOptions); + + // This shader has a complex input, so it can't support shader linking + Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader40Level91); + Assert.AreEqual(compileOptions, D2D1CompileOptions.Default); + } + + [D2DInputCount(1)] + [D2DInputSimple(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct SimpleShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput(0); + } + } + + [D2DInputCount(1)] + [D2DInputComplex(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader40Level91)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ComplexShaderWithExplicitShaderProfileAndNoCompileOptions : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput(0); + } + } + + [TestMethod] + public void LoadBytecode_VerifyEffectiveValues_ExplicitCompileOptions() + { + _ = D2D1PixelShader.LoadBytecode(out D2D1ShaderProfile shaderProfile, out D2D1CompileOptions compileOptions); + + // PS5.0 is the default, when no value is specified + Assert.AreEqual(shaderProfile, D2D1ShaderProfile.PixelShader50); + Assert.AreEqual(compileOptions, D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.PackMatrixRowMajor); + + _ = D2D1PixelShader.LoadBytecode(D2D1ShaderProfile.PixelShader40, out compileOptions); + + Assert.AreEqual(compileOptions, D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.PackMatrixRowMajor); + } + + [D2DInputCount(1)] + [D2DInputSimple(0)] + [D2DCompileOptions(D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.IeeeStrictness)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ShaderWithExplicitCompileOptionsAndNoShaderProfile : ID2D1PixelShader + { + public float4 Execute() + { + return D2D.GetInput(0); + } + } + + [TestMethod] + public void GetConstantBufferSize_Empty() + { + int size = D2D1PixelShader.GetConstantBufferSize(); + + Assert.AreEqual(size, 0); + + // Repeated calls always return the same result + size = D2D1PixelShader.GetConstantBufferSize(); + + Assert.AreEqual(size, 0); + } + + [TestMethod] + public void GetConstantBuffer_Empty_ReadOnlyMemory() + { + ShaderWithNoCapturedValues shader = default; + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); + + Assert.AreEqual(constantBuffer.Length, 0); + } + + // See https://github.com/Sergio0694/ComputeSharp/issues/323 + [TestMethod] + public void GetConstantBuffer_Empty_Span() + { + ShaderWithNoCapturedValues shader = default; + + byte[] buffer = Array.Empty(); + + int bytesWritten = D2D1PixelShader.GetConstantBuffer(in shader, buffer); + + Assert.AreEqual(bytesWritten, 0); + } + + [D2DInputCount(1)] + [D2DInputSimple(0)] + [D2DInputDescription(0, D2D1Filter.MinMagMipPoint)] + [D2DPixelOptions(D2D1PixelOptions.TrivialSampling)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ShaderWithNoCapturedValues : ID2D1PixelShader + { + public float4 Execute() + { + float4 color = D2D.GetInput(0); + + return color * color; + } + } + + [TestMethod] + public void GetConstantBufferSize() + { + int size = D2D1PixelShader.GetConstantBufferSize(); + + Assert.AreEqual(size, 124); + + // Same as above: repeated calls always return the same result + size = D2D1PixelShader.GetConstantBufferSize(); + + Assert.AreEqual(size, 124); + } + + [TestMethod] + public void GetConstantBuffer_RoundTrip() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); + + ShaderWithScalarVectorAndMatrixTypes copy = D2D1PixelShader.CreateFromConstantBuffer(constantBuffer.Span); + + ValidateShadersAreEqual(in shader, in copy); + } + + [TestMethod] + public void GetConstantBuffer_Global_RoundTrip() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in Unsafe.As(ref shader)); + + ShaderWithScalarVectorAndMatrixTypes_Global copy = D2D1PixelShader.CreateFromConstantBuffer(constantBuffer.Span); + + ValidateShadersAreEqual(in shader, in Unsafe.As(ref copy)); + } + + [TestMethod] + public void GetConstantBuffer_WithNestedStructs_RoundTrip() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); + + ShaderWithScalarVectorAndMatrixTypesInNestedStructs copy = D2D1PixelShader.CreateFromConstantBuffer(constantBuffer.Span); + + ValidateShadersAreEqual(in shader, in copy); + } + + [TestMethod] + public void GetConstantBuffer_WithNestedStructs_Global_RoundTrip() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in Unsafe.As(ref shader)); + + ShaderWithScalarVectorAndMatrixTypesInNestedStructs_Global copy = D2D1PixelShader.CreateFromConstantBuffer(constantBuffer.Span); + + ValidateShadersAreEqual(in shader, in Unsafe.As(ref copy)); + } + + [TestMethod] + public void GetConstantBuffer_TryCreate_RoundTrip() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); + + Assert.IsTrue(D2D1PixelShader.TryCreateFromConstantBuffer(constantBuffer.Span, out ShaderWithScalarVectorAndMatrixTypes copy)); + + ValidateShadersAreEqual(in shader, in copy); + } + + [TestMethod] + public void GetConstantBuffer_WithNestedStructs_TryCreate_RoundTrip() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); + + Assert.IsTrue(D2D1PixelShader.TryCreateFromConstantBuffer(constantBuffer.Span, out ShaderWithScalarVectorAndMatrixTypesInNestedStructs copy)); + + ValidateShadersAreEqual(in shader, in copy); + } + + [TestMethod] + [DataRow(0)] + [DataRow(4)] + [DataRow(27)] + [ExpectedException(typeof(ArgumentException))] + public void GetConstantBuffer_BufferTooShort(int size) + { + byte[] buffer = new byte[size]; + + _ = D2D1PixelShader.CreateFromConstantBuffer(buffer); + + Assert.Fail(); + } + + [TestMethod] + [DataRow(0)] + [DataRow(4)] + [DataRow(27)] + public void GetConstantBuffer_TryCreate_BufferTooShort(int size) + { + byte[] buffer = new byte[size]; + + Assert.IsFalse(D2D1PixelShader.TryCreateFromConstantBuffer(buffer, out ShaderWithScalarVectorAndMatrixTypes _)); + } + + [TestMethod] + public void GetConstantBuffer_ReadOnlyMemory() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); + + ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(constantBuffer.Span); + } + + [TestMethod] + public void GetConstantBuffer_Global_ReadOnlyMemory() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in Unsafe.As(ref shader)); + + ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(constantBuffer.Span); + } + + [TestMethod] + public void GetConstantBuffer_Span() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + byte[] buffer = new byte[1024]; + + int bytesWritten = D2D1PixelShader.GetConstantBuffer(in shader, buffer); + + Assert.IsTrue(bytesWritten > 0); + Assert.IsTrue(bytesWritten <= buffer.Length); + + ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten)); + } + + [TestMethod] + [DataRow(0)] + [DataRow(4)] + [DataRow(27)] + [ExpectedException(typeof(ArgumentException))] + public void GetConstantBuffer_Span_DestinationTooShort(int size) + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + byte[] buffer = new byte[size]; + + _ = D2D1PixelShader.GetConstantBuffer(in shader, buffer); + + Assert.Fail(); + } + + [TestMethod] + public void GetConstantBuffer_TryGetSpan() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + byte[] buffer = new byte[1024]; + + bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten); + + Assert.IsTrue(success); + Assert.IsTrue(bytesWritten > 0); + Assert.IsTrue(bytesWritten <= buffer.Length); + + ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten)); + } + + [TestMethod] + [DataRow(0)] + [DataRow(4)] + [DataRow(27)] + public void GetConstantBuffer_TryGetSpan_DestinationTooShort(int size) + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + byte[] buffer = new byte[size]; + + bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten); + + Assert.IsFalse(success); + Assert.AreEqual(bytesWritten, 0); + } + + // Gets a ShaderWithScalarVectorAndMatrixTypes instance with test values + private static void GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader) + { + shader = new ShaderWithScalarVectorAndMatrixTypes( + x: 111, + y: 222, z: 333, f2x3: new(55, 44, 888, 111, 222, 333), a: 22, - container3: new FieldsContainer3( - i1x3: new(1, 2, 3), - d2: new(3.14, 6.28))), - c: 42, - container1: new FieldsContainer1( + i1x3: new(1, 2, 3), + d2: new(3.14, 6.28), + c: 42, i1x2: new(111, 222), i2x2: new(11, 22, 33, 44), - d: 9999)); - } + d: 9999); + } - // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypesInNestedStructs instance - private static void ValidateShadersAreEqual(in ShaderWithScalarVectorAndMatrixTypesInNestedStructs left, in ShaderWithScalarVectorAndMatrixTypesInNestedStructs right) - { - Assert.AreEqual(left.x, right.x); - Assert.AreEqual(left.y, right.y); - Assert.AreEqual(left.container2.z, right.container2.z); - Assert.AreEqual(left.container2.f2x3.M11, right.container2.f2x3.M11); - Assert.AreEqual(left.container2.f2x3.M12, right.container2.f2x3.M12); - Assert.AreEqual(left.container2.f2x3.M13, right.container2.f2x3.M13); - Assert.AreEqual(left.container2.f2x3.M21, right.container2.f2x3.M21); - Assert.AreEqual(left.container2.f2x3.M22, right.container2.f2x3.M22); - Assert.AreEqual(left.container2.f2x3.M23, right.container2.f2x3.M23); - Assert.AreEqual(left.container2.container3.i1x3.M11, right.container2.container3.i1x3.M11); - Assert.AreEqual(left.container2.container3.i1x3.M12, right.container2.container3.i1x3.M12); - Assert.AreEqual(left.container2.container3.i1x3.M13, right.container2.container3.i1x3.M13); - Assert.AreEqual(left.container2.container3.d2.X, right.container2.container3.d2.X); - Assert.AreEqual(left.container2.container3.d2.Y, right.container2.container3.d2.Y); - Assert.AreEqual(left.c, right.c); - Assert.AreEqual(left.container1.i1x2.M11, right.container1.i1x2.M11); - Assert.AreEqual(left.container1.i1x2.M12, right.container1.i1x2.M12); - Assert.AreEqual(left.container1.i2x2.M11, right.container1.i2x2.M11); - Assert.AreEqual(left.container1.i2x2.M12, right.container1.i2x2.M12); - Assert.AreEqual(left.container1.i2x2.M21, right.container1.i2x2.M21); - Assert.AreEqual(left.container1.i2x2.M22, right.container1.i2x2.M22); - Assert.AreEqual(left.container1.d, right.container1.d); - } - - public partial class ContainingClass - { - public partial record ContainingRecord + // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypes instance + private unsafe void ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(ReadOnlySpan span) { - public partial struct ContainingStruct + Assert.AreEqual(124, span.Length); + + fixed (byte* buffer = span) { - public partial record struct ContainingRecordStruct + Assert.AreEqual(111, *(int*)&buffer[0]); + Assert.AreEqual(222, *(int*)&buffer[4]); + Assert.AreEqual(333, *(int*)&buffer[8]); + Assert.AreEqual(55, *(float*)&buffer[16]); + Assert.AreEqual(44, *(float*)&buffer[20]); + Assert.AreEqual(888, *(float*)&buffer[24]); + Assert.AreEqual(111, *(float*)&buffer[32]); + Assert.AreEqual(222, *(float*)&buffer[36]); + Assert.AreEqual(333, *(float*)&buffer[40]); + Assert.AreEqual(22, *(int*)&buffer[44]); + Assert.AreEqual(1, *(int*)&buffer[48]); + Assert.AreEqual(2, *(int*)&buffer[52]); + Assert.AreEqual(3, *(int*)&buffer[56]); + Assert.AreEqual(3.14, *(double*)&buffer[64]); + Assert.AreEqual(6.28, *(double*)&buffer[72]); + Assert.AreEqual(42, *(int*)&buffer[80]); + Assert.AreEqual(111, *(int*)&buffer[84]); + Assert.AreEqual(222, *(int*)&buffer[88]); + Assert.AreEqual(11, *(int*)&buffer[96]); + Assert.AreEqual(22, *(int*)&buffer[100]); + Assert.AreEqual(33, *(int*)&buffer[112]); + Assert.AreEqual(44, *(int*)&buffer[116]); + Assert.AreEqual(9999, *(int*)&buffer[120]); + } + } + + // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypes instance + private static void ValidateShadersAreEqual(in ShaderWithScalarVectorAndMatrixTypes left, in ShaderWithScalarVectorAndMatrixTypes right) + { + Assert.AreEqual(left.x, right.x); + Assert.AreEqual(left.y, right.y); + Assert.AreEqual(left.z, right.z); + Assert.AreEqual(left.f2x3.M11, right.f2x3.M11); + Assert.AreEqual(left.f2x3.M12, right.f2x3.M12); + Assert.AreEqual(left.f2x3.M13, right.f2x3.M13); + Assert.AreEqual(left.f2x3.M21, right.f2x3.M21); + Assert.AreEqual(left.f2x3.M22, right.f2x3.M22); + Assert.AreEqual(left.f2x3.M23, right.f2x3.M23); + Assert.AreEqual(left.i1x3.M11, right.i1x3.M11); + Assert.AreEqual(left.i1x3.M12, right.i1x3.M12); + Assert.AreEqual(left.i1x3.M13, right.i1x3.M13); + Assert.AreEqual(left.d2.X, right.d2.X); + Assert.AreEqual(left.d2.Y, right.d2.Y); + Assert.AreEqual(left.c, right.c); + Assert.AreEqual(left.i1x2.M11, right.i1x2.M11); + Assert.AreEqual(left.i1x2.M12, right.i1x2.M12); + Assert.AreEqual(left.i2x2.M11, right.i2x2.M11); + Assert.AreEqual(left.i2x2.M12, right.i2x2.M12); + Assert.AreEqual(left.i2x2.M21, right.i2x2.M21); + Assert.AreEqual(left.i2x2.M22, right.i2x2.M22); + Assert.AreEqual(left.d, right.d); + } + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] + internal readonly partial struct ShaderWithScalarVectorAndMatrixTypes : ID2D1PixelShader + { + public readonly int x; + public readonly int y; + public readonly int z; + public readonly float2x3 f2x3; + public readonly int a; + public readonly int1x3 i1x3; + public readonly double2 d2; + public readonly int c; + public readonly int1x2 i1x2; + public readonly int2x2 i2x2; + public readonly int d; + + public float4 Execute() + { + return 0; + } + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void SetConstantBufferForD2D1DrawInfo_NullD2D1DrawInfo() + { + D2D1PixelShader.SetConstantBufferForD2D1DrawInfo(null, default); + } + + [TestMethod] + public unsafe void GetConstantBufferSize_WithNestedStructs() + { + Assert.AreEqual(156, D2D1PixelShader.GetConstantBufferSize()); + + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); + + ReadOnlyMemory memory = D2D1PixelShader.GetConstantBuffer(in shader); + + ValidateShaderWithScalarVectorAndMatrixTypesInNestedStructsConstantBuffer(memory.Span); + } + + [TestMethod] + public unsafe void GetConstantBufferSize_Global_WithNestedStructs() + { + Assert.AreEqual(156, D2D1PixelShader.GetConstantBufferSize()); + + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader); + + ReadOnlyMemory memory = D2D1PixelShader.GetConstantBuffer(in Unsafe.As(ref shader)); + + ValidateShaderWithScalarVectorAndMatrixTypesInNestedStructsConstantBuffer(memory.Span); + } + + [AutoConstructor] + public readonly partial struct FieldsContainer1 + { + public readonly int1x2 i1x2; + public readonly int2x2 i2x2; + public readonly int d; + } + + [AutoConstructor] + public readonly partial struct FieldsContainer2 + { + public readonly int z; + public readonly float2x3 f2x3; + public readonly int a; + public readonly FieldsContainer3 container3; + } + + [AutoConstructor] + public readonly partial struct FieldsContainer3 + { + public readonly int1x3 i1x3; + public readonly double2 d2; + } + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] + internal readonly partial struct ShaderWithScalarVectorAndMatrixTypesInNestedStructs : ID2D1PixelShader + { + public readonly int x; + public readonly int y; + public readonly FieldsContainer2 container2; + public readonly int c; + public readonly FieldsContainer1 container1; + + public float4 Execute() + { + return 0; + } + } + + // Gets a ShaderWithScalarVectorAndMatrixTypesInNestedStructs instance with test values + private static void GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypesInNestedStructs shader) + { + shader = new ShaderWithScalarVectorAndMatrixTypesInNestedStructs( + x: 111, + y: 222, + container2: new FieldsContainer2( + z: 333, + f2x3: new(55, 44, 888, 111, 222, 333), + a: 22, + container3: new FieldsContainer3( + i1x3: new(1, 2, 3), + d2: new(3.14, 6.28))), + c: 42, + container1: new FieldsContainer1( + i1x2: new(111, 222), + i2x2: new(11, 22, 33, 44), + d: 9999)); + } + + // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypesInNestedStructs instance + private unsafe void ValidateShaderWithScalarVectorAndMatrixTypesInNestedStructsConstantBuffer(ReadOnlySpan span) + { + Assert.AreEqual(156, span.Length); + + fixed (byte* buffer = span) + { + Assert.AreEqual(111, *(int*)&buffer[0]); + Assert.AreEqual(222, *(int*)&buffer[4]); + Assert.AreEqual(333, *(int*)&buffer[16]); + Assert.AreEqual(55, *(float*)&buffer[32]); + Assert.AreEqual(44, *(float*)&buffer[36]); + Assert.AreEqual(888, *(float*)&buffer[40]); + Assert.AreEqual(111, *(float*)&buffer[48]); + Assert.AreEqual(222, *(float*)&buffer[52]); + Assert.AreEqual(333, *(float*)&buffer[56]); + Assert.AreEqual(22, *(int*)&buffer[60]); + Assert.AreEqual(1, *(int*)&buffer[64]); + Assert.AreEqual(2, *(int*)&buffer[68]); + Assert.AreEqual(3, *(int*)&buffer[72]); + Assert.AreEqual(3.14, *(double*)&buffer[80]); + Assert.AreEqual(6.28, *(double*)&buffer[88]); + Assert.AreEqual(42, *(int*)&buffer[96]); + Assert.AreEqual(111, *(int*)&buffer[112]); + Assert.AreEqual(222, *(int*)&buffer[116]); + Assert.AreEqual(11, *(int*)&buffer[128]); + Assert.AreEqual(22, *(int*)&buffer[132]); + Assert.AreEqual(33, *(int*)&buffer[144]); + Assert.AreEqual(44, *(int*)&buffer[148]); + Assert.AreEqual(9999, *(int*)&buffer[152]); + } + } + + // Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypesInNestedStructs instance + private static void ValidateShadersAreEqual(in ShaderWithScalarVectorAndMatrixTypesInNestedStructs left, in ShaderWithScalarVectorAndMatrixTypesInNestedStructs right) + { + Assert.AreEqual(left.x, right.x); + Assert.AreEqual(left.y, right.y); + Assert.AreEqual(left.container2.z, right.container2.z); + Assert.AreEqual(left.container2.f2x3.M11, right.container2.f2x3.M11); + Assert.AreEqual(left.container2.f2x3.M12, right.container2.f2x3.M12); + Assert.AreEqual(left.container2.f2x3.M13, right.container2.f2x3.M13); + Assert.AreEqual(left.container2.f2x3.M21, right.container2.f2x3.M21); + Assert.AreEqual(left.container2.f2x3.M22, right.container2.f2x3.M22); + Assert.AreEqual(left.container2.f2x3.M23, right.container2.f2x3.M23); + Assert.AreEqual(left.container2.container3.i1x3.M11, right.container2.container3.i1x3.M11); + Assert.AreEqual(left.container2.container3.i1x3.M12, right.container2.container3.i1x3.M12); + Assert.AreEqual(left.container2.container3.i1x3.M13, right.container2.container3.i1x3.M13); + Assert.AreEqual(left.container2.container3.d2.X, right.container2.container3.d2.X); + Assert.AreEqual(left.container2.container3.d2.Y, right.container2.container3.d2.Y); + Assert.AreEqual(left.c, right.c); + Assert.AreEqual(left.container1.i1x2.M11, right.container1.i1x2.M11); + Assert.AreEqual(left.container1.i1x2.M12, right.container1.i1x2.M12); + Assert.AreEqual(left.container1.i2x2.M11, right.container1.i2x2.M11); + Assert.AreEqual(left.container1.i2x2.M12, right.container1.i2x2.M12); + Assert.AreEqual(left.container1.i2x2.M21, right.container1.i2x2.M21); + Assert.AreEqual(left.container1.i2x2.M22, right.container1.i2x2.M22); + Assert.AreEqual(left.container1.d, right.container1.d); + } + + public partial class ContainingClass + { + public partial record ContainingRecord + { + public partial struct ContainingStruct { - public partial interface IContainingInterface + public partial record struct ContainingRecordStruct { - [D2DInputCount(0)] - [D2DGeneratedPixelShaderDescriptor] - [AutoConstructor] - public readonly partial struct DeeplyNestedShader : ID2D1PixelShader + public partial interface IContainingInterface { - public float4 Execute() + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] + public readonly partial struct DeeplyNestedShader : ID2D1PixelShader { - return 0; + public float4 Execute() + { + return 0; + } } } } } } } - } - [TestMethod] - public void DeeplyNestedShaderType_VerifyGenerator() - { - // Just a sanity check that the generated code is present - Assert.AreEqual(0, D2D1PixelShader.GetInputCount()); + [TestMethod] + public void DeeplyNestedShaderType_VerifyGenerator() + { + // Just a sanity check that the generated code is present + Assert.AreEqual(0, D2D1PixelShader.GetInputCount()); + } } } \ No newline at end of file