Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImageEffectShader.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 
6 using SiliconStudio.Paradox.Effects.Modules;
7 using SiliconStudio.Paradox.Graphics;
8 
9 namespace SiliconStudio.Paradox.Effects.Images
10 {
11  /// <summary>
12  /// Post effect using an <see cref="Effect"/> (either pdxfx or pdxsl).
13  /// </summary>
15  {
16  private readonly ParameterCollection parameters;
17 
18  private readonly InternalEffectInstance effectInstance;
19 
20  private readonly DynamicEffectCompiler effectCompiler;
21 
22  /// <summary>
23  /// Initializes a new instance of the <see cref="ImageEffectShader"/> class.
24  /// </summary>
25  /// <param name="context">The context.</param>
26  /// <param name="effectName">Name of the shader.</param>
27  public ImageEffectShader(ImageEffectContext context, string effectName)
28  : base(context, effectName)
29  {
30  if (effectName == null) throw new ArgumentNullException("effectName");
31 
32  // Setup this instance parameters
33  parameters = new ParameterCollection();
34  // As this is used by PostEffectBase, we just setup it here by default
35  parameters.Set(TexturingKeys.Sampler, GraphicsDevice.SamplerStates.LinearClamp);
36 
37  // Setup the effect compiler
38  effectInstance = new InternalEffectInstance(parameters);
39  effectCompiler = new DynamicEffectCompiler(context.Services, effectName);
40  }
41 
42  /// <summary>
43  /// Gets the name of the effect.
44  /// </summary>
45  public string EffectName
46  {
47  get
48  {
49  return effectCompiler.EffectName;
50  }
51  }
52 
53  /// <summary>
54  /// Gets the effect parameters.
55  /// </summary>
56  /// <value>The parameters.</value>
57  public ParameterCollection Parameters
58  {
59  get
60  {
61  return parameters;
62  }
63  }
64 
65  protected override void PreDrawCore(string name)
66  {
67  base.PreDrawCore(name);
68 
69  // Default handler for parameters
70  UpdateParameters();
71  }
72 
73  /// <summary>
74  /// Updates the effect <see cref="Parameters"/> from properties defined in this instance. See remarks.
75  /// </summary>
76  /// <exception cref="System.InvalidOperationException">Expecting less than 10 textures in input</exception>
77  /// <remarks>
78  /// By default, all the input textures will be remapped to <see cref="TexturingKeys.Texture0"/>...etc.
79  /// </remarks>
80  protected virtual void UpdateParameters()
81  {
82  // By default, we are copying all input textures to TexturingKeys.Texture#
83  var count = InputCount;
84  for (int i = 0; i < count; i++)
85  {
86  var texture = GetInput(i);
87  switch (i)
88  {
89  case 0:
90  Parameters.Set(TexturingKeys.Texture0, texture);
91  break;
92  case 1:
93  Parameters.Set(TexturingKeys.Texture1, texture);
94  break;
95  case 2:
96  Parameters.Set(TexturingKeys.Texture2, texture);
97  break;
98  case 3:
99  Parameters.Set(TexturingKeys.Texture3, texture);
100  break;
101  case 4:
102  Parameters.Set(TexturingKeys.Texture4, texture);
103  break;
104  case 5:
105  Parameters.Set(TexturingKeys.Texture5, texture);
106  break;
107  case 6:
108  Parameters.Set(TexturingKeys.Texture6, texture);
109  break;
110  case 7:
111  Parameters.Set(TexturingKeys.Texture7, texture);
112  break;
113  case 8:
114  Parameters.Set(TexturingKeys.Texture8, texture);
115  break;
116  case 9:
117  Parameters.Set(TexturingKeys.Texture9, texture);
118  break;
119  default:
120  // TODO: This is not clean
121  throw new InvalidOperationException("Expecting less than 10 textures in input");
122  }
123  }
124  }
125 
126  protected override void DrawCore()
127  {
128  // Dynamically update/compile the effect based on the current parameters.
129  effectCompiler.Update(effectInstance);
130 
131  // Draw a full screen quad
132  GraphicsDevice.DrawQuad(effectInstance.Effect, Parameters);
133  }
134 
135  /// <summary>
136  /// Internal class used for dynamic effect compilation.
137  /// </summary>
138  private class InternalEffectInstance : DynamicEffectInstance
139  {
140  private readonly ParameterCollection parameters;
141 
142  public InternalEffectInstance(ParameterCollection parameters)
143  {
144  this.parameters = parameters;
145  }
146 
147  public override void FillParameterCollections(IList<ParameterCollection> parameterCollections)
148  {
149  parameterCollections.Add(parameters);
150  }
151  }
152  }
153 }
A dynamic effect instance updated by DynamicEffectCompiler.
virtual void UpdateParameters()
Updates the effect Parameters from properties defined in this instance. See remarks.
ImageEffectShader(ImageEffectContext context, string effectName)
Initializes a new instance of the ImageEffectShader class.
IServiceRegistry Services
Gets the services registry.
_In_ size_t count
Definition: DirectXTexP.h:174
Provides a dynamic compiler for an effect based on parameters changed.
override void PreDrawCore(string name)
Prepare call before DrawCore.
override void DrawCore()
Draws this post effect for a specific pass, implementation dependent.
A container to handle a hierarchical collection of effect variables.
Post effect using an Effect (either pdxfx or pdxsl).