diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index 3f9d462a..0e926a5b 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -228,7 +228,7 @@ internal static class ShaderRunner [MethodImpl(MethodImplOptions.NoInlining)] private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, ref T shader, out ICachedShader shaderData) { - if (shader.TryGetBytecode(threadsX, threadsY, threadsZ, out ReadOnlySpan bytecode)) + if (Unsafe.NullRef().TryGetBytecode(threadsX, threadsY, threadsZ, out ReadOnlySpan bytecode)) { shaderData = new ICachedShader.Embedded(bytecode); } diff --git a/tests/ComputeSharp.Tests.Internals/ShaderBytecodeTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderBytecodeTests.cs new file mode 100644 index 00000000..f5909daf --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals/ShaderBytecodeTests.cs @@ -0,0 +1,79 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Tests.Internals; + +[TestClass] +[TestCategory("ShaderBytecode")] +public partial class ShaderBytecodeTests +{ + [TestMethod] + public void NonPrecompiledShader_Test() + { + Assert.IsFalse(((IComputeShader)default(NonPrecompiledShader)).TryGetBytecode(32, 1, 1, out ReadOnlySpan bytecode)); + Assert.IsFalse(bytecode.Length > 0); + } + + [AutoConstructor] + internal readonly partial struct NonPrecompiledShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + public readonly float factor; + + /// + public void Execute() + { + buffer[ThreadIds.X] *= factor; + } + } + + [TestMethod] + public void ComputeShader_Test() + { + Assert.IsTrue(((IComputeShader)default(ComputeShader)).TryGetBytecode(32, 1, 1, out ReadOnlySpan bytecode)); + Assert.IsTrue(bytecode.Length > 0); + + Assert.IsFalse(((IComputeShader)default(ComputeShader)).TryGetBytecode(64, 1, 1, out bytecode)); + Assert.IsFalse(bytecode.Length > 0); + } + + [AutoConstructor] + [EmbeddedBytecode(32, 1, 1)] + internal readonly partial struct ComputeShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + public readonly float factor; + + /// + public void Execute() + { + buffer[ThreadIds.X] *= factor; + } + } + + [TestMethod] + public void PixelShader_Test() + { + Assert.IsTrue(((IPixelShader)default(PixelShader)).TryGetBytecode(8, 8, 1, out ReadOnlySpan bytecode)); + Assert.IsTrue(bytecode.Length > 0); + + Assert.IsFalse(((IPixelShader)default(PixelShader)).TryGetBytecode(32, 1, 1, out bytecode)); + Assert.IsFalse(bytecode.Length > 0); + } + + [AutoConstructor] + [EmbeddedBytecode(8, 8, 1)] + internal readonly partial struct PixelShader : IPixelShader + { + public readonly ReadWriteTexture2D buffer; + public readonly float factor; + + /// + public float4 Execute() + { + return buffer[ThreadIds.XY] * factor; + } + } +}