Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(ww3d2): a mip filter of none clamps the sampler to the top level

Olcay Seygan3ef2cec8dddb
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp
index 22a11c716..193fa7f4e 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp
@@ -348,9 +348,14 @@ void DX11SamplerBlockClass::Build_Sampler_Description(D3D11_SAMPLER_DESC & descr
description.ComparisonFunc = D3D11_COMPARISON_NEVER;
// D3DSAMP_MAXMIPLEVEL is the most detailed level the sampler may use, which is D3D11's minimum
- // LOD. There is no D3D9 state for the other end, so it stays open.
+ // LOD. The other end is D3D9's mip filter: D3DTEXF_NONE there means the sampler reads the top
+ // level and nothing else, and D3D11 has no filter that says so - every one of its eight walks
+ // the chain. Clamping the maximum to zero is what says it instead. Without this a texture the
+ // engine deliberately unmipped is minified through its chain anyway, which is a softer surface
+ // at distance and no error anywhere.
description.MinLOD = static_cast<float>(SamplerStates[D3DSAMP_MAXMIPLEVEL]);
- description.MaxLOD = D3D11_FLOAT32_MAX;
+ description.MaxLOD = (SamplerStates[D3DSAMP_MIPFILTER] == D3DTEXF_NONE)
+ ? 0.0f : D3D11_FLOAT32_MAX;
const DWORD border = SamplerStates[D3DSAMP_BORDERCOLOR];
description.BorderColor[0] = static_cast<float>((border >> 16) & 0xff) / 255.0f;
diff --git a/GeneralsMD/Code/Tests/test_dx11state.cpp b/GeneralsMD/Code/Tests/test_dx11state.cpp
index d1a2cd47c..cd64a84c1 100644
--- a/GeneralsMD/Code/Tests/test_dx11state.cpp
+++ b/GeneralsMD/Code/Tests/test_dx11state.cpp
@@ -260,6 +260,25 @@ TEST(dx11state_a_clamped_linear_sampler_translates_whole)
// D3DSAMP_ADDRESSW is never set for a two dimensional texture and keeps its default.
CHECK_EQ(description.AddressW, D3D11_TEXTURE_ADDRESS_WRAP);
+
+ // A mip filter that walks the chain leaves the far end open.
+ CHECK_EQ(description.MaxLOD, D3D11_FLOAT32_MAX);
+}
+
+// D3DTEXF_NONE is D3D9 saying the sampler reads the top level and nothing else. All eight of
+// D3D11's filters walk the mip chain, so the only way to say it there is to clamp the maximum LOD,
+// and without that a texture the engine deliberately unmipped is minified through its chain anyway.
+TEST(dx11state_a_mip_filter_of_none_clamps_to_the_top_level)
+{
+ DX11SamplerBlockClass sampler;
+ sampler.Set_Sampler_State(D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
+ sampler.Set_Sampler_State(D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
+ sampler.Set_Sampler_State(D3DSAMP_MIPFILTER, D3DTEXF_NONE);
+
+ D3D11_SAMPLER_DESC description;
+ sampler.Build_Sampler_Description(description);
+ CHECK_EQ(description.Filter, D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT);
+ CHECK_EQ(description.MaxLOD, 0.0f);
}
// A fresh sampler block is the device D3D9 starts with: point filtering, no mip filter, wrapping.