Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
StageStatus.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 SiliconStudio.Paradox.Effects;
5 using SiliconStudio.Paradox.Shaders;
6 
7 namespace SiliconStudio.Paradox.Graphics.Internals
8 {
9  internal class ShaderStageSetup
10  {
11  private readonly ShaderParameterUpdater parameterUpdater;
12  private static readonly EffectParameterResourceBinding.ApplyParameterWithUpdaterDelegate UpdateConstantBufferFromUpdater = UpdateConstantBuffer;
13  private static readonly EffectParameterResourceBinding.ApplyParameterWithUpdaterDelegate UpdateSamplerFromUpdater = UpdateSampler;
14  private static readonly EffectParameterResourceBinding.ApplyParameterWithUpdaterDelegate UpdateShaderResourceViewFromUpdater = UpdateShaderResourceView;
15  private static readonly EffectParameterResourceBinding.ApplyParameterWithUpdaterDelegate UpdateUnorderedAccessViewFromUpdater = UpdateUnorderedAccessView;
16  private static readonly EffectParameterResourceBinding.ApplyParameterFromValueDelegate UpdateConstantBufferDirect = UpdateConstantBuffer;
17  private static readonly EffectParameterResourceBinding.ApplyParameterFromValueDelegate UpdateSamplerDirect = UpdateSampler;
18  private static readonly EffectParameterResourceBinding.ApplyParameterFromValueDelegate UpdateShaderResourceViewDirect = UpdateShaderResourceView;
19  private static readonly EffectParameterResourceBinding.ApplyParameterFromValueDelegate UpdateUnorderedAccessViewDirect = UpdateUnorderedAccessView;
20 
21  public readonly ParameterCollection[] ParameterCollections;
22 
23  public ShaderStageSetup()
24  {
25  parameterUpdater = new ShaderParameterUpdater();
26  ParameterCollections = new ParameterCollection[16];
27  }
28 
29  public void SetParameterCollection(int index, ParameterCollection collection)
30  {
31  ParameterCollections[index] = collection;
32  }
33 
34  public void PrepareBindings(EffectParameterResourceBinding[] bindings)
35  {
36  for (int i = 0; i < bindings.Length; i++)
37  {
38  PrepareBinding(ref bindings[i]);
39  }
40  }
41 
42  public void UpdateParameters(GraphicsDevice graphicsDevice, ShaderParameterUpdaterDefinition parameterUpdaterDefinition, int collectionCount)
43  {
44  parameterUpdater.Update(graphicsDevice, parameterUpdaterDefinition, ParameterCollections, collectionCount);
45  }
46 
47  public void Apply(GraphicsDevice graphicsDevice, EffectParameterResourceBinding[] bindings, ref EffectStateBindings effectStateBindings, bool applyEffectStates)
48  {
49  // Apply shader parameters
50  for (int i = 0; i < bindings.Length; i++)
51  {
52  bindings[i].ApplyParameterWithUpdater(graphicsDevice, ref bindings[i].Description, parameterUpdater);
53  }
54 
55  if (applyEffectStates)
56  {
57  // Apply graphics states
58  var rasterizerState = parameterUpdater.GetValue<RasterizerState>(effectStateBindings.RasterizerStateKeyIndex);
59  graphicsDevice.SetRasterizerState(rasterizerState);
60 
61  var depthStencilState = parameterUpdater.GetValue<DepthStencilState>(effectStateBindings.DepthStencilStateKeyIndex);
62  graphicsDevice.SetDepthStencilState(depthStencilState);
63 
64  var blendState = parameterUpdater.GetValue<BlendState>(effectStateBindings.BlendStateKeyIndex);
65  graphicsDevice.SetBlendState(blendState);
66  }
67  }
68 
69  public void UnbindResources(GraphicsDevice graphicsDevice, EffectParameterResourceBinding[] bindings)
70  {
71  // look for shader resource and unbind
72  for (int i = 0; i < bindings.Length; i++)
73  {
74  var binding = bindings[i];
75  if (binding.Description.Param.Class == EffectParameterClass.ShaderResourceView)
76  graphicsDevice.SetShaderResourceView(binding.Description.Stage, binding.Description.SlotStart, null);
77  else if (binding.Description.Param.Class == EffectParameterClass.UnorderedAccessView)
78  graphicsDevice.SetUnorderedAccessView(binding.Description.Stage, binding.Description.SlotStart, null);
79  }
80  }
81 
82  private void PrepareBinding(ref EffectParameterResourceBinding binding)
83  {
84  switch (binding.Description.Param.Class)
85  {
86  case EffectParameterClass.ConstantBuffer: // Constant buffer
87  binding.ApplyParameterWithUpdater = UpdateConstantBufferFromUpdater;
88  binding.ApplyParameterDirect = UpdateConstantBufferDirect;
89  break;
90  case EffectParameterClass.Sampler: // Sampler state
91  binding.ApplyParameterWithUpdater = UpdateSamplerFromUpdater;
92  binding.ApplyParameterDirect = UpdateSamplerDirect;
93  break;
94  case EffectParameterClass.ShaderResourceView: // Texture & StructuredBuffer (using ShaderResourceView)
95  binding.ApplyParameterWithUpdater = UpdateShaderResourceViewFromUpdater;
96  binding.ApplyParameterDirect = UpdateShaderResourceViewDirect;
97  break;
98  case EffectParameterClass.UnorderedAccessView: // RWTexture, RWStructuredBuffer only valid for Compute shaders.
99  binding.ApplyParameterWithUpdater = UpdateUnorderedAccessViewFromUpdater;
100  binding.ApplyParameterDirect = UpdateUnorderedAccessViewDirect;
101  break;
102  }
103  }
104 
105  private static void UpdateConstantBuffer(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
106  {
107  var constantBufferHelper = parameterUpdater.GetValue<ParameterConstantBuffer>(binding.Param.KeyIndex);
108 
109  // Update constant buffer content (if required)
110  constantBufferHelper.Update(graphicsDevice, parameterUpdater);
111 
112  graphicsDevice.SetConstantBuffer(binding.Stage, binding.SlotStart, constantBufferHelper.Buffer);
113  }
114 
115  private static void UpdateSampler(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
116  {
117  var samplerState = (SamplerState)parameterUpdater.GetObject(binding.Param.KeyIndex);
118  graphicsDevice.SetSamplerState(binding.Stage, binding.SlotStart, samplerState);
119  }
120 
121  private static void UpdateShaderResourceView(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
122  {
123  var shaderResourceView = (GraphicsResource)parameterUpdater.GetObject(binding.Param.KeyIndex);
124  graphicsDevice.SetShaderResourceView(binding.Stage, binding.SlotStart, shaderResourceView);
125  }
126 
127  private static void UpdateUnorderedAccessView(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
128  {
129  var unorderedAccessView = (GraphicsResource)parameterUpdater.GetObject(binding.Param.KeyIndex);
130  graphicsDevice.SetUnorderedAccessView(binding.Stage, binding.SlotStart, unorderedAccessView);
131  }
132 
133  private static void UpdateConstantBuffer(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, object value)
134  {
135  throw new NotSupportedException("Fast update for constant buffer not supported");
136  }
137 
138  private static void UpdateSampler(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, object value)
139  {
140  var samplerState = (SamplerState)value;
141  graphicsDevice.SetSamplerState(binding.Stage, binding.SlotStart, samplerState);
142  }
143 
144  private static void UpdateShaderResourceView(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, object value)
145  {
146  var shaderResourceView = (GraphicsResource)value;
147  graphicsDevice.SetShaderResourceView(binding.Stage, binding.SlotStart, shaderResourceView);
148  }
149 
150  private static void UpdateUnorderedAccessView(GraphicsDevice graphicsDevice, ref EffectParameterResourceData binding, object value)
151  {
152  var unorderedAccessView = (GraphicsResource)value;
153  graphicsDevice.SetUnorderedAccessView(binding.Stage, binding.SlotStart, unorderedAccessView);
154  }
155  }
156 }
SiliconStudio.Paradox.Graphics.Buffer Buffer
Definition: BasicEffect.cs:15
Describes a shader parameter for a resource type.
EffectParameterClass
Values that identify the class of a shader variable.
A container to handle a hierarchical collection of effect variables.