From 4c5957d5eae484ce5b78a13c9d626171eeee0a9d Mon Sep 17 00:00:00 2001 From: fo76utils <87907510+fo76utils@users.noreply.github.com> Date: Thu, 12 Sep 2024 17:39:58 +0200 Subject: [PATCH] Added separate function to activate client texture units/texcoord arrays --- src/gl/glproperty.cpp | 4 ++-- src/gl/gltex.cpp | 23 ++++++++++++++--------- src/gl/gltex.h | 3 ++- src/gl/renderer.cpp | 8 ++++---- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/gl/glproperty.cpp b/src/gl/glproperty.cpp index 87e96694..a4ada999 100644 --- a/src/gl/glproperty.cpp +++ b/src/gl/glproperty.cpp @@ -460,7 +460,7 @@ bool TexturingProperty::bind( int id, const QVector > & texcoor bool TexturingProperty::bind( int id, const QVector > & texcoords, int stage ) { - return ( activateTextureUnit( stage ) && bind( id, texcoords ) ); + return ( activateTextureUnit( stage ) && activateClientTexture( stage ) && bind( id, texcoords ) ); } QString TexturingProperty::fileName( int id ) const @@ -929,7 +929,7 @@ int BSShaderLightingProperty::getSFTexture( int & texunit, FloatVector4 & replUn size_t n = texturePath.length(); if ( ( n - 1 ) & ~( size_t(1023) ) ) break; // empty path or not enough space in tmpBuf - if ( !( texunit >= 3 && texunit < TexCache::num_texture_units && activateTextureUnit(texunit, true) ) ) + if ( !( texunit >= 3 && texunit < TexCache::num_texture_units && activateTextureUnit( texunit ) ) ) break; TexClampMode clampMode = TexClampMode::WRAP_S_WRAP_T; diff --git a/src/gl/gltex.cpp b/src/gl/gltex.cpp index 661f4334..49acf2fe 100644 --- a/src/gl/gltex.cpp +++ b/src/gl/gltex.cpp @@ -109,17 +109,22 @@ void initializeTextureUnits( const QOpenGLContext * context ) initializeTextureLoaders( context ); } -bool activateTextureUnit( int stage, bool noClient ) +bool activateTextureUnit( int stage ) { - if ( stage < TexCache::num_texture_units ) [[likely]] { + if ( stage >= TexCache::num_texture_units ) [[unlikely]] + return ( stage == 0 ); - glActiveTexture( GL_TEXTURE0 + stage ); - if ( stage < TexCache::num_txtunits_client && !noClient ) - glClientActiveTexture( GL_TEXTURE0 + stage ); - return true; - } + glActiveTexture( GL_TEXTURE0 + stage ); + return true; +} - return ( stage == 0 ); +bool activateClientTexture( int stage ) +{ + if ( stage >= TexCache::num_txtunits_client ) [[unlikely]] + return ( stage == 0 ); + + glClientActiveTexture( GL_TEXTURE0 + stage ); + return true; } void resetTextureUnits( int numTex ) @@ -129,7 +134,7 @@ void resetTextureUnits( int numTex ) return; } - for ( int x = std::min( numTex, TexCache::num_texture_units ); --x >= 0; ) { + for ( int x = std::min( std::max< int >( numTex, 1 ), TexCache::num_texture_units ); --x >= 0; ) { glActiveTexture( GL_TEXTURE0 + x ); glDisable( GL_TEXTURE_2D ); glMatrixMode( GL_TEXTURE ); diff --git a/src/gl/gltex.h b/src/gl/gltex.h index 90d229b7..5689f9f7 100644 --- a/src/gl/gltex.h +++ b/src/gl/gltex.h @@ -215,7 +215,8 @@ public slots: void initializeTextureUnits( const QOpenGLContext * ); -bool activateTextureUnit( int x, bool noClient = false ); +bool activateTextureUnit( int x ); +bool activateClientTexture( int x ); void resetTextureUnits( int numTex = TexCache::maxTextureUnits ); float get_max_anisotropy(); diff --git a/src/gl/renderer.cpp b/src/gl/renderer.cpp index 4a9eba67..17adc9c0 100644 --- a/src/gl/renderer.cpp +++ b/src/gl/renderer.cpp @@ -1012,7 +1012,7 @@ bool Renderer::setupProgramCE2( const NifModel * nif, Program * prog, Shape * me prog->uni1i( HAS_MAP_CUBE, hasCubeMap ); // texture unit 2 is reserved for the environment BRDF LUT texture - if ( !activateTextureUnit( texunit, true ) ) + if ( !activateTextureUnit( texunit ) ) return false; if ( !lsp->bind( pbr_lut_sf, true, TexClampMode::CLAMP_S_CLAMP_T ) ) return false; @@ -1336,7 +1336,7 @@ bool Renderer::setupProgramCE2( const NifModel * nif, Program * prog, Shape * me while ( itx.hasNext() ) { itx.next(); - if ( !activateTextureUnit( itx.key() ) ) + if ( !activateClientTexture( itx.key() ) ) return false; auto it = itx.value(); @@ -1696,7 +1696,7 @@ bool Renderer::setupProgramCE1( const NifModel * nif, Program * prog, Shape * me while ( itx.hasNext() ) { itx.next(); - if ( !activateTextureUnit( itx.key() ) ) + if ( !activateClientTexture( itx.key() ) ) return false; auto it = itx.value(); @@ -2003,7 +2003,7 @@ bool Renderer::setupProgramFO3( const NifModel * nif, Program * prog, Shape * me while ( itx.hasNext() ) { itx.next(); - if ( !activateTextureUnit( itx.key() ) ) + if ( !activateClientTexture( itx.key() ) ) return false; auto it = itx.value();