2 using System.Collections.Generic;
4 using SiliconStudio.Paradox.DataModel;
5 using SiliconStudio.Paradox.Effects.Modules;
6 using SiliconStudio.Paradox.Games;
7 using SiliconStudio.Paradox.Graphics;
8 using SiliconStudio.Core.Mathematics;
9 using SiliconStudio.Paradox.Shaders;
11 namespace SiliconStudio.
Paradox.Effects
22 var effectSystem = Services.GetSafeServiceAs<IEffectSystemOld>();
24 throw new NotImplementedException();
25 EffectShaderPass mainShaderPass;
36 mainShaderMacros = DefaultShaderPass.Macros.ToArray();
39 public void SetupLighting(EffectShaderPass effectPass,
object permutationKeyObject)
43 if (permutationKey.PerPixelDirectionalLightCount > 0)
45 effectPass.Shader.Mixins.Add(
new ShaderClassSource(
"LightMultiDirectionalShadingPerPixel", permutationKey.PerPixelDirectionalLightCount));
48 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingPerPixelKeys.LightColorsWithGamma, ParameterDynamicValue.New(LightingPermutation.Key,
50 LightColorsUpdate(lightPermutation.PerPixelDirectionalLights, lightColorsWithGamma)
51 , autoCheckDependencies:
false));
54 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingPerPixelKeys.LightDirectionsVS, ParameterDynamicValue.New(LightingPermutation.Key, TransformationKeys.View, (ref
LightingPermutation lightPermutation, ref
Matrix view, ref
Vector3[] lightDirectionVS) =>
57 foreach (var lightBinding
in lightPermutation.PerPixelDirectionalLights)
60 LightKeys.LightDirectionVSUpdate(ref lightDirection, ref view, ref lightDirectionVS[index++]);
62 }, autoCheckDependencies:
false));
65 if (permutationKey.PerPixelDiffuseDirectionalLightCount > 0)
67 effectPass.Shader.Mixins.Add(
new ShaderClassSource(
"LightMultiDirectionalShadingDiffusePerPixel", permutationKey.PerPixelDiffuseDirectionalLightCount));
70 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingDiffusePerPixelKeys.LightColorsWithGamma, ParameterDynamicValue.New(LightingPermutation.Key,
72 LightColorsUpdate(lightPermutation.PerPixelDiffuseDirectionalLights, lightColorsWithGamma)
73 , autoCheckDependencies:
false));
76 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingDiffusePerPixelKeys.LightDirectionsVS, ParameterDynamicValue.New(LightingPermutation.Key, TransformationKeys.View, (ref
LightingPermutation lightPermutation, ref
Matrix view, ref
Vector3[] lightDirectionVS) =>
79 foreach (var lightBinding
in lightPermutation.PerPixelDiffuseDirectionalLights)
82 LightKeys.LightDirectionVSUpdate(ref lightDirection, ref view, ref lightDirectionVS[index++]);
84 }, autoCheckDependencies:
false));
87 if (permutationKey.PerVertexDirectionalLightCount > 0)
89 effectPass.Shader.Mixins.Add(
new ShaderClassSource(
"LightMultiDirectionalShadingPerVertex", permutationKey.PerVertexDirectionalLightCount));
92 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingPerVertexKeys.LightColorsWithGamma, ParameterDynamicValue.New(LightingPermutation.Key,
94 LightColorsUpdate(lightPermutation.PerVertexDirectionalLights, lightColorsWithGamma)
95 , autoCheckDependencies:
false));
98 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingPerVertexKeys.LightDirectionsWS, ParameterDynamicValue.New(LightingPermutation.Key, (ref
LightingPermutation lightPermutation, ref
Vector3[] lightDirectionWS) =>
101 foreach (var lightBinding
in lightPermutation.PerVertexDirectionalLights)
103 lightDirectionWS[index++] = ((
DirectionalLight)lightBinding.Light).LightDirection;
105 }, autoCheckDependencies:
false));
108 if (permutationKey.PerVertexDiffusePixelSpecularDirectionalLightCount > 0)
110 effectPass.Shader.Mixins.Add(
new ShaderClassSource(
"LightMultiDirectionalShadingSpecularPerPixel", permutationKey.PerVertexDiffusePixelSpecularDirectionalLightCount));
113 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingSpecularPerPixelKeys.LightColorsWithGamma, ParameterDynamicValue.New(LightingPermutation.Key,
115 LightColorsUpdate(lightPermutation.PerVertexDiffusePixelSpecularDirectionalLights, lightColorsWithGamma)
116 , autoCheckDependencies:
false));
119 effectPass.Parameters.AddDynamic(LightMultiDirectionalShadingSpecularPerPixelKeys.LightDirectionsWS, ParameterDynamicValue.New(LightingPermutation.Key, (ref
LightingPermutation lightPermutation, ref
Vector3[] lightDirectionWS) =>
122 foreach (var lightBinding
in lightPermutation.PerVertexDiffusePixelSpecularDirectionalLights)
124 lightDirectionWS[index++] = ((
DirectionalLight)lightBinding.Light).LightDirection;
126 }, autoCheckDependencies:
false));
130 private static void LightColorsUpdate(
LightBinding[] lightBindings,
Color3[] lightColorsWithGamma)
133 foreach (var lightBinding
in lightBindings)
135 var lightColor = lightBinding.Light.LightColor;
136 lightColor.Pow(Color.DefaultGamma);
137 lightColorsWithGamma[index++] = lightColor;
143 var shadowMapPermutation = (
ShadowMap)permutationKey;
147 for (
int i = 0; i < shadowMapPermutation.LevelCount; ++i)
150 var shaderPass =
new EffectShaderPass(shadowMapPermutation.Passes[i],
"ShadowMap");
151 shaderPass.Parameters.AddSources(shadowMapPermutation.Passes[i].Parameters);
153 shaderPass.Macros.AddRange(mainShaderMacros);
154 shaderPass.Shader.Mixins.Add(shaderCaster);
155 yield
return shaderPass;
163 if (currentShadowMapsPermutation.ShadowMaps.Count == 0)
167 effectPass.Shader.Mixins.Add(
"ShadowMapReceiver");
168 effectPass.Shader.Compositions.Add(
"shadows", shadowsArray);
172 var shadowMapTypes = currentShadowMapsPermutation.ShadowMaps.GroupBy(x => Tuple.Create(x.ShadowMap.Filter.GetType(), x.ShadowMap.LevelCount, x.ShadowMap.Texture));
174 int shadowMapTypeIndex = 0;
175 foreach (var shadowMapType
in shadowMapTypes)
177 var shadowMapTypeCopy = shadowMapType.ToArray();
181 var maxShadowMapCount = shadowMapTypeCopy.Length;
184 shadowMixin.Mixins.Add(
new ShaderClassSource(
"ShadowMapCascadeBase", shadowMapType.Key.Item2, 0, maxShadowMapCount, 1));
191 shadowsArray.Add(shadowMixin);
194 var shadowSubKey = string.Format(
".shadows[{0}]", shadowMapTypeIndex++);
196 effectPass.Parameters.Set(ShadowMapKeys.Texture.AppendKey(shadowSubKey), shadowMapType.Key.Item3);
198 effectPass.Parameters.Set(LightingPlugin.ShadowMapLightCount.AppendKey(shadowSubKey), shadowMapTypeCopy.Length);
204 for (int i = 0; i < shadowMapTypeCopy.Length; ++i)
206 var permutationParameters = shadowMapTypeCopy[i].ShadowMap.Parameters;
209 var shadowMapData = permutationParameters.Get(LightingPlugin.ViewProjectionArray);
210 var textureCoords = permutationParameters.Get(LightingPlugin.CascadeTextureCoordsBorder);
211 var distanceMax = permutationParameters.Get(ShadowMapKeys.DistanceMax);
212 var lightDirection = permutationParameters.Get(LightKeys.LightDirection);
214 Matrix* vpPtr = &shadowMapData.ViewProjReceiver0;
215 fixed (Matrix* wvpPtr = &output[i].WorldViewProjReceiver0)
217 for (int j = 0; j < 4; ++j)
218 Matrix.Multiply(ref world, ref vpPtr[j], ref wvpPtr[j]);
221 output[i].Offset0 = shadowMapData.Offset0;
222 output[i].Offset1 = shadowMapData.Offset1;
223 output[i].Offset2 = shadowMapData.Offset2;
224 output[i].Offset3 = shadowMapData.Offset3;
226 fixed (Vector4* targetPtr = &output[i].CascadeTextureCoordsBorder0)
227 fixed (Vector4* sourcePtr = &textureCoords[0])
229 for (int j = 0; j < 4; ++j)
230 targetPtr[j] = sourcePtr[j];
233 output[i].ShadowLightDirection = lightDirection;
234 LightKeys.LightDirectionVSUpdate(ref output[i].ShadowLightDirection, ref viewProj, ref output[i].ShadowLightDirectionVS);
235 output[i].ShadowMapDistance = distanceMax;
236 output[i].ShadowLightColor = shadowMapData.LightColor;
239 }, autoCheckDependencies:
false));
246 for (int i = 0; i < shadowMapTypeCopy.Length; ++i)
248 var permutationParameters = shadowMapTypeCopy[i].ShadowMap.Parameters;
249 output[i].BleedingFactor = permutationParameters.Get(ShadowMapFilterVsm.VsmBleedingFactor);
250 output[i].MinVariance = permutationParameters.Get(ShadowMapFilterVsm.VsmMinVariance);
252 }, autoCheckDependencies:
false));
static ParameterKey< ShadowMapPermutationArray > Key
A mixin performing a combination of ShaderClassSource and other mixins.
Represents a color in the form of rgb.
abstract ShaderClassSource GenerateShaderSource(int shadowMapCount)
Represents a three dimensional mathematical vector.
void SetupLighting(EffectShaderPass effectPass, object permutationKeyObject)
override void SetupPasses(EffectMesh effectMesh)
Base class for ParameterDynamicValue{T}.
Level10 render pass using a depth buffer and a render target.
An array of ShaderSource used only in shader mixin compositions.
void SetupShadersPermutationReceiver(EffectShaderPass effectPass, object permutationKey)
IEnumerable< EffectShaderPass > SetupShadersPermutationCaster(object permutationKey)
override void SetupShaders(EffectMesh effectMesh)
A shader class used for mixin.
Represents a 4x4 mathematical matrix.