public void DecompileTest(string baseFilename)
        {
            string compiledShaderFilename = $"CompiledShaders{Path.DirectorySeparatorChar}{baseFilename}.fxc";
            string asmExpectedFilename    = $"ShaderAssembly{Path.DirectorySeparatorChar}{baseFilename}.asm";
            string hlslExpectedFilename   = $"ShaderSources{Path.DirectorySeparatorChar}{baseFilename}.fx";
            string asmOutputFilename      = $"{baseFilename}.asm";
            string hlslOutputFilename     = $"{baseFilename}.fx";

            ShaderModel shader;

            var inputStream = File.Open(compiledShaderFilename, FileMode.Open, FileAccess.Read);

            using (var input = new ShaderReader(inputStream, true))
            {
                shader = input.ReadShader();
            }

            var asmWriter = new AsmWriter(shader);

            asmWriter.Write(asmOutputFilename);

            var hlslWriter = new HlslWriter(shader, true);

            hlslWriter.Write(hlslOutputFilename);

            FileAssert.AreEqual(asmExpectedFilename, asmOutputFilename, "Assembly not equal");
            FileAssert.AreEqual(hlslExpectedFilename, hlslOutputFilename, "HLSL not equal");
        }
Example #2
0
        public void AsmMatchesFxc(string relPath)
        {
            string file = $"{ShaderDirectory}/{relPath}";
            // Arrange.
            var asmFileText = string.Join(Environment.NewLine,
                                          File.ReadAllLines(file + ".asm").Select(x => x.Trim()));

            asmFileText = TestUtils.NormalizeAssembly(asmFileText);
            // Act.
            var bytecode = File.ReadAllBytes(file + ".o");
            var shader   = ShaderReader.ReadShader(bytecode);

            var    asmWriter     = new AsmWriter(shader);
            string decompiledAsm = "";

            using (var stream = new MemoryStream())
            {
                asmWriter.Write(stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream, Encoding.UTF8))
                {
                    decompiledAsm = reader.ReadToEnd();
                }
            }
            var decompiledAsmText = string.Join(Environment.NewLine, decompiledAsm
                                                .Split(new[] { Environment.NewLine }, StringSplitOptions.None)
                                                .Select(x => x.Trim()));

            File.WriteAllText($"{file}.d.asm", decompiledAsm);

            // Assert.
            Assert.That(decompiledAsmText, Is.EqualTo(asmFileText));
        }