Exemplo n.º 1
0
        internal static void Generate(bool clean, OnShaderCacheProgressDelegate onShaderCacheProgress)
        {
            // remove contents of the whole cache folder
            var outputPath = Path.Combine(MyFileSystem.UserDataPath, MyShadersDefines.CachePath);

            Directory.CreateDirectory(outputPath);
            if (clean)
            {
#if XB1
                System.Diagnostics.Debug.Assert(false, "TODO for XB1.");
#else // !XB1
                string[] cacheFiles = Directory.GetFiles(outputPath, "*.cache");
                for (int i = 0; i < cacheFiles.Length; i++)
                {
                    File.Delete(cacheFiles[i]);
                }
#endif // !XB1
            }

            string         filename      = null;
            CacheGenerator generatorDesc = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CacheGenerator));
                filename = Path.Combine(MyShaders.ShadersPath, CacheGeneratorFile);
                TextReader reader = new StreamReader(filename);
                generatorDesc = serializer.Deserialize(reader) as CacheGenerator;
            }
            catch (Exception ex)
            {
                throw new FileLoadException("File " + filename + " not found or invalid: ", ex);
            }
            if (generatorDesc == null)
            {
                throw new FileLoadException("File " + filename + " not found or invalid: ");
            }

            GenerateInternal(generatorDesc, onShaderCacheProgress);
            GenerateMaterials(generatorDesc, onShaderCacheProgress);
        }
Exemplo n.º 2
0
        private static ShaderMacro[] m_globalMacros = new[] { new ShaderMacro("MS_SAMPLE_COUNT", 2), new ShaderMacro("MS_SAMPLE_COUNT", 4), new ShaderMacro("MS_SAMPLE_COUNT", 8) };//, new ShaderMacro("FXAA_ENABLED",null)};

        private static void GenerateMaterials(CacheGenerator generatorDesc, OnShaderCacheProgressDelegate onShaderCacheProgress)
        {
            int combinationsCount   = generatorDesc.Materials.Length * generatorDesc.Passes.Length * generatorDesc.Combos.Length;
            int combinationsCounter = 0;

            for (int i = 0; i < generatorDesc.Materials.Length; i++)
            {
                var materialId               = MyStringId.GetOrCompute(generatorDesc.Materials[i].Id);
                var materialFlags            = ParseFlags(generatorDesc.Materials[i].FlagNames);
                var unsupportedMaterialFlags = ParseFlags(generatorDesc.Materials[i].UnsupportedFlagNames);
                for (int j = 0; j < generatorDesc.Passes.Length; j++)
                {
                    var passId               = MyStringId.GetOrCompute(generatorDesc.Passes[j].Id);
                    var passFlags            = ParseFlags(generatorDesc.Passes[j].FlagNames);
                    var unsupportedPassFlags = ParseFlags(generatorDesc.Passes[j].UnsupportedFlagNames);
                    for (int c = 0; c < generatorDesc.Combos.Length; c++)
                    {
                        combinationsCounter++;
                        float progress = (float)combinationsCounter * 100 / combinationsCount;
                        if (string.IsNullOrEmpty(generatorDesc.Combos[c].Material) && string.IsNullOrEmpty(generatorDesc.Combos[c].Pass))
                        {
                            GenerateCombo(materialId, passId, materialFlags | passFlags, unsupportedMaterialFlags | unsupportedPassFlags, generatorDesc.Combos[c].ComboList1, generatorDesc.Combos[c].ComboList2, progress, onShaderCacheProgress);
                        }
                    }
                }
            }
            for (int c = 0; c < generatorDesc.Combos.Length; c++)
            {
                if (!string.IsNullOrEmpty(generatorDesc.Combos[c].Material) && !string.IsNullOrEmpty(generatorDesc.Combos[c].Pass))
                {
                    combinationsCounter++;
                    float progress   = (float)combinationsCounter * 100 / combinationsCount;
                    var   materialId = MyStringId.GetOrCompute(generatorDesc.Combos[c].Material);
                    var   passId     = MyStringId.GetOrCompute(generatorDesc.Combos[c].Pass);
                    GenerateCombo(materialId, passId, MyShaderUnifiedFlags.NONE, MyShaderUnifiedFlags.NONE, generatorDesc.Combos[c].ComboList1, generatorDesc.Combos[c].ComboList2, progress, onShaderCacheProgress);
                }
            }
        }
Exemplo n.º 3
0
        private static void GenerateInternal(CacheGenerator generatorDesc, OnShaderCacheProgressDelegate onShaderCacheProgress)
        {
#if XB1
            System.Diagnostics.Debug.Assert(false, "TODO for XB1.");
#else // !XB1
            var      shaderPath            = Path.Combine(MyShaders.ShadersPath);
            string[] files                 = GetShadersRecursively(shaderPath, generatorDesc.Ignores);
            var      macroVariants         = new List <string[]>();
            var      macroVariantMandatory = new List <bool>();
            for (int i = 0; i < files.Length; i++)
            {
                float  progress = (float)i * 100 / files.Length;
                string file     = files[i];
                string fileText = file.Remove(0, shaderPath.Length);
                using (var reader = new StreamReader(file))
                {
                    var  source = reader.ReadToEnd();
                    bool any    = false;
                    if (source.IndexOf(ANNOTATION_SKIP) != -1)
                    {
                        continue;
                    }

                    for (MyShaderProfile profile = 0; profile < MyShaderProfile.count; profile++)
                    {
                        string function = MyShaders.ProfileEntryPoint(profile);
                        if (source.Contains(function))
                        {
                            // generate list of macro variants
                            macroVariants.Clear();
                            macroVariantMandatory.Clear();

                            // global ones
                            macroVariants.Add(new[] { "MS_SAMPLE_COUNT 2", "MS_SAMPLE_COUNT 4", "MS_SAMPLE_COUNT 8", "FXAA_ENABLED" });
                            macroVariantMandatory.Add(false);

                            // shader specific ones
                            int defineEndIndex = 0;
                            while (true)
                            {
                                int defineIndex = source.IndexOf('@', defineEndIndex);
                                if (defineIndex == -1)
                                {
                                    break;
                                }
                                int  advance   = -1;
                                bool mandatory = false;
                                defineIndex++;
                                if (CheckAnnotation(source, defineIndex, ANNOTATION_DEFINE_MANDATORY))
                                {
                                    advance   = ANNOTATION_DEFINE_MANDATORY.Length;
                                    mandatory = true;
                                }
                                else if (CheckAnnotation(source, defineIndex, ANNOTATION_DEFINE))
                                {
                                    advance = ANNOTATION_DEFINE.Length;
                                }
                                if (advance == -1)
                                {
                                    defineEndIndex++;
                                    continue;
                                }
                                defineIndex   += advance;
                                defineEndIndex = source.IndexOf("\n", defineIndex);
                                if (defineEndIndex == -1)
                                {
                                    defineEndIndex = source.Length;
                                }
                                var define = source.Substring(defineIndex, defineEndIndex - defineIndex);
                                define = define.Trim();
                                var defineEntries = define.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                macroVariants.Add(defineEntries);
                                macroVariantMandatory.Add(mandatory);
                            }

                            // init macro counters based on mandatory flag
                            var counters = new int[macroVariants.Count];
                            for (int j = 0; j < macroVariants.Count; j++)
                            {
                                counters[j] = macroVariantMandatory[j] ? 1 : 0;
                            }

                            // compile all variants
                            bool finished = false;
                            while (!finished)
                            {
                                // prepare macros
                                var macros = new List <ShaderMacro>();
                                for (int j = 0; j < macroVariants.Count; j++)
                                {
                                    if (counters[j] > 0)
                                    {
                                        var define      = macroVariants[j][counters[j] - 1];
                                        var defineSplit = define.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                                        MyDebug.AssertDebug(defineSplit.Length == 1 || defineSplit.Length == 2, "Invalid define @ " + fileText + ": " + define);
                                        if (defineSplit.Length > 1)
                                        {
                                            macros.Add(new ShaderMacro(defineSplit[0].Trim(), defineSplit[1].Trim()));
                                        }
                                        else
                                        {
                                            macros.Add(new ShaderMacro(defineSplit[0].Trim(), null));
                                        }
                                    }
                                }

                                // compile
                                PreCompile(file, macros, profile, fileText, "", progress, onShaderCacheProgress);
                                any = true;

                                // increase variants counters
                                int countersIndex = counters.Length - 1;
                                do
                                {
                                    counters[countersIndex]++;
                                    if (counters[countersIndex] > macroVariants[countersIndex].Length)
                                    {
                                        counters[countersIndex] = macroVariantMandatory[countersIndex] ? 1 : 0;
                                        if (countersIndex == 0)
                                        {
                                            finished = true;
                                        }
                                        countersIndex--;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                } while (countersIndex >= 0);
                            }
                        }
                    }
                    if (!any && onShaderCacheProgress != null)
                    {
                        onShaderCacheProgress(progress, file, "", "", "", "No entry point found.", true);
                    }
                }
            }
#endif // !XB1
        }