Esempio n. 1
0
        private string CompileNodesWithComponents(List <HlslTreeNode> components, HlslTreeNode first, int promoteToVectorSize)
        {
            var componentsWithIndices = components.Cast <IHasComponentIndex>();

            if (first is RegisterInputNode shaderInput)
            {
                var registerKey = shaderInput.RegisterComponentKey.RegisterKey;

                string swizzle = "";
                if (registerKey.Type != RegisterType.Sampler)
                {
                    swizzle = GetAstSourceSwizzleName(componentsWithIndices,
                                                      _registers.GetRegisterFullLength(registerKey),
                                                      promoteToVectorSize);
                }

                string name = _registers.GetRegisterName(registerKey);
                return($"{name}{swizzle}");
            }

            if (first is TextureLoadOutputNode textureLoad)
            {
                string swizzle = GetAstSourceSwizzleName(componentsWithIndices, 4);

                string sampler   = Compile(new[] { textureLoad.SamplerInput });
                string texcoords = Compile(textureLoad.TextureCoordinateInputs);
                return($"tex2D({sampler}, {texcoords}){swizzle}");
            }

            if (first is NormalizeOutputNode)
            {
                string input   = Compile(first.Inputs);
                string swizzle = GetAstSourceSwizzleName(componentsWithIndices, 4);
                return($"normalize({input}){swizzle}");
            }

            throw new NotImplementedException();
        }
Esempio n. 2
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. 3
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();
        }