instanced directional shadows

instancing
cosmonaut 2020-12-08 03:35:06 -08:00
parent 4b7d31f2b2
commit 14e07c7476
1 changed files with 54 additions and 7 deletions

View File

@ -31,7 +31,7 @@ namespace Kav
private Kav.Model UnitCube { get; } private Kav.Model UnitCube { get; }
private DynamicVertexBuffer PositionInstanceVertexBuffer { get; } private DynamicVertexBuffer PositionInstanceVertexBuffer { get; }
private readonly PositionInstanceVertex[] GBufferInstanceVertices = new PositionInstanceVertex[MAX_INSTANCE_VERTEX_COUNT]; private readonly PositionInstanceVertex[] PositionInstanceVertices = new PositionInstanceVertex[MAX_INSTANCE_VERTEX_COUNT];
public Renderer( public Renderer(
GraphicsDevice graphicsDevice GraphicsDevice graphicsDevice
@ -243,15 +243,13 @@ namespace Kav
public static int FillAndSetBuffersForInstancing<T, V>( public static int FillAndSetBuffersForInstancing<T, V>(
GraphicsDevice graphicsDevice, GraphicsDevice graphicsDevice,
PerspectiveCamera camera, BoundingFrustum boundingFrustum,
T drawable, T drawable,
IEnumerable<Matrix> transforms, IEnumerable<Matrix> transforms,
V[] vertexData, V[] vertexData,
DynamicVertexBuffer dynamicVertexBuffer DynamicVertexBuffer dynamicVertexBuffer
) where T : ICullable, IIndexDrawable where V : struct, IVertexType, IHasTranslation ) where T : ICullable, IIndexDrawable where V : struct, IVertexType, IHasTranslation
{ {
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
int numInstances = 0; int numInstances = 0;
foreach (var transform in FrustumCull(boundingFrustum, drawable, transforms)) foreach (var transform in FrustumCull(boundingFrustum, drawable, transforms))
{ {
@ -387,10 +385,10 @@ namespace Kav
var numInstances = FillAndSetBuffersForInstancing( var numInstances = FillAndSetBuffersForInstancing(
GraphicsDevice, GraphicsDevice,
camera, new BoundingFrustum(camera.View * camera.Projection),
drawable, drawable,
transforms, transforms,
GBufferInstanceVertices, PositionInstanceVertices,
PositionInstanceVertexBuffer PositionInstanceVertexBuffer
); );
@ -686,7 +684,7 @@ namespace Kav
} }
} }
public void RenderDirectionalShadowMapIndexed<T>( private void RenderDirectionalShadowMapIndexed<T>(
DirectionalShadowMapData shadowMapData, DirectionalShadowMapData shadowMapData,
int shadowCascadeIndex, int shadowCascadeIndex,
IEnumerable<(T, Matrix)> drawableTransforms IEnumerable<(T, Matrix)> drawableTransforms
@ -706,6 +704,55 @@ namespace Kav
); );
} }
public void RenderDirectionalShadowsInstanced<T>(
DirectionalShadowMapData shadowMapData,
T drawable,
IEnumerable<Matrix> transforms
) where T : ICullable, IIndexDrawable
{
// render the individual shadow cascades
for (var i = 0; i < shadowMapData.NumShadowCascades; i++)
{
RenderDirectionalShadowMapInstanced(
shadowMapData,
i,
drawable,
transforms
);
}
}
private void RenderDirectionalShadowMapInstanced<T>(
DirectionalShadowMapData shadowMapData,
int shadowCascadeIndex,
T drawable,
IEnumerable<Matrix> transforms
) where T : ICullable, IIndexDrawable
{
GraphicsDevice.SetRenderTarget(shadowMapData.ShadowMaps[shadowCascadeIndex]);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Opaque;
SimpleDepthEffectInstanced.View = shadowMapData.LightSpaceViews[shadowCascadeIndex];
SimpleDepthEffectInstanced.Projection = shadowMapData.LightSpaceProjections[shadowCascadeIndex];
var numInstances = FillAndSetBuffersForInstancing(
GraphicsDevice,
new BoundingFrustum(SimpleDepthEffectInstanced.View * SimpleDepthEffectInstanced.Projection),
drawable,
transforms,
PositionInstanceVertices,
PositionInstanceVertexBuffer
);
RenderInstanced(
GraphicsDevice,
drawable,
SimpleDepthEffectInstanced,
numInstances
);
}
public void RenderPointShadowMapIndexed<T>( public void RenderPointShadowMapIndexed<T>(
RenderTargetCube pointShadowCubeMap, RenderTargetCube pointShadowCubeMap,
PerspectiveCamera camera, PerspectiveCamera camera,