4 using System.Collections.Generic;
8 using System.Threading.Tasks;
9 using SiliconStudio.Assets.Compiler;
10 using SiliconStudio.BuildEngine;
11 using SiliconStudio.Core;
12 using SiliconStudio.Core.Diagnostics;
13 using SiliconStudio.Core.IO;
14 using SiliconStudio.Core.Serialization;
15 using SiliconStudio.Core.Serialization.Assets;
16 using SiliconStudio.Core.Storage;
17 using SiliconStudio.Paradox.Effects;
18 using SiliconStudio.Paradox.Shaders;
19 using SiliconStudio.Paradox.Shaders.Compiler;
21 namespace SiliconStudio.
Paradox.Assets.Effect
35 var compiler = context.Properties.Get(CompilerKey);
39 var effectCompiler =
new Shaders.Compiler.EffectCompiler();
40 effectCompiler.SourceDirectories.Add(EffectSystem.DefaultSourceShaderFolder);
42 context.Properties.Set(CompilerKey, compiler);
44 var shaderLocations = context.Properties.Get(EffectShaderAssetCompiler.ShaderLocationsKey);
48 if (shaderLocations != null)
50 foreach (var shaderLocation
in shaderLocations)
52 effectCompiler.UrlToFilePath[shaderLocation.Key] = shaderLocation.Value;
65 private string effectName;
67 private static Dictionary<string, int> PermutationCount =
new Dictionary<string, int>();
71 this.context = context;
72 this.effectName = effectName;
73 this.baseUrl = baseUrl;
74 this.compilerParameters = compilerParameters;
77 public override string Title
81 return string.Format(
"EffectCompile [{0}]", effectName);
87 base.ComputeParameterHash(writer);
88 writer.Serialize(ref effectName, ArchiveMode.Serialize);
89 writer.Serialize(ref compilerParameters, ArchiveMode.Serialize);
94 writer.Write(DataSerializer.BinaryFormatVersion);
102 var compiler = GetOrCreateEffectCompiler(context);
104 var isPdxfx = ShaderMixinManager.Contains(effectName);
107 int permutationCount;
108 lock (PermutationCount)
110 PermutationCount.TryGetValue(effectName, out permutationCount);
112 PermutationCount[effectName] = permutationCount;
114 commandContext.Logger.Info(
"Create permutation #{0} for effect [{1}]: \n{2}", permutationCount, effectName, compilerParameters.ToStringDetailed());
116 var compilerResults = compiler.Compile(source, compilerParameters, null, null);
119 compilerResults.CopyTo(commandContext.Logger);
120 if (compilerResults.HasErrors)
122 return Task.FromResult(ResultStatus.Failed);
126 var bytecode = compilerResults.MainBytecode;
127 foreach (var hashSource
in bytecode.HashSources)
129 commandContext.Logger.Verbose(
"Shader [{0}] is using [{1}]", effectName, hashSource.Key);
130 commandContext.RegisterInputDependency(
new ObjectUrl(
UrlType.Internal, hashSource.Key));
136 var outputDirectory = UPath.Combine(context.Package.RootDirectory, baseUrl);
137 var outputClassFile = effectName +
".bytecode." + compilerParameters.Platform +
"." + compilerParameters.Profile +
".cs";
138 var fullOutputClassFile = Path.Combine(outputDirectory, outputClassFile);
140 commandContext.Logger.Info(
"Writing shader bytecode to .cs source [{0}]", fullOutputClassFile);
141 using (var stream =
new FileStream(fullOutputClassFile,
FileMode.Create, FileAccess.Write, FileShare.Write))
142 EffectByteCodeToSourceCodeWriter.Write(effectName, compilerParameters, bytecode,
new StreamWriter(stream,
Encoding.UTF8));
145 return Task.FromResult(ResultStatus.Successful);
148 public override string ToString()
A shader source that is linked to a pdxfx effect.
Checks if an effect has already been compiled in its cache before deferring to a real IEffectCompiler...
Base class for implementations of IEffectCompiler, providing some helper functions.
System.Text.Encoding Encoding
System.IO.FileMode FileMode
Parameters used for compilation.
Implements SerializationStream as a binary writer.
The context used when compiling an asset in a Package.
Defines a normalized directory path. See UPath for details. This class cannot be inherited.
A Command that reads and/or writes to the index file.
A class that represents a tag propety.
Keys used for sourcecode generation.
static readonly ParameterKey< bool > Enable
When compiling a pdxsl, this will generate a source code file
A shader class used for mixin.