Exemple #1
0
        public static bool ParseShader(FxcShader shader)
        {
            ShaderBytecode ByteCodeObj;
            ShaderProfile  ShaderProfile;

            try
            {
                ByteCodeObj = new ShaderBytecode(shader.ByteCode);

                ShaderProfile = ByteCodeObj.GetVersion();


                switch (ShaderProfile.Version)
                {
                case ShaderVersion.VertexShader:
                case ShaderVersion.PixelShader:
                case ShaderVersion.GeometryShader:
                    //VersionMajor = br.ReadByte();//4,5 //appears to be shader model version
                    //VersionMinor = br.ReadByte(); //perhaps shader minor version
                    break;

                default:
                    shader.VersionMajor = (byte)ShaderProfile.Major;
                    shader.VersionMinor = (byte)ShaderProfile.Minor;
                    break;
                }

                shader.Disassembly = ByteCodeObj.Disassemble();
            }
            catch (Exception ex)
            {
                shader.LastError += ex.ToString() + "\r\n";
                return(false);
            }

            return(true);
        }
Exemple #2
0
        public void Load(byte[] data, string name = "")
        {
            Name = name;
            Hash = Jenkins.Hash(name.Replace(".fxc", ""));

            LastError = string.Empty;

            MemoryStream ms = new MemoryStream(data);
            BinaryReader br = new BinaryReader(ms);

            uint magic_rgxe = br.ReadUInt32();

            if (magic_rgxe != 1702389618) //"rgxe"
            {
                return;
            }

            VertexType = (VertexType)br.ReadUInt32();

            byte ec0   = br.ReadByte();
            var  exts1 = new List <FxcHeaderExt>();

            for (int e = 0; e < ec0; e++)
            {
                FxcHeaderExt ext = new FxcHeaderExt();
                ext.Name     = ReadString(br);
                ext.Unk0Byte = br.ReadByte();   //0
                ext.Unk1Uint = br.ReadUInt32(); //2
                exts1.Add(ext);
            }
            Exts = exts1.ToArray();


            List <FxcShader>[] shadergrps = new List <FxcShader> [6];

            var chunks  = new List <FxcHeaderChunk>();
            var shaders = new List <FxcShader>();
            int gindex  = 0;

            while (gindex < 6)// (sc0 > 0)
            {
                var shadergrp = new List <FxcShader>();
                shadergrps[gindex] = shadergrp;

                gindex++;
                byte sc0 = br.ReadByte();
                if (sc0 == 0)
                {
                    sc0 = br.ReadByte(); //this is a little odd, sometimes a byte skip
                }
                FxcHeaderChunk chunk = new FxcHeaderChunk();
                chunk.Read(br);
                chunk.Gindex      = gindex;
                chunk.ShaderCount = sc0;
                chunks.Add(chunk);
                for (int s = 1; s < sc0; s++)
                {
                    bool      exbyteflag1 = (gindex == 5); //GS seems to be in diff format??
                    bool      vsgsps      = (gindex == 1) || (gindex == 2) || (gindex == 5);
                    FxcShader shader      = new FxcShader();
                    if (!shader.Read(br, exbyteflag1, vsgsps))
                    {
                        LastError += shader.LastError;
                        //gindex = 6; //get outta the loop?
                        //break;
                    }
                    shaders.Add(shader);
                    shadergrp.Add(shader);
                }
            }

            Shaders         = shaders.ToArray();
            VertexShaders   = shadergrps[0]?.ToArray();
            PixelShaders    = shadergrps[1]?.ToArray();
            ComputeShaders  = shadergrps[2]?.ToArray();
            DomainShaders   = shadergrps[3]?.ToArray();
            GeometryShaders = shadergrps[4]?.ToArray();
            HullShaders     = shadergrps[5]?.ToArray();


            Chunks = chunks.ToArray();

            //ms.Dispose();
            //return;

            List <FxcVariable> globalVars = new List <FxcVariable>();

            CBufferDict = new Dictionary <uint, FxcCBuffer>();
            FxcCBuffer cbtmp = null;

            try //things can be uncertain after this...
            {
                byte cbCount1 = br.ReadByte();
                if (cbCount1 > 0)
                {
                    var cbuffers1 = new List <FxcCBuffer>();
                    for (int i = 0; i < cbCount1; i++) //cbuffers? includes?
                    {
                        FxcCBuffer cbuf = new FxcCBuffer();
                        cbuf.Read(br);
                        cbuffers1.Add(cbuf);
                        CBufferDict[cbuf.NameHash] = cbuf;
                    }
                    CBuffers1 = cbuffers1.ToArray();
                }

                byte varCount1 = br.ReadByte();
                if (varCount1 > 0)
                {
                    var vars1 = new List <FxcVariable>(); //cbuffer contents/vars
                    for (int i = 0; i < varCount1; i++)
                    {
                        FxcVariable vari = new FxcVariable();
                        vari.Read(br);
                        vars1.Add(vari);
                        if (CBufferDict.TryGetValue(vari.CBufferName, out cbtmp))
                        {
                            cbtmp.AddVariable(vari);
                        }
                        else
                        {
                            globalVars.Add(vari);
                        }
                    }
                    Variables1 = vars1.ToArray();
                }


                byte cbCount2 = br.ReadByte(); //0,1, +?
                if (cbCount2 > 0)
                {
                    var cbuffers2 = new List <FxcCBuffer>(); //more cbuffers..
                    for (int i = 0; i < cbCount2; i++)
                    {
                        FxcCBuffer cbuf = new FxcCBuffer();
                        cbuf.Read(br);
                        cbuffers2.Add(cbuf);
                        CBufferDict[cbuf.NameHash] = cbuf;
                    }
                    CBuffers2 = cbuffers2.ToArray();
                }


                byte varCount2 = br.ReadByte();
                if (varCount2 > 0)
                {
                    var vars2 = new List <FxcVariable>();
                    for (int i = 0; i < varCount2; i++) //textures/samplers
                    {
                        FxcVariable vari = new FxcVariable();
                        vari.Read(br);
                        vars2.Add(vari);
                        if (CBufferDict.TryGetValue(vari.CBufferName, out cbtmp))
                        {
                            cbtmp.AddVariable(vari);
                        }
                        else
                        {
                            globalVars.Add(vari);
                        }
                    }
                    Variables2 = vars2.ToArray();
                }


                byte techCount = br.ReadByte();
                if (techCount > 0)
                {
                    var techniques = new List <FxcTechnique>();
                    for (int i = 0; i < techCount; i++)
                    {
                        FxcTechnique tech = new FxcTechnique();
                        tech.Read(br);
                        techniques.Add(tech);
                    }
                    Techniques = techniques.ToArray();
                }


                foreach (var cbuf in CBufferDict.Values)
                {
                    cbuf.ConsolidateVariables();
                }
                GlobalVariables = globalVars.ToArray();


                if (ms.Position != ms.Length)
                {
                }
            }
            catch (Exception ex)
            {
                LastError = ex.ToString();
            }

            ms.Dispose();
        }