예제 #1
0
        internal static Effect ReadAndCompile(string filePath)
        {
            var fullPath = AssetManager.GetFullPath(filePath, ".fx");

            if (!string.IsNullOrEmpty(fullPath))
            {
                var importer        = new EffectImporter();
                var processor       = new EffectProcessor();
                var pipelineManager = new PipelineManager("", "", "")
                {
                    Profile  = Runner.Application.Game.Graphics.GraphicsProfile,
                    Platform = GetTargetPlatform()
                };

                var processorContext = new PipelineProcessorContext(
                    pipelineManager,
                    new PipelineBuildEvent());
                var content         = importer.Import(fullPath, null);
                var compiledContent = processor.Process(content, processorContext);
                var graphicsDevice  = Runner.Application.Game.GraphicsDevice;

                return(new Effect(graphicsDevice, compiledContent.GetEffectCode()));
            }

            return(null);
        }
예제 #2
0
        public static Effect Compile(GraphicsDevice g, string file)
        {
            EffectImporter  ei  = new EffectImporter();
            EffectContent   ec  = ei.Import(file, new XNADynImporterContext());
            EffectProcessor ep  = new EffectProcessor();
            var             cec = ep.Process(ec, new XNADynProcessorContext());

            return(new Effect(g, cec.GetEffectCode()));
        }
예제 #3
0
        static int Main(string[] args)
        {
            // Make sure we have the right number of commandline arguments.
            if (args.Length != 4)
            {
                Console.Error.WriteLine("Usage: CompileEffect <targetPlatform> <targetProfile> <input.fx> <output.bin>");
                return(1);
            }

            // Parse the commandline arguments.
            TargetPlatform targetPlatform;

            if (!Enum.TryParse(args[0], true, out targetPlatform))
            {
                Console.Error.WriteLine("Invalid target platform {0}. Valid options are {1}.", args[0], GetEnumValues <TargetPlatform>());
                return(1);
            }

            GraphicsProfile targetProfile;

            if (!Enum.TryParse(args[1], true, out targetProfile))
            {
                Console.Error.WriteLine("Invalid target profile {0}. Valid options are {1}.", args[1], GetEnumValues <GraphicsProfile>());
                return(1);
            }

            string inputFilename  = args[2];
            string outputFilename = args[3];

            try
            {
                Console.WriteLine("Compiling {0} -> {1} for {2}, {3}", Path.GetFileName(inputFilename), outputFilename, targetPlatform, targetProfile);

                ContentBuildLogger logger = new CustomLogger();

                // Import the effect source code.
                EffectImporter         importer        = new EffectImporter();
                ContentImporterContext importerContext = new CustomImporterContext(logger);
                EffectContent          sourceEffect    = importer.Import(inputFilename, importerContext);

                // Compile the effect.
                EffectProcessor         processor        = new EffectProcessor();
                ContentProcessorContext processorContext = new CustomProcessorContext(targetPlatform, targetProfile, logger);
                CompiledEffectContent   compiledEffect   = processor.Process(sourceEffect, processorContext);

                // Write out the compiled effect code.
                File.WriteAllBytes(outputFilename, compiledEffect.GetEffectCode());
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0}", e.Message);
                return(1);
            }

            return(0);
        }
예제 #4
0
        public static byte[] ReadBytesFromSourceFile(string fileName)
        {
            EffectImporter  importer  = new EffectImporter( );
            EffectProcessor processor = new EffectProcessor( );

            EffectContent         content  = importer.Import(fileName, new ShaderImporterContext( ));
            CompiledEffectContent compiled = processor.Process(content, new ShaderProcessorContext( ));

            return(compiled.GetEffectCode( ));
        }
예제 #5
0
        private void BuildEffect(string effectFile, TargetPlatform targetPlatform, string defines = null)
        {
            var importerContext = new ImporterContext();
            var importer = new EffectImporter();
            var input = importer.Import(effectFile, importerContext);

            Assert.NotNull(input);

            var processorContext = new TestProcessorContext(targetPlatform, Path.ChangeExtension(effectFile, ".xnb"));
            var processor = new EffectProcessor { Defines = defines };
            var output = processor.Process(input, processorContext);

            Assert.NotNull(output);

            // TODO: Should we test the writer?
        }
예제 #6
0
        private void BuildEffect(string effectFile, TargetPlatform targetPlatform)
        {
            var importerContext = new ImporterContext();
            var importer        = new EffectImporter();
            var input           = importer.Import(effectFile, importerContext);

            Assert.NotNull(input);

            var processorContext = new ProcessorContext(targetPlatform, Path.ChangeExtension(effectFile, ".xnb"));
            var processor        = new EffectProcessor();
            var output           = processor.Process(input, processorContext);

            Assert.NotNull(output);

            // TODO: Should we test the writer?
        }
예제 #7
0
        private void LoadByteCodeFromSource(string text)
        {
            try
            {
                EffectImporter importer = new EffectImporter();
                EffectContent effectContent = new EffectContent
                {
                    Identity = new ContentIdentity { SourceFilename = "myshader.fx" },
                    EffectCode = text,
                };

                EffectProcessor processor = new EffectProcessor();
                processor.DebugMode = EffectProcessorDebugMode.Optimize;
                CompiledEffectContent compiledEffect = processor.Process(effectContent, new FlaiProcessorContext((GraphicsProfile)_targetProfile.SelectedValue, (TargetPlatform)_targetPlatform.SelectedValue));

                byte[] bytes = compiledEffect.GetEffectCode();
                StringBuilder sb = new StringBuilder(bytes.Length * 3);

                sb.AppendLine("#region Effect Byte Code");
                sb.AppendLine();

                sb.Append("byte[] ByteCode = new byte[]").Append(Environment.NewLine).Append("{").Append(Environment.NewLine);
                for (int i = 0; i < bytes.Length;)
                {
                    sb.Append('\t');
                    int max = Math.Min(i + 8, bytes.Length);
                    for (; i < max; i++)
                    {
                        sb.Append(bytes[i]).Append(", ");
                    }

                    sb.Append(Environment.NewLine);
                }

                sb.Append("};").Append(Environment.NewLine);

                sb.AppendLine();
                sb.Append("#endregion");

                _byteCodeTextBox.Text = sb.ToString();
            }
            catch (InvalidContentException e)
            {
                _byteCodeTextBox.Text = e.Message;
            }
        }
예제 #8
0
        static void Main(string[] args)
        {
            Console.Title = "Pico 3D Shader Compiler";
            Console.WriteLine(Console.Title);

            importer  = new EffectImporter( );
            processor = new EffectProcessor( );

            if (args.Length > 1)
            {
                if (args[0].EndsWith(".fx"))
                {
                    if (File.Exists(args[0]))
                    {
                        string output = args[1];

                        if (!output.EndsWith(".psf"))
                        {
                            output = (args[1] + ".psf");
                        }

                        Console.WriteLine("Compiling " + args[0]);
                        Console.WriteLine("Output: " + output);

                        byte[] effect_data = GetEffectCodeBytes(args[0]);
                        SaveBytesToFile(output, effect_data);

                        Console.WriteLine("[ Compiling OK ]");
                        effect_data = null;
                    }
                    else
                    {
                        Console.WriteLine("Input file does not exists!");
                    }
                }
                else
                {
                    Console.WriteLine("Wrong input file format!");
                }
            }
            else
            {
                Console.WriteLine("Check your input arguments.");
            }
        }
예제 #9
0
        static void Main( string[] args )
        {
            Console.Title = "Pico 3D Shader Compiler";
            Console.WriteLine( Console.Title );

            importer = new EffectImporter( );
            processor = new EffectProcessor( );

            if ( args.Length > 1 ) {
                if ( args[ 0 ].EndsWith( ".fx" ) ) {
                    if ( File.Exists( args[ 0 ] ) ) {
                        string output = args[ 1 ];

                        if ( !output.EndsWith( ".psf" ) ) {
                            output = ( args[ 1 ] + ".psf" );
                        }

                        Console.WriteLine( "Compiling " + args[ 0 ] );
                        Console.WriteLine( "Output: " + output );

                        byte[] effect_data = GetEffectCodeBytes( args[ 0 ] );
                        SaveBytesToFile( output, effect_data );

                        Console.WriteLine( "[ Compiling OK ]" );
                        effect_data = null;
                    }
                    else {
                        Console.WriteLine( "Input file does not exists!" );
                    }
                }
                else {
                    Console.WriteLine( "Wrong input file format!" );
                }
            }
            else {
                Console.WriteLine( "Check your input arguments." );
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            Log("Effect farm compiler to efb {0}.", EFParser.EfbVersion);

            if (args.Length < 2)
            {
                Log("Usage: efc <input_file> <config_file> [output_file]");
                return;
            }

            try
            {
                var inputFile = args[0];
                if (!File.Exists(inputFile))
                {
                    Log("Could not find '0'.", inputFile);
                    return;
                }

                var configFile = args[1];
                if (!File.Exists(inputFile))
                {
                    Log("Could not find '0'.", inputFile);
                    return;
                }
                var doc = XDocument.Parse(File.ReadAllText(configFile));

                // Parse config
                var config = new Config
                {
                    Targets = ParseTargets(doc)
                };
                if (config.Targets.Length == 0)
                {
                    GenerateError("No target platforms.");
                }

                var rootEntry = (from n in doc.Descendants("RootEntry") select n).FirstOrDefault();
                if (rootEntry == null)
                {
                    GenerateError("Could not find 'RootEntry' node.");
                }

                config.Root = ParseEntry(rootEntry);

                var variants = config.BuildVariants().ToArray();

                var outputFile = string.Empty;

                if (args.Length < 3)
                {
                    outputFile = Path.ChangeExtension(inputFile, "efb");
                }
                else
                {
                    outputFile = Path.ChangeExtension(args[2], "efb");
                }

                var outputLwt = File.GetLastWriteTime(outputFile);
                if (File.Exists(outputFile) &&
                    outputLwt > File.GetLastWriteTime(inputFile) &&
                    outputLwt > File.GetLastWriteTime(configFile))
                {
                    var resultVariants = Substract(variants, outputFile);
                    if (resultVariants.Length == 0)
                    {
                        Log("{0} is up to date.", Path.GetFileName(outputFile));
                        return;
                    }
                }

                var workingFolder = Path.GetDirectoryName(inputFile);
                var includeFx     = new IncludeFX(workingFolder);

                var importerContext  = new ImporterContext();
                var processorContext = new ProcessorContext();

                var effectImporter = new EffectImporter();
                var effectProcesor = new EffectProcessor();

                Log("{0} variants of effects are going to be compiled.", variants.Length);
                using (var stream = File.Open(outputFile, FileMode.Create))
                    using (var writer = new BinaryWriter(stream))
                    {
                        writer.Write(Encoding.UTF8.GetBytes(EFParser.EfbSignature));
                        writer.Write(EFParser.EfbVersion);

                        var idx = 0;
                        foreach (var variant in variants)
                        {
                            Log("#" + idx + ": " + variant.ToString());

                            switch (variant.Platform)
                            {
                            case EFPlatform.MonoGameDirectX:
                            case EFPlatform.MonoGameOpenGL:
                            {
                                var content = effectImporter.Import(inputFile, importerContext);

                                processorContext._targetPlatform = variant.Platform == EFPlatform.MonoGameDirectX ?
                                                                   TargetPlatform.Windows : TargetPlatform.DesktopGL;
                                effectProcesor.Defines = EFVariant.BuildKey(variant.Defines);

                                var result = effectProcesor.Process(content, processorContext);

                                WriteOutput(writer, variant, result.GetEffectCode());
                            }
                            break;

                            case EFPlatform.FNA:
                            {
                                var result = ShaderBytecode.CompileFromFile(inputFile,
                                                                            "fx_2_0",
                                                                            ShaderFlags.OptimizationLevel3,
                                                                            EffectFlags.None,
                                                                            ToMacroses(variant),
                                                                            includeFx);

                                if (result.ResultCode != Result.Ok)
                                {
                                    GenerateError(result.Message);
                                }

                                WriteOutput(writer, variant, result.Bytecode);
                            }
                            break;
                            }

                            ++idx;
                        }
                    }

                Log("Compilation to {0} was succesful.", Path.GetFileName(outputFile));
            }
            catch (Exception ex)
            {
                Log(ex.ToString());
            }
        }