From bea1fc2e8d40ec792964852f57e7b884dfbd8306 Mon Sep 17 00:00:00 2001 From: riperiperi Date: Sat, 13 Jun 2020 23:31:06 +0100 Subject: Optimize texture format conversion, and MethodCopyBuffer (#1274) * Improve performance when converting texture formats. Still more work to do. * Speed up buffer -> texture copies. No longer copies byte by byte. Fast path when formats are identical. * Fix a few things, 64 byte block fast copy. * Spacing cleanup, unrelated change. * Fix base offset calculation for region copies. * Fix Linear -> BlockLinear * Fix some nits. (part 1 of review feedback) * Use a generic version of the Convert* functions rather than lambdas. This is some real monkey's paw shit. * Remove unnecessary span constructor. * Revert "Use a generic version of the Convert* functions rather than lambdas." This reverts commit aa43dcfbe8bba291eea4e10c68569af7a56a5851. * Fix bug with rectangle destination writing, better rectangle calculation for linear textures. --- Ryujinx.Graphics.Texture/BlockLinearLayout.cs | 94 +++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'Ryujinx.Graphics.Texture/BlockLinearLayout.cs') diff --git a/Ryujinx.Graphics.Texture/BlockLinearLayout.cs b/Ryujinx.Graphics.Texture/BlockLinearLayout.cs index b95db702..0b112242 100644 --- a/Ryujinx.Graphics.Texture/BlockLinearLayout.cs +++ b/Ryujinx.Graphics.Texture/BlockLinearLayout.cs @@ -33,6 +33,11 @@ namespace Ryujinx.Graphics.Texture private int _robSize; private int _sliceSize; + // Variables for built in iteration. + private int _yPart; + private int _yzPart; + private int _zPart; + public BlockLinearLayout( int width, int height, @@ -97,5 +102,94 @@ namespace Ryujinx.Graphics.Texture return offset; } + + public (int offset, int size) GetRectangleRange(int x, int y, int width, int height) + { + // Justification: + // The 2D offset is a combination of separate x and y parts. + // Both components increase with input and never overlap bits. + // Therefore for each component, the minimum input value is the lowest that component can go. + // Minimum total value is minimum X component + minimum Y component. Similar goes for maximum. + + int start = GetOffset(x, y, 0); + int end = GetOffset(x + width - 1, y + height - 1, 0) + _texBpp; // Cover the last pixel. + return (start, end - start); + } + + public bool LayoutMatches(BlockLinearLayout other) + { + return _robSize == other._robSize && + _sliceSize == other._sliceSize && + _texBpp == other._texBpp && + _bhMask == other._bhMask && + _bdMask == other._bdMask; + } + + // Functions for built in iteration. + // Components of the offset can be updated separately, and combined to save some time. + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetY(int y) + { + int yh = y / GobHeight; + int offset = (yh >> _bhShift) * _robSize; + + offset += (yh & _bhMask) * GobSize; + + offset += ((y & 0x07) >> 1) << 6; + offset += ((y & 0x01) >> 0) << 4; + + _yPart = offset; + _yzPart = offset + _zPart; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetZ(int z) + { + int offset = (z >> _bdShift) * _sliceSize; + + offset += ((z & _bdMask) * GobSize) << _bhShift; + + _zPart = offset; + _yzPart = offset + _yPart; + } + + /// + /// Optimized conversion for line offset in bytes to an absolute offset. Input x must be divisible by 16. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int GetOffsetWithLineOffset16(int x) + { + int offset = (x / GobStride) << _xShift; + + offset += ((x & 0x3f) >> 5) << 8; + offset += ((x & 0x1f) >> 4) << 5; + + return offset + _yzPart; + } + + /// + /// Optimized conversion for line offset in bytes to an absolute offset. Input x must be divisible by 64. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int GetOffsetWithLineOffset64(int x) + { + int offset = (x / GobStride) << _xShift; + + return offset + _yzPart; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int GetOffset(int x) + { + x <<= _bppShift; + int offset = (x / GobStride) << _xShift; + + offset += ((x & 0x3f) >> 5) << 8; + offset += ((x & 0x1f) >> 4) << 5; + offset += (x & 0x0f); + + return offset + _yzPart; + } } } \ No newline at end of file -- cgit v1.2.3