Exemplo n.º 1
0
        /*
         * There are a few token types:
         * comment_token fourCC data
         * def_token dest_param literal_param literal_param literal_param literal_param
         * dcl_token decl_param dest_param
         * inst_token dest_param [src_param ...]
         * end_token
         */
        public static ShaderModel Parse(BytecodeReader reader)
        {
            var result = new ShaderModel();

            result.MinorVersion = reader.ReadByte();
            result.MajorVersion = reader.ReadByte();
            result.Type         = (ShaderType)reader.ReadUInt16();
            //SM1 shaders do not encode instruction size which rely on for reading operands.
            //So we won't support SM1
            if (result.MajorVersion == 1)
            {
                throw new ParseException("Shader Model 1 is not supported");
            }
            while (true)
            {
                var instruction = result.ReadInstruction(reader);
                if (instruction == null)
                {
                    continue;
                }
                result.Tokens.Add(instruction);
                if (instruction.Opcode == Opcode.End)
                {
                    break;
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public static ConstantTable Parse(BytecodeReader ctabReader)
        {
            var result        = new ConstantTable();
            var ctabSize      = ctabReader.ReadUInt32();
            var creatorOffset = ctabReader.ReadInt32();

            result.MajorVersion = ctabReader.ReadByte();
            result.MinorVersion = ctabReader.ReadByte();
            var shaderType         = (ShaderType)ctabReader.ReadUInt16();
            var numConstants       = ctabReader.ReadInt32();
            var constantInfoOffset = ctabReader.ReadInt32();
            var shaderFlags        = (ShaderFlags)ctabReader.ReadUInt32();
            var shaderModelOffset  = ctabReader.ReadInt32();

            for (int i = 0; i < numConstants; i++)
            {
                var decReader = ctabReader.CopyAtOffset(constantInfoOffset + i * 20);
                ConstantDeclaration declaration = ConstantDeclaration.Parse(ctabReader, decReader);
                result.ConstantDeclarations.Add(declaration);
            }

            var shaderModelReader = ctabReader.CopyAtOffset(shaderModelOffset);

            result.VersionString = shaderModelReader.ReadString();

            var creatorReader = ctabReader.CopyAtOffset(creatorOffset);

            result.Creator = creatorReader.ReadString();
            return(result);
        }
Exemplo n.º 3
0
 public static PSInfo Parse(BytecodeReader reader)
 {
     return(new PSInfo()
     {
         DepthOutput = reader.ReadByte(),
         SampleFrequency = reader.ReadByte()
     });
 }
Exemplo n.º 4
0
        public static SamplerMapping Parse(BytecodeReader reader)
        {
            var result = new SamplerMapping();

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

            Debug.Assert(padding == 0, "Padding is 0");
            return(result);
        }
Exemplo n.º 5
0
        public static ShaderVersion ParseAon9(BytecodeReader reader)
        {
            byte   minor      = reader.ReadByte();
            byte   major      = reader.ReadByte();
            ushort shaderType = reader.ReadUInt16();

            return(new ShaderVersion
            {
                MinorVersion = minor,
                MajorVersion = major,
                ProgramType = shaderType == 0xFFFF ? ProgramType.PixelShader : ProgramType.VertexShader
            });
        }
Exemplo n.º 6
0
 public static VSInfo Parse(BytecodeReader reader)
 {
     return(new VSInfo()
     {
         OutputPositionPresent = reader.ReadByte() != 0
     });
 }
Exemplo n.º 7
0
 public static DSInfo Parse(BytecodeReader reader)
 {
     return(new DSInfo()
     {
         InputControlPointCount = reader.ReadUInt32(),
         OutputPositionPresent = reader.ReadByte() != 0,
         TessellatorDomain = reader.ReadUInt32(),
     });
 }
Exemplo n.º 8
0
 public static GSInfo Parse(BytecodeReader reader)
 {
     return(new GSInfo()
     {
         InputPrimitive = (Primitive)reader.ReadUInt32(),
         OutputTopology = (PrimitiveTopology)reader.ReadUInt32(),
         OutputStreamMask = reader.ReadUInt32(),
         OutputPositionPresent = reader.ReadByte() != 0
     });
 }
Exemplo n.º 9
0
        public static Number Parse(BytecodeReader reader)
        {
            const int byteCount = 4;
            var       bytes     = new byte[byteCount];

            for (int i = 0; i < byteCount; i++)
            {
                bytes[i] = reader.ReadByte();
            }
            return(new Number(bytes));
        }
Exemplo n.º 10
0
        public static ShaderModel Parse(BytecodeReader reader)
        {
            var result = new ShaderModel();

            result.MinorVersion = reader.ReadByte();
            result.MajorVersion = reader.ReadByte();
            result.Type         = (ShaderType)reader.ReadUInt16();
            while (true)
            {
                var instruction = result.ReadInstruction(reader);
                if (instruction == null)
                {
                    continue;
                }
                result.Tokens.Add(instruction);
                if (instruction.Opcode == Opcode.End)
                {
                    break;
                }
            }
            return(result);
        }
        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;
        }
        public static ConstantBufferMapping Parse(BytecodeReader reader)
        {
            var result = new ConstantBufferMapping();

            result.Buffer    = reader.ReadUInt16();
            result.StartReg  = reader.ReadUInt16();
            result.RegCount  = reader.ReadUInt16();
            result.TargetReg = reader.ReadUInt16();
            for (int i = 0; i < 4; i++)
            {
                var type = (DataConversionType)reader.ReadByte();
                result.DataConversion[i] = type;
                Debug.Assert(Enum.IsDefined(typeof(DataConversionType), type),
                             $"Invalid DataConversionType {type}");
            }
            return(result);
        }
        public static ConstantPool ReadConstantPool(BytecodeReader reader, ushort constantPoolCount)
        {
            // TODO: check if long and double constants reading right
            ConstantPool constantPool  = new ConstantPool();
            int          a             = 0;
            var          tagDictionary = new Dictionary <int, Action>();

            tagDictionary.Add(1, () =>
            {
                ushort length = reader.ReadUShort();
                String value  = reader.ReadString(length);
                constantPool.AddConstantUtf8(new ConstantUtf8(length, value));
            });
            tagDictionary.Add(3, () => constantPool.AddConstantInteger(reader.ReadInt()));
            tagDictionary.Add(4, () => constantPool.AddConstantFloat(reader.ReadFloat()));
            tagDictionary.Add(5, () => constantPool.AddConstantLong(reader.ReadLong()));
            tagDictionary.Add(6, () => constantPool.AddConstantDouble(reader.ReadDouble()));
            tagDictionary.Add(7, () => constantPool.AddConstantClass(new ConstantClass(reader.ReadUShort())));
            tagDictionary.Add(8, () => constantPool.AddConstantString(new ConstantString(reader.ReadUShort())));
            tagDictionary.Add(9, () => constantPool.AddConstantFieldRef(new ConstantFieldRef(reader.ReadUShort(), reader.ReadUShort())));
            tagDictionary.Add(10, () => constantPool.AddConstantMethodRef(new ConstantMethodRef(reader.ReadUShort(), reader.ReadUShort())));
            tagDictionary.Add(11, () => constantPool.AddConstantInterfaceMethodRef(new ConstantInterfaceMethodRef(reader.ReadUShort(), reader.ReadUShort())));
            tagDictionary.Add(12, () => constantPool.AddConstantNameAndType(new ConstantNameAndType(reader.ReadUShort(), reader.ReadUShort())));
            tagDictionary.Add(15, () => constantPool.AddConstantMethodHandle(new ConstantMethodHandle(reader.ReadByte(), reader.ReadUShort())));
            tagDictionary.Add(16, () => constantPool.AddConstantMethodType(new ConstantMethodType(reader.ReadUShort())));
            tagDictionary.Add(18, () => constantPool.AddConstantInvokeDynamic(new ConstantInvokeDynamic(reader.ReadUShort(), reader.ReadUShort())));
            tagDictionary.Add(19, () => constantPool.AddConstantModule(new ConstantModule(reader.ReadUShort())));
            tagDictionary.Add(20, () => constantPool.AddConstantPackage(new ConstantPackage(reader.ReadUShort())));
            Action createConstant;

            for (int i = 0; i < constantPoolCount; i++)
            {
                if (tagDictionary.TryGetValue(reader.ReadByte(), out createConstant))
                {
                    createConstant.Invoke();
                }
                else
                {
                    throw new KeyNotFoundException("Constant type not recognized");
                }
            }
            return(constantPool);
        }