Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion trinity/Resources/TriTextureRes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,49 @@ void CreateDescription( const ImageIO::HostBitmap& bitmap, uint32_t lod, Tr2Bitm
}
}

// --------------------------------------------------------------------------------------
// Description:
// The number of top mips to skip so that a DDS loads with its larger side at most maxSize
// pixels, read from the file's header: the side is halved until it fits, and never past the
// chain's last mip, so a chain that stops early (4x4 for block-compressed files) is capped
// by its size, not by its mip count. The stream is left where it was found.
// Return Value:
// 0 for anything that is not a DDS with a mip chain, or when the stream cannot be rewound.
// --------------------------------------------------------------------------------------
uint32_t DdsMipSkipForMaxSize( ICcpStream& stream, uint32_t maxSize )
{
const ptrdiff_t start = stream.GetPosition();
if( start < 0 )
{
return 0;
}
// magic, dwSize, dwFlags, dwHeight, dwWidth, dwPitchOrLinearSize, dwDepth, dwMipMapCount
uint32_t header[8] = {};
const bool isRead = stream.Read( header, sizeof( header ) ) == ptrdiff_t( sizeof( header ) );
// Seek's return value is not the new position in every stream, so check the position itself.
stream.Seek( start, ICcpStream::SO_BEGIN );
if( stream.GetPosition() != start )
{
CCP_LOGERR( "TriTextureRes: could not rewind the stream after reading the DDS header" );
return 0;
}
const uint32_t ddsMagic = 0x20534444; // "DDS "
const uint32_t ddsdMipMapCount = 0x00020000;
if( !isRead || header[0] != ddsMagic || !( header[2] & ddsdMipMapCount ) )
{
return 0;
}
uint32_t largestSide = std::max( header[3], header[4] );
const uint32_t mipCount = header[7];
uint32_t skip = 0;
while( largestSide > maxSize && skip + 1 < mipCount )
{
largestSide >>= 1;
++skip;
}
return skip;
}

void AtomicMinUpdate( std::atomic<uint32_t>& a, uint32_t b )
{
uint32_t prev = a;
Expand All @@ -138,6 +181,7 @@ TriTextureRes::TriTextureRes() :
m_cutoutWidth( 1.0f ),
m_cutoutHeight( 1.0f ),
m_mipLevelMaxCount( std::numeric_limits<uint32_t>::max() ),
m_groupMaxSize( 0 ),
m_isTextureResizable( true ),
m_lodEnabled( false ),
m_hadLodRequests( false ),
Expand Down Expand Up @@ -259,9 +303,13 @@ void TriTextureRes::Initialize( const wchar_t* name, const wchar_t* ext )
}
}

m_isTextureResizable = Tr2Renderer::IsTextureToResize( CW2A( name ) );

m_mipLevelMaxCount = 255;

m_isTextureResizable = Tr2Renderer::IsTextureToResize( CW2A( name ) );
// The group cap is read here and applied in DoLoad, once the file's size is known. Folders
// excluded from the quality skip (UI) are excluded from caps too.
m_groupMaxSize = m_isTextureResizable ? Tr2Renderer::GetTextureGroupMaxSize( name ) : 0;

m_isTextureLoadDisabled = Tr2Renderer::IsTextureLoadDisabled();

Expand Down Expand Up @@ -608,6 +656,11 @@ BlueAsyncRes::LoadingResult TriTextureRes::DoLoad()

ImageIO::Result result;
auto mipSkip = m_requestedLoadMip + ComputeMipSkipCount();
if( m_groupMaxSize )
{
// A ceiling after the quality skip: the larger skip (the smaller texture) wins.
mipSkip = std::max<unsigned>( mipSkip, DdsMipSkipForMaxSize( *m_dataStream, m_groupMaxSize ) );
}
ImageIO::LoadParameters params( m_path.c_str(), mipSkip, m_mipLevelMaxCount );
result = ImageIO::ReadImage( *m_dataStream, params, *m_loadedBitmap, &m_metadata );

Expand Down
1 change: 1 addition & 0 deletions trinity/Resources/TriTextureRes.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ BLUE_CLASS( TriTextureRes ) :
std::unique_ptr<ImageIO::HostBitmap> m_loadedBitmap;

unsigned m_mipLevelMaxCount;
uint32_t m_groupMaxSize; // texture group cap: largest side in pixels to load at, 0 = no cap
bool m_isTextureLoadDisabled : 1;
bool m_isTextureResizable : 1; // do we listen to the global mipskip setting?

Expand Down
76 changes: 76 additions & 0 deletions trinity/Tr2Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "TriPoolAllocator.h"
#include "Resources/TriGeometryRes.h"

#include <memory>
#include <mutex>

using namespace Tr2RenderContextEnum;

bool Tr2Renderer::m_disableGeometryLoad = false;
Expand Down Expand Up @@ -47,6 +50,12 @@ uint32_t s_upscalingContextID = Tr2UpscalingAL::INVALID_CONTEXT_ID;
TriPoolAllocator* s_poolAllocator = NULL;
// keep an array of directories which are to exclude from texture-sizing
std::vector<std::string> s_dirsToExclude;
// texture group caps: (upper-case path substring, max size in pixels), first match wins. Null = no caps.
// The table is never changed in place: a writer swaps in a new one under the mutex and a reader
// takes a reference under it, so a load on any thread sees either the old table or the new one.
typedef std::vector<std::pair<std::string, uint32_t>> TextureGroupCaps;
std::shared_ptr<const TextureGroupCaps> s_textureGroupCaps;
std::mutex s_textureGroupCapsMutex;

PROJECTION_TYPE s_currentProjectionType = PT_PERSPECTIVE;

Expand Down Expand Up @@ -1193,6 +1202,73 @@ bool Tr2Renderer::IsTextureToResize( const char* filename )
return true;
}

static std::string NormalizedTexturePath( std::string path )
{
std::transform( path.begin(), path.end(), path.begin(), []( char c ) { return c == '\\' ? '/' : (char)toupper( (unsigned char)c ); } );
return path;
}

static std::shared_ptr<const TextureGroupCaps> CurrentTextureGroupCaps()
{
std::lock_guard<std::mutex> lock( s_textureGroupCapsMutex );
return s_textureGroupCaps;
}

static void SwapTextureGroupCaps( std::shared_ptr<const TextureGroupCaps> caps )
{
std::lock_guard<std::mutex> lock( s_textureGroupCapsMutex );
s_textureGroupCaps.swap( caps );
}

void Tr2Renderer::SetTextureGroupCaps( const std::vector<std::pair<std::string, uint32_t>>& caps )
{
if( caps.empty() )
{
SwapTextureGroupCaps( nullptr );
return;
}
auto table = std::make_shared<TextureGroupCaps>();
table->reserve( caps.size() );
for( const auto& cap : caps )
{
// A max size of 0 means "no cap" for this group; the rule is kept so that a later, broader
// rule cannot catch it.
table->emplace_back( NormalizedTexturePath( cap.first ), cap.second );
}
SwapTextureGroupCaps( table );
}

void Tr2Renderer::AddTextureGroupCap( const char* pattern, uint32_t maxSize )
{
auto current = CurrentTextureGroupCaps();
auto table = current ? std::make_shared<TextureGroupCaps>( *current ) : std::make_shared<TextureGroupCaps>();
table->emplace_back( NormalizedTexturePath( pattern ), maxSize );
SwapTextureGroupCaps( table );
}

void Tr2Renderer::ClearTextureGroupCaps()
{
SwapTextureGroupCaps( nullptr );
}

uint32_t Tr2Renderer::GetTextureGroupMaxSize( const wchar_t* filename )
{
const auto caps = CurrentTextureGroupCaps();
if( !caps )
{
return 0;
}
const std::string path = NormalizedTexturePath( std::string( CW2A( filename ) ) );
for( const auto& cap : *caps )
{
if( path.find( cap.first ) != std::string::npos )
{
return cap.second;
}
}
return 0;
}

bool Tr2Renderer::IsLowQuality( void )
{
// is this low quality rendering? Decide here
Expand Down
9 changes: 9 additions & 0 deletions trinity/Tr2Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ class Tr2Renderer
static bool IsTextureToResize( const char* filename );
static void AddMipLevelSkipExclusionDirectory( const char* path );

// texture group caps: ordered (path substring, max size in pixels) rules, first match wins.
// SetTextureGroupCaps replaces the whole table at once; a load on any thread sees the old
// table or the new one, never a half-built one.
static void SetTextureGroupCaps( const std::vector<std::pair<std::string, uint32_t>>& caps );
static void AddTextureGroupCap( const char* pattern, uint32_t maxSize );
static void ClearTextureGroupCaps();
// The largest side, in pixels, a texture with this path may load at; 0 = no cap.
static uint32_t GetTextureGroupMaxSize( const wchar_t* filename );

// Register an effect. All registered effects are automatically reinitialized upon shader model change.
// Reinit's can also be triggered from somewhere else, for example when toggling shadows
static void RegisterEffect( Tr2Effect* f );
Expand Down
33 changes: 33 additions & 0 deletions trinity/Tr2Renderer_Blue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,39 @@ MAP_FUNCTION_AND_WRAP( "AddMipLevelSkipExclusionDirectory", AddMipLevelSkipExclu
"To clear the internal directory-list, use ::ClearMipLevelSkipExclusionDirectories()\n"
":param name: directory res path" );

static void SetTextureGroupCaps( const std::vector<std::pair<std::string, uint32_t>>& caps )
{
Tr2Renderer::SetTextureGroupCaps( caps );
}

MAP_FUNCTION_AND_WRAP( "SetTextureGroupCaps", SetTextureGroupCaps, "Replaces every texture group cap at once. caps is a list of (pattern, maxSize): textures whose\n"
"path contains pattern (case-insensitive, / and \\ alike) load with their larger side at most\n"
"maxSize pixels. Rules are checked in list order and the first match wins; maxSize 0 leaves\n"
"that group uncapped. The cap drops whole top mips, so a maxSize that is not a power of two\n"
"rounds down (3000 loads a 4096 texture at 2048), and a texture never drops below its last mip.\n"
"It is a ceiling after the quality skip (mipLevelSkipCount): the smaller size wins.\n"
"Applies to 2D and volume DDS loads from then on; cube maps and other formats are not capped.\n"
"Reload textures to apply it to loaded ones. Folders excluded with\n"
"AddMipLevelSkipExclusionDirectory are never capped. An empty list removes every cap.\n"
":param caps: list of (path substring, largest side in pixels or 0 for no cap)" );

static void AddTextureGroupCap( const char* pattern, uint32_t maxSize )
{
Tr2Renderer::AddTextureGroupCap( pattern, maxSize );
}

MAP_FUNCTION_AND_WRAP( "AddTextureGroupCap", AddTextureGroupCap, "Appends one texture group cap; see SetTextureGroupCaps, which replaces the whole table at\n"
"once and is the one to use when setting more than one rule.\n"
":param pattern: path substring, e.g. \"/model/ship/\"\n"
":param maxSize: largest side in pixels (rounds down to a power of two), 0 for no cap" );

static void ClearTextureGroupCaps()
{
Tr2Renderer::ClearTextureGroupCaps();
}

MAP_FUNCTION_AND_WRAP( "ClearTextureGroupCaps", ClearTextureGroupCaps, "Removes every texture group cap; textures load as before from then on." );

static void EnableFallbackTextureDebugging()
{
Tr2Renderer::EnableFallbackTextureDebugging();
Expand Down