1 // Copyright 2014 Citra Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4 
5 #include "common/common_types.h"
6 #include "common/math_util.h"
7 #include "video_core/surface.h"
8 
9 namespace VideoCore::Surface {
10 
SurfaceTargetFromTextureType(Tegra::Texture::TextureType texture_type)11 SurfaceTarget SurfaceTargetFromTextureType(Tegra::Texture::TextureType texture_type) {
12     switch (texture_type) {
13     case Tegra::Texture::TextureType::Texture1D:
14         return SurfaceTarget::Texture1D;
15     case Tegra::Texture::TextureType::Texture1DBuffer:
16         return SurfaceTarget::TextureBuffer;
17     case Tegra::Texture::TextureType::Texture2D:
18     case Tegra::Texture::TextureType::Texture2DNoMipmap:
19         return SurfaceTarget::Texture2D;
20     case Tegra::Texture::TextureType::Texture3D:
21         return SurfaceTarget::Texture3D;
22     case Tegra::Texture::TextureType::TextureCubemap:
23         return SurfaceTarget::TextureCubemap;
24     case Tegra::Texture::TextureType::TextureCubeArray:
25         return SurfaceTarget::TextureCubeArray;
26     case Tegra::Texture::TextureType::Texture1DArray:
27         return SurfaceTarget::Texture1DArray;
28     case Tegra::Texture::TextureType::Texture2DArray:
29         return SurfaceTarget::Texture2DArray;
30     default:
31         LOG_CRITICAL(HW_GPU, "Unimplemented texture_type={}", texture_type);
32         UNREACHABLE();
33         return SurfaceTarget::Texture2D;
34     }
35 }
36 
SurfaceTargetIsLayered(SurfaceTarget target)37 bool SurfaceTargetIsLayered(SurfaceTarget target) {
38     switch (target) {
39     case SurfaceTarget::Texture1D:
40     case SurfaceTarget::TextureBuffer:
41     case SurfaceTarget::Texture2D:
42     case SurfaceTarget::Texture3D:
43         return false;
44     case SurfaceTarget::Texture1DArray:
45     case SurfaceTarget::Texture2DArray:
46     case SurfaceTarget::TextureCubemap:
47     case SurfaceTarget::TextureCubeArray:
48         return true;
49     default:
50         LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", target);
51         UNREACHABLE();
52         return false;
53     }
54 }
55 
SurfaceTargetIsArray(SurfaceTarget target)56 bool SurfaceTargetIsArray(SurfaceTarget target) {
57     switch (target) {
58     case SurfaceTarget::Texture1D:
59     case SurfaceTarget::TextureBuffer:
60     case SurfaceTarget::Texture2D:
61     case SurfaceTarget::Texture3D:
62     case SurfaceTarget::TextureCubemap:
63         return false;
64     case SurfaceTarget::Texture1DArray:
65     case SurfaceTarget::Texture2DArray:
66     case SurfaceTarget::TextureCubeArray:
67         return true;
68     default:
69         LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", target);
70         UNREACHABLE();
71         return false;
72     }
73 }
74 
PixelFormatFromDepthFormat(Tegra::DepthFormat format)75 PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format) {
76     switch (format) {
77     case Tegra::DepthFormat::S8_UINT_Z24_UNORM:
78         return PixelFormat::S8_UINT_D24_UNORM;
79     case Tegra::DepthFormat::D24S8_UNORM:
80         return PixelFormat::D24_UNORM_S8_UINT;
81     case Tegra::DepthFormat::D32_FLOAT:
82         return PixelFormat::D32_FLOAT;
83     case Tegra::DepthFormat::D16_UNORM:
84         return PixelFormat::D16_UNORM;
85     case Tegra::DepthFormat::D32_FLOAT_S8X24_UINT:
86         return PixelFormat::D32_FLOAT_S8_UINT;
87     default:
88         UNIMPLEMENTED_MSG("Unimplemented format={}", format);
89         return PixelFormat::S8_UINT_D24_UNORM;
90     }
91 }
92 
PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format)93 PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
94     switch (format) {
95     case Tegra::RenderTargetFormat::R32B32G32A32_FLOAT:
96         return PixelFormat::R32G32B32A32_FLOAT;
97     case Tegra::RenderTargetFormat::R32G32B32A32_SINT:
98         return PixelFormat::R32G32B32A32_SINT;
99     case Tegra::RenderTargetFormat::R32G32B32A32_UINT:
100         return PixelFormat::R32G32B32A32_UINT;
101     case Tegra::RenderTargetFormat::R16G16B16A16_UNORM:
102         return PixelFormat::R16G16B16A16_UNORM;
103     case Tegra::RenderTargetFormat::R16G16B16A16_SNORM:
104         return PixelFormat::R16G16B16A16_SNORM;
105     case Tegra::RenderTargetFormat::R16G16B16A16_SINT:
106         return PixelFormat::R16G16B16A16_SINT;
107     case Tegra::RenderTargetFormat::R16G16B16A16_UINT:
108         return PixelFormat::R16G16B16A16_UINT;
109     case Tegra::RenderTargetFormat::R16G16B16A16_FLOAT:
110         return PixelFormat::R16G16B16A16_FLOAT;
111     case Tegra::RenderTargetFormat::R32G32_FLOAT:
112         return PixelFormat::R32G32_FLOAT;
113     case Tegra::RenderTargetFormat::R32G32_SINT:
114         return PixelFormat::R32G32_SINT;
115     case Tegra::RenderTargetFormat::R32G32_UINT:
116         return PixelFormat::R32G32_UINT;
117     case Tegra::RenderTargetFormat::R16G16B16X16_FLOAT:
118         return PixelFormat::R16G16B16X16_FLOAT;
119     case Tegra::RenderTargetFormat::B8G8R8A8_UNORM:
120         return PixelFormat::B8G8R8A8_UNORM;
121     case Tegra::RenderTargetFormat::B8G8R8A8_SRGB:
122         return PixelFormat::B8G8R8A8_SRGB;
123     case Tegra::RenderTargetFormat::A2B10G10R10_UNORM:
124         return PixelFormat::A2B10G10R10_UNORM;
125     case Tegra::RenderTargetFormat::A2B10G10R10_UINT:
126         return PixelFormat::A2B10G10R10_UINT;
127     case Tegra::RenderTargetFormat::A8B8G8R8_UNORM:
128         return PixelFormat::A8B8G8R8_UNORM;
129     case Tegra::RenderTargetFormat::A8B8G8R8_SRGB:
130         return PixelFormat::A8B8G8R8_SRGB;
131     case Tegra::RenderTargetFormat::A8B8G8R8_SNORM:
132         return PixelFormat::A8B8G8R8_SNORM;
133     case Tegra::RenderTargetFormat::A8B8G8R8_SINT:
134         return PixelFormat::A8B8G8R8_SINT;
135     case Tegra::RenderTargetFormat::A8B8G8R8_UINT:
136         return PixelFormat::A8B8G8R8_UINT;
137     case Tegra::RenderTargetFormat::R16G16_UNORM:
138         return PixelFormat::R16G16_UNORM;
139     case Tegra::RenderTargetFormat::R16G16_SNORM:
140         return PixelFormat::R16G16_SNORM;
141     case Tegra::RenderTargetFormat::R16G16_SINT:
142         return PixelFormat::R16G16_SINT;
143     case Tegra::RenderTargetFormat::R16G16_UINT:
144         return PixelFormat::R16G16_UINT;
145     case Tegra::RenderTargetFormat::R16G16_FLOAT:
146         return PixelFormat::R16G16_FLOAT;
147     case Tegra::RenderTargetFormat::B10G11R11_FLOAT:
148         return PixelFormat::B10G11R11_FLOAT;
149     case Tegra::RenderTargetFormat::R32_SINT:
150         return PixelFormat::R32_SINT;
151     case Tegra::RenderTargetFormat::R32_UINT:
152         return PixelFormat::R32_UINT;
153     case Tegra::RenderTargetFormat::R32_FLOAT:
154         return PixelFormat::R32_FLOAT;
155     case Tegra::RenderTargetFormat::R5G6B5_UNORM:
156         return PixelFormat::R5G6B5_UNORM;
157     case Tegra::RenderTargetFormat::A1R5G5B5_UNORM:
158         return PixelFormat::A1R5G5B5_UNORM;
159     case Tegra::RenderTargetFormat::R8G8_UNORM:
160         return PixelFormat::R8G8_UNORM;
161     case Tegra::RenderTargetFormat::R8G8_SNORM:
162         return PixelFormat::R8G8_SNORM;
163     case Tegra::RenderTargetFormat::R8G8_SINT:
164         return PixelFormat::R8G8_SINT;
165     case Tegra::RenderTargetFormat::R8G8_UINT:
166         return PixelFormat::R8G8_UINT;
167     case Tegra::RenderTargetFormat::R16_UNORM:
168         return PixelFormat::R16_UNORM;
169     case Tegra::RenderTargetFormat::R16_SNORM:
170         return PixelFormat::R16_SNORM;
171     case Tegra::RenderTargetFormat::R16_SINT:
172         return PixelFormat::R16_SINT;
173     case Tegra::RenderTargetFormat::R16_UINT:
174         return PixelFormat::R16_UINT;
175     case Tegra::RenderTargetFormat::R16_FLOAT:
176         return PixelFormat::R16_FLOAT;
177     case Tegra::RenderTargetFormat::R8_UNORM:
178         return PixelFormat::R8_UNORM;
179     case Tegra::RenderTargetFormat::R8_SNORM:
180         return PixelFormat::R8_SNORM;
181     case Tegra::RenderTargetFormat::R8_SINT:
182         return PixelFormat::R8_SINT;
183     case Tegra::RenderTargetFormat::R8_UINT:
184         return PixelFormat::R8_UINT;
185     default:
186         UNIMPLEMENTED_MSG("Unimplemented format={}", format);
187         return PixelFormat::A8B8G8R8_UNORM;
188     }
189 }
190 
PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format)191 PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format) {
192     switch (format) {
193     case Tegra::FramebufferConfig::PixelFormat::A8B8G8R8_UNORM:
194         return PixelFormat::A8B8G8R8_UNORM;
195     case Tegra::FramebufferConfig::PixelFormat::RGB565_UNORM:
196         return PixelFormat::R5G6B5_UNORM;
197     case Tegra::FramebufferConfig::PixelFormat::B8G8R8A8_UNORM:
198         return PixelFormat::B8G8R8A8_UNORM;
199     default:
200         UNIMPLEMENTED_MSG("Unimplemented format={}", format);
201         return PixelFormat::A8B8G8R8_UNORM;
202     }
203 }
204 
GetFormatType(PixelFormat pixel_format)205 SurfaceType GetFormatType(PixelFormat pixel_format) {
206     if (static_cast<std::size_t>(pixel_format) <
207         static_cast<std::size_t>(PixelFormat::MaxColorFormat)) {
208         return SurfaceType::ColorTexture;
209     }
210 
211     if (static_cast<std::size_t>(pixel_format) <
212         static_cast<std::size_t>(PixelFormat::MaxDepthFormat)) {
213         return SurfaceType::Depth;
214     }
215 
216     if (static_cast<std::size_t>(pixel_format) <
217         static_cast<std::size_t>(PixelFormat::MaxDepthStencilFormat)) {
218         return SurfaceType::DepthStencil;
219     }
220 
221     // TODO(Subv): Implement the other formats
222     ASSERT(false);
223 
224     return SurfaceType::Invalid;
225 }
226 
IsPixelFormatASTC(PixelFormat format)227 bool IsPixelFormatASTC(PixelFormat format) {
228     switch (format) {
229     case PixelFormat::ASTC_2D_4X4_UNORM:
230     case PixelFormat::ASTC_2D_5X4_UNORM:
231     case PixelFormat::ASTC_2D_5X5_UNORM:
232     case PixelFormat::ASTC_2D_8X8_UNORM:
233     case PixelFormat::ASTC_2D_8X5_UNORM:
234     case PixelFormat::ASTC_2D_4X4_SRGB:
235     case PixelFormat::ASTC_2D_5X4_SRGB:
236     case PixelFormat::ASTC_2D_5X5_SRGB:
237     case PixelFormat::ASTC_2D_8X8_SRGB:
238     case PixelFormat::ASTC_2D_8X5_SRGB:
239     case PixelFormat::ASTC_2D_10X8_UNORM:
240     case PixelFormat::ASTC_2D_10X8_SRGB:
241     case PixelFormat::ASTC_2D_6X6_UNORM:
242     case PixelFormat::ASTC_2D_6X6_SRGB:
243     case PixelFormat::ASTC_2D_10X10_UNORM:
244     case PixelFormat::ASTC_2D_10X10_SRGB:
245     case PixelFormat::ASTC_2D_12X12_UNORM:
246     case PixelFormat::ASTC_2D_12X12_SRGB:
247     case PixelFormat::ASTC_2D_8X6_UNORM:
248     case PixelFormat::ASTC_2D_8X6_SRGB:
249     case PixelFormat::ASTC_2D_6X5_UNORM:
250     case PixelFormat::ASTC_2D_6X5_SRGB:
251         return true;
252     default:
253         return false;
254     }
255 }
256 
IsPixelFormatSRGB(PixelFormat format)257 bool IsPixelFormatSRGB(PixelFormat format) {
258     switch (format) {
259     case PixelFormat::A8B8G8R8_SRGB:
260     case PixelFormat::B8G8R8A8_SRGB:
261     case PixelFormat::BC1_RGBA_SRGB:
262     case PixelFormat::BC2_SRGB:
263     case PixelFormat::BC3_SRGB:
264     case PixelFormat::BC7_SRGB:
265     case PixelFormat::ASTC_2D_4X4_SRGB:
266     case PixelFormat::ASTC_2D_8X8_SRGB:
267     case PixelFormat::ASTC_2D_8X5_SRGB:
268     case PixelFormat::ASTC_2D_5X4_SRGB:
269     case PixelFormat::ASTC_2D_5X5_SRGB:
270     case PixelFormat::ASTC_2D_10X8_SRGB:
271     case PixelFormat::ASTC_2D_6X6_SRGB:
272     case PixelFormat::ASTC_2D_10X10_SRGB:
273     case PixelFormat::ASTC_2D_12X12_SRGB:
274     case PixelFormat::ASTC_2D_8X6_SRGB:
275     case PixelFormat::ASTC_2D_6X5_SRGB:
276         return true;
277     default:
278         return false;
279     }
280 }
281 
GetASTCBlockSize(PixelFormat format)282 std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
283     return {GetDefaultBlockWidth(format), GetDefaultBlockHeight(format)};
284 }
285 
286 } // namespace VideoCore::Surface
287