예제 #1
0
        private static void FillConfig()
        {
            string[] separators = { "," };

            // Shaders extensions
            Config.ShaderExtensions = new List <string>();
            foreach (string extension in Data["ShaderCompiler"]["ShaderExtensions"].Split(separators, StringSplitOptions.RemoveEmptyEntries))
            {
                Config.ShaderExtensions.Add(extension.Trim());
            }

            // Ignored folders
            Config.IgnoredFolders = new List <string>();
            foreach (string folder in Data["ShaderCompiler"]["IgnoredFolders"].Split(separators, StringSplitOptions.RemoveEmptyEntries))
            {
                Config.IgnoredFolders.Add(ResolvePath(folder.Trim(), false));
            }

            Config.ShaderSourcePath         = ResolvePath(Data["ShaderCompiler"]["ShaderSourcePath"], true);
            Config.ShaderHFile              = ResolvePath(Data["ShaderCompiler"]["ShaderHFile"], true);
            Config.ConstantBufferHFile      = ResolvePath(Data["ShaderCompiler"]["ConstantBufferHFile"], true);
            Config.VertexLayoutHFile        = ResolvePath(Data["ShaderCompiler"]["VertexLayoutHFile"], true);
            Config.DatabasePath             = ResolvePath(Data["ShaderCompiler"]["DatabasePath"], false);
            Config.GeneratedFolderPath      = ResolvePath(Data["ShaderCompiler"]["GeneratedFolderPath"], false);
            Config.GeneratedHeaderExtension = Data["ShaderCompiler"]["GeneratedHeaderExtension"];

            // RecursiveSearch
            bool succes = Boolean.TryParse(Data["ShaderCompiler"]["RecursiveSearch"], out Config.RecursiveSearch);

            if (!succes)
            {
                Config.RecursiveSearch = false;
            }

            // EnableDebugInformation
            succes = Boolean.TryParse(Data["ShaderCompiler"]["EnableDebugInformation"], out Config.EnableDebugInformation);
            if (!succes)
            {
                Config.EnableDebugInformation = false;
            }

            // Weird setup, this is to make code smaller
            string currentKey = "";

            try
            {
                currentKey      = "Compiler";
                Config.Compiler = EnumUtils.FromDescription <Compiler>(Data["ShaderCompiler"][currentKey]);

                currentKey         = "ShaderModel";
                Config.ShaderModel = EnumUtils.FromDescription <ShaderModel>(Data["ShaderCompiler"][currentKey]);
            }
            catch (EnumException e)
            {
                throw new Exception(e.Message + " when parsing config file. Key=" + currentKey);
            }

            Config.GlobalDefines = new List <string>();
            foreach (string define in Data["ShaderCompiler"]["GlobalDefines"].Split(separators, StringSplitOptions.RemoveEmptyEntries))
            {
                Config.GlobalDefines.Add(define.Trim());
            }

            Config.Verify(throws: true);
        }
예제 #2
0
        // Processes all headers from a shader file and compiles every permutation
        public void Compile(HeaderInfo inHeader, ShaderFile ioShaderFile)
        {
            Console.WriteLine("HEADER:\t\t" + inHeader.GetDebugString());

            // Shader code as header file or binary file
            // TODO: Only header files for now
            bool   header_file   = true;
            string export_option = header_file ? "Fh" : "Fo";

            string cmd_input_file    = ioShaderFile.FullPath;
            string cmd_output_file   = CreateCommand(export_option, inHeader.GetGeneratedFileName(ioShaderFile));
            string cmd_variable_name = header_file ? CreateCommand("Vn", "g_" + inHeader.Name) : "";
            string cmd_entry_point   = CreateCommand("E", inHeader.EntryPoint);
            string cmd_profile       = CreateCommand("T", EnumUtils.ToDescription(inHeader.Type).ToLower() + "_" + EnumUtils.ToDescription(Config.ShaderModel));
            string cmd_optimization  = CreateCommand("Od");
            string cmd_debug_info    = Config.EnableDebugInformation ? CreateCommand("Zi") : "";
            string cmd_defines       = GenerateDefines(inHeader, Config.ShaderModel);
            string cmd_nologo        = CreateCommand("nologo");

            // Don't output to file in Test. This should just output the shader code in the console
            if (Arguments.Operation == Arguments.OperationType.Test)
            {
                cmd_variable_name = "";
                cmd_output_file   = "";
            }

            string args = CombineCommands(cmd_input_file, cmd_output_file, cmd_variable_name, cmd_entry_point, cmd_profile,
                                          cmd_optimization, cmd_debug_info, cmd_defines, cmd_nologo);

            string  compiler_exe = GetCompilerPath();
            Process process      = Process.Start(compiler_exe);

            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError  = true;
            process.StartInfo.UseShellExecute        = false;
            process.StartInfo.CreateNoWindow         = true;
            process.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            process.StartInfo.Arguments = args;

            process.Start();

            // To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
            process.StandardOutput.ReadToEnd();

            process.WaitForExit();

            ioShaderFile.DidCompile = (process.ExitCode == 0);

            if (!ioShaderFile.DidCompile)
            {
                string errorMessage = process.StandardError.ReadToEnd() + "Failed with commandline:\n" + compiler_exe + " " + args + "\n\n";
                throw new Exception(errorMessage);
                //throw new Exception(process.StandardOutput.ReadToEnd());
            }

            //Console.WriteLine(process.StandardError.ReadToEnd());
        }