/// <summary> /// Get the ModuleMixinInfo based on the ShaderSource and the macros. Creates the needed shader if necessary /// </summary> /// <param name="shaderSource">the ShaderSource</param> /// <param name="macros">the macros</param> /// <param name="macrosString">the name of the macros</param> /// <returns>ModuleMixinInfo.</returns> private ModuleMixinInfo GetModuleMixinInfo(ShaderSource shaderSource, SiliconStudio.Shaders.Parser.ShaderMacro[] macros, string macrosString = null) { if (macros == null) { macros = new SiliconStudio.Shaders.Parser.ShaderMacro[0]; } if (macrosString == null) { macrosString = string.Join(",", macros.OrderBy(x => x.Name)); } List <ModuleMixinInfo> context; if (!mapMacrosToMixins.TryGetValue(macrosString, out context)) { context = new List <ModuleMixinInfo>(); mapMacrosToMixins.Add(macrosString, context); } var mixinInfo = context.FirstOrDefault(x => x.AreEqual(shaderSource, macros)); if (mixinInfo == null) { mixinInfo = BuildMixinInfo(shaderSource, macros); if (mixinInfo.Instanciated) { MixinInfos.Add(mixinInfo); mapMacrosToMixins[macrosString].Add(mixinInfo); mixinInfo.MinimalContext.Add(mixinInfo); if (!mixinInfo.Log.HasErrors) { LoadNecessaryShaders(mixinInfo, macros, macrosString); } mixinInfo.MinimalContext = new HashSet <ModuleMixinInfo>(mixinInfo.MinimalContext.Distinct()); } } return(mixinInfo); }
/// <summary> /// Merge the set of macros in the mixin. The top level macros are always overidden by the child's ones (the one defined in the current ShaderMixinSource). /// Also update the macros of the mixin. /// </summary> /// <param name="mixin">The mixin that will be looked at with the macros.</param> /// <param name="macros">The external macros.</param> /// <returns>An array with all the macros</returns> private SiliconStudio.Shaders.Parser.ShaderMacro[] MergeMacroSets(ShaderMixinSource mixin, SiliconStudio.Shaders.Parser.ShaderMacro[] macros) { var newMacros = new List <SiliconStudio.Shaders.Parser.ShaderMacro>(); // get the parent macros foreach (var macro in macros) { newMacros.RemoveAll(x => x.Name == macro.Name); newMacros.Add(macro); } // override with child macros, the mixin's ones foreach (var macro in mixin.Macros) { newMacros.RemoveAll(x => x.Name == macro.Name); var tempMacro = new SiliconStudio.Shaders.Parser.ShaderMacro(macro.Name, macro.Definition); newMacros.Add(tempMacro); } mixin.Macros = newMacros.Select(x => new ShaderMacro(x.Name, x.Definition)).ToList(); return(newMacros.ToArray()); }
internal ShaderCompilationContext ParseAndAnalyze(ShaderMixinSource shaderMixinSource, Paradox.Shaders.ShaderMacro[] macros, out ShaderMixinParsingResult parsingResult, out HashSet<ModuleMixinInfo> mixinsToAnalyze) { // Creates a parsing result parsingResult = new ShaderMixinParsingResult(); SiliconStudio.Shaders.Parser.ShaderMacro[] macrosParser; if (macros == null) { macrosParser = new SiliconStudio.Shaders.Parser.ShaderMacro[0]; } else { macrosParser = new SiliconStudio.Shaders.Parser.ShaderMacro[macros.Length]; for (var i = 0; i < macros.Length; ++i) macrosParser[i] = new SiliconStudio.Shaders.Parser.ShaderMacro(macros[i].Name, macros[i].Definition); } //PerformanceLogger.Start(PerformanceStage.Global); // ---------------------------------------------------------- // Load all shaders // ---------------------------------------------------------- lock (shaderLibrary) { //PerformanceLogger.Start(PerformanceStage.Loading); mixinsToAnalyze = shaderLibrary.LoadShaderSource(shaderMixinSource, macrosParser); //PerformanceLogger.Stop(PerformanceStage.Loading); } // Extract all ModuleMixinInfo and check for any errors var allMixinInfos = new HashSet<ModuleMixinInfo>(); foreach (var moduleMixinInfo in mixinsToAnalyze) { allMixinInfos.UnionWith(moduleMixinInfo.MinimalContext); } foreach (var moduleMixinInfo in allMixinInfos) { moduleMixinInfo.Log.CopyTo(parsingResult); var ast = moduleMixinInfo.MixinAst; var shaderClassSource = moduleMixinInfo.ShaderSource as ShaderClassSource; // If we have a ShaderClassSource and it is not an inline one, then we can store the hash sources if (ast != null && shaderClassSource != null) { parsingResult.HashSources[shaderClassSource.ClassName] = ast.SourceHash; } } // Return directly if there was any errors if (parsingResult.HasErrors) return null; // ---------------------------------------------------------- // Perform Type Analysis // ---------------------------------------------------------- //PerformanceLogger.Start(PerformanceStage.TypeAnalysis); var context = GetCompilationContext(mixinsToAnalyze, parsingResult); //PerformanceLogger.Stop(PerformanceStage.TypeAnalysis); // Return directly if there was any errors if (parsingResult.HasErrors) return context; lock (SemanticAnalyzerLock) { //PerformanceLogger.Start(PerformanceStage.SemanticAnalysis); //SemanticPerformance.Start(SemanticStage.Global); foreach (var mixin in mixinsToAnalyze) context.Analyze(mixin); //SemanticPerformance.Pause(SemanticStage.Global); //PerformanceLogger.Stop(PerformanceStage.SemanticAnalysis); } return context; }
/// <summary> /// Merge the set of macros in the mixin. The top level macros are always overidden by the child's ones (the one defined in the current ShaderMixinSource). /// Also update the macros of the mixin. /// </summary> /// <param name="mixin">The mixin that will be looked at with the macros.</param> /// <param name="macros">The external macros.</param> /// <returns>An array with all the macros</returns> private SiliconStudio.Shaders.Parser.ShaderMacro[] MergeMacroSets(ShaderMixinSource mixin, SiliconStudio.Shaders.Parser.ShaderMacro[] macros) { var newMacros = new List<SiliconStudio.Shaders.Parser.ShaderMacro>(); // get the parent macros foreach (var macro in macros) { newMacros.RemoveAll(x => x.Name == macro.Name); newMacros.Add(macro); } // override with child macros, the mixin's ones foreach (var macro in mixin.Macros) { newMacros.RemoveAll(x => x.Name == macro.Name); var tempMacro = new SiliconStudio.Shaders.Parser.ShaderMacro(macro.Name, macro.Definition); newMacros.Add(tempMacro); } mixin.Macros = newMacros.Select(x => new ShaderMacro(x.Name, x.Definition)).ToList(); return newMacros.ToArray(); }
/// <summary> /// Get the ModuleMixinInfo based on the ShaderSource and the macros. Creates the needed shader if necessary /// </summary> /// <param name="shaderSource">the ShaderSource</param> /// <param name="macros">the macros</param> /// <param name="macrosString">the name of the macros</param> /// <returns>ModuleMixinInfo.</returns> private ModuleMixinInfo GetModuleMixinInfo(ShaderSource shaderSource, SiliconStudio.Shaders.Parser.ShaderMacro[] macros, string macrosString = null) { if (macros == null) macros = new SiliconStudio.Shaders.Parser.ShaderMacro[0]; if (macrosString == null) { macrosString = string.Join(",", macros.OrderBy(x => x.Name)); } List<ModuleMixinInfo> context; if (!mapMacrosToMixins.TryGetValue(macrosString, out context)) { context = new List<ModuleMixinInfo>(); mapMacrosToMixins.Add(macrosString, context); } var mixinInfo = context.FirstOrDefault(x => x.AreEqual(shaderSource, macros)); if (mixinInfo == null) { mixinInfo = BuildMixinInfo(shaderSource, macros); if (mixinInfo.Instanciated) { MixinInfos.Add(mixinInfo); mapMacrosToMixins[macrosString].Add(mixinInfo); mixinInfo.MinimalContext.Add(mixinInfo); if (!mixinInfo.Log.HasErrors) { LoadNecessaryShaders(mixinInfo, macros, macrosString); } mixinInfo.MinimalContext = new HashSet<ModuleMixinInfo>(mixinInfo.MinimalContext.Distinct()); } } return mixinInfo; }
internal ShaderCompilationContext ParseAndAnalyze(ShaderMixinSource shaderMixinSource, Xenko.Shaders.ShaderMacro[] macros, out ShaderMixinParsingResult parsingResult, out HashSet <ModuleMixinInfo> mixinsToAnalyze) { // Creates a parsing result parsingResult = new ShaderMixinParsingResult(); SiliconStudio.Shaders.Parser.ShaderMacro[] macrosParser; if (macros == null) { macrosParser = new SiliconStudio.Shaders.Parser.ShaderMacro[0]; } else { macrosParser = new SiliconStudio.Shaders.Parser.ShaderMacro[macros.Length]; for (var i = 0; i < macros.Length; ++i) { macrosParser[i] = new SiliconStudio.Shaders.Parser.ShaderMacro(macros[i].Name, macros[i].Definition); } } //PerformanceLogger.Start(PerformanceStage.Global); // ---------------------------------------------------------- // Load all shaders // ---------------------------------------------------------- lock (shaderLibrary) { //PerformanceLogger.Start(PerformanceStage.Loading); mixinsToAnalyze = shaderLibrary.LoadShaderSource(shaderMixinSource, macrosParser); //PerformanceLogger.Stop(PerformanceStage.Loading); } // Extract all ModuleMixinInfo and check for any errors var allMixinInfos = new HashSet <ModuleMixinInfo>(); foreach (var moduleMixinInfo in mixinsToAnalyze) { allMixinInfos.UnionWith(moduleMixinInfo.MinimalContext); } foreach (var moduleMixinInfo in allMixinInfos) { moduleMixinInfo.Log.CopyTo(parsingResult); var ast = moduleMixinInfo.MixinAst; var shaderClassSource = moduleMixinInfo.ShaderSource as ShaderClassSource; // If we have a ShaderClassSource and it is not an inline one, then we can store the hash sources if (ast != null && shaderClassSource != null) { parsingResult.HashSources[shaderClassSource.ClassName] = moduleMixinInfo.SourceHash; } } // Return directly if there was any errors if (parsingResult.HasErrors) { return(null); } // ---------------------------------------------------------- // Perform Type Analysis // ---------------------------------------------------------- //PerformanceLogger.Start(PerformanceStage.TypeAnalysis); var context = GetCompilationContext(mixinsToAnalyze, parsingResult); //PerformanceLogger.Stop(PerformanceStage.TypeAnalysis); // Return directly if there was any errors if (parsingResult.HasErrors) { return(context); } lock (SemanticAnalyzerLock) { //PerformanceLogger.Start(PerformanceStage.SemanticAnalysis); //SemanticPerformance.Start(SemanticStage.Global); foreach (var mixin in mixinsToAnalyze) { context.Analyze(mixin); } //SemanticPerformance.Pause(SemanticStage.Global); //PerformanceLogger.Stop(PerformanceStage.SemanticAnalysis); } return(context); }
/// <summary> /// Mixes shader parts to produces a single HLSL file shader. /// </summary> /// <param name="shaderMixinSource">The shader source.</param> /// <param name="macros">The shader perprocessor macros.</param> /// <param name="modifiedShaders">The list of modified shaders.</param> /// <returns>The combined shader in AST form.</returns> public ShaderMixinParsingResult Parse(ShaderMixinSource shaderMixinSource, Paradox.Shaders.ShaderMacro[] macros = null, HashSet <string> modifiedShaders = null) { //SemanticPerformance.Reset(); //PerformanceLogger.Reset(); //MixPerformance.Reset(); //GenerateShaderPerformance.Reset(); //StreamCreatorPerformance.Reset(); // updates the list of modified shaders. shaderLibrary.ModifiedShaders = modifiedShaders; // Creates a parsing result var parsingResult = new ShaderMixinParsingResult(); SiliconStudio.Shaders.Parser.ShaderMacro[] macrosParser; if (macros == null) { macrosParser = new SiliconStudio.Shaders.Parser.ShaderMacro[0]; } else { macrosParser = new SiliconStudio.Shaders.Parser.ShaderMacro[macros.Length]; for (var i = 0; i < macros.Length; ++i) { macrosParser[i] = new SiliconStudio.Shaders.Parser.ShaderMacro(macros[i].Name, macros[i].Definition); } } //PerformanceLogger.Start(PerformanceStage.Global); // ---------------------------------------------------------- // Load all shaders // ---------------------------------------------------------- HashSet <ModuleMixinInfo> mixinsToAnalyze; lock (shaderLibrary) { //PerformanceLogger.Start(PerformanceStage.Loading); mixinsToAnalyze = shaderLibrary.LoadShaderSource(shaderMixinSource, macrosParser); //PerformanceLogger.Stop(PerformanceStage.Loading); } // Extract all ModuleMixinInfo and check for any errors var allMixinInfos = new HashSet <ModuleMixinInfo>(); foreach (var moduleMixinInfo in mixinsToAnalyze) { allMixinInfos.UnionWith(moduleMixinInfo.MinimalContext); } foreach (var moduleMixinInfo in allMixinInfos) { moduleMixinInfo.Log.CopyTo(parsingResult); var ast = moduleMixinInfo.MixinAst; var shaderClassSource = moduleMixinInfo.ShaderSource as ShaderClassSource; if (ast != null && shaderClassSource != null) { var sourcePath = SourceManager.FindFilePath(shaderClassSource.ClassName); if (sourcePath == null) { throw new InvalidOperationException(string.Format("Can't find source path for class {0}", shaderClassSource.ClassName)); } parsingResult.HashSources[sourcePath] = ast.SourceHash; } } // Return directly if there was any errors if (parsingResult.HasErrors) { return(parsingResult); } // ---------------------------------------------------------- // Perform Type Analysis // ---------------------------------------------------------- //PerformanceLogger.Start(PerformanceStage.TypeAnalysis); var context = GetCompilationContext(mixinsToAnalyze, parsingResult); //PerformanceLogger.Stop(PerformanceStage.TypeAnalysis); // Return directly if there was any errors if (parsingResult.HasErrors) { return(parsingResult); } lock (SemanticAnalyzerLock) { //PerformanceLogger.Start(PerformanceStage.SemanticAnalysis); //SemanticPerformance.Start(SemanticStage.Global); foreach (var mixin in mixinsToAnalyze) { context.Analyze(mixin); } //SemanticPerformance.Pause(SemanticStage.Global); //PerformanceLogger.Stop(PerformanceStage.SemanticAnalysis); } // Return directly if there was any errors if (parsingResult.HasErrors) { return(parsingResult); } // Update the clone context in case new instances of classes are created lock (hlslCloneContext) { HlslSemanticAnalysis.UpdateCloneContext(hlslCloneContext); } // only clone once the stage classes var mixCloneContext = new CloneContext(hlslCloneContext); foreach (var mixinInfo in mixinsToAnalyze) { foreach (var mixin in mixinInfo.Mixin.MinimalContext.Where(x => x.StageOnlyClass)) { mixin.DeepClone(mixCloneContext); } } // ---------------------------------------------------------- // Perform Shader Mixer // ---------------------------------------------------------- var externDict = new Dictionary <Variable, List <ModuleMixin> >(); var finalModuleList = BuildCompositionsDictionary(shaderMixinSource, externDict, context, mixCloneContext); //PerformanceLogger.Stop(PerformanceStage.DeepClone); var mixinDictionary = BuildMixinDictionary(finalModuleList); if (finalModuleList != null) { var finalModule = finalModuleList[0]; //PerformanceLogger.Start(PerformanceStage.Mix); var mixer = new ParadoxShaderMixer(finalModule, parsingResult, mixinDictionary, externDict, new CloneContext(mixCloneContext)); mixer.Mix(); //PerformanceLogger.Stop(PerformanceStage.Mix); // Return directly if there was any errors if (parsingResult.HasErrors) { return(parsingResult); } var finalShader = mixer.GetMixedShader(); parsingResult.Reflection = new EffectReflection(); var pdxShaderLinker = new ShaderLinker(parsingResult); pdxShaderLinker.Run(finalShader); // Return directly if there was any errors if (parsingResult.HasErrors) { return(parsingResult); } // Find all entry points // TODO: make this configurable by CompileParameters foreach (var stage in new[] { ShaderStage.Compute, ShaderStage.Vertex, ShaderStage.Hull, ShaderStage.Domain, ShaderStage.Geometry, ShaderStage.Pixel }) { var entryPoint = finalShader.Declarations.OfType <MethodDefinition>().FirstOrDefault(f => f.Attributes.OfType <AttributeDeclaration>().Any(a => a.Name == "EntryPoint" && (string)a.Parameters[0].Value == stage.ToString())); if (entryPoint == null) { continue; } parsingResult.EntryPoints[stage] = entryPoint.Name.Text; // When this is a compute shader, there is no need to scan other stages if (stage == ShaderStage.Compute) { break; } } var typeCleaner = new ParadoxShaderCleaner(); typeCleaner.Run(finalShader); //PerformanceLogger.Stop(PerformanceStage.Global); //PerformanceLogger.PrintLastResult(); //SemanticPerformance.PrintResult(); //MixPerformance.PrintResult(); //GenerateShaderPerformance.PrintResult(); //StreamCreatorPerformance.PrintResult(); //ShaderLoader.PrintTime(); //PerformanceLogger.WriteOut(52); parsingResult.Shader = finalShader; } return(parsingResult); }