Permalink
Browse files

Merge pull request #6108 from nkast/MGFXShaderInfo

[2MGFX] Split ShaderInfo
2 parents bd7ef5f + e93691f commit ed77ff8681a96c0a90f8344960c980b44e217a69 @tomspilman tomspilman committed Dec 14, 2017
@@ -201,6 +201,7 @@
<Compile Include="ShaderProfile.cs" />
<Compile Include="ShaderProfile.OpenGL.cs" />
<Compile Include="ShaderProfile.DirectX.cs" />
+ <Compile Include="ShaderResult.cs" />
<Compile Include="TechniqueInfo.cs" />
<Compile Include="TextureFilterType.cs" />
@@ -296,6 +296,10 @@
<Platforms>Windows</Platforms>
<Link>Processors\MGFX\ShaderProfile.DirectX.cs</Link>
</Compile>
+ <Compile Include="..\Tools\2MGFX\ShaderResult.cs">
+ <Platforms>Windows</Platforms>
+ <Link>Processors\MGFX\ShaderResult.cs</Link>
+ </Compile>
<Compile Include="..\Tools\2MGFX\TechniqueInfo.cs">
<Platforms>Windows</Platforms>
<Link>Processors\MGFX\TechniqueInfo.cs</Link>
@@ -63,15 +63,15 @@ public override CompiledEffectContent Process(EffectContent input, ContentProces
options.OutputFile = context.OutputFilename;
// Parse the MGFX file expanding includes, macros, and returning the techniques.
- ShaderInfo shaderInfo;
+ ShaderResult shaderResult;
try
{
- shaderInfo = ShaderInfo.FromFile(options.SourceFile, options,
+ shaderResult = ShaderResult.FromFile(options.SourceFile, options,
new ContentPipelineEffectCompilerOutput(context));
// Add the include dependencies so that if they change
// it will trigger a rebuild of this effect.
- foreach (var dep in shaderInfo.Dependencies)
+ foreach (var dep in shaderResult.Dependencies)
context.AddDependency(dep);
}
catch (InvalidContentException)
@@ -89,11 +89,11 @@ public override CompiledEffectContent Process(EffectContent input, ContentProces
var shaderErrorsAndWarnings = string.Empty;
try
{
- effect = EffectObject.CompileEffect(shaderInfo, out shaderErrorsAndWarnings);
+ effect = EffectObject.CompileEffect(shaderResult, out shaderErrorsAndWarnings);
// If there were any additional output files we register
// them so that the cleanup process can manage them.
- foreach (var outfile in shaderInfo.AdditionalOutputFiles)
+ foreach (var outfile in shaderResult.AdditionalOutputFiles)
context.AddOutputFile(outfile);
}
catch (ShaderCompilerException)
@@ -648,7 +648,7 @@ static internal VertexElementUsage ToXNAVertexElementUsage(MojoShader.MOJOSHADER
}
- static public EffectObject CompileEffect(ShaderInfo shaderInfo, out string errorsAndWarnings)
+ static public EffectObject CompileEffect(ShaderResult shaderResult, out string errorsAndWarnings)
{
var effect = new EffectObject();
errorsAndWarnings = string.Empty;
@@ -659,6 +659,7 @@ static public EffectObject CompileEffect(ShaderInfo shaderInfo, out string error
// Go thru the techniques and that will find all the
// shaders and constant buffers.
+ var shaderInfo = shaderResult.ShaderInfo;
effect.Techniques = new d3dx_technique[shaderInfo.Techniques.Count];
for (var t = 0; t < shaderInfo.Techniques.Count; t++)
{
@@ -683,18 +684,18 @@ static public EffectObject CompileEffect(ShaderInfo shaderInfo, out string error
pass.state_count = 0;
var tempstate = new d3dx_state[2];
- shaderInfo.Profile.ValidateShaderModels(pinfo);
+ shaderResult.Profile.ValidateShaderModels(pinfo);
if (!string.IsNullOrEmpty(pinfo.psFunction))
{
pass.state_count += 1;
- tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.psFunction, pinfo.psModel, false, ref errorsAndWarnings);
+ tempstate[pass.state_count - 1] = effect.CreateShader(shaderResult, pinfo.psFunction, pinfo.psModel, false, ref errorsAndWarnings);
}
if (!string.IsNullOrEmpty(pinfo.vsFunction))
{
pass.state_count += 1;
- tempstate[pass.state_count - 1] = effect.CreateShader(shaderInfo, pinfo.vsFunction, pinfo.vsModel, true, ref errorsAndWarnings);
+ tempstate[pass.state_count - 1] = effect.CreateShader(shaderResult, pinfo.vsFunction, pinfo.vsModel, true, ref errorsAndWarnings);
}
pass.states = new d3dx_state[pass.state_count];
@@ -792,10 +793,10 @@ static public EffectObject CompileEffect(ShaderInfo shaderInfo, out string error
}
- private d3dx_state CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, ref string errorsAndWarnings)
+ private d3dx_state CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, ref string errorsAndWarnings)
{
// Compile and create the shader.
- var shaderData = shaderInfo.Profile.CreateShader(shaderInfo, shaderFunction, shaderProfile, isVertexShader, this, ref errorsAndWarnings);
+ var shaderData = shaderResult.Profile.CreateShader(shaderResult, shaderFunction, shaderProfile, isVertexShader, this, ref errorsAndWarnings);
var state = new d3dx_state();
state.index = 0;
@@ -5,7 +5,7 @@ namespace TwoMGFX
{
partial class EffectObject
{
- public static byte[] CompileHLSL(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, ref string errorsAndWarnings)
+ public static byte[] CompileHLSL(ShaderResult shaderResult, string shaderFunction, string shaderProfile, ref string errorsAndWarnings)
{
SharpDX.D3DCompiler.ShaderBytecode shaderByteCode;
try
@@ -17,10 +17,10 @@ public static byte[] CompileHLSL(ShaderInfo shaderInfo, string shaderFunction, s
// in the first place.
//shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.NoPreshader;
- if (shaderInfo.Profile == ShaderProfile.DirectX_11)
+ if (shaderResult.Profile == ShaderProfile.DirectX_11)
shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility;
- if (shaderInfo.Debug)
+ if (shaderResult.Debug)
{
shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.SkipOptimization;
shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.Debug;
@@ -32,14 +32,14 @@ public static byte[] CompileHLSL(ShaderInfo shaderInfo, string shaderFunction, s
// Compile the shader into bytecode.
var result = SharpDX.D3DCompiler.ShaderBytecode.Compile(
- shaderInfo.FileContent,
+ shaderResult.FileContent,
shaderFunction,
shaderProfile,
shaderFlags,
0,
null,
null,
- shaderInfo.FilePath);
+ shaderResult.FilePath);
// Store all the errors and warnings to log out later.
errorsAndWarnings += result.Message;
View
@@ -37,10 +37,10 @@ public static int Main(string[] args)
// For now we assume we're going right to a compiled MGFXO file.
// Parse the MGFX file expanding includes, macros, and returning the techniques.
- ShaderInfo shaderInfo;
+ ShaderResult shaderResult;
try
{
- shaderInfo = ShaderInfo.FromFile(options.SourceFile, options, new ConsoleEffectCompilerOutput());
+ shaderResult = ShaderResult.FromFile(options.SourceFile, options, new ConsoleEffectCompilerOutput());
}
catch (Exception ex)
{
@@ -54,7 +54,7 @@ public static int Main(string[] args)
var shaderErrorsAndWarnings = string.Empty;
try
{
- effect = EffectObject.CompileEffect(shaderInfo, out shaderErrorsAndWarnings);
+ effect = EffectObject.CompileEffect(shaderResult, out shaderErrorsAndWarnings);
if (!string.IsNullOrEmpty(shaderErrorsAndWarnings))
Console.Error.WriteLine(shaderErrorsAndWarnings);
View
@@ -10,118 +10,8 @@ namespace TwoMGFX
{
public class ShaderInfo
{
- public string FilePath { get; private set; }
-
- public string FileContent { get; private set; }
-
- public ShaderProfile Profile { get; private set; }
-
- public string OutputFilePath { get; private set; }
-
- public bool Debug { get; private set; }
-
public List<TechniqueInfo> Techniques = new List<TechniqueInfo>();
public Dictionary<string, SamplerStateInfo> SamplerStates = new Dictionary<string, SamplerStateInfo>();
-
- public List<string> Dependencies { get; private set; }
-
- public List<string> AdditionalOutputFiles { get; private set; }
-
- static public ShaderInfo FromFile(string path, Options options, IEffectCompilerOutput output)
- {
- var effectSource = File.ReadAllText(path);
- return FromString(effectSource, path, options, output);
- }
-
- static public ShaderInfo FromString(string effectSource, string filePath, Options options, IEffectCompilerOutput output)
- {
- var macros = new Dictionary<string, string>();
- macros.Add("MGFX", "1");
-
- options.Profile.AddMacros(macros);
-
- // If we're building shaders for debug set that flag too.
- if (options.Debug)
- macros.Add("DEBUG", "1");
-
- if (!string.IsNullOrEmpty(options.Defines))
- {
- var defines = options.Defines.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var define in defines)
- {
- var name = define;
- var value = "1";
- if (define.Contains("="))
- {
- var parts = define.Split('=');
-
- if (parts.Length > 0)
- name = parts[0].Trim();
-
- if (parts.Length > 1)
- value = parts[1].Trim();
- }
-
- macros.Add(name, value);
- }
- }
-
- // Use the D3DCompiler to pre-process the file resolving
- // all #includes and macros.... this even works for GLSL.
- string newFile;
- var fullPath = Path.GetFullPath(filePath);
- var dependencies = new List<string>();
- newFile = Preprocessor.Preprocess(effectSource, fullPath, macros, dependencies, output);
-
- // Parse the resulting file for techniques and passes.
- var tree = new Parser(new Scanner()).Parse(newFile, fullPath);
- if (tree.Errors.Count > 0)
- {
- var errors = String.Empty;
- foreach (var error in tree.Errors)
- errors += string.Format("{0}({1},{2}) : {3}\r\n", error.File, error.Line, error.Column, error.Message);
-
- throw new Exception(errors);
- }
-
- // Evaluate the results of the parse tree.
- var result = tree.Eval() as ShaderInfo;
-
- // Remove the samplers and techniques so that the shader compiler
- // gets a clean file without any FX file syntax in it.
- var cleanFile = newFile;
- ParseTreeTools.WhitespaceNodes(TokenType.Technique_Declaration, tree.Nodes, ref cleanFile);
- ParseTreeTools.WhitespaceNodes(TokenType.Sampler_Declaration_States, tree.Nodes, ref cleanFile);
-
- // Setup the rest of the shader info.
- result.Dependencies = dependencies;
- result.FilePath = fullPath;
- result.FileContent = cleanFile;
- if (!string.IsNullOrEmpty(options.OutputFile))
- result.OutputFilePath = Path.GetFullPath(options.OutputFile);
- result.AdditionalOutputFiles = new List<string>();
-
- // Remove empty techniques.
- for (var i=0; i < result.Techniques.Count; i++)
- {
- var tech = result.Techniques[i];
- if (tech.Passes.Count <= 0)
- {
- result.Techniques.RemoveAt(i);
- i--;
- }
- }
-
- // We must have at least one technique.
- if (result.Techniques.Count <= 0)
- throw new Exception("The effect must contain at least one technique and pass!");
-
- result.Profile = options.Profile;
- result.Debug = options.Debug;
-
- return result;
- }
-
}
}
@@ -44,9 +44,9 @@ internal override void ValidateShaderModels(PassInfo pass)
}
}
- internal override ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings)
+ internal override ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings)
{
- var bytecode = EffectObject.CompileHLSL(shaderInfo, shaderFunction, shaderProfile, ref errorsAndWarnings);
+ var bytecode = EffectObject.CompileHLSL(shaderResult, shaderFunction, shaderProfile, ref errorsAndWarnings);
// First look to see if we already created this same shader.
foreach (var shader in effect.Shaders)
@@ -55,7 +55,8 @@ internal override ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFu
return shader;
}
- var shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug);
+ var shaderInfo = shaderResult.ShaderInfo;
+ var shaderData = ShaderData.CreateHLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderResult.Debug);
effect.Shaders.Add(shaderData);
return shaderData;
}
@@ -44,11 +44,11 @@ internal override void ValidateShaderModels(PassInfo pass)
}
}
- internal override ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings)
+ internal override ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings)
{
// For now GLSL is only supported via translation
// using MojoShader which works from HLSL bytecode.
- var bytecode = EffectObject.CompileHLSL(shaderInfo, shaderFunction, shaderProfile, ref errorsAndWarnings);
+ var bytecode = EffectObject.CompileHLSL(shaderResult, shaderFunction, shaderProfile, ref errorsAndWarnings);
// First look to see if we already created this same shader.
foreach (var shader in effect.Shaders)
@@ -57,7 +57,8 @@ internal override ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFu
return shader;
}
- var shaderData = ShaderData.CreateGLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderInfo.Debug);
+ var shaderInfo = shaderResult.ShaderInfo;
+ var shaderData = ShaderData.CreateGLSL(bytecode, isVertexShader, effect.ConstantBuffers, effect.Shaders.Count, shaderInfo.SamplerStates, shaderResult.Debug);
effect.Shaders.Add(shaderData);
return shaderData;
@@ -66,7 +66,7 @@ public static ShaderProfile FromName(string name)
internal abstract void ValidateShaderModels(PassInfo pass);
- internal abstract ShaderData CreateShader(ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings);
+ internal abstract ShaderData CreateShader(ShaderResult shaderResult, string shaderFunction, string shaderProfile, bool isVertexShader, EffectObject effect, ref string errorsAndWarnings);
internal abstract bool Supports(string platform);
Oops, something went wrong.

0 comments on commit ed77ff8

Please sign in to comment.