From aeb03cc4e4ae4b33f48cdcdc745943b7d16ac54a Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Sun, 3 Nov 2019 00:39:43 +0100 Subject: [PATCH] Allow switching off "Carmacks Reverse" via CVar r_useCarmacksReverse mainly useful for comparisons The Z-Fail code is based on Leith Bade's code by the way: https://github.com/ljbade/doom3.gpl/commit/d4de024341e79e0ac1dfb54fb528859f8ccea605 --- neo/renderer/RenderSystem_init.cpp | 3 ++ neo/renderer/draw_common.cpp | 75 ++++++++++++++++-------------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/neo/renderer/RenderSystem_init.cpp b/neo/renderer/RenderSystem_init.cpp index a314c018..28bb9b82 100644 --- a/neo/renderer/RenderSystem_init.cpp +++ b/neo/renderer/RenderSystem_init.cpp @@ -225,6 +225,9 @@ idCVar r_debugRenderToTexture( "r_debugRenderToTexture", "0", CVAR_RENDERER | CV // DG: let users disable the "scale menus to 4:3" hack idCVar r_scaleMenusTo43( "r_scaleMenusTo43", "1", CVAR_RENDERER | CVAR_ARCHIVE | CVAR_BOOL, "Scale menus, fullscreen videos and PDA to 4:3 aspect ratio" ); +// DG: the fscking patent has finally expired +idCVar r_useCarmacksReverse( "r_useCarmacksReverse", "1", CVAR_RENDERER | CVAR_ARCHIVE | CVAR_BOOL, "Use Z-Fail (Carmack's Reverse) when rendering shadows" ); + // define qgl functions #define QGLPROC(name, rettype, args) rettype (APIENTRYP q##name) args; diff --git a/neo/renderer/draw_common.cpp b/neo/renderer/draw_common.cpp index 131dad85..a12070e4 100644 --- a/neo/renderer/draw_common.cpp +++ b/neo/renderer/draw_common.cpp @@ -31,6 +31,8 @@ If you have questions concerning this license or the applicable additional terms #include "renderer/tr_local.h" +extern idCVar r_useCarmacksReverse; + /* ===================== RB_BakeTextureMatrixIntoTexgen @@ -1140,42 +1142,21 @@ static void RB_T_Shadow( const drawSurf_t *surf ) { return; } -#if 0 // LEITH: the original patent free "preload" code + if( !r_useCarmacksReverse.GetBool() ) { // LEITH: the original patent free "preload" code - // patent-free work around - if ( !external ) { - // "preload" the stencil buffer with the number of volumes - // that get clipped by the near or far clip plane - qglStencilOp( GL_KEEP, tr.stencilDecr, tr.stencilDecr ); - GL_Cull( CT_FRONT_SIDED ); - RB_DrawShadowElementsWithCounters( tri, numIndexes ); - qglStencilOp( GL_KEEP, tr.stencilIncr, tr.stencilIncr ); - GL_Cull( CT_BACK_SIDED ); - RB_DrawShadowElementsWithCounters( tri, numIndexes ); - } + // patent-free work around + if ( !external ) { + // "preload" the stencil buffer with the number of volumes + // that get clipped by the near or far clip plane + qglStencilOp( GL_KEEP, tr.stencilDecr, tr.stencilDecr ); + GL_Cull( CT_FRONT_SIDED ); + RB_DrawShadowElementsWithCounters( tri, numIndexes ); + qglStencilOp( GL_KEEP, tr.stencilIncr, tr.stencilIncr ); + GL_Cull( CT_BACK_SIDED ); + RB_DrawShadowElementsWithCounters( tri, numIndexes ); + } - // traditional depth-pass stencil shadows - qglStencilOp( GL_KEEP, GL_KEEP, tr.stencilIncr ); - GL_Cull( CT_FRONT_SIDED ); - RB_DrawShadowElementsWithCounters( tri, numIndexes ); - - qglStencilOp( GL_KEEP, GL_KEEP, tr.stencilDecr ); - GL_Cull( CT_BACK_SIDED ); - RB_DrawShadowElementsWithCounters( tri, numIndexes ); - -#else // LEITH: the patented "Carmack's Reverse" code - - // patented depth-fail stencil shadows - if ( !external ) { - qglStencilOp( GL_KEEP, tr.stencilDecr, GL_KEEP ); - GL_Cull( CT_FRONT_SIDED ); - RB_DrawShadowElementsWithCounters( tri, numIndexes ); - qglStencilOp( GL_KEEP, tr.stencilIncr, GL_KEEP ); - GL_Cull( CT_BACK_SIDED ); - RB_DrawShadowElementsWithCounters( tri, numIndexes ); - } - // traditional depth-pass stencil shadows - else { + // traditional depth-pass stencil shadows qglStencilOp( GL_KEEP, GL_KEEP, tr.stencilIncr ); GL_Cull( CT_FRONT_SIDED ); RB_DrawShadowElementsWithCounters( tri, numIndexes ); @@ -1183,9 +1164,33 @@ static void RB_T_Shadow( const drawSurf_t *surf ) { qglStencilOp( GL_KEEP, GL_KEEP, tr.stencilDecr ); GL_Cull( CT_BACK_SIDED ); RB_DrawShadowElementsWithCounters( tri, numIndexes ); + + } else { // LEITH: the (formerly patented) "Carmack's Reverse" code + + // DG: that bloody patent on depth-fail stencil shadows has finally expired on 2019-10-13, + // so use them (see https://patents.google.com/patent/US6384822B1/en for expiration status) + + // depth-fail/Z-Fail stencil shadows + if ( !external ) { + qglStencilOp( GL_KEEP, tr.stencilDecr, GL_KEEP ); + GL_Cull( CT_FRONT_SIDED ); + RB_DrawShadowElementsWithCounters( tri, numIndexes ); + qglStencilOp( GL_KEEP, tr.stencilIncr, GL_KEEP ); + GL_Cull( CT_BACK_SIDED ); + RB_DrawShadowElementsWithCounters( tri, numIndexes ); + } + // traditional depth-pass stencil shadows + else { + qglStencilOp( GL_KEEP, GL_KEEP, tr.stencilIncr ); + GL_Cull( CT_FRONT_SIDED ); + RB_DrawShadowElementsWithCounters( tri, numIndexes ); + + qglStencilOp( GL_KEEP, GL_KEEP, tr.stencilDecr ); + GL_Cull( CT_BACK_SIDED ); + RB_DrawShadowElementsWithCounters( tri, numIndexes ); + } } -#endif } /*