public void Decompile(string relPath) { string file = $"{ShaderDirectory}/{relPath}"; // Arrange. // Act. var bytecode = File.ReadAllBytes(file + ".o"); var shader = ShaderReader.ReadShader(bytecode); var hlslWriter = new HlslWriter(shader); string decompiledHlsl = ""; using (var stream = new MemoryStream()) { hlslWriter.Write(stream); stream.Position = 0; using (var reader = new StreamReader(stream, Encoding.UTF8)) { decompiledHlsl = reader.ReadToEnd(); } } File.WriteAllText($"{file}.d.hlsl", decompiledHlsl); // Assert. }
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"); }
public void RecompileShaders(string relPath) { string file = $"{ShaderDirectory}/{relPath}"; // Arrange. var relDir = Path.GetDirectoryName(relPath); Directory.CreateDirectory($"{OutputDir}/{relDir}"); var sourceName = GetSourceNameFromObject($"{ShaderDirectory}/{relPath}.o"); if (ShaderDirectory != OutputDir) { File.Copy($"{ShaderDirectory}/{relDir}/{sourceName}", $"{OutputDir}/{relDir}/{sourceName}", true); } if (ShaderDirectory != OutputDir) { File.Copy($"{ShaderDirectory}/{relPath}.asm", $"{OutputDir}/{relPath}.asm", true); } var asmFileText = string.Join(Environment.NewLine, File.ReadAllLines(file + ".asm").Select(x => x.Trim())); // Act. var binaryFileBytes = File.ReadAllBytes(file + ".o"); var shaderModel = ShaderReader.ReadShader(binaryFileBytes); var hlslWriter = new HlslWriter(shaderModel); string decompiledHLSL = ""; using (var stream = new MemoryStream()) { hlslWriter.Write(stream); stream.Position = 0; using (var reader = new StreamReader(stream, Encoding.UTF8)) { decompiledHLSL = reader.ReadToEnd(); } } File.WriteAllText($"{OutputDir}/{relPath}.d.hlsl", decompiledHLSL); using (var shaderBytecode = ShaderBytecode.FromStream(new MemoryStream(binaryFileBytes))) { var profile = shaderModel.Type == DX9Shader.ShaderType.Pixel ? $"ps_{shaderModel.MajorVersion}_{shaderModel.MinorVersion}" : $"vs_{shaderModel.MajorVersion}_{shaderModel.MinorVersion}"; var compiledShader = ShaderBytecode.Compile(decompiledHLSL, "main", profile); var disassembly = shaderBytecode.Disassemble(); var redisassembly = compiledShader.Bytecode.Disassemble(); File.WriteAllText($"{OutputDir}/{relPath}.d1.asm", disassembly); File.WriteAllText($"{OutputDir}/{relPath}.d2.asm", redisassembly); // Assert. Warn.If(disassembly, Is.EqualTo(redisassembly)); } // Assert. Assert.Pass(); }