2018-04-08 21:17:35 +02:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using Ryujinx.Graphics.Gal;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu
|
|
|
|
{
|
2018-04-13 20:12:58 +02:00
|
|
|
public static class TextureReader
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
|
|
|
public static byte[] Read(AMemory Memory, Texture Texture)
|
|
|
|
{
|
|
|
|
switch (Texture.Format)
|
|
|
|
{
|
|
|
|
case GalTextureFormat.A8B8G8R8: return Read4Bpp (Memory, Texture);
|
2018-04-08 23:08:36 +02:00
|
|
|
case GalTextureFormat.A1B5G5R5: return Read2Bpp (Memory, Texture);
|
2018-04-10 21:50:32 +02:00
|
|
|
case GalTextureFormat.B5G6R5: return Read2Bpp (Memory, Texture);
|
2018-04-08 21:17:35 +02:00
|
|
|
case GalTextureFormat.BC1: return Read8Bpt4x4 (Memory, Texture);
|
|
|
|
case GalTextureFormat.BC2: return Read16Bpt4x4(Memory, Texture);
|
|
|
|
case GalTextureFormat.BC3: return Read16Bpt4x4(Memory, Texture);
|
2018-04-10 23:54:50 +02:00
|
|
|
case GalTextureFormat.BC4: return Read8Bpt4x4 (Memory, Texture);
|
|
|
|
case GalTextureFormat.BC5: return Read16Bpt4x4(Memory, Texture);
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotImplementedException(Texture.Format.ToString());
|
|
|
|
}
|
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
private unsafe static byte[] Read2Bpp(AMemory Memory, Texture Texture)
|
2018-04-08 21:17:35 +02:00
|
|
|
{
|
|
|
|
int Width = Texture.Width;
|
|
|
|
int Height = Texture.Height;
|
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
byte[] Output = new byte[Width * Height * 2];
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 2);
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
fixed (byte* BuffPtr = Output)
|
|
|
|
{
|
|
|
|
long OutOffs = 0;
|
|
|
|
|
|
|
|
for (int Y = 0; Y < Height; Y++)
|
|
|
|
for (int X = 0; X < Width; X++)
|
|
|
|
{
|
|
|
|
long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
|
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
short Pixel = Memory.ReadInt16Unchecked(Texture.Position + Offset);
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
*(short*)(BuffPtr + OutOffs) = Pixel;
|
2018-04-08 21:17:35 +02:00
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
OutOffs += 2;
|
2018-04-08 21:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
private unsafe static byte[] Read4Bpp(AMemory Memory, Texture Texture)
|
2018-04-08 23:08:36 +02:00
|
|
|
{
|
|
|
|
int Width = Texture.Width;
|
|
|
|
int Height = Texture.Height;
|
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
byte[] Output = new byte[Width * Height * 4];
|
2018-04-08 23:08:36 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 4);
|
2018-04-08 23:08:36 +02:00
|
|
|
|
|
|
|
fixed (byte* BuffPtr = Output)
|
|
|
|
{
|
|
|
|
long OutOffs = 0;
|
|
|
|
|
|
|
|
for (int Y = 0; Y < Height; Y++)
|
|
|
|
for (int X = 0; X < Width; X++)
|
|
|
|
{
|
|
|
|
long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
|
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
int Pixel = Memory.ReadInt32Unchecked(Texture.Position + Offset);
|
2018-04-08 23:08:36 +02:00
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
*(int*)(BuffPtr + OutOffs) = Pixel;
|
2018-04-08 23:08:36 +02:00
|
|
|
|
2018-04-10 21:50:32 +02:00
|
|
|
OutOffs += 4;
|
2018-04-08 23:08:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
2018-04-08 21:17:35 +02:00
|
|
|
private unsafe static byte[] Read8Bpt4x4(AMemory Memory, Texture Texture)
|
|
|
|
{
|
|
|
|
int Width = (Texture.Width + 3) / 4;
|
|
|
|
int Height = (Texture.Height + 3) / 4;
|
|
|
|
|
|
|
|
byte[] Output = new byte[Width * Height * 8];
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 8);
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
fixed (byte* BuffPtr = Output)
|
|
|
|
{
|
|
|
|
long OutOffs = 0;
|
|
|
|
|
|
|
|
for (int Y = 0; Y < Height; Y++)
|
|
|
|
for (int X = 0; X < Width; X++)
|
|
|
|
{
|
|
|
|
long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
|
|
|
|
|
|
|
|
long Tile = Memory.ReadInt64Unchecked(Texture.Position + Offset);
|
|
|
|
|
|
|
|
*(long*)(BuffPtr + OutOffs) = Tile;
|
|
|
|
|
|
|
|
OutOffs += 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
private unsafe static byte[] Read16Bpt4x4(AMemory Memory, Texture Texture)
|
|
|
|
{
|
|
|
|
int Width = (Texture.Width + 3) / 4;
|
|
|
|
int Height = (Texture.Height + 3) / 4;
|
|
|
|
|
|
|
|
byte[] Output = new byte[Width * Height * 16];
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
ISwizzle Swizzle = TextureHelper.GetSwizzle(Texture, Width, 16);
|
2018-04-08 21:17:35 +02:00
|
|
|
|
|
|
|
fixed (byte* BuffPtr = Output)
|
|
|
|
{
|
|
|
|
long OutOffs = 0;
|
|
|
|
|
|
|
|
for (int Y = 0; Y < Height; Y++)
|
|
|
|
for (int X = 0; X < Width; X++)
|
|
|
|
{
|
|
|
|
long Offset = (uint)Swizzle.GetSwizzleOffset(X, Y);
|
|
|
|
|
|
|
|
long Tile0 = Memory.ReadInt64Unchecked(Texture.Position + Offset + 0);
|
|
|
|
long Tile1 = Memory.ReadInt64Unchecked(Texture.Position + Offset + 8);
|
|
|
|
|
|
|
|
*(long*)(BuffPtr + OutOffs + 0) = Tile0;
|
|
|
|
*(long*)(BuffPtr + OutOffs + 8) = Tile1;
|
|
|
|
|
|
|
|
OutOffs += 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 23:08:36 +02:00
|
|
|
}
|