Beispiel #1
0
        static public void Initialize(Device device)
        {
            if (m_Initialized)
            {
                throw new Exception("Already initialized!");
            }

            string[] filters = new[] { "*.hlsl", "*.fx" };

            foreach (string f in filters)
            {
                FileSystemWatcher w = new FileSystemWatcher();
                w.Filter              = f;
                w.Changed            += new FileSystemEventHandler(OnFileChanged);
                w.Renamed            += new RenamedEventHandler(OnFileChanged);
                w.NotifyFilter        = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
                w.Path                = Directory.GetCurrentDirectory() + "\\shaders";
                w.EnableRaisingEvents = true;
                m_Watchers.Add(w);
            }

            m_Initialized = true;
            m_Include     = new IncludeFX();

            CultureInfo ci = new CultureInfo("en-US", false);

            string[] filePaths = filters.SelectMany(f => Directory.GetFiles(Directory.GetCurrentDirectory() + "\\shaders", f)).ToArray();


            foreach (string path in filePaths)
            {
                ParseFile(device, path);
            }
        }
Beispiel #2
0
        //================================================================================================================//

        public static bool CompileShader(string baseSourceDirectory, string outputDirectory, string includeDirectory, HlslConfiguration global_, HlslFileConfiguration file)
        {
            try
            {
                string _path = Path.GetDirectoryName(file.FileNameRelative);

                IncludeFX include_ = new IncludeFX(baseSourceDirectory + _path + "\\");

                Console.WriteLine("Compiler Flags used: " + global_.GetShaderFlagsAsString().ToString());

                if (File.Exists(baseSourceDirectory + file.FileNameRelative))
                {
                    D3DCompiler.CompilationResult errors_ = D3DCompiler.ShaderBytecode.CompileFromFile(baseSourceDirectory + file.FileNameRelative,
                                                                                                       file.EntryPoint,
                                                                                                       file.PixelShaderVersion,
                                                                                                       global_.GetShaderFlags(),
                                                                                                       D3DCompiler.EffectFlags.None,
                                                                                                       null,
                                                                                                       include_);

                    D3DCompiler.ShaderBytecode shaderByteCode = errors_.Bytecode;

                    if (shaderByteCode != null)
                    {
                        string fileName_ = Path.GetFileNameWithoutExtension(Path.GetFileName(file.FileNameRelative));

                        string path_ = Path.GetFullPath(outputDirectory);

                        file.OutputName = path_ + fileName_ + ".cso";

                        System.IO.Directory.CreateDirectory(path_ + "\\");

                        FileStream write_ = new FileStream(file.OutputName, FileMode.Create, FileAccess.Write);

                        shaderByteCode.Save(write_);
                    }
                    else
                    {
                        throw new Exception(errors_.Message);
                    }

                    return(true);
                }
                else
                {
                    Console.WriteLine("File not found: " + baseSourceDirectory + file.FileNameRelative);
                }
            }
            catch (Exception ex_)
            {
                Console.WriteLine("Failed to compile file: " + ex_.Message);

                return(false);
            }

            return(false);
        }
Beispiel #3
0
        static public void Initialize(Device device)
        {
            if (m_Initialized)
            {
                throw new Exception("Already initialized!");
            }

            string[] filters = new[] { "*.hlsl", "*.fx" };

            foreach (string f in filters)
            {
                FileSystemWatcher w = new FileSystemWatcher();
                w.Filter              = f;
                w.Changed            += new FileSystemEventHandler(OnFileChanged);
                w.Renamed            += new RenamedEventHandler(OnFileChanged);
                w.NotifyFilter        = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
                w.Path                = Directory.GetCurrentDirectory() + "\\shaders";
                w.EnableRaisingEvents = true;
                m_Watchers.Add(w);
            }

            m_Initialized = true;
            m_Include     = new IncludeFX();

            CultureInfo ci = new CultureInfo("en-US", false);

            string[] filePaths = filters.SelectMany(f => Directory.GetFiles(Directory.GetCurrentDirectory() + "\\shaders", f)).ToArray();

            foreach (string path in filePaths)
            {
                string     fileName = GetFileNameFromPath(path);
                ShaderFile sf       = new ShaderFile();
                sf.m_DirectlyIncludedFiles = new List <String>();
                sf.m_FileName       = fileName;
                sf.m_FilePath       = path;
                sf.m_FlattenedFiles = new HashSet <String>();

                m_AllShaderFiles.Add(fileName, sf);
            }

            foreach (string path in filePaths)
            {
                ParseFile(path);
            }
            FlattenIncludes();
            FinalizeCompilationOnDevice(device);
        }
        public static void Initialize(Device device)
        {
            if (m_Initialized)
            {
                throw new Exception("Already initialized!");
            }

            string[] filters = new[] { "*.hlsl", "*.fx" };

            foreach (string f in filters)
            {
                FileSystemWatcher w = new FileSystemWatcher();
                w.Filter = f;
                w.Changed += new FileSystemEventHandler(OnFileChanged);
                w.Renamed += new RenamedEventHandler(OnFileChanged);
                w.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
                w.Path = Directory.GetCurrentDirectory() + "\\shaders";
                w.EnableRaisingEvents = true;
                m_Watchers.Add(w);
            }

            m_Initialized = true;
            m_Include = new IncludeFX();

            CultureInfo ci = new CultureInfo("en-US", false);
            string[] filePaths = filters.SelectMany(f => Directory.GetFiles(Directory.GetCurrentDirectory() + "\\shaders", f)).ToArray();

            foreach (string path in filePaths)
            {
                string fileName = GetFileNameFromPath(path);
                ShaderFile sf = new ShaderFile();
                sf.m_DirectlyIncludedFiles = new List<String>();
                sf.m_FileName = fileName;
                sf.m_FilePath = path;
                sf.m_FlattenedFiles = new HashSet<String>();

                m_AllShaderFiles.Add(fileName, sf);
            }

            foreach (string path in filePaths)
            {
                ParseFile(path);
            }
            FlattenIncludes();
            FinalizeCompilationOnDevice(device);
        }
Beispiel #5
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());
            }
        }
Beispiel #6
0
        static public void Initialize(Device device)
        {
            if (m_Initialized)
            {
                throw new Exception("Already initialized!");
            }

            string[] filters = new[] { "*.hlsl", "*.fx" };

            foreach (string f in filters)
            {
                FileSystemWatcher w = new FileSystemWatcher();
                w.Filter = f;
                w.Changed += new FileSystemEventHandler(OnFileChanged);
                w.Renamed += new RenamedEventHandler(OnFileChanged);
                w.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
                w.Path = Directory.GetCurrentDirectory() + "\\shaders";
                w.EnableRaisingEvents = true;
                m_Watchers.Add(w);
            }

            m_Initialized = true;
            m_Include = new IncludeFX();

            CultureInfo ci = new CultureInfo("en-US", false);
            string[] filePaths = filters.SelectMany(f => Directory.GetFiles(Directory.GetCurrentDirectory() + "\\shaders", f)).ToArray();


            foreach (string path in filePaths)
            {
                ParseFile(device, path);
            }
        }