started instance based depth render

instancing
cosmonaut 2020-12-07 18:46:53 -08:00
parent bb694d3dbe
commit fe222e266f
1 changed files with 41 additions and 16 deletions

View File

@ -128,18 +128,12 @@ namespace Kav
public void MeshSpriteRender( public void MeshSpriteRender(
RenderTarget2D renderTarget, RenderTarget2D renderTarget,
PerspectiveCamera camera, PerspectiveCamera camera,
IEnumerable<(Model, Matrix)> modelTransforms,
IEnumerable<(MeshSprite, Matrix)> meshSpriteTransforms, IEnumerable<(MeshSprite, Matrix)> meshSpriteTransforms,
AmbientLight ambientLight, AmbientLight ambientLight,
IEnumerable<PointLight> pointLights, IEnumerable<PointLight> pointLights,
DirectionalLight? directionalLight DirectionalLight? directionalLight
) { ) {
GraphicsDevice.SetRenderTarget(ColorRenderTarget); GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
DepthRender(ColorRenderTarget, camera, modelTransforms);
GraphicsDevice.Clear(ClearOptions.Target, new Color(0, 0, 0, 0), 1f, 0);
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.RasterizerState = RasterizerState.CullNone;
@ -224,12 +218,6 @@ namespace Kav
); );
} }
} }
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(new Color(0, 0, 0, 0));
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null);
SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White);
SpriteBatch.End();
} }
public void DepthRender( public void DepthRender(
@ -248,7 +236,7 @@ namespace Kav
{ {
foreach (var meshPart in modelMesh.MeshParts) foreach (var meshPart in modelMesh.MeshParts)
{ {
SimpleDepthEffect.Model = transform; SimpleDepthEffect.World = transform;
SimpleDepthEffect.View = camera.View; SimpleDepthEffect.View = camera.View;
SimpleDepthEffect.Projection = camera.Projection; SimpleDepthEffect.Projection = camera.Projection;
@ -280,6 +268,8 @@ namespace Kav
) { ) {
GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.RasterizerState.CullMode = CullMode.CullClockwiseFace; GraphicsDevice.RasterizerState.CullMode = CullMode.CullClockwiseFace;
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
SkyboxEffect.Skybox = skybox; SkyboxEffect.Skybox = skybox;
var view = camera.View; var view = camera.View;
@ -312,6 +302,7 @@ namespace Kav
/// </summary> /// </summary>
public void InstancedGBufferRender<T>( public void InstancedGBufferRender<T>(
RenderTargetBinding[] gBuffer, RenderTargetBinding[] gBuffer,
RenderTarget2D depthBuffer,
PerspectiveCamera camera, PerspectiveCamera camera,
T drawable, T drawable,
IEnumerable<Matrix> transforms IEnumerable<Matrix> transforms
@ -339,7 +330,6 @@ namespace Kav
foreach (var transform in FrustumCull(boundingFrustum, drawable, transforms)) foreach (var transform in FrustumCull(boundingFrustum, drawable, transforms))
{ {
GBufferInstanceVertices[numInstances].World = transform; GBufferInstanceVertices[numInstances].World = transform;
numInstances += 1; numInstances += 1;
} }
@ -372,6 +362,41 @@ namespace Kav
numInstances numInstances
); );
} }
GraphicsDevice.SetRenderTarget(depthBuffer);
foreach (var pass in Deferred_GBufferEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawInstancedPrimitives(
PrimitiveType.TriangleList,
0,
0,
drawable.VertexBuffer.VertexCount,
0,
drawable.IndexBuffer.IndexCount / 3,
numInstances
);
}
//SimpleDepthEffect.HardwareInstancingEnabled = true;
//SimpleDepthEffect.View = camera.View;
//SimpleDepthEffect.Projection = camera.Projection;
//foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes)
//{
// pass.Apply();
// GraphicsDevice.DrawInstancedPrimitives(
// PrimitiveType.TriangleList,
// 0,
// 0,
// drawable.VertexBuffer.VertexCount,
// 0,
// drawable.IndexBuffer.IndexCount / 3,
// numInstances
// );
//}
} }
public void GBufferRender( public void GBufferRender(
@ -694,7 +719,7 @@ namespace Kav
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
GraphicsDevice.Indices = meshPart.IndexBuffer; GraphicsDevice.Indices = meshPart.IndexBuffer;
SimpleDepthEffect.Model = transform; SimpleDepthEffect.World = transform;
foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes) foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes)
{ {