Render 2D with depth, reverse depth of camera projection

dirty fix in Renderer...
This commit is contained in:
Simon Lübeß
2024-02-16 17:31:32 +01:00
parent 0321bd91c4
commit 59aaec572b
5 changed files with 24 additions and 10 deletions
+3
View File
@@ -392,6 +392,9 @@ namespace GlitchyEngine.Renderer
_lights.Clear(); _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 // Copy EntityIDs to compositionTarget
_gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, int2.Zero, int2((int32)_sceneConstants.CameraTarget.Width, (int32)_sceneConstants.CameraTarget.Height), int2.Zero, 6); _gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, int2.Zero, int2((int32)_sceneConstants.CameraTarget.Width, (int32)_sceneConstants.CameraTarget.Height), int2.Zero, 6);
+4
View File
@@ -759,9 +759,13 @@ namespace GlitchyEngine.Renderer
SortInstances(); SortInstances();
RenderCommand.SetDepthStencilState(Renderer.[Friend]_meshDepthStencilState);
DrawDeferredQuads(); DrawDeferredQuads();
DrawDeferredCircles(); DrawDeferredCircles();
DrawDeferredLines(); DrawDeferredLines();
RenderCommand.SetDepthStencilState(Renderer.[Friend]_fullscreenDepthState);
} }
private static void DrawDeferredQuads() private static void DrawDeferredQuads()
@@ -274,15 +274,16 @@ namespace GlitchyEngine.World
float halfWidth = halfHeight * _aspectRatio; float halfWidth = halfHeight * _aspectRatio;
_projection = Matrix.OrthographicProjectionOffCenter(-halfWidth, halfWidth, halfHeight, -halfHeight, _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) else if (_projectionType case .Perspective)
{ {
_projection = Matrix.PerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane, _perspectiveFarPlane); _projection = Matrix.ReversedPerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane, _perspectiveFarPlane);
} }
else if (_projectionType case .InfinitePerspective) else if (_projectionType case .InfinitePerspective)
{ {
_projection = Matrix.InfinitePerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane); _projection = Matrix.ReversedInfinitePerspectiveProjection(_perspectiveFovY, _aspectRatio, _perspectiveNearPlane);
} }
} }
} }
+1 -1
View File
@@ -308,7 +308,7 @@ namespace GlitchyEngine.World
private void UpdateProjection() mut private void UpdateProjection() mut
{ {
//_projection = Matrix.InfinitePerspectiveProjection(_fovY, _aspectRatio, _nearPlane); //_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 public void OnViewportResize(uint32 sizeX, uint32 sizeY) mut
+12 -6
View File
@@ -1,6 +1,7 @@
using GlitchyEngine.Renderer; using GlitchyEngine.Renderer;
using GlitchyEngine.Content; using GlitchyEngine.Content;
using GlitchyEngine.Math; using GlitchyEngine.Math;
using System;
namespace GlitchyEngine.World; namespace GlitchyEngine.World;
@@ -26,20 +27,23 @@ class SceneRenderer
{ {
RenderTargetGroupDescription desc = .(100, 100, RenderTargetGroupDescription desc = .(100, 100,
TargetDescription[]( TargetDescription[](
RenderTargetFormat.R16G16B16A16_Float, .(.R16G16B16A16_Float, ownDebugName: new String("Color")),
.(RenderTargetFormat.R32_UInt) {ClearColor = .UInt(uint32.MaxValue)}), .(.R32_UInt) {ClearColor = .UInt(uint32.MaxValue), DebugName = new String("EntityId")}
RenderTargetFormat.D24_UNorm_S8_UInt); ),
TargetDescription(.D24_UNorm_S8_UInt, clearColor: ClearColor.DepthStencil(0.0f, 0)));
_compositeTarget = new RenderTargetGroup(desc); _compositeTarget = new RenderTargetGroup(desc);
_compositeTarget.[Friend]Identifier = "Composite Target";
_cameraTarget = new RenderTargetGroup(.(){ _cameraTarget = new RenderTargetGroup(.(){
Width = 100, Width = 100,
Height = 100, Height = 100,
ColorTargetDescriptions = TargetDescription[]( ColorTargetDescriptions = TargetDescription[](
.(.R16G16B16A16_Float), .(.R16G16B16A16_Float, ownDebugName: new String("Color")),
.(.R32_UInt) .(.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"); _gammaCorrectEffect = Content.LoadAsset("Resources/Shaders/GammaCorrect.hlsl");
} }
@@ -84,6 +88,8 @@ class SceneRenderer
renderTarget = _cameraTarget..AddRef(); renderTarget = _cameraTarget..AddRef();
RenderCommand.Clear(_compositeTarget, .ColorDepth);
// 3D render // 3D render
Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, _compositeTarget); Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, _compositeTarget);