Ejemplo n.º 1
0
        private AnalysisInfo Analyze(TranslationContext context, Dictionary <string, string> cSharpSources)
        {
            AnalysisInfo info = new AnalysisInfo();

            info.Trees = GenerateSyntaxTrees(context, cSharpSources);

            List <MetadataReference> references = new List <MetadataReference>();

            references.Add(MetadataReference.CreateFromFile(typeof(Single).Assembly.Location));
            references.Add(MetadataReference.CreateFromFile(typeof(Vector4).Assembly.Location));
            references.Add(MetadataReference.CreateFromFile(typeof(CSharpShader).Assembly.Location));

            CSharpCompilationOptions options = new CSharpCompilationOptions(
                OutputKind.DynamicallyLinkedLibrary,
                optimizationLevel: OptimizationLevel.Debug,
                allowUnsafe: true);

            CSharpCompilation compilation = CSharpCompilation.Create("sharp_shader_temp", info.Trees, references, options);

            using (MemoryStream ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                foreach (Diagnostic d in result.Diagnostics)
                {
                    switch (d.Severity)
                    {
                    case DiagnosticSeverity.Error:
                        context.AddMessage(d.GetMessage(), d.Location);
                        info.HasError = true;
                        break;

                    case DiagnosticSeverity.Warning:
                        context.AddMessage(d.GetMessage(), d.Location, TranslationMessageType.Warning);
                        break;
                    }
                }

                if (!info.HasError)
                {
                    context.Reflection.Assembly = Assembly.Load(ms.ToArray());
                }
            }

            return(info);
        }
Ejemplo n.º 2
0
        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);
        }