Merge pull request #809 from Sergio0694/dev/d2d-constant-buffer-zeroing
Ensure padding data in D2D constant buffer is always zero-ed
このコミットが含まれているのは:
3ファイル変更
+100
-5
検索条件に一致するファイルはありません
+13
-1
@@ -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<byte>(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)));");
|
||||
}
|
||||
|
||||
+20
-4
@@ -216,7 +216,11 @@ public class Test_D2DPixelShaderDescriptorGenerator
|
||||
[global::System.Runtime.CompilerServices.SkipLocalsInit]
|
||||
static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<MyShader>.LoadConstantBuffer<TLoader>(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<byte>(&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<MyShader>.LoadConstantBuffer<TLoader>(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<byte>(&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<MyShader>.LoadConstantBuffer<TLoader>(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<byte>(&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<MyShader>.LoadConstantBuffer<TLoader>(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<byte>(&buffer, sizeof(global::ComputeSharp.D2D1.Generated.ConstantBuffer)));
|
||||
}
|
||||
|
||||
@@ -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<byte> span = stackalloc byte[4096];
|
||||
|
||||
Random.Shared.NextBytes(span);
|
||||
|
||||
Dummy(span);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
static void Dummy(ReadOnlySpan<byte> span)
|
||||
{
|
||||
}
|
||||
|
||||
DirtyStack();
|
||||
|
||||
ReadOnlyMemory<byte> memory = D2D1PixelShader.GetConstantBuffer(in shader);
|
||||
|
||||
ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer_WithPadding(memory.Span);
|
||||
}
|
||||
|
||||
// Same as 'ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer', but also validates the padding
|
||||
private unsafe void ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer_WithPadding(ReadOnlySpan<byte> 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする