public static DebugShaderModel Parse(DebugBytecodeReader reader)
        {
            var result = new DebugShaderModel();

            result.MinorVersion = reader.ReadByte("MinorVersion");
            result.MajorVersion = reader.ReadByte("MajorVersion");
            result.Type         = reader.ReadEnum16 <ShaderType>("ShaderType");
            while (true)
            {
                var    token  = reader.PeakUint32();
                Opcode opcode = (Opcode)(token & 0xffff);
                if (opcode == Opcode.Comment && result.ReadCommentToken(reader))
                {
                    continue;
                }
                reader.AddIndent($"T{result.Tokens.Count}");

                var         indent      = reader.LocalMembers.OfType <DebugIndent>().Last();
                IDebugToken instruction = result.ReadInstruction(reader);
                result.Tokens.Add(instruction);
                indent.Name += $" {instruction.Opcode} {string.Join(" ", instruction.Operands)}";
                reader.RemoveIndent();
                if (instruction.Opcode == Opcode.End)
                {
                    break;
                }
            }
            return(result);
        }
Beispiel #2
0
 public static DebugPSInfo Parse(DebugBytecodeReader reader)
 {
     return(new DebugPSInfo()
     {
         DepthOutput = reader.ReadByte("DepthOutput"),
         SampleFrequency = reader.ReadByte("SampleFrequency")
     });
 }
        public static DebugSamplerMapping Parse(DebugBytecodeReader reader)
        {
            var result = new DebugSamplerMapping();

            result.TargetSampler  = reader.ReadByte("TargetSampler");
            result.SourceSampler  = reader.ReadByte("SourceSampler");
            result.SourceResource = reader.ReadByte("SourceResource");
            var padding = reader.ReadByte("padding");

            return(result);
        }
 public static DebugVSInfo Parse(DebugBytecodeReader reader)
 {
     return(new DebugVSInfo()
     {
         OutputPositionPresent = reader.ReadByte("OutputPositionPresent"),
     });
 }
Beispiel #5
0
 public static DebugDSInfo Parse(DebugBytecodeReader reader)
 {
     return(new DebugDSInfo()
     {
         InputControlPointCount = reader.ReadUInt32("InputControlPointCount"),
         OutputPositionPresent = reader.ReadByte("OutputPositionPresent"),
         TessellatorDomain = reader.ReadUInt32("TessellatorDomain"),
     });
 }
 public static DebugGSInfo Parse(DebugBytecodeReader reader)
 {
     return(new DebugGSInfo()
     {
         InputPrimitive = reader.ReadUInt32("InputPrimitive"),
         OutputTopology = reader.ReadUInt32("OutputTopology"),
         OutputStreamMask = reader.ReadUInt32("OutputStreamMask"),
         OutputPositionPresent = reader.ReadByte("OutputPositionPresent")
     });
 }
        internal void UpdateVersion(DebugShaderVersion version)
        {
            if (Reader == null)
            {
                return;
            }
            Reader.ReadUInt32("Unknown");
            switch (version.ProgramType)
            {
            case ProgramType.VertexShader:
                Info = DebugVSInfo.Parse(Reader);
                break;

            case ProgramType.HullShader:
                Info = DebugHSInfo.Parse(Reader);
                break;

            case ProgramType.DomainShader:
                Info = DebugDSInfo.Parse(Reader);
                break;

            case ProgramType.GeometryShader:
                Info = DebugGSInfo.Parse(Reader);
                break;

            case ProgramType.PixelShader:
                Info = DebugPSInfo.Parse(Reader);
                break;

            default:
                Reader.ReadBytes("Unknown", 32);
                break;
            }
            if (Info != null && Info.StructSize < DebugValidationInfo.UnionSize)
            {
                Reader.ReadBytes("Padding", DebugValidationInfo.UnionSize - Info.StructSize);
            }
            MinimumExpectedWaveLaneCount = Reader.ReadUInt32("MinimumExpectedWaveLaneCount");
            MaximumExpectedWaveLaneCount = Reader.ReadUInt32("MaximumExpectedWaveLaneCount");
            if (ChunkSize > 20)
            {
                Reader.ReadEnum8 <PSVShaderKind>("ShaderStage");
                UsesViewID                  = Reader.ReadByte("UsesViewID");
                GSMaxVertexCount            = Reader.ReadByte("MaxVertexCount");
                SigInputElements            = Reader.ReadByte("SigInputElements");
                SigOutputElements           = Reader.ReadByte("SigOutputElements");
                SigPatchConstOrPrimElements = Reader.ReadByte("SigPatchConstOrPrimElements");
                SigInputVectors             = Reader.ReadByte("SigInputVectors");
                SigOutputVectors            = Reader.ReadBytes("SigOutputVectors", 4);
            }
            Reader = null;
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            args = new string[]
            {
                "-O",
                "test.html",
                "-h",
                @"C:\Files\KM\ShaderStudio\src\UnityTests\bin\Debug\Blob\outer_wilds\Assets\Shader\SpritesDefault\VS_BF58DDF502C51FEB5145F1D0310B4327.o"
            };
            var options = new Options();

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-O":
                    if (args.Length <= i + 1)
                    {
                        Console.Error.WriteLine("No output path specified");
                        return;
                    }
                    options.DestPath = args[i + 1];
                    i += 1;
                    break;

                case "-a":
                    options.Mode = DecompileMode.Dissassemble;
                    break;

                case "-d":
                    options.Mode = DecompileMode.Debug;
                    break;

                case "-h":
                    options.Mode = DecompileMode.DebugHtml;
                    break;

                default:
                    options.SourcePath = args[i];
                    break;
                }
            }
            if (string.IsNullOrEmpty(options.SourcePath))
            {
                Console.Error.WriteLine("No source path specified");
                Environment.Exit(1);
            }

            byte[] data = null;
            try
            {
                data = File.ReadAllBytes(options.SourcePath);
            } catch (Exception ex) {
                Console.Error.WriteLine("Error reading source");
                Console.Error.WriteLine(ex);
                Environment.Exit(1);
            }
            var programType = GetProgramType(data);

            using (var sw = GetStream(options))
            {
                if (programType == ProgramType.Unknown)
                {
                    Console.Error.WriteLine($"Unable to identify shader object format");
                    Environment.Exit(1);
                }
                else if (programType == ProgramType.DXBC)
                {
                    if (options.Mode == DecompileMode.Dissassemble)
                    {
                        var container = new BytecodeContainer(data);
                        sw.Write(container.ToString());
                    }
                    else if (options.Mode == DecompileMode.Decompile)
                    {
                        var hlsl = DXDecompiler.Decompile(data);
                        sw.Write(hlsl);
                    }
                    else if (options.Mode == DecompileMode.Debug)
                    {
                        sw.WriteLine(string.Join(" ", args));
                        var shaderBytecode = DebugBytecodeContainer.Parse(data);
                        var result         = shaderBytecode.Dump();
                        sw.Write(result);
                    }
                    else if (options.Mode == DecompileMode.DebugHtml)
                    {
                        var shaderBytecode = DebugBytecodeContainer.Parse(data);
                        var result         = shaderBytecode.DumpHTML();
                        sw.Write(result);
                    }
                }
                else if (programType == ProgramType.DX9)
                {
                    if (options.Mode == DecompileMode.Dissassemble)
                    {
                        var disasm = SlimShader.DX9Shader.AsmWriter.Disassemble(data);
                        sw.Write(disasm);
                    }
                    else if (options.Mode == DecompileMode.Decompile)
                    {
                        var hlsl = SlimShader.DX9Shader.HlslWriter.Decompile(data);
                        sw.Write(hlsl);
                    }
                    else if (options.Mode == DecompileMode.Debug)
                    {
                        sw.WriteLine(string.Join(" ", args));
                        var shaderType = (SlimShader.DX9Shader.ShaderType)BitConverter.ToUInt16(data, 2);
                        if (shaderType == SlimShader.DX9Shader.ShaderType.Effect)
                        {
                            var    reader = new DebugBytecodeReader(data, 0, data.Length);
                            string error  = "";
                            try
                            {
                                reader.ReadByte("minorVersion");
                                reader.ReadByte("majorVersion");
                                reader.ReadUInt16("shaderType");
                                DebugEffectChunk.Parse(reader, (uint)(data.Length - 4));
                            }
                            catch (Exception ex)
                            {
                                error = ex.ToString();
                            }
                            var dump = reader.DumpStructure();
                            if (!string.IsNullOrEmpty(error))
                            {
                                dump += "\n" + error;
                            }
                            sw.Write(dump);
                        }
                        else
                        {
                            var    reader = new DebugBytecodeReader(data, 0, data.Length);
                            string error  = "";
                            try
                            {
                                DebugShaderModel.Parse(reader);
                            }
                            catch (Exception ex)
                            {
                                error = ex.ToString();
                            }
                            var dump = reader.DumpStructure();
                            if (!string.IsNullOrEmpty(error))
                            {
                                dump += "\n" + error;
                            }
                            sw.Write(dump);
                        }
                    }
                    else if (options.Mode == DecompileMode.DebugHtml)
                    {
                        var shaderType = (SlimShader.DX9Shader.ShaderType)BitConverter.ToUInt16(data, 2);
                        if (shaderType == SlimShader.DX9Shader.ShaderType.Effect)
                        {
                            var    reader = new DebugBytecodeReader(data, 0, data.Length);
                            string error  = "";
                            try
                            {
                                reader.ReadByte("minorVersion");
                                reader.ReadByte("majorVersion");
                                reader.ReadUInt16("shaderType");
                                DebugEffectChunk.Parse(reader, (uint)(data.Length - 4));
                            }
                            catch (Exception ex)
                            {
                                error = ex.ToString();
                            }
                            var dump = reader.DumpHtml();
                            if (!string.IsNullOrEmpty(error))
                            {
                                dump += "\n" + error;
                            }
                            sw.Write(dump);
                        }
                        else
                        {
                            var    reader = new DebugBytecodeReader(data, 0, data.Length);
                            string error  = "";
                            try
                            {
                                DebugShaderModel.Parse(reader);
                            }
                            catch (Exception ex)
                            {
                                error = ex.ToString();
                            }
                            var dump = reader.DumpHtml();
                            if (!string.IsNullOrEmpty(error))
                            {
                                dump += "\n" + error;
                            }
                            sw.Write(dump);
                        }
                    }
                }
            }
        }