From 7a65e3b3a61dafcb029f884042d55dc1c3e2444f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 9 Oct 2026 17:11:41 +0300 Subject: [PATCH 1/2] Fix texture filtering convars not applying on level init --- Source.Common/MaterialSystem/ITextureInternal.cs | 2 ++ Source.Engine/MatSysInterface.cs | 2 +- Source.Engine/Render.cs | 2 ++ Source.MaterialSystem/MaterialSystem.cs | 2 +- Source.MaterialSystem/Texture.cs | 2 +- Source.MaterialSystem/TextureManager.cs | 5 +++++ 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Source.Common/MaterialSystem/ITextureInternal.cs b/Source.Common/MaterialSystem/ITextureInternal.cs index f309aa3e0..6fbfeee39 100644 --- a/Source.Common/MaterialSystem/ITextureInternal.cs +++ b/Source.Common/MaterialSystem/ITextureInternal.cs @@ -27,6 +27,7 @@ public static ReadOnlySpan NormalizeTextureName(ReadOnlySpan name, S void Bind(Sampler sampler, int frame); int GetTextureHandle(int frame, int textureChannel = 0); void OnRestore(); + void SetFilteringAndClampingMode(bool onlyLodValues = false); void Precache(); bool SetRenderTarget(int rt, ITexture? depthTexture = null); void CopyFrameBufferToMe(int renderTargetID = 0, Rectangle? srcRect = null, Rectangle? dstRect = null); @@ -77,6 +78,7 @@ public void Dispose() { } public void Bind(Sampler sampler, int frame) => throw new NotSupportedException(); public int GetTextureHandle(int v) => throw new NotSupportedException(); public void OnRestore() => throw new NotSupportedException(); + public void SetFilteringAndClampingMode(bool onlyLodValues = false) => throw new NotSupportedException(); public void Precache() => throw new NotSupportedException(); public bool SetRenderTarget(int rt, ITexture? depthTexture = null) => throw new NotSupportedException(); public void GetReflectivity(out Vector3 reflectivity) => throw new NotSupportedException(); diff --git a/Source.Engine/MatSysInterface.cs b/Source.Engine/MatSysInterface.cs index 85187bdf2..11dc0e2e4 100644 --- a/Source.Engine/MatSysInterface.cs +++ b/Source.Engine/MatSysInterface.cs @@ -1142,7 +1142,7 @@ static void mat_savechanges(in TokenizedCommand args) { WriteMaterialSystemConfigToRegistry(MaterialSystemConfig); } - static void UpdateMaterialSystemConfig() { + internal static void UpdateMaterialSystemConfig() { if (host_state.WorldBrush != null && host_state.WorldBrush.LightData?.Length == 0) mat_fullbright.SetValue(1); diff --git a/Source.Engine/Render.cs b/Source.Engine/Render.cs index c17d2143f..b34d16b33 100644 --- a/Source.Engine/Render.cs +++ b/Source.Engine/Render.cs @@ -330,6 +330,8 @@ public void LevelInit() { g_ShadowMgr.LevelInit(host_state.WorldBrush!.NumSurfaces); } + UpdateMaterialSystemConfig(); + LoadWorldGeometry(); Surface_LevelInit(); diff --git a/Source.MaterialSystem/MaterialSystem.cs b/Source.MaterialSystem/MaterialSystem.cs index b30f49c1d..b92ec3169 100644 --- a/Source.MaterialSystem/MaterialSystem.cs +++ b/Source.MaterialSystem/MaterialSystem.cs @@ -310,7 +310,7 @@ public bool OverrideConfig(MaterialSystem_Config config, bool forceUpdate) { } } else if (resetTextureFilter) { - // TextureSystem.ResetTextureFilteringState(); + TextureSystem.ResetTextureFilteringState(); } // Recompute all state snapshots diff --git a/Source.MaterialSystem/Texture.cs b/Source.MaterialSystem/Texture.cs index 8e7fe7ae3..f79762d56 100644 --- a/Source.MaterialSystem/Texture.cs +++ b/Source.MaterialSystem/Texture.cs @@ -328,7 +328,7 @@ private void DownloadTexture(Rectangle rect, bool copyFromCurrent = false) { ActualDimensionLimit = DesiredDimensionLimit; } - private void SetFilteringAndClampingMode(bool onlyLodValues = false) { + public void SetFilteringAndClampingMode(bool onlyLodValues = false) { if (!HasBeenAllocated()) return; diff --git a/Source.MaterialSystem/TextureManager.cs b/Source.MaterialSystem/TextureManager.cs index 0bb4eaf91..be3d2922d 100644 --- a/Source.MaterialSystem/TextureManager.cs +++ b/Source.MaterialSystem/TextureManager.cs @@ -161,6 +161,11 @@ internal void RestoreNonRenderTargetTextures() { RestoreTexture(tex.Value); } + internal void ResetTextureFilteringState() { + foreach (var tex in TextureList) + tex.Value.SetFilteringAndClampingMode(); + } + internal void AllocateStandardRenderTargets() { MaterialSystem.BeginRenderTargetAllocation(); MaterialSystem.EndRenderTargetAllocation(); From 205805e2694e10388cb6b14d44c7f72f8209cd27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sat, 10 Oct 2026 17:59:45 +0300 Subject: [PATCH 2/2] Better defaults for mat_trilinear and mat_forceaniso, actually use them --- Source.MaterialSystem/MaterialSystem.cs | 10 +++++----- Source.ShaderAPI.Gl46/ShaderAPIGl46.cs | 13 +++++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Source.MaterialSystem/MaterialSystem.cs b/Source.MaterialSystem/MaterialSystem.cs index b92ec3169..c0a08a5f1 100644 --- a/Source.MaterialSystem/MaterialSystem.cs +++ b/Source.MaterialSystem/MaterialSystem.cs @@ -62,8 +62,8 @@ public class MaterialSystem : IMaterialSystemInternal, IShaderUtil readonly static ConVar mat_vsync = new("mat_vsync", "0", 0, "Force sync to vertical retrace", 0, 1); readonly static ConVar mat_forcehardwaresync = new(IsPC() ? "1" : "0", 0); - readonly static ConVar mat_trilinear = new("0", 0); - readonly static ConVar mat_forceaniso = new("1", FCvar.Archive); // 0 = Bilinear, 1 = Trilinear, 2+ = Aniso + readonly static ConVar mat_trilinear = new("1", 0); + readonly static ConVar mat_forceaniso = new("16", FCvar.Archive); // 0 = Bilinear, 1 = Trilinear, 2+ = Aniso readonly static ConVar mat_filterlightmaps = new("1", 0); readonly static ConVar mat_filtertextures = new("1", 0); readonly static ConVar mat_mipmaptextures = new("1", 0); @@ -303,6 +303,9 @@ public bool OverrideConfig(MaterialSystem_Config config, bool forceUpdate) { if (reloadMaterials) ReloadMaterials(); + if (resetAnisotropy) + ShaderAPI.SetAnisotropicLevel(config.ForceAnisotropicLevel); + if (redownloadTextures) { if (ShaderAPI.CanDownloadTextures()) { TextureSystem.RestoreRenderTargets(); @@ -317,9 +320,6 @@ public bool OverrideConfig(MaterialSystem_Config config, bool forceUpdate) { if (recomputeSnapshots) RecomputeAllStateSnapshots(); - // if (resetAnisotropy) - // ShaderAPI.SetAnisotropicLevel(config.ForceAnisotropicLevel); - // if (setStandardVertexShaderConstants) // ShaderAPI.SetStandardVertexShaderConstants(IMaterialSystem.OVERBRIGHT); diff --git a/Source.ShaderAPI.Gl46/ShaderAPIGl46.cs b/Source.ShaderAPI.Gl46/ShaderAPIGl46.cs index 2eb151583..bacd1dc06 100644 --- a/Source.ShaderAPI.Gl46/ShaderAPIGl46.cs +++ b/Source.ShaderAPI.Gl46/ShaderAPIGl46.cs @@ -1757,7 +1757,7 @@ unsafe void ApplyForcedFilter(Sampler sampler, ShaderAPITextureHandle_t textureH glSamplerParameteri(samplerObject, GL_TEXTURE_MIN_FILTER, mipped ? GL_NEAREST_MIPMAP_LINEAR : GL_NEAREST); break; case 3: - glSamplerParameterf(samplerObject, GL_TEXTURE_MAX_ANISOTROPY, HardwareConfig.MaximumAnisotropicLevel()); + glSamplerParameterf(samplerObject, GL_TEXTURE_MAX_ANISOTROPY, AnisotropicLevel); glSamplerParameteri(samplerObject, GL_TEXTURE_MIN_FILTER, mipped ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); break; default: @@ -2640,7 +2640,7 @@ public void TexMinFilter(TexFilterMode mode) { glTextureParameteri(GetModifyTexture(), GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); break; case TexFilterMode.Anisotropic: - glTextureParameterf(GetModifyTexture(), GL_TEXTURE_MAX_ANISOTROPY, HardwareConfig.MaximumAnisotropicLevel()); + glTextureParameterf(GetModifyTexture(), GL_TEXTURE_MAX_ANISOTROPY, AnisotropicLevel); glTextureParameteri(GetModifyTexture(), GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); break; default: @@ -2670,7 +2670,7 @@ public void TexMagFilter(TexFilterMode mode) { break; case TexFilterMode.Anisotropic: glTextureParameteri(GetModifyTexture(), GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTextureParameterf(GetModifyTexture(), GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f); + glTextureParameterf(GetModifyTexture(), GL_TEXTURE_MAX_ANISOTROPY_EXT, AnisotropicLevel); break; default: break; @@ -3251,8 +3251,13 @@ public int SelectionMode(bool selectionMode) { throw new NotImplementedException("Incomplete port of IShaderAPI"); } + int AnisotropicLevel = 1; public void SetAnisotropicLevel(int anisotropyLevel) { - throw new NotImplementedException("Incomplete port of IShaderAPI"); + int maxAnisotropy = HardwareConfig.MaximumAnisotropicLevel(); + if (anisotropyLevel > maxAnisotropy || anisotropyLevel <= 1) + anisotropyLevel = Math.Max(2, Math.Min(8, maxAnisotropy / 4)); + + AnisotropicLevel = anisotropyLevel; } public void SetClipPlane(int index, ReadOnlySpan plane) {