summaryrefslogtreecommitdiffstats
path: root/src/video_core/surface.cpp
blob: c1c2f2c5245768e5bb797a08594611fce67698ba (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
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include "common/common_types.h"
#include "common/math_util.h"
#include "video_core/surface.h"

namespace VideoCore::Surface {

SurfaceTarget SurfaceTargetFromTextureType(Tegra::Texture::TextureType texture_type) {
    switch (texture_type) {
    case Tegra::Texture::TextureType::Texture1D:
        return SurfaceTarget::Texture1D;
    case Tegra::Texture::TextureType::Texture1DBuffer:
        return SurfaceTarget::TextureBuffer;
    case Tegra::Texture::TextureType::Texture2D:
    case Tegra::Texture::TextureType::Texture2DNoMipmap:
        return SurfaceTarget::Texture2D;
    case Tegra::Texture::TextureType::Texture3D:
        return SurfaceTarget::Texture3D;
    case Tegra::Texture::TextureType::TextureCubemap:
        return SurfaceTarget::TextureCubemap;
    case Tegra::Texture::TextureType::TextureCubeArray:
        return SurfaceTarget::TextureCubeArray;
    case Tegra::Texture::TextureType::Texture1DArray:
        return SurfaceTarget::Texture1DArray;
    case Tegra::Texture::TextureType::Texture2DArray:
        return SurfaceTarget::Texture2DArray;
    default:
        LOG_CRITICAL(HW_GPU, "Unimplemented texture_type={}", static_cast<u32>(texture_type));
        UNREACHABLE();
        return SurfaceTarget::Texture2D;
    }
}

bool SurfaceTargetIsLayered(SurfaceTarget target) {
    switch (target) {
    case SurfaceTarget::Texture1D:
    case SurfaceTarget::TextureBuffer:
    case SurfaceTarget::Texture2D:
    case SurfaceTarget::Texture3D:
        return false;
    case SurfaceTarget::Texture1DArray:
    case SurfaceTarget::Texture2DArray:
    case SurfaceTarget::TextureCubemap:
    case SurfaceTarget::TextureCubeArray:
        return true;
    default:
        LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
        UNREACHABLE();
        return false;
    }
}

bool SurfaceTargetIsArray(SurfaceTarget target) {
    switch (target) {
    case SurfaceTarget::Texture1D:
    case SurfaceTarget::TextureBuffer:
    case SurfaceTarget::Texture2D:
    case SurfaceTarget::Texture3D:
    case SurfaceTarget::TextureCubemap:
        return false;
    case SurfaceTarget::Texture1DArray:
    case SurfaceTarget::Texture2DArray:
    case SurfaceTarget::TextureCubeArray:
        return true;
    default:
        LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
        UNREACHABLE();
        return false;
    }
}

PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format) {
    switch (format) {
    case Tegra::DepthFormat::S8_Z24_UNORM:
        return PixelFormat::S8Z24;
    case Tegra::DepthFormat::Z24_S8_UNORM:
        return PixelFormat::Z24S8;
    case Tegra::DepthFormat::Z32_FLOAT:
        return PixelFormat::Z32F;
    case Tegra::DepthFormat::Z16_UNORM:
        return PixelFormat::Z16;
    case Tegra::DepthFormat::Z32_S8_X24_FLOAT:
        return PixelFormat::Z32FS8;
    default:
        UNIMPLEMENTED_MSG("Unimplemented format={}", static_cast<u32>(format));
        return PixelFormat::S8Z24;
    }
}

PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
    switch (format) {
    case Tegra::RenderTargetFormat::RGBA32_FLOAT:
        return PixelFormat::RGBA32F;
    case Tegra::RenderTargetFormat::RGBA32_SINT:
        return PixelFormat::RGBA32I;
    case Tegra::RenderTargetFormat::RGBA32_UINT:
        return PixelFormat::RGBA32UI;
    case Tegra::RenderTargetFormat::RGBA16_UNORM:
        return PixelFormat::RGBA16U;
    case Tegra::RenderTargetFormat::RGBA16_SNORM:
        return PixelFormat::RGBA16S;
    case Tegra::RenderTargetFormat::RGBA16_SINT:
        return PixelFormat::RGBA16I;
    case Tegra::RenderTargetFormat::RGBA16_UINT:
        return PixelFormat::RGBA16UI;
    case Tegra::RenderTargetFormat::RGBA16_FLOAT:
        return PixelFormat::RGBA16F;
    case Tegra::RenderTargetFormat::RG32_FLOAT:
        return PixelFormat::RG32F;
    case Tegra::RenderTargetFormat::RG32_SINT:
        return PixelFormat::RG32I;
    case Tegra::RenderTargetFormat::RG32_UINT:
        return PixelFormat::RG32UI;
    case Tegra::RenderTargetFormat::RGBX16_FLOAT:
        return PixelFormat::RGBX16F;
    case Tegra::RenderTargetFormat::BGRA8_UNORM:
        return PixelFormat::BGRA8;
    case Tegra::RenderTargetFormat::BGRA8_SRGB:
        return PixelFormat::BGRA8_SRGB;
    case Tegra::RenderTargetFormat::RGB10_A2_UNORM:
        return PixelFormat::A2B10G10R10U;
    case Tegra::RenderTargetFormat::RGB10_A2_UINT:
        return PixelFormat::A2B10G10R10UI;
    case Tegra::RenderTargetFormat::RGBA8_UNORM:
        return PixelFormat::ABGR8U;
    case Tegra::RenderTargetFormat::RGBA8_SRGB:
        return PixelFormat::RGBA8_SRGB;
    case Tegra::RenderTargetFormat::RGBA8_SNORM:
        return PixelFormat::ABGR8S;
    case Tegra::RenderTargetFormat::RGBA8_SINT:
        return PixelFormat::ABGR8I;
    case Tegra::RenderTargetFormat::RGBA8_UINT:
        return PixelFormat::ABGR8UI;
    case Tegra::RenderTargetFormat::RG16_UNORM:
        return PixelFormat::RG16;
    case Tegra::RenderTargetFormat::RG16_SNORM:
        return PixelFormat::RG16S;
    case Tegra::RenderTargetFormat::RG16_SINT:
        return PixelFormat::RG16I;
    case Tegra::RenderTargetFormat::RG16_UINT:
        return PixelFormat::RG16UI;
    case Tegra::RenderTargetFormat::RG16_FLOAT:
        return PixelFormat::RG16F;
    case Tegra::RenderTargetFormat::R11G11B10_FLOAT:
        return PixelFormat::R11FG11FB10F;
    case Tegra::RenderTargetFormat::R32_SINT:
        return PixelFormat::R32I;
    case Tegra::RenderTargetFormat::R32_UINT:
        return PixelFormat::R32UI;
    case Tegra::RenderTargetFormat::R32_FLOAT:
        return PixelFormat::R32F;
    case Tegra::RenderTargetFormat::B5G6R5_UNORM:
        return PixelFormat::B5G6R5U;
    case Tegra::RenderTargetFormat::BGR5A1_UNORM:
        return PixelFormat::B5G5R5A1U;
    case Tegra::RenderTargetFormat::RG8_UNORM:
        return PixelFormat::RG8U;
    case Tegra::RenderTargetFormat::RG8_SNORM:
        return PixelFormat::RG8S;
    case Tegra::RenderTargetFormat::RG8_SINT:
        return PixelFormat::RG8I;
    case Tegra::RenderTargetFormat::RG8_UINT:
        return PixelFormat::RG8UI;
    case Tegra::RenderTargetFormat::R16_UNORM:
        return PixelFormat::R16U;
    case Tegra::RenderTargetFormat::R16_SNORM:
        return PixelFormat::R16S;
    case Tegra::RenderTargetFormat::R16_SINT:
        return PixelFormat::R16I;
    case Tegra::RenderTargetFormat::R16_UINT:
        return PixelFormat::R16UI;
    case Tegra::RenderTargetFormat::R16_FLOAT:
        return PixelFormat::R16F;
    case Tegra::RenderTargetFormat::R8_UNORM:
        return PixelFormat::R8U;
    case Tegra::RenderTargetFormat::R8_SNORM:
        return PixelFormat::R8S;
    case Tegra::RenderTargetFormat::R8_SINT:
        return PixelFormat::R8I;
    case Tegra::RenderTargetFormat::R8_UINT:
        return PixelFormat::R8UI;
    default:
        UNIMPLEMENTED_MSG("Unimplemented format={}", static_cast<int>(format));
        return PixelFormat::ABGR8U;
    }
}

PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format) {
    switch (format) {
    case Tegra::FramebufferConfig::PixelFormat::ABGR8:
        return PixelFormat::ABGR8U;
    case Tegra::FramebufferConfig::PixelFormat::RGB565:
        return PixelFormat::B5G6R5U;
    case Tegra::FramebufferConfig::PixelFormat::BGRA8:
        return PixelFormat::BGRA8;
    default:
        UNIMPLEMENTED_MSG("Unimplemented format={}", static_cast<u32>(format));
        return PixelFormat::ABGR8U;
    }
}

SurfaceType GetFormatType(PixelFormat pixel_format) {
    if (static_cast<std::size_t>(pixel_format) <
        static_cast<std::size_t>(PixelFormat::MaxColorFormat)) {
        return SurfaceType::ColorTexture;
    }

    if (static_cast<std::size_t>(pixel_format) <
        static_cast<std::size_t>(PixelFormat::MaxDepthFormat)) {
        return SurfaceType::Depth;
    }

    if (static_cast<std::size_t>(pixel_format) <
        static_cast<std::size_t>(PixelFormat::MaxDepthStencilFormat)) {
        return SurfaceType::DepthStencil;
    }

    // TODO(Subv): Implement the other formats
    ASSERT(false);

    return SurfaceType::Invalid;
}

bool IsPixelFormatASTC(PixelFormat format) {
    switch (format) {
    case PixelFormat::ASTC_2D_4X4:
    case PixelFormat::ASTC_2D_5X4:
    case PixelFormat::ASTC_2D_5X5:
    case PixelFormat::ASTC_2D_8X8:
    case PixelFormat::ASTC_2D_8X5:
    case PixelFormat::ASTC_2D_4X4_SRGB:
    case PixelFormat::ASTC_2D_5X4_SRGB:
    case PixelFormat::ASTC_2D_5X5_SRGB:
    case PixelFormat::ASTC_2D_8X8_SRGB:
    case PixelFormat::ASTC_2D_8X5_SRGB:
    case PixelFormat::ASTC_2D_10X8:
    case PixelFormat::ASTC_2D_10X8_SRGB:
    case PixelFormat::ASTC_2D_6X6:
    case PixelFormat::ASTC_2D_6X6_SRGB:
    case PixelFormat::ASTC_2D_10X10:
    case PixelFormat::ASTC_2D_10X10_SRGB:
    case PixelFormat::ASTC_2D_12X12:
    case PixelFormat::ASTC_2D_12X12_SRGB:
    case PixelFormat::ASTC_2D_8X6:
    case PixelFormat::ASTC_2D_8X6_SRGB:
    case PixelFormat::ASTC_2D_6X5:
    case PixelFormat::ASTC_2D_6X5_SRGB:
        return true;
    default:
        return false;
    }
}

bool IsPixelFormatSRGB(PixelFormat format) {
    switch (format) {
    case PixelFormat::RGBA8_SRGB:
    case PixelFormat::BGRA8_SRGB:
    case PixelFormat::DXT1_SRGB:
    case PixelFormat::DXT23_SRGB:
    case PixelFormat::DXT45_SRGB:
    case PixelFormat::BC7U_SRGB:
    case PixelFormat::ASTC_2D_4X4_SRGB:
    case PixelFormat::ASTC_2D_8X8_SRGB:
    case PixelFormat::ASTC_2D_8X5_SRGB:
    case PixelFormat::ASTC_2D_5X4_SRGB:
    case PixelFormat::ASTC_2D_5X5_SRGB:
    case PixelFormat::ASTC_2D_10X8_SRGB:
    case PixelFormat::ASTC_2D_6X6_SRGB:
    case PixelFormat::ASTC_2D_10X10_SRGB:
    case PixelFormat::ASTC_2D_12X12_SRGB:
    case PixelFormat::ASTC_2D_8X6_SRGB:
    case PixelFormat::ASTC_2D_6X5_SRGB:
        return true;
    default:
        return false;
    }
}

std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
    return {GetDefaultBlockWidth(format), GetDefaultBlockHeight(format)};
}

bool IsFormatBCn(PixelFormat format) {
    switch (format) {
    case PixelFormat::DXT1:
    case PixelFormat::DXT23:
    case PixelFormat::DXT45:
    case PixelFormat::DXN1:
    case PixelFormat::DXN2SNORM:
    case PixelFormat::DXN2UNORM:
    case PixelFormat::BC7U:
    case PixelFormat::BC6H_UF16:
    case PixelFormat::BC6H_SF16:
    case PixelFormat::DXT1_SRGB:
    case PixelFormat::DXT23_SRGB:
    case PixelFormat::DXT45_SRGB:
    case PixelFormat::BC7U_SRGB:
        return true;
    default:
        return false;
    }
}

} // namespace VideoCore::Surface