Kav/Renderer.cs

457 lines
18 KiB
C#
Raw Normal View History

2020-08-04 09:32:02 +00:00
using System.Collections.Generic;
2020-08-07 00:58:50 +00:00
using Microsoft.Xna.Framework;
2020-08-04 09:32:02 +00:00
using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
2020-08-07 00:58:50 +00:00
public class Renderer
2020-08-04 09:32:02 +00:00
{
2020-09-18 11:00:53 +00:00
private const int MAX_SHADOW_CASCADES = 4;
2020-08-07 00:58:50 +00:00
private GraphicsDevice GraphicsDevice { get; }
private int RenderDimensionsX { get; }
private int RenderDimensionsY { get; }
2020-09-17 22:47:23 +00:00
private VertexBuffer FullscreenTriangle { get; }
2020-09-18 11:00:53 +00:00
private int NumShadowCascades { get; }
private RenderTarget2D ColorRenderTarget { get; }
private RenderTarget2D DirectionalRenderTarget { get; }
2020-09-18 11:00:53 +00:00
private RenderTarget2D[] ShadowRenderTargets { get; }
2020-09-17 22:47:23 +00:00
private DeferredPBREffect DeferredPBREffect { get; }
private DeferredPBR_AmbientLightEffect DeferredAmbientLightEffect { get; }
private DeferredPBR_PointLightEffect DeferredPointLightEffect { get; }
private DeferredPBR_DirectionalLightEffect DeferredDirectionalLightEffect { get; }
2020-08-07 00:58:50 +00:00
private SimpleDepthEffect SimpleDepthEffect { get; }
private RenderTarget2D gPosition { get; }
private RenderTarget2D gNormal { get; }
private RenderTarget2D gAlbedo { get; }
private RenderTarget2D gMetallicRoughness { get; }
private RenderTargetBinding[] GBuffer { get; }
private SpriteBatch SpriteBatch { get; }
2020-09-18 11:00:53 +00:00
public Renderer(GraphicsDevice graphicsDevice, int renderDimensionsX, int renderDimensionsY, int numShadowCascades)
2020-08-07 00:58:50 +00:00
{
GraphicsDevice = graphicsDevice;
RenderDimensionsX = renderDimensionsX;
RenderDimensionsY = renderDimensionsY;
2020-09-18 11:00:53 +00:00
NumShadowCascades = (int)MathHelper.Clamp(numShadowCascades, 1, MAX_SHADOW_CASCADES);
ShadowRenderTargets = new RenderTarget2D[numShadowCascades];
for (var i = 0; i < numShadowCascades; i++)
{
ShadowRenderTargets[i] = new RenderTarget2D(
GraphicsDevice,
1024,
1024,
false,
SurfaceFormat.Single,
DepthFormat.Depth24
);
}
2020-08-07 00:58:50 +00:00
ColorRenderTarget = new RenderTarget2D(
graphicsDevice,
renderDimensionsX,
renderDimensionsY,
false,
SurfaceFormat.Color,
DepthFormat.None,
0,
RenderTargetUsage.PreserveContents
);
DirectionalRenderTarget = new RenderTarget2D(
graphicsDevice,
renderDimensionsX,
renderDimensionsY,
false,
SurfaceFormat.Color,
DepthFormat.None,
0,
RenderTargetUsage.PreserveContents
);
gPosition = new RenderTarget2D(
GraphicsDevice,
renderDimensionsX,
renderDimensionsY,
false,
SurfaceFormat.Vector4,
DepthFormat.Depth24
);
gNormal = new RenderTarget2D(
GraphicsDevice,
renderDimensionsX,
renderDimensionsY,
false,
SurfaceFormat.Vector4,
DepthFormat.None
);
gAlbedo = new RenderTarget2D(
GraphicsDevice,
renderDimensionsX,
renderDimensionsY,
false,
SurfaceFormat.Color,
DepthFormat.None
);
gMetallicRoughness = new RenderTarget2D(
GraphicsDevice,
renderDimensionsX,
renderDimensionsY,
false,
SurfaceFormat.HalfVector2,
DepthFormat.None
);
GBuffer = new RenderTargetBinding[4] {
new RenderTargetBinding(gPosition),
new RenderTargetBinding(gNormal),
new RenderTargetBinding(gAlbedo),
new RenderTargetBinding(gMetallicRoughness)
};
2020-08-07 00:58:50 +00:00
SimpleDepthEffect = new SimpleDepthEffect(GraphicsDevice);
DeferredPBREffect = new DeferredPBREffect(GraphicsDevice);
DeferredAmbientLightEffect = new DeferredPBR_AmbientLightEffect(GraphicsDevice);
DeferredPointLightEffect = new DeferredPBR_PointLightEffect(GraphicsDevice);
DeferredDirectionalLightEffect = new DeferredPBR_DirectionalLightEffect(GraphicsDevice);
2020-09-17 22:47:23 +00:00
FullscreenTriangle = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 3, BufferUsage.WriteOnly);
FullscreenTriangle.SetData(new VertexPositionTexture[3] {
new VertexPositionTexture(new Vector3(-1, -3, 0), new Vector2(0, 2)),
new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(3, 1, 0), new Vector2(2, 0))
2020-09-17 22:47:23 +00:00
});
SpriteBatch = new SpriteBatch(graphicsDevice);
}
public void DeferredRender(
2020-09-18 11:00:53 +00:00
PerspectiveCamera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
IEnumerable<PointLight> pointLights,
2020-09-18 11:00:53 +00:00
DirectionalLight directionalLight
) {
// g-buffer pass
2020-08-27 21:46:20 +00:00
GraphicsDevice.SetRenderTargets(GBuffer);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Opaque;
foreach (var (model, transform) in modelTransforms)
{
foreach (var modelMesh in model.Meshes)
{
foreach (var meshPart in modelMesh.MeshParts)
{
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
GraphicsDevice.Indices = meshPart.IndexBuffer;
if (meshPart.Effect is TransformEffect transformEffect)
{
transformEffect.World = transform;
transformEffect.View = camera.View;
transformEffect.Projection = camera.Projection;
}
foreach (var pass in meshPart.Effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
meshPart.VertexBuffer.VertexCount,
0,
meshPart.Triangles.Length
);
}
}
}
}
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
GraphicsDevice.Clear(Color.Black);
GraphicsDevice.BlendState = BlendState.Additive;
GraphicsDevice.DepthStencilState = DepthStencilState.None;
DeferredAmbientLightEffect.GPosition = gPosition;
DeferredAmbientLightEffect.GAlbedo = gAlbedo;
foreach (var pass in DeferredAmbientLightEffect.CurrentTechnique.Passes)
2020-09-18 11:00:53 +00:00
{
pass.Apply();
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
2020-09-18 11:00:53 +00:00
}
DeferredPointLightEffect.EyePosition = camera.Position;
foreach (var pointLight in pointLights)
{
PointLightRender(pointLight);
}
DirectionalLightRender(camera, modelTransforms, directionalLight);
// return;
// GraphicsDevice.SetRenderTarget(null);
// SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
// SpriteBatch.Draw(DirectionalRenderTarget, Vector2.Zero, Color.White);
// SpriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White);
SpriteBatch.End();
}
private void PointLightRender(PointLight pointLight)
{
DeferredPointLightEffect.GPosition = gPosition;
DeferredPointLightEffect.GAlbedo = gAlbedo;
DeferredPointLightEffect.GNormal = gNormal;
DeferredPointLightEffect.GMetallicRoughness = gMetallicRoughness;
DeferredPointLightEffect.PointLightPosition = pointLight.Position;
DeferredPointLightEffect.PointLightColor =
pointLight.Color.ToVector3() * pointLight.Intensity;
2020-09-17 22:47:23 +00:00
foreach (var pass in DeferredPointLightEffect.CurrentTechnique.Passes)
2020-09-17 22:47:23 +00:00
{
pass.Apply();
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}
2020-08-07 00:58:50 +00:00
}
private void DirectionalLightRender(
PerspectiveCamera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
2020-09-18 11:00:53 +00:00
DirectionalLight directionalLight
2020-09-17 22:47:23 +00:00
) {
// render the individual shadow cascades
2020-09-19 05:43:12 +00:00
var previousFarPlane = camera.NearPlane;
2020-09-18 11:00:53 +00:00
for (var i = 0; i < NumShadowCascades; i++)
{
2020-09-19 05:43:12 +00:00
var farPlane = camera.FarPlane / (MathHelper.Max((NumShadowCascades - i - 1) * 5f, 1f));
2020-09-18 11:00:53 +00:00
// divide the view frustum
var shadowCamera = new PerspectiveCamera(
camera.Position,
camera.Forward,
camera.Up,
camera.FieldOfView,
camera.AspectRatio,
2020-09-19 05:43:12 +00:00
previousFarPlane,
farPlane
2020-09-18 11:00:53 +00:00
);
// TODO: This is tightly coupled to the effect and it sucks
2020-09-18 11:00:53 +00:00
RenderShadowMap(shadowCamera, modelTransforms, directionalLight, i);
2020-09-19 05:43:12 +00:00
previousFarPlane = farPlane;
2020-09-17 22:47:23 +00:00
}
DeferredDirectionalLightEffect.GPosition = gPosition;
DeferredDirectionalLightEffect.GAlbedo = gAlbedo;
DeferredDirectionalLightEffect.GNormal = gNormal;
DeferredDirectionalLightEffect.GMetallicRoughness = gMetallicRoughness;
DeferredDirectionalLightEffect.ShadowMapOne = ShadowRenderTargets[0];
if (NumShadowCascades > 1)
{
DeferredDirectionalLightEffect.ShadowMapTwo = ShadowRenderTargets[1];
}
if (NumShadowCascades > 2)
{
DeferredDirectionalLightEffect.ShadowMapThree = ShadowRenderTargets[2];
}
if (NumShadowCascades > 3)
{
DeferredDirectionalLightEffect.ShadowMapFour = ShadowRenderTargets[3];
}
DeferredDirectionalLightEffect.DirectionalLightDirection = directionalLight.Direction;
DeferredDirectionalLightEffect.DirectionalLightColor =
directionalLight.Color.ToVector3() * directionalLight.Intensity;
DeferredDirectionalLightEffect.ViewMatrix = camera.View;
DeferredDirectionalLightEffect.EyePosition = Matrix.Invert(camera.View).Translation;
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
GraphicsDevice.BlendState = BlendState.Additive;
foreach (EffectPass pass in DeferredDirectionalLightEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}
2020-09-17 22:47:23 +00:00
}
2020-09-18 11:00:53 +00:00
private void RenderShadowMap(
PerspectiveCamera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
DirectionalLight directionalLight,
int shadowCascadeIndex
) {
GraphicsDevice.SetRenderTarget(ShadowRenderTargets[shadowCascadeIndex]);
GraphicsDevice.Clear(Color.White);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Opaque;
2020-09-19 05:43:12 +00:00
2020-09-17 22:47:23 +00:00
var cameraBoundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
Vector3[] frustumCorners = cameraBoundingFrustum.GetCorners();
Vector3 frustumCenter = Vector3.Zero;
for (var i = 0; i < frustumCorners.Length; i++)
{
frustumCenter += frustumCorners[i];
}
frustumCenter /= 8f;
2020-09-19 05:43:12 +00:00
var lightView = Matrix.CreateLookAt(frustumCenter + directionalLight.Direction, frustumCenter, Vector3.Backward);
2020-09-17 22:47:23 +00:00
for (var i = 0; i < frustumCorners.Length; i++)
{
frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightView);
}
BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners);
2020-09-19 05:43:12 +00:00
SimpleDepthEffect.View = lightView;
2020-09-17 22:47:23 +00:00
SimpleDepthEffect.Projection = Matrix.CreateOrthographicOffCenter(
lightBox.Min.X,
lightBox.Max.X,
lightBox.Min.Y,
lightBox.Max.Y,
2020-09-19 05:43:12 +00:00
-lightBox.Max.Z - 10f, // TODO: near clip plane needs scene AABB info to get rid of this magic value
-lightBox.Min.Z
2020-09-17 22:47:23 +00:00
);
2020-09-18 11:00:53 +00:00
var lightSpaceMatrix = SimpleDepthEffect.View * SimpleDepthEffect.Projection;
if (shadowCascadeIndex == 0)
{
DeferredDirectionalLightEffect.LightSpaceMatrixOne = lightSpaceMatrix;
2020-09-18 11:00:53 +00:00
}
else if (shadowCascadeIndex == 1)
{
DeferredDirectionalLightEffect.LightSpaceMatrixTwo = lightSpaceMatrix;
2020-09-18 11:00:53 +00:00
}
else if (shadowCascadeIndex == 2)
{
DeferredDirectionalLightEffect.LightSpaceMatrixThree = lightSpaceMatrix;
2020-09-18 11:00:53 +00:00
}
else if (shadowCascadeIndex == 3)
{
DeferredDirectionalLightEffect.LightSpaceMatrixFour = lightSpaceMatrix;
2020-09-18 11:00:53 +00:00
}
DeferredDirectionalLightEffect.CascadeFarPlanes[shadowCascadeIndex] = camera.FarPlane;
2020-09-18 11:00:53 +00:00
2020-08-07 00:58:50 +00:00
foreach (var (model, transform) in modelTransforms)
{
foreach (var modelMesh in model.Meshes)
{
foreach (var meshPart in modelMesh.MeshParts)
{
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
GraphicsDevice.Indices = meshPart.IndexBuffer;
SimpleDepthEffect.Model = transform;
2020-08-04 09:32:02 +00:00
2020-08-07 00:58:50 +00:00
foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
meshPart.VertexBuffer.VertexCount,
0,
meshPart.Triangles.Length
);
}
}
}
}
}
2020-08-27 21:46:20 +00:00
public void Render(
2020-09-18 11:00:53 +00:00
PerspectiveCamera camera,
2020-08-27 21:46:20 +00:00
IEnumerable<(Model, Matrix)> modelTransforms,
IEnumerable<PointLight> pointLights,
IEnumerable<DirectionalLight> directionalLights
) {
Render(camera.View, camera.Projection, modelTransforms, pointLights, directionalLights);
}
2020-08-07 08:12:46 +00:00
private void Render(
Matrix view,
Matrix projection,
IEnumerable<(Model, Matrix)> modelTransforms,
IEnumerable<PointLight> pointLights,
IEnumerable<DirectionalLight> directionalLights
) {
foreach (var (model, transform) in modelTransforms)
2020-08-04 09:32:02 +00:00
{
foreach (var modelMesh in model.Meshes)
{
foreach (var meshPart in modelMesh.MeshParts)
{
2020-08-07 00:58:50 +00:00
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
GraphicsDevice.Indices = meshPart.IndexBuffer;
2020-08-04 09:32:02 +00:00
if (meshPart.Effect is TransformEffect transformEffect)
{
2020-08-07 08:12:46 +00:00
transformEffect.World = transform;
2020-08-04 09:32:02 +00:00
transformEffect.View = view;
transformEffect.Projection = projection;
}
if (meshPart.Effect is PointLightEffect pointLightEffect)
{
2020-08-05 03:50:44 +00:00
int i = 0;
foreach (var pointLight in pointLights)
2020-08-04 09:32:02 +00:00
{
if (i > pointLightEffect.MaxPointLights) { break; }
2020-08-05 03:50:44 +00:00
pointLightEffect.PointLights[i] = pointLight;
i++;
2020-08-04 09:32:02 +00:00
}
}
foreach (var pass in meshPart.Effect.CurrentTechnique.Passes)
{
pass.Apply();
2020-08-07 00:58:50 +00:00
GraphicsDevice.DrawIndexedPrimitives(
2020-08-04 09:32:02 +00:00
PrimitiveType.TriangleList,
0,
0,
meshPart.VertexBuffer.VertexCount,
0,
meshPart.Triangles.Length
);
}
}
}
}
}
}
}