public SmokeParticleEmitterComponent() { Type = ParticleEmitterType.GpuStatic; Shader = new ShaderClassSource("ParticleUpdaterSmoke"); UpdateData += UpdateParticlesData; ParticleElementSize = Utilities.SizeOf<ScriptParticleSmoke.ParticleData>(); Parameters.Set(ScriptParticleSmoke.SmokeEmitterKey, new SmokeEmitterDescription()); }
public BulletParticleEmitterComponent() { Type = ParticleEmitterType.GpuStatic; Shader = new ShaderClassSource("ParticleUpdaterBullet"); MeshUpdate += OnMeshUpdate; UpdateData += UpdateParticlesData; UpdateSystem += OnUpdateSystem; ParticleElementSize = Utilities.SizeOf<ScriptParticleSmoke.ParticleData>(); Description = new BulletEmitterDescription(); }
public void TestRenaming() { var color1Mixin = new ShaderClassSource("ComputeColorFixed", "Material.DiffuseColorValue"); var color2Mixin = new ShaderClassSource("ComputeColorFixed", "Material.SpecularColorValue"); var compMixin = new ShaderMixinSource(); compMixin.Mixins.Add(new ShaderClassSource("ComputeColorMultiply")); compMixin.AddComposition("color1", color1Mixin); compMixin.AddComposition("color2", color2Mixin); var mixinSource = new ShaderMixinSource { Name = "testRenaming", UsedParameters = MixinParameters }; mixinSource.Mixins.Add(new ShaderClassSource("ShadingBase")); mixinSource.Mixins.Add(new ShaderClassSource("AlbedoFlatShading")); mixinSource.AddComposition("albedoDiffuse", compMixin); var byteCode = Compiler.Compile(mixinSource, new CompilerParameters()); Assert.IsNotNull(byteCode); }
public void TestMaterial() { var compiler = new EffectCompiler { UseFileSystem = true }; compiler.SourceDirectories.Add(@"..\..\sources\engine\SiliconStudio.Paradox.Graphics\Shaders"); compiler.SourceDirectories.Add(@"..\..\sources\engine\SiliconStudio.Paradox.Engine\Shaders"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Core"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Lights"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Materials"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Shadows"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\ComputeColor"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Skinning"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Shading"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Transformation"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Utils"); var compilerParameters = new CompilerParameters { Platform = GraphicsPlatform.Direct3D11 }; var layers = new MaterialBlendLayers(); layers.Add(new MaterialBlendLayer { BlendMap = new ComputeFloat(0.5f), Material = AttachedReferenceManager.CreateSerializableVersion<Material>(Guid.Empty, "fake") }); var materialAsset = new MaterialAsset { Attributes = new MaterialAttributes() { Diffuse = new MaterialDiffuseMapFeature() { DiffuseMap = new ComputeColor(Color4.White) }, DiffuseModel = new MaterialDiffuseLambertModelFeature() }, Layers = layers }; var fakeAsset = new MaterialAsset { Attributes = new MaterialAttributes() { Diffuse = new MaterialDiffuseMapFeature() { DiffuseMap = new ComputeColor(Color.Blue) }, } }; var context = new MaterialGeneratorContext { FindAsset = reference => fakeAsset }; var result = MaterialGenerator.Generate(new MaterialDescriptor { Attributes = materialAsset.Attributes, Layers = materialAsset.Layers }, context, "TestMaterial"); compilerParameters.Set(MaterialKeys.PixelStageSurfaceShaders, result.Material.Parameters.Get(MaterialKeys.PixelStageSurfaceShaders)); var directionalLightGroup = new ShaderClassSource("LightDirectionalGroup", 1); compilerParameters.Set(LightingKeys.DirectLightGroups, new List<ShaderSource> { directionalLightGroup }); //compilerParameters.Set(LightingKeys.CastShadows, false); //compilerParameters.Set(MaterialParameters.HasSkinningPosition, true); //compilerParameters.Set(MaterialParameters.HasSkinningNormal, true); compilerParameters.Set(MaterialKeys.HasNormalMap, true); var results = compiler.Compile(new ShaderMixinGeneratorSource("ParadoxEffectBase"), compilerParameters); Assert.IsFalse(results.HasErrors); }
public override ShaderSource GenerateShaderSource(ShaderGeneratorContext context, MaterialComputeColorKeys baseKeys) { if (!Enabled || Texture == null) // generate shader from default value when the texture is null or disabled { return(GenerateShaderFromFallbackValue(context, baseKeys)); } // generate shader from the texture // TODO: Use a generated UsedTexcoordIndex when backing textures var usedTexcoord = "TEXCOORD" + MaterialUtility.GetTextureIndex(TexcoordIndex); var textureKey = context.GetTextureKey(this, baseKeys); var samplerKey = context.GetSamplerKey(Sampler); UsedKey = textureKey; var scale = Scale; var scaleFactor = context.CurrentOverrides.UVScale; if (scaleFactor != Vector2.One) { scale *= scaleFactor; } var channelStr = GetTextureChannelAsString(); // "TTEXTURE", "TStream" ShaderClassSource shaderSource; // TODO: Workaround bad to have to copy all the new ShaderClassSource(). Check how to improve this if (context.OptimizeMaterials) { var scaleStr = MaterialUtility.GetAsShaderString(scale); var offsetStr = MaterialUtility.GetAsShaderString(Offset); // If materials are optimized, we precompute best shader combination (note: will generate shader permutations!) if (context.IsNotPixelStage) { if (Offset != Vector2.Zero) { shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledOffsetSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, offsetStr, 0.0f); } else if (scale != Vector2.One) { shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, 0.0f); } else { shaderSource = new ShaderClassSource("ComputeColorTextureLodSampler", textureKey, usedTexcoord, samplerKey, channelStr, 0.0f); } } else { if (Offset != Vector2.Zero) { shaderSource = new ShaderClassSource("ComputeColorTextureScaledOffsetSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, offsetStr); } else if (scale != Vector2.One) { shaderSource = new ShaderClassSource("ComputeColorTextureScaledSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr); } else { shaderSource = new ShaderClassSource("ComputeColorTextureSampler", textureKey, usedTexcoord, samplerKey, channelStr); } } } else { // Try to avoid shader permutations, by putting UV scaling/offset in shader parameters var textureScale = (ParameterKey <Vector2>)context.GetParameterKey(MaterialKeys.TextureScale); var textureOffset = (ParameterKey <Vector2>)context.GetParameterKey(MaterialKeys.TextureOffset); context.Parameters.Set(textureScale, scale); context.Parameters.Set(textureOffset, Offset); if (context.IsNotPixelStage) { shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledOffsetDynamicSampler", textureKey, usedTexcoord, samplerKey, channelStr, textureScale, textureOffset, 0.0f); } else { shaderSource = new ShaderClassSource("ComputeColorTextureScaledOffsetDynamicSampler", textureKey, usedTexcoord, samplerKey, channelStr, textureScale, textureOffset); } } return(shaderSource); }
public override ShaderSource GenerateShaderSource(MaterialGeneratorContext context, MaterialComputeColorKeys baseKeys) { if (!Enabled || Texture == null) // generate shader from default value when the texture is null or disabled return GenerateShaderFromFallbackValue(context, baseKeys); // generate shader from the texture // TODO: Use a generated UsedTexcoordIndex when backing textures var usedTexcoord = "TEXCOORD" + MaterialUtility.GetTextureIndex(TexcoordIndex); var textureKey = context.GetTextureKey(this, baseKeys); var samplerKey = context.GetSamplerKey(Sampler); UsedKey = textureKey; var scale = Scale; var scaleFactor = context.CurrentOverrides.UVScale; if (scaleFactor != Vector2.One) { scale *= scaleFactor; } var channelStr = GetTextureChannelAsString(); // "TTEXTURE", "TStream" ShaderClassSource shaderSource; // TODO: Workaround bad to have to copy all the new ShaderClassSource(). Check how to improve this if (context.OptimizeMaterials) { var scaleStr = MaterialUtility.GetAsShaderString(scale); var offsetStr = MaterialUtility.GetAsShaderString(Offset); // If materials are optimized, we precompute best shader combination (note: will generate shader permutations!) if (context.IsNotPixelStage) { if (Offset != Vector2.Zero) shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledOffsetSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, offsetStr, 0.0f); else if (scale != Vector2.One) shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, 0.0f); else shaderSource = new ShaderClassSource("ComputeColorTextureLodSampler", textureKey, usedTexcoord, samplerKey, channelStr, 0.0f); } else { if (Offset != Vector2.Zero) shaderSource = new ShaderClassSource("ComputeColorTextureScaledOffsetSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr, offsetStr); else if (scale != Vector2.One) shaderSource = new ShaderClassSource("ComputeColorTextureScaledSampler", textureKey, usedTexcoord, samplerKey, channelStr, scaleStr); else shaderSource = new ShaderClassSource("ComputeColorTextureSampler", textureKey, usedTexcoord, samplerKey, channelStr); } } else { // Try to avoid shader permutations, by putting UV scaling/offset in shader parameters var textureScale = (ParameterKey<Vector2>)context.GetParameterKey(MaterialKeys.TextureScale); var textureOffset = (ParameterKey<Vector2>)context.GetParameterKey(MaterialKeys.TextureOffset); context.Parameters.Set(textureScale, scale); context.Parameters.Set(textureOffset, Offset); if (context.IsNotPixelStage) { shaderSource = new ShaderClassSource("ComputeColorTextureLodScaledOffsetDynamicSampler", textureKey, usedTexcoord, samplerKey, channelStr, textureScale, textureOffset, 0.0f); } else { shaderSource = new ShaderClassSource("ComputeColorTextureScaledOffsetDynamicSampler", textureKey, usedTexcoord, samplerKey, channelStr, textureScale, textureOffset); } } return shaderSource; }
/// <summary> /// Loads generic classes that may appear in the mixin /// </summary> /// <param name="mixinInfo">The mixin to investigate</param> /// <param name="macros">The macros.</param> /// <param name="macrosString">The macros string.</param> private void LoadNecessaryShaders(ModuleMixinInfo mixinInfo, SiliconStudio.Shaders.Parser.ShaderMacro[] macros, string macrosString) { if (!mixinInfo.Instanciated) return; // Look for all the generic calls var shaderDependencyVisitor = new ShaderDependencyVisitor(mixinInfo.Log, ShaderLoader.SourceManager); shaderDependencyVisitor.Run(mixinInfo.MixinAst); foreach (var foundClass in shaderDependencyVisitor.FoundClasses) { var classSource = new ShaderClassSource(foundClass, null); var foundMixinInfo = GetModuleMixinInfo(classSource, macros, macrosString); mixinInfo.MinimalContext.UnionWith(foundMixinInfo.MinimalContext); } foreach (var id in shaderDependencyVisitor.FoundIdentifiers) { var genericClass = id.Item1; ModuleMixinInfo.CleanIdentifiers(genericClass.Identifiers); var genericParams = BuildShaderGenericParameters(genericClass); var classSource = new ShaderClassSource(genericClass.Text, genericParams); var instanciatedClassInfo = GetModuleMixinInfo(classSource, macros, macrosString); mixinInfo.MinimalContext.UnionWith(instanciatedClassInfo.MinimalContext); var newId = new Identifier(instanciatedClassInfo.MixinName); if (id.Item2 is TypeName) // in the baseclass list or in a variable declaration (id.Item2 as TypeName).Name = newId; else if (id.Item2 is VariableReferenceExpression) (id.Item2 as VariableReferenceExpression).Name = newId; else if (id.Item2 is MemberReferenceExpression) (id.Item2 as MemberReferenceExpression).Member = newId; } }
public override void VisitFeature(MaterialGeneratorContext context) { var shaderSource = new ShaderClassSource("MaterialSurfaceShadingDiffuseLambert", IsEnergyConservative); context.AddShading(this, shaderSource); }
public override void VisitFeature(MaterialGeneratorContext context) { var shaderSource = new ShaderClassSource("MaterialSurfaceShadingDiffuseLambert", IsEnergyConservative); context.AddShading(this, shaderSource); }
/// <summary> /// Initializes a new instance of class <see cref="LightShaftsPlugin"/>. /// </summary> public MinMaxShaderPlugin(string name) : base(name) { MinMaxShader = new ShaderClassSource("MinMaxBounding"); }
public override ShaderSource GenerateShaderSource(MaterialGeneratorContext context, MaterialComputeColorKeys baseKeys) { if (string.IsNullOrEmpty(MixinReference)) { return(new ShaderClassSource("ComputeColor")); } var mixinName = MixinReference; object[] generics = null; if (Generics.Count > 0) { // TODO: correct generic order var mixinGenerics = new List <object>(); foreach (var genericKey in Generics.Keys) { var generic = Generics[genericKey]; if (generic is ComputeColorParameterTexture) { var textureParameter = ((ComputeColorParameterTexture)generic); var textureKey = context.GetTextureKey(textureParameter.Texture, baseKeys); mixinGenerics.Add(textureKey.ToString()); } else if (generic is ComputeColorParameterSampler) { var pk = context.GetSamplerKey((ComputeColorParameterSampler)generic); mixinGenerics.Add(pk.ToString()); } else if (generic is ComputeColorParameterFloat) { mixinGenerics.Add(((ComputeColorParameterFloat)generic).Value.ToString(CultureInfo.InvariantCulture)); } else if (generic is ComputeColorParameterInt) { mixinGenerics.Add(((ComputeColorParameterInt)generic).Value.ToString(CultureInfo.InvariantCulture)); } else if (generic is ComputeColorParameterFloat2) { mixinGenerics.Add(MaterialUtility.GetAsShaderString(((ComputeColorParameterFloat2)generic).Value)); } else if (generic is ComputeColorParameterFloat3) { mixinGenerics.Add(MaterialUtility.GetAsShaderString(((ComputeColorParameterFloat3)generic).Value)); } else if (generic is ComputeColorParameterFloat4) { mixinGenerics.Add(MaterialUtility.GetAsShaderString(((ComputeColorParameterFloat4)generic).Value)); } else if (generic is ComputeColorStringParameter) { mixinGenerics.Add(((ComputeColorStringParameter)generic).Value); } else { throw new Exception("[Material] Unknown node type: " + generic.GetType()); } } generics = mixinGenerics.ToArray(); } var shaderClassSource = new ShaderClassSource(mixinName, generics); if (CompositionNodes.Count == 0) { return(shaderClassSource); } var mixin = new ShaderMixinSource(); mixin.Mixins.Add(shaderClassSource); foreach (var comp in CompositionNodes) { if (comp.Value != null) { var compShader = comp.Value.GenerateShaderSource(context, baseKeys); if (compShader != null) { mixin.Compositions.Add(comp.Key, compShader); } } } return(mixin); }
private LoadedShaderClassType LoadShaderClass(ShaderClassSource classSource, string generics, LoggerResult log, SiliconStudio.Shaders.Parser.ShaderMacro[] macros = null) { var type = classSource.ClassName; if (type == null) { throw new ArgumentNullException("type"); } var shaderSourceKey = new ShaderSourceKey(type, generics, macros); lock (loadedShaders) { // Already instantiated LoadedShaderClassType shaderClass; if (loadedShaders.TryGetValue(shaderSourceKey, out shaderClass)) { return(shaderClass); } // Load file var shaderSource = SourceManager.LoadShaderSource(type); string preprocessedSource; try { preprocessedSource = PreProcessor.Run(shaderSource.Source, shaderSource.Path, macros); } catch (Exception ex) { log.Error(MessageCode.ErrorUnexpectedException, new SourceSpan(new SourceLocation(shaderSource.Path, 0, 1, 1), 1), ex); return(null); } byte[] byteArray = Encoding.UTF8.GetBytes(preprocessedSource); var hashPreprocessSource = ObjectId.FromBytes(byteArray); // Compile var parsingResult = XenkoShaderParser.TryParse(preprocessedSource, shaderSource.Path); parsingResult.CopyTo(log); if (parsingResult.HasErrors) { return(null); } var shader = parsingResult.Shader; // As shaders can be embedded in namespaces, get only the shader class and make sure there is only one in a xksl. var shaderClassTypes = XenkoShaderParser.GetShaderClassTypes(shader.Declarations).ToList(); if (shaderClassTypes.Count != 1) { var sourceSpan = new SourceSpan(new SourceLocation(shaderSource.Path, 0, 0, 0), 1); if (shaderClassTypes.Count > 1) { sourceSpan = shaderClassTypes[1].Span; } log.Error(XenkoMessageCode.ShaderMustContainSingleClassDeclaration, sourceSpan, type); return(null); } shaderClass = new LoadedShaderClassType(); shaderClass.Type = shaderClassTypes.First(); shaderClass.SourcePath = shaderSource.Path; shaderClass.SourceHash = shaderSource.Hash; shaderClass.PreprocessedSourceHash = hashPreprocessSource; shaderClass.IsInstanciated = false; // TODO: We should not use Console. Change the way we log things here // Console.WriteLine("Loading Shader {0}{1}", type, macros != null && macros.Length > 0 ? String.Format("<{0}>", string.Join(", ", macros)) : string.Empty); // If the file name is not matching the class name, provide an error if (shaderClass.Type.Name.Text != type) { log.Error(XenkoMessageCode.FileNameNotMatchingClassName, shaderClass.Type.Name.Span, type, shaderClass.Type.Name.Text); return(null); } loadedShaders.Add(shaderSourceKey, shaderClass); return(shaderClass); } }
public PointLightShaderGroupData(RenderContext renderContext, LightClusteredPointGroupRenderer pointGroupRenderer) : base(renderContext, null) { this.pointGroupRenderer = pointGroupRenderer; ShaderSource = new ShaderClassSource("LightClusteredPointGroup", ClusterSize); }
/// <summary> /// Loads the <see cref="ShaderClassType" />. /// </summary> /// <param name="shaderClassSource">The shader class source.</param> /// <param name="shaderMacros">The shader macros.</param> /// <param name="log">The log to output error logs.</param> /// <param name="autoGenericInstances"></param> /// <returns>A ShaderClassType or null if there was some errors.</returns> /// <exception cref="System.ArgumentNullException">shaderClassSource</exception> public LoadedShaderClassType LoadClassSource(ShaderClassSource shaderClassSource, SiliconStudio.Shaders.Parser.ShaderMacro[] shaderMacros, LoggerResult log, bool autoGenericInstances) { if (shaderClassSource == null) { throw new ArgumentNullException("shaderClassSource"); } string generics = null; if (shaderClassSource.GenericArguments != null) { generics = ""; foreach (var gen in shaderClassSource.GenericArguments) { generics += "___" + gen; } } var shaderClassType = LoadShaderClass(shaderClassSource.ClassName, generics, log, shaderMacros); if (shaderClassType == null) { return(null); } // Instantiate generic class if (shaderClassSource.GenericArguments != null || (shaderClassType.Type.ShaderGenerics.Count > 0 && autoGenericInstances)) { if (shaderClassType.IsInstanciated) { return(shaderClassType); } // If we want to automatically generate a generic instance (in case we just want to parse and verify the generic) if (autoGenericInstances) { shaderClassSource.GenericArguments = new string[shaderClassType.Type.ShaderGenerics.Count]; for (int i = 0; i < shaderClassSource.GenericArguments.Length; i++) { var variableGeneric = shaderClassType.Type.ShaderGenerics[i]; shaderClassSource.GenericArguments[i] = GetDefaultConstValue(variableGeneric); } } if (shaderClassSource.GenericArguments.Length != shaderClassType.Type.ShaderGenerics.Count) { log.Error(XenkoMessageCode.WrongGenericNumber, shaderClassType.Type.Span, shaderClassSource.ClassName); return(null); } // check the name of the generics foreach (var generic in shaderClassType.Type.ShaderGenerics) { foreach (var genericCompare in shaderClassType.Type.ShaderGenerics.Where(x => x != generic)) { if (generic.Name.Text == genericCompare.Name.Text) { log.Error(XenkoMessageCode.SameNameGenerics, generic.Span, generic, genericCompare, shaderClassSource.ClassName); } } } if (log.HasErrors) { return(null); } // When we use an actual generic instance, we replace the name with the name of the class + a hash of the generic parameters if (!autoGenericInstances) { var className = GenerateGenericClassName(shaderClassSource); shaderClassType.Type.Name = new Identifier(className); } var genericAssociation = CreateGenericAssociation(shaderClassType.Type.ShaderGenerics, shaderClassSource.GenericArguments); var identifierGenerics = GenerateIdentifierFromGenerics(genericAssociation); var expressionGenerics = GenerateGenericsExpressionValues(shaderClassType.Type.ShaderGenerics, shaderClassSource.GenericArguments); XenkoClassInstantiator.Instantiate(shaderClassType.Type, expressionGenerics, identifierGenerics, autoGenericInstances, log); shaderClassType.Type.ShaderGenerics.Clear(); shaderClassType.IsInstanciated = true; } return(shaderClassType); }
public ShaderClassSource GetSamplingShader() { sampler = new ShaderClassSource("VoxelStorageTextureClipmapShader", VoxelSize, ClipMapCount, LayoutSize, ClipMapResolution.Y / 2.0f); return(sampler); }
public void TestMaterial() { var compiler = new EffectCompiler(TestHelper.CreateDatabaseProvider().FileProvider) { UseFileSystem = true }; var currentPath = Core.PlatformFolders.ApplicationBinaryDirectory; compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Graphics\Shaders")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Shaders")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Core")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Lights")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Shadows")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Materials\Shaders")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Materials\ComputeColors\Shaders")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Skinning")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Shading")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Transformation")); compiler.SourceDirectories.Add(Path.Combine(currentPath, @"..\..\sources\engine\Xenko.Engine\Rendering\Utils")); var compilerParameters = new CompilerParameters { EffectParameters = { Platform = GraphicsPlatform.OpenGL } }; var layers = new MaterialBlendLayers(); layers.Add(new MaterialBlendLayer { BlendMap = new ComputeFloat(0.5f), Material = AttachedReferenceManager.CreateProxyObject <Material>(AssetId.Empty, "fake") }); var materialAsset = new MaterialAsset { Attributes = new MaterialAttributes() { Diffuse = new MaterialDiffuseMapFeature() { DiffuseMap = new ComputeColor(Color4.White) }, DiffuseModel = new MaterialDiffuseLambertModelFeature() }, Layers = layers }; var fakeAsset = new MaterialAsset { Attributes = new MaterialAttributes() { Diffuse = new MaterialDiffuseMapFeature() { DiffuseMap = new ComputeColor(Color.Blue) }, } }; var context = new MaterialGeneratorContext { FindAsset = reference => fakeAsset }; var result = MaterialGenerator.Generate(new MaterialDescriptor { Attributes = materialAsset.Attributes, Layers = materialAsset.Layers }, context, "TestMaterial"); compilerParameters.Set(MaterialKeys.PixelStageSurfaceShaders, result.Material.Passes[0].Parameters.Get(MaterialKeys.PixelStageSurfaceShaders)); var directionalLightGroup = new ShaderClassSource("LightDirectionalGroup", 1); compilerParameters.Set(LightingKeys.DirectLightGroups, new ShaderSourceCollection { directionalLightGroup }); //compilerParameters.Set(LightingKeys.CastShadows, false); //compilerParameters.Set(MaterialParameters.HasSkinningPosition, true); //compilerParameters.Set(MaterialParameters.HasSkinningNormal, true); compilerParameters.Set(MaterialKeys.HasNormalMap, true); var results = compiler.Compile(new ShaderMixinGeneratorSource("XenkoEffectBase"), compilerParameters); Assert.False(results.HasErrors); }
public void TestMaterial() { var compiler = new EffectCompiler { UseFileSystem = true }; compiler.SourceDirectories.Add(@"..\..\sources\engine\SiliconStudio.Xenko.Graphics\Shaders"); compiler.SourceDirectories.Add(@"..\..\sources\engine\SiliconStudio.Xenko.Engine\Shaders"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Core"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Lights"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Materials"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Shadows"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\ComputeColor"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Skinning"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Shading"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Transformation"); compiler.SourceDirectories.Add(@"..\..\sources\shaders\Utils"); var compilerParameters = new CompilerParameters { EffectParameters = { Platform = GraphicsPlatform.OpenGL } }; var layers = new MaterialBlendLayers(); layers.Add(new MaterialBlendLayer { BlendMap = new ComputeFloat(0.5f), Material = AttachedReferenceManager.CreateSerializableVersion <Material>(Guid.Empty, "fake") }); var materialAsset = new MaterialAsset { Attributes = new MaterialAttributes() { Diffuse = new MaterialDiffuseMapFeature() { DiffuseMap = new ComputeColor(Color4.White) }, DiffuseModel = new MaterialDiffuseLambertModelFeature() }, Layers = layers }; var fakeAsset = new MaterialAsset { Attributes = new MaterialAttributes() { Diffuse = new MaterialDiffuseMapFeature() { DiffuseMap = new ComputeColor(Color.Blue) }, } }; var context = new MaterialGeneratorContext { FindAsset = reference => fakeAsset }; var result = MaterialGenerator.Generate(new MaterialDescriptor { Attributes = materialAsset.Attributes, Layers = materialAsset.Layers }, context, "TestMaterial"); compilerParameters.Set(MaterialKeys.PixelStageSurfaceShaders, result.Material.Parameters.Get(MaterialKeys.PixelStageSurfaceShaders)); var directionalLightGroup = new ShaderClassSource("LightDirectionalGroup", 1); compilerParameters.Set(LightingKeys.DirectLightGroups, new ShaderSourceCollection { directionalLightGroup }); //compilerParameters.Set(LightingKeys.CastShadows, false); //compilerParameters.Set(MaterialParameters.HasSkinningPosition, true); //compilerParameters.Set(MaterialParameters.HasSkinningNormal, true); compilerParameters.Set(MaterialKeys.HasNormalMap, true); var results = compiler.Compile(new ShaderMixinGeneratorSource("XenkoEffectBase"), compilerParameters); Assert.IsFalse(results.HasErrors); }
public PointSpotShaderGroupData(RenderContext renderContext, PointLightShaderGroupData pointLightGroup) : base(renderContext, null) { ShaderSource = new ShaderClassSource("LightClusteredSpotGroup", pointLightGroup.ClusterSize); }
//private Plane[] zPlanes; public PointLightShaderGroupData(RenderContext renderContext, LightClusteredPointSpotGroupRenderer clusteredGroupRenderer) : base(renderContext, null) { this.clusteredGroupRenderer = clusteredGroupRenderer; ShaderSource = new ShaderClassSource("LightClusteredPointGroup"); }
/// <summary> /// Build the ShaderMixinSource to evaluate the binaryNode. /// </summary> /// <param name="node">The MaterialShaderClassNode binaryNode used as source to find the ShaderMixinSource.</param> /// <returns>The corresponding ShaderMixinSource.</returns> private ShaderSource GetShaderSource(MaterialShaderClassNode node) { if (!node.MixinReference.HasLocation()) { return(new ShaderClassSource("ComputeColor")); } var mixinName = Path.GetFileNameWithoutExtension(node.MixinReference.Location); object[] generics = null; if (node.Generics.Count > 0) { // TODO: correct generic order var mixinGenerics = new List <object>(); foreach (var genericKey in node.Generics.Keys) { var generic = node.Generics[genericKey]; if (generic is NodeParameterTexture) { var textureReference = ((NodeParameterTexture)generic).Reference; var foundNode = Material.FindNode(textureReference); while (foundNode != null && !(foundNode is MaterialTextureNode)) { var refNode = foundNode as MaterialReferenceNode; if (refNode == null) { break; } foundNode = Material.FindNode(refNode.Name); } var foundTextureNode = foundNode as MaterialTextureNode; if (foundTextureNode == null || foundTextureNode.UsedParameterKey == null) { Logger.Warning("[Material] The generic texture reference in node [" + node + "] is incorrect."); mixinGenerics.Add("Texturing.Texture0"); } else { mixinGenerics.Add(foundTextureNode.UsedParameterKey.ToString()); } } else if (generic is NodeParameterSampler) { var pk = ((NodeParameterSampler)generic).SamplerParameterKey; if (pk == null) { Logger.Warning("[Material] The generic sampler reference in node [" + node + "] is incorrect."); mixinGenerics.Add("Texturing.Sampler"); } else { mixinGenerics.Add(pk.ToString()); } } else if (generic is NodeParameterFloat) { mixinGenerics.Add(((NodeParameterFloat)generic).Value.ToString(CultureInfo.InvariantCulture)); } else if (generic is NodeParameterInt) { mixinGenerics.Add(((NodeParameterInt)generic).Value.ToString(CultureInfo.InvariantCulture)); } else if (generic is NodeParameterFloat2) { mixinGenerics.Add(GetAsShaderString(((NodeParameterFloat2)generic).Value)); } else if (generic is NodeParameterFloat3) { mixinGenerics.Add(GetAsShaderString(((NodeParameterFloat3)generic).Value)); } else if (generic is NodeParameterFloat4) { mixinGenerics.Add(GetAsShaderString(((NodeParameterFloat4)generic).Value)); } else if (generic is NodeParameter) { mixinGenerics.Add(((NodeParameter)generic).Reference); } else { throw new Exception("[Material] Unknown node type: " + generic.GetType()); } } generics = mixinGenerics.ToArray(); } var shaderClassSource = new ShaderClassSource(mixinName, generics); if (node.CompositionNodes.Count == 0) { return(shaderClassSource); } var mixin = new ShaderMixinSource(); mixin.Mixins.Add(shaderClassSource); foreach (var comp in node.CompositionNodes) { if (comp.Value != null) { var compShader = GetShaderSource(comp.Value); if (compShader != null) { mixin.Compositions.Add(comp.Key, compShader); } } } return(mixin); }