public void TestConstantKeyCreation() { var materialAsset = AssetSerializer.Load <MaterialAsset>("materials/testConstantValueKey.pdxmat"); var materialShaderCreator = new MaterialTreeShaderCreator(materialAsset.Material); var allParameters = materialShaderCreator.GenerateModelShaders(); Assert.IsTrue(allParameters.Keys.Any(x => x == DummyFloatKey)); Assert.IsTrue(allParameters.Keys.Any(x => x == DummyVector4Key)); }
public void TestMakeShader() { var materialAsset = AssetSerializer.Load <MaterialAsset>("materials/testMaterial.pdxmat"); var materialShaderCreator = new MaterialTreeShaderCreator(materialAsset.Material); var allShaders = materialShaderCreator.GenerateModelShaders(); Assert.AreEqual(6, materialShaderCreator.ModelShaderSources.Count); Assert.AreEqual(4, allShaders.Count); // TODO: more tests }
/// <summary> /// Performs the maximal reduction. /// </summary> //public Dictionary<UFile, Image> Run(EffectCompilerBase compiler) public bool Run(EffectCompilerBase compiler) { var result = true; if (commandList.Count > 0) { if (plane == null) { plane = GeometricPrimitive.Plane.New(graphicsDevice, 2.0f, 2.0f); } var assetManager = new AssetManager(); assetManager.Serializer.RegisterSerializer(new GpuTextureSerializer2(graphicsDevice)); var textures = new Dictionary <string, Graphics.Texture>(); var materialTreeShaderCreator = new MaterialTreeShaderCreator(Material); var textureVisitor = new MaterialTextureVisitor(Material); var compilerParameters = new CompilerParameters { Platform = GraphicsPlatform.Direct3D11, Profile = GraphicsProfile.Level_11_0 }; foreach (var command in commandList) { var computeColorShader = materialTreeShaderCreator.GenerateShaderForReduction(command.OldNode); if (computeColorShader == null) { continue; } var finalShader = new ShaderMixinSource(); finalShader.Mixins.Add(new ShaderClassSource("FlattenLayers")); finalShader.Compositions.Add("outColor", computeColorShader); var results = compiler.Compile(finalShader, compilerParameters); if (results.HasErrors) { continue; } command.TreeEffect = new Graphics.Effect(graphicsDevice, results.MainBytecode, results.MainUsedParameters); command.Parameters = new ParameterCollection(); var maxWidth = 0; var maxHeight = 0; var allTextures = textureVisitor.GetAllTextureValues(command.OldNode); foreach (var texSlot in allTextures) { Graphics.Texture tex; if (!textures.TryGetValue(texSlot.TextureName, out tex)) { //TODO: change load so that texture can be unloaded. tex = assetManager.Load <Graphics.Texture>(texSlot.TextureName); textures.Add(texSlot.TextureName, tex); } if (tex == null) { throw new FileNotFoundException("Texture " + texSlot.TextureName + " not found"); } command.Parameters.Set(texSlot.UsedParameterKey, tex); maxWidth = Math.Max(maxWidth, tex.ViewWidth); maxHeight = Math.Max(maxHeight, tex.ViewHeight); // can take min, a user-defined size, or clamp the min/max // exclude mask? } command.RenderTarget = Graphics.Texture.New2D(graphicsDevice, maxWidth, maxHeight, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget); command.ToExecute = true; } // remove wrong commands commandList.RemoveAll(x => !x.ToExecute); var nodeReplacer = new MaterialNodeReplacer(Material); foreach (var command in commandList.Where(x => x.ToExecute)) { lock (graphicsDevice) { graphicsDevice.Clear(command.RenderTarget, Color4.Black); graphicsDevice.SetRenderTarget(command.RenderTarget); graphicsDevice.SetRasterizerState(graphicsDevice.RasterizerStates.CullNone); graphicsDevice.SetDepthStencilState(graphicsDevice.DepthStencilStates.None); command.TreeEffect.Apply(command.Parameters); plane.Draw(); // save texture SaveTexture(command.RenderTarget, command.TextureUrl, assetManager); } // make new tree var newNode = new MaterialTextureNode(command.TextureUrl.FullPath, command.TexcoordIndex, Vector2.One, Vector2.Zero); nodeReplacer.Replace(command.OldNode, newNode); // save new material? command.ToExecute = false; } foreach (var command in commandList) { command.TreeEffect.Dispose(); command.RenderTarget.Dispose(); } foreach (var texture in textures) { texture.Value.Dispose(); } textures.Clear(); foreach (var tex in textures) { assetManager.Unload(tex); } textures.Clear(); result = commandList.All(x => !x.ToExecute); commandList.Clear(); } return(result); }