start removing global render procedures

instancing
cosmonaut 2020-12-07 01:57:57 -08:00
parent ee8b0c5ee8
commit 8b43e8f45e
1 changed files with 11 additions and 173 deletions

View File

@ -33,14 +33,8 @@ namespace Kav
private SkyboxEffect SkyboxEffect { get; } private SkyboxEffect SkyboxEffect { get; }
private DiffuseLitSpriteEffect DiffuseLitSpriteEffect { get; } private DiffuseLitSpriteEffect DiffuseLitSpriteEffect { get; }
private RenderTarget2D GPosition { get; }
private RenderTarget2D GNormal { get; }
private RenderTarget2D GAlbedo { get; }
private RenderTarget2D GMetallicRoughness { get; }
private RenderTargetCube PointShadowCubeMap { get; } private RenderTargetCube PointShadowCubeMap { get; }
private RenderTargetBinding[] GBuffer { get; }
private Kav.Model UnitCube { get; } private Kav.Model UnitCube { get; }
private SpriteBatch SpriteBatch { get; } private SpriteBatch SpriteBatch { get; }
@ -85,49 +79,6 @@ namespace Kav
RenderTargetUsage.PreserveContents 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)
};
PointShadowCubeMap = new RenderTargetCube( PointShadowCubeMap = new RenderTargetCube(
GraphicsDevice, GraphicsDevice,
shadowMapSize, shadowMapSize,
@ -172,126 +123,6 @@ namespace Kav
); );
} }
public void DeferredRender(
RenderTarget2D renderTarget,
PerspectiveCamera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
AmbientLight ambientLight,
IEnumerable<PointLight> pointLights,
DirectionalLight? directionalLight
) {
GBufferRender(GBuffer, camera, modelTransforms);
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
GraphicsDevice.Clear(Color.Black);
AmbientLightRender(
ColorRenderTarget,
GPosition,
GAlbedo,
ambientLight
);
DeferredPointLightEffect.EyePosition = camera.Position;
foreach (var pointLight in pointLights)
{
PointLightRender(
ColorRenderTarget,
GPosition,
GAlbedo,
GNormal,
GMetallicRoughness,
camera,
modelTransforms,
pointLight
);
}
if (directionalLight.HasValue)
{
DirectionalLightRender(
ColorRenderTarget,
GPosition,
GAlbedo,
GNormal,
GMetallicRoughness,
camera,
modelTransforms,
directionalLight.Value,
NumShadowCascades
);
}
GraphicsDevice.SetRenderTarget(renderTarget);
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, ToneMapEffect);
SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White);
SpriteBatch.End();
}
public void DeferredToonRender(
RenderTarget2D renderTarget,
PerspectiveCamera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
AmbientLight ambientLight,
IEnumerable<PointLight> pointLights,
DirectionalLight? directionalLight,
TextureCube skybox
) {
GBufferRender(GBuffer, camera, modelTransforms);
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
DepthRender(camera, modelTransforms);
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
AmbientLightRender(
ColorRenderTarget,
GPosition,
GAlbedo,
ambientLight
);
foreach (var pointLight in pointLights)
{
PointLightRender(
ColorRenderTarget,
GPosition,
GAlbedo,
GNormal,
GMetallicRoughness,
camera,
modelTransforms,
pointLight
);
}
if (directionalLight.HasValue)
{
DirectionalLightToonRender(
ColorRenderTarget,
GPosition,
GAlbedo,
GNormal,
GMetallicRoughness,
camera,
modelTransforms,
directionalLight.Value,
NumShadowCascades,
false
);
}
SkyboxRender(ColorRenderTarget, camera, skybox);
GraphicsDevice.SetRenderTarget(renderTarget);
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, null);
SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White);
SpriteBatch.End();
}
// TODO: we could make this a lot more efficient probably // TODO: we could make this a lot more efficient probably
// draws mesh sprites with a forward rendered diffuse lighting technique // draws mesh sprites with a forward rendered diffuse lighting technique
public void MeshSpriteRender( public void MeshSpriteRender(
@ -307,7 +138,7 @@ namespace Kav
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0); GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.DepthStencilState = DepthStencilState.Default;
DepthRender(camera, modelTransforms); DepthRender(ColorRenderTarget, camera, modelTransforms);
GraphicsDevice.Clear(ClearOptions.Target, new Color(0, 0, 0, 0), 1f, 0); GraphicsDevice.Clear(ClearOptions.Target, new Color(0, 0, 0, 0), 1f, 0);
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
@ -401,10 +232,14 @@ namespace Kav
SpriteBatch.End(); SpriteBatch.End();
} }
private void DepthRender( public void DepthRender(
RenderTarget2D renderTarget,
PerspectiveCamera camera, PerspectiveCamera camera,
IEnumerable<(Model, Matrix)> modelTransforms IEnumerable<(Model, Matrix)> modelTransforms
) { ) {
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms)) foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms))
@ -438,7 +273,7 @@ namespace Kav
} }
} }
private void SkyboxRender( public void SkyboxRender(
RenderTarget2D renderTarget, RenderTarget2D renderTarget,
PerspectiveCamera camera, PerspectiveCamera camera,
TextureCube skybox TextureCube skybox
@ -586,6 +421,7 @@ namespace Kav
) { ) {
GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
DeferredAmbientLightEffect.GPosition = gPosition; DeferredAmbientLightEffect.GPosition = gPosition;
DeferredAmbientLightEffect.GAlbedo = gAlbedo; DeferredAmbientLightEffect.GAlbedo = gAlbedo;
@ -621,6 +457,8 @@ namespace Kav
DeferredPointLightEffect.GMetallicRoughness = gMetallicRoughness; DeferredPointLightEffect.GMetallicRoughness = gMetallicRoughness;
DeferredPointLightEffect.ShadowMap = PointShadowCubeMap; DeferredPointLightEffect.ShadowMap = PointShadowCubeMap;
DeferredPointLightEffect.EyePosition = camera.Position;
DeferredPointLightEffect.PointLightPosition = pointLight.Position; DeferredPointLightEffect.PointLightPosition = pointLight.Position;
DeferredPointLightEffect.PointLightColor = DeferredPointLightEffect.PointLightColor =
pointLight.Color.ToVector3() * pointLight.Intensity; pointLight.Color.ToVector3() * pointLight.Intensity;
@ -686,7 +524,7 @@ namespace Kav
} }
} }
private void DirectionalLightToonRender( public void DirectionalLightToonRender(
RenderTarget2D renderTarget, RenderTarget2D renderTarget,
Texture2D gPosition, Texture2D gPosition,
Texture2D gAlbedo, Texture2D gAlbedo,