using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Texture;
using System;
namespace Ryujinx.Graphics.Gpu.Image
{
///
/// Texture information.
///
readonly struct TextureInfo
{
///
/// Address of the texture in GPU mapped memory.
///
public ulong GpuAddress { get; }
///
/// The width of the texture.
///
public int Width { get; }
///
/// The height of the texture, or layers count for 1D array textures.
///
public int Height { get; }
///
/// The depth of the texture (for 3D textures), or layers count for array textures.
///
public int DepthOrLayers { get; }
///
/// The number of mipmap levels of the texture.
///
public int Levels { get; }
///
/// The number of samples in the X direction for multisampled textures.
///
public int SamplesInX { get; }
///
/// The number of samples in the Y direction for multisampled textures.
///
public int SamplesInY { get; }
///
/// The number of bytes per line for linear textures.
///
public int Stride { get; }
///
/// Indicates whenever or not the texture is a linear texture.
///
public bool IsLinear { get; }
///
/// GOB blocks in the Y direction, for block linear textures.
///
public int GobBlocksInY { get; }
///
/// GOB blocks in the Z direction, for block linear textures.
///
public int GobBlocksInZ { get; }
///
/// Number of GOB blocks per tile in the X direction, for block linear textures.
///
public int GobBlocksInTileX { get; }
///
/// Total number of samples for multisampled textures.
///
public int Samples => SamplesInX * SamplesInY;
///
/// Texture target type.
///
public Target Target { get; }
///
/// Texture format information.
///
public FormatInfo FormatInfo { get; }
///
/// Depth-stencil mode of the texture. This defines whenever the depth or stencil value is read from shaders,
/// for depth-stencil texture formats.
///
public DepthStencilMode DepthStencilMode { get; }
///
/// Texture swizzle for the red color channel.
///
public SwizzleComponent SwizzleR { get; }
///
/// Texture swizzle for the green color channel.
///
public SwizzleComponent SwizzleG { get; }
///
/// Texture swizzle for the blue color channel.
///
public SwizzleComponent SwizzleB { get; }
///
/// Texture swizzle for the alpha color channel.
///
public SwizzleComponent SwizzleA { get; }
///
/// Constructs the texture information structure.
///
/// The GPU address of the texture
/// The width of the texture
/// The height or the texture
/// The depth or layers count of the texture
/// The amount of mipmap levels of the texture
/// The number of samples in the X direction for multisample textures, should be 1 otherwise
/// The number of samples in the Y direction for multisample textures, should be 1 otherwise
/// The stride for linear textures
/// Whenever the texture is linear or block linear
/// Number of GOB blocks in the Y direction
/// Number of GOB blocks in the Z direction
/// Number of GOB blocks per tile in the X direction
/// Texture target type
/// Texture format information
/// Depth-stencil mode
/// Swizzle for the red color channel
/// Swizzle for the green color channel
/// Swizzle for the blue color channel
/// Swizzle for the alpha color channel
public TextureInfo(
ulong gpuAddress,
int width,
int height,
int depthOrLayers,
int levels,
int samplesInX,
int samplesInY,
int stride,
bool isLinear,
int gobBlocksInY,
int gobBlocksInZ,
int gobBlocksInTileX,
Target target,
FormatInfo formatInfo,
DepthStencilMode depthStencilMode = DepthStencilMode.Depth,
SwizzleComponent swizzleR = SwizzleComponent.Red,
SwizzleComponent swizzleG = SwizzleComponent.Green,
SwizzleComponent swizzleB = SwizzleComponent.Blue,
SwizzleComponent swizzleA = SwizzleComponent.Alpha)
{
GpuAddress = gpuAddress;
Width = width;
Height = height;
DepthOrLayers = depthOrLayers;
Levels = levels;
SamplesInX = samplesInX;
SamplesInY = samplesInY;
Stride = stride;
IsLinear = isLinear;
GobBlocksInY = gobBlocksInY;
GobBlocksInZ = gobBlocksInZ;
GobBlocksInTileX = gobBlocksInTileX;
Target = target;
FormatInfo = formatInfo;
DepthStencilMode = depthStencilMode;
SwizzleR = swizzleR;
SwizzleG = swizzleG;
SwizzleB = swizzleB;
SwizzleA = swizzleA;
}
///
/// Gets the real texture depth.
/// Returns 1 for any target other than 3D textures.
///
/// Texture depth
public int GetDepth()
{
return GetDepth(Target, DepthOrLayers);
}
///
/// Gets the real texture depth.
/// Returns 1 for any target other than 3D textures.
///
/// Texture target
/// Texture depth if the texture is 3D, otherwise ignored
/// Texture depth
public static int GetDepth(Target target, int depthOrLayers)
{
return target == Target.Texture3D ? depthOrLayers : 1;
}
///
/// Gets the number of layers of the texture.
/// Returns 1 for non-array textures, 6 for cubemap textures, and layer faces for cubemap array textures.
///
/// The number of texture layers
public int GetLayers()
{
return GetLayers(Target, DepthOrLayers);
}
///
/// Gets the number of layers of the texture.
/// Returns 1 for non-array textures, 6 for cubemap textures, and layer faces for cubemap array textures.
///
/// Texture target
/// Texture layers if the is a array texture, ignored otherwise
/// The number of texture layers
public static int GetLayers(Target target, int depthOrLayers)
{
if (target == Target.Texture2DArray || target == Target.Texture2DMultisampleArray)
{
return depthOrLayers;
}
else if (target == Target.CubemapArray)
{
return depthOrLayers * 6;
}
else if (target == Target.Cubemap)
{
return 6;
}
else
{
return 1;
}
}
///
/// Gets the number of 2D slices of the texture.
/// Returns 6 for cubemap textures, layer faces for cubemap array textures, and DepthOrLayers for everything else.
///
/// The number of texture slices
public int GetSlices()
{
if (Target == Target.Texture3D || Target == Target.Texture2DArray || Target == Target.Texture2DMultisampleArray)
{
return DepthOrLayers;
}
else if (Target == Target.CubemapArray)
{
return DepthOrLayers * 6;
}
else if (Target == Target.Cubemap)
{
return 6;
}
else
{
return 1;
}
}
///
/// Calculates the size information from the texture information.
///
/// Optional size of each texture layer in bytes
/// Texture size information
public SizeInfo CalculateSizeInfo(int layerSize = 0)
{
if (Target == Target.TextureBuffer)
{
return new SizeInfo(Width * FormatInfo.BytesPerPixel);
}
else if (IsLinear)
{
return SizeCalculator.GetLinearTextureSize(
Stride,
Height,
FormatInfo.BlockHeight);
}
else
{
return SizeCalculator.GetBlockLinearTextureSize(
Width,
Height,
GetDepth(),
Levels,
GetLayers(),
FormatInfo.BlockWidth,
FormatInfo.BlockHeight,
FormatInfo.BytesPerPixel,
GobBlocksInY,
GobBlocksInZ,
GobBlocksInTileX,
layerSize);
}
}
///
/// Creates texture information for a given mipmap level of the specified parent texture and this information.
///
/// The parent texture
/// The first level of the texture view
/// The adjusted texture information with the new size
public TextureInfo CreateInfoForLevelView(Texture parent, int firstLevel)
{
// When the texture is used as view of another texture, we must
// ensure that the sizes are valid, otherwise data uploads would fail
// (and the size wouldn't match the real size used on the host API).
// Given a parent texture from where the view is created, we have the
// following rules:
// - The view size must be equal to the parent size, divided by (2 ^ l),
// where l is the first mipmap level of the view. The division result must
// be rounded down, and the result must be clamped to 1.
// - If the parent format is compressed, and the view format isn't, the
// view size is calculated as above, but the width and height of the
// view must be also divided by the compressed format block width and height.
// - If the parent format is not compressed, and the view is, the view
// size is calculated as described on the first point, but the width and height
// of the view must be also multiplied by the block width and height.
int width = Math.Max(1, parent.Info.Width >> firstLevel);
int height = Math.Max(1, parent.Info.Height >> firstLevel);
if (parent.Info.FormatInfo.IsCompressed && !FormatInfo.IsCompressed)
{
width = BitUtils.DivRoundUp(width, parent.Info.FormatInfo.BlockWidth);
height = BitUtils.DivRoundUp(height, parent.Info.FormatInfo.BlockHeight);
}
else if (!parent.Info.FormatInfo.IsCompressed && FormatInfo.IsCompressed)
{
width *= FormatInfo.BlockWidth;
height *= FormatInfo.BlockHeight;
}
int depthOrLayers;
if (Target == Target.Texture3D)
{
depthOrLayers = Math.Max(1, parent.Info.DepthOrLayers >> firstLevel);
}
else
{
depthOrLayers = DepthOrLayers;
}
// 2D and 2D multisample textures are not considered compatible.
// This specific case is required for copies, where the source texture might be multisample.
// In this case, we inherit the parent texture multisample state.
Target target = Target;
int samplesInX = SamplesInX;
int samplesInY = SamplesInY;
if (target == Target.Texture2D && parent.Target == Target.Texture2DMultisample)
{
target = Target.Texture2DMultisample;
samplesInX = parent.Info.SamplesInX;
samplesInY = parent.Info.SamplesInY;
}
return new TextureInfo(
GpuAddress,
width,
height,
depthOrLayers,
Levels,
samplesInX,
samplesInY,
Stride,
IsLinear,
GobBlocksInY,
GobBlocksInZ,
GobBlocksInTileX,
target,
FormatInfo,
DepthStencilMode,
SwizzleR,
SwizzleG,
SwizzleB,
SwizzleA);
}
}
}