fix(textureloader): Simplify and fix faulty implementations of texture reduction (2)#2814
fix(textureloader): Simplify and fix faulty implementations of texture reduction (2)#2814xezon wants to merge 2 commits into
Conversation
|
| Filename | Overview |
|---|---|
| Core/Libraries/Source/WWVegas/WW3D2/textureloader.cpp | Fixes texture reduction logic across 2D, cube, and volume texture load paths: constructor init, removal of double-reduction in Load_Compressed_Mipmap, consolidation of mip subtraction in Apply_Mip_Reduction, and named constants. Logic is correct; one potential unsigned underflow path if the WWASSERT is compiled out in release. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Begin_Compressed_Load] --> B[Get_Texture_Information]
B --> C[Validate_Reduction\nensures reduction < mip_count]
C --> D[Width = orig_width\nHeight = orig_height]
D --> E[Apply_Dim_Reduction\nstored directly in Width/Height]
E --> F[Apply_Mip_Reduction\nuses already-reduced Width/Height]
F --> G{mip_level_count == MIP_LEVELS_ALL?}
G -- Yes --> H[mip_level_count = mip_count]
G -- No --> I[mip_level_count = min requested, mip_count]
H --> J[WWASSERT reduction < mip_level_count]
I --> J
J --> K[mip_level_count -= reduction]
K --> L[clamp to max_mip_level_count]
L --> M[Create DX8 Texture with reduced Width x Height]
M --> N[Load_Compressed_Mipmap]
N --> O[width = Get_Width - already reduced]
O --> P{level < mip_level_count?}
P -- Yes --> Q[WWASSERT width >= 4 && height >= 4]
Q --> R[Copy_Level_To_Surface]
R --> S[width >>= 1 / height >>= 1]
S --> P
P -- No --> T[Done]
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A[Begin_Compressed_Load] --> B[Get_Texture_Information]
B --> C[Validate_Reduction\nensures reduction < mip_count]
C --> D[Width = orig_width\nHeight = orig_height]
D --> E[Apply_Dim_Reduction\nstored directly in Width/Height]
E --> F[Apply_Mip_Reduction\nuses already-reduced Width/Height]
F --> G{mip_level_count == MIP_LEVELS_ALL?}
G -- Yes --> H[mip_level_count = mip_count]
G -- No --> I[mip_level_count = min requested, mip_count]
H --> J[WWASSERT reduction < mip_level_count]
I --> J
J --> K[mip_level_count -= reduction]
K --> L[clamp to max_mip_level_count]
L --> M[Create DX8 Texture with reduced Width x Height]
M --> N[Load_Compressed_Mipmap]
N --> O[width = Get_Width - already reduced]
O --> P{level < mip_level_count?}
P -- Yes --> Q[WWASSERT width >= 4 && height >= 4]
Q --> R[Copy_Level_To_Surface]
R --> S[width >>= 1 / height >>= 1]
S --> P
P -- No --> T[Done]
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
Core/Libraries/Source/WWVegas/WW3D2/textureloader.cpp:1495-1496
**Unsigned underflow if assert is disabled in release**
`WWASSERT` is typically stripped in release/retail builds. If, despite `Validate_Reduction`'s guards, `reduction >= mip_level_count` ever occurs (e.g., due to a corrupt DDS file returning a smaller-than-expected `mip_count`), the unsigned subtraction on the next line silently wraps around to `~0u`. That value is then passed as `MipLevelCount` to `DX8Wrapper::_Create_DX8_Texture`, almost certainly producing a crash or D3D error rather than a graceful fallback. The old code had an explicit `if (mip_level_count < 1) mip_level_count = 1;` safety net; the new consolidation removed it without an equivalent release-safe guard.
Reviews (2): Last reviewed commit: "Simplify Apply_Mip_Reduction" | Re-trigger Greptile
This change further simplifies and fixes the implementations of texture reduction.
I tested retail vs patched game with different texture reduction levels and it looked identical.