diff --git a/GlitchyEngine/src/Renderer/Renderer.bf b/GlitchyEngine/src/Renderer/Renderer.bf index 514fcf9..4cb551f 100644 --- a/GlitchyEngine/src/Renderer/Renderer.bf +++ b/GlitchyEngine/src/Renderer/Renderer.bf @@ -392,6 +392,9 @@ namespace GlitchyEngine.Renderer _lights.Clear(); + // TODO: I don't understand why we need to clear depth here. It should happen in SceneRenderer, but doesn't work for some reason... + RenderCommand.Clear(_sceneConstants.CompositionTarget, .Depth); + // Copy EntityIDs to compositionTarget _gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, int2.Zero, int2((int32)_sceneConstants.CameraTarget.Width, (int32)_sceneConstants.CameraTarget.Height), int2.Zero, 6); diff --git a/GlitchyEngine/src/Renderer/Renderer2D.bf b/GlitchyEngine/src/Renderer/Renderer2D.bf index 478eab6..5d3f27e 100644 --- a/GlitchyEngine/src/Renderer/Renderer2D.bf +++ b/GlitchyEngine/src/Renderer/Renderer2D.bf @@ -758,10 +758,14 @@ namespace GlitchyEngine.Renderer return; SortInstances(); + + RenderCommand.SetDepthStencilState(Renderer.[Friend]_meshDepthStencilState); DrawDeferredQuads(); DrawDeferredCircles(); DrawDeferredLines(); + + RenderCommand.SetDepthStencilState(Renderer.[Friend]_fullscreenDepthState); } private static void DrawDeferredQuads() diff --git a/GlitchyEngine/src/World/Components/Components.bf b/GlitchyEngine/src/World/Components/Components.bf index d7801cd..7d83ad5 100644 --- a/GlitchyEngine/src/World/Components/Components.bf +++ b/GlitchyEngine/src/World/Components/Components.bf @@ -274,15 +274,16 @@ namespace GlitchyEngine.World float halfWidth = halfHeight * _aspectRatio; _projection = Matrix.OrthographicProjectionOffCenter(-halfWidth, halfWidth, halfHeight, -halfHeight, - _orthographicNearPlane, _orthographicFarPlane); + // Swap far and near plane, because we use reversed depth! + _orthographicFarPlane, _orthographicNearPlane); } else if (_projectionType case .Perspective) { - _projection = Matrix.PerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane, _perspectiveFarPlane); + _projection = Matrix.ReversedPerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane, _perspectiveFarPlane); } else if (_projectionType case .InfinitePerspective) { - _projection = Matrix.InfinitePerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane); + _projection = Matrix.ReversedInfinitePerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane); } } } diff --git a/GlitchyEngine/src/World/EditorCamera.bf b/GlitchyEngine/src/World/EditorCamera.bf index 308357c..482b8b4 100644 --- a/GlitchyEngine/src/World/EditorCamera.bf +++ b/GlitchyEngine/src/World/EditorCamera.bf @@ -308,7 +308,7 @@ namespace GlitchyEngine.World private void UpdateProjection() mut { //_projection = Matrix.InfinitePerspectiveProjection(_fovY, _aspectRatio, _nearPlane); - _projection = Matrix.PerspectiveProjection(_fovY, _aspectRatio, _nearPlane, 10000); + _projection = Matrix.ReversedPerspectiveProjection(_fovY, _aspectRatio, _nearPlane, 10000); } public void OnViewportResize(uint32 sizeX, uint32 sizeY) mut diff --git a/GlitchyEngine/src/World/SceneRenderer.bf b/GlitchyEngine/src/World/SceneRenderer.bf index e310b01..eb5d7ea 100644 --- a/GlitchyEngine/src/World/SceneRenderer.bf +++ b/GlitchyEngine/src/World/SceneRenderer.bf @@ -1,6 +1,7 @@ using GlitchyEngine.Renderer; using GlitchyEngine.Content; using GlitchyEngine.Math; +using System; namespace GlitchyEngine.World; @@ -26,20 +27,23 @@ class SceneRenderer { RenderTargetGroupDescription desc = .(100, 100, TargetDescription[]( - RenderTargetFormat.R16G16B16A16_Float, - .(RenderTargetFormat.R32_UInt) {ClearColor = .UInt(uint32.MaxValue)}), - RenderTargetFormat.D24_UNorm_S8_UInt); + .(.R16G16B16A16_Float, ownDebugName: new String("Color")), + .(.R32_UInt) {ClearColor = .UInt(uint32.MaxValue), DebugName = new String("EntityId")} + ), + TargetDescription(.D24_UNorm_S8_UInt, clearColor: ClearColor.DepthStencil(0.0f, 0))); _compositeTarget = new RenderTargetGroup(desc); + _compositeTarget.[Friend]Identifier = "Composite Target"; _cameraTarget = new RenderTargetGroup(.(){ Width = 100, Height = 100, ColorTargetDescriptions = TargetDescription[]( - .(.R16G16B16A16_Float), - .(.R32_UInt) + .(.R16G16B16A16_Float, ownDebugName: new String("Color")), + .(.R32_UInt) {ClearColor = .UInt(uint32.MaxValue), DebugName = new String("EntityId")} ), - DepthTargetDescription = .(.D24_UNorm_S8_UInt) + DepthTargetDescription = .(.D24_UNorm_S8_UInt, clearColor: ClearColor.DepthStencil(0.0f, 0)) }); + _cameraTarget.[Friend]Identifier = "Camera Target"; _gammaCorrectEffect = Content.LoadAsset("Resources/Shaders/GammaCorrect.hlsl"); } @@ -83,6 +87,8 @@ class SceneRenderer finalTarget.AddRef(); renderTarget = _cameraTarget..AddRef(); + + RenderCommand.Clear(_compositeTarget, .ColorDepth); // 3D render Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, _compositeTarget);