From 6cd6e6546f8a6481bd343fb9d1e22238491c2578 Mon Sep 17 00:00:00 2001 From: degasus Date: Sat, 17 Jan 2015 10:01:41 +0100 Subject: [PATCH] TexCache: merge texture and rendertarget factory function --- .../Core/VideoBackends/D3D/TextureCache.cpp | 64 ++++++++----------- Source/Core/VideoBackends/D3D/TextureCache.h | 3 +- .../Core/VideoBackends/OGL/TextureCache.cpp | 54 +++++----------- Source/Core/VideoBackends/OGL/TextureCache.h | 4 +- Source/Core/VideoCommon/TextureCacheBase.cpp | 14 +++- Source/Core/VideoCommon/TextureCacheBase.h | 3 +- 6 files changed, 59 insertions(+), 83 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/TextureCache.cpp b/Source/Core/VideoBackends/D3D/TextureCache.cpp index 93a94c0a9e..aab90e2560 100644 --- a/Source/Core/VideoBackends/D3D/TextureCache.cpp +++ b/Source/Core/VideoBackends/D3D/TextureCache.cpp @@ -81,39 +81,43 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, D3D::ReplaceRGBATexture2D(texture->GetTex(), TextureCache::temp, width, height, expanded_width, level, usage); } -TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels) +TextureCache::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config) { - D3D11_USAGE usage = D3D11_USAGE_DEFAULT; - D3D11_CPU_ACCESS_FLAG cpu_access = (D3D11_CPU_ACCESS_FLAG)0; - - if (tex_levels == 1) + if (config.rendertarget) { - usage = D3D11_USAGE_DYNAMIC; - cpu_access = D3D11_CPU_ACCESS_WRITE; + return new TCacheEntry(config, D3DTexture2D::Create(config.width, config.height, + (D3D11_BIND_FLAG)((int)D3D11_BIND_RENDER_TARGET | (int)D3D11_BIND_SHADER_RESOURCE), + D3D11_USAGE_DEFAULT, DXGI_FORMAT_R8G8B8A8_UNORM, 1, config.layers)); } + else + { + D3D11_USAGE usage = D3D11_USAGE_DEFAULT; + D3D11_CPU_ACCESS_FLAG cpu_access = (D3D11_CPU_ACCESS_FLAG)0; - const D3D11_TEXTURE2D_DESC texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, - width, height, 1, tex_levels, D3D11_BIND_SHADER_RESOURCE, usage, cpu_access); + if (config.levels == 1) + { + usage = D3D11_USAGE_DYNAMIC; + cpu_access = D3D11_CPU_ACCESS_WRITE; + } - ID3D11Texture2D *pTexture; - const HRESULT hr = D3D::device->CreateTexture2D(&texdesc, nullptr, &pTexture); - CHECK(SUCCEEDED(hr), "Create texture of the TextureCache"); + const D3D11_TEXTURE2D_DESC texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, + config.width, config.height, 1, config.levels, D3D11_BIND_SHADER_RESOURCE, usage, cpu_access); - TCacheEntryConfig config; - config.width = width; - config.height = height; - config.levels = tex_levels; + ID3D11Texture2D *pTexture; + const HRESULT hr = D3D::device->CreateTexture2D(&texdesc, nullptr, &pTexture); + CHECK(SUCCEEDED(hr), "Create texture of the TextureCache"); - TCacheEntry* const entry = new TCacheEntry(config, new D3DTexture2D(pTexture, D3D11_BIND_SHADER_RESOURCE)); - entry->usage = usage; + TCacheEntry* const entry = new TCacheEntry(config, new D3DTexture2D(pTexture, D3D11_BIND_SHADER_RESOURCE)); + entry->usage = usage; - // TODO: better debug names - D3D::SetDebugObjectName((ID3D11DeviceChild*)entry->texture->GetTex(), "a texture of the TextureCache"); - D3D::SetDebugObjectName((ID3D11DeviceChild*)entry->texture->GetSRV(), "shader resource view of a texture of the TextureCache"); + // TODO: better debug names + D3D::SetDebugObjectName((ID3D11DeviceChild*)entry->texture->GetTex(), "a texture of the TextureCache"); + D3D::SetDebugObjectName((ID3D11DeviceChild*)entry->texture->GetSRV(), "shader resource view of a texture of the TextureCache"); - SAFE_RELEASE(pTexture); + SAFE_RELEASE(pTexture); - return entry; + return entry; + } } void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFormat, @@ -191,20 +195,6 @@ 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 layers) -{ - 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, layers)); -} - TextureCache::TextureCache() { // FIXME: Is it safe here? diff --git a/Source/Core/VideoBackends/D3D/TextureCache.h b/Source/Core/VideoBackends/D3D/TextureCache.h index 62dcb076e9..49dfc15340 100644 --- a/Source/Core/VideoBackends/D3D/TextureCache.h +++ b/Source/Core/VideoBackends/D3D/TextureCache.h @@ -38,9 +38,8 @@ private: bool Save(const std::string& filename, unsigned int level) override; }; - TCacheEntryBase* CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels) override; + TCacheEntryBase* CreateTexture(const TCacheEntryConfig& config) 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 15df7dedb1..1f12d5c148 100644 --- a/Source/Core/VideoBackends/OGL/TextureCache.cpp +++ b/Source/Core/VideoBackends/OGL/TextureCache.cpp @@ -109,22 +109,28 @@ bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int l return SaveTexture(filename, GL_TEXTURE_2D_ARRAY, texture, config.width, config.height, level); } -TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels) +TextureCache::TCacheEntryBase* TextureCache::CreateTexture(const TCacheEntryConfig& config) { - TCacheEntryConfig config; - config.width = width; - config.height = height; - config.levels = tex_levels; - - TCacheEntry &entry = *new TCacheEntry(config); + TCacheEntry* entry = new TCacheEntry(config); glActiveTexture(GL_TEXTURE0+9); - glBindTexture(GL_TEXTURE_2D_ARRAY, entry.texture); - glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, tex_levels - 1); + glBindTexture(GL_TEXTURE_2D_ARRAY, entry->texture); + + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, config.levels - 1); + + if (config.rendertarget) + { + for (u32 level = 0; level <= config.levels; level++) + { + glTexImage3D(GL_TEXTURE_2D_ARRAY, level, GL_RGBA, config.width, config.height, config.layers, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + } + glGenFramebuffers(1, &entry->framebuffer); + FramebufferManager::SetFramebuffer(entry->framebuffer); + FramebufferManager::FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_ARRAY, entry->texture, 0); + } TextureCache::SetStage(); - - return &entry; + return entry; } void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, @@ -150,32 +156,6 @@ void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, TextureCache::SetStage(); } -TextureCache::TCacheEntryBase* TextureCache::CreateRenderTargetTexture( - unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) -{ - 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); - - glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 0); - glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, scaled_tex_w, scaled_tex_h, layers, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); - glBindTexture(GL_TEXTURE_2D_ARRAY, 0); - - glGenFramebuffers(1, &entry->framebuffer); - FramebufferManager::SetFramebuffer(entry->framebuffer); - FramebufferManager::FramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_ARRAY, entry->texture, 0); - - SetStage(); - - return entry; -} - void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFormat, PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect, bool isIntensity, bool scaleByHalf, unsigned int cbufid, diff --git a/Source/Core/VideoBackends/OGL/TextureCache.h b/Source/Core/VideoBackends/OGL/TextureCache.h index b60b98427f..a855b450b6 100644 --- a/Source/Core/VideoBackends/OGL/TextureCache.h +++ b/Source/Core/VideoBackends/OGL/TextureCache.h @@ -47,9 +47,7 @@ private: ~TextureCache(); - TCacheEntryBase* CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels) override; - - TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) override; + TCacheEntryBase* CreateTexture(const TCacheEntryConfig& config) override; void CompileShaders() override; void DeleteShaders() override; diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index 36f059181e..a30716f45c 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -474,7 +474,11 @@ TextureCache::TCacheEntryBase* TextureCache::Load(const u32 stage) // create the entry/texture if (nullptr == entry) { - textures[texID] = entry = g_texture_cache->CreateTexture(width, height, texLevels); + TCacheEntryConfig config; + config.width = width; + config.height = height; + config.levels = texLevels; + textures[texID] = entry = g_texture_cache->CreateTexture(config); entry->type = TCET_NORMAL; GFX_DEBUGGER_PAUSE_AT(NEXT_NEW_TEXTURE, true); @@ -900,7 +904,13 @@ TextureCache::TCacheEntryBase* TextureCache::AllocateRenderTarget(unsigned int w return rt; } - return g_texture_cache->CreateRenderTargetTexture(width, height, layers); + TCacheEntryConfig config; + config.rendertarget = true; + config.width = width; + config.height = height; + config.layers = layers; + + return g_texture_cache->CreateTexture(config); } void TextureCache::FreeRenderTarget(TCacheEntryBase* entry) diff --git a/Source/Core/VideoCommon/TextureCacheBase.h b/Source/Core/VideoCommon/TextureCacheBase.h index 09489e9462..6e64e5fbad 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.h +++ b/Source/Core/VideoCommon/TextureCacheBase.h @@ -104,8 +104,7 @@ public: static void ClearRenderTargets(); // currently only used by OGL static bool Find(u32 start_address, u64 hash); - virtual TCacheEntryBase* CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels) = 0; - virtual TCacheEntryBase* CreateRenderTargetTexture(unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers) = 0; + virtual TCacheEntryBase* CreateTexture(const TCacheEntryConfig& config) = 0; virtual void CompileShaders() = 0; // currently only implemented by OGL virtual void DeleteShaders() = 0; // currently only implemented by OGL