4 using System.Collections.Generic;
6 using System.Runtime.InteropServices;
7 using SiliconStudio.Paradox.Effects.Data;
8 using SiliconStudio.Paradox.Effects.Modules;
9 using SiliconStudio.Paradox.Graphics;
10 using SiliconStudio.Core.Mathematics;
11 using SiliconStudio.Paradox.Shaders;
16 namespace SiliconStudio.
Paradox.Effects
18 internal class LightingPrepassShaderPlugin : ShaderPlugin<LightingPrepassPlugin>
20 public static readonly ParameterKey<LightData> LightInfos = ParameterKeys.ArrayValue(
new LightData[64]);
21 public static readonly ParameterKey<int> LightCount = LightPrepassKeys.LightCount;
22 public static readonly ParameterKey<int> TileIndex = LightPrepassKeys.TileIndex;
25 private static LightData[] currentTiles;
28 public LightingPrepassShaderPlugin()
32 public LightingPrepassShaderPlugin(
string name)
37 public bool Debug {
get; set; }
39 public override void SetupShaders(EffectMesh effectMesh)
42 var lightPrepassShader =
new ShaderClassSource(
string.Format(
"LightPrepass{0}", Debug ?
"Debug" :
string.Empty));
43 DefaultShaderPass.Shader.Mixins.Add(lightPrepassShader);
44 DefaultShaderPass.Shader.Compositions.Add(
"DiffuseColor",
new ShaderClassSource(
"ComputeBRDFColorFresnel"));
45 DefaultShaderPass.Shader.Compositions.Add(
"DiffuseLighting",
new ShaderClassSource(
"ComputeBRDFDiffuseLambert"));
46 DefaultShaderPass.Shader.Compositions.Add(
"SpecularColor",
new ShaderClassSource(
"ComputeBRDFColor"));
47 DefaultShaderPass.Shader.Compositions.Add(
"SpecularLighting",
new ShaderClassSource(
"ComputeBRDFColorSpecularBlinnPhong"));
50 public override void SetupResources(EffectMesh effectMesh)
53 blendStateDesc.SetDefaults();
54 blendStateDesc.AlphaToCoverageEnable =
false;
55 blendStateDesc.IndependentBlendEnable =
false;
56 blendStateDesc.RenderTargets[0].BlendEnable =
true;
58 blendStateDesc.RenderTargets[0].AlphaBlendFunction = BlendFunction.Add;
59 blendStateDesc.RenderTargets[0].AlphaSourceBlend = Blend.One;
60 blendStateDesc.RenderTargets[0].AlphaDestinationBlend = Blend.One;
62 blendStateDesc.RenderTargets[0].ColorBlendFunction = BlendFunction.Add;
63 blendStateDesc.RenderTargets[0].ColorSourceBlend = Blend.One;
64 blendStateDesc.RenderTargets[0].ColorDestinationBlend = Blend.One;
69 Effect.Parameters.Set(BlendStateKey, BlendState.New(
GraphicsDevice, blendStateDesc));
74 rasterizer.Name =
"LightPrePassWireFrame";
75 Effect.Parameters.Set(RasterizerStateKey, rasterizer);
78 Effect.PrepareMesh += SetupMeshResources;
79 Effect.UpdateMeshData += UpdateMeshResources;
82 private void SetupMeshResources(EffectOld effect, EffectMesh effectMesh)
94 effectMesh.MeshData.Draw =
new MeshDraw
105 private void UpdateMeshResources(EffectOld effect, EffectMesh effectMesh)
107 var oldStartPass = effectMesh.Render;
111 effectMesh.Render.Set = (context) =>
113 int tileIndex = context.Parameters.Get(TileIndex);
114 var tiles = RenderPassPlugin.Tiles[tileIndex];
116 var mainParameters = RenderPassPlugin.GBufferPlugin.MainPlugin.ViewParameters;
119 mainParameters.Get(TransformationKeys.Projection, out projMatrix);
120 context.Parameters.Set(TransformationKeys.Projection, projMatrix);
123 context.Parameters.Set(RenderTargetKeys.DepthStencilSource, RenderPassPlugin.GBufferPlugin.DepthStencil.Texture);
125 if (currentTiles == null)
126 currentTiles =
new LightData[LightingPrepassPlugin.MaxLightsPerTileDrawCall];
130 for (
int i = 0; i < (tiles.Count + LightingPrepassPlugin.MaxLightsPerTileDrawCall - 1) / LightingPrepassPlugin.MaxLightsPerTileDrawCall; ++i)
132 int lightCount = Math.Min(tiles.Count - i * LightingPrepassPlugin.MaxLightsPerTileDrawCall, LightingPrepassPlugin.MaxLightsPerTileDrawCall);
136 var startLightIndex = i * LightingPrepassPlugin.MaxLightsPerTileDrawCall;
137 for (
int lightIndex = 0; lightIndex < lightCount; ++lightIndex)
138 currentTiles[lightIndex] =
RenderPassPlugin.Tiles[tileIndex][startLightIndex + lightIndex];
139 context.Parameters.Set(LightCount, lightCount);
140 context.Parameters.Set(LightInfos, currentTiles);
143 oldStartPass.Invoke(context);
148 [StructLayout(LayoutKind.Sequential, Pack = 4)]
The layout of a vertex buffer with a set of VertexElement.
Represents a two dimensional mathematical vector.
Describes a rasterizer state.
Represents a color in the form of rgb.
Represents a three dimensional mathematical vector.
All-in-One Buffer class linked SharpDX.Direct3D11.Buffer.
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.
static Buffer New(GraphicsDevice device, BufferDescription description, PixelFormat viewFormat=PixelFormat.None)
Creates a new Buffer instance.
static VertexElement Position(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "POSITION".
ColorWriteChannels
Identify which components of each pixel of a render target are writable during blending.
SiliconStudio.Paradox.Graphics.PrimitiveType PrimitiveType
PrimitiveType
Defines how vertex data is ordered.
SiliconStudio.Paradox.Graphics.Buffer Buffer
A description of a single element for the input-assembler stage. This structure is related to Direct3...
A shader class used for mixin.
Binding structure that specifies a vertex buffer and other per-vertex parameters (such as offset and ...
Represents a 4x4 mathematical matrix.