Merge branch 'reverse-z' of https://github.com/HydrogenC/FlaxEngine into HydrogenC-reverse-z
# Conflicts: # Source/Engine/Graphics/GPUDevice.cpp # Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp # Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp # Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp # Source/Engine/Renderer/LightPass.cpp # Source/Engine/Renderer/ReflectionsPass.cpp # Source/Engine/Renderer/ShadowsPass.cpp # Source/Shaders/SSR.hlsl # Source/Shaders/ShadowsSampling.hlsl # Source/Shaders/Sky.shader
This commit is contained in:
@@ -147,7 +147,11 @@ namespace FlaxEditor.Gizmo
|
||||
_material.SetParameterValue("OutlineColor0", _color0);
|
||||
_material.SetParameterValue("OutlineColor1", _color1);
|
||||
_material.SetParameterValue("CustomDepth", customDepth);
|
||||
_material.SetParameterValue("ViewInfo", new Float4(1.0f / projection.M11, 1.0f / projection.M22, far / (far - near), (-far * near) / (far - near) / far));
|
||||
#if FLAX_REVERSE_Z
|
||||
_material.SetParameterValue("ViewInfo", new Float4(1.0f / projection.M11, 1.0f / projection.M22, -near / (far - near), (far * near) / (far - near) / far));
|
||||
#else
|
||||
_material.SetParameterValue("ViewInfo", new Float4(1.0f / projection.M11, 1.0f / projection.M22, far / (far - near), -(far * near) / (far - near) / far));
|
||||
#endif
|
||||
Renderer.DrawPostFxMaterial(context, ref renderContext, _material, output, input.View());
|
||||
|
||||
// Cleanup
|
||||
|
||||
@@ -1497,9 +1497,14 @@ namespace FlaxEditor.Viewport
|
||||
Matrix.Multiply(ref v, ref p, out var ivp);
|
||||
ivp.Invert();
|
||||
|
||||
// Create near and far points
|
||||
var nearPoint = new Vector3(mousePosition, _nearPlane);
|
||||
var farPoint = new Vector3(mousePosition, _farPlane);
|
||||
// Create near and far points, with device depth of 1 and 0 respectively
|
||||
#if FLAX_REVERSE_Z
|
||||
var nearPoint = new Vector3(mousePosition, 1.0f);
|
||||
var farPoint = new Vector3(mousePosition, 0.0f);
|
||||
#else
|
||||
var nearPoint = new Vector3(mousePosition, 0.0f);
|
||||
var farPoint = new Vector3(mousePosition, 1.0f);
|
||||
#endif
|
||||
viewport.Unproject(ref nearPoint, ref ivp, out nearPoint);
|
||||
viewport.Unproject(ref farPoint, ref ivp, out farPoint);
|
||||
|
||||
|
||||
@@ -59,6 +59,11 @@ void BoundingFrustum::SetMatrix(const Matrix& matrix)
|
||||
_pFar.Normal.Z = matrix.M34 - matrix.M33;
|
||||
_pFar.D = matrix.M44 - matrix.M43;
|
||||
_pFar.Normalize();
|
||||
|
||||
#if FLAX_REVERSE_Z
|
||||
// Swap far and near planes if reverse z
|
||||
Swap(_pFar, _pNear);
|
||||
#endif
|
||||
}
|
||||
|
||||
Plane BoundingFrustum::GetPlane(int32 index) const
|
||||
|
||||
@@ -243,6 +243,11 @@ namespace FlaxEngine
|
||||
far.Normal.Z = matrix.M34 - matrix.M33;
|
||||
far.D = matrix.M44 - matrix.M43;
|
||||
far.Normalize();
|
||||
|
||||
#if FLAX_REVERSE_Z
|
||||
// Swap far and near planes if reverse z
|
||||
(near, far) = (far, near);
|
||||
#endif
|
||||
}
|
||||
|
||||
private static Vector3 Get3PlanesInterPoint(ref Plane p1, ref Plane p2, ref Plane p3)
|
||||
|
||||
@@ -522,10 +522,15 @@ void Matrix::OrthoOffCenter(float left, float right, float bottom, float top, fl
|
||||
result = Identity;
|
||||
result.M11 = 2.0f / (right - left);
|
||||
result.M22 = 2.0f / (top - bottom);
|
||||
result.M33 = zRange;
|
||||
result.M41 = (left + right) / (left - right);
|
||||
result.M42 = (top + bottom) / (bottom - top);
|
||||
#if FLAX_REVERSE_Z
|
||||
result.M33 = -zRange;
|
||||
result.M43 = zFar * zRange;
|
||||
#else
|
||||
result.M33 = zRange;
|
||||
result.M43 = -zNear * zRange;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Matrix::PerspectiveFov(float fov, float aspect, float zNear, float zFar, Matrix& result)
|
||||
@@ -541,16 +546,21 @@ void Matrix::PerspectiveFov(float fov, float aspect, float zNear, float zFar, Ma
|
||||
|
||||
void Matrix::PerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, Matrix& result)
|
||||
{
|
||||
const float zRange = zFar / (zFar - zNear);
|
||||
const float zRange = 1.0f / (zFar - zNear);
|
||||
|
||||
result = Zero;
|
||||
result.M11 = 2.0f * zNear / (right - left);
|
||||
result.M22 = 2.0f * zNear / (top - bottom);
|
||||
result.M31 = (left + right) / (left - right);
|
||||
result.M32 = (top + bottom) / (bottom - top);
|
||||
result.M33 = zRange;
|
||||
result.M34 = 1.0f;
|
||||
result.M43 = -zNear * zRange;
|
||||
#if FLAX_REVERSE_Z
|
||||
result.M33 = -zNear * zRange;
|
||||
result.M43 = zFar * zNear * zRange;
|
||||
#else
|
||||
result.M33 = zFar * zRange;
|
||||
result.M43 = -zFar * zNear * zRange;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Matrix::RotationX(float angle, Matrix& result)
|
||||
|
||||
@@ -2176,10 +2176,15 @@ namespace FlaxEngine
|
||||
result = Identity;
|
||||
result.M11 = 2.0f / (right - left);
|
||||
result.M22 = 2.0f / (top - bottom);
|
||||
result.M33 = zRange;
|
||||
result.M41 = (left + right) / (left - right);
|
||||
result.M42 = (top + bottom) / (bottom - top);
|
||||
#if FLAX_REVERSE_Z
|
||||
result.M33 = -zRange;
|
||||
result.M43 = zfar * zRange;
|
||||
#else
|
||||
result.M33 = zRange;
|
||||
result.M43 = -znear * zRange;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -2238,14 +2243,19 @@ namespace FlaxEngine
|
||||
public static void PerspectiveFov(float fov, float aspect, float znear, float zfar, out Matrix result)
|
||||
{
|
||||
var yScale = (float)(1.0f / Math.Tan(fov * 0.5f));
|
||||
var q = zfar / (zfar - znear);
|
||||
var zRange = 1.0f / (zfar - znear);
|
||||
result = new Matrix
|
||||
{
|
||||
M11 = yScale / aspect,
|
||||
M22 = yScale,
|
||||
M33 = q,
|
||||
M34 = 1.0f,
|
||||
M43 = -q * znear,
|
||||
#if FLAX_REVERSE_Z
|
||||
M33 = -znear * zRange,
|
||||
M43 = znear * zfar * zRange,
|
||||
#else
|
||||
M33 = zfar * zRange,
|
||||
M43 = -znear * zfar * zRange,
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2275,16 +2285,21 @@ namespace FlaxEngine
|
||||
/// <param name="result">When the method completes, contains the created projection matrix.</param>
|
||||
public static void PerspectiveOffCenter(float left, float right, float bottom, float top, float znear, float zfar, out Matrix result)
|
||||
{
|
||||
float zRange = zfar / (zfar - znear);
|
||||
float zRange = 1.0f / (zfar - znear);
|
||||
result = new Matrix
|
||||
{
|
||||
M11 = 2.0f * znear / (right - left),
|
||||
M22 = 2.0f * znear / (top - bottom),
|
||||
M31 = (left + right) / (left - right),
|
||||
M32 = (top + bottom) / (bottom - top),
|
||||
M33 = zRange,
|
||||
M34 = 1.0f,
|
||||
M43 = -znear * zRange,
|
||||
#if FLAX_REVERSE_Z
|
||||
M33 = -znear * zRange,
|
||||
M43 = znear * zfar * zRange,
|
||||
#else
|
||||
M33 = zfar * zRange,
|
||||
M43 = -znear * zfar * zRange,
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -296,15 +296,13 @@ namespace FlaxEngine
|
||||
/// <summary>
|
||||
/// Converts a screen space point into a corresponding point in world space.
|
||||
/// </summary>
|
||||
/// <param name="source">The vector to project.</param>
|
||||
/// <param name="source">The vector to project, screen uv and device depth.</param>
|
||||
/// <param name="projection">The projection matrix.</param>
|
||||
/// <param name="view">The view matrix.</param>
|
||||
/// <param name="world">The world matrix.</param>
|
||||
/// <returns>The unprojected Vector.</returns>
|
||||
public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
|
||||
public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view)
|
||||
{
|
||||
Matrix.Multiply(ref world, ref view, out Matrix matrix);
|
||||
Matrix.Multiply(ref matrix, ref projection, out matrix);
|
||||
Matrix.Multiply(ref view, ref projection, out Matrix matrix);
|
||||
Matrix.Invert(ref matrix, out matrix);
|
||||
|
||||
Unproject(ref source, ref matrix, out Vector3 vector);
|
||||
@@ -314,8 +312,8 @@ namespace FlaxEngine
|
||||
/// <summary>
|
||||
/// Converts a screen space point into a corresponding point in world space.
|
||||
/// </summary>
|
||||
/// <param name="source">The vector to project.</param>
|
||||
/// <param name="matrix">An inverted combined WorldViewProjection matrix.</param>
|
||||
/// <param name="source">The vector to project, screen uv and device depth.</param>
|
||||
/// <param name="matrix">An inverted combined ViewProjection matrix.</param>
|
||||
/// <param name="vector">The unprojected vector.</param>
|
||||
public void Unproject(ref Vector3 source, ref Matrix matrix, out Vector3 vector)
|
||||
{
|
||||
|
||||
@@ -14,6 +14,12 @@
|
||||
#undef MemoryBarrier
|
||||
#endif
|
||||
|
||||
#if FLAX_REVERSE_Z
|
||||
#define GPU_DEPTH_CLEAR_VALUE 0.0f
|
||||
#else
|
||||
#define GPU_DEPTH_CLEAR_VALUE 1.0f
|
||||
#endif
|
||||
|
||||
class GPUConstantBuffer;
|
||||
class GPUShaderProgramCS;
|
||||
class GPUBuffer;
|
||||
@@ -208,7 +214,7 @@ public:
|
||||
/// <param name="depthBuffer">The depth buffer to clear.</param>
|
||||
/// <param name="depthValue">The clear depth value.</param>
|
||||
/// <param name="stencilValue">The clear stencil value.</param>
|
||||
API_FUNCTION() virtual void ClearDepth(GPUTextureView* depthBuffer, float depthValue = 1.0f, uint8 stencilValue = 0) = 0;
|
||||
API_FUNCTION() virtual void ClearDepth(GPUTextureView* depthBuffer, float depthValue = GPU_DEPTH_CLEAR_VALUE, uint8 stencilValue = 0) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Clears an unordered access buffer with a float value.
|
||||
|
||||
@@ -160,7 +160,11 @@ GPUPipelineState::Description GPUPipelineState::Description::Default =
|
||||
true, // DepthWriteEnable
|
||||
true, // DepthClipEnable
|
||||
false, // DepthBoundsEnable
|
||||
#if FLAX_REVERSE_Z
|
||||
ComparisonFunc::Greater, // DepthFunc
|
||||
#else
|
||||
ComparisonFunc::Less, // DepthFunc
|
||||
#endif
|
||||
false, // StencilEnable
|
||||
0xff, // StencilReadMask
|
||||
0xff, // StencilWriteMask
|
||||
@@ -185,7 +189,11 @@ GPUPipelineState::Description GPUPipelineState::Description::DefaultNoDepth =
|
||||
false, // DepthWriteEnable
|
||||
false, // DepthClipEnable
|
||||
false, // DepthBoundsEnable
|
||||
#if FLAX_REVERSE_Z
|
||||
ComparisonFunc::Greater, // DepthFunc
|
||||
#else
|
||||
ComparisonFunc::Less, // DepthFunc
|
||||
#endif
|
||||
false, // StencilEnable
|
||||
0xff, // StencilReadMask
|
||||
0xff, // StencilWriteMask
|
||||
@@ -210,7 +218,11 @@ GPUPipelineState::Description GPUPipelineState::Description::DefaultFullscreenTr
|
||||
false, // DepthWriteEnable
|
||||
false, // DepthClipEnable
|
||||
false, // DepthBoundsEnable
|
||||
#if FLAX_REVERSE_Z
|
||||
ComparisonFunc::Greater, // DepthFunc
|
||||
#else
|
||||
ComparisonFunc::Less, // DepthFunc
|
||||
#endif
|
||||
false, // StencilEnable
|
||||
0xff, // StencilReadMask
|
||||
0xff, // StencilWriteMask
|
||||
|
||||
@@ -182,7 +182,11 @@ bool DeferredMaterialShader::Load()
|
||||
// Motion Vectors pass
|
||||
psDesc.DepthWriteEnable = false;
|
||||
psDesc.DepthEnable = true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::GreaterEqual;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::LessEqual;
|
||||
#endif
|
||||
psDesc.VS = _shader->GetVS("VS");
|
||||
psDesc.PS = _shader->GetPS("PS_MotionVectors");
|
||||
_cache.MotionVectors.Init(psDesc);
|
||||
@@ -200,7 +204,11 @@ bool DeferredMaterialShader::Load()
|
||||
psDesc.DepthClipEnable = false;
|
||||
psDesc.DepthWriteEnable = true;
|
||||
psDesc.DepthEnable = true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#endif
|
||||
psDesc.BlendMode.RenderTargetWriteMask = BlendingMode::ColorWrite::None;
|
||||
psDesc.HS = nullptr;
|
||||
psDesc.DS = nullptr;
|
||||
|
||||
@@ -178,7 +178,11 @@ bool DeformableMaterialShader::Load()
|
||||
psDesc.DepthClipEnable = false;
|
||||
psDesc.DepthWriteEnable = true;
|
||||
psDesc.DepthEnable = true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#endif
|
||||
psDesc.HS = nullptr;
|
||||
psDesc.DS = nullptr;
|
||||
_cache.Depth.Init(psDesc);
|
||||
|
||||
@@ -185,7 +185,11 @@ bool ForwardMaterialShader::Load()
|
||||
psDesc.DepthClipEnable = false;
|
||||
psDesc.DepthWriteEnable = true;
|
||||
psDesc.DepthEnable = true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#endif
|
||||
psDesc.BlendMode.RenderTargetWriteMask = BlendingMode::ColorWrite::None;
|
||||
psDesc.HS = nullptr;
|
||||
psDesc.DS = nullptr;
|
||||
|
||||
@@ -189,7 +189,11 @@ bool TerrainMaterialShader::Load()
|
||||
psDesc.DepthClipEnable = false;
|
||||
psDesc.DepthWriteEnable = true;
|
||||
psDesc.DepthEnable = true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#endif
|
||||
psDesc.BlendMode.RenderTargetWriteMask = BlendingMode::ColorWrite::None;
|
||||
psDesc.HS = nullptr;
|
||||
psDesc.DS = nullptr;
|
||||
|
||||
@@ -65,7 +65,11 @@ void RenderView::Prepare(RenderContext& renderContext)
|
||||
void RenderView::PrepareCache(const RenderContext& renderContext, float width, float height, const Float2& temporalAAJitter, const RenderView* mainView)
|
||||
{
|
||||
// The same format used by the Flax common shaders and postFx materials
|
||||
ViewInfo = Float4(1.0f / Projection.M11, 1.0f / Projection.M22, Far / (Far - Near), (-Far * Near) / (Far - Near) / Far);
|
||||
#if FLAX_REVERSE_Z
|
||||
ViewInfo = Float4(1.0f / Projection.M11, 1.0f / Projection.M22, -Near / (Far - Near), (Far * Near) / (Far - Near) / Far);
|
||||
#else
|
||||
ViewInfo = Float4(1.0f / Projection.M11, 1.0f / Projection.M22, Far / (Far - Near), -(Far * Near) / (Far - Near) / Far);
|
||||
#endif
|
||||
ScreenSize = Float4(width, height, 1.0f / width, 1.0f / height);
|
||||
|
||||
TemporalAAJitter.Z = TemporalAAJitter.X;
|
||||
|
||||
@@ -103,7 +103,11 @@ void Sky::Draw(RenderContext& renderContext)
|
||||
psDesc.CullMode = CullMode::Inverted;
|
||||
psDesc.DepthWriteEnable = false;
|
||||
psDesc.DepthClipEnable = false;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::GreaterEqual;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::LessEqual;
|
||||
#endif
|
||||
|
||||
if (_psSky->Init(psDesc))
|
||||
{
|
||||
|
||||
@@ -271,8 +271,13 @@ void DepthOfFieldPass::Render(RenderContext& renderContext, GPUTexture*& frame,
|
||||
cbData.BokehTargetSize.Y = static_cast<float>(bokehTargetHeight);
|
||||
|
||||
// TODO: use projection matrix instead of this far and near stuff?
|
||||
#if FLAX_REVERSE_Z
|
||||
cbData.ProjectionAB.X = -nearPlane / (farPlane - nearPlane);
|
||||
cbData.ProjectionAB.Y = (farPlane * nearPlane) / (farPlane - nearPlane);
|
||||
#else
|
||||
cbData.ProjectionAB.X = farPlane / (farPlane - nearPlane);
|
||||
cbData.ProjectionAB.Y = (-farPlane * nearPlane) / (farPlane - nearPlane);
|
||||
cbData.ProjectionAB.Y = -(farPlane * nearPlane) / (farPlane - nearPlane);
|
||||
#endif
|
||||
|
||||
auto cb = shader->GetCB(0);
|
||||
context->UpdateCB(cb, &cbData);
|
||||
|
||||
@@ -442,7 +442,10 @@ bool GBufferPass::IsDebugView(ViewMode mode)
|
||||
void GBufferPass::SetInputs(const RenderView& view, ShaderGBufferData& gBuffer)
|
||||
{
|
||||
// GBuffer params:
|
||||
// ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(Far / (Far - Near) w-(-Far * Near) / (Far - Near) / Far)
|
||||
// With reverse Z enabled:
|
||||
// ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(-(Near / (Far - Near)) w-((Far * Near) / (Far - Near) / Far)
|
||||
// Otherwise:
|
||||
// ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(Far / (Far - Near)) w-(-(Far * Near) / (Far - Near) / Far)
|
||||
// ScreenSize : x-Width y-Height z-1 / Width w-1 / Height
|
||||
// ViewPos,ViewFar : x,y,z - world space view position w-Far
|
||||
// InvViewMatrix : inverse view matrix (4 rows by 4 columns)
|
||||
|
||||
@@ -90,7 +90,11 @@ bool LightPass::setupResources()
|
||||
psDesc.CullMode = CullMode::Normal;
|
||||
if (_psLightLocal.Create(psDesc, shader, "PS_LocalLight"))
|
||||
return true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#endif
|
||||
psDesc.CullMode = CullMode::Inverted;
|
||||
if (_psLightLocalInside.Create(psDesc, shader, "PS_LocalLight"))
|
||||
return true;
|
||||
@@ -107,7 +111,11 @@ bool LightPass::setupResources()
|
||||
psDesc.CullMode = CullMode::Normal;
|
||||
if (_psLightSky->Init(psDesc))
|
||||
return true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#endif
|
||||
psDesc.CullMode = CullMode::Inverted;
|
||||
if (_psLightSkyInside->Init(psDesc))
|
||||
return true;
|
||||
|
||||
@@ -536,7 +536,11 @@ bool ShadowsPass::setupResources()
|
||||
psDesc.CullMode = CullMode::Normal;
|
||||
if (_psShadowPoint.Create(psDesc, shader, psLocalLight))
|
||||
return true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#endif
|
||||
psDesc.CullMode = CullMode::Inverted;
|
||||
if (_psShadowPointInside.Create(psDesc, shader, psLocalLight))
|
||||
return true;
|
||||
@@ -551,7 +555,11 @@ bool ShadowsPass::setupResources()
|
||||
psDesc.CullMode = CullMode::Normal;
|
||||
if (_psShadowSpot.Create(psDesc, shader, psLocalLight, 8))
|
||||
return true;
|
||||
#if FLAX_REVERSE_Z
|
||||
psDesc.DepthFunc = ComparisonFunc::Less;
|
||||
#else
|
||||
psDesc.DepthFunc = ComparisonFunc::Greater;
|
||||
#endif
|
||||
psDesc.CullMode = CullMode::Inverted;
|
||||
if (_psShadowSpotInside.Create(psDesc, shader, psLocalLight, 8))
|
||||
return true;
|
||||
@@ -1139,7 +1147,11 @@ void ShadowsPass::SetupLight(ShadowsCustomBuffer& shadows, RenderContext& render
|
||||
void ShadowsPass::ClearShadowMapTile(GPUContext* context, GPUConstantBuffer* quadShaderCB, QuadShaderData& quadShaderData) const
|
||||
{
|
||||
// Color.r is used by PS_DepthClear in Quad shader to clear depth
|
||||
#if FLAX_REVERSE_Z
|
||||
quadShaderData.Color = Float4::Zero;
|
||||
#else
|
||||
quadShaderData.Color = Float4::One;
|
||||
#endif
|
||||
context->UpdateCB(quadShaderCB, &quadShaderData);
|
||||
context->BindCB(0, quadShaderCB);
|
||||
|
||||
@@ -1590,14 +1602,7 @@ void ShadowsPass::RenderShadowMaps(RenderContextBatch& renderContextBatch)
|
||||
context->SetViewportAndScissors(Viewport(tile.StaticRectTile->X, tile.StaticRectTile->Y, tile.StaticRectTile->Width, tile.StaticRectTile->Height));
|
||||
if (!shadows.ClearStaticShadowMapAtlas)
|
||||
{
|
||||
// Color.r is used by PS_DepthClear in Quad shader to clear depth
|
||||
quadShaderData.Color = Float4::One;
|
||||
context->UpdateCB(quadShaderCB, &quadShaderData);
|
||||
context->BindCB(0, quadShaderCB);
|
||||
|
||||
// Clear tile depth
|
||||
context->SetState(_psDepthClear);
|
||||
context->DrawFullscreenTriangle();
|
||||
ClearShadowMapTile(context, quadShaderCB, quadShaderData);
|
||||
}
|
||||
|
||||
// Draw objects depth
|
||||
|
||||
@@ -249,6 +249,9 @@ bool ShaderCompiler::OnCompileBegin()
|
||||
const auto profile = GetProfile();
|
||||
const auto featureLevel = RenderTools::GetFeatureLevel(profile);
|
||||
_globalMacros.Add({ "FEATURE_LEVEL", Numbers[(int32)featureLevel] });
|
||||
#if FLAX_REVERSE_Z
|
||||
_globalMacros.Add({ "FLAX_REVERSE_Z", "1"});
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -4,22 +4,26 @@
|
||||
#define __COMMON__
|
||||
|
||||
// Platform macros
|
||||
#if !defined(DIRECTX)
|
||||
#ifndef DIRECTX
|
||||
#define DIRECTX 0
|
||||
#endif
|
||||
#if !defined(OPENGL)
|
||||
#ifndef OPENGL
|
||||
#define OPENGL 0
|
||||
#endif
|
||||
#if !defined(VULKAN)
|
||||
#ifndef VULKAN
|
||||
#define VULKAN 0
|
||||
#endif
|
||||
#if defined(PLATFORM_PS4)
|
||||
#ifdef PLATFORM_PS4
|
||||
#include "./FlaxPlatforms/PS4/Shaders/PS4Common.hlsl"
|
||||
#endif
|
||||
#if defined(PLATFORM_PS5)
|
||||
#ifdef PLATFORM_PS5
|
||||
#include "./FlaxPlatforms/PS5/Shaders/PS5Common.hlsl"
|
||||
#endif
|
||||
|
||||
#ifndef FLAX_REVERSE_Z
|
||||
#define FLAX_REVERSE_Z 0
|
||||
#endif
|
||||
|
||||
// Feature levels
|
||||
#define FEATURE_LEVEL_ES2 0
|
||||
#define FEATURE_LEVEL_ES3 1
|
||||
@@ -165,7 +169,11 @@ float4 LoadTextureWGSL(Texture2D tex, float2 uv)
|
||||
// Structure that contains information about GBuffer
|
||||
struct GBufferData
|
||||
{
|
||||
float4 ViewInfo; // x-1/Projection[0,0], y-1/Projection[1,1], z-(Far / (Far - Near), w-(-Far * Near) / (Far - Near) / Far)
|
||||
// If reverse Z enabled:
|
||||
// x-1/Projection[0,0], y-1/Projection[1,1], z-(-Near / (Far - Near)), w-((Far * Near) / (Far - Near) / Far)
|
||||
// Otherwise:
|
||||
// x-1/Projection[0,0], y-1/Projection[1,1], z-(Far / (Far - Near)), w-(-(Far * Near) / (Far - Near) / Far)
|
||||
float4 ViewInfo;
|
||||
float4 ScreenSize; // x-Width, y-Height, z-1/Width, w-1/Height
|
||||
float3 ViewPos; // view position (in world space)
|
||||
float ViewFar; // view far plane distance (in world space)
|
||||
|
||||
@@ -41,7 +41,11 @@ float4 PS(VS2PS input) : SV_Target
|
||||
{
|
||||
float sceneDepthDeviceZ = SceneDepthTexture.Load(int3(input.Position.xy, 0)).r;
|
||||
float interpolatedDeviceZ = input.Position.z;
|
||||
#if FLAX_REVERSE_Z
|
||||
clip(interpolatedDeviceZ - sceneDepthDeviceZ);
|
||||
#else
|
||||
clip(sceneDepthDeviceZ - interpolatedDeviceZ);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -117,7 +117,11 @@ float3 TraceScreenSpaceReflection(
|
||||
{
|
||||
// Sample depth buffer and calculate depth difference
|
||||
float currSample = SAMPLE_RT_DEPTH(depthBuffer, currOffset.xy);
|
||||
float depthDiff = currOffset.z - currSample;
|
||||
#if FLAX_REVERSE_Z
|
||||
depthDiff = currSample - currOffset.z;
|
||||
#else
|
||||
depthDiff = currOffset.z - currSample;
|
||||
#endif
|
||||
|
||||
// Check intersection
|
||||
if (depthDiff >= 0)
|
||||
|
||||
@@ -47,7 +47,7 @@ float RayCastScreenSpaceShadow(GBufferData gBufferData, GBufferSample gBuffer, f
|
||||
float3 rayUV = rayCS.xyz / rayCS.w;
|
||||
rayUV.xy = rayUV.xy * float2(0.5, -0.5) + float2(0.5, 0.5);
|
||||
float sceneDepth = SampleDepth(gBufferData, rayUV.xy) * gBufferData.ViewFar;
|
||||
float rayDepth = (gBufferData.ViewInfo.w / (rayUV.z - gBufferData.ViewInfo.z)) * gBufferData.ViewFar * 0.998;
|
||||
float rayDepth = LinearizeZ(gBufferData, rayUV.z) * gBufferData.ViewFar * 0.998;
|
||||
float surfaceThickness = 0.035f + rayDepth * rayLength;
|
||||
float depthTestHardness = 0.005f;
|
||||
float lightAmount = saturate((rayDepth - sceneDepth) / depthTestHardness) * saturate((sceneDepth + surfaceThickness - rayDepth) / depthTestHardness);
|
||||
|
||||
@@ -74,9 +74,20 @@ float3 GetShadowPositionOffset(float offsetScale, float NoL, float3 normal)
|
||||
|
||||
float CalculateSubsurfaceOcclusion(float opacity, float sceneDepth, float shadowMapDepth)
|
||||
{
|
||||
// `sceneDepth` and `shadowMapDepth`are raw depths, so we have to flip them when reverse-z is enabled
|
||||
#if FLAX_REVERSE_Z
|
||||
float thickness = max(shadowMapDepth - sceneDepth, 0);
|
||||
#else
|
||||
float thickness = max(sceneDepth - shadowMapDepth, 0);
|
||||
#endif
|
||||
|
||||
float occlusion = 1 - saturate(thickness * lerp(1.0f, 100.0f, opacity));
|
||||
|
||||
#if FLAX_REVERSE_Z
|
||||
return shadowMapDepth < 0.01f ? 1 : occlusion;
|
||||
#else
|
||||
return shadowMapDepth > 0.99f ? 1 : occlusion;
|
||||
#endif
|
||||
}
|
||||
|
||||
float PostProcessShadow(ShadowData lightShadow, float shadow)
|
||||
|
||||
@@ -89,6 +89,13 @@ namespace Flax.Build
|
||||
options.ScriptingAPI.Defines.Add("PLATFORM_SDL");
|
||||
}
|
||||
|
||||
if (!EngineConfiguration.WithTraditionalZ(options))
|
||||
{
|
||||
// Add reverse-z definitions
|
||||
options.CompileEnv.PreprocessorDefinitions.Add("FLAX_REVERSE_Z");
|
||||
options.ScriptingAPI.Defines.Add("FLAX_REVERSE_Z");
|
||||
}
|
||||
|
||||
// Add include paths for this and all referenced projects sources
|
||||
foreach (var project in Project.GetAllProjects())
|
||||
{
|
||||
|
||||
@@ -306,6 +306,12 @@ namespace Flax.Build
|
||||
[CommandLine("useLargeWorlds", "1 to enable large worlds with 64-bit coordinates precision support in build (USE_LARGE_WORLDS=1)")]
|
||||
public static bool UseLargeWorlds = false;
|
||||
|
||||
/// <summary>
|
||||
/// 1 to use traditional z buffer, or reversed z will be adopted by default.
|
||||
/// </summary>
|
||||
[CommandLine("noReverseZ", "1 to use traditional z buffer, or reversed z will be adopted by default.")]
|
||||
public static bool NoReverseZ = false;
|
||||
|
||||
/// <summary>
|
||||
/// True if managed C# scripting should be enabled, otherwise false. Engine without C# is partially supported and can be used when porting to a new platform before implementing C# runtime on it.
|
||||
/// </summary>
|
||||
@@ -343,6 +349,12 @@ namespace Flax.Build
|
||||
return UseLargeWorlds;
|
||||
}
|
||||
|
||||
public static bool WithTraditionalZ(NativeCpp.BuildOptions options)
|
||||
{
|
||||
// Whether to use traditional z-buffer instead of reversed z
|
||||
return NoReverseZ;
|
||||
}
|
||||
|
||||
public static bool WithDotNet(NativeCpp.BuildOptions options)
|
||||
{
|
||||
return UseDotNet;
|
||||
|
||||
Reference in New Issue
Block a user