aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Image/TextureInfo.cs
blob: 571f440e5685672d2223d961d32745da0fd89e62 (plain) (blame)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Texture;

namespace Ryujinx.Graphics.Gpu.Image
{
    /// <summary>
    /// Texture information.
    /// </summary>
    struct TextureInfo
    {
        /// <summary>
        /// Address of the texture in GPU mapped memory.
        /// </summary>
        public ulong GpuAddress { get; }

        /// <summary>
        /// The width of the texture.
        /// </summary>
        public int Width { get; }

        /// <summary>
        /// The height of the texture, or layers count for 1D array textures.
        /// </summary>
        public int Height { get; }

        /// <summary>
        /// The depth of the texture (for 3D textures), or layers count for array textures.
        /// </summary>
        public int DepthOrLayers { get; }

        /// <summary>
        /// The number of mipmap levels of the texture.
        /// </summary>
        public int Levels { get; }

        /// <summary>
        /// The number of samples in the X direction for multisampled textures.
        /// </summary>
        public int SamplesInX { get; }

        /// <summary>
        /// The number of samples in the Y direction for multisampled textures.
        /// </summary>
        public int SamplesInY { get; }

        /// <summary>
        /// The number of bytes per line for linear textures.
        /// </summary>
        public int Stride { get; }

        /// <summary>
        /// Indicates whenever or not the texture is a linear texture.
        /// </summary>
        public bool IsLinear { get; }

        /// <summary>
        /// GOB blocks in the Y direction, for block linear textures.
        /// </summary>
        public int GobBlocksInY { get; }

        /// <summary>
        /// GOB blocks in the Z direction, for block linear textures.
        /// </summary>
        public int GobBlocksInZ { get; }

        /// <summary>
        /// Number of GOB blocks per tile in the X direction, for block linear textures.
        /// </summary>
        public int GobBlocksInTileX { get; }

        /// <summary>
        /// Total number of samples for multisampled textures.
        /// </summary>
        public int Samples => SamplesInX * SamplesInY;

        /// <summary>
        /// Texture target type.
        /// </summary>
        public Target Target { get; }

        /// <summary>
        /// Texture format information.
        /// </summary>
        public FormatInfo FormatInfo { get; }

        /// <summary>
        /// Depth-stencil mode of the texture. This defines whenever the depth or stencil value is read from shaders,
        /// for depth-stencil texture formats.
        /// </summary>
        public DepthStencilMode DepthStencilMode { get; }

        /// <summary>
        /// Texture swizzle for the red color channel.
        /// </summary>
        public SwizzleComponent SwizzleR { get; }

        /// <summary>
        /// Texture swizzle for the green color channel.
        /// </summary>
        public SwizzleComponent SwizzleG { get; }

        /// <summary>
        /// Texture swizzle for the blue color channel.
        /// </summary>
        public SwizzleComponent SwizzleB { get; }

        /// <summary>
        /// Texture swizzle for the alpha color channel.
        /// </summary>
        public SwizzleComponent SwizzleA { get; }

        /// <summary>
        /// Constructs the texture information structure.
        /// </summary>
        /// <param name="gpuAddress">The GPU address of the texture</param>
        /// <param name="width">The width of the texture</param>
        /// <param name="height">The height or the texture</param>
        /// <param name="depthOrLayers">The depth or layers count of the texture</param>
        /// <param name="levels">The amount of mipmap levels of the texture</param>
        /// <param name="samplesInX">The number of samples in the X direction for multisample textures, should be 1 otherwise</param>
        /// <param name="samplesInY">The number of samples in the Y direction for multisample textures, should be 1 otherwise</param>
        /// <param name="stride">The stride for linear textures</param>
        /// <param name="isLinear">Whenever the texture is linear or block linear</param>
        /// <param name="gobBlocksInY">Number of GOB blocks in the Y direction</param>
        /// <param name="gobBlocksInZ">Number of GOB blocks in the Z direction</param>
        /// <param name="gobBlocksInTileX">Number of GOB blocks per tile in the X direction</param>
        /// <param name="target">Texture target type</param>
        /// <param name="formatInfo">Texture format information</param>
        /// <param name="depthStencilMode">Depth-stencil mode</param>
        /// <param name="swizzleR">Swizzle for the red color channel</param>
        /// <param name="swizzleG">Swizzle for the green color channel</param>
        /// <param name="swizzleB">Swizzle for the blue color channel</param>
        /// <param name="swizzleA">Swizzle for the alpha color channel</param>
        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;
        }

        /// <summary>
        /// Gets the real texture depth.
        /// Returns 1 for any target other than 3D textures.
        /// </summary>
        /// <returns>Texture depth</returns>
        public int GetDepth()
        {
            return GetDepth(Target, DepthOrLayers);
        }

        /// <summary>
        /// Gets the real texture depth.
        /// Returns 1 for any target other than 3D textures.
        /// </summary>
        /// <param name="target">Texture target</param>
        /// <param name="depthOrLayers">Texture depth if the texture is 3D, otherwise ignored</param>
        /// <returns>Texture depth</returns>
        public static int GetDepth(Target target, int depthOrLayers)
        {
            return target == Target.Texture3D ? depthOrLayers : 1;
        }

        /// <summary>
        /// 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.
        /// </summary>
        /// <returns>The number of texture layers</returns>
        public int GetLayers()
        {
            return GetLayers(Target, DepthOrLayers);
        }

        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="target">Texture target</param>
        /// <param name="depthOrLayers">Texture layers if the is a array texture, ignored otherwise</param>
        /// <returns>The number of texture layers</returns>
        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;
            }
        }

        /// <summary>
        /// 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.
        /// </summary>
        /// <returns>The number of texture slices</returns>
        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;
            }
        }

        /// <summary>
        /// Calculates the size information from the texture information.
        /// </summary>
        /// <param name="layerSize">Optional size of each texture layer in bytes</param>
        /// <returns>Texture size information</returns>
        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);
            }
        }
    }
}