diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs index dc7b9b41..9c3a69a5 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs @@ -59,8 +59,20 @@ partial class D2DPixelShaderDescriptorGenerator } else { + // Because D2D (and possibly other consumers) will do a bitwise check on the constant buffer to check for changes, + // it is important that the padding space between aligned elements in the generated constant buffer type are also + // cleared, and not filled with random data (which might be the case if no special care is taken). We cannot just + // rely on the default zero-ing performed by the JIT, because there is no guarantee that the padding space in types + // representing unions will also be zero-ed. So instead we create an uninitialized instance and manually clear it + // with 'NativeMemory.Clear' (which can also be unrolled by the JIT). This still maintains good performance, and + // makes it possible for downstream consumers to further improve performance by implementing better caching strategies. + writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer);"); + writer.WriteLine(); + writer.WriteLine("global::System.Runtime.InteropServices.NativeMemory.Clear(&buffer, (uint)sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer));"); + writer.WriteLine(); + // Otherwise, pass a span into the marshalled native layout buffer - writer.WriteLine("global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer);"); + writer.WriteLine("global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out buffer);"); writer.WriteLine(); writer.WriteLine("loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)));"); } diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs index 91348cec..c7b39878 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs @@ -216,7 +216,11 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Runtime.CompilerServices.SkipLocalsInit] static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) { - global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + + global::System.Runtime.InteropServices.NativeMemory.Clear(&buffer, (uint)sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)); + + global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out buffer); loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer))); } @@ -512,7 +516,11 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Runtime.CompilerServices.SkipLocalsInit] static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) { - global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + + global::System.Runtime.InteropServices.NativeMemory.Clear(&buffer, (uint)sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)); + + global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out buffer); loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer))); } @@ -860,7 +868,11 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Runtime.CompilerServices.SkipLocalsInit] static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) { - global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + + global::System.Runtime.InteropServices.NativeMemory.Clear(&buffer, (uint)sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)); + + global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out buffer); loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer))); } @@ -1154,7 +1166,11 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Runtime.CompilerServices.SkipLocalsInit] static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) { - global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer); + + global::System.Runtime.InteropServices.NativeMemory.Clear(&buffer, (uint)sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)); + + global::ComputeSharp.D2D1.Generated.ConstantBufferMarshaller.FromManaged(in shader, out buffer); loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer))); } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index ab7e00ea..dea905b7 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -1261,5 +1261,72 @@ namespace ComputeSharp.D2D1.Tests return Hlsl.BoolToFloat(this.value); } } + + // See https://github.com/Sergio0694/ComputeSharp/issues/808 + [TestMethod] + public void GetConstantBuffer_IsBitwiseEquatable() + { + GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader); + + [MethodImpl(MethodImplOptions.NoInlining)] + static void DirtyStack() + { + Span span = stackalloc byte[4096]; + + Random.Shared.NextBytes(span); + + Dummy(span); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static void Dummy(ReadOnlySpan span) + { + } + + DirtyStack(); + + ReadOnlyMemory memory = D2D1PixelShader.GetConstantBuffer(in shader); + + ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer_WithPadding(memory.Span); + } + + // Same as 'ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer', but also validates the padding + private unsafe void ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer_WithPadding(ReadOnlySpan span) + { + Assert.AreEqual(124, span.Length); + + fixed (byte* buffer = span) + { + Assert.AreEqual(111, *(int*)&buffer[0]); + Assert.AreEqual(222, *(int*)&buffer[4]); + Assert.AreEqual(333, *(int*)&buffer[8]); + Assert.AreEqual(0, *(int*)&buffer[12]); // Padding + Assert.AreEqual(55, *(float*)&buffer[16]); + Assert.AreEqual(44, *(float*)&buffer[20]); + Assert.AreEqual(888, *(float*)&buffer[24]); + Assert.AreEqual(0, *(int*)&buffer[28]); // Padding + 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(0, *(int*)&buffer[60]); // Padding + 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(0, *(int*)&buffer[92]); // Padding + Assert.AreEqual(11, *(int*)&buffer[96]); + Assert.AreEqual(22, *(int*)&buffer[100]); + Assert.AreEqual(0, *(int*)&buffer[104]); // Padding + Assert.AreEqual(0, *(int*)&buffer[108]); // Padding + Assert.AreEqual(33, *(int*)&buffer[112]); + Assert.AreEqual(44, *(int*)&buffer[116]); + Assert.AreEqual(9999, *(int*)&buffer[120]); + } + } } } \ No newline at end of file