4 using SiliconStudio.Paradox.Effects;
5 using SiliconStudio.Paradox.Shaders;
7 namespace SiliconStudio.
Paradox.Graphics.Internals
9 internal class ShaderStageSetup
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;
23 public ShaderStageSetup()
25 parameterUpdater =
new ShaderParameterUpdater();
31 ParameterCollections[index] = collection;
34 public void PrepareBindings(EffectParameterResourceBinding[] bindings)
36 for (
int i = 0; i < bindings.Length; i++)
38 PrepareBinding(ref bindings[i]);
42 public void UpdateParameters(GraphicsDevice graphicsDevice, ShaderParameterUpdaterDefinition parameterUpdaterDefinition,
int collectionCount)
44 parameterUpdater.Update(graphicsDevice, parameterUpdaterDefinition, ParameterCollections, collectionCount);
47 public void Apply(GraphicsDevice graphicsDevice, EffectParameterResourceBinding[] bindings, ref EffectStateBindings effectStateBindings,
bool applyEffectStates)
50 for (
int i = 0; i < bindings.Length; i++)
52 bindings[i].ApplyParameterWithUpdater(graphicsDevice, ref bindings[i].Description, parameterUpdater);
55 if (applyEffectStates)
58 var rasterizerState = parameterUpdater.GetValue<RasterizerState>(effectStateBindings.RasterizerStateKeyIndex);
59 graphicsDevice.SetRasterizerState(rasterizerState);
61 var depthStencilState = parameterUpdater.GetValue<DepthStencilState>(effectStateBindings.DepthStencilStateKeyIndex);
62 graphicsDevice.SetDepthStencilState(depthStencilState);
64 var blendState = parameterUpdater.GetValue<BlendState>(effectStateBindings.BlendStateKeyIndex);
65 graphicsDevice.SetBlendState(blendState);
69 public void UnbindResources(GraphicsDevice graphicsDevice, EffectParameterResourceBinding[] bindings)
72 for (
int i = 0; i < bindings.Length; i++)
74 var binding = bindings[i];
76 graphicsDevice.SetShaderResourceView(binding.Description.Stage, binding.Description.SlotStart, null);
78 graphicsDevice.SetUnorderedAccessView(binding.Description.Stage, binding.Description.SlotStart, null);
82 private void PrepareBinding(ref EffectParameterResourceBinding binding)
84 switch (binding.Description.Param.Class)
86 case EffectParameterClass.ConstantBuffer:
87 binding.ApplyParameterWithUpdater = UpdateConstantBufferFromUpdater;
88 binding.ApplyParameterDirect = UpdateConstantBufferDirect;
90 case EffectParameterClass.Sampler:
91 binding.ApplyParameterWithUpdater = UpdateSamplerFromUpdater;
92 binding.ApplyParameterDirect = UpdateSamplerDirect;
94 case EffectParameterClass.ShaderResourceView:
95 binding.ApplyParameterWithUpdater = UpdateShaderResourceViewFromUpdater;
96 binding.ApplyParameterDirect = UpdateShaderResourceViewDirect;
98 case EffectParameterClass.UnorderedAccessView:
99 binding.ApplyParameterWithUpdater = UpdateUnorderedAccessViewFromUpdater;
100 binding.ApplyParameterDirect = UpdateUnorderedAccessViewDirect;
105 private static void UpdateConstantBuffer(GraphicsDevice graphicsDevice, ref
EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
107 var constantBufferHelper = parameterUpdater.GetValue<ParameterConstantBuffer>(binding.Param.KeyIndex);
110 constantBufferHelper.Update(graphicsDevice, parameterUpdater);
115 private static void UpdateSampler(GraphicsDevice graphicsDevice, ref
EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
117 var samplerState = (SamplerState)parameterUpdater.GetObject(binding.Param.KeyIndex);
118 graphicsDevice.SetSamplerState(binding.Stage, binding.SlotStart, samplerState);
121 private static void UpdateShaderResourceView(GraphicsDevice graphicsDevice, ref
EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
123 var shaderResourceView = (GraphicsResource)parameterUpdater.GetObject(binding.Param.KeyIndex);
124 graphicsDevice.SetShaderResourceView(binding.Stage, binding.SlotStart, shaderResourceView);
127 private static void UpdateUnorderedAccessView(GraphicsDevice graphicsDevice, ref
EffectParameterResourceData binding, ShaderParameterUpdater parameterUpdater)
129 var unorderedAccessView = (GraphicsResource)parameterUpdater.GetObject(binding.Param.KeyIndex);
130 graphicsDevice.SetUnorderedAccessView(binding.Stage, binding.SlotStart, unorderedAccessView);
135 throw new NotSupportedException(
"Fast update for constant buffer not supported");
140 var samplerState = (SamplerState)value;
141 graphicsDevice.SetSamplerState(binding.Stage, binding.SlotStart, samplerState);
146 var shaderResourceView = (GraphicsResource)value;
147 graphicsDevice.SetShaderResourceView(binding.Stage, binding.SlotStart, shaderResourceView);
150 private static void UpdateUnorderedAccessView(GraphicsDevice graphicsDevice, ref
EffectParameterResourceData binding,
object value)
152 var unorderedAccessView = (GraphicsResource)value;
153 graphicsDevice.SetUnorderedAccessView(binding.Stage, binding.SlotStart, unorderedAccessView);
SiliconStudio.Paradox.Graphics.Buffer Buffer
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.