| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/TextureInfo.h" |
| 5 | |
| 6 | #include <span> |
| 7 | |
| 8 | #include <fmt/format.h> |
| 9 | #include <xxhash.h> |
| 10 | |
| 11 | #include "Common/Align.h" |
| 12 | #include "Common/Assert.h" |
| 13 | #include "Common/Logging/Log.h" |
| 14 | #include "Common/SpanUtils.h" |
| 15 | #include "Core/HW/Memmap.h" |
| 16 | #include "Core/System.h" |
| 17 | #include "VideoCommon/BPMemory.h" |
| 18 | #include "VideoCommon/TextureDecoder.h" |
| 19 | |
| 20 | TextureInfo TextureInfo::FromStage(u32 stage) |
| 21 | { |
| 22 | const auto tex = bpmem.tex.GetUnit(stage); |
| 23 | |
| 24 | const auto texture_format = tex.texImage0.format; |
| 25 | const auto tlut_format = tex.texTlut.tlut_format; |
| 26 | |
| 27 | const auto width = tex.texImage0.width + 1; |
| 28 | const auto height = tex.texImage0.height + 1; |
| 29 | |
| 30 | const u32 address = (tex.texImage3.image_base /* & 0x1FFFFF*/) << 5; |
| 31 | |
| 32 | const u32 tlutaddr = tex.texTlut.tmem_offset << 9; |
| 33 | std::span<const u8> tlut_data = TexDecoder_GetTmemSpan(tlutaddr); |
| 34 | |
| 35 | std::optional<u32> mip_count; |
| 36 | const bool has_mipmaps = tex.texMode0.mipmap_filter != MipMode::None; |
| 37 | if (has_mipmaps) |
| 38 | { |
| 39 | mip_count = (tex.texMode1.max_lod + 0xf) / 0x10; |
| 40 | } |
| 41 | |
| 42 | const bool from_tmem = tex.texImage1.cache_manually_managed != 0; |
| 43 | const u32 tmem_address_even = from_tmem ? tex.texImage1.tmem_even * TMEM_LINE_SIZE : 0; |
| 44 | const u32 tmem_address_odd = from_tmem ? tex.texImage2.tmem_odd * TMEM_LINE_SIZE : 0; |
| 45 | |
| 46 | if (from_tmem) |
| 47 | { |
| 48 | return TextureInfo(stage, TexDecoder_GetTmemSpan(tmem_address_even), tlut_data, address, |
| 49 | texture_format, tlut_format, width, height, true, |
| 50 | TexDecoder_GetTmemSpan(tmem_address_odd), |
| 51 | TexDecoder_GetTmemSpan(tmem_address_even), mip_count); |
| 52 | } |
| 53 | |
| 54 | auto& system = Core::System::GetInstance(); |
| 55 | auto& memory = system.GetMemory(); |
| 56 | return TextureInfo(stage, memory.GetSpanForAddress(address), tlut_data, address, texture_format, |
| 57 | tlut_format, width, height, false, {}, {}, mip_count); |
| 58 | } |
| 59 | |
| 60 | TextureInfo::TextureInfo(u32 stage, std::span<const u8> data, std::span<const u8> tlut_data, |
| 61 | u32 address, TextureFormat texture_format, TLUTFormat tlut_format, |
| 62 | u32 width, u32 height, bool from_tmem, std::span<const u8> tmem_odd, |
| 63 | std::span<const u8> tmem_even, std::optional<u32> mip_count) |
| 64 | : m_data(data), m_tlut_data(tlut_data), m_address(address), m_from_tmem(from_tmem), |
| 65 | m_tmem_even(tmem_even), m_tmem_odd(tmem_odd), m_texture_format(texture_format), |
| 66 | m_tlut_format(tlut_format), m_raw_width(width), m_raw_height(height), m_stage(stage) |
| 67 | { |
| 68 | const bool is_palette_texture = IsColorIndexed(m_texture_format); |
| 69 | if (is_palette_texture) |
| 70 | m_palette_size = TexDecoder_GetPaletteSize(m_texture_format); |
| 71 | |
| 72 | // TexelSizeInNibbles(format) * width * height / 16; |
| 73 | m_block_width = TexDecoder_GetBlockWidthInTexels(m_texture_format); |
| 74 | m_block_height = TexDecoder_GetBlockHeightInTexels(m_texture_format); |
| 75 | |
| 76 | m_expanded_width = Common::AlignUp(m_raw_width, m_block_width); |
| 77 | m_expanded_height = Common::AlignUp(m_raw_height, m_block_height); |
| 78 | |
| 79 | m_texture_size = |
| 80 | TexDecoder_GetTextureSizeInBytes(m_expanded_width, m_expanded_height, m_texture_format); |
| 81 | |
| 82 | if (data.size() < m_texture_size) |
| 83 | { |
| 84 | ERROR_LOG_FMT(VIDEO, "Trying to use an invalid texture address {:#010x}", GetRawAddress()); |
| 85 | m_data_valid = false; |
| 86 | } |
| 87 | else if (m_palette_size && tlut_data.size() < *m_palette_size) |
| 88 | { |
| 89 | ERROR_LOG_FMT(VIDEO, "Trying to use an invalid TLUT address {:#010x}", GetRawAddress()); |
| 90 | m_data_valid = false; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | m_data_valid = true; |
| 95 | } |
| 96 | |
| 97 | if (mip_count) |
| 98 | { |
| 99 | m_mipmaps_enabled = true; |
| 100 | const u32 raw_mip_count = *mip_count; |
| 101 | |
| 102 | // GPUs don't like when the specified mipmap count would require more than one 1x1-sized LOD in |
| 103 | // the mipmap chain |
| 104 | // e.g. 64x64 with 7 LODs would have the mipmap chain 64x64,32x32,16x16,8x8,4x4,2x2,1x1,0x0, so |
| 105 | // we limit the mipmap count to 6 there |
| 106 | m_limited_mip_count = |
| 107 | std::min<u32>(MathUtil::IntLog2(std::max(width, height)) + 1, raw_mip_count + 1) - 1; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | std::string TextureInfo::NameDetails::GetFullName() const |
| 112 | { |
| 113 | return fmt::format("{}_{}{}_{}", base_name, texture_name, tlut_name, format_name); |
| 114 | } |
| 115 | |
| 116 | TextureInfo::NameDetails TextureInfo::CalculateTextureName() const |
| 117 | { |
| 118 | if (!IsDataValid()) |
| 119 | return NameDetails{}; |
| 120 | |
| 121 | const u8* tlut = m_tlut_data.data(); |
| 122 | size_t tlut_size = m_palette_size ? *m_palette_size : 0; |
| 123 | |
| 124 | // checking for min/max on paletted textures |
| 125 | u32 min = 0xffff; |
| 126 | u32 max = 0; |
| 127 | switch (tlut_size) |
| 128 | { |
| 129 | case 0: |
| 130 | break; |
| 131 | case 16 * 2: |
| 132 | for (size_t i = 0; i < m_texture_size; i++) |
| 133 | { |
| 134 | const u32 low_nibble = m_data[i] & 0xf; |
| 135 | const u32 high_nibble = m_data[i] >> 4; |
| 136 | |
| 137 | min = std::min({min, low_nibble, high_nibble}); |
| 138 | max = std::max({max, low_nibble, high_nibble}); |
| 139 | } |
| 140 | break; |
| 141 | case 256 * 2: |
| 142 | for (size_t i = 0; i < m_texture_size; i++) |
| 143 | { |
| 144 | const u32 texture_byte = m_data[i]; |
| 145 | |
| 146 | min = std::min(min, texture_byte); |
| 147 | max = std::max(max, texture_byte); |
| 148 | } |
| 149 | break; |
| 150 | case 16384 * 2: |
| 151 | for (size_t i = 0; i < m_texture_size; i += sizeof(u16)) |
| 152 | { |
| 153 | const u32 texture_halfword = Common::swap16(m_data[i]) & 0x3fff; |
| 154 | |
| 155 | min = std::min(min, texture_halfword); |
| 156 | max = std::max(max, texture_halfword); |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | if (tlut_size > 0) |
| 161 | { |
| 162 | tlut_size = 2 * (max + 1 - min); |
| 163 | tlut += 2 * min; |
| 164 | } |
| 165 | |
| 166 | DEBUG_ASSERT(tlut_size <= m_palette_size.value_or(0)); |
| 167 | |
| 168 | const u64 tex_hash = XXH64(m_data.data(), m_texture_size, 0); |
| 169 | const u64 tlut_hash = tlut_size ? XXH64(tlut, tlut_size, 0) : 0; |
| 170 | |
| 171 | return {.base_name = fmt::format("{}{}x{}{}", format_prefix, m_raw_width, m_raw_height, |
| 172 | m_mipmaps_enabled ? "_m" : ""), |
| 173 | .texture_name = fmt::format("{:016x}", tex_hash), |
| 174 | .tlut_name = tlut_size ? fmt::format("_{:016x}", tlut_hash) : "", |
| 175 | .format_name = fmt::to_string(static_cast<int>(m_texture_format))}; |
| 176 | } |
| 177 | |
| 178 | TextureInfo::MipLevels TextureInfo::GetMipMapLevels() const |
| 179 | { |
| 180 | MipLevelIterator begin; |
| 181 | begin.m_parent = this; |
| 182 | begin.m_from_tmem = m_from_tmem; |
| 183 | begin.m_data = Common::SafeSubspan(m_data, GetTextureSize()); |
| 184 | begin.m_tmem_even = Common::SafeSubspan(m_tmem_even, GetTextureSize()); |
| 185 | begin.m_tmem_odd = m_tmem_odd; |
| 186 | begin.CreateMipLevel(); |
| 187 | |
| 188 | MipLevelIterator end; |
| 189 | end.m_level_index = m_limited_mip_count; |
| 190 | |
| 191 | return MipLevels(begin, end); |
| 192 | } |
| 193 | |
| 194 | u32 TextureInfo::GetFullLevelSize() const |
| 195 | { |
| 196 | u32 all_mips_size = 0; |
| 197 | for (const auto& mip_map : GetMipMapLevels()) |
| 198 | { |
| 199 | if (mip_map.IsDataValid()) |
| 200 | all_mips_size += mip_map.GetTextureSize(); |
| 201 | } |
| 202 | return m_texture_size + all_mips_size; |
| 203 | } |
| 204 | |
| 205 | TextureInfo::MipLevel::MipLevel(u32 level, const TextureInfo& parent, std::span<const u8>* data) |
| 206 | : m_level(level) |
| 207 | { |
| 208 | m_raw_width = std::max(parent.GetRawWidth() >> level, 1u); |
| 209 | m_raw_height = std::max(parent.GetRawHeight() >> level, 1u); |
| 210 | m_expanded_width = Common::AlignUp(m_raw_width, parent.GetBlockWidth()); |
| 211 | m_expanded_height = Common::AlignUp(m_raw_height, parent.GetBlockHeight()); |
| 212 | |
| 213 | m_texture_size = TexDecoder_GetTextureSizeInBytes(m_expanded_width, m_expanded_height, |
| 214 | parent.GetTextureFormat()); |
| 215 | |
| 216 | m_ptr = data->data(); |
| 217 | m_data_valid = data->size() >= m_texture_size; |
| 218 | |
| 219 | *data = Common::SafeSubspan(*data, m_texture_size); |
| 220 | } |
| 221 | |
| 222 | TextureInfo::MipLevelIterator& TextureInfo::MipLevelIterator::operator++() |
| 223 | { |
| 224 | ++m_level_index; |
| 225 | CreateMipLevel(); |
| 226 | return *this; |
| 227 | } |
| 228 | |
| 229 | void TextureInfo::MipLevelIterator::CreateMipLevel() |
| 230 | { |
| 231 | const u32 level = m_level_index + 1; |
| 232 | std::span<const u8>* data = m_from_tmem ? ((level % 2) ? &m_tmem_odd : &m_tmem_even) : &m_data; |
| 233 | |
| 234 | // The MipLevel constructor mutates the data argument so the next MipLevel gets the right data |
| 235 | m_mip_level = MipLevel(level, *m_parent, data); |
| 236 | } |