From 57d92098270330f2546b6ba75d10a1bbd6895eda Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 6 Jun 2024 18:46:40 -0700 Subject: [PATCH] Ensure padding space in D2D constant buffer is zeroed --- ...haderDescriptorGenerator.ConstantBuffer.cs | 14 ++++++++++- .../Test_D2DPixelShaderDescriptorGenerator.cs | 24 +++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) 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))); }