public static void DumpFloat(int value, StringBuilder sb) { var floatValue = ToFloat(value); if (float.IsNaN(floatValue)) { return; } string shaderCode = string.Format(@" float4 main(): sv_target {{ return asfloat({0}); }}" , value); var compiledShader = ShaderBytecode.Compile(shaderCode, "main", "ps_5_0"); var shaderBytecode = compiledShader.Bytecode; var disassembly = shaderBytecode.Disassemble(); var bytecode = BytecodeContainer.Parse(shaderBytecode); var number = bytecode.Shader.InstructionTokens.First().Operands[1].ImmediateValues; StringAssert.Contains("Generated by Microsoft (R) HLSL Shader Compiler 10.1", disassembly); var expectedFloat = Regex.Match(disassembly, @"l\((.*?),").Groups[1].Value; var actualFloat = FormatFloat(floatValue); var fullFloat = DoubleConverter.ToExactString(floatValue, -1); var correct = expectedFloat == actualFloat; if (value != number.Int0) { return; } sb.AppendLine(string.Format("{0}\t{1, 50}\t{2, 50}\t{3}\t{4}", correct, expectedFloat, actualFloat, fullFloat, value)); }
public void AsmMatchesFxc(string relPath) { var file = $"{ShaderDirectory}/{relPath}"; // Arrange. var asmFileText = string.Join(Environment.NewLine, File.ReadAllLines(file + ".asm").Select(x => x.Trim())); // Act. var bytecode = File.ReadAllBytes(file + ".o"); var container = BytecodeContainer.Parse(bytecode); var decompiledAsmText = container.ToString(); File.WriteAllText($"{file}.d.asm", decompiledAsmText); File.WriteAllText($"{file}.x", FileUtil.FormatReadable(bytecode)); decompiledAsmText = string.Join(Environment.NewLine, decompiledAsmText .Split(new[] { Environment.NewLine }, StringSplitOptions.None) .Select(x => x.Trim())); decompiledAsmText = TestUtils.NormalizeAssembly(decompiledAsmText); asmFileText = TestUtils.NormalizeAssembly(asmFileText); // Assert. if (container.Chunks.OfType <DebuggingChunk>().Any()) { Warn.If(true, "Debugging information is ignored during dissasembly"); } else { Assert.That(decompiledAsmText, Is.EqualTo(asmFileText)); } }
public static void TestFloat(int value) { var floatValue = ToFloat(value); if (float.IsNaN(floatValue)) { return; } string shaderCode = string.Format(@" float4 main(): sv_target {{ return asfloat({0}); }}" , value); var compiledShader = ShaderBytecode.Compile(shaderCode, "main", "ps_5_0"); var shaderBytecode = compiledShader.Bytecode; var disassembly = shaderBytecode.Disassemble(); var bytecode = BytecodeContainer.Parse(shaderBytecode); var number = bytecode.Shader.InstructionTokens.First().Operands[1].ImmediateValues; StringAssert.Contains("Generated by Microsoft (R) HLSL Shader Compiler 10.1", disassembly); var expectedFloat = Regex.Match(disassembly, @"l\((.*?),").Groups[1].Value; var actualFloat = FormatFloat(floatValue); if (value != number.Int0) { Directory.CreateDirectory(OutputDir); File.WriteAllText($"{OutputDir}/FloatParserDissassembly.asm", disassembly); return; } Assert.AreEqual(value, number.Int0, "Parsed binary represention doesn't match input"); Assert.AreEqual(expectedFloat, actualFloat, $"String format doesn't match expected for {value} {DoubleConverter.ToExactString(floatValue, -1)}"); }
public void Open(string path) { _path = path; _fileName = Path.GetFileName(path); _bytecodeContainer = BytecodeContainer.Parse(File.ReadAllBytes(path)); DisassembledCode = _bytecodeContainer.ToString(); }
public void CanExecutePixelShaderBasicHlsl(IShaderExecutor shaderExecutor) { // Arrange. VirtualMachine.ShaderExecutor = shaderExecutor; var vm = new VirtualMachine(BytecodeContainer.Parse(File.ReadAllBytes("Shaders/PS/BasicHLSL_PS.o")), 4); vm.SetRegister(0, OperandType.ConstantBuffer, new RegisterIndex(0, 0), new Number4 { Number0 = Number.FromUInt(1) // bTexture = true }); vm.SetRegister(0, OperandType.Input, new RegisterIndex(1), new Number4(0.5f, 0.5f, 1.5f, 1)); // COLOR0 vm.SetRegister(0, OperandType.Input, new RegisterIndex(2), new Number4(0, 0, 0, 0)); // TEXCOORD0 vm.SetTexture(new RegisterIndex(0), new FakeTexture(new Number4(0.8f, 0.6f, 0.4f, 1))); vm.SetSampler(new RegisterIndex(0), new SamplerState()); // Act. vm.Execute(); // Assert. var output0 = vm.GetRegister(0, OperandType.Output, new RegisterIndex(0)); Assert.That(output0.Number0.Float, Is.EqualTo(0.4f)); Assert.That(output0.Number1.Float, Is.EqualTo(0.3f)); Assert.That(output0.Number2.Float, Is.EqualTo(0.6f)); Assert.That(output0.Number3.Float, Is.EqualTo(1)); }
public new static EffectExpressionAssignment Parse(BytecodeReader reader, BytecodeReader assignmentReader) { var result = new EffectExpressionAssignment(); var shaderSize = assignmentReader.ReadUInt32(); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(assignmentReader.ReadBytes((int)shaderSize)); } return(result); }
public static DebugEffectShaderData Parse(DebugBytecodeReader reader, DebugBytecodeReader variableReader) { var result = new DebugEffectShaderData(); var shaderOffset = result.ShaderOffset = variableReader.ReadUInt32("ShaderOffset"); var bytecodeReader = reader.CopyAtOffset("BytecodeReader", variableReader, (int)shaderOffset); var shaderSize = bytecodeReader.ReadUInt32("ShaderSize"); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(bytecodeReader.ReadBytes("Shader", (int)shaderSize)); } return(result); }
public static EffectShaderData Parse(BytecodeReader reader, BytecodeReader variableReader) { var result = new EffectShaderData(); var shaderOffset = variableReader.ReadUInt32(); var bytecodeReader = reader.CopyAtOffset((int)shaderOffset); var shaderSize = bytecodeReader.ReadUInt32(); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(bytecodeReader.ReadBytes((int)shaderSize)); } return(result); }
public void PerformanceTest(IShaderExecutor shaderExecutor) { var bytecodeContainer = BytecodeContainer.Parse(File.ReadAllBytes("Shaders/VS/BasicHLSL_VS.o")); VirtualMachine.ShaderExecutor = shaderExecutor; var vm = new VirtualMachine(bytecodeContainer, 1); var globals = new BasicHlsl.ConstantBufferGlobals { WorldViewProjection = Matrix.LookAtRH(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY) * Matrix.PerspectiveFovRH(MathUtil.PiOverFour, 1, 1, 10) }; var vertexInput = new VertexPositionNormalTexture { Position = new Vector4(3, 0, 2, 1), Normal = new Vector3(0, 1, 0), TextureCoordinate = new Vector2(0, 1) }; SetConstantBuffer(vm, 0, globals); vm.SetRegister(0, OperandType.ConstantBuffer, new RegisterIndex(1, 0), new Number4 { Number0 = Number.FromInt(3), // nNumLights = 3 Number1 = Number.FromInt(1) // bTexture = true }); vm.SetRegister(0, OperandType.Input, new RegisterIndex(0), vertexInput.Position.ToNumber4()); vm.SetRegister(0, OperandType.Input, new RegisterIndex(1), vertexInput.Normal.ToNumber4()); vm.SetRegister(0, OperandType.Input, new RegisterIndex(2), vertexInput.TextureCoordinate.ToNumber4()); // Prime the pump by executing shader once. vm.Execute(); var stopwatch = new Stopwatch(); stopwatch.Start(); const int iterations = 100000; for (var i = 0; i < iterations; i++) { vm.Execute(); } stopwatch.Stop(); Debug.WriteLine("Time: " + stopwatch.Elapsed); }
public void ParsedShaderOutputMatchesFxcOutput(string file) { // Arrange. var asmFileText = string.Join(Environment.NewLine, File.ReadAllLines(file + ".asm").Select(x => x.Trim())); // Act. var container = BytecodeContainer.Parse(File.ReadAllBytes(file + ".o")); var decompiledAsmText = string.Join(Environment.NewLine, container.ToString() .Split(new[] { Environment.NewLine }, StringSplitOptions.None) .Select(x => x.Trim())); // Assert. Assert.That(decompiledAsmText, Is.EqualTo(asmFileText)); }
public void CanExecuteSimplePixelShader(IShaderExecutor shaderExecutor) { // Arrange. VirtualMachine.ShaderExecutor = shaderExecutor; var vm = new VirtualMachine(BytecodeContainer.Parse(File.ReadAllBytes("Shaders/PS/Simple.o")), 4); // Act. vm.Execute(); // Assert. var output0 = vm.GetRegister(0, OperandType.Output, new RegisterIndex(0)); Assert.That(output0.Number0.Float, Is.EqualTo(1.0f)); Assert.That(output0.Number1.Float, Is.EqualTo(0.5f)); Assert.That(output0.Number2.Float, Is.EqualTo(0.4f)); Assert.That(output0.Number3.Float, Is.EqualTo(1.0f)); }
public new static EffectInlineShaderAssignment Parse(BytecodeReader reader, BytecodeReader assignmentReader) { var result = new EffectInlineShaderAssignment(); var shaderOffset = assignmentReader.ReadUInt32(); var SODeclOffset = assignmentReader.ReadUInt32(); var shaderReader = reader.CopyAtOffset((int)shaderOffset); var shaderSize = shaderReader.ReadUInt32(); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(shaderReader.ReadBytes((int)shaderSize)); } var SODeclReader = reader.CopyAtOffset((int)SODeclOffset); result.SODecl = SODeclReader.ReadString(); return(result); }
public new static DebugEffectInlineShaderAssignment Parse(DebugBytecodeReader reader, DebugBytecodeReader assignmentReader) { var result = new DebugEffectInlineShaderAssignment(); var shaderOffset = result.ShaderOffset = assignmentReader.ReadUInt32("ShaderOffset"); var SODeclOffset = result.SODeclOffset = assignmentReader.ReadUInt32("DODeclOffset"); var shaderReader = reader.CopyAtOffset("ShaderReader", assignmentReader, (int)shaderOffset); var shaderSize = shaderReader.ReadUInt32("ShaderSize"); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(shaderReader.ReadBytes("Shader", (int)shaderSize)); } var SODeclReader = reader.CopyAtOffset("SODeclReader", assignmentReader, (int)SODeclOffset); result.SODecl = SODeclReader.ReadString("SODecl"); return(result); }
public void CanExecuteVertexShaderBasicHlsl(IShaderExecutor shaderExecutor) { // Arrange. VirtualMachine.ShaderExecutor = shaderExecutor; var vm = new VirtualMachine(BytecodeContainer.Parse(File.ReadAllBytes("Shaders/VS/BasicHLSL_VS.o")), 1); var globals = new BasicHlsl.ConstantBufferGlobals { WorldViewProjection = Matrix.LookAtRH(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY) * Matrix.PerspectiveFovRH(MathUtil.PiOverFour, 1, 1, 10), NumLights = 3 }; var vertexInput = new VertexPositionNormalTexture { Position = new Vector4(3, 0, 2, 1), Normal = new Vector3(0, 1, 0), TextureCoordinate = new Vector2(0, 1) }; var direct3DResult = Direct3DUtility.ExecuteVertexShader("Shaders/VS/BasicHLSL_VS.o", globals, vertexInput); SetConstantBuffer(vm, 0, globals); vm.SetConstantBufferRegisterValue(1, 0, new Number4 { Number0 = Number.FromInt(3), // nNumLights = 3 Number1 = Number.FromInt(1) // bTexture = true }); vm.SetInputRegisterValue(0, 0, 0, vertexInput.Position.ToNumber4()); vm.SetInputRegisterValue(0, 0, 1, vertexInput.Normal.ToNumber4()); vm.SetInputRegisterValue(0, 0, 2, vertexInput.TextureCoordinate.ToNumber4()); // Act. vm.Execute(); // Assert. var output0 = vm.GetOutputRegisterValue(0, 0); Assert.That(output0.Number0.Float, Is.EqualTo(direct3DResult.Position.X)); Assert.That(output0.Number1.Float, Is.EqualTo(direct3DResult.Position.Y)); Assert.That(output0.Number2.Float, Is.EqualTo(direct3DResult.Position.Z)); Assert.That(output0.Number3.Float, Is.EqualTo(direct3DResult.Position.W)); }
public new static EffectExpressionIndexAssignment Parse(BytecodeReader reader, BytecodeReader assignmentReader) { var result = new EffectExpressionIndexAssignment(); var arrayNameOffset = assignmentReader.ReadUInt32(); var arrayNameReader = reader.CopyAtOffset((int)arrayNameOffset); result.ArrayName = arrayNameReader.ReadString(); var shaderOffset = assignmentReader.ReadUInt32(); var shaderReader = reader.CopyAtOffset((int)shaderOffset); var shaderSize = shaderReader.ReadUInt32(); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(shaderReader.ReadBytes((int)shaderSize)); } return(result); }
public static DebugEffectGSSOInitializer Parse(DebugBytecodeReader reader, DebugBytecodeReader variableReader) { var result = new DebugEffectGSSOInitializer(); var shaderOffset = result.ShaderOffset = variableReader.ReadUInt32("ShaderOffset"); var SODeclOffset = result.SODeclOffset = variableReader.ReadUInt32("SODeclOffset"); var bytecodeReader = reader.CopyAtOffset("BytecodeReader", variableReader, (int)shaderOffset); var shaderSize = bytecodeReader.ReadUInt32("ShaderSize"); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(bytecodeReader.ReadBytes("Shader", (int)shaderSize)); } var declReader = reader.CopyAtOffset("DeclReader", variableReader, (int)SODeclOffset); result.SODecl = declReader.ReadString("SODecl"); return(result); }
public static EffectGSSOInitializer Parse(BytecodeReader reader, BytecodeReader variableReader) { var result = new EffectGSSOInitializer(); var shaderOffset = variableReader.ReadUInt32(); var SODeclOffset = variableReader.ReadUInt32(); var bytecodeReader = reader.CopyAtOffset((int)shaderOffset); var shaderSize = bytecodeReader.ReadUInt32(); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(bytecodeReader.ReadBytes((int)shaderSize)); } var declReader = reader.CopyAtOffset((int)SODeclOffset); result.SODecl = declReader.ReadString(); return(result); }
public static DebugEffectShaderData5 Parse(DebugBytecodeReader reader, DebugBytecodeReader variableReader) { var result = new DebugEffectShaderData5(); var shaderOffset = result.ShaderOffset = variableReader.ReadUInt32("ShaderOffset"); result.SODeclsOffset[0] = variableReader.ReadUInt32("SODeclsOffset0"); result.SODeclsOffset[1] = variableReader.ReadUInt32("SODeclsOffset1"); result.SODeclsOffset[2] = variableReader.ReadUInt32("SODeclsOffset2"); result.SODeclsOffset[3] = variableReader.ReadUInt32("SODeclsOffset3"); var SoDeclsCount = result.SODeclsCount = variableReader.ReadUInt32("SoDeclsCount"); result.RasterizedStream = variableReader.ReadUInt32("RasterizedStream"); var interfaceBindingCount = result.InterfaceBindingCount = variableReader.ReadUInt32("InterfaceBindingCount"); var interfaceBindingOffset = result.InterfaceBindingOffset = variableReader.ReadUInt32("InterfaceBindingOffset"); var shaderReader = reader.CopyAtOffset("ShaderReader", variableReader, (int)shaderOffset); var shaderSize = shaderReader.ReadUInt32("ShaderSize"); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(shaderReader.ReadBytes("Shader", (int)shaderSize)); } for (int i = 0; i < 4; i++) { var offset = result.SODeclsOffset[i]; if (offset != 0) { var soDeclReader = reader.CopyAtOffset("SODeclReader", variableReader, (int)offset); result.SODecls.Add(soDeclReader.ReadString("SODecls")); } } var interfaceReader = reader.CopyAtOffset("InterfaceReader", variableReader, (int)interfaceBindingOffset); for (int i = 0; i < interfaceBindingCount; i++) { result.InterfaceBindings.Add(DebugEffectInterfaceInitializer.Parse(reader, interfaceReader)); } return(result); }
public new static EffectInlineShader5Assignment Parse(BytecodeReader reader, BytecodeReader variableReader) { var result = new EffectInlineShader5Assignment(); var shaderOffset = variableReader.ReadUInt32(); result.SODeclsOffset[0] = variableReader.ReadUInt32(); result.SODeclsOffset[1] = variableReader.ReadUInt32(); result.SODeclsOffset[2] = variableReader.ReadUInt32(); result.SODeclsOffset[3] = variableReader.ReadUInt32(); var SoDeclsCount = result.SODeclsCount = variableReader.ReadUInt32(); result.RasterizedStream = variableReader.ReadUInt32(); var interfaceBindingCount = variableReader.ReadUInt32(); var interfaceBindingOffset = variableReader.ReadUInt32(); var shaderReader = reader.CopyAtOffset((int)shaderOffset); var shaderSize = shaderReader.ReadUInt32(); if (shaderSize != 0) { result.Shader = BytecodeContainer.Parse(shaderReader.ReadBytes((int)shaderSize)); } for (int i = 0; i < 4; i++) { var offset = result.SODeclsOffset[i]; if (offset != 0) { var soDeclReader = reader.CopyAtOffset((int)offset); result.SODecls.Add(soDeclReader.ReadString()); } } var interfaceReader = reader.CopyAtOffset((int)interfaceBindingOffset); for (int i = 0; i < interfaceBindingCount; i++) { result.InterfaceBindings.Add(EffectInterfaceInitializer.Parse(reader, interfaceReader)); } return(result); }
public void CanExecuteSimpleVertexShader(IShaderExecutor shaderExecutor) { // Arrange. VirtualMachine.ShaderExecutor = shaderExecutor; var vm = new VirtualMachine(BytecodeContainer.Parse(File.ReadAllBytes("Shaders/VS/Simple.o")), 1); // Set WorldViewProjection matrix into constant buffer vm.SetRegister(0, OperandType.ConstantBuffer, new RegisterIndex(0, 0), new Number4(1, 2, 3, 4)); vm.SetRegister(0, OperandType.ConstantBuffer, new RegisterIndex(0, 1), new Number4(5, 6, 7, 8)); vm.SetRegister(0, OperandType.ConstantBuffer, new RegisterIndex(0, 2), new Number4(4, 3, 2, 1)); vm.SetRegister(0, OperandType.ConstantBuffer, new RegisterIndex(0, 3), new Number4(8, 7, 6, 5)); // Set Inputs vm.SetRegister(0, OperandType.Input, new RegisterIndex(0), new Number4(3.0f, -5.0f, 2.0f, 0.0f)); // POSITION vm.SetRegister(0, OperandType.Input, new RegisterIndex(1), new Number4(0.0f, 1.0f, 0.0f, 0.0f)); // NORMAL vm.SetRegister(0, OperandType.Input, new RegisterIndex(2), new Number4(0.5f, 0.3f, 0.0f, 0.0f)); // TEXCOORD // Act. vm.Execute(); // Assert. var output0 = vm.GetRegister(0, OperandType.Output, new RegisterIndex(0)); Assert.That(output0, Is.EqualTo(new Number4(-14.0f, -18.0f, -22.0f, -26.0f))); var output1 = vm.GetRegister(0, OperandType.Output, new RegisterIndex(1)); Assert.That(output1.Number0.Float, Is.EqualTo(0.0f)); Assert.That(output1.Number1.Float, Is.EqualTo(1.0f)); Assert.That(output1.Number2.Float, Is.EqualTo(0.0f)); var output2 = vm.GetRegister(0, OperandType.Output, new RegisterIndex(2)); Assert.That(output2.Number0.Float, Is.EqualTo(0.5f)); Assert.That(output2.Number1.Float, Is.EqualTo(0.3f)); }
public static BytecodeContainer CompileFromFile(string fileName, string entryPoint, string profile) { var compiledShader = ShaderBytecode.CompileFromFile(fileName, entryPoint, profile); return(BytecodeContainer.Parse(compiledShader.Bytecode.Data)); }
internal InputLayout(Device device, InputElement[] elements, byte[] shaderBytecodeWithInputSignature) : this(device, elements, BytecodeContainer.Parse(shaderBytecodeWithInputSignature).InputSignature) { }
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]); } } }
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 CanExecuteCubeMapGeometryShader(IShaderExecutor shaderExecutor) { // Arrange. VirtualMachine.ShaderExecutor = shaderExecutor; var vm = new VirtualMachine(BytecodeContainer.Parse(File.ReadAllBytes("Shaders/GS/GS_CubeMap_GS.o")), 1); // Set constant buffer values. (These values are taken from the Rasterizr environment mapping sample). vm.SetConstantBufferRegisterValue(0, 0, new Number4(0.143317f, 0f, 0.976717f, 0.976619f)); vm.SetConstantBufferRegisterValue(0, 1, new Number4(0f, 1.000000f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 2, new Number4(-0.651080f, 0f, 0.214997f, 0.214975f)); vm.SetConstantBufferRegisterValue(0, 3, new Number4(-32.553982f, 0f, 9.749746f, 10.748772f)); vm.SetConstantBufferRegisterValue(0, 4, new Number4(-0.143317f, 0f, -0.976717f, -0.976619f)); vm.SetConstantBufferRegisterValue(0, 5, new Number4(0f, 1.000000f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 6, new Number4(0.651080f, 0f, -0.214997f, -0.214975f)); vm.SetConstantBufferRegisterValue(0, 7, new Number4(32.553982f, 0f, -11.749947f, -10.748772f)); vm.SetConstantBufferRegisterValue(0, 8, new Number4(0.651080f, 0.214975f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 9, new Number4(0f, 0f, 1.000100f, 1.000000f)); vm.SetConstantBufferRegisterValue(0, 10, new Number4(0.143317f, -0.976619f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 11, new Number4(7.165847f, -48.830967f, -1.000100f, 0)); vm.SetConstantBufferRegisterValue(0, 12, new Number4(0.651080f, -0.214975f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 13, new Number4(0f, 0f, -1.000100f, -1.000000f)); vm.SetConstantBufferRegisterValue(0, 14, new Number4(0.143317f, 0.976619f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 15, new Number4(7.165847f, 48.830967f, -1.000100f, 0)); vm.SetConstantBufferRegisterValue(0, 16, new Number4(0.651080f, 0f, -0.214997f, -0.214975f)); vm.SetConstantBufferRegisterValue(0, 17, new Number4(0f, 1.000000f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 18, new Number4(0.143317f, 0f, 0.976717f, 0.976619f)); vm.SetConstantBufferRegisterValue(0, 19, new Number4(7.165847f, 0f, 47.835758f, 48.830975f)); vm.SetConstantBufferRegisterValue(0, 20, new Number4(-0.651080f, 0f, 0.214997f, 0.214975f)); vm.SetConstantBufferRegisterValue(0, 21, new Number4(0f, 1.000000f, 0f, 0)); vm.SetConstantBufferRegisterValue(0, 22, new Number4(-0.143317f, 0f, -0.976717f, -0.976619f)); vm.SetConstantBufferRegisterValue(0, 23, new Number4(-7.165847f, 0f, -49.835957f, -48.830975f)); // Set input values. vm.SetInputRegisterValue(0, 0, 0, new Number4(12.834300f, 11.131500f, -0.087300f, 0)); vm.SetInputRegisterValue(0, 0, 1, new Number4(12.515460f, 11.131500f, -2.844318f, 0)); vm.SetInputRegisterValue(0, 0, 2, new Number4(-0.944098f, -0.255800f, 0.207817f, 0)); vm.SetInputRegisterValue(0, 0, 3, new Number4(56.420399f, -55.420399f, 0, 0)); vm.SetInputRegisterValue(0, 1, 0, new Number4(11.860000f, 11.131500f, 4.847000f, 0)); vm.SetInputRegisterValue(0, 1, 1, new Number4(12.624693f, 11.131500f, 2.184066f, 0)); vm.SetInputRegisterValue(0, 1, 2, new Number4(-0.951641f, -0.256300f, -0.169278f, 0)); vm.SetInputRegisterValue(0, 1, 3, new Number4(49.367802f, -55.420399f, 0, 0)); vm.SetInputRegisterValue(0, 2, 0, new Number4(11.698200f, 11.794400f, 4.778200f, 0)); vm.SetInputRegisterValue(0, 2, 1, new Number4(12.451886f, 11.794400f, 2.151658f, 0)); vm.SetInputRegisterValue(0, 2, 2, new Number4(-0.951859f, 0.256000f, -0.168411f, 0)); vm.SetInputRegisterValue(0, 2, 3, new Number4(49.367802f, -54.715099f, 0, 0)); var expectedOutputsForFirstVertex = new[] { new Number4(-30.657768f, 11.131500f, 22.266457f, 23.264225f), new Number4(12.515460f, 11.131500f, -2.844318f, 0), new Number4(-0.944098f, -0.255800f, 0.207817f, 0), new Number4(56.420399f, -55.420399f, 0, 0), new Number4(0, 0, 0, 0) }; // Act. var executionEnumerator = vm.ExecuteMultiple().GetEnumerator(); int index = 0; while (true) { executionEnumerator.MoveNext(); if (executionEnumerator.Current == ExecutionResponse.Finished) { break; } Assert.That(executionEnumerator.Current, Is.EqualTo(ExecutionResponse.Emit)); if (index == 0) { for (ushort i = 0; i < expectedOutputsForFirstVertex.Length; i++) { var output = vm.GetRegister(0, OperandType.Output, new RegisterIndex(i)); Assert.That(output, Is.EqualTo(expectedOutputsForFirstVertex[i])); } } executionEnumerator.MoveNext(); Assert.That(executionEnumerator.Current, Is.EqualTo(ExecutionResponse.Emit)); executionEnumerator.MoveNext(); Assert.That(executionEnumerator.Current, Is.EqualTo(ExecutionResponse.Emit)); executionEnumerator.MoveNext(); Assert.That(executionEnumerator.Current, Is.EqualTo(ExecutionResponse.Cut)); // SV_RenderTargetArrayIndex var output5 = vm.GetRegister(0, OperandType.Output, new RegisterIndex(4)); Assert.That(output5.Number0.Int, Is.EqualTo(index)); index++; } Assert.That(index, Is.EqualTo(6)); }
public ShaderBase(Device device, byte[] shaderBytecode) : base(device) { _shaderBytecode = BytecodeContainer.Parse(shaderBytecode); }