Пример #1
0
        private D3D9Instruction ReadInstruction()
        {
            uint   instructionToken = ReadUInt32();
            Opcode opcode           = (Opcode)(instructionToken & 0xffff);

            int size;

            if (opcode == Opcode.Comment)
            {
                size = (int)((instructionToken >> 16) & 0x7FFF);
            }
            else
            {
                size = (int)((instructionToken >> 24) & 0x0f);
            }

            uint[] paramTokens = new uint[size];
            for (int i = 0; i < size; i++)
            {
                paramTokens[i] = ReadUInt32();
            }
            var instruction = new D3D9Instruction(opcode, paramTokens);

            if (opcode != Opcode.Comment)
            {
                instruction.Modifier   = (int)((instructionToken >> 16) & 0xff);
                instruction.Predicated = (instructionToken & 0x10000000) != 0;
                System.Diagnostics.Debug.Assert((instructionToken & 0xE0000000) == 0);
            }

            return(instruction);
        }
Пример #2
0
        private static string GetSourceName(D3D9Instruction instruction, int srcIndex)
        {
            string sourceName = instruction.GetParamRegisterName(srcIndex);

            if (instruction.Opcode != Opcode.Loop)
            {
                sourceName += instruction.GetSourceSwizzleName(srcIndex);
                sourceName  = ApplyModifier(instruction.GetSourceModifier(srcIndex), sourceName);
            }
            return(sourceName);
        }
Пример #3
0
        private static string GetModifier(D3D9Instruction instruction)
        {
            ResultModifier resultModifier = instruction.GetDestinationResultModifier();

            switch (resultModifier)
            {
            case ResultModifier.None:
                return(string.Empty);

            case ResultModifier.Centroid:
                return("_centroid");

            case ResultModifier.PartialPrecision:
                return("_pp");

            case ResultModifier.Saturate:
                return("_sat");

            default:
                throw new NotSupportedException("Not supported result modifier " + resultModifier);
            }
        }
Пример #4
0
        virtual public ShaderModel ReadShader()
        {
            // Version token
            byte       minorVersion = ReadByte();
            byte       majorVersion = ReadByte();
            ShaderType shaderType   = (ShaderType)ReadUInt16();

            var shader = new ShaderModel(majorVersion, minorVersion, shaderType);

            while (true)
            {
                D3D9Instruction instruction = ReadInstruction();
                InstructionVerifier.Verify(instruction);
                shader.Instructions.Add(instruction);
                if (instruction.Opcode == Opcode.End)
                {
                    break;
                }
            }

            return(shader);
        }
Пример #5
0
        private void WriteInstruction(D3D9Instruction instruction)
        {
            switch (instruction.Opcode)
            {
            case Opcode.Abs:
                WriteLine("abs{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Add:
                WriteLine("add{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Cmp:
                WriteLine("cmp{0} {1}, {2}, {3}, {4}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2), GetSourceName(instruction, 3));
                break;

            case Opcode.Dcl:
                string dclInstruction = "dcl";
                if (instruction.GetParamRegisterType(1) != RegisterType.MiscType)
                {
                    dclInstruction += "_" + instruction.GetDeclSemantic().ToLower();
                }
                WriteLine("{0} {1}", dclInstruction, GetDestinationName(instruction));
                break;

            case Opcode.Def:
            {
                string constRegisterName = instruction.GetParamRegisterName(0);
                string constValue0       = ConstantFormatter.Format(instruction.GetParamSingle(1));
                string constValue1       = ConstantFormatter.Format(instruction.GetParamSingle(2));
                string constValue2       = ConstantFormatter.Format(instruction.GetParamSingle(3));
                string constValue3       = ConstantFormatter.Format(instruction.GetParamSingle(4));
                WriteLine("def {0}, {1}, {2}, {3}, {4}", constRegisterName, constValue0, constValue1, constValue2, constValue3);
            }
            break;

            case Opcode.DefI:
            {
                string constRegisterName = instruction.GetParamRegisterName(0);
                WriteLine("defi {0}, {1}, {2}, {3}, {4}", constRegisterName,
                          instruction.Params[1], instruction.Params[2], instruction.Params[3], instruction.Params[4]);
            }
            break;

            case Opcode.DP2Add:
                WriteLine("dp2add {0}, {1}, {2}, {3}", GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2), GetSourceName(instruction, 3));
                break;

            case Opcode.Dp3:
                WriteLine("dp3{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Dp4:
                WriteLine("dp4{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Else:
                WriteLine("else");
                break;

            case Opcode.Endif:
                WriteLine("endif");
                break;

            case Opcode.EndLoop:
                WriteLine("endloop");
                break;

            case Opcode.EndRep:
                WriteLine("endrep");
                break;

            case Opcode.Exp:
                WriteLine("exp{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Frc:
                WriteLine("frac {0}, {1}", GetDestinationName(instruction), GetSourceName(instruction, 1));
                break;

            case Opcode.If:
                WriteLine("if {0}", GetSourceName(instruction, 0));
                break;

            case Opcode.IfC:
                WriteLine("if_{0} {1}, {2}",
                          ((IfComparison)instruction.Modifier).ToString().ToLower(),
                          GetSourceName(instruction, 0), GetSourceName(instruction, 1));
                break;

            case Opcode.Log:
                WriteLine("log{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Loop:
                WriteLine("loop {0}, {1}", GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Lrp:
                WriteLine("lrp{0} {1}, {2}, {3}, {4}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2), GetSourceName(instruction, 3));
                break;

            case Opcode.Mad:
                WriteLine("mad{0} {1}, {2}, {3}, {4}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2), GetSourceName(instruction, 3));
                break;

            case Opcode.Max:
                WriteLine("max{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Min:
                WriteLine("min{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Mov:
                WriteLine("mov{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.MovA:
                WriteLine("mova{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Mul:
                WriteLine("mul{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Nop:
                WriteLine("nop");
                break;

            case Opcode.Nrm:
                WriteLine("nrm{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Pow:
                WriteLine("pow{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Rep:
                WriteLine("rep {0}", GetDestinationName(instruction));
                break;

            case Opcode.Rcp:
                WriteLine("rcp{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Rsq:
                WriteLine("rsq{0} {1}, {2}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1));
                break;

            case Opcode.Sge:
                WriteLine("sge{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Slt:
                WriteLine("slt{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.SinCos:
                if (shader.MajorVersion >= 3)
                {
                    WriteLine("sincos {0}, {1}", GetDestinationName(instruction),
                              GetSourceName(instruction, 1));
                }
                else
                {
                    WriteLine("sincos {0}, {1}, {2}, {3}", GetDestinationName(instruction),
                              GetSourceName(instruction, 1), GetSourceName(instruction, 2), GetSourceName(instruction, 3));
                }
                break;

            case Opcode.Sub:
                WriteLine("sub{0} {1}, {2}, {3}", GetModifier(instruction), GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.Tex:
                if ((shader.MajorVersion == 1 && shader.MinorVersion >= 4) || (shader.MajorVersion > 1))
                {
                    WriteLine("texld {0}, {1}, {2}", GetDestinationName(instruction),
                              GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                }
                else
                {
                    WriteLine("tex {0}", GetDestinationName(instruction));
                }
                break;

            case Opcode.TexLDL:
                WriteLine("texldl {0}, {1}, {2}", GetDestinationName(instruction),
                          GetSourceName(instruction, 1), GetSourceName(instruction, 2));
                break;

            case Opcode.TexKill:
                WriteLine("texkill {0}", GetDestinationName(instruction));
                break;

            case Opcode.Comment:
            case Opcode.End:
                break;

            default:
                WriteLine(instruction.Opcode.ToString());
                Console.WriteLine(instruction.Opcode);
                //throw new NotImplementedException();
                break;
            }
        }