From fe222e266ffc296ffacbed82dccecedb45204537 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 7 Dec 2020 18:46:53 -0800 Subject: [PATCH] started instance based depth render --- Renderer.cs | 57 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/Renderer.cs b/Renderer.cs index 05b2ab1..e3d2820 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -128,18 +128,12 @@ namespace Kav public void MeshSpriteRender( RenderTarget2D renderTarget, PerspectiveCamera camera, - IEnumerable<(Model, Matrix)> modelTransforms, IEnumerable<(MeshSprite, Matrix)> meshSpriteTransforms, AmbientLight ambientLight, IEnumerable pointLights, DirectionalLight? directionalLight ) { - GraphicsDevice.SetRenderTarget(ColorRenderTarget); - 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.SetRenderTarget(renderTarget); GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; 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( @@ -248,7 +236,7 @@ namespace Kav { foreach (var meshPart in modelMesh.MeshParts) { - SimpleDepthEffect.Model = transform; + SimpleDepthEffect.World = transform; SimpleDepthEffect.View = camera.View; SimpleDepthEffect.Projection = camera.Projection; @@ -280,6 +268,8 @@ namespace Kav ) { GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.RasterizerState.CullMode = CullMode.CullClockwiseFace; + GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; + SkyboxEffect.Skybox = skybox; var view = camera.View; @@ -312,6 +302,7 @@ namespace Kav /// public void InstancedGBufferRender( RenderTargetBinding[] gBuffer, + RenderTarget2D depthBuffer, PerspectiveCamera camera, T drawable, IEnumerable transforms @@ -339,7 +330,6 @@ namespace Kav foreach (var transform in FrustumCull(boundingFrustum, drawable, transforms)) { - GBufferInstanceVertices[numInstances].World = transform; numInstances += 1; } @@ -372,6 +362,41 @@ namespace Kav 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( @@ -694,7 +719,7 @@ namespace Kav GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); GraphicsDevice.Indices = meshPart.IndexBuffer; - SimpleDepthEffect.Model = transform; + SimpleDepthEffect.World = transform; foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes) {