aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Gpu/Texture/TextureHelper.cs
blob: c0749d6a255758d459c5bcb4b247a1947b204584 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using ChocolArm64.Memory;
using Ryujinx.Graphics.Gal;
using Ryujinx.HLE.Gpu.Memory;
using System;

namespace Ryujinx.HLE.Gpu.Texture
{
    static class TextureHelper
    {
        public static ISwizzle GetSwizzle(TextureInfo Texture, int Width, int Bpp)
        {
            switch (Texture.Swizzle)
            {
                case TextureSwizzle._1dBuffer:
                case TextureSwizzle.Pitch:
                case TextureSwizzle.PitchColorKey:
                     return new LinearSwizzle(Texture.Pitch, Bpp);

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

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

        public static int GetTextureSize(GalTexture Texture)
        {
            switch (Texture.Format)
            {
                case GalTextureFormat.R32G32B32A32:
                    return Texture.Width * Texture.Height * 16;

                case GalTextureFormat.R16G16B16A16:
                    return Texture.Width * Texture.Height * 8;

                case GalTextureFormat.A8B8G8R8:
                case GalTextureFormat.R32:
                case GalTextureFormat.ZF32:
                    return Texture.Width * Texture.Height * 4;

                case GalTextureFormat.A1B5G5R5:
                case GalTextureFormat.B5G6R5:
                case GalTextureFormat.G8R8:
                case GalTextureFormat.R16:
                    return Texture.Width * Texture.Height * 2;

                case GalTextureFormat.R8:
                    return Texture.Width * Texture.Height;

                case GalTextureFormat.BC1:
                case GalTextureFormat.BC4:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 4, 4, 8);
                }

                case GalTextureFormat.BC7U:
                case GalTextureFormat.BC2:
                case GalTextureFormat.BC3:
                case GalTextureFormat.BC5:
                case GalTextureFormat.Astc2D4x4:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 4, 4, 16);
                }

                case GalTextureFormat.Astc2D5x5:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 5, 5, 16);
                }

                case GalTextureFormat.Astc2D6x6:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 6, 6, 16);
                }

                case GalTextureFormat.Astc2D8x8:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 8, 8, 16);
                }

                case GalTextureFormat.Astc2D10x10:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 10, 10, 16);
                }

                case GalTextureFormat.Astc2D12x12:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 12, 12, 16);
                }

                case GalTextureFormat.Astc2D5x4:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 5, 4, 16);
                }

                case GalTextureFormat.Astc2D6x5:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 6, 5, 16);
                }

                case GalTextureFormat.Astc2D8x6:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 8, 6, 16);
                }

                case GalTextureFormat.Astc2D10x8:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 10, 8, 16);
                }

                case GalTextureFormat.Astc2D12x10:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 12, 10, 16);
                }

                case GalTextureFormat.Astc2D8x5:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 8, 5, 16);
                }

                case GalTextureFormat.Astc2D10x5:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 10, 5, 16);
                }

                case GalTextureFormat.Astc2D10x6:
                {
                    return CompressedTextureSize(Texture.Width, Texture.Height, 10, 6, 16);
                }
            }

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

        public static int CompressedTextureSize(int TextureWidth, int TextureHeight, int BlockWidth, int BlockHeight, int Bpb)
        {
            int W = (TextureWidth  + (BlockWidth - 1)) / BlockWidth;
            int H = (TextureHeight + (BlockHeight - 1)) / BlockHeight;

            return W * H * Bpb;
        }

        public static (AMemory Memory, long Position) GetMemoryAndPosition(
            IAMemory Memory,
            long     Position)
        {
            if (Memory is NvGpuVmm Vmm)
            {
                return (Vmm.Memory, Vmm.GetPhysicalAddress(Position));
            }

            return ((AMemory)Memory, Position);
        }
    }
}