Esempio n. 1
0
        protected override void Write()
        {
            if (_shader.Type == ShaderType.Expression)
            {
                Write($"// Writing expression");
                WriteExpression(_shader);
                return;
            }
            _registers = new RegisterState(_shader);

            WriteConstantDeclarations();

            if (_shader.Preshader != null)
            {
                WriteExpression(_shader.Preshader.Shader);
            }
            if (_registers.MethodInputRegisters.Count > 1)
            {
                WriteInputStructureDeclaration();
            }

            if (_registers.MethodOutputRegisters.Count > 1)
            {
                WriteOutputStructureDeclaration();
            }

            string methodReturnType = GetMethodReturnType();
            string methodParameters = GetMethodParameters();
            string methodSemantic   = GetMethodSemantic();

            WriteLine("{0} {1}({2}){3}",
                      methodReturnType,
                      _entryPoint,
                      methodParameters,
                      methodSemantic);
            WriteLine("{");
            Indent++;

            if (_registers.MethodOutputRegisters.Count > 1)
            {
                var outputStructType = _shader.Type == ShaderType.Pixel ? "PS_OUT" : "VS_OUT";
                WriteIndent();
                WriteLine($"{outputStructType} o;");
            }
            else
            {
                var output = _registers.MethodOutputRegisters.First().Value;
                WriteLine("{0} {1};", methodReturnType, _registers.GetRegisterName(output.RegisterKey));
            }
            WriteTemps();
            HlslAst ast = null;

            if (_doAstAnalysis)
            {
                var parser = new BytecodeParser();
                ast = parser.Parse(_shader);
                ast.ReduceTree();

                WriteAst(ast);
            }
            else
            {
                WriteInstructionList();

                if (_registers.MethodOutputRegisters.Count > 1)
                {
                    WriteLine($"return o;");
                }
                else
                {
                    var output = _registers.MethodOutputRegisters.First().Value;
                    WriteLine($"return {_registers.GetRegisterName(output.RegisterKey)};");
                }
            }
            Indent--;
            WriteLine("}");
        }
Esempio n. 2
0
        public void Write(Stream stream)
        {
            hlslWriter = new StreamWriter(stream);

            _registers = new RegisterState(_shader);

            WriteConstantDeclarations();

            if (_registers.MethodInputRegisters.Count > 1)
            {
                WriteInputStructureDeclaration();
            }

            if (_registers.MethodOutputRegisters.Count > 1)
            {
                WriteOutputStructureDeclaration();
            }

            string methodReturnType = GetMethodReturnType();
            string methodParameters = GetMethodParameters();
            string methodSemantic   = GetMethodSemantic();

            WriteLine("{0} main({1}){2}", methodReturnType, methodParameters, methodSemantic);
            WriteLine("{");
            indent = "\t";

            if (_registers.MethodOutputRegisters.Count > 1)
            {
                var outputStructType = _shader.Type == ShaderType.Pixel ? "PS_OUT" : "VS_OUT";
                WriteLine($"{outputStructType} o;");
                WriteLine();
            }
            else
            {
                var output = _registers.MethodOutputRegisters.First().Value;
                WriteLine("{0} {1};", methodReturnType, _registers.GetRegisterName(output.RegisterKey));
                WriteLine();
            }
            WriteTemps();
            HlslAst ast = null;

            if (_doAstAnalysis)
            {
                var parser = new BytecodeParser();
                ast = parser.Parse(_shader);
                ast.ReduceTree();

                WriteAst(ast);
            }
            else
            {
                WriteInstructionList();

                if (_registers.MethodOutputRegisters.Count > 1)
                {
                    WriteLine($"return o;");
                }
                else
                {
                    var output = _registers.MethodOutputRegisters.First().Value;
                    WriteLine($"return {_registers.GetRegisterName(output.RegisterKey)};");
                }
            }
            indent = "";
            WriteLine("}");
            hlslWriter.Flush();
        }