VERY experimental Nintendo Switch 2 emulator
C++
video_core: Use un-shifted block sizes to avoid integer divisions
ReinUsesLisp1d10810d2b90
diff --git a/src/common/alignment.h b/src/common/alignment.hindex d94a2291f7..3379a69675 100644--- a/src/common/alignment.h+++ b/src/common/alignment.h@@ -19,6 +19,11 @@ constexpr T AlignDown(T value, std::size_t size) {return static_cast<T>(value - value % size);}+template <typename T>+constexpr T AlignBits(T value, T align) {+ return (value + ((1 << align) - 1)) >> align << align;+}+template <typename T>constexpr bool Is4KBAligned(T value) {static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");diff --git a/src/video_core/engines/fermi_2d.h b/src/video_core/engines/fermi_2d.hindex 45f59a4d99..3d28afa913 100644--- a/src/video_core/engines/fermi_2d.h+++ b/src/video_core/engines/fermi_2d.h@@ -63,18 +63,15 @@ public:}u32 BlockWidth() const {- // The block width is stored in log2 format.- return 1 << block_width;+ return block_width;}u32 BlockHeight() const {- // The block height is stored in log2 format.- return 1 << block_height;+ return block_height;}u32 BlockDepth() const {- // The block depth is stored in log2 format.- return 1 << block_depth;+ return block_depth;}};static_assert(sizeof(Surface) == 0x28, "Surface has incorrect size");diff --git a/src/video_core/engines/maxwell_dma.h b/src/video_core/engines/maxwell_dma.hindex e5942f671c..522fa97dc3 100644--- a/src/video_core/engines/maxwell_dma.h+++ b/src/video_core/engines/maxwell_dma.h@@ -59,11 +59,11 @@ public:};u32 BlockHeight() const {- return 1 << block_height;+ return block_height;}u32 BlockDepth() const {- return 1 << block_depth;+ return block_depth;}};diff --git a/src/video_core/texture_cache/surface_base.cpp b/src/video_core/texture_cache/surface_base.cppindex 36ca72b4a2..510d1aef5f 100644--- a/src/video_core/texture_cache/surface_base.cpp+++ b/src/video_core/texture_cache/surface_base.cpp@@ -22,7 +22,6 @@ SurfaceBaseImpl::SurfaceBaseImpl(GPUVAddr gpu_addr, const SurfaceParams& params): params{params}, mipmap_sizes(params.num_levels),mipmap_offsets(params.num_levels), gpu_addr{gpu_addr}, host_memory_size{params.GetHostSizeInBytes()} {-std::size_t offset = 0;for (u32 level = 0; level < params.num_levels; ++level) {const std::size_t mipmap_size{params.GetGuestMipmapSize(level)};@@ -75,7 +74,7 @@ void SurfaceBaseImpl::LoadBuffer(Tegra::MemoryManager& memory_manager,return;}if (params.is_tiled) {- ASSERT_MSG(params.block_width == 1, "Block width is defined as {} on texture target {}",+ ASSERT_MSG(params.block_width == 0, "Block width is defined as {} on texture target {}",params.block_width, static_cast<u32>(params.target));for (u32 level = 0; level < params.num_levels; ++level) {const std::size_t host_offset{params.GetHostMipmapLevelOffset(level)};diff --git a/src/video_core/texture_cache/surface_params.cpp b/src/video_core/texture_cache/surface_params.cppindex b537b26e2d..3a47f404dc 100644--- a/src/video_core/texture_cache/surface_params.cpp+++ b/src/video_core/texture_cache/surface_params.cpp@@ -96,9 +96,9 @@ SurfaceParams SurfaceParams::CreateForDepthBuffer(SurfaceParams params;params.is_tiled = type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;params.srgb_conversion = false;- params.block_width = 1 << std::min(block_width, 5U);- params.block_height = 1 << std::min(block_height, 5U);- params.block_depth = 1 << std::min(block_depth, 5U);+ params.block_width = std::min(block_width, 5U);+ params.block_height = std::min(block_height, 5U);+ params.block_depth = std::min(block_depth, 5U);params.tile_width_spacing = 1;params.pixel_format = PixelFormatFromDepthFormat(format);params.component_type = ComponentTypeFromDepthFormat(format);@@ -120,9 +120,9 @@ SurfaceParams SurfaceParams::CreateForFramebuffer(Core::System& system, std::sizconfig.memory_layout.type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;- params.block_width = 1 << config.memory_layout.block_width;- params.block_height = 1 << config.memory_layout.block_height;- params.block_depth = 1 << config.memory_layout.block_depth;+ params.block_width = config.memory_layout.block_width;+ params.block_height = config.memory_layout.block_height;+ params.block_depth = config.memory_layout.block_depth;params.tile_width_spacing = 1;params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);params.component_type = ComponentTypeFromRenderTarget(config.format);@@ -149,9 +149,9 @@ SurfaceParams SurfaceParams::CreateForFermiCopySurface(params.is_tiled = !config.linear;params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;- params.block_width = params.is_tiled ? std::min(config.BlockWidth(), 32U) : 0,- params.block_height = params.is_tiled ? std::min(config.BlockHeight(), 32U) : 0,- params.block_depth = params.is_tiled ? std::min(config.BlockDepth(), 32U) : 0,+ params.block_width = params.is_tiled ? std::min(config.BlockWidth(), 5U) : 0,+ params.block_height = params.is_tiled ? std::min(config.BlockHeight(), 5U) : 0,+ params.block_depth = params.is_tiled ? std::min(config.BlockDepth(), 5U) : 0,params.tile_width_spacing = 1;params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);params.component_type = ComponentTypeFromRenderTarget(config.format);@@ -190,9 +190,9 @@ u32 SurfaceParams::GetMipBlockHeight(u32 level) const {const u32 height{GetMipHeight(level)};const u32 default_block_height{GetDefaultBlockHeight()};const u32 blocks_in_y{(height + default_block_height - 1) / default_block_height};- u32 block_height = 16;- while (block_height > 1 && blocks_in_y <= block_height * 4) {- block_height >>= 1;+ u32 block_height = 4;+ while (block_height > 0 && blocks_in_y <= (1U << block_height) * 4) {+ --block_height;}return block_height;}@@ -202,17 +202,17 @@ u32 SurfaceParams::GetMipBlockDepth(u32 level) const {return this->block_depth;}if (is_layered) {- return 1;+ return 0;}const u32 depth{GetMipDepth(level)};- u32 block_depth = 32;- while (block_depth > 1 && depth * 2 <= block_depth) {- block_depth >>= 1;+ u32 block_depth = 5;+ while (block_depth > 0 && depth * 2 <= (1U << block_depth)) {+ --block_depth;}- if (block_depth == 32 && GetMipBlockHeight(level) >= 4) {- return 16;+ if (block_depth == 5 && GetMipBlockHeight(level) >= 2) {+ return 4;}return block_depth;@@ -252,7 +252,8 @@ std::size_t SurfaceParams::GetLayerSize(bool as_host_size, bool uncompressed) cosize += GetInnerMipmapMemorySize(level, as_host_size, uncompressed);}if (is_tiled && is_layered) {- return Common::AlignUp(size, Tegra::Texture::GetGOBSize() * block_height * block_depth);+ return Common::AlignBits(size,+ Tegra::Texture::GetGOBSizeShift() + block_height + block_depth);}return size;}diff --git a/src/video_core/texture_cache/surface_params.h b/src/video_core/texture_cache/surface_params.hindex e0ec1be0ee..7c48782c78 100644--- a/src/video_core/texture_cache/surface_params.h+++ b/src/video_core/texture_cache/surface_params.h@@ -54,12 +54,12 @@ public:constexpr std::size_t rgb8_bpp = 4ULL;// ASTC is uncompressed in software, in emulated as RGBA8host_size_in_bytes = 0;- for (std::size_t level = 0; level < num_levels; level++) {+ for (u32 level = 0; level < num_levels; ++level) {const std::size_t width =Common::AlignUp(GetMipWidth(level), GetDefaultBlockWidth());const std::size_t height =Common::AlignUp(GetMipHeight(level), GetDefaultBlockHeight());- const std::size_t depth = is_layered ? depth : GetMipDepth(level);+ const std::size_t depth = is_layered ? this->depth : GetMipDepth(level);host_size_in_bytes += width * height * depth * rgb8_bpp;}} else {@@ -96,7 +96,8 @@ public:// Helper used for out of class size calculationsstatic std::size_t AlignLayered(const std::size_t out_size, const u32 block_height,const u32 block_depth) {- return Common::AlignUp(out_size, Tegra::Texture::GetGOBSize() * block_height * block_depth);+ return Common::AlignBits(out_size,+ Tegra::Texture::GetGOBSizeShift() + block_height + block_depth);}/// Returns the offset in bytes in guest memory of a given mipmap level.diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.hindex 1c2b63dae6..f35d0c88fd 100644--- a/src/video_core/texture_cache/texture_cache.h+++ b/src/video_core/texture_cache/texture_cache.h@@ -81,6 +81,9 @@ public:if (!gpu_addr) {return {};}+ if (gpu_addr == 0x1b7ec0000) {+ // __debugbreak();+ }const auto params{SurfaceParams::CreateForTexture(system, config, entry)};return GetSurface(gpu_addr, params, true).second;}diff --git a/src/video_core/textures/decoders.cpp b/src/video_core/textures/decoders.cppindex 2178053865..f45fd175a6 100644--- a/src/video_core/textures/decoders.cpp+++ b/src/video_core/textures/decoders.cpp@@ -36,10 +36,16 @@ struct alignas(64) SwizzleTable {std::array<std::array<u16, M>, N> values{};};-constexpr u32 gob_size_x = 64;-constexpr u32 gob_size_y = 8;-constexpr u32 gob_size_z = 1;-constexpr u32 gob_size = gob_size_x * gob_size_y * gob_size_z;+constexpr u32 gob_size_x_shift = 6;+constexpr u32 gob_size_y_shift = 3;+constexpr u32 gob_size_z_shift = 0;+constexpr u32 gob_size_shift = gob_size_x_shift + gob_size_y_shift + gob_size_z_shift;++constexpr u32 gob_size_x = 1U << gob_size_x_shift;+constexpr u32 gob_size_y = 1U << gob_size_y_shift;+constexpr u32 gob_size_z = 1U << gob_size_z_shift;+constexpr u32 gob_size = 1U << gob_size_shift;+constexpr u32 fast_swizzle_align = 16;constexpr auto legacy_swizzle_table = SwizzleTable<gob_size_y, gob_size_x, gob_size_z>();@@ -171,14 +177,16 @@ void SwizzledData(u8* const swizzled_data, u8* const unswizzled_data, const boolvoid CopySwizzledData(u32 width, u32 height, u32 depth, u32 bytes_per_pixel,u32 out_bytes_per_pixel, u8* const swizzled_data, u8* const unswizzled_data,bool unswizzle, u32 block_height, u32 block_depth, u32 width_spacing) {+ const u32 block_height_size{1U << block_height};+ const u32 block_depth_size{1U << block_depth};if (bytes_per_pixel % 3 != 0 && (width * bytes_per_pixel) % fast_swizzle_align == 0) {SwizzledData<true>(swizzled_data, unswizzled_data, unswizzle, width, height, depth,- bytes_per_pixel, out_bytes_per_pixel, block_height, block_depth,- width_spacing);+ bytes_per_pixel, out_bytes_per_pixel, block_height_size,+ block_depth_size, width_spacing);} else {SwizzledData<false>(swizzled_data, unswizzled_data, unswizzle, width, height, depth,- bytes_per_pixel, out_bytes_per_pixel, block_height, block_depth,- width_spacing);+ bytes_per_pixel, out_bytes_per_pixel, block_height_size,+ block_depth_size, width_spacing);}}@@ -249,16 +257,18 @@ std::vector<u8> UnswizzleTexture(u8* address, u32 tile_size_x, u32 tile_size_y,void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width,u32 bytes_per_pixel, u8* swizzled_data, u8* unswizzled_data, u32 block_height) {+ const u32 block_height_size{1U << block_height};const u32 image_width_in_gobs{(swizzled_width * bytes_per_pixel + (gob_size_x - 1)) /gob_size_x};for (u32 line = 0; line < subrect_height; ++line) {const u32 gob_address_y =- (line / (gob_size_y * block_height)) * gob_size * block_height * image_width_in_gobs +- ((line % (gob_size_y * block_height)) / gob_size_y) * gob_size;+ (line / (gob_size_y * block_height_size)) * gob_size * block_height_size *+ image_width_in_gobs ++ ((line % (gob_size_y * block_height_size)) / gob_size_y) * gob_size;const auto& table = legacy_swizzle_table[line % gob_size_y];for (u32 x = 0; x < subrect_width; ++x) {const u32 gob_address =- gob_address_y + (x * bytes_per_pixel / gob_size_x) * gob_size * block_height;+ gob_address_y + (x * bytes_per_pixel / gob_size_x) * gob_size * block_height_size;const u32 swizzled_offset = gob_address + table[(x * bytes_per_pixel) % gob_size_x];u8* source_line = unswizzled_data + line * source_pitch + x * bytes_per_pixel;u8* dest_addr = swizzled_data + swizzled_offset;@@ -271,14 +281,17 @@ void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32void UnswizzleSubrect(u32 subrect_width, u32 subrect_height, u32 dest_pitch, u32 swizzled_width,u32 bytes_per_pixel, u8* swizzled_data, u8* unswizzled_data, u32 block_height,u32 offset_x, u32 offset_y) {+ const u32 block_height_size{1U << block_height};for (u32 line = 0; line < subrect_height; ++line) {const u32 y2 = line + offset_y;- const u32 gob_address_y = (y2 / (gob_size_y * block_height)) * gob_size * block_height +- ((y2 % (gob_size_y * block_height)) / gob_size_y) * gob_size;+ const u32 gob_address_y =+ (y2 / (gob_size_y * block_height_size)) * gob_size * block_height_size ++ ((y2 % (gob_size_y * block_height_size)) / gob_size_y) * gob_size;const auto& table = legacy_swizzle_table[y2 % gob_size_y];for (u32 x = 0; x < subrect_width; ++x) {const u32 x2 = (x + offset_x) * bytes_per_pixel;- const u32 gob_address = gob_address_y + (x2 / gob_size_x) * gob_size * block_height;+ const u32 gob_address =+ gob_address_y + (x2 / gob_size_x) * gob_size * block_height_size;const u32 swizzled_offset = gob_address + table[x2 % gob_size_x];u8* dest_line = unswizzled_data + line * dest_pitch + x * bytes_per_pixel;u8* source_addr = swizzled_data + swizzled_offset;@@ -291,16 +304,18 @@ void UnswizzleSubrect(u32 subrect_width, u32 subrect_height, u32 dest_pitch, u32void SwizzleKepler(const u32 width, const u32 height, const u32 dst_x, const u32 dst_y,const u32 block_height, const std::size_t copy_size, const u8* source_data,u8* swizzle_data) {+ const u32 block_height_size{1U << block_height};const u32 image_width_in_gobs{(width + gob_size_x - 1) / gob_size_x};std::size_t count = 0;for (std::size_t y = dst_y; y < height && count < copy_size; ++y) {const std::size_t gob_address_y =- (y / (gob_size_y * block_height)) * gob_size * block_height * image_width_in_gobs +- ((y % (gob_size_y * block_height)) / gob_size_y) * gob_size;+ (y / (gob_size_y * block_height_size)) * gob_size * block_height_size *+ image_width_in_gobs ++ ((y % (gob_size_y * block_height_size)) / gob_size_y) * gob_size;const auto& table = legacy_swizzle_table[y % gob_size_y];for (std::size_t x = dst_x; x < width && count < copy_size; ++x) {const std::size_t gob_address =- gob_address_y + (x / gob_size_x) * gob_size * block_height;+ gob_address_y + (x / gob_size_x) * gob_size * block_height_size;const std::size_t swizzled_offset = gob_address + table[x % gob_size_x];const u8* source_line = source_data + count;u8* dest_addr = swizzle_data + swizzled_offset;@@ -356,9 +371,9 @@ std::vector<u8> DecodeTexture(const std::vector<u8>& texture_data, TextureFormatstd::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth,u32 block_height, u32 block_depth) {if (tiled) {- const u32 aligned_width = Common::AlignUp(width * bytes_per_pixel, gob_size_x);- const u32 aligned_height = Common::AlignUp(height, gob_size_y * block_height);- const u32 aligned_depth = Common::AlignUp(depth, gob_size_z * block_depth);+ const u32 aligned_width = Common::AlignBits(width * bytes_per_pixel, gob_size_x_shift);+ const u32 aligned_height = Common::AlignBits(height, gob_size_y_shift + block_height);+ const u32 aligned_depth = Common::AlignBits(depth, gob_size_z_shift + block_depth);return aligned_width * aligned_height * aligned_depth;} else {return width * height * depth * bytes_per_pixel;diff --git a/src/video_core/textures/decoders.h b/src/video_core/textures/decoders.hindex e072d84017..eaec9b5a5b 100644--- a/src/video_core/textures/decoders.h+++ b/src/video_core/textures/decoders.h@@ -12,8 +12,8 @@ namespace Tegra::Texture {// GOBSize constant. Calculated by 64 bytes in x multiplied by 8 y coords, represents// an small rect of (64/bytes_per_pixel)X8.-inline std::size_t GetGOBSize() {- return 512;+inline std::size_t GetGOBSizeShift() {+ return 9;}/// Unswizzles a swizzled texture without changing its format.diff --git a/src/video_core/textures/texture.h b/src/video_core/textures/texture.hindex 219bfd5598..f22b4e7c77 100644--- a/src/video_core/textures/texture.h+++ b/src/video_core/textures/texture.h@@ -219,20 +219,17 @@ struct TICEntry {u32 BlockWidth() const {ASSERT(IsTiled());- // The block height is stored in log2 format.- return 1 << block_width;+ return block_width;}u32 BlockHeight() const {ASSERT(IsTiled());- // The block height is stored in log2 format.- return 1 << block_height;+ return block_height;}u32 BlockDepth() const {ASSERT(IsTiled());- // The block height is stored in log2 format.- return 1 << block_depth;+ return block_depth;}bool IsTiled() const {