浏览代码

Merge pull request #1011 from AtomicWalrus/ReverseDepthOnly_PR

Reverse depth & 32F buffer format
Brian Roberts 2 年之前
父节点
当前提交
4f403afe9a
共有 37 个文件被更改,包括 96 次插入57 次删除
  1. 2 2
      Engine/source/T3D/gameFunctions.cpp
  2. 1 1
      Engine/source/T3D/lightFlareData.cpp
  3. 2 2
      Engine/source/environment/VolumetricFog.cpp
  4. 1 1
      Engine/source/environment/skyBox.cpp
  5. 1 1
      Engine/source/environment/waterPlane.cpp
  6. 1 0
      Engine/source/gfx/D3D11/gfxD3D11EnumTranslate.cpp
  7. 5 0
      Engine/source/gfx/D3D11/gfxD3D11TextureManager.cpp
  8. 1 0
      Engine/source/gfx/gfxAPI.cpp
  9. 1 0
      Engine/source/gfx/gfxEnums.h
  10. 10 0
      Engine/source/gfx/gfxFormatUtils.cpp
  11. 1 1
      Engine/source/gfx/gfxStateBlock.cpp
  12. 1 0
      Engine/source/gfx/gfxStringEnumTranslate.cpp
  13. 17 23
      Engine/source/gfx/gl/gfxGLDevice.cpp
  14. 3 0
      Engine/source/gfx/gl/gfxGLEnumTranslate.cpp
  15. 1 1
      Engine/source/gui/3d/guiTSControl.cpp
  16. 1 1
      Engine/source/gui/core/guiCanvas.cpp
  17. 1 1
      Engine/source/lighting/shadowMap/cubeLightShadowMap.cpp
  18. 1 1
      Engine/source/lighting/shadowMap/dualParaboloidLightShadowMap.cpp
  19. 1 1
      Engine/source/lighting/shadowMap/paraboloidLightShadowMap.cpp
  20. 8 3
      Engine/source/lighting/shadowMap/pssmLightShadowMap.cpp
  21. 1 1
      Engine/source/lighting/shadowMap/singleLightShadowMap.cpp
  22. 8 0
      Engine/source/math/mMatrix.cpp
  23. 3 0
      Engine/source/math/mMatrix.h
  24. 5 1
      Engine/source/math/mathUtils.cpp
  25. 1 1
      Engine/source/renderInstance/renderDeferredMgr.cpp
  26. 1 1
      Engine/source/renderInstance/renderFormatChanger.cpp
  27. 7 4
      Engine/source/scene/reflector.cpp
  28. 1 1
      Engine/source/shaderGen/GLSL/shaderFeatureGLSL.cpp
  29. 1 1
      Engine/source/shaderGen/HLSL/shaderFeatureHLSL.cpp
  30. 1 1
      Engine/source/util/imposterCapture.cpp
  31. 1 1
      Templates/BaseGame/game/core/rendering/scripts/renderManager.tscript
  32. 1 1
      Templates/BaseGame/game/core/rendering/shaders/basicCloudsV.hlsl
  33. 1 1
      Templates/BaseGame/game/core/rendering/shaders/cloudLayerV.hlsl
  34. 1 1
      Templates/BaseGame/game/core/rendering/shaders/gl/basicCloudsV.glsl
  35. 1 1
      Templates/BaseGame/game/core/rendering/shaders/gl/cloudLayerV.glsl
  36. 1 1
      Templates/BaseGame/game/core/rendering/shaders/water/gl/waterV.glsl
  37. 1 1
      Templates/BaseGame/game/core/rendering/shaders/water/waterV.hlsl

+ 2 - 2
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();

+ 1 - 1
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);

+ 2 - 2
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);
 

+ 1 - 1
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.

+ 1 - 1
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);
    }
 

+ 1 - 0
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;

+ 5 - 0
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;

+ 1 - 0
Engine/source/gfx/gfxAPI.cpp

@@ -128,6 +128,7 @@ ImplementEnumType( GFXFormat,
    { GFXFormatD24S8, "GFXFormatD24S8" },
    { GFXFormatD24FS8, "GFXFormatD24FS8" },
    { GFXFormatD16, "GFXFormatD16" }, 
+   { GFXFormatD32FS8X24, "GFXFormatD32FS8X24" },
 
    { GFXFormatR32G32B32A32F, "GFXFormatR32G32B32A32F" }, 
    { GFXFormatR16G16B16A16F, "GFXFormatR16G16B16A16F" }, 

+ 1 - 0
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...

+ 10 - 0
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
 };
 
 //-----------------------------------------------------------------------------

+ 1 - 1
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;
 

+ 1 - 0
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 );

+ 17 - 23
Engine/source/gfx/gl/gfxGLDevice.cpp

@@ -191,6 +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);
 
@@ -794,38 +800,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 );

+ 3 - 0
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;

+ 1 - 1
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();

+ 1 - 1
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();
 

+ 1 - 1
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();

+ 1 - 1
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);
 

+ 1 - 1
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();

+ 8 - 3
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);

+ 1 - 1
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();
    

+ 8 - 0
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;

+ 3 - 0
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;
 

+ 5 - 1
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();
 }
 
 //-----------------------------------------------------------------------------

+ 1 - 1
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

+ 1 - 1
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())

+ 7 - 4
Engine/source/scene/reflector.cpp

@@ -418,7 +418,7 @@ void CubeReflector::updateFace( const ReflectParams &params, 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 &params )
    // 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;
 }

+ 1 - 1
Engine/source/shaderGen/GLSL/shaderFeatureGLSL.cpp

@@ -1735,7 +1735,7 @@ void VertPositionGLSL::processVert( Vector<ShaderComponent*> &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;
 }

+ 1 - 1
Engine/source/shaderGen/HLSL/shaderFeatureHLSL.cpp

@@ -1781,7 +1781,7 @@ void VertPositionHLSL::processVert( Vector<ShaderComponent*> &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;

+ 1 - 1
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 );
 

+ 1 - 1
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

+ 1 - 1
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;

+ 1 - 1
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 );
    

+ 1 - 1
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;

+ 1 - 1
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 );

+ 1 - 1
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 );

+ 1 - 1
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 );