From 18e9a5ac72e68d246d0d09b52b8bbb6dddb04cf3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 27 Oct 2023 13:14:55 +0200 Subject: [PATCH] Add unit test for shader with no reflection metadata --- .../Core/Attributes/Enums/CompileOptions.cs | 4 +- .../ComputeSharp.Tests/ShaderCompilerTests.cs | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp/Core/Attributes/Enums/CompileOptions.cs b/src/ComputeSharp/Core/Attributes/Enums/CompileOptions.cs index 09604c12..77ff9663 100644 --- a/src/ComputeSharp/Core/Attributes/Enums/CompileOptions.cs +++ b/src/ComputeSharp/Core/Attributes/Enums/CompileOptions.cs @@ -131,8 +131,8 @@ public enum CompileOptions ResourcesMayAlias = 1 << 13, /// - /// Strips the reflection data from the generated shader bytecode. The bytecode size will be significantly lower, - /// but trying to perform reflection on the shader will fail at runtime. Recommend if not reflection is done. + /// Strips the reflection data from the generated shader bytecode. The bytecode size will be significantly lower, but + /// trying to perform reflection on the shader will return inaccurate results. Recommend if not reflection is done. /// /// /// This flag maps to -Qstrip_reflect. diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 01f99b32..f18ea12d 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -559,6 +559,54 @@ namespace ComputeSharp.Tests this.buffer[ThreadIds.X] *= x; } } + + [TestMethod] + public void ShaderWithStrippedReflectionData() + { + ShaderInfo info1 = ReflectionServices.GetShaderInfo(); + + // With no reflection data available, the instruction count is just 0 + Assert.AreEqual(0u, info1.InstructionCount); + + ShaderInfo info2 = ReflectionServices.GetShaderInfo(); + + // Sanity check, here instead we should have some valid count + Assert.AreNotEqual(0u, info2.InstructionCount); + + // Verify that the bytecode with stripped reflection is much smaller + Assert.IsTrue(info1.HlslBytecode.Length < 1800); + Assert.IsTrue(info2.HlslBytecode.Length > 3300); + } + + [AutoConstructor] + [EmbeddedBytecode(DispatchAxis.X)] + [CompileOptions(CompileOptions.Default | CompileOptions.StripReflectionData)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct ShaderWithStrippedReflectionData1 : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() + { + this.buffer[ThreadIds.X] = ThreadIds.X; + } + } + + [AutoConstructor] + [EmbeddedBytecode(DispatchAxis.X)] + [CompileOptions(CompileOptions.Default)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct ShaderWithStrippedReflectionData2 : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + /// + public void Execute() + { + this.buffer[ThreadIds.X] = ThreadIds.X; + } + } } }