private static Dictionary <RegisterKey, HlslTreeNode> GetConstantOutputs(ShaderModel shader)
        {
            var constantTable = shader.ParseConstantTable();

            var constantOutputs = new Dictionary <RegisterKey, HlslTreeNode>();

            foreach (var constant in constantTable)
            {
                for (int i = 0; i < 4; i++)
                {
                    var destinationKey = new RegisterKey()
                    {
                        RegisterNumber = constant.RegisterIndex,
                        RegisterType   = RegisterType.Const,
                        ComponentIndex = i
                    };
                    var shaderInput = new HlslShaderInput()
                    {
                        InputDecl      = destinationKey,
                        ComponentIndex = i
                    };
                    constantOutputs.Add(destinationKey, shaderInput);
                }
            }

            return(constantOutputs);
        }
Example #2
0
 private static HlslWriter CreateHlslWriter(ShaderModel shader, bool doAstAnalysis)
 {
     if (doAstAnalysis)
     {
         return new HlslAstWriter(shader);
     }
     return new HlslSimpleWriter(shader);
 }
Example #3
0
        private void LoadConstantOutputs(ShaderModel shader)
        {
            IList <ConstantDeclaration> constantTable = shader.ParseConstantTable();

            _activeOutputs = new Dictionary <RegisterComponentKey, HlslTreeNode>();
            _samplers      = new Dictionary <RegisterKey, HlslTreeNode>();

            foreach (var constant in constantTable)
            {
                if (constant.RegisterSet == RegisterSet.Sampler)
                {
                    var registerKey    = new RegisterKey(RegisterType.Sampler, constant.RegisterIndex);
                    var destinationKey = new RegisterComponentKey(registerKey, 0);
                    int samplerTextureDimension;
                    switch (constant.ParameterType)
                    {
                    case ParameterType.Sampler1D:
                        samplerTextureDimension = 1;
                        break;

                    case ParameterType.Sampler2D:
                        samplerTextureDimension = 2;
                        break;

                    case ParameterType.Sampler3D:
                    case ParameterType.SamplerCube:
                        samplerTextureDimension = 3;
                        break;

                    default:
                        throw new InvalidOperationException();
                    }
                    var shaderInput = new RegisterInputNode(destinationKey, samplerTextureDimension);
                    _samplers.Add(registerKey, shaderInput);
                }
                else
                {
                    for (int r = 0; r < constant.RegisterCount; r++)
                    {
                        if (constant.ParameterType != ParameterType.Float)
                        {
                            throw new NotImplementedException();
                        }
                        var registerKey = new RegisterKey(RegisterType.Const, constant.RegisterIndex + r);
                        for (int i = 0; i < 4; i++)
                        {
                            var destinationKey = new RegisterComponentKey(registerKey, i);
                            var shaderInput    = new RegisterInputNode(destinationKey);
                            _activeOutputs.Add(destinationKey, shaderInput);
                        }
                    }
                }
            }
        }
Example #4
0
        private static void ReadShaderModel(string baseFilename, FileStream inputStream, bool doAstAnalysis)
        {
            using (var input = new ShaderReader(inputStream, true))
            {
                ShaderModel shader = input.ReadShader();

                AsmWriter writer = new AsmWriter(shader);
                string asmFilename = $"{baseFilename}.asm";
                Console.WriteLine("Writing {0}", asmFilename);
                writer.Write(asmFilename);

                var hlslWriter = CreateHlslWriter(shader, doAstAnalysis);
                string hlslFilename = $"{baseFilename}.fx";
                Console.WriteLine("Writing {0}", hlslFilename);
                hlslWriter.Write(hlslFilename);
            }
        }
        public HlslAst Parse(ShaderModel shader)
        {
            _activeOutputs = GetConstantOutputs(shader);

            int instructionPointer = 0;

            while (instructionPointer < shader.Instructions.Count)
            {
                ParseInstruction(shader.Instructions[instructionPointer]);
                instructionPointer++;
            }

            var roots = _activeOutputs
                        .Where(o => o.Key.RegisterType == RegisterType.ColorOut)
                        .ToDictionary(o => o.Key, o => o.Value);

            return(new HlslAst(roots));
        }
Example #6
0
        public HlslAst Parse(ShaderModel shader)
        {
            LoadConstantOutputs(shader);

            int  instructionPointer = 0;
            bool ifBlock            = false;

            while (instructionPointer < shader.Instructions.Count)
            {
                var instruction = shader.Instructions[instructionPointer];
                if (ifBlock)
                {
                    if (instruction.Opcode == Opcode.Else)
                    {
                        ifBlock = false;
                    }
                }
                else
                {
                    if (instruction.Opcode == Opcode.IfC)
                    {
                        ifBlock = true;
                    }
                    ParseInstruction(instruction);
                }
                instructionPointer++;
            }

            Dictionary <RegisterComponentKey, HlslTreeNode> roots;

            if (shader.Type == ShaderType.Pixel)
            {
                roots = _activeOutputs
                        .Where(o => o.Key.Type == RegisterType.ColorOut)
                        .ToDictionary(o => o.Key, o => o.Value);
            }
            else
            {
                roots = _activeOutputs
                        .Where(o => o.Key.Type == RegisterType.Output)
                        .ToDictionary(o => o.Key, o => o.Value);
            }
            return(new HlslAst(roots));
        }
Example #7
0
        virtual public ShaderModel ReadShader()
        {
            // Version token
            byte       minorVersion = ReadByte();
            byte       majorVersion = ReadByte();
            ShaderType shaderType   = (ShaderType)ReadUInt16();

            _shader = new ShaderModel(majorVersion, minorVersion, shaderType);

            do
            {
                ReadInstruction();
                VerifyInstruction();
            } while (_instruction.Opcode != Opcode.End);


            _instruction = null;
            return(_shader);
        }
Example #8
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)
            {
                Instruction instruction = ReadInstruction();
                InstructionVerifier.Verify(instruction);
                shader.Instructions.Add(instruction);
                if (instruction.Opcode == Opcode.End)
                {
                    break;
                }
            }

            return(shader);
        }
Example #9
0
        private static void ReadRgxa(string baseFilename, FileStream inputStream, bool doAstAnalysis)
        {
            using (var input = new RgxaReader(inputStream, true))
            {
                int ivs = 0, ips = 0;
                while (true)
                {
                    ShaderModel shader = input.ReadShader();
                    if (shader == null)
                    {
                        break;
                    }

                    string outFilename;
                    if (shader.Type == ShaderType.Vertex)
                    {
                        outFilename = $"{baseFilename}_vs{ivs}";
                        ivs++;
                    }
                    else
                    {
                        outFilename = $"{baseFilename}_ps{ips}";
                        ips++;
                    }
                    Console.WriteLine(outFilename);

                    //shader.ToFile("outFilename.fxc");

                    var writer = new AsmWriter(shader);
                    writer.Write(outFilename + ".asm");

                    var hlslWriter = CreateHlslWriter(shader, doAstAnalysis);
                    hlslWriter.Write(outFilename + ".fx");
                }
            }
        }
Example #10
0
 public AsmWriter(ShaderModel shader)
 {
     this.shader = shader;
 }
Example #11
0
 public HlslSimpleWriter(ShaderModel shader)
     : base(shader)
 {
 }
Example #12
0
 public HlslWriter(ShaderModel shader)
 {
     _shader = shader;
 }
Example #13
0
 public HlslWriter(ShaderModel shader, bool doAstAnalysis = false)
 {
     _shader        = shader;
     _doAstAnalysis = doAstAnalysis;
 }
 public HlslAstWriter(ShaderModel shader)
     : base(shader)
 {
 }
Example #15
0
 public HlslWriter(ShaderModel shader, bool doAstAnalysis = false)
 {
     this.shader        = shader;
     this.doAstAnalysis = doAstAnalysis;
 }