public void ShaderReflectionMatchesDirect3DReflection(string relPath) { // Arrange. var file = $"{ShaderDirectory}/{relPath}"; var binaryFileBytes = File.ReadAllBytes(file + ".o"); // Act. if (binaryFileBytes[0] == 0x01 && binaryFileBytes[1] == 0x20 && binaryFileBytes[2] == 0xFF && binaryFileBytes[3] == 0xFE) { Effects11.CompareEffect(null, binaryFileBytes, Path.GetFileNameWithoutExtension(relPath)); return; } var container = BytecodeContainer.Parse(binaryFileBytes); if (container.Chunks.OfType <LibHeaderChunk>().Any()) { CompareLibrary(container, binaryFileBytes); return; } if (container.Chunks.OfType <EffectChunk>().Any()) { Effects10.CompareEffect(container, binaryFileBytes, Path.GetFileNameWithoutExtension(relPath)); return; } using (var shaderBytecode = ShaderBytecode.FromStream(new MemoryStream(binaryFileBytes))) using (var shaderReflection = new ShaderReflection(shaderBytecode)) { var desc = shaderReflection.Description; // Assert. Assert.AreEqual(shaderReflection.BitwiseInstructionCount, 0); // TODO Assert.AreEqual(shaderReflection.ConditionalMoveInstructionCount, container.Statistics.MovCInstructionCount); Assert.AreEqual(shaderReflection.ConversionInstructionCount, container.Statistics.ConversionInstructionCount); Assert.AreEqual((int)shaderReflection.GeometryShaderSInputPrimitive, (int)container.Statistics.InputPrimitive); Assert.AreEqual(shaderReflection.InterfaceSlotCount, container.ResourceDefinition.InterfaceSlotCount); Assert.AreEqual((bool)shaderReflection.IsSampleFrequencyShader, container.Statistics.IsSampleFrequencyShader); Assert.AreEqual(shaderReflection.MoveInstructionCount, container.Statistics.MovInstructionCount); var flags = ShaderRequiresFlags.None; if (container.Version.MajorVersion >= 5) { if (container.Sfi0 != null) { flags = (ShaderRequiresFlags)container.Sfi0.Flags; } else { var dcl = container.Shader.DeclarationTokens .OfType <GlobalFlagsDeclarationToken>() .FirstOrDefault(); var globals = dcl?.Flags ?? 0; flags = (ShaderRequiresFlags)Chunks.Sfi0.Sfi0Chunk.GlobalFlagsToRequireFlags(globals); } } Assert.AreEqual(shaderReflection.RequiresFlags, flags); int expectedSizeX, expectedSizeY, expectedSizeZ; uint actualSizeX, actualSizeY, actualSizeZ; shaderReflection.GetThreadGroupSize(out expectedSizeX, out expectedSizeY, out expectedSizeZ); container.Shader.GetThreadGroupSize(out actualSizeX, out actualSizeY, out actualSizeZ); Assert.AreEqual(expectedSizeX, actualSizeX); Assert.AreEqual(expectedSizeY, actualSizeY); Assert.AreEqual(expectedSizeZ, actualSizeZ); SharpDX.Direct3D.FeatureLevel featureLevel = 0; if (container.Chunks.OfType <Chunks.Aon9.Level9ShaderChunk>().Any()) { var level9Chunk = container.Chunks .OfType <Chunks.Aon9.Level9ShaderChunk>() .First(); featureLevel = level9Chunk.ShaderModel.MinorVersion == 1 ? SharpDX.Direct3D.FeatureLevel.Level_9_3 : SharpDX.Direct3D.FeatureLevel.Level_9_1; } else if (container.Version.MajorVersion == 4 && container.Version.MinorVersion == 0) { featureLevel = SharpDX.Direct3D.FeatureLevel.Level_10_0; } else if (container.Version.MajorVersion == 4 && container.Version.MinorVersion == 1) { featureLevel = SharpDX.Direct3D.FeatureLevel.Level_10_1; } else if (container.Version.MajorVersion == 5) { featureLevel = SharpDX.Direct3D.FeatureLevel.Level_11_0; if (flags.HasFlag(ShaderRequiresFlags.ShaderRequires64UnorderedAccessViews)) { featureLevel = SharpDX.Direct3D.FeatureLevel.Level_11_1; } } Assert.AreEqual(shaderReflection.MinFeatureLevel, featureLevel); // TODO Assert.AreEqual(desc.ArrayInstructionCount, container.Statistics.ArrayInstructionCount); Assert.AreEqual(desc.BarrierInstructions, container.Statistics.BarrierInstructions); Assert.AreEqual(desc.BoundResources, container.ResourceDefinition.ResourceBindings.Count); Assert.AreEqual(desc.ConstantBuffers, container.ResourceDefinition.ConstantBuffers.Count); Assert.AreEqual(desc.ControlPoints, container.Statistics.ControlPoints); Assert.AreEqual(desc.Creator, container.ResourceDefinition.Creator); Assert.AreEqual(desc.CutInstructionCount, container.Statistics.CutInstructionCount); Assert.AreEqual(desc.DeclarationCount, container.Statistics.DeclarationCount); Assert.AreEqual(desc.DefineCount, container.Statistics.DefineCount); Assert.AreEqual(desc.DynamicFlowControlCount, container.Statistics.DynamicFlowControlCount); Assert.AreEqual(desc.EmitInstructionCount, container.Statistics.EmitInstructionCount); Assert.AreEqual((int)desc.Flags, (int)container.ResourceDefinition.Flags); Assert.AreEqual(desc.FloatInstructionCount, container.Statistics.FloatInstructionCount); Assert.AreEqual(desc.GeometryShaderInstanceCount, container.Statistics.GeometryShaderInstanceCount); Assert.AreEqual(desc.GeometryShaderMaxOutputVertexCount, container.Statistics.GeometryShaderMaxOutputVertexCount); Assert.AreEqual((int)desc.GeometryShaderOutputTopology, (int)container.Statistics.GeometryShaderOutputTopology); Assert.AreEqual((int)desc.HullShaderOutputPrimitive, (int)container.Statistics.HullShaderOutputPrimitive); Assert.AreEqual((int)desc.HullShaderPartitioning, (int)container.Statistics.HullShaderPartitioning); Assert.AreEqual(desc.InputParameters, container.InputSignature.Parameters.Count); Assert.AreEqual((int)desc.InputPrimitive, (int)container.Statistics.InputPrimitive); Assert.AreEqual(desc.InstructionCount, container.Statistics.InstructionCount); Assert.AreEqual(desc.InterlockedInstructions, container.Statistics.InterlockedInstructions); Assert.AreEqual(desc.IntInstructionCount, container.Statistics.IntInstructionCount); Assert.AreEqual(desc.MacroInstructionCount, container.Statistics.MacroInstructionCount); Assert.AreEqual(desc.OutputParameters, container.OutputSignature.Parameters.Count); Assert.AreEqual(desc.PatchConstantParameters, (container.PatchConstantSignature != null) ? container.PatchConstantSignature.Parameters.Count : 0); Assert.AreEqual(desc.StaticFlowControlCount, container.Statistics.StaticFlowControlCount); Assert.AreEqual(desc.TempArrayCount, container.Statistics.TempArrayCount); Assert.AreEqual(desc.TempRegisterCount, container.Statistics.TempRegisterCount); Assert.AreEqual((int)desc.TessellatorDomain, (int)container.Statistics.TessellatorDomain); Assert.AreEqual(desc.TextureBiasInstructions, container.Statistics.TextureBiasInstructions); Assert.AreEqual(desc.TextureCompInstructions, container.Statistics.TextureCompInstructions); Assert.AreEqual(desc.TextureGradientInstructions, container.Statistics.TextureGradientInstructions); Assert.AreEqual(desc.TextureLoadInstructions, container.Statistics.TextureLoadInstructions); Assert.AreEqual(desc.TextureNormalInstructions, container.Statistics.TextureNormalInstructions); Assert.AreEqual(desc.TextureStoreInstructions, container.Statistics.TextureStoreInstructions); Assert.AreEqual(desc.UintInstructionCount, container.Statistics.UIntInstructionCount); var version = Chunks.Common.ShaderVersion.FromShexToken((uint)desc.Version); Assert.AreEqual(version.ToString(), container.ResourceDefinition.Target.ToString()); for (int i = 0; i < shaderReflection.Description.ConstantBuffers; i++) { CompareConstantBuffer(shaderReflection.GetConstantBuffer(i), container.ResourceDefinition.ConstantBuffers[i]); } for (int i = 0; i < shaderReflection.Description.BoundResources; i++) { CompareResourceBinding(shaderReflection.GetResourceBindingDescription(i), container.ResourceDefinition.ResourceBindings[i]); } for (int i = 0; i < shaderReflection.Description.InputParameters; i++) { CompareParameter(shaderReflection.GetInputParameterDescription(i), container.InputSignature.Parameters[i]); } for (int i = 0; i < shaderReflection.Description.OutputParameters; i++) { CompareParameter(shaderReflection.GetOutputParameterDescription(i), container.OutputSignature.Parameters[i]); } for (int i = 0; i < shaderReflection.Description.PatchConstantParameters; i++) { CompareParameter(shaderReflection.GetPatchConstantParameterDescription(i), container.PatchConstantSignature.Parameters[i]); } } }
public void ShaderReflectionMatchesDirect3DReflection(string file) { // Arrange. var binaryFileBytes = File.ReadAllBytes(file + ".o"); using (var shaderBytecode = ShaderBytecode.FromStream(new MemoryStream(binaryFileBytes))) using (var shaderReflection = new ShaderReflection(shaderBytecode)) { var desc = shaderReflection.Description; // Act. var container = BytecodeContainer.Parse(binaryFileBytes); // Assert. Assert.AreEqual(shaderReflection.BitwiseInstructionCount, 0); // TODO Assert.AreEqual(shaderReflection.ConditionalMoveInstructionCount, container.Statistics.MovCInstructionCount); Assert.AreEqual(shaderReflection.ConversionInstructionCount, container.Statistics.ConversionInstructionCount); Assert.AreEqual((int)shaderReflection.GeometryShaderSInputPrimitive, (int)container.Statistics.InputPrimitive); Assert.AreEqual(shaderReflection.InterfaceSlotCount, container.ResourceDefinition.InterfaceSlotCount); Assert.AreEqual((bool)shaderReflection.IsSampleFrequencyShader, container.Statistics.IsSampleFrequencyShader); Assert.AreEqual(shaderReflection.MoveInstructionCount, container.Statistics.MovInstructionCount); //Assert.AreEqual(shaderReflection.RequiresFlags, 0); // TODO int expectedSizeX, expectedSizeY, expectedSizeZ; uint actualSizeX, actualSizeY, actualSizeZ; shaderReflection.GetThreadGroupSize(out expectedSizeX, out expectedSizeY, out expectedSizeZ); container.Shader.GetThreadGroupSize(out actualSizeX, out actualSizeY, out actualSizeZ); Assert.AreEqual(expectedSizeX, actualSizeX); Assert.AreEqual(expectedSizeY, actualSizeY); Assert.AreEqual(expectedSizeZ, actualSizeZ); //Assert.AreEqual((int) shaderReflection.MinFeatureLevel, 0); // TODO Assert.AreEqual(desc.ArrayInstructionCount, container.Statistics.ArrayInstructionCount); Assert.AreEqual(desc.BarrierInstructions, container.Statistics.BarrierInstructions); Assert.AreEqual(desc.BoundResources, container.ResourceDefinition.ResourceBindings.Count); Assert.AreEqual(desc.ConstantBuffers, container.ResourceDefinition.ConstantBuffers.Count); Assert.AreEqual(desc.ControlPoints, container.Statistics.ControlPoints); Assert.AreEqual(desc.Creator, container.ResourceDefinition.Creator); Assert.AreEqual(desc.CutInstructionCount, container.Statistics.CutInstructionCount); Assert.AreEqual(desc.DeclarationCount, container.Statistics.DeclarationCount); Assert.AreEqual(desc.DefineCount, container.Statistics.DefineCount); Assert.AreEqual(desc.DynamicFlowControlCount, container.Statistics.DynamicFlowControlCount); Assert.AreEqual(desc.EmitInstructionCount, container.Statistics.EmitInstructionCount); Assert.AreEqual((int)desc.Flags, (int)container.ResourceDefinition.Flags); Assert.AreEqual(desc.FloatInstructionCount, container.Statistics.FloatInstructionCount); Assert.AreEqual(desc.GeometryShaderInstanceCount, container.Statistics.GeometryShaderInstanceCount); Assert.AreEqual(desc.GeometryShaderMaxOutputVertexCount, container.Statistics.GeometryShaderMaxOutputVertexCount); Assert.AreEqual((int)desc.GeometryShaderOutputTopology, (int)container.Statistics.GeometryShaderOutputTopology); Assert.AreEqual((int)desc.HullShaderOutputPrimitive, (int)container.Statistics.HullShaderOutputPrimitive); Assert.AreEqual((int)desc.HullShaderPartitioning, (int)container.Statistics.HullShaderPartitioning); Assert.AreEqual(desc.InputParameters, container.InputSignature.Parameters.Count); Assert.AreEqual((int)desc.InputPrimitive, (int)container.Statistics.InputPrimitive); Assert.AreEqual(desc.InstructionCount, container.Statistics.InstructionCount); Assert.AreEqual(desc.InterlockedInstructions, container.Statistics.InterlockedInstructions); Assert.AreEqual(desc.IntInstructionCount, container.Statistics.IntInstructionCount); Assert.AreEqual(desc.MacroInstructionCount, container.Statistics.MacroInstructionCount); Assert.AreEqual(desc.OutputParameters, container.OutputSignature.Parameters.Count); Assert.AreEqual(desc.PatchConstantParameters, (container.PatchConstantSignature != null) ? container.PatchConstantSignature.Parameters.Count : 0); Assert.AreEqual(desc.StaticFlowControlCount, container.Statistics.StaticFlowControlCount); Assert.AreEqual(desc.TempArrayCount, container.Statistics.TempArrayCount); Assert.AreEqual(desc.TempRegisterCount, container.Statistics.TempRegisterCount); Assert.AreEqual((int)desc.TessellatorDomain, (int)container.Statistics.TessellatorDomain); Assert.AreEqual(desc.TextureBiasInstructions, container.Statistics.TextureBiasInstructions); Assert.AreEqual(desc.TextureCompInstructions, container.Statistics.TextureCompInstructions); Assert.AreEqual(desc.TextureGradientInstructions, container.Statistics.TextureGradientInstructions); Assert.AreEqual(desc.TextureLoadInstructions, container.Statistics.TextureLoadInstructions); Assert.AreEqual(desc.TextureNormalInstructions, container.Statistics.TextureNormalInstructions); Assert.AreEqual(desc.TextureStoreInstructions, container.Statistics.TextureStoreInstructions); Assert.AreEqual(desc.UintInstructionCount, container.Statistics.UIntInstructionCount); //Assert.AreEqual(desc.Version, container.ResourceDefinition.Target); // TODO for (int i = 0; i < shaderReflection.Description.ConstantBuffers; i++) { CompareConstantBuffer(shaderReflection.GetConstantBuffer(i), container.ResourceDefinition.ConstantBuffers[i]); } for (int i = 0; i < shaderReflection.Description.BoundResources; i++) { CompareResourceBinding(shaderReflection.GetResourceBindingDescription(i), container.ResourceDefinition.ResourceBindings[i]); } for (int i = 0; i < shaderReflection.Description.InputParameters; i++) { CompareParameter(shaderReflection.GetInputParameterDescription(i), container.InputSignature.Parameters[i]); } for (int i = 0; i < shaderReflection.Description.OutputParameters; i++) { CompareParameter(shaderReflection.GetOutputParameterDescription(i), container.OutputSignature.Parameters[i]); } for (int i = 0; i < shaderReflection.Description.PatchConstantParameters; i++) { CompareParameter(shaderReflection.GetPatchConstantParameterDescription(i), container.PatchConstantSignature.Parameters[i]); } } }