From de299fb6e0ef9b8d0e2334e6b3deff29b42b2d08 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Mon, 13 Apr 2020 05:16:18 +0200 Subject: [PATCH 1/4] tr_shader: store heathaze and liquid normal map in normal map slot --- src/engine/renderer/tr_shade.cpp | 12 ++++++------ src/engine/renderer/tr_shader.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/engine/renderer/tr_shade.cpp b/src/engine/renderer/tr_shade.cpp index 23d77b55a5..e5f075fa6f 100644 --- a/src/engine/renderer/tr_shade.cpp +++ b/src/engine/renderer/tr_shade.cpp @@ -2370,7 +2370,7 @@ static void Render_heatHaze( int stage ) GLimp_LogComment( "--- Render_heatHaze ---\n" ); - bool hasNormalMap = pStage->bundle[ TB_COLORMAP ].image[ 0 ] != nullptr; + bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; bool normalMapping = r_normalMapping->integer && hasNormalMap; @@ -2435,9 +2435,9 @@ static void Render_heatHaze( int stage ) if ( normalMapping ) { // bind u_NormalMap - GL_BindToTMU( 0, pStage->bundle[ TB_COLORMAP ].image[ 0 ] ); + GL_BindToTMU( 0, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - gl_heatHazeShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_COLORMAP ] ); + gl_heatHazeShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_NORMALMAP ] ); vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -2475,7 +2475,7 @@ static void Render_liquid( int stage ) GLimp_LogComment( "--- Render_liquid ---\n" ); - bool hasNormalMap = pStage->bundle[ TB_COLORMAP ].image[ 0 ] != nullptr; + bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; @@ -2556,7 +2556,7 @@ static void Render_liquid( int stage ) if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) { // bind u_NormalMap - GL_BindToTMU( 3, pStage->bundle[ TB_COLORMAP ].image[ 0 ] ); + GL_BindToTMU( 3, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); } else { @@ -2573,7 +2573,7 @@ static void Render_liquid( int stage ) gl_liquidShader->SetUniform_NormalScale( normalScale ); } - gl_liquidShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_COLORMAP ] ); + gl_liquidShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_NORMALMAP ] ); Tess_DrawElements(); diff --git a/src/engine/renderer/tr_shader.cpp b/src/engine/renderer/tr_shader.cpp index 53d40a1f7d..c3733cc353 100644 --- a/src/engine/renderer/tr_shader.cpp +++ b/src/engine/renderer/tr_shader.cpp @@ -4422,6 +4422,18 @@ static void CollapseStages() for ( int i = 0; i < MAX_SHADER_STAGES; i++ ) { + /* Store hethaze and liquid normal map in normal map slot. + + NOTE: liquidMap may be entirely redesigned in the future + to be a variant of diffuseMap (hence supporting all others textures). + */ + if ( stages[ i ].type == stageType_t::ST_HEATHAZEMAP + || stages[ i ].type == stageType_t::ST_LIQUIDMAP ) + { + stages[ i ].bundle[ TB_NORMALMAP ] = stages[ i ].bundle[ TB_COLORMAP ]; + stages[ i ].bundle[ TB_COLORMAP ] = {}; + } + if ( lightStage != -1 && stages[ i ].collapseType != collapseType_t::COLLAPSE_none ) { if ( stages[ i ].dpMaterial ) From 43623bde57e7035d02138458b8dc1b6b21b27f7a Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Mon, 13 Apr 2020 04:38:20 +0200 Subject: [PATCH 2/4] tr_shader: prepare to precompute stuff at shader parsing instead of doing it per frame per surface --- src/engine/renderer/tr_shader.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/engine/renderer/tr_shader.cpp b/src/engine/renderer/tr_shader.cpp index c3733cc353..daa866f390 100644 --- a/src/engine/renderer/tr_shader.cpp +++ b/src/engine/renderer/tr_shader.cpp @@ -4615,6 +4615,13 @@ static void CollapseStages() } shader.numStages = numActiveStages; + + // Do some precomputation. + for ( int s = 0; s < shader.numStages; s++ ) + { + shaderStage_t *stage = &stages[ s ]; + // STUB + } } // *INDENT-ON* From 50723fd9efbf3035be29cb51c72a342fc5b219b3 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Mon, 13 Apr 2020 06:09:23 +0200 Subject: [PATCH 3/4] renderer: compute feature enablement in tr_shader instead of tr_shade Compute them once per shader at map load instead of doing it per frame per surface. --- src/engine/renderer/tr_local.h | 18 ++- src/engine/renderer/tr_shade.cpp | 239 +++++++++--------------------- src/engine/renderer/tr_shader.cpp | 23 ++- 3 files changed, 104 insertions(+), 176 deletions(-) diff --git a/src/engine/renderer/tr_local.h b/src/engine/renderer/tr_local.h index 4acaba818f..c4a0fce349 100644 --- a/src/engine/renderer/tr_local.h +++ b/src/engine/renderer/tr_local.h @@ -1094,7 +1094,6 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out ) bool tcGen_Environment; bool tcGen_Lightmap; - bool heightMapInNormalMap; // material has normalmap suited for parallax mapping bool implicitLightmap; Color::Color32Bit constantColor; // for CGEN_CONST and AGEN_CONST @@ -1125,6 +1124,22 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out ) expression_t fresnelScaleExp; expression_t fresnelBiasExp; + // Available textures. + bool hasNormalMap; + bool hasHeightMap; + bool isHeightMapInNormalMap; + bool hasMaterialMap; + bool isMaterialPhysical; + bool hasGlowMap; + + // Available features. + bool enableNormalMapping; + bool enableDeluxeMapping; + bool enableParallaxMapping; + bool enablePhysicalMapping; + bool enableSpecularMapping; + bool enableGlowMapping; + // normalMap channel scale, negative value flips channel bool hasNormalScale; vec3_t normalScale; @@ -1212,7 +1227,6 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out ) fogPass_t fogPass; // draw a blended pass, possibly with depth test equals bool noFog; - bool heightMapInNormalMap; // material has normalmap suited for parallax mapping bool noParallax; // disable parallax for this material even if it's available float parallaxOffsetBias; // offset the heightmap top relatively to the floor float parallaxDepthScale; // per-shader parallax depth scale diff --git a/src/engine/renderer/tr_shade.cpp b/src/engine/renderer/tr_shade.cpp index e5f075fa6f..2331bf18b9 100644 --- a/src/engine/renderer/tr_shade.cpp +++ b/src/engine/renderer/tr_shade.cpp @@ -744,33 +744,19 @@ static void Render_vertexLighting_DBS_entity( int stage ) GL_State( stateBits ); - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - bool hasMaterialMap = pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] != nullptr; - bool hasGlowMap = pStage->bundle[ TB_GLOWMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - bool isMaterialPhysical = pStage->collapseType == collapseType_t::COLLAPSE_lighting_PBR; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - bool physicalMapping = r_physicalMapping->integer && hasMaterialMap && isMaterialPhysical; - bool specularMapping = r_specularMapping->integer && hasMaterialMap && !isMaterialPhysical; - bool glowMapping = r_glowMapping->integer && hasGlowMap; - // choose right shader program ---------------------------------- tess.vboVertexSprite = false; gl_vertexLightingShader_DBS_entity->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning ); gl_vertexLightingShader_DBS_entity->SetVertexAnimation( tess.vboVertexAnimation ); - gl_vertexLightingShader_DBS_entity->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_vertexLightingShader_DBS_entity->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_vertexLightingShader_DBS_entity->SetParallaxMapping( parallaxMapping ); + gl_vertexLightingShader_DBS_entity->SetParallaxMapping( pStage->enableParallaxMapping ); - gl_vertexLightingShader_DBS_entity->SetReflectiveSpecular( normalMapping && tr.cubeHashTable != nullptr ); + gl_vertexLightingShader_DBS_entity->SetReflectiveSpecular( pStage->enableNormalMapping && tr.cubeHashTable != nullptr ); - gl_vertexLightingShader_DBS_entity->SetPhysicalShading( isMaterialPhysical ); + gl_vertexLightingShader_DBS_entity->SetPhysicalShading( pStage->isMaterialPhysical ); gl_vertexLightingShader_DBS_entity->BindProgram( pStage->deformIndex ); // end choose right shader program ------------------------------ @@ -818,7 +804,7 @@ static void Render_vertexLighting_DBS_entity( int stage ) gl_vertexLightingShader_DBS_entity->SetUniform_AlphaTest( pStage->stateBits ); // bind u_HeightMap - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -830,7 +816,7 @@ static void Render_vertexLighting_DBS_entity( int stage ) gl_vertexLightingShader_DBS_entity->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } @@ -840,7 +826,7 @@ static void Render_vertexLighting_DBS_entity( int stage ) GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_vertexLightingShader_DBS_entity->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) + if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) { // bind u_NormalMap GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -851,7 +837,7 @@ static void Render_vertexLighting_DBS_entity( int stage ) } // bind u_NormalMap - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -860,12 +846,12 @@ static void Render_vertexLighting_DBS_entity( int stage ) gl_vertexLightingShader_DBS_entity->SetUniform_NormalScale( normalScale ); } - if ( physicalMapping || specularMapping ) + if ( pStage->enablePhysicalMapping || pStage->enableSpecularMapping ) { // bind u_MaterialMap GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - if ( specularMapping ) + if ( pStage->enableSpecularMapping ) { float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); @@ -976,7 +962,7 @@ static void Render_vertexLighting_DBS_entity( int stage ) } // bind u_GlowMap - if ( glowMapping ) + if ( pStage->enableGlowMapping ) { GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); } else { @@ -1029,29 +1015,15 @@ static void Render_vertexLighting_DBS_world( int stage ) break; } - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - bool hasMaterialMap = pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] != nullptr; - bool hasGlowMap = pStage->bundle[ TB_GLOWMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - bool isMaterialPhysical = pStage->collapseType == collapseType_t::COLLAPSE_lighting_PBR; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - bool physicalMapping = r_physicalMapping->integer && hasMaterialMap && isMaterialPhysical; - bool specularMapping = r_specularMapping->integer && hasMaterialMap && !isMaterialPhysical; - bool glowMapping = r_glowMapping->integer && hasGlowMap; - // choose right shader program ---------------------------------- tess.vboVertexSprite = false; - gl_vertexLightingShader_DBS_world->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_vertexLightingShader_DBS_world->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_vertexLightingShader_DBS_world->SetParallaxMapping( parallaxMapping ); + gl_vertexLightingShader_DBS_world->SetParallaxMapping( pStage->enableParallaxMapping ); - gl_vertexLightingShader_DBS_world->SetPhysicalShading( isMaterialPhysical ); + gl_vertexLightingShader_DBS_world->SetPhysicalShading( pStage->isMaterialPhysical ); gl_vertexLightingShader_DBS_world->BindProgram( pStage->deformIndex ); // end choose right shader program ------------------------------ @@ -1093,7 +1065,7 @@ static void Render_vertexLighting_DBS_world( int stage ) gl_vertexLightingShader_DBS_world->SetUniform_AlphaTest( pStage->stateBits ); // bind u_HeightMap - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -1105,7 +1077,7 @@ static void Render_vertexLighting_DBS_world( int stage ) gl_vertexLightingShader_DBS_world->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } @@ -1115,7 +1087,7 @@ static void Render_vertexLighting_DBS_world( int stage ) GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_vertexLightingShader_DBS_world->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) + if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) { // bind u_NormalMap GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -1126,7 +1098,7 @@ static void Render_vertexLighting_DBS_world( int stage ) } // bind u_NormalMap - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -1135,12 +1107,12 @@ static void Render_vertexLighting_DBS_world( int stage ) gl_vertexLightingShader_DBS_world->SetUniform_NormalScale( normalScale ); } - if ( physicalMapping || specularMapping ) + if ( pStage->enablePhysicalMapping || pStage->enableSpecularMapping ) { // bind u_MaterialMap GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - if ( specularMapping ) + if ( pStage->enableSpecularMapping ) { float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); @@ -1167,7 +1139,7 @@ static void Render_vertexLighting_DBS_world( int stage ) } // bind u_GlowMap - if ( glowMapping ) + if ( pStage->enableGlowMapping ) { GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); } else { @@ -1225,20 +1197,7 @@ static void Render_lightMapping( int stage ) break; } - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - bool hasMaterialMap = pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] != nullptr; - bool hasGlowMap = pStage->bundle[ TB_GLOWMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - bool isMaterialPhysical = pStage->collapseType == collapseType_t::COLLAPSE_lighting_PBR; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool deluxeMapping = r_deluxeMapping->integer && tr.worldDeluxeMapping && normalMapping; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - bool physicalMapping = r_physicalMapping->integer && hasMaterialMap && isMaterialPhysical; - bool specularMapping = r_specularMapping->integer && hasMaterialMap && !isMaterialPhysical; - bool glowMapping = r_glowMapping->integer && hasGlowMap; + bool enableDeluxeMapping = pStage->enableDeluxeMapping && tr.worldDeluxeMapping; bool noLightMap = !pStage->implicitLightmap && (tess.surfaceShader->surfaceFlags & SURF_NOLIGHTMAP) @@ -1247,13 +1206,13 @@ static void Render_lightMapping( int stage ) // choose right shader program ---------------------------------- tess.vboVertexSprite = false; - gl_lightMappingShader->SetDeluxeMapping( deluxeMapping ); + gl_lightMappingShader->SetDeluxeMapping( enableDeluxeMapping ); - gl_lightMappingShader->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_lightMappingShader->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_lightMappingShader->SetParallaxMapping( parallaxMapping ); + gl_lightMappingShader->SetParallaxMapping( pStage->enableParallaxMapping ); - gl_lightMappingShader->SetPhysicalShading( isMaterialPhysical ); + gl_lightMappingShader->SetPhysicalShading( pStage->isMaterialPhysical ); gl_lightMappingShader->BindProgram( pStage->deformIndex ); // end choose right shader program ------------------------------ @@ -1292,7 +1251,7 @@ static void Render_lightMapping( int stage ) gl_lightMappingShader->SetUniform_AlphaTest( pStage->stateBits ); // bind u_HeightMap - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -1304,7 +1263,7 @@ static void Render_lightMapping( int stage ) gl_lightMappingShader->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } @@ -1322,7 +1281,7 @@ static void Render_lightMapping( int stage ) gl_lightMappingShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); } - if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) + if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) { // bind u_NormalMap GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -1333,7 +1292,7 @@ static void Render_lightMapping( int stage ) } // bind u_NormalMap - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -1342,12 +1301,12 @@ static void Render_lightMapping( int stage ) gl_lightMappingShader->SetUniform_NormalScale( normalScale ); } - if ( physicalMapping || specularMapping ) + if ( pStage->enablePhysicalMapping || pStage->enableSpecularMapping ) { // bind u_MaterialMap GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - if ( specularMapping ) + if ( pStage->enableSpecularMapping ) { float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); @@ -1364,7 +1323,7 @@ static void Render_lightMapping( int stage ) BindLightMap( 3, noLightMap ); // bind u_DeluxeMap - if ( deluxeMapping ) + if ( enableDeluxeMapping ) { BindDeluxeMap( 4 ); } @@ -1374,7 +1333,7 @@ static void Render_lightMapping( int stage ) } // bind u_GlowMap - if ( glowMapping ) + if ( pStage->enableGlowMapping ) { GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); } else { @@ -1550,28 +1509,15 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *pStage, GLimp_LogComment( "--- Render_forwardLighting_DBS_omni ---\n" ); - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - bool hasMaterialMap = pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - bool isMaterialPhysical = pStage->collapseType == collapseType_t::COLLAPSE_lighting_PBR; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - // physical mapping is not implemented - bool specularMapping = r_specularMapping->integer && hasMaterialMap && !isMaterialPhysical; - // glow mapping is not implemented - bool shadowCompare = ( r_shadows->integer >= Util::ordinal(shadowingMode_t::SHADOWING_ESM16) && !light->l.noShadows && light->shadowLOD >= 0 ); // choose right shader program ---------------------------------- gl_forwardLightingShader_omniXYZ->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning ); gl_forwardLightingShader_omniXYZ->SetVertexAnimation( glState.vertexAttribsInterpolation > 0 ); - gl_forwardLightingShader_omniXYZ->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_forwardLightingShader_omniXYZ->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_forwardLightingShader_omniXYZ->SetParallaxMapping( parallaxMapping ); + gl_forwardLightingShader_omniXYZ->SetParallaxMapping( pStage->enableParallaxMapping ); gl_forwardLightingShader_omniXYZ->SetShadowing( shadowCompare ); @@ -1613,7 +1559,7 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *pStage, gl_forwardLightingShader_omniXYZ->SetUniform_AlphaTest( pStage->stateBits ); // bind u_HeightMap - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -1625,7 +1571,7 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *pStage, gl_forwardLightingShader_omniXYZ->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } @@ -1687,7 +1633,7 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *pStage, GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_forwardLightingShader_omniXYZ->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) + if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) { // bind u_NormalMap GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -1697,7 +1643,7 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *pStage, GL_BindToTMU( 1, tr.flatImage ); } - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -1707,7 +1653,7 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *pStage, } // physical mapping is not implemented - if ( specularMapping ) + if ( pStage->enableSpecularMapping ) { // bind u_MaterialMap GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); @@ -1759,27 +1705,15 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *pStage, GLimp_LogComment( "--- Render_forwardLighting_DBS_proj ---\n" ); - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - bool hasMaterialMap = pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - bool isMaterialPhysical = pStage->collapseType == collapseType_t::COLLAPSE_lighting_PBR; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - // physical mapping is not implemented - bool specularMapping = r_specularMapping->integer && hasMaterialMap && !isMaterialPhysical; - bool shadowCompare = ( r_shadows->integer >= Util::ordinal(shadowingMode_t::SHADOWING_ESM16) && !light->l.noShadows && light->shadowLOD >= 0 ); // choose right shader program ---------------------------------- gl_forwardLightingShader_projXYZ->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning ); gl_forwardLightingShader_projXYZ->SetVertexAnimation( glState.vertexAttribsInterpolation > 0 ); - gl_forwardLightingShader_projXYZ->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_forwardLightingShader_projXYZ->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_forwardLightingShader_projXYZ->SetParallaxMapping( parallaxMapping ); + gl_forwardLightingShader_projXYZ->SetParallaxMapping( pStage->enableParallaxMapping ); gl_forwardLightingShader_projXYZ->SetShadowing( shadowCompare ); @@ -1821,7 +1755,7 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *pStage, gl_forwardLightingShader_projXYZ->SetUniform_AlphaTest( pStage->stateBits ); // bind u_HeightMap - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -1833,7 +1767,7 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *pStage, gl_forwardLightingShader_projXYZ->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } @@ -1896,7 +1830,7 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *pStage, GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_forwardLightingShader_projXYZ->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) + if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) { // bind u_NormalMap GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -1906,7 +1840,7 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *pStage, GL_BindToTMU( 1, tr.flatImage ); } - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -1916,7 +1850,7 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *pStage, } // physical mapping is not implemented - if ( specularMapping ) + if ( pStage->enableSpecularMapping ) { // bind u_MaterialMap GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); @@ -1966,27 +1900,15 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *pStage, trRef GLimp_LogComment( "--- Render_forwardLighting_DBS_directional ---\n" ); - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - bool hasMaterialMap = pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - bool isMaterialPhysical = pStage->collapseType == collapseType_t::COLLAPSE_lighting_PBR; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - // physical mapping is not implemented - bool specularMapping = r_specularMapping->integer && hasMaterialMap && !isMaterialPhysical; - bool shadowCompare = ( r_shadows->integer >= Util::ordinal(shadowingMode_t::SHADOWING_ESM16) && !light->l.noShadows && light->shadowLOD >= 0 ); // choose right shader program ---------------------------------- gl_forwardLightingShader_directionalSun->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning ); gl_forwardLightingShader_directionalSun->SetVertexAnimation( glState.vertexAttribsInterpolation > 0 ); - gl_forwardLightingShader_directionalSun->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_forwardLightingShader_directionalSun->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_forwardLightingShader_directionalSun->SetParallaxMapping( parallaxMapping ); + gl_forwardLightingShader_directionalSun->SetParallaxMapping( pStage->enableParallaxMapping ); gl_forwardLightingShader_directionalSun->SetShadowing( shadowCompare ); @@ -2028,7 +1950,7 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *pStage, trRef gl_forwardLightingShader_directionalSun->SetUniform_AlphaTest( pStage->stateBits ); // bind u_HeightMap - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -2040,7 +1962,7 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *pStage, trRef gl_forwardLightingShader_directionalSun->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } @@ -2107,7 +2029,7 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *pStage, trRef GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_forwardLightingShader_directionalSun->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { // bind u_NormalMap GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -2117,7 +2039,7 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *pStage, trRef GL_BindToTMU( 1, tr.flatImage ); } - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -2127,7 +2049,7 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *pStage, trRef } // physical mapping is not implemented - if ( specularMapping ) + if ( pStage->enableSpecularMapping ) { // bind u_MaterialMap GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); @@ -2187,18 +2109,10 @@ static void Render_reflection_CB( int stage ) GL_State( pStage->stateBits ); - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - // choose right shader program ---------------------------------- - gl_reflectionShader->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_reflectionShader->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_reflectionShader->SetParallaxMapping( parallaxMapping ); + gl_reflectionShader->SetParallaxMapping( pStage->enableParallaxMapping ); gl_reflectionShader->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning ); gl_reflectionShader->SetVertexAnimation( glState.vertexAttribsInterpolation > 0 ); @@ -2234,7 +2148,7 @@ static void Render_reflection_CB( int stage ) GL_BindNearestCubeMap( backEnd.viewParms.orientation.origin ); } - if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) + if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) { // bind u_NormalMap GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -2244,7 +2158,7 @@ static void Render_reflection_CB( int stage ) GL_BindToTMU( 1, tr.flatImage ); } - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); @@ -2256,7 +2170,7 @@ static void Render_reflection_CB( int stage ) gl_reflectionShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_NORMALMAP ] ); // bind u_HeightMap u_depthScale u_parallaxOffsetBias - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -2268,7 +2182,7 @@ static void Render_reflection_CB( int stage ) gl_reflectionShader->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } @@ -2370,10 +2284,6 @@ static void Render_heatHaze( int stage ) GLimp_LogComment( "--- Render_heatHaze ---\n" ); - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - // remove alpha test stateBits = pStage->stateBits; stateBits &= ~GLS_ATEST_BITS; @@ -2432,7 +2342,7 @@ static void Render_heatHaze( int stage ) // draw to background image R_BindFBO( tr.mainFBO[ 1 - backEnd.currentMainFBO ] ); - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { // bind u_NormalMap GL_BindToTMU( 0, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -2475,24 +2385,13 @@ static void Render_liquid( int stage ) GLimp_LogComment( "--- Render_liquid ---\n" ); - bool hasNormalMap = pStage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; - bool hasHeightMap = pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; - - bool hasHeightMapInNormalMap = pStage->heightMapInNormalMap && hasNormalMap; - - bool normalMapping = r_normalMapping->integer && hasNormalMap; - bool parallaxMapping = r_parallaxMapping->integer && !tess.surfaceShader->noParallax && ( hasHeightMap || hasHeightMapInNormalMap ); - // specular component is computed by shader - // physical mapping is not implemented - bool specularMapping = r_specularMapping->integer; - // Tr3B: don't allow blend effects GL_State( pStage->stateBits & ~( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS | GLS_DEPTHMASK_TRUE ) ); // choose right shader program - gl_liquidShader->SetHeightMapInNormalMap( hasHeightMapInNormalMap ); + gl_liquidShader->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap ); - gl_liquidShader->SetParallaxMapping( parallaxMapping ); + gl_liquidShader->SetParallaxMapping( pStage->enableParallaxMapping ); // enable shader, set arrays gl_liquidShader->BindProgram( pStage->deformIndex ); @@ -2518,7 +2417,7 @@ static void Render_liquid( int stage ) // specular component is computed by shader // physical mapping is not implemented - if ( specularMapping ) + if ( pStage->enableSpecularMapping ) { float specMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); float specMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); @@ -2535,7 +2434,7 @@ static void Render_liquid( int stage ) GL_BindToTMU( 2, tr.currentDepthImage ); // bind u_HeightMap u_depthScale u_parallaxOffsetBias - if ( parallaxMapping ) + if ( pStage->enableParallaxMapping ) { float depthScale; float parallaxDepthScale; @@ -2547,13 +2446,13 @@ static void Render_liquid( int stage ) gl_liquidShader->SetUniform_ParallaxOffsetBias( tess.surfaceShader->parallaxOffsetBias ); // FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap - if ( !hasHeightMapInNormalMap ) + if ( !pStage->isHeightMapInNormalMap ) { GL_BindToTMU( 15, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] ); } } - if ( normalMapping || ( parallaxMapping && hasHeightMapInNormalMap ) ) + if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) { // bind u_NormalMap GL_BindToTMU( 3, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); @@ -2563,7 +2462,7 @@ static void Render_liquid( int stage ) GL_BindToTMU( 3, tr.flatImage ); } - if ( normalMapping ) + if ( pStage->enableNormalMapping ) { vec3_t normalScale; // FIXME: NormalIntensity default was 0.5 diff --git a/src/engine/renderer/tr_shader.cpp b/src/engine/renderer/tr_shader.cpp index daa866f390..cd275e479e 100644 --- a/src/engine/renderer/tr_shader.cpp +++ b/src/engine/renderer/tr_shader.cpp @@ -1481,7 +1481,7 @@ static bool LoadMap( shaderStage_t *stage, const char *buffer, const int bundleI && stage->bundle[ bundleIndex ].image[ 0 ]->bits & IF_ALPHA ) { Log::Debug("found heightmap embedded in normalmap '%s'", buffer); - stage->heightMapInNormalMap = true; + stage->isHeightMapInNormalMap = true; } return true; @@ -1909,7 +1909,7 @@ static bool ParseStage( shaderStage_t *stage, const char **text ) stage->collapseType = collapseType_t::COLLAPSE_generic; } - stage->heightMapInNormalMap = true; + stage->isHeightMapInNormalMap = true; ParseNormalMap( stage, text ); } else if ( !Q_stricmp( token, "heightMap" ) ) @@ -4513,7 +4513,7 @@ static void CollapseStages() stages[ diffuseStage ].normalScale[ 1 ] = stages[ normalStage ].normalScale[ 1 ]; stages[ diffuseStage ].normalScale[ 2 ] = stages[ normalStage ].normalScale[ 2 ]; stages[ diffuseStage ].hasNormalScale = stages[ normalStage ].hasNormalScale; - stages[ diffuseStage ].heightMapInNormalMap = stages[ normalStage ].heightMapInNormalMap; + stages[ diffuseStage ].isHeightMapInNormalMap = stages[ normalStage ].isHeightMapInNormalMap; // disable since it's merged stages[ normalStage ].active = false; } @@ -4620,7 +4620,22 @@ static void CollapseStages() for ( int s = 0; s < shader.numStages; s++ ) { shaderStage_t *stage = &stages[ s ]; - // STUB + + // Available textures. + stage->hasNormalMap = stage->bundle[ TB_NORMALMAP ].image[ 0 ] != nullptr; + stage->hasHeightMap = stage->bundle[ TB_HEIGHTMAP ].image[ 0 ] != nullptr; + stage->isHeightMapInNormalMap = stage->isHeightMapInNormalMap && stage->hasNormalMap; + stage->hasMaterialMap = stage->bundle[ TB_MATERIALMAP ].image[ 0 ] != nullptr; + stage->hasGlowMap = stage->bundle[ TB_GLOWMAP ].image[ 0 ] != nullptr; + stage->isMaterialPhysical = stage->collapseType == collapseType_t::COLLAPSE_lighting_PBR; + + // Available features. + stage->enableNormalMapping = r_normalMapping->integer && stage->hasNormalMap; + stage->enableDeluxeMapping = r_deluxeMapping->integer && stage->hasNormalMap; + stage->enableParallaxMapping = r_parallaxMapping->integer && !shader.noParallax && ( stage->hasHeightMap || stage->isHeightMapInNormalMap ); + stage->enablePhysicalMapping = r_physicalMapping->integer && stage->hasMaterialMap && stage->isMaterialPhysical; + stage->enableSpecularMapping = r_specularMapping->integer && stage->hasMaterialMap && !stage->isMaterialPhysical; + stage->enableGlowMapping = r_glowMapping->integer && stage->hasGlowMap; } } From b102902d5c5fc44c0bbe22c9949e642d67155e78 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Mon, 13 Apr 2020 07:20:29 +0200 Subject: [PATCH 4/4] renderer: set texture fallback in tr_shader instead of tr_shade Compute them once per shader at map load instead of doing it per frame per surface. --- src/engine/renderer/tr_shade.cpp | 241 ++++++++---------------------- src/engine/renderer/tr_shader.cpp | 16 ++ 2 files changed, 81 insertions(+), 176 deletions(-) diff --git a/src/engine/renderer/tr_shade.cpp b/src/engine/renderer/tr_shade.cpp index 2331bf18b9..2a5cd483dc 100644 --- a/src/engine/renderer/tr_shade.cpp +++ b/src/engine/renderer/tr_shade.cpp @@ -826,42 +826,27 @@ static void Render_vertexLighting_DBS_entity( int stage ) GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_vertexLightingShader_DBS_entity->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) - { - // bind u_NormalMap - GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 1, tr.flatImage ); - } - // bind u_NormalMap + GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_vertexLightingShader_DBS_entity->SetUniform_NormalScale( normalScale ); } - if ( pStage->enablePhysicalMapping || pStage->enableSpecularMapping ) - { - // bind u_MaterialMap - GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - - if ( pStage->enableSpecularMapping ) - { - float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); - float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); + // bind u_MaterialMap + GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - gl_vertexLightingShader_DBS_entity->SetUniform_SpecularExponent( specExpMin, specExpMax ); - } - } - else + if ( pStage->enableSpecularMapping ) { - GL_BindToTMU( 2, tr.blackImage ); + float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); + float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); + + gl_vertexLightingShader_DBS_entity->SetUniform_SpecularExponent( specExpMin, specExpMax ); } if ( tr.cubeHashTable != nullptr ) @@ -962,12 +947,7 @@ static void Render_vertexLighting_DBS_entity( int stage ) } // bind u_GlowMap - if ( pStage->enableGlowMapping ) - { - GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); - } else { - GL_BindToTMU( 5, tr.blackImage ); - } + GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); gl_vertexLightingShader_DBS_entity->SetRequiredVertexPointers(); @@ -1087,42 +1067,27 @@ static void Render_vertexLighting_DBS_world( int stage ) GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_vertexLightingShader_DBS_world->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) - { - // bind u_NormalMap - GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 1, tr.flatImage ); - } - // bind u_NormalMap + GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_vertexLightingShader_DBS_world->SetUniform_NormalScale( normalScale ); } - if ( pStage->enablePhysicalMapping || pStage->enableSpecularMapping ) - { - // bind u_MaterialMap - GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); + // bind u_MaterialMap + GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - if ( pStage->enableSpecularMapping ) - { - float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); - float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); - - gl_vertexLightingShader_DBS_world->SetUniform_SpecularExponent( specExpMin, specExpMax ); - } - } - else + if ( pStage->enableSpecularMapping ) { - GL_BindToTMU( 2, tr.blackImage ); + float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); + float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); + + gl_vertexLightingShader_DBS_world->SetUniform_SpecularExponent( specExpMin, specExpMax ); } if( tr.world ) @@ -1139,12 +1104,7 @@ static void Render_vertexLighting_DBS_world( int stage ) } // bind u_GlowMap - if ( pStage->enableGlowMapping ) - { - GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); - } else { - GL_BindToTMU( 5, tr.blackImage ); - } + GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); gl_vertexLightingShader_DBS_world->SetRequiredVertexPointers(); @@ -1281,42 +1241,27 @@ static void Render_lightMapping( int stage ) gl_lightMappingShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); } - if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) - { - // bind u_NormalMap - GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 1, tr.flatImage ); - } - // bind u_NormalMap + GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_lightMappingShader->SetUniform_NormalScale( normalScale ); } - if ( pStage->enablePhysicalMapping || pStage->enableSpecularMapping ) - { - // bind u_MaterialMap - GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); + // bind u_MaterialMap + GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - if ( pStage->enableSpecularMapping ) - { - float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); - float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); - - gl_lightMappingShader->SetUniform_SpecularExponent( specExpMin, specExpMax ); - } - } - else + if ( pStage->enableSpecularMapping ) { - GL_BindToTMU( 2, tr.blackImage ); + float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); + float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); + + gl_lightMappingShader->SetUniform_SpecularExponent( specExpMin, specExpMax ); } // bind u_LightMap @@ -1333,12 +1278,7 @@ static void Render_lightMapping( int stage ) } // bind u_GlowMap - if ( pStage->enableGlowMapping ) - { - GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); - } else { - GL_BindToTMU( 5, tr.blackImage ); - } + GL_BindToTMU( 5, pStage->bundle[ TB_GLOWMAP ].image[ 0 ] ); gl_lightMappingShader->SetRequiredVertexPointers(); @@ -1633,40 +1573,29 @@ static void Render_forwardLighting_DBS_omni( shaderStage_t *pStage, GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_forwardLightingShader_omniXYZ->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) - { - // bind u_NormalMap - GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 1, tr.flatImage ); - } + // bind u_NormalMap + GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_forwardLightingShader_omniXYZ->SetUniform_NormalScale( normalScale ); } - // physical mapping is not implemented + // bind u_MaterialMap + GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); + + // FIXME: physical mapping is not implemented. if ( pStage->enableSpecularMapping ) { - // bind u_MaterialMap - GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - float minSpec = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); float maxSpec = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); gl_forwardLightingShader_omniXYZ->SetUniform_SpecularExponent( minSpec, maxSpec ); } - else - { - GL_BindToTMU( 2, tr.blackImage ); - } // bind u_AttenuationMapXY GL_SelectTexture( 3 ); @@ -1830,40 +1759,29 @@ static void Render_forwardLighting_DBS_proj( shaderStage_t *pStage, GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_forwardLightingShader_projXYZ->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) - { - // bind u_NormalMap - GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 1, tr.flatImage ); - } + // bind u_NormalMap + GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_forwardLightingShader_projXYZ->SetUniform_NormalScale( normalScale ); } - // physical mapping is not implemented + // bind u_MaterialMap + GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); + + // FIXME: physical mapping is not implemented. if ( pStage->enableSpecularMapping ) { - // bind u_MaterialMap - GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - float minSpec = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); float maxSpec = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); gl_forwardLightingShader_projXYZ->SetUniform_SpecularExponent( minSpec, maxSpec ); } - else - { - GL_BindToTMU( 2, tr.blackImage ); - } // bind u_AttenuationMapXY GL_SelectTexture( 3 ); @@ -2029,39 +1947,28 @@ static void Render_forwardLighting_DBS_directional( shaderStage_t *pStage, trRef GL_BindToTMU( 0, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] ); gl_forwardLightingShader_directionalSun->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] ); - if ( pStage->enableNormalMapping ) - { - // bind u_NormalMap - GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 1, tr.flatImage ); - } + // bind u_NormalMap + GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_forwardLightingShader_directionalSun->SetUniform_NormalScale( normalScale ); } - // physical mapping is not implemented + // bind u_MaterialMap + GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); + + // FIXME: physical mapping is not implemented. if ( pStage->enableSpecularMapping ) { - // bind u_MaterialMap - GL_BindToTMU( 2, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] ); - float minSpec = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); float maxSpec = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value ); gl_forwardLightingShader_directionalSun->SetUniform_SpecularExponent( minSpec, maxSpec ); } - else - { - GL_BindToTMU( 2, tr.blackImage ); - } // bind u_ShadowMap if ( shadowCompare ) @@ -2148,22 +2055,15 @@ static void Render_reflection_CB( int stage ) GL_BindNearestCubeMap( backEnd.viewParms.orientation.origin ); } - if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) - { - // bind u_NormalMap - GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 1, tr.flatImage ); - } + // bind u_NormalMap + GL_BindToTMU( 1, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_reflectionShader->SetUniform_NormalScale( normalScale ); } @@ -2342,11 +2242,11 @@ static void Render_heatHaze( int stage ) // draw to background image R_BindFBO( tr.mainFBO[ 1 - backEnd.currentMainFBO ] ); + // bind u_NormalMap + GL_BindToTMU( 0, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + if ( pStage->enableNormalMapping ) { - // bind u_NormalMap - GL_BindToTMU( 0, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - gl_heatHazeShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_NORMALMAP ] ); vec3_t normalScale; @@ -2355,10 +2255,6 @@ static void Render_heatHaze( int stage ) // bind u_NormalScale gl_heatHazeShader->SetUniform_NormalScale( normalScale ); } - else - { - GL_BindToTMU( 0, tr.flatImage ); - } // bind u_CurrentMap GL_BindToTMU( 1, tr.currentRenderImage[ backEnd.currentMainFBO ] ); @@ -2415,8 +2311,8 @@ static void Render_liquid( int stage ) gl_liquidShader->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix ); gl_liquidShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] ); - // specular component is computed by shader - // physical mapping is not implemented + // NOTE: specular component is computed by shader. + // FIXME: physical mapping is not implemented. if ( pStage->enableSpecularMapping ) { float specMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value ); @@ -2452,23 +2348,16 @@ static void Render_liquid( int stage ) } } - if ( pStage->enableNormalMapping || ( pStage->enableParallaxMapping && pStage->isHeightMapInNormalMap ) ) - { - // bind u_NormalMap - GL_BindToTMU( 3, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); - } - else - { - GL_BindToTMU( 3, tr.flatImage ); - } + // bind u_NormalMap + GL_BindToTMU( 3, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] ); + // bind u_NormalScale if ( pStage->enableNormalMapping ) { vec3_t normalScale; // FIXME: NormalIntensity default was 0.5 SetNormalScale( pStage, normalScale ); - // bind u_NormalScale gl_liquidShader->SetUniform_NormalScale( normalScale ); } diff --git a/src/engine/renderer/tr_shader.cpp b/src/engine/renderer/tr_shader.cpp index cd275e479e..27df38813f 100644 --- a/src/engine/renderer/tr_shader.cpp +++ b/src/engine/renderer/tr_shader.cpp @@ -4636,6 +4636,22 @@ static void CollapseStages() stage->enablePhysicalMapping = r_physicalMapping->integer && stage->hasMaterialMap && stage->isMaterialPhysical; stage->enableSpecularMapping = r_specularMapping->integer && stage->hasMaterialMap && !stage->isMaterialPhysical; stage->enableGlowMapping = r_glowMapping->integer && stage->hasGlowMap; + + // Bind fallback textures if required. + if ( !stage->enableNormalMapping && !( stage->enableParallaxMapping && stage->isHeightMapInNormalMap) ) + { + stage->bundle[ TB_NORMALMAP ].image[ 0 ] = tr.flatImage; + } + + if ( !stage->enablePhysicalMapping && !stage->enableSpecularMapping ) + { + stage->bundle[ TB_MATERIALMAP ].image[ 0 ] = tr.blackImage; + } + + if ( !stage->enableGlowMapping ) + { + stage->bundle[ TB_GLOWMAP ].image[ 0 ] = tr.blackImage; + } } }