diff --git a/Source/Core/VideoBackends/D3D/TextureCache.cpp b/Source/Core/VideoBackends/D3D/TextureCache.cpp index 0b0eea50b1..23da4a1352 100644 --- a/Source/Core/VideoBackends/D3D/TextureCache.cpp +++ b/Source/Core/VideoBackends/D3D/TextureCache.cpp @@ -100,7 +100,12 @@ TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width, u const HRESULT hr = D3D::device->CreateTexture2D(&texdesc, nullptr, &pTexture); CHECK(SUCCEEDED(hr), "Create texture of the TextureCache"); - TCacheEntry* const entry = new TCacheEntry(new D3DTexture2D(pTexture, D3D11_BIND_SHADER_RESOURCE)); + TCacheEntryConfig config; + config.width = width; + config.height = height; + config.levels = tex_levels; + + TCacheEntry* const entry = new TCacheEntry(config, new D3DTexture2D(pTexture, D3D11_BIND_SHADER_RESOURCE)); entry->usage = usage; // TODO: better debug names @@ -122,7 +127,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo g_renderer->ResetAPIState(); // stretch picture with increased internal resolution - const D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, (float)virtual_width, (float)virtual_height); + const D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, (float)config.width, (float)config.height); D3D::context->RSSetViewports(1, &vp); // set transformation @@ -188,11 +193,17 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo } TextureCache::TCacheEntryBase* TextureCache::CreateRenderTargetTexture( - unsigned int scaled_tex_w, unsigned int scaled_tex_h) + unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) { - return new TCacheEntry(D3DTexture2D::Create(scaled_tex_w, scaled_tex_h, + TCacheEntryConfig config; + config.width = scaled_tex_w; + config.height = scaled_tex_h; + config.layers = layers; + config.rendertarget = true; + + return new TCacheEntry(config, D3DTexture2D::Create(scaled_tex_w, scaled_tex_h, (D3D11_BIND_FLAG)((int)D3D11_BIND_RENDER_TARGET | (int)D3D11_BIND_SHADER_RESOURCE), - D3D11_USAGE_DEFAULT, DXGI_FORMAT_R8G8B8A8_UNORM, 1, FramebufferManager::GetEFBLayers())); + D3D11_USAGE_DEFAULT, DXGI_FORMAT_R8G8B8A8_UNORM, 1, layers)); } TextureCache::TextureCache() diff --git a/Source/Core/VideoBackends/D3D/TextureCache.h b/Source/Core/VideoBackends/D3D/TextureCache.h index 3845165ad5..08ce4008f9 100644 --- a/Source/Core/VideoBackends/D3D/TextureCache.h +++ b/Source/Core/VideoBackends/D3D/TextureCache.h @@ -23,7 +23,7 @@ private: D3D11_USAGE usage; - TCacheEntry(D3DTexture2D *_tex) : texture(_tex) {} + TCacheEntry(const TCacheEntryConfig& config, D3DTexture2D *_tex) : TCacheEntryBase(config), texture(_tex) {} ~TCacheEntry(); void Load(unsigned int width, unsigned int height, @@ -41,7 +41,7 @@ private: TCacheEntryBase* CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels, PC_TexFormat pcfmt) override; - TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h) override; + TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) override; u64 EncodeToRamFromTexture(u32 address, void* source_texture, u32 SourceW, u32 SourceH, bool bFromZBuffer, bool bIsIntensityFmt, u32 copyfmt, int bScaleByHalf, const EFBRectangle& source) {return 0;}; void CompileShaders() override { } diff --git a/Source/Core/VideoBackends/OGL/TextureCache.cpp b/Source/Core/VideoBackends/OGL/TextureCache.cpp index bb80b863e8..4bf70271d1 100644 --- a/Source/Core/VideoBackends/OGL/TextureCache.cpp +++ b/Source/Core/VideoBackends/OGL/TextureCache.cpp @@ -81,7 +81,8 @@ TextureCache::TCacheEntry::~TCacheEntry() } } -TextureCache::TCacheEntry::TCacheEntry() +TextureCache::TCacheEntry::TCacheEntry(const TCacheEntryConfig& _config) +: TCacheEntryBase(_config) { glGenTextures(1, &texture); @@ -105,7 +106,7 @@ void TextureCache::TCacheEntry::Bind(unsigned int stage) bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int level) { - return SaveTexture(filename, GL_TEXTURE_2D_ARRAY, texture, virtual_width, virtual_height, level); + return SaveTexture(filename, GL_TEXTURE_2D_ARRAY, texture, config.width, config.height, level); } TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width, unsigned int height, @@ -164,7 +165,12 @@ TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width, u } } - TCacheEntry &entry = *new TCacheEntry; + TCacheEntryConfig config; + config.width = width; + config.height = height; + config.levels = tex_levels; + + TCacheEntry &entry = *new TCacheEntry(config); entry.gl_format = gl_format; entry.gl_iformat = gl_iformat; entry.gl_type = gl_type; @@ -205,9 +211,15 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, } TextureCache::TCacheEntryBase* TextureCache::CreateRenderTargetTexture( - unsigned int scaled_tex_w, unsigned int scaled_tex_h) + unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) { - TCacheEntry *const entry = new TCacheEntry; + TCacheEntryConfig config; + config.width = scaled_tex_w; + config.height = scaled_tex_h; + config.layers = layers; + config.rendertarget = true; + TCacheEntry *const entry = new TCacheEntry(config); + glActiveTexture(GL_TEXTURE0+9); glBindTexture(GL_TEXTURE_2D_ARRAY, entry->texture); @@ -217,7 +229,7 @@ TextureCache::TCacheEntryBase* TextureCache::CreateRenderTargetTexture( gl_type = GL_UNSIGNED_BYTE; glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 0); - glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, gl_iformat, scaled_tex_w, scaled_tex_h, FramebufferManager::GetEFBLayers(), 0, gl_format, gl_type, nullptr); + glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, gl_iformat, scaled_tex_w, scaled_tex_h, layers, 0, gl_format, gl_type, nullptr); glBindTexture(GL_TEXTURE_2D_ARRAY, 0); glGenFramebuffers(1, &entry->framebuffer); @@ -250,7 +262,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo glActiveTexture(GL_TEXTURE0+9); glBindTexture(GL_TEXTURE_2D_ARRAY, read_texture); - glViewport(0, 0, virtual_width, virtual_height); + glViewport(0, 0, config.width, config.height); GLuint uniform_location; if (srcFormat == PEControl::Z24) @@ -308,7 +320,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFo { static int count = 0; SaveTexture(StringFromFormat("%sefb_frame_%i.png", File::GetUserPath(D_DUMPTEXTURES_IDX).c_str(), - count++), GL_TEXTURE_2D_ARRAY, texture, virtual_width, virtual_height, 0); + count++), GL_TEXTURE_2D_ARRAY, texture, config.width, config.height, 0); } g_renderer->RestoreAPIState(); diff --git a/Source/Core/VideoBackends/OGL/TextureCache.h b/Source/Core/VideoBackends/OGL/TextureCache.h index c2001d4bca..270e0d6f1a 100644 --- a/Source/Core/VideoBackends/OGL/TextureCache.h +++ b/Source/Core/VideoBackends/OGL/TextureCache.h @@ -36,7 +36,7 @@ private: //TexMode0 mode; // current filter and clamp modes that texture is set to //TexMode1 mode1; // current filter and clamp modes that texture is set to - TCacheEntry(); + TCacheEntry(const TCacheEntryConfig& config); ~TCacheEntry(); void Load(unsigned int width, unsigned int height, @@ -56,7 +56,7 @@ private: TCacheEntryBase* CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels, PC_TexFormat pcfmt) override; - TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h) override; + TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) override; void CompileShaders() override; void DeleteShaders() override; diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index 99bac2b519..fd64f9b701 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -13,6 +13,7 @@ #include "Core/HW/Memmap.h" #include "VideoCommon/Debugger.h" +#include "VideoCommon/FramebufferManagerBase.h" #include "VideoCommon/HiresTextures.h" #include "VideoCommon/RenderBase.h" #include "VideoCommon/Statistics.h" @@ -383,7 +384,7 @@ TextureCache::TCacheEntryBase* TextureCache::Load(const u32 stage) // 2. b) For normal textures, all texture parameters need to match if (address == entry->addr && tex_hash == entry->hash && full_format == entry->format && - entry->tex_levels >= tex_levels && entry->native_width == nativeW && entry->native_height == nativeH) + entry->config.levels >= tex_levels && entry->native_width == nativeW && entry->native_height == nativeH) { return ReturnEntry(stage, entry); } @@ -394,14 +395,14 @@ TextureCache::TCacheEntryBase* TextureCache::Load(const u32 stage) // TODO: Don't we need to force texture decoding to RGBA8 for dynamic EFB copies? // TODO: Actually, it should be enough if the internal texture format matches... if (((entry->type == TCET_NORMAL && - width == entry->virtual_width && - height == entry->virtual_height && + width == entry->config.width && + height == entry->config.height && full_format == entry->format && - entry->tex_levels >= tex_levels) || + entry->config.levels >= tex_levels) || (entry->type == TCET_EC_DYNAMIC && entry->native_width == width && entry->native_height == height)) && - entry->num_layers == 1) + entry->config.layers == 1) { // reuse the texture } @@ -470,23 +471,13 @@ TextureCache::TCacheEntryBase* TextureCache::Load(const u32 stage) if (nullptr == entry) { textures[texID] = entry = g_texture_cache->CreateTexture(width, height, texLevels, pcfmt); - - // Sometimes, we can get around recreating a texture if only the number of mip levels changes - // e.g. if our texture cache entry got too many mipmap levels we can limit the number of used levels by setting the appropriate render states - // Thus, we don't update this member for every Load, but just whenever the texture gets recreated - - // TODO: This is the wrong value. We should be storing the number of levels our actual texture has. - // But that will currently make the above "existing entry" tests fail as "texLevels" is not calculated until after. - // Currently, we might try to reuse a texture which appears to have more levels than actual, maybe.. - entry->tex_levels = tex_levels; - entry->num_layers = 1; entry->type = TCET_NORMAL; GFX_DEBUGGER_PAUSE_AT(NEXT_NEW_TEXTURE, true); } - entry->SetGeneralParameters(address, texture_size, full_format, entry->tex_levels, entry->num_layers); - entry->SetDimensions(nativeW, nativeH, width, height); + entry->SetGeneralParameters(address, texture_size, full_format); + entry->SetDimensions(nativeW, nativeH); entry->hash = tex_hash; // load texture @@ -854,12 +845,12 @@ void TextureCache::CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFormat TCacheEntryBase *entry = textures[dstAddr]; if (entry) { - if (entry->type == TCET_EC_DYNAMIC && entry->native_width == tex_w && entry->native_height == tex_h && entry->num_layers == efb_layers) + if (entry->type == TCET_EC_DYNAMIC && entry->native_width == tex_w && entry->native_height == tex_h && entry->config.layers == efb_layers) { scaled_tex_w = tex_w; scaled_tex_h = tex_h; } - else if (!(entry->type == TCET_EC_VRAM && entry->virtual_width == scaled_tex_w && entry->virtual_height == scaled_tex_h && entry->num_layers == efb_layers)) + else if (!(entry->type == TCET_EC_VRAM && entry->config.width == scaled_tex_w && entry->config.height == scaled_tex_h && entry->config.layers == efb_layers)) { if (entry->type == TCET_EC_VRAM) { @@ -879,11 +870,11 @@ void TextureCache::CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFormat if (nullptr == entry) { // create the texture - textures[dstAddr] = entry = AllocateRenderTarget(scaled_tex_w, scaled_tex_h); + textures[dstAddr] = entry = AllocateRenderTarget(scaled_tex_w, scaled_tex_h, FramebufferManagerBase::GetEFBLayers()); // TODO: Using the wrong dstFormat, dumb... - entry->SetGeneralParameters(dstAddr, 0, dstFormat, 1, efb_layers); - entry->SetDimensions(tex_w, tex_h, scaled_tex_w, scaled_tex_h); + entry->SetGeneralParameters(dstAddr, 0, dstFormat); + entry->SetDimensions(tex_w, tex_h); entry->SetHashes(TEXHASH_INVALID); entry->type = TCET_EC_VRAM; } @@ -893,13 +884,13 @@ void TextureCache::CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFormat entry->FromRenderTarget(dstAddr, dstFormat, srcFormat, srcRect, isIntensity, scaleByHalf, cbufid, colmat); } -TextureCache::TCacheEntryBase* TextureCache::AllocateRenderTarget(unsigned int width, unsigned int height) +TextureCache::TCacheEntryBase* TextureCache::AllocateRenderTarget(unsigned int width, unsigned int height, unsigned int layers) { for (size_t i = 0; i < render_target_pool.size(); ++i) { auto rt = render_target_pool[i]; - if (rt->virtual_width != width || rt->virtual_height != height) + if (rt->config.width != width || rt->config.height != height || rt->config.layers != layers) continue; render_target_pool[i] = render_target_pool.back(); @@ -908,7 +899,7 @@ TextureCache::TCacheEntryBase* TextureCache::AllocateRenderTarget(unsigned int w return rt; } - return g_texture_cache->CreateRenderTargetTexture(width, height); + return g_texture_cache->CreateRenderTargetTexture(width, height, layers); } void TextureCache::FreeRenderTarget(TCacheEntryBase* entry) diff --git a/Source/Core/VideoCommon/TextureCacheBase.h b/Source/Core/VideoCommon/TextureCacheBase.h index 0d0bc90314..94acccdc5f 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.h +++ b/Source/Core/VideoCommon/TextureCacheBase.h @@ -25,8 +25,19 @@ public: TCET_EC_DYNAMIC, // EFB copy which sits in RAM and needs to be decoded before being used }; + struct TCacheEntryConfig + { + TCacheEntryConfig() : width(0), height(0), levels(1), layers(1), rendertarget(false) {} + + u32 width, height; + u32 levels, layers; + bool rendertarget; + }; + struct TCacheEntryBase { + const TCacheEntryConfig config; + // common members u32 addr; u32 size_in_bytes; @@ -35,30 +46,23 @@ public: enum TexCacheEntryType type; - unsigned int tex_levels; - unsigned int num_layers; unsigned int native_width, native_height; // Texture dimensions from the GameCube's point of view - unsigned int virtual_width, virtual_height; // Texture dimensions from OUR point of view - for hires textures or scaled EFB copies // used to delete textures which haven't been used for TEXTURE_KILL_THRESHOLD frames int frameCount; - void SetGeneralParameters(u32 _addr, u32 _size, u32 _format, unsigned int _tex_levels, unsigned int _num_layers) + void SetGeneralParameters(u32 _addr, u32 _size, u32 _format) { addr = _addr; size_in_bytes = _size; format = _format; - tex_levels = _tex_levels; - num_layers = _num_layers; } - void SetDimensions(unsigned int _native_width, unsigned int _native_height, unsigned int _virtual_width, unsigned int _virtual_height) + void SetDimensions(unsigned int _native_width, unsigned int _native_height) { native_width = _native_width; native_height = _native_height; - virtual_width = _virtual_width; - virtual_height = _virtual_height; } void SetHashes(u64 _hash) @@ -66,7 +70,7 @@ public: hash = _hash; } - + TCacheEntryBase(const TCacheEntryConfig& c) : config(c) {} virtual ~TCacheEntryBase(); virtual void Bind(unsigned int stage) = 0; @@ -100,7 +104,7 @@ public: virtual TCacheEntryBase* CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels, PC_TexFormat pcfmt) = 0; - virtual TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h) = 0; + virtual TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) = 0; virtual void CompileShaders() = 0; // currently only implemented by OGL virtual void DeleteShaders() = 0; // currently only implemented by OGL @@ -121,7 +125,7 @@ private: static void DumpTexture(TCacheEntryBase* entry, std::string basename, unsigned int level); static void CheckTempSize(size_t required_size); - static TCacheEntryBase* AllocateRenderTarget(unsigned int width, unsigned int height); + static TCacheEntryBase* AllocateRenderTarget(unsigned int width, unsigned int height, unsigned int layers); static void FreeRenderTarget(TCacheEntryBase* entry); typedef std::map TexCache;