| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // Licensed under GPLv2+ |
| 3 | // Refer to the license.txt file included. |
| 4 | |
| 5 | #include "VideoCommon/TMEM.h" |
| 6 | |
| 7 | #include <array> |
| 8 | |
| 9 | #include "Common/ChunkFile.h" |
| 10 | #include "VideoCommon/BPMemory.h" |
| 11 | |
| 12 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 13 | // |
| 14 | // TMEM emulation tracks which textures should be cached in TMEM on a real console. |
| 15 | // There are two good reasons to do this: |
| 16 | // |
| 17 | // 1. Some games deliberately avoid invalidating a texture, overwrite it with an EFB copy, |
| 18 | // and then expect the original texture to still be found in TMEM for another draw call. |
| 19 | // Spyro: A Hero's Tail is known for using such overwritten textures. |
| 20 | // However, other games like: |
| 21 | // * Sonic Riders |
| 22 | // * Metal Arms: Glitch in the System |
| 23 | // * Godzilla: Destroy All Monsters Melee |
| 24 | // * NHL Slapshot |
| 25 | // * Tak and the Power of Juju |
| 26 | // * Night at the Museum: Battle of the Smithsonian |
| 27 | // * 428: Fūsa Sareta Shibuya de |
| 28 | // are known to (accidentally or deliberately) avoid invalidating and then expect the pattern |
| 29 | // of the draw and the fact that the whole texture doesn't fit in TMEM to self-invalidate the |
| 30 | // texture. These are usually full-screen efb copies. |
| 31 | // So we must track the size of the textures as an heuristic to see if they will self-invalidate |
| 32 | // or not. |
| 33 | // |
| 34 | // 2. It actually improves Dolphin's performance in safer texture hashing modes, by reducing the |
| 35 | // amount of times a texture needs to be hashed when reused in subsequent draws. |
| 36 | // |
| 37 | // As a side-effect, TMEM emulation also tracks if the texture unit configuration has changed at |
| 38 | // all, which Dolphin's TextureCache takes advantage of. |
| 39 | // |
| 40 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 41 | // |
| 42 | // Checking if a texture fits in TMEM or not is complicated by the fact that Flipper's TMEM is quite |
| 43 | // configurable. |
| 44 | // Each of the eight texture units has two banks (even and odd) that can be pointed at any offset |
| 45 | // and set to any size. It is completely valid to have overlapping banks, and performance can be |
| 46 | // improved by overlapping the caches of texture units that are drawing the same textures. |
| 47 | // |
| 48 | // For trilinear textures, the even/odd banks contain the even/odd LODs of the texture. TMEM has two |
| 49 | // banks of 512KB each, covering the upper and lower halves of TMEM's address space. The two banks |
| 50 | // be accessed simultaneously, allowing a trilinear texture sample to be completed at the same cost |
| 51 | // as a bilinear sample, assuming the even and odd banks are mapped onto different banks. |
| 52 | // |
| 53 | // 32bit textures are actually stored as two 16bit textures in separate banks, allowing a bilinear |
| 54 | // sample of a 32bit texture at the same cost as a 16bit bilinear/trilinear sample. A trilinear |
| 55 | // sample of a 32bit texture costs more. |
| 56 | // |
| 57 | // TODO: I'm not sure if it's valid for a texture unit's even and odd banks to overlap. There might |
| 58 | // actually be a hard requirement for even and odd banks to live in different banks of TMEM. |
| 59 | // |
| 60 | // Note: This is still very much a heuristic. |
| 61 | // Actually knowing if a texture is partially or fully cached within TMEM would require |
| 62 | // extensive software rasterization, or sampler feedback from a hardware backend. |
| 63 | // |
| 64 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | namespace TMEM |
| 67 | { |
| 68 | struct TextureUnitState |
| 69 | { |
| 70 | enum class State |
| 71 | { |
| 72 | // Cache is invalid. Configuration has changed |
| 73 | INVALID, |
| 74 | |
| 75 | // Valid, but not cached due to either being too big, or overlapping with another texture unit |
| 76 | VALID, |
| 77 | |
| 78 | // Texture unit has cached all of the previous draw |
| 79 | CACHED, |
| 80 | }; |
| 81 | |
| 82 | struct BankConfig |
| 83 | { |
| 84 | u32 width = 0; |
| 85 | u32 height = 0; |
| 86 | u32 base = 0; |
| 87 | u32 size = 0; |
| 88 | bool Overlaps(const BankConfig& other) const; |
| 89 | }; |
| 90 | |
| 91 | BankConfig even = {}; |
| 92 | BankConfig odd = {}; |
| 93 | State state = State::INVALID; |
| 94 | |
| 95 | bool Overlaps(const TextureUnitState& other) const; |
| 96 | }; |
| 97 | |
| 98 | static u32 CalculateUnitSize(TextureUnitState::BankConfig bank_config); |
| 99 | |
| 100 | static std::array<TextureUnitState, 8> s_unit; |
| 101 | |
| 102 | // On TMEM configuration changed: |
| 103 | // 1. invalidate stage. |
| 104 | |
| 105 | void ConfigurationChanged(TexUnitAddress bp_addr, u32 config) |
| 106 | { |
| 107 | TextureUnitState& unit_state = s_unit[bp_addr.GetUnitID()]; |
| 108 | |
| 109 | // If anything has changed, we can't assume existing state is still valid. |
| 110 | unit_state.state = TextureUnitState::State::INVALID; |
| 111 | |
| 112 | // Note: BPStructs has already filtered out NOP changes before calling us |
| 113 | switch (bp_addr.Reg) |
| 114 | { |
| 115 | case TexUnitAddress::Register::SETIMAGE1: |
| 116 | { |
| 117 | // Image Type and Even bank's Cache Height, Cache Width, TMEM Offset |
| 118 | TexImage1 even = {.hex = config}; |
| 119 | unit_state.even = {even.cache_width, even.cache_height, even.tmem_even << 5, 0}; |
| 120 | break; |
| 121 | } |
| 122 | case TexUnitAddress::Register::SETIMAGE2: |
| 123 | { |
| 124 | // Odd bank's Cache Height, Cache Width, TMEM Offset |
| 125 | TexImage2 odd = {.hex = config}; |
| 126 | unit_state.odd = {odd.cache_width, odd.cache_height, odd.tmem_odd << 5, 0}; |
| 127 | break; |
| 128 | } |
| 129 | default: |
| 130 | // Something else has changed |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void InvalidateAll() |
| 136 | { |
| 137 | for (auto& unit : s_unit) |
| 138 | { |
| 139 | unit.state = TextureUnitState::State::INVALID; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // On invalidate cache: |
| 144 | // 1. invalidate all texture units. |
| 145 | |
| 146 | void Invalidate(u32 /* param */) |
| 147 | { |
| 148 | // The exact arguments of Invalidate commands is currently unknown. |
| 149 | // It appears to contain the TMEM address and a size. |
| 150 | |
| 151 | // For simplicity, we will just invalidate everything |
| 152 | InvalidateAll(); |
| 153 | } |
| 154 | |
| 155 | // On bind: |
| 156 | // 1. use mipmapping/32bit status to calculate final sizes |
| 157 | // 2. if texture size is small enough to fit in region mark as cached. |
| 158 | // otherwise, mark as valid |
| 159 | |
| 160 | void Bind(u32 unit, int width, int height, bool is_mipmapped, bool is_32_bit) |
| 161 | { |
| 162 | TextureUnitState& unit_state = s_unit[unit]; |
| 163 | |
| 164 | // All textures use the even bank. |
| 165 | // It holds the level 0 mipmap (and other even mipmap LODs, if mipmapping is enabled) |
| 166 | unit_state.even.size = CalculateUnitSize(unit_state.even); |
| 167 | |
| 168 | bool fits = (width * height * 32U) <= unit_state.even.size; |
| 169 | |
| 170 | if (is_mipmapped || is_32_bit) |
| 171 | { |
| 172 | // And the odd bank is enabled when either mipmapping is enabled or the texture is 32 bit |
| 173 | // It holds the Alpha and Red channels of 32 bit textures or the odd layers of a mipmapped |
| 174 | // texture |
| 175 | unit_state.odd.size = CalculateUnitSize(unit_state.odd); |
| 176 | |
| 177 | fits = fits && (width * height * 32U) <= unit_state.odd.size; |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | unit_state.odd.size = 0; |
| 182 | } |
| 183 | |
| 184 | if (is_mipmapped) |
| 185 | { |
| 186 | // TODO: This is what games appear to expect from hardware. But seems odd, as it doesn't line up |
| 187 | // with how much extra memory is required for mipmapping, just 33% more. |
| 188 | // Hardware testing is required to see exactly what gets used. |
| 189 | |
| 190 | // When mipmapping is enabled, the even bank is doubled in size |
| 191 | // The extended region holds the remaining even mipmap layers |
| 192 | unit_state.even.size *= 2; |
| 193 | |
| 194 | if (is_32_bit) |
| 195 | { |
| 196 | // When a 32bit texture is mipmapped, the odd bank is also doubled in size |
| 197 | unit_state.odd.size *= 2; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | unit_state.state = fits ? TextureUnitState::State::CACHED : TextureUnitState::State::VALID; |
| 202 | } |
| 203 | |
| 204 | static u32 CalculateUnitSize(TextureUnitState::BankConfig bank_config) |
| 205 | { |
| 206 | u32 width = bank_config.width; |
| 207 | u32 height = bank_config.height; |
| 208 | |
| 209 | // These are the only cache sizes supported by the sdk |
| 210 | if (width == height) |
| 211 | { |
| 212 | switch (width) |
| 213 | { |
| 214 | case 3: // 32KB |
| 215 | return 32 * 1024; |
| 216 | case 4: // 128KB |
| 217 | return 128 * 1024; |
| 218 | case 5: // 512KB |
| 219 | return 512 * 1024; |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // However, the registers allow a much larger amount of configurablity. |
| 226 | // Maybe other sizes are broken? |
| 227 | // Until hardware tests are done, this is a guess at the size algorithm |
| 228 | |
| 229 | return 512 * (1 << width) * (1 << height); |
| 230 | } |
| 231 | |
| 232 | bool TextureUnitState::BankConfig::Overlaps(const BankConfig& other) const |
| 233 | { |
| 234 | if (size == 0 || other.size == 0) |
| 235 | return false; |
| 236 | return (base <= other.base && (base + size) > other.base) || |
| 237 | (other.base <= base && (other.base + other.size) > base); |
| 238 | } |
| 239 | |
| 240 | bool TextureUnitState::Overlaps(const TextureUnitState& other) const |
| 241 | { |
| 242 | if (state == TextureUnitState::State::INVALID || other.state == TextureUnitState::State::INVALID) |
| 243 | return false; |
| 244 | return even.Overlaps(other.even) || even.Overlaps(other.odd) || odd.Overlaps(other.even) || |
| 245 | odd.Overlaps(other.odd); |
| 246 | } |
| 247 | |
| 248 | // Scans though active texture units checks for overlaps. |
| 249 | void FinalizeBinds(BitSet32 used_textures) |
| 250 | { |
| 251 | for (u32 i : used_textures) |
| 252 | { |
| 253 | if (s_unit[i].even.Overlaps(s_unit[i].odd)) |
| 254 | { // Self-overlap |
| 255 | s_unit[i].state = TextureUnitState::State::VALID; |
| 256 | } |
| 257 | for (size_t j = 0; j < s_unit.size(); j++) |
| 258 | { |
| 259 | if (j != i && s_unit[i].Overlaps(s_unit[j])) |
| 260 | { |
| 261 | // There is an overlap, downgrade both from CACHED |
| 262 | // (for there to be an overlap, both must have started as valid or cached) |
| 263 | s_unit[i].state = TextureUnitState::State::VALID; |
| 264 | s_unit[j].state = TextureUnitState::State::VALID; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | bool IsCached(u32 unit) |
| 271 | { |
| 272 | return s_unit[unit].state == TextureUnitState::State::CACHED; |
| 273 | } |
| 274 | |
| 275 | bool IsValid(u32 unit) |
| 276 | { |
| 277 | return s_unit[unit].state != TextureUnitState::State::INVALID; |
| 278 | } |
| 279 | |
| 280 | void Init() |
| 281 | { |
| 282 | s_unit.fill({}); |
| 283 | } |
| 284 | |
| 285 | void DoState(PointerWrap& p) |
| 286 | { |
| 287 | p.DoArray(s_unit); |
| 288 | } |
| 289 | |
| 290 | } // namespace TMEM |