diff --git a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs index c410988e..392b6a17 100644 --- a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs @@ -174,11 +174,6 @@ namespace ComputeSharp.Shaders.Translation BindingFlags.Public | BindingFlags.NonPublic).ToArray(); - if (shaderFields.Count == 0) - { - ThrowHelper.ThrowInvalidOperationException("The shader body must contain at least one field"); - } - List d3D12DescriptorRanges1 = new(); // Add the implicit texture descriptor if the shader is a pixel shader @@ -198,6 +193,11 @@ namespace ComputeSharp.Shaders.Translation } } + if (shaderFields.Count == 0 && d3D12DescriptorRanges1.Count == 0) + { + ThrowHelper.ThrowInvalidOperationException("The shader body must contain at least one field"); + } + // Inspect the captured fields foreach (FieldInfo fieldInfo in shaderFields) { diff --git a/tests/ComputeSharp.Tests/DispatchTests.cs b/tests/ComputeSharp.Tests/DispatchTests.cs index a7a19fc5..7b59d084 100644 --- a/tests/ComputeSharp.Tests/DispatchTests.cs +++ b/tests/ComputeSharp.Tests/DispatchTests.cs @@ -262,5 +262,41 @@ namespace ComputeSharp.Tests buffer[5] = (int)Hlsl.Dot(DispatchSize.XYZ, Float3.One); } } + + [CombinatorialTestMethod] + [AllDevices] + public void Verify_DispatchAsPixelShader(Device device) + { + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(256, 256); + + device.Get().ForEach(texture); + + Rgba32[,] data = texture.ToArray(); + + for (int y = 0; y < texture.Height; y++) + { + for (int x = 0; x < texture.Width; x++) + { + Rgba32 pixel = data[y, x]; + + Assert.AreEqual((float)pixel.R / 255, (float)x / texture.Width, 0.1f); + Assert.AreEqual((float)pixel.G / 255, (float)y / texture.Height, 0.1f); + Assert.AreEqual(pixel.B, 255); + Assert.AreEqual(pixel.A, 255); + } + } + } + + internal readonly partial struct DispatchPixelShader : IPixelShader + { + public Float4 Execute() + { + return new( + (float)ThreadIds.X / DispatchSize.X, + (float)ThreadIds.Y / DispatchSize.Y, + 1, + 1); + } + } } }