2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using static Ryujinx.Graphics.Texture.BlockLinearConstants;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Texture
|
|
|
|
{
|
|
|
|
public static class SizeCalculator
|
|
|
|
{
|
|
|
|
private const int StrideAlignment = 32;
|
|
|
|
|
|
|
|
public static SizeInfo GetBlockLinearTextureSize(
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int depth,
|
|
|
|
int levels,
|
|
|
|
int layers,
|
|
|
|
int blockWidth,
|
|
|
|
int blockHeight,
|
|
|
|
int bytesPerPixel,
|
|
|
|
int gobBlocksInY,
|
|
|
|
int gobBlocksInZ,
|
2021-01-15 13:14:00 -05:00
|
|
|
int gobBlocksInTileX,
|
|
|
|
int gpuLayerSize = 0)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-10-30 19:45:01 -04:00
|
|
|
bool is3D = depth > 1;
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
int layerSize = 0;
|
|
|
|
|
2019-10-30 19:45:01 -04:00
|
|
|
int[] allOffsets = new int[levels * layers * depth];
|
2019-10-13 02:02:07 -04:00
|
|
|
int[] mipOffsets = new int[levels];
|
|
|
|
|
|
|
|
int mipGobBlocksInY = gobBlocksInY;
|
|
|
|
int mipGobBlocksInZ = gobBlocksInZ;
|
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX;
|
|
|
|
int gobHeight = gobBlocksInY * GobHeight;
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
for (int level = 0; level < levels; level++)
|
|
|
|
{
|
|
|
|
int w = Math.Max(1, width >> level);
|
|
|
|
int h = Math.Max(1, height >> level);
|
|
|
|
int d = Math.Max(1, depth >> level);
|
|
|
|
|
|
|
|
w = BitUtils.DivRoundUp(w, blockWidth);
|
|
|
|
h = BitUtils.DivRoundUp(h, blockHeight);
|
|
|
|
|
|
|
|
while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1)
|
|
|
|
{
|
|
|
|
mipGobBlocksInY >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1)
|
|
|
|
{
|
|
|
|
mipGobBlocksInZ >>= 1;
|
|
|
|
}
|
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
int widthInGobs = BitUtils.DivRoundUp(w * bytesPerPixel, GobStride);
|
|
|
|
|
|
|
|
int alignment = gobBlocksInTileX;
|
|
|
|
|
|
|
|
if (d < gobBlocksInZ || w <= gobWidth || h <= gobHeight)
|
|
|
|
{
|
|
|
|
alignment = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
widthInGobs = BitUtils.AlignUp(widthInGobs, alignment);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
int totalBlocksOfGobsInZ = BitUtils.DivRoundUp(d, mipGobBlocksInZ);
|
|
|
|
int totalBlocksOfGobsInY = BitUtils.DivRoundUp(BitUtils.DivRoundUp(h, GobHeight), mipGobBlocksInY);
|
|
|
|
|
|
|
|
int robSize = widthInGobs * mipGobBlocksInY * mipGobBlocksInZ * GobSize;
|
|
|
|
|
2019-10-30 19:45:01 -04:00
|
|
|
if (is3D)
|
|
|
|
{
|
|
|
|
int gobSize = mipGobBlocksInY * GobSize;
|
|
|
|
|
|
|
|
int sliceSize = totalBlocksOfGobsInY * widthInGobs * gobSize;
|
|
|
|
|
|
|
|
int baseOffset = layerSize;
|
|
|
|
|
|
|
|
int mask = gobBlocksInZ - 1;
|
|
|
|
|
|
|
|
for (int z = 0; z < d; z++)
|
|
|
|
{
|
|
|
|
int zLow = z & mask;
|
|
|
|
int zHigh = z & ~mask;
|
|
|
|
|
|
|
|
allOffsets[z * levels + level] = baseOffset + zLow * gobSize + zHigh * sliceSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:02:07 -04:00
|
|
|
mipOffsets[level] = layerSize;
|
|
|
|
|
|
|
|
layerSize += totalBlocksOfGobsInZ * totalBlocksOfGobsInY * robSize;
|
|
|
|
}
|
|
|
|
|
2021-01-15 13:14:00 -05:00
|
|
|
if (layers > 1)
|
|
|
|
{
|
|
|
|
layerSize = AlignLayerSize(
|
|
|
|
layerSize,
|
|
|
|
height,
|
|
|
|
depth,
|
|
|
|
blockHeight,
|
|
|
|
gobBlocksInY,
|
|
|
|
gobBlocksInZ,
|
|
|
|
gobBlocksInTileX);
|
|
|
|
}
|
|
|
|
|
|
|
|
int totalSize;
|
|
|
|
|
|
|
|
if (layerSize < gpuLayerSize)
|
|
|
|
{
|
|
|
|
totalSize = (layers - 1) * gpuLayerSize + layerSize;
|
|
|
|
layerSize = gpuLayerSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalSize = layerSize * layers;
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-10-30 19:45:01 -04:00
|
|
|
if (!is3D)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-10-30 19:45:01 -04:00
|
|
|
for (int layer = 0; layer < layers; layer++)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2019-10-30 19:45:01 -04:00
|
|
|
int baseIndex = layer * levels;
|
|
|
|
int baseOffset = layer * layerSize;
|
|
|
|
|
|
|
|
for (int level = 0; level < levels; level++)
|
|
|
|
{
|
|
|
|
allOffsets[baseIndex + level] = baseOffset + mipOffsets[level];
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SizeInfo(mipOffsets, allOffsets, levels, layerSize, totalSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SizeInfo GetLinearTextureSize(int stride, int height, int blockHeight)
|
|
|
|
{
|
|
|
|
// Non-2D or mipmapped linear textures are not supported by the Switch GPU,
|
|
|
|
// so we only need to handle a single case (2D textures without mipmaps).
|
|
|
|
int totalSize = stride * BitUtils.DivRoundUp(height, blockHeight);
|
|
|
|
|
|
|
|
return new SizeInfo(new int[] { 0 }, new int[] { 0 }, 1, totalSize, totalSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int AlignLayerSize(
|
|
|
|
int size,
|
|
|
|
int height,
|
|
|
|
int depth,
|
|
|
|
int blockHeight,
|
|
|
|
int gobBlocksInY,
|
2020-04-25 09:40:20 -04:00
|
|
|
int gobBlocksInZ,
|
|
|
|
int gobBlocksInTileX)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-04-25 09:40:20 -04:00
|
|
|
if (gobBlocksInTileX < 2)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-04-25 09:40:20 -04:00
|
|
|
height = BitUtils.DivRoundUp(height, blockHeight);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
while (height <= (gobBlocksInY >> 1) * GobHeight && gobBlocksInY != 1)
|
|
|
|
{
|
|
|
|
gobBlocksInY >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (depth <= (gobBlocksInZ >> 1) && gobBlocksInZ != 1)
|
|
|
|
{
|
|
|
|
gobBlocksInZ >>= 1;
|
|
|
|
}
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
int blockOfGobsSize = gobBlocksInY * gobBlocksInZ * GobSize;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
int sizeInBlockOfGobs = size / blockOfGobsSize;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
if (size != sizeInBlockOfGobs * blockOfGobsSize)
|
|
|
|
{
|
|
|
|
size = (sizeInBlockOfGobs + 1) * blockOfGobsSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-04-25 09:40:20 -04:00
|
|
|
int alignment = (gobBlocksInTileX * GobSize) * gobBlocksInY * gobBlocksInZ;
|
|
|
|
|
|
|
|
size = BitUtils.AlignUp(size, alignment);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Size GetBlockLinearAlignedSize(
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int depth,
|
|
|
|
int blockWidth,
|
|
|
|
int blockHeight,
|
|
|
|
int bytesPerPixel,
|
|
|
|
int gobBlocksInY,
|
|
|
|
int gobBlocksInZ,
|
|
|
|
int gobBlocksInTileX)
|
|
|
|
{
|
|
|
|
width = BitUtils.DivRoundUp(width, blockWidth);
|
|
|
|
height = BitUtils.DivRoundUp(height, blockHeight);
|
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX;
|
|
|
|
int gobHeight = gobBlocksInY * GobHeight;
|
|
|
|
|
|
|
|
int alignment = gobWidth;
|
|
|
|
|
|
|
|
if (depth < gobBlocksInZ || width <= gobWidth || height <= gobHeight)
|
|
|
|
{
|
|
|
|
alignment = GobStride / bytesPerPixel;
|
|
|
|
}
|
|
|
|
|
2020-11-20 11:30:59 -05:00
|
|
|
// Height has already been divided by block height, so pass it as 1.
|
|
|
|
(gobBlocksInY, gobBlocksInZ) = GetMipGobBlockSizes(height, depth, 1, gobBlocksInY, gobBlocksInZ);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
int blockOfGobsHeight = gobBlocksInY * GobHeight;
|
|
|
|
int blockOfGobsDepth = gobBlocksInZ;
|
|
|
|
|
2020-04-25 09:40:20 -04:00
|
|
|
width = BitUtils.AlignUp(width, alignment);
|
2019-10-13 02:02:07 -04:00
|
|
|
height = BitUtils.AlignUp(height, blockOfGobsHeight);
|
|
|
|
depth = BitUtils.AlignUp(depth, blockOfGobsDepth);
|
|
|
|
|
|
|
|
return new Size(width, height, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Size GetLinearAlignedSize(
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int blockWidth,
|
|
|
|
int blockHeight,
|
|
|
|
int bytesPerPixel)
|
|
|
|
{
|
|
|
|
width = BitUtils.DivRoundUp(width, blockWidth);
|
|
|
|
height = BitUtils.DivRoundUp(height, blockHeight);
|
|
|
|
|
|
|
|
int widthAlignment = StrideAlignment / bytesPerPixel;
|
|
|
|
|
|
|
|
width = BitUtils.AlignUp(width, widthAlignment);
|
|
|
|
|
|
|
|
return new Size(width, height, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static (int, int) GetMipGobBlockSizes(
|
|
|
|
int height,
|
|
|
|
int depth,
|
|
|
|
int blockHeight,
|
|
|
|
int gobBlocksInY,
|
|
|
|
int gobBlocksInZ)
|
|
|
|
{
|
|
|
|
height = BitUtils.DivRoundUp(height, blockHeight);
|
|
|
|
|
|
|
|
while (height <= (gobBlocksInY >> 1) * GobHeight && gobBlocksInY != 1)
|
|
|
|
{
|
|
|
|
gobBlocksInY >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (depth <= (gobBlocksInZ >> 1) && gobBlocksInZ != 1)
|
|
|
|
{
|
|
|
|
gobBlocksInZ >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (gobBlocksInY, gobBlocksInZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|