GSTextureCache: Reformat code and adjust comments.

This commit is contained in:
Alessandro Vetere 2018-12-10 21:18:47 +01:00 committed by lightningterror
parent e1dff69a43
commit c368afd6d2
2 changed files with 28 additions and 43 deletions

View File

@ -28,8 +28,8 @@ bool GSTextureCache::m_disable_partial_invalidation = false;
bool GSTextureCache::m_wrap_gs_mem = false; bool GSTextureCache::m_wrap_gs_mem = false;
GSTextureCache::GSTextureCache(GSRenderer* r) GSTextureCache::GSTextureCache(GSRenderer* r)
: m_renderer(r), : m_renderer(r)
m_palette_map(r) , m_palette_map(r)
{ {
s_IS_DIRECT3D9 = theApp.GetCurrentRendererType() == GSRendererType::DX9_HW; s_IS_DIRECT3D9 = theApp.GetCurrentRendererType() == GSRendererType::DX9_HW;
@ -166,7 +166,6 @@ GSTextureCache::Source* GSTextureCache::LookupDepthSource(const GIFRegTEX0& TEX0
// If it is too expensive, one could cut memory allocation in Source constructor for this // If it is too expensive, one could cut memory allocation in Source constructor for this
// use case. // use case.
if (palette) { if (palette) {
// Palette is attached (Palette object, clut copy and palette texture) directly because the texture is not going in the texture cache list
AttachPaletteToSource(src, psm_s.pal, true); AttachPaletteToSource(src, psm_s.pal, true);
} }
@ -1335,7 +1334,6 @@ GSTextureCache::Source* GSTextureCache::CreateSource(const GIFRegTEX0& TEX0, con
// Because the texture is already on the GPU, CPU can't convert it. // Because the texture is already on the GPU, CPU can't convert it.
if (psm.pal > 0) { if (psm.pal > 0) {
src->m_should_have_tex_palette = true; src->m_should_have_tex_palette = true;
// Palette (Palette object, clut copy and palette texture) is going to be attached in Source lookup code exploiting initial condition !src->m_clut
} }
// Disable linear filtering for various GS post-processing effect // Disable linear filtering for various GS post-processing effect
// 1/ Palette is used to interpret the alpha channel of the RT as an index. // 1/ Palette is used to interpret the alpha channel of the RT as an index.
@ -1450,11 +1448,9 @@ GSTextureCache::Source* GSTextureCache::CreateSource(const GIFRegTEX0& TEX0, con
{ {
src->m_texture = m_renderer->m_dev->CreateTexture(tw, th, Get8bitFormat()); src->m_texture = m_renderer->m_dev->CreateTexture(tw, th, Get8bitFormat());
src->m_should_have_tex_palette = true; src->m_should_have_tex_palette = true;
// Palette (Palette object, clut copy and palette texture) is going to be attached in Source lookup code exploiting initial condition !src->m_clut
} }
else { else {
src->m_texture = m_renderer->m_dev->CreateTexture(tw, th); src->m_texture = m_renderer->m_dev->CreateTexture(tw, th);
// by default: src->m_should_have_tex_palette = false;
AttachPaletteToSource(src, psm.pal, false); // Attach only Palette object and clut copy AttachPaletteToSource(src, psm.pal, false); // Attach only Palette object and clut copy
} }
} }
@ -2035,7 +2031,6 @@ void GSTextureCache::SourceMap::RemoveAt(Source* s)
delete s; delete s;
} }
// Query the PaletteMap for a valid Palette, then assign its reference, CLUT copy pointer and optionally its palette texture pointer to the Source object.
void GSTextureCache::AttachPaletteToSource(Source* s, uint16 pal, bool need_gs_texture) void GSTextureCache::AttachPaletteToSource(Source* s, uint16 pal, bool need_gs_texture)
{ {
std::shared_ptr<Palette> p = m_palette_map.LookupPalette(pal, need_gs_texture); std::shared_ptr<Palette> p = m_palette_map.LookupPalette(pal, need_gs_texture);
@ -2048,7 +2043,6 @@ void GSTextureCache::AttachPaletteToSource(Source* s, uint16 pal, bool need_gs_t
// GSTextureCache::Palette // GSTextureCache::Palette
// Creates a copy of the current clut with eventually a new palette texture with its content
GSTextureCache::Palette::Palette(const GSRenderer* renderer, uint16 pal, bool need_gs_texture) { GSTextureCache::Palette::Palette(const GSRenderer* renderer, uint16 pal, bool need_gs_texture) {
uint16 palette_size = pal * sizeof(uint32); uint16 palette_size = pal * sizeof(uint32);
m_clut = (uint32*)_aligned_malloc(palette_size, 64); m_clut = (uint32*)_aligned_malloc(palette_size, 64);
@ -2063,7 +2057,6 @@ GSTextureCache::Palette::Palette(const GSRenderer* renderer, uint16 pal, bool ne
} }
} }
// Default destructor, frees clut copy and recycles eventual palette texture
GSTextureCache::Palette::~Palette() { GSTextureCache::Palette::~Palette() {
if (GetPaletteGSTexture()) { if (GetPaletteGSTexture()) {
m_renderer->m_dev->Recycle(GetPaletteGSTexture()); // Recycle palette texture, if any m_renderer->m_dev->Recycle(GetPaletteGSTexture()); // Recycle palette texture, if any
@ -2096,23 +2089,23 @@ std::size_t GSTextureCache::PaletteKeyHash::operator()(const PaletteKey &key) co
size_t clut_hash = 3831179159; size_t clut_hash = 3831179159;
for (uint16 i = 0; i < pal; i += 16) { for (uint16 i = 0; i < pal; i += 16) {
clut_hash = (clut_hash + 1488000301) ^ (clut[i] + 33644011); clut_hash = (clut_hash + 1488000301) ^ (clut[i ] + 33644011);
clut_hash = (clut_hash + 3831179159) ^ (clut[i + 1] + 47627467); clut_hash = (clut_hash + 3831179159) ^ (clut[i + 1] + 47627467);
clut_hash = (clut_hash + 3659574209) ^ (clut[i + 2] + 577038523); clut_hash = (clut_hash + 3659574209) ^ (clut[i + 2] + 577038523);
clut_hash = (clut_hash + 33644011) ^ (clut[i + 3] + 3491555267); clut_hash = (clut_hash + 33644011) ^ (clut[i + 3] + 3491555267);
clut_hash = (clut_hash + 777771959) ^ (clut[i + 4] + 3301075993); clut_hash = (clut_hash + 777771959) ^ (clut[i + 4] + 3301075993);
clut_hash = (clut_hash + 4019618579) ^ (clut[i + 5] + 4186992613); clut_hash = (clut_hash + 4019618579) ^ (clut[i + 5] + 4186992613);
clut_hash = (clut_hash + 3465668953) ^ (clut[i + 6] + 3043435883); clut_hash = (clut_hash + 3465668953) ^ (clut[i + 6] + 3043435883);
clut_hash = (clut_hash + 3494478943) ^ (clut[i + 7] + 3441897883); clut_hash = (clut_hash + 3494478943) ^ (clut[i + 7] + 3441897883);
clut_hash = (clut_hash + 3432010979) ^ (clut[i + 8] + 2167922789); clut_hash = (clut_hash + 3432010979) ^ (clut[i + 8] + 2167922789);
clut_hash = (clut_hash + 1570862863) ^ (clut[i + 9] + 3401920591); clut_hash = (clut_hash + 1570862863) ^ (clut[i + 9] + 3401920591);
clut_hash = (clut_hash + 1002648679) ^ (clut[i + 10] + 1293530519); clut_hash = (clut_hash + 1002648679) ^ (clut[i + 10] + 1293530519);
clut_hash = (clut_hash + 551381741) ^ (clut[i + 11] + 2539834039); clut_hash = (clut_hash + 551381741) ^ (clut[i + 11] + 2539834039);
clut_hash = (clut_hash + 3768974459) ^ (clut[i + 12] + 169943507); clut_hash = (clut_hash + 3768974459) ^ (clut[i + 12] + 169943507);
clut_hash = (clut_hash + 862380703) ^ (clut[i + 13] + 2906932549); clut_hash = (clut_hash + 862380703) ^ (clut[i + 13] + 2906932549);
clut_hash = (clut_hash + 3433082137) ^ (clut[i + 14] + 4234384109); clut_hash = (clut_hash + 3433082137) ^ (clut[i + 14] + 4234384109);
clut_hash = (clut_hash + 2679083843) ^ (clut[i + 15] + 2719605247); clut_hash = (clut_hash + 2679083843) ^ (clut[i + 15] + 2719605247);
} }
@ -2121,7 +2114,6 @@ std::size_t GSTextureCache::PaletteKeyHash::operator()(const PaletteKey &key) co
// GSTextureCache::PaletteKeyEqual // GSTextureCache::PaletteKeyEqual
// Compare clut contents
bool GSTextureCache::PaletteKeyEqual::operator()(const PaletteKey &lhs, const PaletteKey &rhs) const { bool GSTextureCache::PaletteKeyEqual::operator()(const PaletteKey &lhs, const PaletteKey &rhs) const {
if (lhs.pal != rhs.pal) { if (lhs.pal != rhs.pal) {
return false; return false;
@ -2132,15 +2124,14 @@ bool GSTextureCache::PaletteKeyEqual::operator()(const PaletteKey &lhs, const Pa
// GSTextureCache::PaletteMap // GSTextureCache::PaletteMap
// Default constructor, stores renderer pointer and reverses space in the maps GSTextureCache::PaletteMap::PaletteMap(const GSRenderer* renderer)
GSTextureCache::PaletteMap::PaletteMap(const GSRenderer* renderer) { : m_renderer(renderer)
this->m_renderer = renderer; {
for (auto& map : m_maps) { for (auto& map : m_maps) {
map.reserve(MAX_SIZE); map.reserve(MAX_SIZE);
} }
} }
// Retrieves the palette with the desired clut
std::shared_ptr<GSTextureCache::Palette> GSTextureCache::PaletteMap::LookupPalette(uint16 pal, bool need_gs_texture) { std::shared_ptr<GSTextureCache::Palette> GSTextureCache::PaletteMap::LookupPalette(uint16 pal, bool need_gs_texture) {
ASSERT(pal == 16 || pal == 256); ASSERT(pal == 16 || pal == 256);
@ -2161,7 +2152,7 @@ std::shared_ptr<GSTextureCache::Palette> GSTextureCache::PaletteMap::LookupPalet
return it1->second; return it1->second;
} }
// No Palette with matching clut content hash, MISS // No palette with matching clut content, MISS
if (map.size() > MAX_SIZE) { if (map.size() > MAX_SIZE) {
// If the map is too big, try to clean it by disposing and removing unused palettes, before adding the new one // If the map is too big, try to clean it by disposing and removing unused palettes, before adding the new one
@ -2193,18 +2184,14 @@ std::shared_ptr<GSTextureCache::Palette> GSTextureCache::PaletteMap::LookupPalet
} }
} }
// Create new Palette using shared pointer
std::shared_ptr<Palette> palette = std::make_shared<Palette>(m_renderer, pal, need_gs_texture); std::shared_ptr<Palette> palette = std::make_shared<Palette>(m_renderer, pal, need_gs_texture);
// Create key for storing the Palette into the map (use copy of the clut stored into Palette itself as key attribute)
palette_key = { palette->GetClut(), pal }; palette_key = { palette->GetClut(), pal };
// Add the new palette to the map
map.emplace(palette_key, palette); map.emplace(palette_key, palette);
GL_CACHE("TC, %u-bit PaletteMap (Size %u): Added new palette.", pal * sizeof(uint32), map.size()); GL_CACHE("TC, %u-bit PaletteMap (Size %u): Added new palette.", pal * sizeof(uint32), map.size());
// Return the shared pointer to the newly created Palette
return palette; return palette;
} }

View File

@ -54,13 +54,13 @@ public:
class Palette class Palette
{ {
private: private:
uint32* m_clut; // Pointer to a copy of relevant clut uint32* m_clut;
GSTexture* m_tex_palette; // Pointer to valid texture with relevant clut as content, if instantiated by the constructor GSTexture* m_tex_palette;
const GSRenderer* m_renderer; // Pointer to the current renderer, needed to recycle the eventually referenced GSTexture on destruction const GSRenderer* m_renderer;
public: public:
Palette(const GSRenderer* renderer, uint16 pal, bool need_gs_texture); // Creates a copy of the current clut and, if needed (need_gs_texture == true), a texture with its content Palette(const GSRenderer* renderer, uint16 pal, bool need_gs_texture);
~Palette(); // Default destructor, frees clut copy and eventually recycles palette texture ~Palette();
// Disable copy constructor and copy operator // Disable copy constructor and copy operator
Palette(const Palette&) = delete; Palette(const Palette&) = delete;
@ -70,10 +70,8 @@ public:
Palette(const Palette&&) = delete; Palette(const Palette&&) = delete;
Palette& operator=(const Palette&&) = delete; Palette& operator=(const Palette&&) = delete;
// Getter for clut pointer
uint32* GetClut(); uint32* GetClut();
// Getter for palette texture pointer, may be nullptr if object has been instantiated with need_gs_texture == false
GSTexture* GetPaletteGSTexture(); GSTexture* GetPaletteGSTexture();
}; };
@ -88,7 +86,7 @@ public:
}; };
struct PaletteKeyEqual { struct PaletteKeyEqual {
// Compare clut contents // Compare pal value and clut contents
bool operator()(const PaletteKey &lhs, const PaletteKey &rhs) const; bool operator()(const PaletteKey &lhs, const PaletteKey &rhs) const;
}; };
@ -100,7 +98,7 @@ public:
void Flush(uint32 count, int layer); void Flush(uint32 count, int layer);
public: public:
std::shared_ptr<Palette> m_palette_obj; // Shared pointer to the relevant Palette object (if any) std::shared_ptr<Palette> m_palette_obj;
GSTexture* m_palette; GSTexture* m_palette;
bool m_should_have_tex_palette; // Enables m_clut (and possibly m_palette) recycling on object destruction bool m_should_have_tex_palette; // Enables m_clut (and possibly m_palette) recycling on object destruction
uint32 m_valid[MAX_PAGES]; // each uint32 bits map to the 32 blocks of that page uint32 m_valid[MAX_PAGES]; // each uint32 bits map to the 32 blocks of that page
@ -151,7 +149,7 @@ public:
{ {
private: private:
static const uint16 MAX_SIZE = 65535; // Max size of each map. static const uint16 MAX_SIZE = 65535; // Max size of each map.
const GSRenderer* m_renderer; // Reference to the current renderer const GSRenderer* m_renderer;
// Array of 2 maps, the first for 64B palettes and the second for 1024B palettes. // Array of 2 maps, the first for 64B palettes and the second for 1024B palettes.
// Each map stores the key PaletteKey (clut copy, pal value) pointing to the relevant shared pointer to Palette object. // Each map stores the key PaletteKey (clut copy, pal value) pointing to the relevant shared pointer to Palette object.
@ -159,7 +157,7 @@ public:
std::array<std::unordered_map<PaletteKey, std::shared_ptr<Palette>, PaletteKeyHash, PaletteKeyEqual>, 2> m_maps; std::array<std::unordered_map<PaletteKey, std::shared_ptr<Palette>, PaletteKeyHash, PaletteKeyEqual>, 2> m_maps;
public: public:
PaletteMap(const GSRenderer* renderer); // Default constructor PaletteMap(const GSRenderer* renderer);
// Retrieves a shared pointer to a valid Palette from m_maps or creates a new one adding it to the data structure // Retrieves a shared pointer to a valid Palette from m_maps or creates a new one adding it to the data structure
std::shared_ptr<Palette> LookupPalette(uint16 pal, bool need_gs_texture); std::shared_ptr<Palette> LookupPalette(uint16 pal, bool need_gs_texture);