public void WriteHeaders(ShaderOutputGlobalContext ctx)
        {
            TemplateGroup  structCppGroup          = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/cppTemplate.stg");
            TemplateGroup  commonGroup             = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/commonStructuresTemplate.stg");
            var            structWithoutDuplicates = RemoveDuplicates(ctx.Structures);
            string         cppStructures           = CppHeaderWriter.WriteStructures(structCppGroup, structWithoutDuplicates);
            StringTemplate commonTemplate          = commonGroup.GetInstanceOf("cppHeader");

            commonTemplate.Add("structs", cppStructures);

            string cppFilename = Program.CppOutputDir + "/CommonStructures.h";

            System.IO.File.WriteAllText(cppFilename, commonTemplate.Render());

            TemplateGroup  structHlslGroup    = new Antlr4.StringTemplate.TemplateGroupFile(Program.TemplatesDir + "/hlslTemplate.stg");
            string         hlslStructures     = HlslHeadersWriter.WriteStructures(structHlslGroup, structWithoutDuplicates);
            StringTemplate commonHlslTemplate = commonGroup.GetInstanceOf("hlslHeader");

            commonHlslTemplate.Add("structs", hlslStructures);

            string hlslFilename = Program.HlslOutputDir + "/CommonStructures.hlsl";

            System.IO.File.WriteAllText(hlslFilename, commonHlslTemplate.Render());
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            ParseCommandLine(args);
            try
            {
                if (InputDir == null)
                {
                    throw new InvalidCommandLineException("Input (inDir) directory isn't set in the command line");
                }
                if (!Directory.Exists(InputDir))
                {
                    throw new InvalidCommandLineException("Input directory doesn't exist (" + InputDir + ")");
                }

                if (TemplatesDir == null)
                {
                    throw new InvalidCommandLineException("Template (templatesDir) directory isn't set in the command line");
                }
                if (!Directory.Exists(TemplatesDir))
                {
                    throw new InvalidCommandLineException("Template directory doesn't exist (" + TemplatesDir + ")");
                }

                if (HlslOutputDir == null)
                {
                    throw new InvalidCommandLineException("Hlsl directory isn't set in the command line");
                }

                if (CppOutputDir == null)
                {
                    throw new InvalidCommandLineException("Cpp output directory isn't set in the command line");
                }
            }
            catch (InvalidCommandLineException ex)
            {
                Console.WriteLine(ex.Message);
            }

            CreateOutputDirectories(false);

            DateTime lastInputUpdateTime;
            string   updateTimeFilepath;

            if (CompareOutputsVersion(out lastInputUpdateTime, out updateTimeFilepath) && !ForceRegenerate)
            {
                return;
            }

            Console.WriteLine("Generating...\n");
            string[] files = Directory.GetFiles(InputDir, "*.sinp", SearchOption.AllDirectories);
            ShaderOutputGlobalContext globalContext = new ShaderOutputGlobalContext();

            foreach (var filepath in files)
            {
                Console.WriteLine("Parsing file " + filepath + '\n');
                try
                {
                    string content = File.ReadAllText(filepath);

                    ShaderInputsParser parser = InitializeAntlr(content);
                    ShaderInputsParser.InputFileContext ctx = parser.inputFile();

                    ShaderInputsVisitor visitor = new ShaderInputsVisitor(globalContext);
                    visitor.Visit(ctx);
                    ShaderOutputContext outputCtx = visitor.OutputContext;

                    //Validator validator = new Validator();
                    //validator.Validate(outputCtx.ConstantBuffers);

                    BindpointManager bindpointManager = new BindpointManager();
                    bindpointManager.AssignBindpoints(outputCtx);

                    string            filename   = Path.GetFileNameWithoutExtension(filepath);
                    HlslHeadersWriter hlslWriter = new HlslHeadersWriter();
                    hlslWriter.WriteHeaders(outputCtx, filename);
                    CppHeaderWriter cppWriter = new CppHeaderWriter();
                    cppWriter.WriteHeaders(outputCtx, filename);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occurred while parsing file " + filepath);
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                    throw;
                }
            }
            Console.WriteLine("Writing common structures...");
            CommonStructHeaderWriter commonStructWriter = new CommonStructHeaderWriter();

            commonStructWriter.WriteHeaders(globalContext);

            Console.WriteLine("Writing outputs...\n");
            FactoryWriter factoryWriter = new FactoryWriter();

            factoryWriter.WriteFactory(files);
            Console.WriteLine("Writing done\n");

            UpdateOutputVersionFile(lastInputUpdateTime, updateTimeFilepath);

            Console.Write("Output file timestamp updated\n");

            Console.Write("\n******* SHADERS OUTPUT FILES ARE UPDATED WITH SUCESS ******\n");
        }