using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
namespace Ryujinx.Graphics.Gpu.Image
{
///
/// Maxwell texture descriptor, as stored on the GPU texture pool memory region.
///
struct TextureDescriptor : ITextureDescriptor, IEquatable
{
#pragma warning disable CS0649 // Field is never assigned to
public uint Word0;
public uint Word1;
public uint Word2;
public uint Word3;
public uint Word4;
public uint Word5;
public uint Word6;
public uint Word7;
#pragma warning restore CS0649
///
/// Unpacks Maxwell texture format integer.
///
/// The texture format integer
public readonly uint UnpackFormat()
{
return Word0 & 0x8007ffff;
}
///
/// Unpacks the swizzle component for the texture red color channel.
///
/// The swizzle component
public readonly TextureComponent UnpackSwizzleR()
{
return (TextureComponent)((Word0 >> 19) & 7);
}
///
/// Unpacks the swizzle component for the texture green color channel.
///
/// The swizzle component
public readonly TextureComponent UnpackSwizzleG()
{
return (TextureComponent)((Word0 >> 22) & 7);
}
///
/// Unpacks the swizzle component for the texture blue color channel.
///
/// The swizzle component
public readonly TextureComponent UnpackSwizzleB()
{
return (TextureComponent)((Word0 >> 25) & 7);
}
///
/// Unpacks the swizzle component for the texture alpha color channel.
///
/// The swizzle component
public readonly TextureComponent UnpackSwizzleA()
{
return (TextureComponent)((Word0 >> 28) & 7);
}
///
/// Unpacks the 40-bits texture GPU virtual address.
///
/// The GPU virtual address
public readonly ulong UnpackAddress()
{
return Word1 | ((ulong)(Word2 & 0xffff) << 32);
}
///
/// Unpacks texture descriptor type for this texture descriptor.
/// This defines the texture layout, among other things.
///
/// The texture descriptor type
public readonly TextureDescriptorType UnpackTextureDescriptorType()
{
return (TextureDescriptorType)((Word2 >> 21) & 7);
}
///
/// Unpacks the texture stride (bytes per line) for linear textures only.
/// Always 32-bytes aligned.
///
/// The linear texture stride
public readonly int UnpackStride()
{
return (int)(Word3 & 0xffff) << 5;
}
///
/// Unpacks the GOB block size in X (width) for block linear textures.
/// Must be always 1, ignored by the GPU.
///
/// THe GOB block X size
public readonly int UnpackGobBlocksInX()
{
return 1 << (int)(Word3 & 7);
}
///
/// Unpacks the GOB block size in Y (height) for block linear textures.
/// Must be always a power of 2, with a maximum value of 32.
///
/// THe GOB block Y size
public readonly int UnpackGobBlocksInY()
{
return 1 << (int)((Word3 >> 3) & 7);
}
///
/// Unpacks the GOB block size in Z (depth) for block linear textures.
/// Must be always a power of 2, with a maximum value of 32.
/// Must be 1 for any texture target other than 3D textures.
///
/// The GOB block Z size
public readonly int UnpackGobBlocksInZ()
{
return 1 << (int)((Word3 >> 6) & 7);
}
///
/// Number of GOB blocks per tile in the X direction.
/// This is only used for sparse textures, should be 1 otherwise.
///
/// The number of GOB blocks per tile
public readonly int UnpackGobBlocksInTileX()
{
return 1 << (int)((Word3 >> 10) & 7);
}
///
/// Unpacks the number of mipmap levels of the texture.
///
/// The number of mipmap levels
public readonly int UnpackLevels()
{
return (int)(Word3 >> 28) + 1;
}
///
/// Unpack the base level texture width size.
///
/// The texture width
public readonly int UnpackWidth()
{
return (int)(Word4 & 0xffff) + 1;
}
///
/// Unpack the width of a buffer texture.
///
/// The texture width
public readonly int UnpackBufferTextureWidth()
{
return (int)((Word4 & 0xffff) | (Word3 << 16)) + 1;
}
///
/// Unpacks the texture sRGB format flag.
///
/// True if the texture is sRGB, false otherwise
public readonly bool UnpackSrgb()
{
return (Word4 & (1 << 22)) != 0;
}
///
/// Unpacks the texture target.
///
/// The texture target
public readonly TextureTarget UnpackTextureTarget()
{
return (TextureTarget)((Word4 >> 23) & 0xf);
}
///
/// Unpack the base level texture height size, or array layers for 1D array textures.
/// Should be ignored for 1D or buffer textures.
///
/// The texture height or layers count
public readonly int UnpackHeight()
{
return (int)(Word5 & 0xffff) + 1;
}
///
/// Unpack the base level texture depth size, number of array layers or cubemap faces.
/// The meaning of this value depends on the texture target.
///
/// The texture depth, layer or faces count
public readonly int UnpackDepth()
{
return (int)((Word5 >> 16) & 0x3fff) + 1;
}
///
/// Unpacks the texture coordinates normalized flag.
/// When this is true, texture coordinates are expected to be in the [0, 1] range on the shader.
/// When this is false, texture coordinates are expected to be in the [0, W], [0, H] and [0, D] range.
/// It must be set to false (by the guest driver) for rectangle textures.
///
/// The texture coordinates normalized flag
public readonly bool UnpackTextureCoordNormalized()
{
return (Word5 & (1 << 31)) != 0;
}
///
/// Unpacks the base mipmap level of the texture.
///
/// The base mipmap level of the texture
public readonly int UnpackBaseLevel()
{
return (int)(Word7 & 0xf);
}
///
/// Unpacks the maximum mipmap level (inclusive) of the texture.
/// Usually equal to Levels minus 1.
///
/// The maximum mipmap level (inclusive) of the texture
public readonly int UnpackMaxLevelInclusive()
{
return (int)((Word7 >> 4) & 0xf);
}
///
/// Unpacks the multisampled texture samples count in each direction.
/// Must be ignored for non-multisample textures.
///
/// The multisample counts enum
public readonly TextureMsaaMode UnpackTextureMsaaMode()
{
return (TextureMsaaMode)((Word7 >> 8) & 0xf);
}
///
/// Check if two descriptors are equal.
///
/// The descriptor to compare against
/// True if they are equal, false otherwise
public bool Equals(ref TextureDescriptor other)
{
return Unsafe.As>(ref this).Equals(Unsafe.As>(ref other));
}
///
/// Check if two descriptors are equal.
///
/// The descriptor to compare against
/// True if they are equal, false otherwise
public bool Equals(TextureDescriptor other)
{
return Equals(ref other);
}
///
/// Gets a hash code for this descriptor.
///
/// The hash code for this descriptor.
public override int GetHashCode()
{
return Unsafe.As>(ref this).GetHashCode();
}
public override bool Equals(object obj)
{
return obj is TextureDescriptor descriptor && Equals(descriptor);
}
}
}