1 using System.Collections.Generic;
4 using SiliconStudio.Paradox.Graphics;
7 using QuickGraph.Algorithms;
9 namespace SiliconStudio.
Paradox.Effects
16 BidirectionalGraph<EffectMesh, PostEffectEdge> graph =
new BidirectionalGraph<EffectMesh, PostEffectEdge>();
17 private Dictionary<TextureDescription, List<RenderTarget>> textures =
new Dictionary<TextureDescription, List<RenderTarget>>();
31 return graph.Vertices;
37 if (!graph.ContainsVertex(mesh))
38 graph.AddVertex(mesh);
43 if (!graph.ContainsVertex(source))
44 graph.AddVertex(source);
46 if (!graph.ContainsVertex(target))
47 graph.AddVertex(target);
49 graph.AddEdge(
new PostEffectEdge(source, sourceKey, target, targetKey, texture));
54 if (!graph.ContainsVertex(source))
55 graph.AddVertex(source);
57 if (!graph.ContainsVertex(target))
58 graph.AddVertex(target);
60 graph.AddEdge(
new PostEffectEdge(source, sourceKey, target, targetKey, textureDescription));
69 var currentTextures =
new Dictionary<RenderTarget, int>();
72 foreach (var node
in graph.TopologicalSort())
74 var inEdges = graph.InEdges(node);
81 foreach (var outEdges
in graph.OutEdges(node).GroupBy(x => x.SourceKey))
86 var resource = outEdges.Where(x => x.ProvidedTexture != null).Select(x => x.ProvidedTexture).FirstOrDefault();
93 var forcedTextureDescriptions = outEdges.Where(x => x.TextureDescription != null).Select(x => x.TextureDescription);
96 var forcedTextureDescription = forcedTextureDescriptions.FirstOrDefault();
98 if (forcedTextureDescription != null)
100 textureDescription = forcedTextureDescription;
105 if (textureDescription == null)
107 foreach (var inEdge
in inEdges)
109 textureDescription = ((
Texture) node.Parameters.GetObject(inEdge.TargetKey)).Description;
110 if (textureDescription != null)
break;
115 if (textureDescription == null)
119 textureDescription =
new TextureDescription {Width = 1024, Height = 768, Format = PixelFormat.R8G8B8A8_UNorm};
123 List<RenderTarget> matchingTextureList;
124 if (!textures.TryGetValue(textureDescription.Value, out matchingTextureList))
125 textures[textureDescription.Value] = matchingTextureList =
new List<RenderTarget>();
127 resource = matchingTextureList.FirstOrDefault(x => !currentTextures.ContainsKey(x));
130 if (resource == null)
132 resource = Texture2D.New(
GraphicsDevice, textureDescription.Value).ToRenderTarget();
133 matchingTextureList.Add(resource);
138 node.Parameters.SetObject(outEdges.Key, resource);
140 foreach (var outEdge
in outEdges)
143 outEdge.Target.Parameters.SetObject(outEdge.TargetKey, resource.Texture);
144 outEdge.Texture = resource;
146 if (!currentTextures.ContainsKey(resource))
147 currentTextures[resource] = outEdges.Count();
149 currentTextures[resource] += outEdges.Count();
152 foreach (var inEdge
in inEdges)
154 var resource = inEdge.Texture;
155 if (--currentTextures[resource] == 0)
156 currentTextures.Remove(resource);
161 private class PostEffectEdge : IEdge<EffectMesh>
167 SourceKey = sourceKey;
168 TargetKey = targetKey;
169 ProvidedTexture = texture;
172 public PostEffectEdge(EffectMesh source, ParameterKey<RenderTarget> sourceKey, EffectMesh target, ParameterKey<Texture> targetKey,
TextureDescription? textureDescription = null)
176 SourceKey = sourceKey;
177 TargetKey = targetKey;
181 public EffectMesh
Source {
get;
private set; }
183 public EffectMesh Target {
get;
private set; }
185 public ParameterKey SourceKey {
get;
private set; }
187 public ParameterKey TargetKey {
get;
private set; }
void AddMesh(EffectMesh mesh)
Key of an effect parameter.
A renderable texture view.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
A Common description for all textures.
void AddLink(EffectMesh source, ParameterKey< RenderTarget > sourceKey, EffectMesh target, ParameterKey< Texture > targetKey, RenderTarget texture)
void Resolve()
Resolve the dependency graph for the resources and create the necessary RenderTarget2D. It will take care of reusing resources (if possible) and creating them if more are necessaries.
PostEffectGraphPlugin(string name)
RenderPass is a hierarchy that defines how to collect and render meshes.
void AddLink(EffectMesh source, ParameterKey< RenderTarget > sourceKey, EffectMesh target, ParameterKey< Texture > targetKey, TextureDescription?textureDescription=null)
Base class for texture resources.