private ShaderMixinSource GetShaderMixinSource(ShaderSource shaderSource, CompilerParameters compilerParameters) { ShaderMixinSource mixinToCompile; var shaderMixinGeneratorSource = shaderSource as ShaderMixinGeneratorSource; if (shaderMixinGeneratorSource != null) { mixinToCompile = ShaderMixinManager.Generate(shaderMixinGeneratorSource.Name, compilerParameters); } else { mixinToCompile = shaderSource as ShaderMixinSource; var shaderClassSource = shaderSource as ShaderClassSource; if (shaderClassSource != null) { mixinToCompile = new ShaderMixinSource { Name = shaderClassSource.ClassName }; mixinToCompile.Mixins.Add(shaderClassSource); } if (mixinToCompile == null) { throw new ArgumentException("Unsupported ShaderSource type [{0}]. Supporting only ShaderMixinSource/xkfx, ShaderClassSource", "shaderSource"); } if (string.IsNullOrEmpty(mixinToCompile.Name)) { throw new ArgumentException("ShaderMixinSource must have a name", "shaderSource"); } } return(mixinToCompile); }
public CompilerResults Compile(ShaderSource shaderSource, CompilerParameters compilerParameters) { ShaderMixinSource mixinToCompile; var shaderMixinGeneratorSource = shaderSource as ShaderMixinGeneratorSource; if (shaderMixinGeneratorSource != null) { mixinToCompile = ShaderMixinManager.Generate(shaderMixinGeneratorSource.Name, compilerParameters); } else { mixinToCompile = shaderSource as ShaderMixinSource; var shaderClassSource = shaderSource as ShaderClassSource; if (shaderClassSource != null) { mixinToCompile = new ShaderMixinSource { Name = shaderClassSource.ClassName }; mixinToCompile.Mixins.Add(shaderClassSource); } if (mixinToCompile == null) { throw new ArgumentException("Unsupported ShaderSource type [{0}]. Supporting only ShaderMixinSource/xkfx, ShaderClassSource", "shaderSource"); } if (string.IsNullOrEmpty(mixinToCompile.Name)) { throw new ArgumentException("ShaderMixinSource must have a name", "shaderSource"); } } // Copy global parameters to used Parameters by default, as it is used by the compiler mixinToCompile.UsedParameters.Set(CompilerParameters.GraphicsPlatformKey, compilerParameters.Platform); mixinToCompile.UsedParameters.Set(CompilerParameters.GraphicsProfileKey, compilerParameters.Profile); mixinToCompile.UsedParameters.Set(CompilerParameters.DebugKey, compilerParameters.Debug); // Compile the whole mixin tree var compilerResults = new CompilerResults { Module = string.Format("EffectCompile [{0}]", mixinToCompile.Name) }; var bytecode = Compile(mixinToCompile, compilerParameters); // Since bytecode.Result is a struct, we check if any of its member has been set to know if it's valid if (bytecode.Result.CompilationLog != null || bytecode.Task != null) { if (bytecode.Result.CompilationLog != null) { bytecode.Result.CompilationLog.CopyTo(compilerResults); } compilerResults.Bytecode = bytecode; compilerResults.UsedParameters = mixinToCompile.UsedParameters; } return(compilerResults); }
public CompilerResults Compile(ShaderSource shaderSource, CompilerParameters compilerParameters) { ShaderMixinSourceTree mixinTree; var shaderMixinGeneratorSource = shaderSource as ShaderMixinGeneratorSource; string mainEffectName = null; if (shaderMixinGeneratorSource != null) { string subEffect; mainEffectName = GetEffectName(shaderMixinGeneratorSource.Name, out subEffect); mixinTree = ShaderMixinManager.Generate(mainEffectName, compilerParameters); } else { mainEffectName = "Effect"; var shaderMixinSource = shaderSource as ShaderMixinSource; var shaderClassSource = shaderSource as ShaderClassSource; if (shaderClassSource != null) { shaderMixinSource = new ShaderMixinSource(); shaderMixinSource.Mixins.Add(shaderClassSource); mainEffectName = shaderClassSource.ClassName; } if (shaderMixinSource != null) { mixinTree = new ShaderMixinSourceTree() { Name = mainEffectName, Mixin = shaderMixinSource, UsedParameters = new ShaderMixinParameters() }; } else { throw new ArgumentException("Unsupported ShaderSource type [{0}]. Supporting only ShaderMixinSource/pdxfx, ShaderClassSource", "shaderSource"); } } // Copy global parameters to used Parameters by default, as it is used by the compiler mixinTree.SetGlobalUsedParameter(CompilerParameters.GraphicsPlatformKey, compilerParameters.Platform); mixinTree.SetGlobalUsedParameter(CompilerParameters.GraphicsProfileKey, compilerParameters.Profile); mixinTree.SetGlobalUsedParameter(CompilerParameters.DebugKey, compilerParameters.Debug); // Compile the whole mixin tree var compilerResults = new CompilerResults { Module = string.Format("EffectCompile [{0}]", mainEffectName) }; Compile(string.Empty, mixinTree, compilerParameters, compilerResults); return(compilerResults); }
/// <summary> /// Generates the mixin. /// </summary> /// <param name="mixinName">Name of the mixin.</param> /// <param name="properties">The properties that the mixin will use.</param> /// <returns>ShaderMixinSource.</returns> private static ShaderMixinSource GenerateMixin(string mixinName, ParameterCollection properties) { var mixin = ShaderMixinManager.Generate(mixinName, properties); // Verify that output used properties are a subset of input properties //Assert.That(usedProperties.IsSubsetOf(properties), Is.True); //foreach(var usedProps in allUsedProperties) // Assert.That(usedProps.IsSubsetOf(properties), Is.True); return(mixin); }
/// <summary> /// Generates the mixin. /// </summary> /// <param name="mixinName">Name of the mixin.</param> /// <param name="properties">The properties that the mixin will use.</param> /// <param name="usedProperties">The properties effectively used by the mixin.</param> /// <returns>ShaderMixinSource.</returns> private static ShaderMixinSourceTree GenerateMixin(string mixinName, ParameterCollection properties, out ShaderMixinParameters usedProperties) { var allUsedProperties = new List <ShaderMixinParameters>(); var mixin = ShaderMixinManager.Generate(mixinName, properties, out usedProperties, out allUsedProperties); // Verify that output used properties are a subset of input properties Assert.That(usedProperties.IsSubsetOf(properties), Is.True); foreach (var usedProps in allUsedProperties) { Assert.That(usedProps.IsSubsetOf(properties), Is.True); } return(mixin); }
public CompilerResults Compile(ShaderSource shaderSource, CompilerParameters compilerParameters, HashSet <string> modifiedShaders, HashSet <string> recentlyModifiedShaders) { ShaderMixinSourceTree mixinTree; var shaderMixinGeneratorSource = shaderSource as ShaderMixinGeneratorSource; var mainUsedParameters = new ShaderMixinParameters(); var usedParameters = new List <ShaderMixinParameters>(); string effectName = null; if (shaderMixinGeneratorSource != null) { effectName = shaderMixinGeneratorSource.Name; // getting the effect from the used parameters only makes sense when the source files are the same // TODO: improve this by updating earlyCompilerCache - cache can still be relevant if (modifiedShaders == null || modifiedShaders.Count == 0) { // perform an early test only based on the parameters var foundCompilerResults = GetShaderFromParameters(effectName, compilerParameters); if (foundCompilerResults != null) { var earlyCompilerResults = new CompilerResults(); earlyCompilerResults.Module = string.Format("EffectCompile [{0}]", effectName); earlyCompilerResults.MainBytecode = foundCompilerResults.MainBytecode; earlyCompilerResults.MainUsedParameters = foundCompilerResults.MainUsedParameters; foreach (var foundBytecode in foundCompilerResults.Bytecodes) { earlyCompilerResults.Bytecodes.Add(foundBytecode.Key, foundBytecode.Value); } foreach (var foundUsedParameters in foundCompilerResults.UsedParameters) { earlyCompilerResults.UsedParameters.Add(foundUsedParameters.Key, foundUsedParameters.Value); } return(earlyCompilerResults); } } mixinTree = ShaderMixinManager.Generate(effectName, compilerParameters, out mainUsedParameters, out usedParameters); } else { effectName = "Effect"; var shaderMixinSource = shaderSource as ShaderMixinSource; var shaderClassSource = shaderSource as ShaderClassSource; if (shaderClassSource != null) { shaderMixinSource = new ShaderMixinSource(); shaderMixinSource.Mixins.Add(shaderClassSource); } if (shaderMixinSource != null) { mixinTree = new ShaderMixinSourceTree() { Mixin = shaderMixinSource }; } else { throw new ArgumentException("Unsupported ShaderSource type [{0}]. Supporting only ShaderMixinSource/pdxfx, ShaderClassSource", "shaderSource"); } } // Copy global parameters to used Parameters by default, as it is used by the compiler mainUsedParameters.Set(CompilerParameters.GraphicsPlatformKey, compilerParameters.Platform); mainUsedParameters.Set(CompilerParameters.GraphicsProfileKey, compilerParameters.Profile); mainUsedParameters.Set(CompilerParameters.DebugKey, compilerParameters.Debug); foreach (var parameters in usedParameters) { parameters.Set(CompilerParameters.GraphicsPlatformKey, compilerParameters.Platform); parameters.Set(CompilerParameters.GraphicsProfileKey, compilerParameters.Profile); parameters.Set(CompilerParameters.DebugKey, compilerParameters.Debug); } // Compile the whole mixin tree var compilerResults = new CompilerResults(); compilerResults.Module = string.Format("EffectCompile [{0}]", effectName); var wasCompiled = Compile(string.Empty, effectName, mixinTree, mainUsedParameters, usedParameters, modifiedShaders, recentlyModifiedShaders, compilerResults); if (wasCompiled && shaderMixinGeneratorSource != null) { lock (earlyCompilerCache) { List <CompilerResults> effectCompilerResults; if (!earlyCompilerCache.TryGetValue(effectName, out effectCompilerResults)) { effectCompilerResults = new List <CompilerResults>(); earlyCompilerCache.Add(effectName, effectCompilerResults); } // Register bytecode used parameters so that they are checked when another effect is instanced effectCompilerResults.Add(compilerResults); } } return(compilerResults); }