Exemple #1
0
        /// <summary>
        /// Converts the provided C# source code to the specified shader language.
        /// </summary>
        /// <param name="cSharpSources">A dictionary containing source code by file or friendly name. The name is used to identify the source code in message logs.</param>
        /// <param name="outputLanguage">The language that the input source code should be translated to.</param>
        /// <param name="flags">A set of flags to change the default behaviour of the converter.</param>
        /// <param name="preprocessorSymbols">A list of defined preprocessor symbols.</param>
        /// <returns></returns>
        public TranslationResult Translate(Dictionary <string, string> cSharpSources,
                                           OutputLanguage outputLanguage,
                                           TranslationFlags flags            = TranslationFlags.None,
                                           List <string> preprocessorSymbols = null)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Translator instance has been disposed.");
            }

            TranslationArgs tArgs = new TranslationArgs()
            {
                CSharpSources       = new Dictionary <string, string>(cSharpSources),
                Flags               = flags,
                Language            = outputLanguage,
                PreprocessorSymbols = preprocessorSymbols ?? new List <string>(),
            };

            TranslationContext context = _runner.Run(tArgs);
            TranslationResult  result  = BuildResult(context, flags);

            context.Recycle();
            return(result);
        }
        internal TranslationContext Run(TranslationArgs args)
        {
            List <string> preprocessorSymbols = null;

            if (args.PreprocessorSymbols != null)
            {
                preprocessorSymbols = new List <string>(args.PreprocessorSymbols);
            }

            Stopwatch mainTimer = new Stopwatch();

            mainTimer.Start();

            ShaderLanguage     foundation = ShaderLanguage.Get(args.Language);
            TranslationContext context    = Pooling.Contexts.Get();

            context.Initialize(this, foundation, preprocessorSymbols, args.Flags);

            Message("Analyzing", TranslationMessageType.Status);
            AnalysisInfo analysis = Analyze(context, args.CSharpSources);

            Message($"Analysis completed");

            if (analysis.HasError)
            {
                foreach (TranslationMessage msg in context.Messages)
                {
                    Message(msg.Text, msg.MessageType);
                }

                Message($"Cannot proceed until errors are fixed. Aborting.");
            }
            else
            {
                Message($"Mapping shader classes", TranslationMessageType.Status);
                Map(context, analysis.Trees);
                Message($"Mapping completed. Found {context.Shaders.Count} shader classes.");

                foreach (ShaderTranslationContext sc in context.Shaders)
                {
                    Message($"Translating {sc.Name}", TranslationMessageType.Status);
                    Stopwatch timer = new Stopwatch();
                    timer.Start();

                    Message($"Translating to {context.Language.Language}");
                    Translate(sc, sc.RootNode);
                    sc.FinalSource = sc.Source.ToString();

                    timer.Stop();
                    Message($"  Finished '{sc.Name}' in {timer.Elapsed.TotalMilliseconds:N2} milliseconds");
                }

                mainTimer.Stop();
                foreach (TranslationMessage msg in context.Messages)
                {
                    Message(msg.Text, msg.MessageType);
                }

                int errors   = context.Messages.Count(t => t.MessageType == TranslationMessageType.Error);
                int warnings = context.Messages.Count(t => t.MessageType == TranslationMessageType.Warning);
                Message($"Finished conversion of { args.CSharpSources.Count} source(s) with {errors} errors and {warnings} warnings. ");
                Message($"Took {mainTimer.Elapsed.TotalMilliseconds:N2} milliseconds");
            }

            return(context);
        }