Skip to content

Commit 7179520

Browse files
authored
fix(textureloader): Simplify and fix faulty implementations of texture reduction (2) (TheSuperHackers#2814)
1 parent 6a19b8b commit 7179520

1 file changed

Lines changed: 52 additions & 81 deletions

File tree

Core/Libraries/Source/WWVegas/WW3D2/textureloader.cpp

Lines changed: 52 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ TextureLoadTaskClass::TextureLoadTaskClass()
10121012
Format (WW3D_FORMAT_UNKNOWN),
10131013
Width (0),
10141014
Height (0),
1015-
MipLevelCount (0),
1015+
MipLevelCount (MIP_LEVELS_ALL),
10161016
Reduction (0),
10171017
Type (TASK_NONE),
10181018
Priority (PRIORITY_LOW),
@@ -1421,6 +1421,9 @@ static void Validate_Reduction(const TextureBaseClass* texture, unsigned& reduct
14211421
}
14221422
}
14231423

1424+
// Will not present textures smaller than 4 pixels wide or high.
1425+
static constexpr const unsigned MinTextureDim = 4u;
1426+
static constexpr const unsigned MinTextureDepth = 1u;
14241427

14251428
// If the size doesn't match, try and see if texture reduction would help...
14261429
// (mainly for cases where loaded texture is larger than hardware limit)
@@ -1430,8 +1433,8 @@ static void Apply_Dim_Reduction(unsigned& width, unsigned& height, unsigned& red
14301433

14311434
for (unsigned r = reduction; r < mip_count; ++r)
14321435
{
1433-
unsigned w = max(width >> r, 4u);
1434-
unsigned h = max(height >> r, 4u);
1436+
unsigned w = max(width >> r, MinTextureDim);
1437+
unsigned h = max(height >> r, MinTextureDim);
14351438
unsigned tmp_w = w;
14361439
unsigned tmp_h = h;
14371440

@@ -1453,9 +1456,9 @@ static void Apply_Dim_Reduction_With_Depth(unsigned& width, unsigned& height, un
14531456
{
14541457
for (unsigned r = reduction; r < mip_count; ++r)
14551458
{
1456-
unsigned w = max(width >> r, 4u);
1457-
unsigned h = max(height >> r, 4u);
1458-
unsigned d = max(depth >> r, 1u);
1459+
unsigned w = max(width >> r, MinTextureDim);
1460+
unsigned h = max(height >> r, MinTextureDim);
1461+
unsigned d = max(depth >> r, MinTextureDepth);
14591462
unsigned tmp_w = w;
14601463
unsigned tmp_h = h;
14611464
unsigned tmp_d = d;
@@ -1474,37 +1477,32 @@ static void Apply_Dim_Reduction_With_Depth(unsigned& width, unsigned& height, un
14741477
}
14751478

14761479

1477-
static void Apply_Mip_Reduction(unsigned& mip_level_count, unsigned Reduction, unsigned width, unsigned height, unsigned mip_count)
1480+
static void Apply_Mip_Reduction(unsigned& mip_level_count, unsigned reduction, unsigned width, unsigned height, unsigned mip_count)
14781481
{
14791482
// If texture wants all mip levels, take as many as the file contains (not necessarily all)
14801483
// Otherwise take as many mip levels as the texture wants, not to exceed the count in file...
1481-
if (mip_level_count == 0)
1484+
if (mip_level_count == MIP_LEVELS_ALL)
14821485
{
1483-
mip_level_count = mip_count-Reduction;
1484-
1485-
// Sanity check to make sure something gets loaded.
1486-
if (mip_level_count < 1)
1487-
mip_level_count = 1;
1486+
mip_level_count = mip_count;
14881487
}
14891488
else
14901489
{
14911490
if (mip_level_count > mip_count)
14921491
mip_level_count = mip_count;
1493-
1494-
// Reduce requested number by those removed.
1495-
mip_level_count -= Reduction;
14961492
}
14971493

1494+
// Reduce requested number by those removed.
1495+
WWASSERT(reduction < mip_level_count);
1496+
mip_level_count -= reduction;
1497+
14981498
// Once more, verify that the mip level count is correct (in case it was changed here it might not
14991499
// match the size...well actually it doesn't have to match but it can't be bigger than the size)
15001500
unsigned int max_mip_level_count = 1;
1501-
unsigned int w = 4;
1502-
unsigned int h = 4;
1501+
unsigned int dim = MinTextureDim;
15031502

1504-
while (w < width && h < height)
1503+
while (dim < width && dim < height)
15051504
{
1506-
w += w;
1507-
h += h;
1505+
dim <<= 1;
15081506
max_mip_level_count++;
15091507
}
15101508

@@ -1533,26 +1531,21 @@ bool TextureLoadTaskClass::Begin_Compressed_Load()
15331531
return false;
15341532
}
15351533

1536-
// Destination size will be the next power of two square from the larger width and height...
1537-
Width = orig_width;
1538-
Height = orig_height;
1539-
TextureLoader::Validate_Texture_Size(Width, Height, orig_depth);
1540-
15411534
Format = Get_Valid_Texture_Format(orig_format, Texture->Is_Compression_Allowed());
15421535

15431536
Reduction = orig_reduction;
15441537
Validate_Reduction(Texture, Reduction, orig_mip_count);
15451538

1546-
unsigned reduced_width = orig_width;
1547-
unsigned reduced_height = orig_height;
1548-
Apply_Dim_Reduction(reduced_width, reduced_height, Reduction, orig_mip_count);
1539+
Width = orig_width;
1540+
Height = orig_height;
1541+
Apply_Dim_Reduction(Width, Height, Reduction, orig_mip_count);
15491542

15501543
Apply_Mip_Reduction(MipLevelCount, Reduction, Width, Height, orig_mip_count);
15511544

15521545
D3DTexture = DX8Wrapper::_Create_DX8_Texture
15531546
(
1554-
reduced_width,
1555-
reduced_height,
1547+
Width,
1548+
Height,
15561549
Format,
15571550
(MipCountType)MipLevelCount,
15581551
#ifdef USE_MANAGED_TEXTURES
@@ -1692,15 +1685,13 @@ bool TextureLoadTaskClass::Load_Compressed_Mipmap()
16921685
}
16931686

16941687
// regular 2d texture
1695-
unsigned int width = Get_Width();
1696-
unsigned int height = Get_Height();
1697-
1698-
width >>= Reduction;
1699-
height >>= Reduction;
1688+
unsigned int width = Get_Width();
1689+
unsigned int height = Get_Height();
17001690

17011691
for (unsigned int level = 0; level < Get_Mip_Level_Count(); ++level)
17021692
{
1703-
WWASSERT(width && height);
1693+
WWASSERT(width >= MinTextureDim && height >= MinTextureDim);
1694+
17041695
dds_file.Copy_Level_To_Surface
17051696
(
17061697
level,
@@ -1712,8 +1703,8 @@ bool TextureLoadTaskClass::Load_Compressed_Mipmap()
17121703
HSVShift
17131704
);
17141705

1715-
width >>= 1;
1716-
height >>= 1;
1706+
width >>= 1;
1707+
height >>= 1;
17171708
}
17181709

17191710
return true;
@@ -2091,26 +2082,21 @@ bool CubeTextureLoadTaskClass::Begin_Compressed_Load()
20912082
return false;
20922083
}
20932084

2094-
// Destination size will be the next power of two square from the larger width and height...
2095-
Width = orig_width;
2096-
Height = orig_height;
2097-
TextureLoader::Validate_Texture_Size(Width, Height, orig_depth);
2098-
20992085
Format = Get_Valid_Texture_Format(orig_format, Texture->Is_Compression_Allowed());
21002086

21012087
Reduction = orig_reduction;
21022088
Validate_Reduction(Texture, Reduction, orig_mip_count);
21032089

2104-
unsigned reduced_width = orig_width;
2105-
unsigned reduced_height = orig_height;
2106-
Apply_Dim_Reduction(reduced_width, reduced_height, Reduction, orig_mip_count);
2090+
Width = orig_width;
2091+
Height = orig_height;
2092+
Apply_Dim_Reduction(Width, Height, Reduction, orig_mip_count);
21072093

21082094
Apply_Mip_Reduction(MipLevelCount, Reduction, Width, Height, orig_mip_count);
21092095

21102096
D3DTexture = DX8Wrapper::_Create_DX8_Cube_Texture
21112097
(
2112-
reduced_width,
2113-
reduced_height,
2098+
Width,
2099+
Height,
21142100
Format,
21152101
(MipCountType)MipLevelCount,
21162102
#ifdef USE_MANAGED_TEXTURES
@@ -2208,12 +2194,9 @@ bool CubeTextureLoadTaskClass::Load_Compressed_Mipmap()
22082194
unsigned int width = Get_Width();
22092195
unsigned int height = Get_Height();
22102196

2211-
width >>= Reduction;
2212-
height >>= Reduction;
2213-
22142197
for (unsigned int level=0; level<Get_Mip_Level_Count(); level++)
22152198
{
2216-
WWASSERT(width && height);
2199+
WWASSERT(width >= MinTextureDim && height >= MinTextureDim);
22172200

22182201
// get cube map surface
22192202
dds_file.Copy_CubeMap_Level_To_Surface
@@ -2228,8 +2211,8 @@ bool CubeTextureLoadTaskClass::Load_Compressed_Mipmap()
22282211
HSVShift
22292212
);
22302213

2231-
width>>=1;
2232-
height>>=1;
2214+
width >>= 1;
2215+
height >>= 1;
22332216
}
22342217
}
22352218

@@ -2405,29 +2388,23 @@ bool VolumeTextureLoadTaskClass::Begin_Compressed_Load()
24052388
return false;
24062389
}
24072390

2408-
// Destination size will be the next power of two square from the larger width and height...
2409-
Width = orig_width;
2410-
Height = orig_height;
2411-
Depth = orig_depth;
2412-
TextureLoader::Validate_Texture_Size(Width, Height, Depth);
2413-
24142391
Format = Get_Valid_Texture_Format(orig_format, Texture->Is_Compression_Allowed());
24152392

24162393
Reduction = orig_reduction;
24172394
Validate_Reduction(Texture, Reduction, orig_mip_count);
24182395

2419-
unsigned reduced_width = orig_width;
2420-
unsigned reduced_height = orig_height;
2421-
unsigned reduced_depth = orig_depth;
2422-
Apply_Dim_Reduction_With_Depth(reduced_width, reduced_height, reduced_depth, Reduction, orig_mip_count);
2396+
Width = orig_width;
2397+
Height = orig_height;
2398+
Depth = orig_depth;
2399+
Apply_Dim_Reduction_With_Depth(Width, Height, Depth, Reduction, orig_mip_count);
24232400

24242401
Apply_Mip_Reduction(MipLevelCount, Reduction, Width, Height, orig_mip_count);
24252402

24262403
D3DTexture = DX8Wrapper::_Create_DX8_Volume_Texture
24272404
(
2428-
reduced_width,
2429-
reduced_height,
2430-
reduced_depth,
2405+
Width,
2406+
Height,
2407+
Depth,
24312408
Format,
24322409
(MipCountType)MipLevelCount,
24332410
#ifdef USE_MANAGED_TEXTURES
@@ -2523,19 +2500,13 @@ bool VolumeTextureLoadTaskClass::Load_Compressed_Mipmap()
25232500
}
25242501

25252502
// load volume
2526-
unsigned int depth=dds_file.Get_Depth(0);
2527-
unsigned int width=Get_Width();
2528-
unsigned int height=Get_Height();
2529-
2530-
depth >>= Reduction;
2531-
width >>= Reduction;
2532-
height >>= Reduction;
2503+
unsigned int width = Get_Width();
2504+
unsigned int height = Get_Height();
2505+
unsigned int depth = Depth;
25332506

25342507
for (unsigned int level=0; level<Get_Mip_Level_Count(); level++)
25352508
{
2536-
if (width<4) width=4;
2537-
if (height<4) height=4;
2538-
if (depth<1) depth=1;
2509+
WWASSERT(width >= MinTextureDim && height >= MinTextureDim && depth >= MinTextureDepth);
25392510

25402511
// get volume
25412512
dds_file.Copy_Volume_Level_To_Surface
@@ -2551,9 +2522,9 @@ bool VolumeTextureLoadTaskClass::Load_Compressed_Mipmap()
25512522
HSVShift
25522523
);
25532524

2554-
width>>=1;
2555-
height>>=1;
2556-
depth>>=1;
2525+
width >>= 1;
2526+
height >>= 1;
2527+
depth = max(depth >> 1, MinTextureDepth);
25572528
}
25582529

25592530
return true;

0 commit comments

Comments
 (0)