aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/Gpu/TextureReader.cs
blob: 63dd27977ccf0cbe2120f273190ae7a09f86d65c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using ChocolArm64.Memory;
using Ryujinx.Graphics.Gal;
using System;

namespace Ryujinx.Graphics.Gpu
{
    static class TextureReader
    {
        public static byte[] Read(AMemory Memory, Texture Texture)
        {
            switch (Texture.Format)
            {
                case GalTextureFormat.A8B8G8R8: return Read4Bpp    (Memory, Texture);
                case GalTextureFormat.BC1:      return Read8Bpt4x4 (Memory, Texture);
                case GalTextureFormat.BC2:      return Read16Bpt4x4(Memory, Texture);
                case GalTextureFormat.BC3:      return Read16Bpt4x4(Memory, Texture);
            }

            throw new NotImplementedException(Texture.Format.ToString());
        }

        private unsafe static byte[] Read4Bpp(AMemory Memory, Texture Texture)
        {
            int Width  = Texture.Width;
            int Height = Texture.Height;

            byte[] Output = new byte[Width * Height * 4];

            ISwizzle Swizzle = GetSwizzle(Texture, 4);

            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);

                    int Pixel = Memory.ReadInt32Unchecked(Texture.Position + Offset);

                    *(int*)(BuffPtr + OutOffs) = Pixel;

                    OutOffs += 4;
                }
            }

            return Output;
        }

        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];

            ISwizzle Swizzle = GetSwizzle(Texture, 8);

            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];

            ISwizzle Swizzle = GetSwizzle(Texture, 16);

            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;
        }

        private static ISwizzle GetSwizzle(Texture Texture, int Bpp)
        {
            switch (Texture.Swizzle)
            {
                case TextureSwizzle.Pitch:
                case TextureSwizzle.PitchColorKey:
                     return new LinearSwizzle(Texture.Pitch, Bpp);

                case TextureSwizzle.BlockLinear:
                case TextureSwizzle.BlockLinearColorKey:
                    return new BlockLinearSwizzle(Texture.Width, Bpp, Texture.BlockHeight);
            }

            throw new NotImplementedException(Texture.Swizzle.ToString());
        }
    }
}