internal void UpdateVersion(ShaderVersion version)
        {
            if (Reader == null)
            {
                return;
            }
            Reader.ReadUInt32();             //Unknown
            switch (version.ProgramType)
            {
            case ProgramType.VertexShader:
                Info = VSInfo.Parse(Reader);
                break;

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

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

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

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

            default:
                Reader.ReadBytes(ValidationInfo.UnionSize);
                break;
            }
            if (Info != null && Info.StructSize < ValidationInfo.UnionSize)
            {
                //Padding
                Reader.ReadBytes(ValidationInfo.UnionSize - Info.StructSize);
            }
            MinimumExpectedWaveLaneCount = Reader.ReadUInt32();
            MaximumExpectedWaveLaneCount = Reader.ReadUInt32();
            if (ChunkSize > 20)
            {
                var shaderKind = (PSVShaderKind)Reader.ReadByte();
                UsesViewID                  = Reader.ReadByte();
                GSMaxVertexCount            = Reader.ReadByte();
                SigInputElements            = Reader.ReadByte();
                SigOutputElements           = Reader.ReadByte();
                SigPatchConstOrPrimElements = Reader.ReadByte();
                SigInputVectors             = Reader.ReadByte();
                SigOutputVectors            = Reader.ReadBytes(4);
            }
            Reader = null;
        }
Exemplo n.º 2
0
        public static RuntimeDataChunk Parse(BytecodeReader reader, uint chunkSize)
        {
            var result = new RuntimeDataChunk();

            result.RawData = reader.ReadBytes((int)chunkSize);
            return(result);
        }
Exemplo n.º 3
0
        public static ShaderMessageDeclarationToken Parse(BytecodeReader reader)
        {
            var token0 = reader.ReadUInt32();
            var length = reader.ReadUInt32() - 2;

            var result = new ShaderMessageDeclarationToken
            {
                DeclarationLength  = length,
                InfoQueueMessageID = reader.ReadUInt32(),
                MessageFormat      = (ShaderMessageFormat)reader.ReadUInt32(),
                NumCharacters      = reader.ReadUInt32(),
                NumOperands        = reader.ReadUInt32(),
                OperandsLength     = reader.ReadUInt32()
            };

            for (int i = 0; i < result.NumOperands; i++)
            {
                result.Operands.Add(Operand.Parse(reader, OpcodeType.CustomData));
            }

            result.Format = reader.ReadString();

            // String is padded to a multiple of DWORDs.
            uint remainingBytes = (4 - ((result.NumCharacters + 1) % 4)) % 4;

            reader.ReadBytes((int)remainingBytes);

            return(result);
        }
Exemplo n.º 4
0
        public static VariableBlob Parse(BytecodeReader reader, BytecodeReader dataReader)
        {
            var result = new VariableBlob();

            result.Index = dataReader.ReadUInt32();
            var blobSize     = dataReader.ReadUInt32();
            var paddedSize   = blobSize + (blobSize % 4 == 0 ? 0 : 4 - blobSize % 4);
            var shaderReader = dataReader.CopyAtCurrentPosition();
            var data         = dataReader.ReadBytes((int)paddedSize);

            if (!_IsShader(data))
            {
                if (blobSize == 0)
                {
                    result.Value = "";
                }
                else
                {
                    result.Value = Encoding.UTF8.GetString(data, 0, (int)(blobSize - 1));
                }
            }
            else
            {
                result.Shader = ShaderModel.Parse(shaderReader);
            }
            return(result);
        }
Exemplo n.º 5
0
        public static PrivateChunk Parse(BytecodeReader reader, uint chunkSize)
        {
            var result = new PrivateChunk();

            result.PrivateData = reader.ReadBytes((int)chunkSize);

            return(result);
        }
Exemplo n.º 6
0
        public static HashChunk Parse(BytecodeReader reader, uint chunkSize)
        {
            var result = new HashChunk()
            {
                Flags  = (HashFlags)reader.ReadUInt32(),
                Digest = reader.ReadBytes(DigestSize)
            };

            return(result);
        }
        public new static EffectExpressionAssignment Parse(BytecodeReader reader, BytecodeReader assignmentReader)
        {
            var result     = new EffectExpressionAssignment();
            var shaderSize = assignmentReader.ReadUInt32();

            if (shaderSize != 0)
            {
                result.Shader = BytecodeContainer.Parse(assignmentReader.ReadBytes((int)shaderSize));
            }
            return(result);
        }
Exemplo n.º 8
0
        public static DebuggingChunk Parse(BytecodeReader reader, ChunkType chunkType, int chunkSize)
        {
            var result = new DebuggingChunk();

            if (chunkType == ChunkType.Sdbg)             // SDGB is not supported.
            {
                return(result);
            }

            result.PdbBytes = reader.ReadBytes(chunkSize);
            return(result);
        }
Exemplo n.º 9
0
 public static string TryReadString(this BytecodeReader reader)
 {
     try
     {
         var length = reader.ReadUInt32();
         if (length == 0)
         {
             return("");
         }
         var bytes = reader.ReadBytes((int)length);
         return(Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1));
     } catch (Exception ex)
     {
         return("Error reading string");
     }
 }
Exemplo n.º 10
0
        public static StateBlob Parse(BytecodeReader reader, BytecodeReader shaderReader)
        {
            var result = new StateBlob();

            result.TechniqueIndex    = shaderReader.ReadUInt32();
            result.PassIndex         = shaderReader.ReadUInt32();
            result.SamplerStateIndex = shaderReader.ReadUInt32();
            result.AssignmentIndex   = shaderReader.ReadUInt32();
            result.BlobType          = (StateBlobType)shaderReader.ReadUInt32();
            var dataReader = shaderReader.CopyAtCurrentPosition();
            var blobSize   = shaderReader.ReadUInt32();
            var paddedSize = blobSize + (blobSize % 4 == 0 ? 0 : 4 - blobSize % 4);
            //Seak ahead
            var data = shaderReader.ReadBytes((int)paddedSize);

            if (result.BlobType == StateBlobType.Shader)
            {
                result.Shader = ShaderReader.ReadShader(data);
            }
            else if (result.BlobType == StateBlobType.Variable)
            {
                result.VariableName = dataReader.TryReadString();
            }
            else if (result.BlobType == StateBlobType.IndexShader)
            {
                var _blobSize    = dataReader.ReadUInt32();
                var variableSize = dataReader.ReadUInt32();
                result.VariableName = dataReader.ReadString();
                if (variableSize > (result.VariableName.Length + 1))
                {
                    var paddingCount = variableSize - (result.VariableName.Length + 1);
                    var padding      = dataReader.ReadBytes((int)paddingCount);
                }
                result.Shader = result.Shader = ShaderModel.Parse(dataReader);
            }
            return(result);
        }
        public static Attributes ReadAttributes(BytecodeReader reader, ushort attributesCount)
        {
            Attributes attributes = new Attributes();
            Dictionary <String, Action> attributeNameDictionary = new Dictionary <string, Action>();

            attributeNameDictionary.Add("Code", () =>
            {
                ushort attributeNameIndex = reader.ReadUShort();
                uint attributeLength      = reader.ReadUInt();
                ushort maxStack           = reader.ReadUShort();
                ushort maxLocals          = reader.ReadUShort();
                uint codeLength           = reader.ReadUInt();
                byte[] code = reader.ReadBytes(codeLength);
                ushort exceptionTableLength = reader.ReadUShort();
                byte[] exceptionTable       = reader.ReadBytes((uint)exceptionTableLength * 8);
                ushort codeAttributesCount  = reader.ReadUShort();
                Attributes codeAttributes   = ReadAttributes(reader, codeAttributesCount);
                attributes.AddAttributeCode(attributeNameIndex, attributeLength,
                                            maxStack, maxLocals, codeLength, code, exceptionTableLength,
                                            exceptionTable, attributesCount, attributes);
            });
            short attributeIndex  = 0;
            var   attributesTable = new List <AttributeSuper>();
            int   curAttributeNameIndex;

            while (attributesCount > attributeIndex)
            {
                curAttributeNameIndex = bytecode[index] * 0x100 + bytecode[index + 1];
                switch (cp.getConstantUtf8(curAttributeNameIndex).Value)
                {
                case "Code":
                    attributesTable.Add(AttributeCode.Create(bytecode, cp));
                    attributeIndex++;
                    break;

                case "ConstantValue":
                    // TODO: not skipping bytes
                    Console.WriteLine("ConstantValue attribute was created");
                    attributesTable.Add(AttributeSuper.Create(bytecode));
                    index += (int)attributesTable.Last().AttributeLength;
                    attributeIndex++;
                    break;

                case "StackMapTable":
                    // TODO: not skipping bytes
                    Console.WriteLine("StackMapTable attribute was created");
                    attributesTable.Add(AttributeSuper.Create(bytecode));
                    index += (int)attributesTable.Last().AttributeLength;
                    attributeIndex++;
                    break;

                case "BootstrapMethods":
                    // TODO: not skipping bytes
                    Console.WriteLine("BootstrapMethods attribute was created");
                    attributesTable.Add(AttributeSuper.Create(bytecode));
                    index += (int)attributesTable.Last().AttributeLength;
                    attributeIndex++;
                    break;

                case "LineNumberTable":
                    attributesTable.Add(AttributeLineNumberTable.Create(bytecode, cp));
                    attributeIndex++;
                    break;

                case "SourceFile":
                    attributesTable.Add(AttributeSourceFile.Create(bytecode, cp));
                    attributeIndex++;
                    break;

                default:
                    attributeIndex++;
                    Console.WriteLine("no attribute found");
                    break;
                }
            }
            return(new Attributes(attributesTable));
        }
Exemplo n.º 12
0
        Token ReadInstruction(BytecodeReader reader)
        {
            uint   instructionToken = reader.ReadUInt32();
            Opcode opcode           = (Opcode)(instructionToken & 0xffff);
            int    size;

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

            if (opcode == Opcode.Comment)
            {
                var fourCC = reader.ReadUInt32();
                if (KnownCommentTypes.ContainsKey(fourCC))
                {
                    var commentReader = reader.CopyAtCurrentPosition();
                    reader.ReadBytes(size * 4 - 4);
                    switch (KnownCommentTypes[fourCC])
                    {
                    case CommentType.CTAB:
                        ConstantTable = ConstantTable.Parse(commentReader);
                        return(null);

                    case CommentType.C**T:
                        Cli = CliToken.Parse(commentReader);
                        return(null);

                    case CommentType.FXLC:
                        Fxlc = FxlcBlock.Parse(commentReader);
                        return(null);

                    case CommentType.PRES:
                        Preshader = Preshader.Parse(commentReader);
                        return(null);

                    case CommentType.PRSI:
                        Prsi = PrsiToken.Parse(commentReader);
                        return(null);
                    }
                }
                token         = new CommentToken(opcode, size, this);
                token.Data[0] = fourCC;
                for (int i = 1; i < size; i++)
                {
                    token.Data[i] = reader.ReadUInt32();
                }
            }
            else
            {
                token = new InstructionToken(opcode, size, this);
                var inst = token as InstructionToken;

                for (int i = 0; i < size; i++)
                {
                    token.Data[i] = reader.ReadUInt32();
                    if (opcode == Opcode.Def || opcode == Opcode.DefB || opcode == Opcode.DefI)
                    {
                        if (i == 0)
                        {
                            inst.Operands.Add(new DestinationOperand(token.Data[i]));
                        }
                    }
                    else if (opcode == Opcode.Dcl)
                    {
                        if (i == 0)
                        {
                            inst.Operands.Add(new DeclarationOperand(token.Data[i]));
                        }
                        else
                        {
                            inst.Operands.Add(new DestinationOperand(token.Data[i]));
                        }
                    }
                    else if (i == 0 && opcode.HasDestination())
                    {
                        if ((token.Data[i] & (1 << 13)) != 0)
                        {
                            //Relative Address mode
                            token.Data[i + 1] = reader.ReadUInt32();
                            inst.Operands.Add(new DestinationOperand(token.Data[i], token.Data[i + 1]));
                            i++;
                        }
                        else
                        {
                            inst.Operands.Add(new DestinationOperand(token.Data[i]));
                        }
                    }
                    else if ((token.Data[i] & (1 << 13)) != 0)
                    {
                        //Relative Address mode
                        token.Data[i + 1] = reader.ReadUInt32();
                        inst.Operands.Add(new SourceOperand(token.Data[i], token.Data[i + 1]));
                        i++;
                    }
                    else
                    {
                        inst.Operands.Add(new SourceOperand(token.Data[i]));
                    }
                }
                if (opcode != Opcode.Comment)
                {
                    token.Modifier   = (int)((instructionToken >> 16) & 0xff);
                    token.Predicated = (instructionToken & 0x10000000) != 0;
                    token.CoIssue    = (instructionToken & 0x40000000) != 0;
                    Debug.Assert((instructionToken & 0xA0000000) == 0, $"Instruction has unexpected bits set {instructionToken & 0xE0000000}");
                }
            }
            return(token);
        }