Exemplo n.º 1
0
        void Run(string[] args)
        {
            var assemblyUri = new Uri(Assembly.GetEntryAssembly().CodeBase);
            var assemblyPath = Path.GetDirectoryName(assemblyUri.LocalPath);

            var newPath = Path.GetFullPath(Path.Combine(assemblyPath, @"..\Redist\D3D\" + (IntPtr.Size == 4 ? "x86" : "x64"))) + ";" + Environment.GetEnvironmentVariable("PATH");
            Environment.SetEnvironmentVariable("PATH", newPath);

            // Print the exe header
            PrintHeader();

            // Parse the command line
            if (!ParseCommandLine(args))
                Environment.Exit(-1);

            var options = this;

            // ----------------------------------------------------------------
            // Process macros
            // ----------------------------------------------------------------
            var macros = new List<EffectData.ShaderMacro>();
            foreach (var define in options.Defines)
            {
                var nameValue = define.Split('=');
                string name = nameValue[0];
                string value = null;
                if (nameValue.Length > 1)
                {
                    value = nameValue[1];
                }
                macros.Add(new EffectData.ShaderMacro(name, value));
            }

            // ----------------------------------------------------------------
            // Setup compiler flags
            // ----------------------------------------------------------------
            var flags = EffectCompilerFlags.None;
            if (options.Debug)
                flags |= EffectCompilerFlags.Debug;

            switch (options.OptimizationLevel)
            {
                case 0:
                    flags |= EffectCompilerFlags.OptimizationLevel0;
                    break;
                case 1:
                    flags |= EffectCompilerFlags.OptimizationLevel1;
                    break;
                case 2:
                    flags |= EffectCompilerFlags.OptimizationLevel2;
                    break;
                case 3:
                    flags |= EffectCompilerFlags.OptimizationLevel3;
                    break;
            }

            if (options.PackRowMajor)
                flags |= EffectCompilerFlags.PackMatrixRowMajor;

            if (options.PackColumnMajor)
                flags |= EffectCompilerFlags.PackMatrixColumnMajor;

            hasErrors = false;

            // ----------------------------------------------------------------
            // Pre check files
            // ----------------------------------------------------------------
            if (options.OutputClassFile == null && options.OutputFile == null)
            {
                options.OutputFile = "output.tkfxo";
            }

            // Check for output files
            bool outputFileExist = options.OutputClassFile != null && File.Exists(options.OutputClassFile);
            if (options.OutputFile != null && !File.Exists(options.OutputFile))
            {
                outputFileExist = false;
            }

            // ----------------------------------------------------------------
            // Process each fx files / tkfxo files
            // ----------------------------------------------------------------
            var fxFile = options.FxFile;
            var filePath = Path.Combine(Environment.CurrentDirectory, fxFile);

            // Check that input file exists
            if (!File.Exists(filePath))
            {
                ErrorColor();
                Console.Error.WriteLine("File [{0}] does not exist", fxFile);
                ResetColor();
                Abort();
            }
            
            // New Compiler
            var compiler = new EffectCompiler();

            string outputDependencyDirPath = Path.Combine(Environment.CurrentDirectory, OutputDependencyDirectory);
            string outputDependencyFilePath = Path.Combine(outputDependencyDirPath,  compiler.GetDependencyFileNameFromEffectPath(options.FxFile));

            if (AllowDynamicCompiling)
            {
                CompileOnlyIfNewer = true;
            }

            if (CompileOnlyIfNewer)
            {
                if (!compiler.CheckForChanges(outputDependencyFilePath) && outputFileExist)
                {
                    Console.Error.WriteLine("Nothing to compile. Output file [{0}] is up-to-date", options.OutputFile);
                    Environment.Exit(0);
                }
            }

            var viewOnly = false;
            // Try to load this file as a precompiled file
            var effectData = EffectData.Load(fxFile);
            EffectCompilerResult compilerResult = null;

            if (effectData != null)
            {
                Console.WriteLine("Load Compiled File [{0}]", fxFile);
                viewOnly = true;
            }
            else
            {
                // Compile the fx file
                Console.WriteLine("Compile Effect File [{0}]", filePath);
                compilerResult = compiler.Compile(File.ReadAllText(filePath), filePath, flags, macros, options.IncludeDirs, AllowDynamicCompiling, CompileOnlyIfNewer ? outputDependencyFilePath : null);

                // If there is any warning, errors, turn Error color on
                if (compilerResult.Logger.Messages.Count > 0)
                {
                    ErrorColor();
                }

                // Show a message error for the current file
                if (compilerResult.HasErrors)
                {
                    Console.Error.WriteLine("Error when compiling file [{0}]:", fxFile);
                    hasErrors = true;
                }

                // Print all messages (warning and errors)
                foreach (var logMessage in compilerResult.Logger.Messages)
                {
                    Console.WriteLine(logMessage);
                }

                // If we have some messages, reset the color back
                if (compilerResult.Logger.Messages.Count > 0)
                {
                    ResetColor();
                }

                effectData = compilerResult.EffectData;
            }

            if (!NoDisassembly && effectData != null)
            {
                DumpBytecode(compiler, effectData);
            }

            if (hasErrors)
            {
                Abort();
            }

            if (!viewOnly)
            {
                Console.WriteLine();

                if (CompileOnlyIfNewer && compilerResult.DependencyFilePath != null)
                {
                    // Dependency file save to 
                    Console.WriteLine("Save dependency list to [{0}]", outputDependencyFilePath);
                }

                if (OutputClassFile != null)
                {
                    var codeWriter = new EffectDataCodeWriter
                                         {
                                             Namespace = OutputNamespace,
                                             ClassName = OutputClassname ?? Path.GetFileNameWithoutExtension(OutputClassFile),
                                             FieldName = OutputFieldName,
                                         };

                    Console.WriteLine("Save C# code output to [{0}]", OutputClassFile);
                    using (var stream = new NativeFileStream(OutputClassFile, NativeFileMode.Create, NativeFileAccess.Write, NativeFileShare.Write)) codeWriter.Write(effectData, new StreamWriter(stream, Encoding.UTF8));
                }

                if (options.OutputFile != null)
                {
                    Console.WriteLine("Save binary output to [{0}]", options.OutputFile);
                    // Save the result
                    effectData.Save(options.OutputFile);
                }
            }
        }