public override object Execute(List <string> args) { if (args.Count < 2) { return(false); } bool use_assembly_compiler = args.Count >= 2 && args[0].ToLower() == "asm"; string file = args[args.Count - 1]; string shader_code = File.ReadAllText(file); if (use_assembly_compiler && args.Count < 3) { return(false); } int index = use_assembly_compiler ? int.Parse(args[1]) : int.Parse(args[0]); if (!use_assembly_compiler) { using (var reader = new StringReader(shader_code)) { for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) { var str = line.Trim(); if (str == "ps_3_0" || str == "vs_3_0") { use_assembly_compiler = true; break; } } } } byte[] bytecode = null; if (use_assembly_compiler) { bytecode = D3DCompiler.Assemble(shader_code); } else { var profile = IsVertexShader ? "vs_3_0" : "ps_3_0"; try { bytecode = D3DCompiler.CompileFromFile(shader_code, "main", profile); } catch (Exception e) { Console.WriteLine(e); return(true); } } var disassembly = D3DCompiler.Disassemble(bytecode); Console.WriteLine(disassembly); if (typeof(T) == typeof(PixelShader) || typeof(T) == typeof(GlobalPixelShader)) { var shader_data_block = new PixelShaderBlock { PCShaderBytecode = bytecode }; if (typeof(T) == typeof(PixelShader)) { var _definition = Definition as PixelShader; var existing_block = _definition.Shaders[index]; shader_data_block.PCParameters = existing_block.PCParameters; _definition.Shaders[index] = shader_data_block; } if (typeof(T) == typeof(GlobalPixelShader)) { var _definition = Definition as GlobalPixelShader; var existing_block = _definition.Shaders[index]; shader_data_block.PCParameters = existing_block.PCParameters; _definition.Shaders[index] = shader_data_block; } } if (typeof(T) == typeof(VertexShader) || typeof(T) == typeof(GlobalVertexShader)) { var shader_data_block = new VertexShaderBlock { PCShaderBytecode = bytecode }; if (typeof(T) == typeof(VertexShader)) { var _definition = Definition as VertexShader; var existing_block = _definition.Shaders[index]; shader_data_block.PCParameters = existing_block.PCParameters; _definition.Shaders[index] = shader_data_block; } if (typeof(T) == typeof(GlobalVertexShader)) { var _definition = Definition as GlobalVertexShader; var existing_block = _definition.Shaders[index]; shader_data_block.PCParameters = existing_block.PCParameters; _definition.Shaders[index] = shader_data_block; } } return(true); }
public override object Execute(List <string> args) { if (args.Count != 1) { return(false); } if (typeof(T) == typeof(PixelShader) || typeof(T) == typeof(GlobalPixelShader)) { PixelShaderBlock shader_block = null; if (typeof(T) == typeof(PixelShader)) { var _definition = Definition as PixelShader; shader_block = _definition.Shaders[int.Parse(args[0])]; } if (typeof(T) == typeof(GlobalPixelShader)) { var _definition = Definition as GlobalPixelShader; shader_block = _definition.Shaders[int.Parse(args[0])]; } var pc_shader = shader_block.PCShaderBytecode; var disassembly = D3DCompiler.Disassemble(pc_shader); if (pc_shader != null) { Console.WriteLine(disassembly); } else { Console.WriteLine("Failed to disassemble shader"); } } if (typeof(T) == typeof(VertexShader) || typeof(T) == typeof(GlobalVertexShader)) { VertexShaderBlock shader_block = null; if (typeof(T) == typeof(VertexShader)) { var _definition = Definition as VertexShader; shader_block = _definition.Shaders[int.Parse(args[0])]; } if (typeof(T) == typeof(GlobalVertexShader)) { var _definition = Definition as GlobalVertexShader; shader_block = _definition.Shaders[int.Parse(args[0])]; } var pc_shader = shader_block.PCShaderBytecode; var disassembly = D3DCompiler.Disassemble(pc_shader); if (pc_shader != null) { Console.WriteLine(disassembly); } else { Console.WriteLine("Failed to disassemble shader"); } } return(true); }
private void GenerateGen3ShaderHeader(int shaderIndex, StreamWriter writer) { List <ShaderParameter> parameters = null; if (typeof(T) == typeof(PixelShader) || typeof(T) == typeof(GlobalPixelShader)) { PixelShaderBlock shader_block = null; if (typeof(T) == typeof(PixelShader)) { var _definition = Definition as PixelShader; if (shaderIndex < _definition.Shaders.Count) { shader_block = _definition.Shaders[shaderIndex]; } else { return; } } if (typeof(T) == typeof(GlobalPixelShader)) { var _definition = Definition as GlobalPixelShader; if (shaderIndex < _definition.Shaders.Count) { shader_block = _definition.Shaders[shaderIndex]; } else { return; } } parameters = shader_block.XboxParameters; } if (typeof(T) == typeof(VertexShader) || typeof(T) == typeof(GlobalVertexShader)) { VertexShaderBlock shaderBlock = null; if (typeof(T) == typeof(VertexShader)) { var _definition = Definition as VertexShader; if (shaderIndex < _definition.Shaders.Count) { shaderBlock = _definition.Shaders[shaderIndex]; } else { return; } } if (typeof(T) == typeof(GlobalVertexShader)) { var _definition = Definition as GlobalVertexShader; if (shaderIndex < _definition.Shaders.Count) { shaderBlock = _definition.Shaders[shaderIndex]; } else { return; } } parameters = shaderBlock.XboxParameters; } List <string> parameterNames = new List <string>(); for (int i = 0; i < parameters.Count; i++) { parameterNames.Add(Cache.StringTable.GetString(parameters[i].ParameterName)); } WriteHeaderLine(writer); WriteHeaderLine(writer, "Generated by TagTool and Xbox Shader Disassembler"); WriteHeaderLine(writer); WriteHeaderLine(writer, "Parameters"); WriteHeaderLine(writer); for (int i = 0; i < parameters.Count; i++) { var param = parameters[i]; WriteHeaderLine(writer, $" {GetTypeString(param.RegisterType, param.RegisterCount)} {parameterNames[i]};"); } WriteHeaderLine(writer); WriteHeaderLine(writer); WriteHeaderLine(writer, "Registers"); WriteHeaderLine(writer); // sort later for (int i = 0; i < parameters.Count; i++) { var param = parameters[i]; WriteHeaderLine(writer, $" {parameterNames[i]} {GetRegisterString(param.RegisterType, param.RegisterIndex)} {param.RegisterCount}"); } WriteHeaderLine(writer); }
private string Disassemble(int shaderIndex) { string disassembly = null; if (typeof(T) == typeof(PixelShader) || typeof(T) == typeof(GlobalPixelShader)) { PixelShaderBlock shader_block = null; if (typeof(T) == typeof(PixelShader)) { var _definition = Definition as PixelShader; if (shaderIndex < _definition.Shaders.Count) { shader_block = _definition.Shaders[shaderIndex]; } else { return(null); } } if (typeof(T) == typeof(GlobalPixelShader)) { var _definition = Definition as GlobalPixelShader; if (shaderIndex < _definition.Shaders.Count) { shader_block = _definition.Shaders[shaderIndex]; } else { return(null); } } var pc_shader = shader_block.PCShaderBytecode; disassembly = D3DCompiler.Disassemble(pc_shader); if (pc_shader == null) { disassembly = null; } } if (typeof(T) == typeof(VertexShader) || typeof(T) == typeof(GlobalVertexShader)) { VertexShaderBlock shader_block = null; if (typeof(T) == typeof(VertexShader)) { var _definition = Definition as VertexShader; if (shaderIndex < _definition.Shaders.Count) { shader_block = _definition.Shaders[shaderIndex]; } else { return(null); } } if (typeof(T) == typeof(GlobalVertexShader)) { var _definition = Definition as GlobalVertexShader; if (shaderIndex < _definition.Shaders.Count) { shader_block = _definition.Shaders[shaderIndex]; } else { return(null); } } var pc_shader = shader_block.PCShaderBytecode; disassembly = D3DCompiler.Disassemble(pc_shader); if (pc_shader == null) { disassembly = null; } } return(disassembly); }
private string DisassembleGen3Shader(int shaderIndex) { var file = UseXSDCommand.XSDFileInfo; if (file == null) { Console.WriteLine("xsd.exe not found! use command usexsd <directory> to load xsd.exe"); return(null); } var tempFile = Path.GetTempFileName(); string disassembly = null; string xsdArguments = " \"" + tempFile + "\""; byte[] microcode = new byte[0]; byte[] debugData = new byte[0]; byte[] constantData = new byte[0]; // // Set the arguments for xsd.exe according to the XDK documentation // try { if (typeof(T) == typeof(PixelShader) || typeof(T) == typeof(GlobalPixelShader)) { xsdArguments = "/rawps" + xsdArguments; PixelShaderBlock shaderBlock = null; if (typeof(T) == typeof(PixelShader)) { var _definition = Definition as PixelShader; if (shaderIndex < _definition.Shaders.Count) { shaderBlock = _definition.Shaders[shaderIndex]; } else { return(null); } } if (typeof(T) == typeof(GlobalPixelShader)) { var _definition = Definition as GlobalPixelShader; if (shaderIndex < _definition.Shaders.Count) { shaderBlock = _definition.Shaders[shaderIndex]; } else { return(null); } } microcode = shaderBlock.XboxShaderReference.ShaderData; debugData = shaderBlock.XboxShaderReference.DebugData; constantData = shaderBlock.XboxShaderReference.ConstantData; } if (typeof(T) == typeof(VertexShader) || typeof(T) == typeof(GlobalVertexShader)) { xsdArguments = "/rawvs" + xsdArguments; VertexShaderBlock shaderBlock = null; if (typeof(T) == typeof(VertexShader)) { var _definition = Definition as VertexShader; if (shaderIndex < _definition.Shaders.Count) { shaderBlock = _definition.Shaders[shaderIndex]; } else { return(null); } } if (typeof(T) == typeof(GlobalVertexShader)) { var _definition = Definition as GlobalVertexShader; if (shaderIndex < _definition.Shaders.Count) { shaderBlock = _definition.Shaders[shaderIndex]; } else { return(null); } } microcode = shaderBlock.XboxShaderReference.ShaderData; debugData = shaderBlock.XboxShaderReference.DebugData; constantData = shaderBlock.XboxShaderReference.ConstantData; } File.WriteAllBytes(tempFile, debugData); File.WriteAllBytes(tempFile, constantData); File.WriteAllBytes(tempFile, microcode); ProcessStartInfo info = new ProcessStartInfo(UseXSDCommand.XSDFileInfo.FullName) { Arguments = xsdArguments, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, RedirectStandardError = false, RedirectStandardOutput = true, RedirectStandardInput = false }; Process xsd = Process.Start(info); disassembly = xsd.StandardOutput.ReadToEnd(); xsd.WaitForExit(); } catch (Exception e) { Console.WriteLine($"Failed to disassemble shader: {e.ToString()}"); } finally { File.Delete(tempFile); } return(disassembly); }