Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EffectCompileGeneratorBuildStep.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Collections.Generic;
4 using System.Threading.Tasks;
5 using SiliconStudio.Assets.Compiler;
6 using SiliconStudio.BuildEngine;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Diagnostics;
9 using SiliconStudio.Core.IO;
10 using SiliconStudio.Paradox.Assets.Effect.ValueGenerators;
11 using SiliconStudio.Paradox.Effects;
12 using SiliconStudio.Paradox.Shaders.Compiler;
13 
14 namespace SiliconStudio.Paradox.Assets.Effect
15 {
16  /// <summary>
17  /// This build steps is responsible to generate all compiler commmands for all effect permutations.
18  /// </summary>
19  internal class EffectCompileGeneratorBuildStep : PermutationGeneratorBuildStep
20  {
21  private readonly string url;
22 
23  protected readonly EffectLibraryAsset Asset;
24 
25  /// <summary>
26  /// Initializes a new instance of the <see cref="EffectCompileGeneratorBuildStep" /> class.
27  /// </summary>
28  /// <param name="context">The context.</param>
29  /// <param name="url">The URL.</param>
30  /// <param name="compilerParametersGenerators">The compiler parameters generators.</param>
31  /// <param name="asset">The asset.</param>
32  public EffectCompileGeneratorBuildStep(AssetCompilerContext context, string url, List<ICompilerParametersGenerator> compilerParametersGenerators, EffectLibraryAsset asset)
33  : base(context, compilerParametersGenerators)
34  {
35  this.url = url;
36  this.Asset = asset;
37  }
38 
39  public override string Title
40  {
41  get
42  {
43  return "EffectCompileGenerator";
44  }
45  }
46 
47  public override Task<ResultStatus> Execute(IExecuteContext executeContext, BuilderContext builderContext)
48  {
49  var steps = new List<BuildStep>();
50  Steps = steps;
51 
52  // Pre-generate CompilerParameters for all meshes
53  var log = new LoggerResult("EffectLibrary [{0}]".ToFormat(url));
54 
55  var urlRoot = new UFile(url).GetParent();
56  var rootParameters = new CompilerParameters
57  {
58  Platform = Context.GetGraphicsPlatform(),
59  Profile = Context.GetGraphicsProfile()
60  };
61 
62  var keys = Asset.Permutations == null ? null : Asset.Permutations.Keys;
63  var children = Asset.Permutations == null ? null : Asset.Permutations.Children;
64 
65  foreach (var parametersPerPermutation in GeneratePermutation(rootParameters, keys, children, log))
66  {
67  var parameters = parametersPerPermutation.Clone();
68 
69  if (!parameters.ContainsKey(EffectKeys.Name))
70  {
71  log.Warning("Permutation not compiled. It doesn't contain [Effect.Name] key to select the correct pdxfx/pdxsl to compile with parameters [{0}]", parametersPerPermutation.ToStringDetailed());
72  }
73  else
74  {
75  var effectName = parameters.Get(EffectKeys.Name);
76  steps.Add(new CommandBuildStep(new EffectCompileCommand(Context, urlRoot, effectName, parameters)));
77  }
78  }
79 
80  // Copy all logs
81  log.CopyTo(executeContext.Logger);
82 
83  return base.Execute(executeContext, builderContext);
84  }
85 
86  private IEnumerable<CompilerParameters> GeneratePermutation(CompilerParameters parameters, EffectParameterKeyStandardGenerator keys, List<EffectPermutation> permutations, ILogger log)
87  {
88  if (keys == null || keys.Count == 0)
89  {
90  if (permutations == null || permutations.Count == 0)
91  {
92  foreach (var newParameters in GenerateCompilerParametersPermutation(parameters, 0, log))
93  {
94  yield return newParameters;
95  }
96  }
97  else
98  {
99  foreach (var permutation in permutations)
100  {
101  foreach (var parametersPerPermutations in GeneratePermutation(parameters, permutation.Keys, permutation.Children, log))
102  {
103  yield return parametersPerPermutations;
104  }
105  }
106  }
107  }
108  else
109  {
110  foreach (var parametersPerKeyValuePermutations in GenerateKeysPermutation(parameters, keys.GenerateKeyValues()))
111  {
112  foreach (var subParameters in GeneratePermutation(parametersPerKeyValuePermutations, null, permutations, log))
113  {
114  yield return subParameters;
115  }
116  }
117  }
118  }
119 
120  private IEnumerable<CompilerParameters> GenerateCompilerParametersPermutation(CompilerParameters parameters, int compilerGeneratorIndex, ILogger log)
121  {
122  if (compilerGeneratorIndex >= CompilerParametersGenerators.Count)
123  {
124  // Clone for each version of CompilerParameters
125  yield return parameters;
126  }
127  else
128  {
129  var generator = CompilerParametersGenerators[compilerGeneratorIndex];
130  foreach (var nextParameters in generator.Generate(Context, parameters, log))
131  {
132  foreach (var nextSubParameters in GenerateCompilerParametersPermutation(nextParameters, compilerGeneratorIndex + 1, log))
133  {
134  yield return nextSubParameters;
135  }
136  }
137  }
138  }
139  }
140 }
Keys
Enumeration for keys.
Definition: Keys.cs:8
SiliconStudio.Core.Diagnostics.LoggerResult LoggerResult
Default implementation for a IEffectParameterGenerator using a dictionary of ParameterKey associated ...
The context used when compiling an asset in a Package.
IEnumerable< ParameterKey > Keys
Gets the keys of this collection.
Platform specific queries and functions.
Definition: Platform.cs:15
Interface for logging.
Definition: ILogger.cs:8
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13