summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2019-11-09 03:26:30 -0300
committerGravatar ReinUsesLisp2019-11-14 20:57:10 -0300
commit80eacdf89b55528a66b2e94391e640e641e8cb57 (patch)
tree38f52e7744ba4db1aba6252837459b1ca72fa465 /src
parenttexture_cache: Drop abstracted ComponentType (diff)
downloadyuzu-80eacdf89b55528a66b2e94391e640e641e8cb57.tar.gz
yuzu-80eacdf89b55528a66b2e94391e640e641e8cb57.tar.xz
yuzu-80eacdf89b55528a66b2e94391e640e641e8cb57.zip
texture_cache: Use a table instead of switch for texture formats
Use a large flat array to look up texture formats. This allows us to properly implement formats with different component types. It should also be faster.
Diffstat (limited to 'src')
-rw-r--r--src/video_core/CMakeLists.txt2
-rw-r--r--src/video_core/engines/maxwell_3d.cpp8
-rw-r--r--src/video_core/surface.cpp236
-rw-r--r--src/video_core/surface.h5
-rw-r--r--src/video_core/texture_cache/format_lookup_table.cpp214
-rw-r--r--src/video_core/texture_cache/format_lookup_table.h51
-rw-r--r--src/video_core/texture_cache/surface_params.cpp20
-rw-r--r--src/video_core/texture_cache/surface_params.h8
-rw-r--r--src/video_core/texture_cache/texture_cache.h7
9 files changed, 290 insertions, 261 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index 45d8eaf23..6f3f2aa9f 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -127,6 +127,8 @@ add_library(video_core STATIC
127 shader/track.cpp 127 shader/track.cpp
128 surface.cpp 128 surface.cpp
129 surface.h 129 surface.h
130 texture_cache/format_lookup_table.cpp
131 texture_cache/format_lookup_table.h
130 texture_cache/surface_base.cpp 132 texture_cache/surface_base.cpp
131 texture_cache/surface_base.h 133 texture_cache/surface_base.h
132 texture_cache/surface_params.cpp 134 texture_cache/surface_params.cpp
diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp
index 42ce49a4d..a44c09003 100644
--- a/src/video_core/engines/maxwell_3d.cpp
+++ b/src/video_core/engines/maxwell_3d.cpp
@@ -742,14 +742,6 @@ Texture::TICEntry Maxwell3D::GetTICEntry(u32 tic_index) const {
742 Texture::TICEntry tic_entry; 742 Texture::TICEntry tic_entry;
743 memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry)); 743 memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
744 744
745 [[maybe_unused]] const auto r_type{tic_entry.r_type.Value()};
746 [[maybe_unused]] const auto g_type{tic_entry.g_type.Value()};
747 [[maybe_unused]] const auto b_type{tic_entry.b_type.Value()};
748 [[maybe_unused]] const auto a_type{tic_entry.a_type.Value()};
749
750 // TODO(Subv): Different data types for separate components are not supported
751 DEBUG_ASSERT(r_type == g_type && r_type == b_type && r_type == a_type);
752
753 return tic_entry; 745 return tic_entry;
754} 746}
755 747
diff --git a/src/video_core/surface.cpp b/src/video_core/surface.cpp
index 20bcda6bc..1655ccf16 100644
--- a/src/video_core/surface.cpp
+++ b/src/video_core/surface.cpp
@@ -168,242 +168,6 @@ PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format)
168 } 168 }
169} 169}
170 170
171PixelFormat PixelFormatFromTextureFormat(Tegra::Texture::TextureFormat format,
172 Tegra::Texture::ComponentType component_type,
173 bool is_srgb) {
174 // TODO(Subv): Properly implement this
175 switch (format) {
176 case Tegra::Texture::TextureFormat::A8R8G8B8:
177 if (is_srgb) {
178 return PixelFormat::RGBA8_SRGB;
179 }
180 switch (component_type) {
181 case Tegra::Texture::ComponentType::UNORM:
182 return PixelFormat::ABGR8U;
183 case Tegra::Texture::ComponentType::SNORM:
184 return PixelFormat::ABGR8S;
185 case Tegra::Texture::ComponentType::UINT:
186 return PixelFormat::ABGR8UI;
187 default:
188 break;
189 }
190 break;
191 case Tegra::Texture::TextureFormat::B5G6R5:
192 switch (component_type) {
193 case Tegra::Texture::ComponentType::UNORM:
194 return PixelFormat::B5G6R5U;
195 default:
196 break;
197 }
198 break;
199 case Tegra::Texture::TextureFormat::A2B10G10R10:
200 switch (component_type) {
201 case Tegra::Texture::ComponentType::UNORM:
202 return PixelFormat::A2B10G10R10U;
203 default:
204 break;
205 }
206 break;
207 case Tegra::Texture::TextureFormat::A1B5G5R5:
208 switch (component_type) {
209 case Tegra::Texture::ComponentType::UNORM:
210 return PixelFormat::A1B5G5R5U;
211 default:
212 break;
213 }
214 break;
215 case Tegra::Texture::TextureFormat::A4B4G4R4:
216 switch (component_type) {
217 case Tegra::Texture::ComponentType::UNORM:
218 return PixelFormat::R4G4B4A4U;
219 default:
220 break;
221 }
222 break;
223 case Tegra::Texture::TextureFormat::R8:
224 switch (component_type) {
225 case Tegra::Texture::ComponentType::UNORM:
226 return PixelFormat::R8U;
227 case Tegra::Texture::ComponentType::UINT:
228 return PixelFormat::R8UI;
229 default:
230 break;
231 }
232 break;
233 case Tegra::Texture::TextureFormat::G8R8:
234 // TextureFormat::G8R8 is actually ordered red then green, as such we can use
235 // PixelFormat::RG8U and PixelFormat::RG8S. This was tested with The Legend of Zelda: Breath
236 // of the Wild, which uses this format to render the hearts on the UI.
237 switch (component_type) {
238 case Tegra::Texture::ComponentType::UNORM:
239 return PixelFormat::RG8U;
240 case Tegra::Texture::ComponentType::SNORM:
241 return PixelFormat::RG8S;
242 default:
243 break;
244 }
245 break;
246 case Tegra::Texture::TextureFormat::R16_G16_B16_A16:
247 switch (component_type) {
248 case Tegra::Texture::ComponentType::UNORM:
249 return PixelFormat::RGBA16U;
250 case Tegra::Texture::ComponentType::FLOAT:
251 return PixelFormat::RGBA16F;
252 case Tegra::Texture::ComponentType::UINT:
253 return PixelFormat::RGBA16UI;
254 default:
255 break;
256 }
257 break;
258 case Tegra::Texture::TextureFormat::BF10GF11RF11:
259 switch (component_type) {
260 case Tegra::Texture::ComponentType::FLOAT:
261 return PixelFormat::R11FG11FB10F;
262 default:
263 break;
264 }
265 break;
266 case Tegra::Texture::TextureFormat::R32_G32_B32_A32:
267 switch (component_type) {
268 case Tegra::Texture::ComponentType::FLOAT:
269 return PixelFormat::RGBA32F;
270 case Tegra::Texture::ComponentType::UINT:
271 return PixelFormat::RGBA32UI;
272 default:
273 break;
274 }
275 break;
276 case Tegra::Texture::TextureFormat::R32_G32:
277 switch (component_type) {
278 case Tegra::Texture::ComponentType::FLOAT:
279 return PixelFormat::RG32F;
280 case Tegra::Texture::ComponentType::UINT:
281 return PixelFormat::RG32UI;
282 default:
283 break;
284 }
285 break;
286 case Tegra::Texture::TextureFormat::R32_G32_B32:
287 switch (component_type) {
288 case Tegra::Texture::ComponentType::FLOAT:
289 return PixelFormat::RGB32F;
290 default:
291 break;
292 }
293 break;
294 case Tegra::Texture::TextureFormat::R16:
295 switch (component_type) {
296 case Tegra::Texture::ComponentType::FLOAT:
297 return PixelFormat::R16F;
298 case Tegra::Texture::ComponentType::UNORM:
299 return PixelFormat::R16U;
300 case Tegra::Texture::ComponentType::SNORM:
301 return PixelFormat::R16S;
302 case Tegra::Texture::ComponentType::UINT:
303 return PixelFormat::R16UI;
304 case Tegra::Texture::ComponentType::SINT:
305 return PixelFormat::R16I;
306 default:
307 break;
308 }
309 break;
310 case Tegra::Texture::TextureFormat::R32:
311 switch (component_type) {
312 case Tegra::Texture::ComponentType::FLOAT:
313 return PixelFormat::R32F;
314 case Tegra::Texture::ComponentType::UINT:
315 return PixelFormat::R32UI;
316 default:
317 break;
318 }
319 break;
320 case Tegra::Texture::TextureFormat::E5B9G9R9_SHAREDEXP:
321 switch (component_type) {
322 case Tegra::Texture::ComponentType::FLOAT:
323 return PixelFormat::E5B9G9R9F;
324 default:
325 break;
326 }
327 break;
328 case Tegra::Texture::TextureFormat::ZF32:
329 return PixelFormat::Z32F;
330 case Tegra::Texture::TextureFormat::Z16:
331 return PixelFormat::Z16;
332 case Tegra::Texture::TextureFormat::S8Z24:
333 return PixelFormat::S8Z24;
334 case Tegra::Texture::TextureFormat::ZF32_X24S8:
335 return PixelFormat::Z32FS8;
336 case Tegra::Texture::TextureFormat::DXT1:
337 return is_srgb ? PixelFormat::DXT1_SRGB : PixelFormat::DXT1;
338 case Tegra::Texture::TextureFormat::DXT23:
339 return is_srgb ? PixelFormat::DXT23_SRGB : PixelFormat::DXT23;
340 case Tegra::Texture::TextureFormat::DXT45:
341 return is_srgb ? PixelFormat::DXT45_SRGB : PixelFormat::DXT45;
342 case Tegra::Texture::TextureFormat::DXN1:
343 return PixelFormat::DXN1;
344 case Tegra::Texture::TextureFormat::DXN2:
345 switch (component_type) {
346 case Tegra::Texture::ComponentType::UNORM:
347 return PixelFormat::DXN2UNORM;
348 case Tegra::Texture::ComponentType::SNORM:
349 return PixelFormat::DXN2SNORM;
350 default:
351 break;
352 }
353 break;
354 case Tegra::Texture::TextureFormat::BC7U:
355 return is_srgb ? PixelFormat::BC7U_SRGB : PixelFormat::BC7U;
356 case Tegra::Texture::TextureFormat::BC6H_UF16:
357 return PixelFormat::BC6H_UF16;
358 case Tegra::Texture::TextureFormat::BC6H_SF16:
359 return PixelFormat::BC6H_SF16;
360 case Tegra::Texture::TextureFormat::ASTC_2D_4X4:
361 return is_srgb ? PixelFormat::ASTC_2D_4X4_SRGB : PixelFormat::ASTC_2D_4X4;
362 case Tegra::Texture::TextureFormat::ASTC_2D_5X4:
363 return is_srgb ? PixelFormat::ASTC_2D_5X4_SRGB : PixelFormat::ASTC_2D_5X4;
364 case Tegra::Texture::TextureFormat::ASTC_2D_5X5:
365 return is_srgb ? PixelFormat::ASTC_2D_5X5_SRGB : PixelFormat::ASTC_2D_5X5;
366 case Tegra::Texture::TextureFormat::ASTC_2D_8X8:
367 return is_srgb ? PixelFormat::ASTC_2D_8X8_SRGB : PixelFormat::ASTC_2D_8X8;
368 case Tegra::Texture::TextureFormat::ASTC_2D_8X5:
369 return is_srgb ? PixelFormat::ASTC_2D_8X5_SRGB : PixelFormat::ASTC_2D_8X5;
370 case Tegra::Texture::TextureFormat::ASTC_2D_10X8:
371 return is_srgb ? PixelFormat::ASTC_2D_10X8_SRGB : PixelFormat::ASTC_2D_10X8;
372 case Tegra::Texture::TextureFormat::ASTC_2D_6X6:
373 return is_srgb ? PixelFormat::ASTC_2D_6X6_SRGB : PixelFormat::ASTC_2D_6X6;
374 case Tegra::Texture::TextureFormat::ASTC_2D_10X10:
375 return is_srgb ? PixelFormat::ASTC_2D_10X10_SRGB : PixelFormat::ASTC_2D_10X10;
376 case Tegra::Texture::TextureFormat::ASTC_2D_12X12:
377 return is_srgb ? PixelFormat::ASTC_2D_12X12_SRGB : PixelFormat::ASTC_2D_12X12;
378 case Tegra::Texture::TextureFormat::ASTC_2D_8X6:
379 return is_srgb ? PixelFormat::ASTC_2D_8X6_SRGB : PixelFormat::ASTC_2D_8X6;
380 case Tegra::Texture::TextureFormat::ASTC_2D_6X5:
381 return is_srgb ? PixelFormat::ASTC_2D_6X5_SRGB : PixelFormat::ASTC_2D_6X5;
382 case Tegra::Texture::TextureFormat::R16_G16:
383 switch (component_type) {
384 case Tegra::Texture::ComponentType::FLOAT:
385 return PixelFormat::RG16F;
386 case Tegra::Texture::ComponentType::UNORM:
387 return PixelFormat::RG16;
388 case Tegra::Texture::ComponentType::SNORM:
389 return PixelFormat::RG16S;
390 case Tegra::Texture::ComponentType::UINT:
391 return PixelFormat::RG16UI;
392 case Tegra::Texture::ComponentType::SINT:
393 return PixelFormat::RG16I;
394 default:
395 break;
396 }
397 break;
398 default:
399 break;
400 }
401 LOG_CRITICAL(HW_GPU, "Unimplemented format={}, component_type={}", static_cast<u32>(format),
402 static_cast<u32>(component_type));
403 UNREACHABLE();
404 return PixelFormat::ABGR8U;
405}
406
407PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format) { 171PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format) {
408 switch (format) { 172 switch (format) {
409 case Tegra::FramebufferConfig::PixelFormat::ABGR8: 173 case Tegra::FramebufferConfig::PixelFormat::ABGR8:
diff --git a/src/video_core/surface.h b/src/video_core/surface.h
index a3bf2a5b2..0d17a93ed 100644
--- a/src/video_core/surface.h
+++ b/src/video_core/surface.h
@@ -106,7 +106,6 @@ enum class PixelFormat {
106 Max = MaxDepthStencilFormat, 106 Max = MaxDepthStencilFormat,
107 Invalid = 255, 107 Invalid = 255,
108}; 108};
109
110static constexpr std::size_t MaxPixelFormat = static_cast<std::size_t>(PixelFormat::Max); 109static constexpr std::size_t MaxPixelFormat = static_cast<std::size_t>(PixelFormat::Max);
111 110
112enum class SurfaceType { 111enum class SurfaceType {
@@ -600,10 +599,6 @@ PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format);
600 599
601PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format); 600PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format);
602 601
603PixelFormat PixelFormatFromTextureFormat(Tegra::Texture::TextureFormat format,
604 Tegra::Texture::ComponentType component_type,
605 bool is_srgb);
606
607PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format); 602PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format);
608 603
609SurfaceType GetFormatType(PixelFormat pixel_format); 604SurfaceType GetFormatType(PixelFormat pixel_format);
diff --git a/src/video_core/texture_cache/format_lookup_table.cpp b/src/video_core/texture_cache/format_lookup_table.cpp
new file mode 100644
index 000000000..ee050cc31
--- /dev/null
+++ b/src/video_core/texture_cache/format_lookup_table.cpp
@@ -0,0 +1,214 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include "common/common_types.h"
7#include "common/logging/log.h"
8#include "video_core/texture_cache/format_lookup_table.h"
9
10namespace VideoCommon {
11
12using Tegra::Texture::ComponentType;
13using Tegra::Texture::TextureFormat;
14using VideoCore::Surface::PixelFormat;
15
16namespace {
17
18static constexpr auto SNORM = ComponentType::SNORM;
19static constexpr auto UNORM = ComponentType::UNORM;
20static constexpr auto SINT = ComponentType::SINT;
21static constexpr auto UINT = ComponentType::UINT;
22static constexpr auto SNORM_FORCE_FP16 = ComponentType::SNORM_FORCE_FP16;
23static constexpr auto UNORM_FORCE_FP16 = ComponentType::UNORM_FORCE_FP16;
24static constexpr auto FLOAT = ComponentType::FLOAT;
25static constexpr bool C = false; // Normal color
26static constexpr bool S = true; // Srgb
27
28struct Table {
29 constexpr Table(TextureFormat texture_format, bool is_srgb, ComponentType red_component,
30 ComponentType green_component, ComponentType blue_component,
31 ComponentType alpha_component, PixelFormat pixel_format)
32 : texture_format{static_cast<u32>(texture_format)},
33 pixel_format{static_cast<u32>(pixel_format)}, red_component{static_cast<u32>(
34 red_component)},
35 green_component{static_cast<u32>(green_component)}, blue_component{static_cast<u32>(
36 blue_component)},
37 alpha_component{static_cast<u32>(alpha_component)}, is_srgb{is_srgb ? 1U : 0U} {}
38
39 u32 texture_format : 8;
40 u32 pixel_format : 8;
41 u32 red_component : 3;
42 u32 green_component : 3;
43 u32 blue_component : 3;
44 u32 alpha_component : 3;
45 u32 is_srgb : 1;
46};
47constexpr std::array<Table, 74> DefinitionTable = {{
48 {TextureFormat::A8R8G8B8, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ABGR8U},
49 {TextureFormat::A8R8G8B8, C, SNORM, SNORM, SNORM, SNORM, PixelFormat::ABGR8S},
50 {TextureFormat::A8R8G8B8, C, UINT, UINT, UINT, UINT, PixelFormat::ABGR8UI},
51 {TextureFormat::A8R8G8B8, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::RGBA8_SRGB},
52
53 {TextureFormat::B5G6R5, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::B5G6R5U},
54
55 {TextureFormat::A2B10G10R10, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::A2B10G10R10U},
56
57 {TextureFormat::A1B5G5R5, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::A1B5G5R5U},
58
59 {TextureFormat::A4B4G4R4, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::R4G4B4A4U},
60
61 {TextureFormat::R8, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::R8U},
62 {TextureFormat::R8, C, UINT, UINT, UINT, UINT, PixelFormat::R8UI},
63
64 {TextureFormat::G8R8, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::RG8U},
65 {TextureFormat::G8R8, C, SNORM, SNORM, SNORM, SNORM, PixelFormat::RG8S},
66
67 {TextureFormat::R16_G16_B16_A16, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::RGBA16U},
68 {TextureFormat::R16_G16_B16_A16, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::RGBA16F},
69 {TextureFormat::R16_G16_B16_A16, C, UINT, UINT, UINT, UINT, PixelFormat::RGBA16UI},
70
71 {TextureFormat::R16_G16, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::RG16F},
72 {TextureFormat::R16_G16, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::RG16},
73 {TextureFormat::R16_G16, C, SNORM, SNORM, SNORM, SNORM, PixelFormat::RG16S},
74 {TextureFormat::R16_G16, C, UINT, UINT, UINT, UINT, PixelFormat::RG16UI},
75 {TextureFormat::R16_G16, C, SINT, SINT, SINT, SINT, PixelFormat::RG16I},
76
77 {TextureFormat::R16, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::R16F},
78 {TextureFormat::R16, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::R16U},
79 {TextureFormat::R16, C, SNORM, SNORM, SNORM, SNORM, PixelFormat::R16S},
80 {TextureFormat::R16, C, UINT, UINT, UINT, UINT, PixelFormat::R16UI},
81 {TextureFormat::R16, C, SINT, SINT, SINT, SINT, PixelFormat::R16I},
82
83 {TextureFormat::BF10GF11RF11, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::R11FG11FB10F},
84
85 {TextureFormat::R32_G32_B32_A32, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::RGBA32F},
86 {TextureFormat::R32_G32_B32_A32, C, UINT, UINT, UINT, UINT, PixelFormat::RGBA32UI},
87
88 {TextureFormat::R32_G32_B32, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::RGB32F},
89
90 {TextureFormat::R32_G32, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::RG32F},
91 {TextureFormat::R32_G32, C, UINT, UINT, UINT, UINT, PixelFormat::RG32UI},
92
93 {TextureFormat::R32, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::R32F},
94 {TextureFormat::R32, C, UINT, UINT, UINT, UINT, PixelFormat::R32UI},
95
96 {TextureFormat::E5B9G9R9_SHAREDEXP, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::E5B9G9R9F},
97
98 {TextureFormat::ZF32, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::Z32F},
99 {TextureFormat::Z16, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::Z16},
100 {TextureFormat::S8Z24, C, UINT, UNORM, UNORM, UNORM, PixelFormat::S8Z24},
101 {TextureFormat::ZF32_X24S8, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::Z32FS8},
102
103 {TextureFormat::DXT1, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXT1},
104 {TextureFormat::DXT1, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXT1_SRGB},
105
106 {TextureFormat::DXT23, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXT23},
107 {TextureFormat::DXT23, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXT23_SRGB},
108
109 {TextureFormat::DXT45, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXT45},
110 {TextureFormat::DXT45, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXT45_SRGB},
111
112 // TODO: Use a different pixel format for SNORM
113 {TextureFormat::DXN1, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXN1},
114 {TextureFormat::DXN1, C, SNORM, SNORM, SNORM, SNORM, PixelFormat::DXN1},
115
116 {TextureFormat::DXN2, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::DXN2UNORM},
117 {TextureFormat::DXN2, C, SNORM, SNORM, SNORM, SNORM, PixelFormat::DXN2SNORM},
118
119 {TextureFormat::BC7U, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::BC7U},
120 {TextureFormat::BC7U, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::BC7U_SRGB},
121
122 {TextureFormat::BC6H_SF16, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::BC6H_SF16},
123 {TextureFormat::BC6H_UF16, C, FLOAT, FLOAT, FLOAT, FLOAT, PixelFormat::BC6H_UF16},
124
125 {TextureFormat::ASTC_2D_4X4, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_4X4},
126 {TextureFormat::ASTC_2D_4X4, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_4X4_SRGB},
127
128 {TextureFormat::ASTC_2D_5X4, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_5X4},
129 {TextureFormat::ASTC_2D_5X4, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_5X4_SRGB},
130
131 {TextureFormat::ASTC_2D_5X5, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_5X5},
132 {TextureFormat::ASTC_2D_5X5, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_5X5_SRGB},
133
134 {TextureFormat::ASTC_2D_8X8, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_8X8},
135 {TextureFormat::ASTC_2D_8X8, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_8X8_SRGB},
136
137 {TextureFormat::ASTC_2D_8X5, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_8X5},
138 {TextureFormat::ASTC_2D_8X5, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_8X5_SRGB},
139
140 {TextureFormat::ASTC_2D_10X8, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_10X8},
141 {TextureFormat::ASTC_2D_10X8, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_10X8_SRGB},
142
143 {TextureFormat::ASTC_2D_6X6, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_6X6},
144 {TextureFormat::ASTC_2D_6X6, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_6X6_SRGB},
145
146 {TextureFormat::ASTC_2D_10X10, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_10X10},
147 {TextureFormat::ASTC_2D_10X10, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_10X10_SRGB},
148
149 {TextureFormat::ASTC_2D_12X12, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_12X12},
150 {TextureFormat::ASTC_2D_12X12, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_12X12_SRGB},
151
152 {TextureFormat::ASTC_2D_8X6, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_8X6},
153 {TextureFormat::ASTC_2D_8X6, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_8X6_SRGB},
154
155 {TextureFormat::ASTC_2D_6X5, C, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_6X5},
156 {TextureFormat::ASTC_2D_6X5, S, UNORM, UNORM, UNORM, UNORM, PixelFormat::ASTC_2D_6X5_SRGB},
157}};
158
159} // Anonymous namespace
160
161FormatLookupTable::FormatLookupTable() {
162 table.fill(static_cast<u8>(PixelFormat::Invalid));
163
164 for (const auto entry : DefinitionTable) {
165 table[CalculateIndex(static_cast<TextureFormat>(entry.texture_format), entry.is_srgb != 0,
166 static_cast<ComponentType>(entry.red_component),
167 static_cast<ComponentType>(entry.green_component),
168 static_cast<ComponentType>(entry.blue_component),
169 static_cast<ComponentType>(entry.alpha_component))] =
170 static_cast<u8>(entry.pixel_format);
171 }
172}
173
174PixelFormat FormatLookupTable::GetPixelFormat(TextureFormat format, bool is_srgb,
175 ComponentType red_component,
176 ComponentType green_component,
177 ComponentType blue_component,
178 ComponentType alpha_component) const noexcept {
179 const auto pixel_format = static_cast<PixelFormat>(table[CalculateIndex(
180 format, is_srgb, red_component, green_component, blue_component, alpha_component)]);
181 // [[likely]]
182 if (pixel_format != PixelFormat::Invalid) {
183 return pixel_format;
184 }
185 UNIMPLEMENTED_MSG("texture format={} srgb={} components={{{} {} {} {}}}",
186 static_cast<int>(format), is_srgb, static_cast<int>(red_component),
187 static_cast<int>(green_component), static_cast<int>(blue_component),
188 static_cast<int>(alpha_component));
189 return PixelFormat::ABGR8U;
190}
191
192void FormatLookupTable::Set(TextureFormat format, bool is_srgb, ComponentType red_component,
193 ComponentType green_component, ComponentType blue_component,
194 ComponentType alpha_component, PixelFormat pixel_format) {}
195
196std::size_t FormatLookupTable::CalculateIndex(TextureFormat format, bool is_srgb,
197 ComponentType red_component,
198 ComponentType green_component,
199 ComponentType blue_component,
200 ComponentType alpha_component) noexcept {
201 const auto format_index = static_cast<std::size_t>(format);
202 const auto red_index = static_cast<std::size_t>(red_component);
203 const auto green_index = static_cast<std::size_t>(red_component);
204 const auto blue_index = static_cast<std::size_t>(red_component);
205 const auto alpha_index = static_cast<std::size_t>(red_component);
206 const std::size_t srgb_index = is_srgb ? 1 : 0;
207
208 return format_index * PerFormat +
209 srgb_index * PerComponent * PerComponent * PerComponent * PerComponent +
210 alpha_index * PerComponent * PerComponent * PerComponent +
211 blue_index * PerComponent * PerComponent + green_index * PerComponent + red_index;
212}
213
214} // namespace VideoCommon
diff --git a/src/video_core/texture_cache/format_lookup_table.h b/src/video_core/texture_cache/format_lookup_table.h
new file mode 100644
index 000000000..8da7481cd
--- /dev/null
+++ b/src/video_core/texture_cache/format_lookup_table.h
@@ -0,0 +1,51 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <numeric>
9#include "video_core/surface.h"
10#include "video_core/textures/texture.h"
11
12namespace VideoCommon {
13
14class FormatLookupTable {
15public:
16 explicit FormatLookupTable();
17
18 VideoCore::Surface::PixelFormat GetPixelFormat(
19 Tegra::Texture::TextureFormat format, bool is_srgb,
20 Tegra::Texture::ComponentType red_component, Tegra::Texture::ComponentType green_component,
21 Tegra::Texture::ComponentType blue_component,
22 Tegra::Texture::ComponentType alpha_component) const noexcept;
23
24private:
25 static_assert(VideoCore::Surface::MaxPixelFormat <= std::numeric_limits<u8>::max());
26
27 static constexpr std::size_t NumTextureFormats = 128;
28
29 static constexpr std::size_t PerComponent = 8;
30 static constexpr std::size_t PerComponents2 = PerComponent * PerComponent;
31 static constexpr std::size_t PerComponents3 = PerComponents2 * PerComponent;
32 static constexpr std::size_t PerComponents4 = PerComponents3 * PerComponent;
33 static constexpr std::size_t PerFormat = PerComponents4 * 2;
34
35 static std::size_t CalculateIndex(Tegra::Texture::TextureFormat format, bool is_srgb,
36 Tegra::Texture::ComponentType red_component,
37 Tegra::Texture::ComponentType green_component,
38 Tegra::Texture::ComponentType blue_component,
39 Tegra::Texture::ComponentType alpha_component) noexcept;
40
41 void Set(Tegra::Texture::TextureFormat format, bool is_srgb,
42 Tegra::Texture::ComponentType red_component,
43 Tegra::Texture::ComponentType green_component,
44 Tegra::Texture::ComponentType blue_component,
45 Tegra::Texture::ComponentType alpha_component,
46 VideoCore::Surface::PixelFormat pixel_format);
47
48 std::array<u8, NumTextureFormats * PerFormat> table;
49};
50
51} // namespace VideoCommon
diff --git a/src/video_core/texture_cache/surface_params.cpp b/src/video_core/texture_cache/surface_params.cpp
index 04b5e8ba3..858e17e08 100644
--- a/src/video_core/texture_cache/surface_params.cpp
+++ b/src/video_core/texture_cache/surface_params.cpp
@@ -2,13 +2,16 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <map> 5#include <algorithm>
6#include <string>
7#include <tuple>
6 8
7#include "common/alignment.h" 9#include "common/alignment.h"
8#include "common/bit_util.h" 10#include "common/bit_util.h"
9#include "core/core.h" 11#include "core/core.h"
10#include "video_core/engines/shader_bytecode.h" 12#include "video_core/engines/shader_bytecode.h"
11#include "video_core/surface.h" 13#include "video_core/surface.h"
14#include "video_core/texture_cache/format_lookup_table.h"
12#include "video_core/texture_cache/surface_params.h" 15#include "video_core/texture_cache/surface_params.h"
13 16
14namespace VideoCommon { 17namespace VideoCommon {
@@ -16,7 +19,6 @@ namespace VideoCommon {
16using VideoCore::Surface::PixelFormat; 19using VideoCore::Surface::PixelFormat;
17using VideoCore::Surface::PixelFormatFromDepthFormat; 20using VideoCore::Surface::PixelFormatFromDepthFormat;
18using VideoCore::Surface::PixelFormatFromRenderTargetFormat; 21using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
19using VideoCore::Surface::PixelFormatFromTextureFormat;
20using VideoCore::Surface::SurfaceTarget; 22using VideoCore::Surface::SurfaceTarget;
21using VideoCore::Surface::SurfaceTargetFromTextureType; 23using VideoCore::Surface::SurfaceTargetFromTextureType;
22using VideoCore::Surface::SurfaceType; 24using VideoCore::Surface::SurfaceType;
@@ -66,7 +68,8 @@ constexpr u32 GetMipmapSize(bool uncompressed, u32 mip_size, u32 tile) {
66 68
67} // Anonymous namespace 69} // Anonymous namespace
68 70
69SurfaceParams SurfaceParams::CreateForTexture(const Tegra::Texture::TICEntry& tic, 71SurfaceParams SurfaceParams::CreateForTexture(const FormatLookupTable& lookup_table,
72 const Tegra::Texture::TICEntry& tic,
70 const VideoCommon::Shader::Sampler& entry) { 73 const VideoCommon::Shader::Sampler& entry) {
71 SurfaceParams params; 74 SurfaceParams params;
72 params.is_tiled = tic.IsTiled(); 75 params.is_tiled = tic.IsTiled();
@@ -75,8 +78,8 @@ SurfaceParams SurfaceParams::CreateForTexture(const Tegra::Texture::TICEntry& ti
75 params.block_height = params.is_tiled ? tic.BlockHeight() : 0, 78 params.block_height = params.is_tiled ? tic.BlockHeight() : 0,
76 params.block_depth = params.is_tiled ? tic.BlockDepth() : 0, 79 params.block_depth = params.is_tiled ? tic.BlockDepth() : 0,
77 params.tile_width_spacing = params.is_tiled ? (1 << tic.tile_width_spacing.Value()) : 1; 80 params.tile_width_spacing = params.is_tiled ? (1 << tic.tile_width_spacing.Value()) : 1;
78 params.pixel_format = 81 params.pixel_format = lookup_table.GetPixelFormat(
79 PixelFormatFromTextureFormat(tic.format, tic.r_type.Value(), params.srgb_conversion); 82 tic.format, params.srgb_conversion, tic.r_type, tic.g_type, tic.b_type, tic.a_type);
80 params.type = GetFormatType(params.pixel_format); 83 params.type = GetFormatType(params.pixel_format);
81 if (entry.IsShadow() && params.type == SurfaceType::ColorTexture) { 84 if (entry.IsShadow() && params.type == SurfaceType::ColorTexture) {
82 switch (params.pixel_format) { 85 switch (params.pixel_format) {
@@ -124,7 +127,8 @@ SurfaceParams SurfaceParams::CreateForTexture(const Tegra::Texture::TICEntry& ti
124 return params; 127 return params;
125} 128}
126 129
127SurfaceParams SurfaceParams::CreateForImage(const Tegra::Texture::TICEntry& tic, 130SurfaceParams SurfaceParams::CreateForImage(const FormatLookupTable& lookup_table,
131 const Tegra::Texture::TICEntry& tic,
128 const VideoCommon::Shader::Image& entry) { 132 const VideoCommon::Shader::Image& entry) {
129 SurfaceParams params; 133 SurfaceParams params;
130 params.is_tiled = tic.IsTiled(); 134 params.is_tiled = tic.IsTiled();
@@ -133,8 +137,8 @@ SurfaceParams SurfaceParams::CreateForImage(const Tegra::Texture::TICEntry& tic,
133 params.block_height = params.is_tiled ? tic.BlockHeight() : 0, 137 params.block_height = params.is_tiled ? tic.BlockHeight() : 0,
134 params.block_depth = params.is_tiled ? tic.BlockDepth() : 0, 138 params.block_depth = params.is_tiled ? tic.BlockDepth() : 0,
135 params.tile_width_spacing = params.is_tiled ? (1 << tic.tile_width_spacing.Value()) : 1; 139 params.tile_width_spacing = params.is_tiled ? (1 << tic.tile_width_spacing.Value()) : 1;
136 params.pixel_format = 140 params.pixel_format = lookup_table.GetPixelFormat(
137 PixelFormatFromTextureFormat(tic.format, tic.r_type.Value(), params.srgb_conversion); 141 tic.format, params.srgb_conversion, tic.r_type, tic.g_type, tic.b_type, tic.a_type);
138 params.type = GetFormatType(params.pixel_format); 142 params.type = GetFormatType(params.pixel_format);
139 params.type = GetFormatType(params.pixel_format); 143 params.type = GetFormatType(params.pixel_format);
140 params.target = ImageTypeToSurfaceTarget(entry.GetType()); 144 params.target = ImageTypeToSurfaceTarget(entry.GetType());
diff --git a/src/video_core/texture_cache/surface_params.h b/src/video_core/texture_cache/surface_params.h
index a84058869..709aa0dc2 100644
--- a/src/video_core/texture_cache/surface_params.h
+++ b/src/video_core/texture_cache/surface_params.h
@@ -16,16 +16,20 @@
16 16
17namespace VideoCommon { 17namespace VideoCommon {
18 18
19class FormatLookupTable;
20
19using VideoCore::Surface::SurfaceCompression; 21using VideoCore::Surface::SurfaceCompression;
20 22
21class SurfaceParams { 23class SurfaceParams {
22public: 24public:
23 /// Creates SurfaceCachedParams from a texture configuration. 25 /// Creates SurfaceCachedParams from a texture configuration.
24 static SurfaceParams CreateForTexture(const Tegra::Texture::TICEntry& tic, 26 static SurfaceParams CreateForTexture(const FormatLookupTable& lookup_table,
27 const Tegra::Texture::TICEntry& tic,
25 const VideoCommon::Shader::Sampler& entry); 28 const VideoCommon::Shader::Sampler& entry);
26 29
27 /// Creates SurfaceCachedParams from an image configuration. 30 /// Creates SurfaceCachedParams from an image configuration.
28 static SurfaceParams CreateForImage(const Tegra::Texture::TICEntry& tic, 31 static SurfaceParams CreateForImage(const FormatLookupTable& lookup_table,
32 const Tegra::Texture::TICEntry& tic,
29 const VideoCommon::Shader::Image& entry); 33 const VideoCommon::Shader::Image& entry);
30 34
31 /// Creates SurfaceCachedParams for a depth buffer configuration. 35 /// Creates SurfaceCachedParams for a depth buffer configuration.
diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h
index 8074cc945..41309ebea 100644
--- a/src/video_core/texture_cache/texture_cache.h
+++ b/src/video_core/texture_cache/texture_cache.h
@@ -29,6 +29,7 @@
29#include "video_core/rasterizer_interface.h" 29#include "video_core/rasterizer_interface.h"
30#include "video_core/surface.h" 30#include "video_core/surface.h"
31#include "video_core/texture_cache/copy_params.h" 31#include "video_core/texture_cache/copy_params.h"
32#include "video_core/texture_cache/format_lookup_table.h"
32#include "video_core/texture_cache/surface_base.h" 33#include "video_core/texture_cache/surface_base.h"
33#include "video_core/texture_cache/surface_params.h" 34#include "video_core/texture_cache/surface_params.h"
34#include "video_core/texture_cache/surface_view.h" 35#include "video_core/texture_cache/surface_view.h"
@@ -96,7 +97,7 @@ public:
96 if (!gpu_addr) { 97 if (!gpu_addr) {
97 return {}; 98 return {};
98 } 99 }
99 const auto params{SurfaceParams::CreateForTexture(tic, entry)}; 100 const auto params{SurfaceParams::CreateForTexture(format_lookup_table, tic, entry)};
100 const auto [surface, view] = GetSurface(gpu_addr, params, true, false); 101 const auto [surface, view] = GetSurface(gpu_addr, params, true, false);
101 if (guard_samplers) { 102 if (guard_samplers) {
102 sampled_textures.push_back(surface); 103 sampled_textures.push_back(surface);
@@ -111,7 +112,7 @@ public:
111 if (!gpu_addr) { 112 if (!gpu_addr) {
112 return {}; 113 return {};
113 } 114 }
114 const auto params{SurfaceParams::CreateForImage(tic, entry)}; 115 const auto params{SurfaceParams::CreateForImage(format_lookup_table, tic, entry)};
115 const auto [surface, view] = GetSurface(gpu_addr, params, true, false); 116 const auto [surface, view] = GetSurface(gpu_addr, params, true, false);
116 if (guard_samplers) { 117 if (guard_samplers) {
117 sampled_textures.push_back(surface); 118 sampled_textures.push_back(surface);
@@ -953,6 +954,8 @@ private:
953 954
954 VideoCore::RasterizerInterface& rasterizer; 955 VideoCore::RasterizerInterface& rasterizer;
955 956
957 FormatLookupTable format_lookup_table;
958
956 u64 ticks{}; 959 u64 ticks{};
957 960
958 // Guards the cache for protection conflicts. 961 // Guards the cache for protection conflicts.