diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs index 9c3a69a5..8fad2ba0 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs @@ -48,7 +48,11 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine($"static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader)"); + + // Write the method declaration (we only need unsafe blocks if we have any values in the constant buffer) + writer.Write("static "); + writer.WriteIf(!info.Fields.IsEmpty, "unsafe "); + writer.WriteLine($"void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.LoadConstantBuffer(in {typeName} shader, ref TLoader loader)"); using (writer.WriteBlock()) { @@ -91,7 +95,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine("[global::System.Runtime.CompilerServices.SkipLocalsInit]"); - writer.WriteLine($"static unsafe {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan data)"); + writer.WriteLine($"static {typeName} global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{typeName}>.CreateFromConstantBuffer(global::System.ReadOnlySpan data)"); using (writer.WriteBlock()) { diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs index 26f0f706..c49e2d72 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs @@ -202,7 +202,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) + static MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) { ref readonly global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer = ref global::System.Runtime.InteropServices.MemoryMarshal.AsRef(data); @@ -502,7 +502,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) + static MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) { ref readonly global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer = ref global::System.Runtime.InteropServices.MemoryMarshal.AsRef(data); @@ -854,7 +854,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) + static MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) { ref readonly global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer = ref global::System.Runtime.InteropServices.MemoryMarshal.AsRef(data); @@ -1152,7 +1152,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) + static MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) { ref readonly global::ComputeSharp.D2D1.Generated.ConstantBuffer buffer = ref global::System.Runtime.InteropServices.MemoryMarshal.AsRef(data); @@ -1442,7 +1442,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) + static MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) { return default; } @@ -1452,7 +1452,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) + static void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) { loader.LoadConstantBuffer(default); } @@ -1728,7 +1728,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) + static MyShader global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.CreateFromConstantBuffer(global::System.ReadOnlySpan data) { return default; } @@ -1738,7 +1738,7 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::System.Runtime.CompilerServices.SkipLocalsInit] - static unsafe void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) + static void global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.LoadConstantBuffer(in MyShader shader, ref TLoader loader) { loader.LoadConstantBuffer(default); }