diff --git a/trinity/Resources/TriTextureRes.cpp b/trinity/Resources/TriTextureRes.cpp index 85d60156d..834a849ea 100644 --- a/trinity/Resources/TriTextureRes.cpp +++ b/trinity/Resources/TriTextureRes.cpp @@ -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& a, uint32_t b ) { uint32_t prev = a; @@ -138,6 +181,7 @@ TriTextureRes::TriTextureRes() : m_cutoutWidth( 1.0f ), m_cutoutHeight( 1.0f ), m_mipLevelMaxCount( std::numeric_limits::max() ), + m_groupMaxSize( 0 ), m_isTextureResizable( true ), m_lodEnabled( false ), m_hadLodRequests( false ), @@ -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(); @@ -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( 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 ); diff --git a/trinity/Resources/TriTextureRes.h b/trinity/Resources/TriTextureRes.h index 0cff58dc7..733ac65be 100644 --- a/trinity/Resources/TriTextureRes.h +++ b/trinity/Resources/TriTextureRes.h @@ -174,6 +174,7 @@ BLUE_CLASS( TriTextureRes ) : std::unique_ptr 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? diff --git a/trinity/Tr2Renderer.cpp b/trinity/Tr2Renderer.cpp index d1fbc170a..bf937a8e1 100644 --- a/trinity/Tr2Renderer.cpp +++ b/trinity/Tr2Renderer.cpp @@ -11,6 +11,9 @@ #include "TriPoolAllocator.h" #include "Resources/TriGeometryRes.h" +#include +#include + using namespace Tr2RenderContextEnum; bool Tr2Renderer::m_disableGeometryLoad = false; @@ -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 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> TextureGroupCaps; +std::shared_ptr s_textureGroupCaps; +std::mutex s_textureGroupCapsMutex; PROJECTION_TYPE s_currentProjectionType = PT_PERSPECTIVE; @@ -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 CurrentTextureGroupCaps() +{ + std::lock_guard lock( s_textureGroupCapsMutex ); + return s_textureGroupCaps; +} + +static void SwapTextureGroupCaps( std::shared_ptr caps ) +{ + std::lock_guard lock( s_textureGroupCapsMutex ); + s_textureGroupCaps.swap( caps ); +} + +void Tr2Renderer::SetTextureGroupCaps( const std::vector>& caps ) +{ + if( caps.empty() ) + { + SwapTextureGroupCaps( nullptr ); + return; + } + auto table = std::make_shared(); + 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( *current ) : std::make_shared(); + 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 diff --git a/trinity/Tr2Renderer.h b/trinity/Tr2Renderer.h index f278df469..349d4ddcb 100644 --- a/trinity/Tr2Renderer.h +++ b/trinity/Tr2Renderer.h @@ -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>& 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 ); diff --git a/trinity/Tr2Renderer_Blue.cpp b/trinity/Tr2Renderer_Blue.cpp index 0a747cbe7..069598674 100644 --- a/trinity/Tr2Renderer_Blue.cpp +++ b/trinity/Tr2Renderer_Blue.cpp @@ -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>& 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();