From 75625dc679be2bc6739f02cf98920f80bd498bd2 Mon Sep 17 00:00:00 2001 From: AtomicWalrus Date: Fri, 14 Apr 2023 20:13:28 -0600 Subject: [PATCH 1/2] Reverse depth & 32F buffer format -Adds reversed depth projection model, dramatically increasing depth buffer effective resolution. -Adds 32F depth 8U stencil format GFXFormatD32FS8X24 (following DX naming conventions). Note this is a 64-bit format, and likely not suitable for mobile platforms. Revert to GFXFormatD24S8 in renderManager.tscript for mobile & "ancient" platforms. -Corrects alignment of texture type details array. --- Engine/source/T3D/gameFunctions.cpp | 4 +-- Engine/source/T3D/lightFlareData.cpp | 2 +- Engine/source/environment/VolumetricFog.cpp | 4 +-- Engine/source/environment/skyBox.cpp | 2 +- Engine/source/environment/waterPlane.cpp | 2 +- .../gfx/D3D11/gfxD3D11EnumTranslate.cpp | 1 + .../gfx/D3D11/gfxD3D11TextureManager.cpp | 5 +++ Engine/source/gfx/gfxAPI.cpp | 1 + Engine/source/gfx/gfxEnums.h | 1 + Engine/source/gfx/gfxFormatUtils.cpp | 10 ++++++ Engine/source/gfx/gfxStateBlock.cpp | 2 +- Engine/source/gfx/gfxStringEnumTranslate.cpp | 1 + Engine/source/gfx/gl/gfxGLDevice.cpp | 35 +++++++------------ Engine/source/gfx/gl/gfxGLEnumTranslate.cpp | 3 ++ Engine/source/gui/3d/guiTSControl.cpp | 2 +- Engine/source/gui/core/guiCanvas.cpp | 2 +- .../lighting/shadowMap/cubeLightShadowMap.cpp | 2 +- .../dualParaboloidLightShadowMap.cpp | 2 +- .../shadowMap/paraboloidLightShadowMap.cpp | 2 +- .../lighting/shadowMap/pssmLightShadowMap.cpp | 11 ++++-- .../shadowMap/singleLightShadowMap.cpp | 2 +- Engine/source/math/mMatrix.cpp | 8 +++++ Engine/source/math/mMatrix.h | 3 ++ Engine/source/math/mathUtils.cpp | 6 +++- .../renderInstance/renderDeferredMgr.cpp | 2 +- .../renderInstance/renderFormatChanger.cpp | 2 +- Engine/source/scene/reflector.cpp | 11 +++--- .../shaderGen/GLSL/shaderFeatureGLSL.cpp | 2 +- .../shaderGen/HLSL/shaderFeatureHLSL.cpp | 2 +- Engine/source/util/imposterCapture.cpp | 2 +- .../rendering/scripts/renderManager.tscript | 2 +- .../core/rendering/shaders/basicCloudsV.hlsl | 2 +- .../core/rendering/shaders/cloudLayerV.hlsl | 2 +- .../rendering/shaders/gl/basicCloudsV.glsl | 2 +- .../rendering/shaders/gl/cloudLayerV.glsl | 2 +- .../rendering/shaders/water/gl/waterV.glsl | 2 +- .../core/rendering/shaders/water/waterV.hlsl | 2 +- 37 files changed, 91 insertions(+), 57 deletions(-) diff --git a/Engine/source/T3D/gameFunctions.cpp b/Engine/source/T3D/gameFunctions.cpp index 9535496f5..c40e2f081 100644 --- a/Engine/source/T3D/gameFunctions.cpp +++ b/Engine/source/T3D/gameFunctions.cpp @@ -531,7 +531,7 @@ void renderFrame(GFXTextureTargetRef* target, MatrixF transform, Frustum frustum // Clear the current viewport area GFX->setViewport(targetRect); - GFX->clear(GFXClearZBuffer | GFXClearStencil | GFXClearTarget, canvasClearColor, 1.0f, 0); + GFX->clear(GFXClearZBuffer | GFXClearStencil | GFXClearTarget, canvasClearColor, 0.0f, 0); // Make sure we have a clean matrix state // before we start rendering anything! @@ -555,7 +555,7 @@ void renderFrame(GFXTextureTargetRef* target, MatrixF transform, Frustum frustum GFXTargetRef origTarget = GFX->getActiveRenderTarget(); // Clear the zBuffer so GUI doesn't hose object rendering accidentally - GFX->clear(GFXClearZBuffer, ColorI(20, 20, 20), 1.0f, 0); + GFX->clear(GFXClearZBuffer, ColorI(20, 20, 20), 0.0f, 0); GFX->setFrustum(frustum); MatrixF mSaveProjection = GFX->getProjectionMatrix(); diff --git a/Engine/source/T3D/lightFlareData.cpp b/Engine/source/T3D/lightFlareData.cpp index 4aaa4747e..a6c18a6b0 100644 --- a/Engine/source/T3D/lightFlareData.cpp +++ b/Engine/source/T3D/lightFlareData.cpp @@ -455,7 +455,7 @@ void LightFlareData::prepRender(SceneRenderState *state, LightFlareState *flareS lightPosSS *= oneOverViewportExtent; lightPosSS = (lightPosSS * 2.0f) - Point3F::One; lightPosSS.y = -lightPosSS.y; - lightPosSS.z = 0.0f; + lightPosSS.z = 1.0f; // Determine the center of the current projection so we can converge there Point3F centerProj(0); diff --git a/Engine/source/environment/VolumetricFog.cpp b/Engine/source/environment/VolumetricFog.cpp index e6302d826..0dd3841ec 100644 --- a/Engine/source/environment/VolumetricFog.cpp +++ b/Engine/source/environment/VolumetricFog.cpp @@ -1104,7 +1104,7 @@ void VolumetricFog::render(ObjectRenderInst *ri, SceneRenderState *state, BaseMa z_buf->attachTexture(GFXTextureTarget::Color0, mDepthBuffer); GFX->setActiveRenderTarget(z_buf); - GFX->clear(GFXClearStencil | GFXClearTarget , ColorI(0,0,0,0), 1.0f, 0); + GFX->clear(GFXClearStencil | GFXClearTarget , ColorI(0,0,0,0), 0.0f, 0); GFX->drawPrimitive(0); z_buf->resolve(); @@ -1112,7 +1112,7 @@ void VolumetricFog::render(ObjectRenderInst *ri, SceneRenderState *state, BaseMa //render frontside to target mFrontBuffer z_buf->attachTexture(GFXTextureTarget::DepthStencil, GFXTextureTarget::sDefaultDepthStencil); z_buf->attachTexture(GFXTextureTarget::Color0, mFrontBuffer); - GFX->clear(GFXClearStencil | GFXClearTarget, ColorI(0, 0, 0, 0), 1.0f, 0); + GFX->clear(GFXClearStencil | GFXClearTarget, ColorI(0, 0, 0, 0), 0.0f, 0); GFX->setStateBlock(mStateblock_preF); diff --git a/Engine/source/environment/skyBox.cpp b/Engine/source/environment/skyBox.cpp index 7cd590a0e..16ad8d166 100644 --- a/Engine/source/environment/skyBox.cpp +++ b/Engine/source/environment/skyBox.cpp @@ -604,7 +604,7 @@ void SkyBox::_initMaterial() desc.setCullMode( GFXCullNone ); desc.setBlend( true ); desc.setZReadWrite( true, false ); - desc.zFunc = GFXCmpLessEqual; + desc.zFunc = GFXCmpGreaterEqual; mMatInstance->addStateBlockDesc( desc ); // Also disable lighting on the skybox material by default. diff --git a/Engine/source/environment/waterPlane.cpp b/Engine/source/environment/waterPlane.cpp index 55470e253..83faed217 100644 --- a/Engine/source/environment/waterPlane.cpp +++ b/Engine/source/environment/waterPlane.cpp @@ -714,7 +714,7 @@ void WaterPlane::prepRenderImage( SceneRenderState *state ) mGenerateVB = false; MatrixF proj( true ); - MathUtils::getZBiasProjectionMatrix( 0.0001f, mFrustum, &proj ); + MathUtils::getZBiasProjectionMatrix( 0.0f, mFrustum, &proj ); mMatrixSet->setSceneProjection(proj); } diff --git a/Engine/source/gfx/D3D11/gfxD3D11EnumTranslate.cpp b/Engine/source/gfx/D3D11/gfxD3D11EnumTranslate.cpp index 60982078a..4e2609bcb 100644 --- a/Engine/source/gfx/D3D11/gfxD3D11EnumTranslate.cpp +++ b/Engine/source/gfx/D3D11/gfxD3D11EnumTranslate.cpp @@ -73,6 +73,7 @@ void GFXD3D11EnumTranslate::init() GFXD3D11TextureFormat[GFXFormatD24S8] = DXGI_FORMAT_D24_UNORM_S8_UINT; GFXD3D11TextureFormat[GFXFormatD24FS8] = DXGI_FORMAT_UNKNOWN; GFXD3D11TextureFormat[GFXFormatD16] = DXGI_FORMAT_D16_UNORM; + GFXD3D11TextureFormat[GFXFormatD32FS8X24] = DXGI_FORMAT_D32_FLOAT_S8X24_UINT; //sRGB GFXD3D11TextureFormat[GFXFormatR8G8B8A8_SRGB] = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB; GFXD3D11TextureFormat[GFXFormatR8G8B8_SRGB] = DXGI_FORMAT_B8G8R8X8_UNORM_SRGB; diff --git a/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp b/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp index 0e4b11795..dd34b9ec5 100644 --- a/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp +++ b/Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp @@ -173,7 +173,12 @@ void GFXD3D11TextureManager::_innerCreateTexture( GFXD3D11TextureObject *retTex, desc.CPUAccessFlags = cpuFlags; //depth stencil must be a typeless format if it is bound on render target and shader resource simultaneously // we'll send the real format for the creation of the views + if (format == GFXFormatD24S8) + { desc.Format = DXGI_FORMAT_R24G8_TYPELESS; + } + else // Note: right now only GFXFormatD24S8 and GFXFormatD32FS8U24 are supported. Additional cases required to support 16-bit depth for mobile. + desc.Format = DXGI_FORMAT_R32G8X24_TYPELESS; desc.MipLevels = numMipLevels; desc.SampleDesc.Count = antialiasLevel; desc.SampleDesc.Quality = numQualityLevels; diff --git a/Engine/source/gfx/gfxAPI.cpp b/Engine/source/gfx/gfxAPI.cpp index 6c59e2ff4..be9564732 100644 --- a/Engine/source/gfx/gfxAPI.cpp +++ b/Engine/source/gfx/gfxAPI.cpp @@ -128,6 +128,7 @@ ImplementEnumType( GFXFormat, { GFXFormatD24S8, "GFXFormatD24S8" }, { GFXFormatD24FS8, "GFXFormatD24FS8" }, { GFXFormatD16, "GFXFormatD16" }, + { GFXFormatD32FS8X24, "GFXFormatD32FS8X24" }, { GFXFormatR32G32B32A32F, "GFXFormatR32G32B32A32F" }, { GFXFormatR16G16B16A16F, "GFXFormatR16G16B16A16F" }, diff --git a/Engine/source/gfx/gfxEnums.h b/Engine/source/gfx/gfxEnums.h index d6544636b..7a0572d40 100644 --- a/Engine/source/gfx/gfxEnums.h +++ b/Engine/source/gfx/gfxEnums.h @@ -161,6 +161,7 @@ enum GFXFormat // 64 bit texture formats... GFXFormatR16G16B16A16,// first in group... GFXFormatR16G16B16A16F, + GFXFormatD32FS8X24, // 128 bit texture formats... GFXFormatR32G32B32A32F,// first in group... diff --git a/Engine/source/gfx/gfxFormatUtils.cpp b/Engine/source/gfx/gfxFormatUtils.cpp index ce5674a39..aeed53a3b 100644 --- a/Engine/source/gfx/gfxFormatUtils.cpp +++ b/Engine/source/gfx/gfxFormatUtils.cpp @@ -39,6 +39,7 @@ GFXFormatInfo::Data GFXFormatInfo::smFormatInfos[ GFXFormat_COUNT ] = // 16 bit texture formats... GFXFormatInfo::Data( 2, false, false, false ), // GFXFormatR5G6B5 GFXFormatInfo::Data( 2, true, false, false ), // GFXFormatR5G5B5A1 + GFXFormatInfo::Data( 2, false, false, false ), // GFXFormatR5G5B5X1 GFXFormatInfo::Data( 2, true, false, false ), // GFXFormatA8L8 GFXFormatInfo::Data( 2, false, false, false ), // GFXFormatL16 GFXFormatInfo::Data( 2, false, false, false ), // GFXFormatR16F @@ -46,22 +47,28 @@ GFXFormatInfo::Data GFXFormatInfo::smFormatInfos[ GFXFormat_COUNT ] = // 24 bit texture formats... GFXFormatInfo::Data( 3, false, false, false ), // GFXFormatR8G8B8 + GFXFormatInfo::Data( 3, false, false, false ), // GFXFormatR8G8B8_SRGB // 32 bit texture formats... GFXFormatInfo::Data( 4, true, false, false ), // GFXFormatR8G8B8A8 GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatR8G8B8X8 + GFXFormatInfo::Data( 4, true, false, false ), // GFXFormatB8G8R8A8 + GFXFormatInfo::Data( 4, true, false, false ), // GFXFormatR8G8B8A8_SRGB GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatR32F GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatR16G16 GFXFormatInfo::Data( 4, false, false, true ), // GFXFormatR16G16F GFXFormatInfo::Data( 4, true, false, false ), // GFXFormatR10G10B10A2 + GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatR11G11B10 GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatD32 GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatD24X8 GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatD24S8 GFXFormatInfo::Data( 4, false, false, false ), // GFXFormatD24FS8 + GFXFormatInfo::Data( 4, true, false, false ), // GFXFormatR8G8B8A8_LINEAR_FORCE // 64 bit texture formats... GFXFormatInfo::Data( 8, true, false, false ), // GFXFormatR16G16B16A16 GFXFormatInfo::Data( 8, true, false, true ), // GFXFormatR16G16B16A16F + GFXFormatInfo::Data( 8, false, false, false ), // GFXFormatD32FS8X24 // 128 bit texture formats... GFXFormatInfo::Data( 16, true, false, true ), // GFXFormatR32G32B32A32F @@ -72,6 +79,9 @@ GFXFormatInfo::Data GFXFormatInfo::smFormatInfos[ GFXFormat_COUNT ] = GFXFormatInfo::Data( 0, true, true, false ), // GFXFormatBC3 GFXFormatInfo::Data( 0, false, true, false ), // GFXFormatBC4 GFXFormatInfo::Data( 0, false, true, false ), // GFXFormatBC5 + GFXFormatInfo::Data( 0, false, true, false ), // GFXFormatBC1_SRGB + GFXFormatInfo::Data( 0, false, true, false ), // GFXFormatBC2_SRGB + GFXFormatInfo::Data( 0, false, true, false ), // GFXFormatBC3_SRGB }; //----------------------------------------------------------------------------- diff --git a/Engine/source/gfx/gfxStateBlock.cpp b/Engine/source/gfx/gfxStateBlock.cpp index 5fb7a31dc..61ff8958c 100644 --- a/Engine/source/gfx/gfxStateBlock.cpp +++ b/Engine/source/gfx/gfxStateBlock.cpp @@ -73,7 +73,7 @@ GFXStateBlockDesc::GFXStateBlockDesc() zDefined = false; zEnable = true; zWriteEnable = true; - zFunc = GFXCmpLessEqual; + zFunc = GFXCmpGreaterEqual; zBias = 0; zSlopeBias = 0; diff --git a/Engine/source/gfx/gfxStringEnumTranslate.cpp b/Engine/source/gfx/gfxStringEnumTranslate.cpp index fd8a3c11a..813648f17 100644 --- a/Engine/source/gfx/gfxStringEnumTranslate.cpp +++ b/Engine/source/gfx/gfxStringEnumTranslate.cpp @@ -122,6 +122,7 @@ void GFXStringEnumTranslate::init() GFX_STRING_ASSIGN_MACRO( GFXStringTextureFormat, GFXFormatD24S8 ); GFX_STRING_ASSIGN_MACRO( GFXStringTextureFormat, GFXFormatD24FS8 ); GFX_STRING_ASSIGN_MACRO( GFXStringTextureFormat, GFXFormatD16 ); + GFX_STRING_ASSIGN_MACRO( GFXStringTextureFormat, GFXFormatD32FS8X24 ); GFX_STRING_ASSIGN_MACRO( GFXStringTextureFormat, GFXFormatR32G32B32A32F ); GFX_STRING_ASSIGN_MACRO( GFXStringTextureFormat, GFXFormatR16G16B16A16F ); diff --git a/Engine/source/gfx/gl/gfxGLDevice.cpp b/Engine/source/gfx/gl/gfxGLDevice.cpp index 88026a56c..9c0df9ce1 100644 --- a/Engine/source/gfx/gl/gfxGLDevice.cpp +++ b/Engine/source/gfx/gl/gfxGLDevice.cpp @@ -191,6 +191,7 @@ void GFXGLDevice::initGLState() glGenVertexArrays(1, &vao); glBindVertexArray(vao); + glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE); //enable sRGB glEnable(GL_FRAMEBUFFER_SRGB); @@ -794,38 +795,26 @@ void GFXGLDevice::setClipRect( const RectI &inRect ) mClip = inRect; mClip.intersect(maxRect); - // Create projection matrix. See http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/ortho.html - const F32 left = mClip.point.x; - const F32 right = mClip.point.x + mClip.extent.x; - const F32 bottom = mClip.extent.y; - const F32 top = 0.0f; - const F32 nearPlane = 0.0f; - const F32 farPlane = 1.0f; - - const F32 tx = -(right + left)/(right - left); - const F32 ty = -(top + bottom)/(top - bottom); - const F32 tz = -(farPlane + nearPlane)/(farPlane - nearPlane); - static Point4F pt; - pt.set(2.0f / (right - left), 0.0f, 0.0f, 0.0f); + F32 l = F32(mClip.point.x); + F32 r = F32(mClip.point.x + mClip.extent.x); + F32 b = F32(mClip.point.y + mClip.extent.y); + F32 t = F32(mClip.point.y); + + // Set up projection matrix, + //static Point4F pt; + pt.set(2.0f / (r - l), 0.0f, 0.0f, 0.0f); mProjectionMatrix.setColumn(0, pt); - pt.set(0.0f, 2.0f/(top - bottom), 0.0f, 0.0f); + pt.set(0.0f, 2.0f / (t - b), 0.0f, 0.0f); mProjectionMatrix.setColumn(1, pt); - pt.set(0.0f, 0.0f, -2.0f/(farPlane - nearPlane), 0.0f); + pt.set(0.0f, 0.0f, 1.0f, 0.0f); mProjectionMatrix.setColumn(2, pt); - pt.set(tx, ty, tz, 1.0f); + pt.set((l + r) / (l - r), (t + b) / (b - t), 1.0f, 1.0f); mProjectionMatrix.setColumn(3, pt); - // Translate projection matrix. - static MatrixF translate(true); - pt.set(0.0f, -mClip.point.y, 0.0f, 1.0f); - translate.setColumn(3, pt); - - mProjectionMatrix *= translate; - MatrixF mTempMatrix(true); setViewMatrix( mTempMatrix ); setWorldMatrix( mTempMatrix ); diff --git a/Engine/source/gfx/gl/gfxGLEnumTranslate.cpp b/Engine/source/gfx/gl/gfxGLEnumTranslate.cpp index 657ee7743..93c8ee57c 100644 --- a/Engine/source/gfx/gl/gfxGLEnumTranslate.cpp +++ b/Engine/source/gfx/gl/gfxGLEnumTranslate.cpp @@ -132,6 +132,7 @@ void GFXGLEnumTranslate::init() GFXGLTextureInternalFormat[GFXFormatD32] = GL_DEPTH_COMPONENT32; GFXGLTextureInternalFormat[GFXFormatD24X8] = GL_DEPTH24_STENCIL8; GFXGLTextureInternalFormat[GFXFormatD24S8] = GL_DEPTH24_STENCIL8; + GFXGLTextureInternalFormat[GFXFormatD32FS8X24] = GL_DEPTH32F_STENCIL8; GFXGLTextureInternalFormat[GFXFormatR16G16B16A16] = GL_RGBA16; GFXGLTextureInternalFormat[GFXFormatBC1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; GFXGLTextureInternalFormat[GFXFormatBC2] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; @@ -161,6 +162,7 @@ void GFXGLEnumTranslate::init() GFXGLTextureFormat[GFXFormatD32] = GL_DEPTH_COMPONENT; GFXGLTextureFormat[GFXFormatD24X8] = GL_DEPTH_STENCIL; GFXGLTextureFormat[GFXFormatD24S8] = GL_DEPTH_STENCIL; + GFXGLTextureFormat[GFXFormatD32FS8X24] = GL_DEPTH_STENCIL; GFXGLTextureFormat[GFXFormatR16G16B16A16] = GL_RGBA; GFXGLTextureFormat[GFXFormatBC1] = GL_RGBA; GFXGLTextureFormat[GFXFormatBC2] = GL_RGBA; @@ -190,6 +192,7 @@ void GFXGLEnumTranslate::init() GFXGLTextureType[GFXFormatD32] = GL_UNSIGNED_INT; GFXGLTextureType[GFXFormatD24X8] = GL_UNSIGNED_INT_24_8; GFXGLTextureType[GFXFormatD24S8] = GL_UNSIGNED_INT_24_8; + GFXGLTextureType[GFXFormatD32FS8X24] = GL_FLOAT_32_UNSIGNED_INT_24_8_REV; GFXGLTextureType[GFXFormatR16G16B16A16] = GL_UNSIGNED_SHORT; GFXGLTextureType[GFXFormatBC1] = GL_UNSIGNED_BYTE; GFXGLTextureType[GFXFormatBC2] = GL_UNSIGNED_BYTE; diff --git a/Engine/source/gui/3d/guiTSControl.cpp b/Engine/source/gui/3d/guiTSControl.cpp index ba882278a..0a01a1d25 100644 --- a/Engine/source/gui/3d/guiTSControl.cpp +++ b/Engine/source/gui/3d/guiTSControl.cpp @@ -387,7 +387,7 @@ void GuiTSCtrl::_internalRender(RectI guiViewport, RectI renderViewport, Frustum GFX->setViewport(renderViewport); // Clear the zBuffer so GUI doesn't hose object rendering accidentally - GFX->clear(GFXClearZBuffer, ColorI(20, 20, 20), 1.0f, 0); + GFX->clear(GFXClearZBuffer, ColorI(20, 20, 20), 0.0f, 0); GFX->setFrustum(frustum); mSaveProjection = GFX->getProjectionMatrix(); diff --git a/Engine/source/gui/core/guiCanvas.cpp b/Engine/source/gui/core/guiCanvas.cpp index de57ce280..a1d945ecf 100644 --- a/Engine/source/gui/core/guiCanvas.cpp +++ b/Engine/source/gui/core/guiCanvas.cpp @@ -1915,7 +1915,7 @@ void GuiCanvas::renderFrame(bool preRenderOnly, bool bufferSwap /* = true */) // Clear the current viewport area GFX->setViewport( screenRect ); - GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, gCanvasClearColor, 1.0f, 0 ); + GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, gCanvasClearColor, 0.0f, 0 ); resetUpdateRegions(); diff --git a/Engine/source/lighting/shadowMap/cubeLightShadowMap.cpp b/Engine/source/lighting/shadowMap/cubeLightShadowMap.cpp index 5ab65b357..df8466bd3 100644 --- a/Engine/source/lighting/shadowMap/cubeLightShadowMap.cpp +++ b/Engine/source/lighting/shadowMap/cubeLightShadowMap.cpp @@ -170,7 +170,7 @@ void CubeLightShadowMap::_render( RenderPassManager* renderPass, mTarget->attachTexture(GFXTextureTarget::Color0, mCubemap, i); mTarget->attachTexture(GFXTextureTarget::DepthStencil, _getDepthTarget( mTexSize, mTexSize )); GFX->setActiveRenderTarget(mTarget); - GFX->clear( GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 1.0f, 0 ); + GFX->clear( GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 0.0f, 0 ); // Create scene state, prep it SceneManager* sceneManager = diffuseState->getSceneManager(); diff --git a/Engine/source/lighting/shadowMap/dualParaboloidLightShadowMap.cpp b/Engine/source/lighting/shadowMap/dualParaboloidLightShadowMap.cpp index 6f4685006..d0d59db1f 100644 --- a/Engine/source/lighting/shadowMap/dualParaboloidLightShadowMap.cpp +++ b/Engine/source/lighting/shadowMap/dualParaboloidLightShadowMap.cpp @@ -71,7 +71,7 @@ void DualParaboloidLightShadowMap::_render( RenderPassManager* renderPass, mTarget->attachTexture(GFXTextureTarget::Color0, mShadowMapTex); mTarget->attachTexture( GFXTextureTarget::DepthStencil, mShadowMapDepth ); GFX->setActiveRenderTarget(mTarget); - GFX->clear(GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI::WHITE, 1.0f, 0); + GFX->clear(GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI::WHITE, 0.0f, 0); const bool bUseSinglePassDPM = (p->shadowType == ShadowType_DualParaboloidSinglePass); diff --git a/Engine/source/lighting/shadowMap/paraboloidLightShadowMap.cpp b/Engine/source/lighting/shadowMap/paraboloidLightShadowMap.cpp index 91a76efdc..211ea3790 100644 --- a/Engine/source/lighting/shadowMap/paraboloidLightShadowMap.cpp +++ b/Engine/source/lighting/shadowMap/paraboloidLightShadowMap.cpp @@ -113,7 +113,7 @@ void ParaboloidLightShadowMap::_render( RenderPassManager* renderPass, mTarget->attachTexture( GFXTextureTarget::DepthStencil, _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() ) ); GFX->setActiveRenderTarget(mTarget); - GFX->clear(GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 1.0f, 0); + GFX->clear(GFXClearTarget | GFXClearStencil | GFXClearZBuffer, ColorI(255,255,255,255), 0.0f, 0); // Create scene state, prep it SceneManager* sceneManager = diffuseState->getSceneManager(); diff --git a/Engine/source/lighting/shadowMap/pssmLightShadowMap.cpp b/Engine/source/lighting/shadowMap/pssmLightShadowMap.cpp index 86e4bc6bc..c5061e880 100644 --- a/Engine/source/lighting/shadowMap/pssmLightShadowMap.cpp +++ b/Engine/source/lighting/shadowMap/pssmLightShadowMap.cpp @@ -168,6 +168,7 @@ void PSSMLightShadowMap::_roundProjection(const MatrixF& lightMat, const MatrixF { // Round to the nearest shadowmap texel, this helps reduce shimmering MatrixF currentProj = GFX->getProjectionMatrix(); + currentProj.reverseProjection(); currentProj = cropMatrix * currentProj * lightMat; // Project origin to screen. @@ -226,13 +227,15 @@ void PSSMLightShadowMap::_render( RenderPassManager* renderPass, mTarget->attachTexture( GFXTextureTarget::Color0, mShadowMapTex ); mTarget->attachTexture( GFXTextureTarget::DepthStencil, mShadowMapDepth ); GFX->setActiveRenderTarget( mTarget ); - GFX->clear( GFXClearStencil | GFXClearZBuffer | GFXClearTarget, ColorI(255,255,255), 1.0f, 0 ); + GFX->clear( GFXClearStencil | GFXClearZBuffer | GFXClearTarget, ColorI(255,255,255), 0.0f, 0 ); // Calculate our standard light matrices MatrixF lightMatrix; calcLightMatrices( lightMatrix, diffuseState->getCameraFrustum() ); lightMatrix.inverse(); - MatrixF lightViewProj = GFX->getProjectionMatrix() * lightMatrix; + MatrixF tempProjMat = GFX->getProjectionMatrix(); + tempProjMat.reverseProjection(); + MatrixF lightViewProj = tempProjMat * lightMatrix; // TODO: This is just retrieving the near and far calculated // in calcLightMatrices... we should make that clear. @@ -245,7 +248,7 @@ void PSSMLightShadowMap::_render( RenderPassManager* renderPass, _calcSplitPos(fullFrustum); - mWorldToLightProj = GFX->getProjectionMatrix() * toLightSpace; + mWorldToLightProj = tempProjMat * toLightSpace; // Apply the PSSM const F32 savedSmallestVisible = TSShapeInstance::smSmallestVisiblePixelSize; @@ -320,7 +323,9 @@ void PSSMLightShadowMap::_render( RenderPassManager* renderPass, // Crop matrix multiply needs to be post-projection. MatrixF alightProj = GFX->getProjectionMatrix(); + alightProj.reverseProjection(); alightProj = cropMatrix * alightProj; + alightProj.reverseProjection(); // Set our new projection GFX->setProjectionMatrix(alightProj); diff --git a/Engine/source/lighting/shadowMap/singleLightShadowMap.cpp b/Engine/source/lighting/shadowMap/singleLightShadowMap.cpp index e8dc240a2..cd4724838 100644 --- a/Engine/source/lighting/shadowMap/singleLightShadowMap.cpp +++ b/Engine/source/lighting/shadowMap/singleLightShadowMap.cpp @@ -80,7 +80,7 @@ void SingleLightShadowMap::_render( RenderPassManager* renderPass, mTarget->attachTexture( GFXTextureTarget::DepthStencil, _getDepthTarget( mShadowMapTex->getWidth(), mShadowMapTex->getHeight() ) ); GFX->setActiveRenderTarget(mTarget); - GFX->clear(GFXClearStencil | GFXClearZBuffer | GFXClearTarget, ColorI(255,255,255), 1.0f, 0); + GFX->clear(GFXClearStencil | GFXClearZBuffer | GFXClearTarget, ColorI(255,255,255), 0.0f, 0); SceneManager* sceneManager = diffuseState->getSceneManager(); diff --git a/Engine/source/math/mMatrix.cpp b/Engine/source/math/mMatrix.cpp index 33f0c821e..0131699b9 100644 --- a/Engine/source/math/mMatrix.cpp +++ b/Engine/source/math/mMatrix.cpp @@ -160,6 +160,14 @@ bool MatrixF::fullInverse() return true; } +void MatrixF::reverseProjection() +{ + m[idx(0, 2)] = m[idx(0, 3)] - m[idx(0, 2)]; + m[idx(1, 2)] = m[idx(1, 3)] - m[idx(1, 2)]; + m[idx(2, 2)] = m[idx(2, 3)] - m[idx(2, 2)]; + m[idx(3, 2)] = m[idx(3, 3)] - m[idx(3, 2)]; +} + EulerF MatrixF::toEuler() const { const F32 * mat = m; diff --git a/Engine/source/math/mMatrix.h b/Engine/source/math/mMatrix.h index 9591bc308..d245e55ec 100644 --- a/Engine/source/math/mMatrix.h +++ b/Engine/source/math/mMatrix.h @@ -123,6 +123,9 @@ public: /// be used if the matrix has something other than (0,0,0,1) in the bottom row. bool fullInverse(); + /// Reverse depth for projection matrix + /// Simplifies reversal matrix mult to 4 subtractions + void reverseProjection(); /// Swaps rows and columns into matrix. void transposeTo(F32 *matrix) const; diff --git a/Engine/source/math/mathUtils.cpp b/Engine/source/math/mathUtils.cpp index 8b9cd9fab..458abfec1 100644 --- a/Engine/source/math/mathUtils.cpp +++ b/Engine/source/math/mathUtils.cpp @@ -439,6 +439,7 @@ bool mProjectWorldToScreen( const Point3F &in, const MatrixF &projection ) { MatrixF worldProjection = projection; + worldProjection.reverseProjection(); worldProjection.mul(world); return mProjectWorldToScreen( in, out, view, worldProjection ); @@ -487,6 +488,7 @@ void mProjectScreenToWorld( const Point3F &in, F32 znear ) { MatrixF invWorldProjection = projection; + invWorldProjection.reverseProjection(); invWorldProjection.mul(world); invWorldProjection.inverse(); @@ -1472,7 +1474,7 @@ void makeProjection( MatrixF *outMatrix, F32 farPlane, bool gfxRotate) { - const bool isGL = GFX->getAdapterType() == OpenGL; + const bool isGL = false; // No longer need special OGL case w/ reversed depth Point4F row; row.x = 2.0f * nearPlane / (right - left); row.y = 0.0f; @@ -1502,6 +1504,7 @@ void makeProjection( MatrixF *outMatrix, if (gfxRotate) outMatrix->mul(sGFXProjRotMatrix); + outMatrix->reverseProjection(); } //----------------------------------------------------------------------------- @@ -1545,6 +1548,7 @@ void makeOrthoProjection( MatrixF *outMatrix, if ( gfxRotate ) outMatrix->mul( sGFXProjRotMatrix ); + outMatrix->reverseProjection(); } //----------------------------------------------------------------------------- diff --git a/Engine/source/renderInstance/renderDeferredMgr.cpp b/Engine/source/renderInstance/renderDeferredMgr.cpp index c8c65c280..664302528 100644 --- a/Engine/source/renderInstance/renderDeferredMgr.cpp +++ b/Engine/source/renderInstance/renderDeferredMgr.cpp @@ -321,7 +321,7 @@ void RenderDeferredMgr::render( SceneRenderState *state ) const bool isRenderingToTarget = _onPreRender(state); // Clear z-buffer and g-buffer. - GFX->clear(GFXClearZBuffer | GFXClearStencil, LinearColorF::ZERO, 1.0f, 0); + GFX->clear(GFXClearZBuffer | GFXClearStencil, LinearColorF::ZERO, 0.0f, 0); GFX->clearColorAttachment(0, LinearColorF::ONE);//normdepth GFX->clearColorAttachment(1, LinearColorF::ZERO);//albedo GFX->clearColorAttachment(2, LinearColorF::ZERO);//matinfo diff --git a/Engine/source/renderInstance/renderFormatChanger.cpp b/Engine/source/renderInstance/renderFormatChanger.cpp index 5e9fe6e31..893ef2cf6 100644 --- a/Engine/source/renderInstance/renderFormatChanger.cpp +++ b/Engine/source/renderInstance/renderFormatChanger.cpp @@ -129,7 +129,7 @@ void RenderFormatToken::process(SceneRenderState *state, RenderPassStateBin *cal GFX->setViewport( mTarget.getViewport() ); // Clear - GFX->clear(GFXClearTarget | GFXClearZBuffer | GFXClearStencil, gCanvasClearColor, 1.0f, 0); + GFX->clear(GFXClearTarget | GFXClearZBuffer | GFXClearStencil, gCanvasClearColor, 0.0f, 0); // Set active z target on render pass if(mTargetDepthStencilTexture[mTargetChainIdx].isValid()) diff --git a/Engine/source/scene/reflector.cpp b/Engine/source/scene/reflector.cpp index b9f19fe08..09c03e426 100644 --- a/Engine/source/scene/reflector.cpp +++ b/Engine/source/scene/reflector.cpp @@ -418,7 +418,7 @@ void CubeReflector::updateFace( const ReflectParams ¶ms, U32 faceidx, Point3 GFX->clearTextureStateImmediate(0); mRenderTarget->attachTexture( GFXTextureTarget::Color0, mCubemap, faceidx ); GFX->setActiveRenderTarget(mRenderTarget); - GFX->clear( GFXClearStencil | GFXClearTarget | GFXClearZBuffer, gCanvasClearColor, 1.0f, 0 ); + GFX->clear( GFXClearStencil | GFXClearTarget | GFXClearZBuffer, gCanvasClearColor, 0.0f, 0 ); SceneRenderState reflectRenderState ( @@ -624,7 +624,7 @@ void PlaneReflector::updateReflection( const ReflectParams ¶ms ) // render a skirt or something in its lower half. // LinearColorF clearColor = gClientSceneGraph->getAmbientLightColor(); - GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, clearColor, 1.0f, 0 ); + GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, clearColor, 0.0f, 0 ); if(GFX->getCurrentRenderStyle() == GFXDevice::RS_StereoSideBySide) { @@ -845,6 +845,7 @@ MatrixF PlaneReflector::getFrustumClipProj( MatrixF &modelview ) // Manipulate projection matrix //------------------------------------------------------------------------ MatrixF proj = GFX->getProjectionMatrix(); + proj.reverseProjection(); // convert back into normal depth space from reversed, otherwise we have to figure out how to convert this modification into inverted depth space proj.mul( invRotMat ); // reverse rotation imposed by Torque proj.transpose(); // switch to row-major order @@ -862,14 +863,16 @@ MatrixF PlaneReflector::getFrustumClipProj( MatrixF &modelview ) Vector4F c = clipPlane * a; + // [ZREV] This was a hack to handle OGL using -1 to 1 as its Z range // CodeReview [ags 1/23/08] Come up with a better way to deal with this. - if(GFX->getAdapterType() == OpenGL) - c.z += 1.0f; + //if(GFX->getAdapterType() == OpenGL) + // c.z += 1.0f; // Replace the third column of the projection matrix proj.setColumn( 2, c ); proj.transpose(); // convert back to column major order proj.mul( rotMat ); // restore Torque rotation + proj.reverseProjection(); // convert back to reversed depth space return proj; } diff --git a/Engine/source/shaderGen/GLSL/shaderFeatureGLSL.cpp b/Engine/source/shaderGen/GLSL/shaderFeatureGLSL.cpp index d044fbad3..143a1590a 100644 --- a/Engine/source/shaderGen/GLSL/shaderFeatureGLSL.cpp +++ b/Engine/source/shaderGen/GLSL/shaderFeatureGLSL.cpp @@ -1735,7 +1735,7 @@ void VertPositionGLSL::processVert( Vector &componentList, outPosition, modelview, inPosition ) ); if (fd.materialFeatures[MFT_isBackground]) { - meta->addStatement(new GenOp(" @ = @.xyww;\r\n", outPosition, outPosition)); + meta->addStatement(new GenOp(" @.z = 0.0f;\r\n", outPosition)); } output = meta; } diff --git a/Engine/source/shaderGen/HLSL/shaderFeatureHLSL.cpp b/Engine/source/shaderGen/HLSL/shaderFeatureHLSL.cpp index 40b2b4a47..af8866301 100644 --- a/Engine/source/shaderGen/HLSL/shaderFeatureHLSL.cpp +++ b/Engine/source/shaderGen/HLSL/shaderFeatureHLSL.cpp @@ -1781,7 +1781,7 @@ void VertPositionHLSL::processVert( Vector &componentList, if (fd.materialFeatures[MFT_isBackground]) { - meta->addStatement(new GenOp(" @ = @.xyww;\r\n", outPosition, outPosition)); + meta->addStatement(new GenOp(" @.z = 0.0f;\r\n", outPosition)); } output = meta; diff --git a/Engine/source/util/imposterCapture.cpp b/Engine/source/util/imposterCapture.cpp index ffc36bad8..73ab1d9d4 100644 --- a/Engine/source/util/imposterCapture.cpp +++ b/Engine/source/util/imposterCapture.cpp @@ -234,7 +234,7 @@ void ImposterCapture::_renderToTexture( GFXTexHandle texHandle, GBitmap *outBitm mRenderTarget->attachTexture( GFXTextureTarget::DepthStencil, mDepthBuffer ); GFX->setActiveRenderTarget( mRenderTarget ); - GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, color, 1.0f, 0 ); + GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, color, 0.0f, 0 ); mShapeInstance->render( mRData, mDl, 1.0f ); diff --git a/Templates/BaseGame/game/core/rendering/scripts/renderManager.tscript b/Templates/BaseGame/game/core/rendering/scripts/renderManager.tscript index 8c024c222..34a48f64a 100644 --- a/Templates/BaseGame/game/core/rendering/scripts/renderManager.tscript +++ b/Templates/BaseGame/game/core/rendering/scripts/renderManager.tscript @@ -35,7 +35,7 @@ function initRenderManager() //When hdr is enabled this will be changed to the appropriate format format = "GFXFormatR16G16B16A16F"; - depthFormat = "GFXFormatD24S8"; + depthFormat = "GFXFormatD32FS8X24"; // 64-bit depth format. Old 32-bit format was "GFXFormatD24S8" aaLevel = 0; // -1 = match backbuffer // The contents of the back buffer before this format token is executed diff --git a/Templates/BaseGame/game/core/rendering/shaders/basicCloudsV.hlsl b/Templates/BaseGame/game/core/rendering/shaders/basicCloudsV.hlsl index 79976343c..63103051a 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/basicCloudsV.hlsl +++ b/Templates/BaseGame/game/core/rendering/shaders/basicCloudsV.hlsl @@ -46,7 +46,7 @@ ConnectData main( CloudVert IN ) ConnectData OUT; OUT.hpos = mul(modelview, float4(IN.pos,1.0)); - OUT.hpos.z = OUT.hpos.w; + OUT.hpos.z = 0.0f; // OUT.hpos.w; // reverse depth -- put cloud layer in the far dist and let render order hints sort it out? float2 uv = IN.uv0; uv += texOffset; uv *= texScale; diff --git a/Templates/BaseGame/game/core/rendering/shaders/cloudLayerV.hlsl b/Templates/BaseGame/game/core/rendering/shaders/cloudLayerV.hlsl index 49b325b42..6650b106f 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/cloudLayerV.hlsl +++ b/Templates/BaseGame/game/core/rendering/shaders/cloudLayerV.hlsl @@ -63,7 +63,7 @@ ConnectData main( CloudVert IN ) ConnectData OUT; OUT.hpos = mul(modelview, float4(IN.pos,1.0)); - OUT.hpos.z = OUT.hpos.w; + OUT.hpos.z = 0.0f; //OUT.hpos.w; // for reversed depth // Offset the uv so we don't have a seam directly over our head. float2 uv = IN.uv0 + float2( 0.5, 0.5 ); diff --git a/Templates/BaseGame/game/core/rendering/shaders/gl/basicCloudsV.glsl b/Templates/BaseGame/game/core/rendering/shaders/gl/basicCloudsV.glsl index 5cd9d219e..5bce347d3 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/gl/basicCloudsV.glsl +++ b/Templates/BaseGame/game/core/rendering/shaders/gl/basicCloudsV.glsl @@ -41,7 +41,7 @@ out vec2 texCoord; void main() { gl_Position = tMul(modelview, IN_pos); - gl_Position.z = gl_Position.w; + gl_Position.z = 0.0; // gl_Position.w; // reverse depth -- put cloud layer in the far dist and let render order hints sort it out? vec2 uv = IN_uv0; uv += texOffset; diff --git a/Templates/BaseGame/game/core/rendering/shaders/gl/cloudLayerV.glsl b/Templates/BaseGame/game/core/rendering/shaders/gl/cloudLayerV.glsl index a1085f755..6a7351aac 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/gl/cloudLayerV.glsl +++ b/Templates/BaseGame/game/core/rendering/shaders/gl/cloudLayerV.glsl @@ -62,7 +62,7 @@ void main() vec2 IN_uv0 = vTexCoord0.st; gl_Position = modelview * IN_pos; - gl_Position.z = gl_Position.w; + gl_Position.z = 0.0;//gl_Position.w; // reversed depth -- put clouds in the far distance (0) with other sky-type objects // Offset the uv so we don't have a seam directly over our head. vec2 uv = IN_uv0 + vec2( 0.5, 0.5 ); diff --git a/Templates/BaseGame/game/core/rendering/shaders/water/gl/waterV.glsl b/Templates/BaseGame/game/core/rendering/shaders/water/gl/waterV.glsl index 490af63a7..25146669b 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/water/gl/waterV.glsl +++ b/Templates/BaseGame/game/core/rendering/shaders/water/gl/waterV.glsl @@ -173,7 +173,7 @@ void main() // } // Save world space camera dist/depth of the outgoing pixel - OUT_rippleTexCoord2.z = OUT_hpos.z; + OUT_rippleTexCoord2.z = OUT_hpos.w; // for reversed depth // Convert to reflection texture space OUT_posPostWave = tMul( texGen, OUT_posPostWave ); diff --git a/Templates/BaseGame/game/core/rendering/shaders/water/waterV.hlsl b/Templates/BaseGame/game/core/rendering/shaders/water/waterV.hlsl index c869f0e9f..1e8ded792 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/water/waterV.hlsl +++ b/Templates/BaseGame/game/core/rendering/shaders/water/waterV.hlsl @@ -149,7 +149,7 @@ ConnectData main( VertData IN ) // } // Save world space camera dist/depth of the outgoing pixel - OUT.rippleTexCoord2.z = OUT.hpos.z; + OUT.rippleTexCoord2.z = OUT.hpos.w; // use the world-coordinate version of depth, this will work with either projection model // Convert to reflection texture space OUT.posPostWave = mul( texGen, OUT.posPostWave ); From c64c2212ad7a6ce757ccb2f7ce07a13d9f34a263 Mon Sep 17 00:00:00 2001 From: AtomicWalrus Date: Sat, 15 Apr 2023 15:21:49 -0600 Subject: [PATCH 2/2] Fix for MacOS OGL 4.1 support Workaround: MacOS does not have access to glClipControl. Because the lower range of the 32F buffer is not available using this workaround, the improvements to depth behavior from depth reversal will be limited on MacOS. Behavior is equivalent to reversed depth on a 24UINT buffer; Better than stock, but not dramatically. --- Engine/source/gfx/gl/gfxGLDevice.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Engine/source/gfx/gl/gfxGLDevice.cpp b/Engine/source/gfx/gl/gfxGLDevice.cpp index 9c0df9ce1..07dfcb10d 100644 --- a/Engine/source/gfx/gl/gfxGLDevice.cpp +++ b/Engine/source/gfx/gl/gfxGLDevice.cpp @@ -191,7 +191,12 @@ void GFXGLDevice::initGLState() glGenVertexArrays(1, &vao); glBindVertexArray(vao); + // MacOS uses OGL 4.1. This workaround is functional, but will not provide the improvied depth performance. +#if defined(__MACOSX__) + glDepthRangef(0.0, 1.0); +#else glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE); +#endif //enable sRGB glEnable(GL_FRAMEBUFFER_SRGB);