summaryrefslogtreecommitdiff
path: root/src/video_core
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core')
-rw-r--r--src/video_core/CMakeLists.txt36
-rw-r--r--src/video_core/debug_utils/debug_utils.cpp256
-rw-r--r--src/video_core/debug_utils/debug_utils.h25
-rw-r--r--src/video_core/pica.h5
-rw-r--r--src/video_core/rasterizer.cpp5
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.cpp14
-rw-r--r--src/video_core/texture/etc1.cpp124
-rw-r--r--src/video_core/texture/etc1.h16
-rw-r--r--src/video_core/texture/texture_decode.cpp229
-rw-r--r--src/video_core/texture/texture_decode.h60
10 files changed, 466 insertions, 304 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index d55b84ce0..ad984cd94 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -1,36 +1,30 @@
1set(SRCS 1set(SRCS
2 renderer_opengl/gl_rasterizer.cpp
3 renderer_opengl/gl_rasterizer_cache.cpp
4 renderer_opengl/gl_shader_gen.cpp
5 renderer_opengl/gl_shader_util.cpp
6 renderer_opengl/gl_state.cpp
7 renderer_opengl/renderer_opengl.cpp
8 debug_utils/debug_utils.cpp
9 clipper.cpp 2 clipper.cpp
10 command_processor.cpp 3 command_processor.cpp
4 debug_utils/debug_utils.cpp
11 pica.cpp 5 pica.cpp
12 primitive_assembly.cpp 6 primitive_assembly.cpp
13 rasterizer.cpp 7 rasterizer.cpp
14 renderer_base.cpp 8 renderer_base.cpp
9 renderer_opengl/gl_rasterizer.cpp
10 renderer_opengl/gl_rasterizer_cache.cpp
11 renderer_opengl/gl_shader_gen.cpp
12 renderer_opengl/gl_shader_util.cpp
13 renderer_opengl/gl_state.cpp
14 renderer_opengl/renderer_opengl.cpp
15 shader/shader.cpp 15 shader/shader.cpp
16 shader/shader_interpreter.cpp 16 shader/shader_interpreter.cpp
17 swrasterizer.cpp 17 swrasterizer.cpp
18 texture/etc1.cpp
19 texture/texture_decode.cpp
18 vertex_loader.cpp 20 vertex_loader.cpp
19 video_core.cpp 21 video_core.cpp
20 ) 22 )
21 23
22set(HEADERS 24set(HEADERS
23 debug_utils/debug_utils.h
24 renderer_opengl/gl_rasterizer.h
25 renderer_opengl/gl_rasterizer_cache.h
26 renderer_opengl/gl_resource_manager.h
27 renderer_opengl/gl_shader_gen.h
28 renderer_opengl/gl_shader_util.h
29 renderer_opengl/gl_state.h
30 renderer_opengl/pica_to_gl.h
31 renderer_opengl/renderer_opengl.h
32 clipper.h 25 clipper.h
33 command_processor.h 26 command_processor.h
27 debug_utils/debug_utils.h
34 gpu_debugger.h 28 gpu_debugger.h
35 pica.h 29 pica.h
36 pica_state.h 30 pica_state.h
@@ -39,10 +33,20 @@ set(HEADERS
39 rasterizer.h 33 rasterizer.h
40 rasterizer_interface.h 34 rasterizer_interface.h
41 renderer_base.h 35 renderer_base.h
36 renderer_opengl/gl_rasterizer.h
37 renderer_opengl/gl_rasterizer_cache.h
38 renderer_opengl/gl_resource_manager.h
39 renderer_opengl/gl_shader_gen.h
40 renderer_opengl/gl_shader_util.h
41 renderer_opengl/gl_state.h
42 renderer_opengl/pica_to_gl.h
43 renderer_opengl/renderer_opengl.h
42 shader/debug_data.h 44 shader/debug_data.h
43 shader/shader.h 45 shader/shader.h
44 shader/shader_interpreter.h 46 shader/shader_interpreter.h
45 swrasterizer.h 47 swrasterizer.h
48 texture/etc1.h
49 texture/texture_decode.h
46 utils.h 50 utils.h
47 vertex_loader.h 51 vertex_loader.h
48 video_core.h 52 video_core.h
diff --git a/src/video_core/debug_utils/debug_utils.cpp b/src/video_core/debug_utils/debug_utils.cpp
index c44b3d95a..2d40f7d4f 100644
--- a/src/video_core/debug_utils/debug_utils.cpp
+++ b/src/video_core/debug_utils/debug_utils.cpp
@@ -35,6 +35,7 @@
35#include "video_core/rasterizer_interface.h" 35#include "video_core/rasterizer_interface.h"
36#include "video_core/renderer_base.h" 36#include "video_core/renderer_base.h"
37#include "video_core/shader/shader.h" 37#include "video_core/shader/shader.h"
38#include "video_core/texture/texture_decode.h"
38#include "video_core/utils.h" 39#include "video_core/utils.h"
39#include "video_core/video_core.h" 40#include "video_core/video_core.h"
40 41
@@ -315,257 +316,6 @@ std::unique_ptr<PicaTrace> FinishPicaTracing() {
315 return ret; 316 return ret;
316} 317}
317 318
318const Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const TextureInfo& info,
319 bool disable_alpha) {
320 const unsigned int coarse_x = x & ~7;
321 const unsigned int coarse_y = y & ~7;
322
323 if (info.format != Regs::TextureFormat::ETC1 && info.format != Regs::TextureFormat::ETC1A4) {
324 // TODO(neobrain): Fix code design to unify vertical block offsets!
325 source += coarse_y * info.stride;
326 }
327
328 // TODO: Assert that width/height are multiples of block dimensions
329
330 switch (info.format) {
331 case Regs::TextureFormat::RGBA8: {
332 auto res = Color::DecodeRGBA8(source + VideoCore::GetMortonOffset(x, y, 4));
333 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
334 }
335
336 case Regs::TextureFormat::RGB8: {
337 auto res = Color::DecodeRGB8(source + VideoCore::GetMortonOffset(x, y, 3));
338 return {res.r(), res.g(), res.b(), 255};
339 }
340
341 case Regs::TextureFormat::RGB5A1: {
342 auto res = Color::DecodeRGB5A1(source + VideoCore::GetMortonOffset(x, y, 2));
343 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
344 }
345
346 case Regs::TextureFormat::RGB565: {
347 auto res = Color::DecodeRGB565(source + VideoCore::GetMortonOffset(x, y, 2));
348 return {res.r(), res.g(), res.b(), 255};
349 }
350
351 case Regs::TextureFormat::RGBA4: {
352 auto res = Color::DecodeRGBA4(source + VideoCore::GetMortonOffset(x, y, 2));
353 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
354 }
355
356 case Regs::TextureFormat::IA8: {
357 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 2);
358
359 if (disable_alpha) {
360 // Show intensity as red, alpha as green
361 return {source_ptr[1], source_ptr[0], 0, 255};
362 } else {
363 return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
364 }
365 }
366
367 case Regs::TextureFormat::RG8: {
368 auto res = Color::DecodeRG8(source + VideoCore::GetMortonOffset(x, y, 2));
369 return {res.r(), res.g(), 0, 255};
370 }
371
372 case Regs::TextureFormat::I8: {
373 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
374 return {*source_ptr, *source_ptr, *source_ptr, 255};
375 }
376
377 case Regs::TextureFormat::A8: {
378 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
379
380 if (disable_alpha) {
381 return {*source_ptr, *source_ptr, *source_ptr, 255};
382 } else {
383 return {0, 0, 0, *source_ptr};
384 }
385 }
386
387 case Regs::TextureFormat::IA4: {
388 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
389
390 u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
391 u8 a = Color::Convert4To8((*source_ptr) & 0xF);
392
393 if (disable_alpha) {
394 // Show intensity as red, alpha as green
395 return {i, a, 0, 255};
396 } else {
397 return {i, i, i, a};
398 }
399 }
400
401 case Regs::TextureFormat::I4: {
402 u32 morton_offset = VideoCore::GetMortonOffset(x, y, 1);
403 const u8* source_ptr = source + morton_offset / 2;
404
405 u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
406 i = Color::Convert4To8(i);
407
408 return {i, i, i, 255};
409 }
410
411 case Regs::TextureFormat::A4: {
412 u32 morton_offset = VideoCore::GetMortonOffset(x, y, 1);
413 const u8* source_ptr = source + morton_offset / 2;
414
415 u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
416 a = Color::Convert4To8(a);
417
418 if (disable_alpha) {
419 return {a, a, a, 255};
420 } else {
421 return {0, 0, 0, a};
422 }
423 }
424
425 case Regs::TextureFormat::ETC1:
426 case Regs::TextureFormat::ETC1A4: {
427 bool has_alpha = (info.format == Regs::TextureFormat::ETC1A4);
428
429 // ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
430 const int subtile_width = 4;
431 const int subtile_height = 4;
432
433 int subtile_index = ((x / subtile_width) & 1) + 2 * ((y / subtile_height) & 1);
434 unsigned subtile_bytes = has_alpha ? 2 : 1; // TODO: Name...
435
436 const u64* source_ptr = (const u64*)(source + coarse_x * subtile_bytes * 4 +
437 coarse_y * subtile_bytes * 4 * (info.width / 8) +
438 subtile_index * subtile_bytes * 8);
439 u64 alpha = 0xFFFFFFFFFFFFFFFF;
440 if (has_alpha) {
441 alpha = *source_ptr;
442 source_ptr++;
443 }
444
445 union ETC1Tile {
446 // Each of these two is a collection of 16 bits (one per lookup value)
447 BitField<0, 16, u64> table_subindexes;
448 BitField<16, 16, u64> negation_flags;
449
450 unsigned GetTableSubIndex(unsigned index) const {
451 return (table_subindexes >> index) & 1;
452 }
453
454 bool GetNegationFlag(unsigned index) const {
455 return ((negation_flags >> index) & 1) == 1;
456 }
457
458 BitField<32, 1, u64> flip;
459 BitField<33, 1, u64> differential_mode;
460
461 BitField<34, 3, u64> table_index_2;
462 BitField<37, 3, u64> table_index_1;
463
464 union {
465 // delta value + base value
466 BitField<40, 3, s64> db;
467 BitField<43, 5, u64> b;
468
469 BitField<48, 3, s64> dg;
470 BitField<51, 5, u64> g;
471
472 BitField<56, 3, s64> dr;
473 BitField<59, 5, u64> r;
474 } differential;
475
476 union {
477 BitField<40, 4, u64> b2;
478 BitField<44, 4, u64> b1;
479
480 BitField<48, 4, u64> g2;
481 BitField<52, 4, u64> g1;
482
483 BitField<56, 4, u64> r2;
484 BitField<60, 4, u64> r1;
485 } separate;
486
487 const Math::Vec3<u8> GetRGB(int x, int y) const {
488 int texel = 4 * x + y;
489
490 if (flip)
491 std::swap(x, y);
492
493 // Lookup base value
494 Math::Vec3<int> ret;
495 if (differential_mode) {
496 ret.r() = static_cast<int>(differential.r);
497 ret.g() = static_cast<int>(differential.g);
498 ret.b() = static_cast<int>(differential.b);
499 if (x >= 2) {
500 ret.r() += static_cast<int>(differential.dr);
501 ret.g() += static_cast<int>(differential.dg);
502 ret.b() += static_cast<int>(differential.db);
503 }
504 ret.r() = Color::Convert5To8(ret.r());
505 ret.g() = Color::Convert5To8(ret.g());
506 ret.b() = Color::Convert5To8(ret.b());
507 } else {
508 if (x < 2) {
509 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1));
510 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1));
511 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1));
512 } else {
513 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2));
514 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2));
515 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2));
516 }
517 }
518
519 // Add modifier
520 unsigned table_index =
521 static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value());
522
523 static const std::array<std::array<u8, 2>, 8> etc1_modifier_table = {{
524 {{2, 8}},
525 {{5, 17}},
526 {{9, 29}},
527 {{13, 42}},
528 {{18, 60}},
529 {{24, 80}},
530 {{33, 106}},
531 {{47, 183}},
532 }};
533
534 int modifier = etc1_modifier_table.at(table_index).at(GetTableSubIndex(texel));
535 if (GetNegationFlag(texel))
536 modifier *= -1;
537
538 ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255);
539 ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255);
540 ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255);
541
542 return ret.Cast<u8>();
543 }
544 } const* etc1_tile = reinterpret_cast<const ETC1Tile*>(source_ptr);
545
546 alpha >>= 4 * ((x & 3) * 4 + (y & 3));
547 return Math::MakeVec(etc1_tile->GetRGB(x & 3, y & 3),
548 disable_alpha ? (u8)255 : Color::Convert4To8(alpha & 0xF));
549 }
550
551 default:
552 LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
553 DEBUG_ASSERT(false);
554 return {};
555 }
556}
557
558TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
559 const Regs::TextureFormat& format) {
560 TextureInfo info;
561 info.physical_address = config.GetPhysicalAddress();
562 info.width = config.width;
563 info.height = config.height;
564 info.format = format;
565 info.stride = Pica::Regs::NibblesPerPixel(info.format) * info.width / 2;
566 return info;
567}
568
569#ifdef HAVE_PNG 319#ifdef HAVE_PNG
570// Adapter functions to libpng to write/flush to File::IOFile instances. 320// Adapter functions to libpng to write/flush to File::IOFile instances.
571static void WriteIOFile(png_structp png_ptr, png_bytep data, png_size_t length) { 321static void WriteIOFile(png_structp png_ptr, png_bytep data, png_size_t length) {
@@ -642,12 +392,12 @@ void DumpTexture(const Pica::Regs::TextureConfig& texture_config, u8* data) {
642 buf = new u8[row_stride * texture_config.height]; 392 buf = new u8[row_stride * texture_config.height];
643 for (unsigned y = 0; y < texture_config.height; ++y) { 393 for (unsigned y = 0; y < texture_config.height; ++y) {
644 for (unsigned x = 0; x < texture_config.width; ++x) { 394 for (unsigned x = 0; x < texture_config.width; ++x) {
645 TextureInfo info; 395 Pica::Texture::TextureInfo info;
646 info.width = texture_config.width; 396 info.width = texture_config.width;
647 info.height = texture_config.height; 397 info.height = texture_config.height;
648 info.stride = row_stride; 398 info.stride = row_stride;
649 info.format = g_state.regs.texture0_format; 399 info.format = g_state.regs.texture0_format;
650 Math::Vec4<u8> texture_color = LookupTexture(data, x, y, info); 400 Math::Vec4<u8> texture_color = Pica::Texture::LookupTexture(data, x, y, info);
651 buf[3 * x + y * row_stride] = texture_color.r(); 401 buf[3 * x + y * row_stride] = texture_color.r();
652 buf[3 * x + y * row_stride + 1] = texture_color.g(); 402 buf[3 * x + y * row_stride + 1] = texture_color.g();
653 buf[3 * x + y * row_stride + 2] = texture_color.b(); 403 buf[3 * x + y * row_stride + 2] = texture_color.b();
diff --git a/src/video_core/debug_utils/debug_utils.h b/src/video_core/debug_utils/debug_utils.h
index 46ea8d9c7..938a2e1b5 100644
--- a/src/video_core/debug_utils/debug_utils.h
+++ b/src/video_core/debug_utils/debug_utils.h
@@ -205,31 +205,6 @@ inline bool IsPicaTracing() {
205void OnPicaRegWrite(PicaTrace::Write write); 205void OnPicaRegWrite(PicaTrace::Write write);
206std::unique_ptr<PicaTrace> FinishPicaTracing(); 206std::unique_ptr<PicaTrace> FinishPicaTracing();
207 207
208struct TextureInfo {
209 PAddr physical_address;
210 int width;
211 int height;
212 int stride;
213 Pica::Regs::TextureFormat format;
214
215 static TextureInfo FromPicaRegister(const Pica::Regs::TextureConfig& config,
216 const Pica::Regs::TextureFormat& format);
217};
218
219/**
220 * Lookup texel located at the given coordinates and return an RGBA vector of its color.
221 * @param source Source pointer to read data from
222 * @param s,t Texture coordinates to read from
223 * @param info TextureInfo object describing the texture setup
224 * @param disable_alpha This is used for debug widgets which use this method to display textures
225 * without providing a good way to visualize alpha by themselves. If true, this will return 255 for
226 * the alpha component, and either drop the information entirely or store it in an "unused" color
227 * channel.
228 * @todo Eventually we should get rid of the disable_alpha parameter.
229 */
230const Math::Vec4<u8> LookupTexture(const u8* source, int s, int t, const TextureInfo& info,
231 bool disable_alpha = false);
232
233void DumpTexture(const Pica::Regs::TextureConfig& texture_config, u8* data); 208void DumpTexture(const Pica::Regs::TextureConfig& texture_config, u8* data);
234 209
235std::string GetTevStageConfigColorCombinerString(const Pica::Regs::TevStageConfig& tev_stage); 210std::string GetTevStageConfigColorCombinerString(const Pica::Regs::TevStageConfig& tev_stage);
diff --git a/src/video_core/pica.h b/src/video_core/pica.h
index b2db609ec..4ab4f1f40 100644
--- a/src/video_core/pica.h
+++ b/src/video_core/pica.h
@@ -275,8 +275,11 @@ struct Regs {
275 case TextureFormat::I8: 275 case TextureFormat::I8:
276 case TextureFormat::A8: 276 case TextureFormat::A8:
277 case TextureFormat::IA4: 277 case TextureFormat::IA4:
278 default: // placeholder for yet unknown formats
279 return 2; 278 return 2;
279
280 default: // placeholder for yet unknown formats
281 UNIMPLEMENTED();
282 return 0;
280 } 283 }
281 } 284 }
282 285
diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp
index b9f5d4533..c034c12d3 100644
--- a/src/video_core/rasterizer.cpp
+++ b/src/video_core/rasterizer.cpp
@@ -21,6 +21,7 @@
21#include "video_core/pica_types.h" 21#include "video_core/pica_types.h"
22#include "video_core/rasterizer.h" 22#include "video_core/rasterizer.h"
23#include "video_core/shader/shader.h" 23#include "video_core/shader/shader.h"
24#include "video_core/texture/texture_decode.h"
24#include "video_core/utils.h" 25#include "video_core/utils.h"
25 26
26namespace Pica { 27namespace Pica {
@@ -579,10 +580,10 @@ static void ProcessTriangleInternal(const Shader::OutputVertex& v0, const Shader
579 u8* texture_data = 580 u8* texture_data =
580 Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress()); 581 Memory::GetPhysicalPointer(texture.config.GetPhysicalAddress());
581 auto info = 582 auto info =
582 DebugUtils::TextureInfo::FromPicaRegister(texture.config, texture.format); 583 Texture::TextureInfo::FromPicaRegister(texture.config, texture.format);
583 584
584 // TODO: Apply the min and mag filters to the texture 585 // TODO: Apply the min and mag filters to the texture
585 texture_color[i] = DebugUtils::LookupTexture(texture_data, s, t, info); 586 texture_color[i] = Texture::LookupTexture(texture_data, s, t, info);
586#if PICA_DUMP_TEXTURES 587#if PICA_DUMP_TEXTURES
587 DebugUtils::DumpTexture(texture.config, texture_data); 588 DebugUtils::DumpTexture(texture.config, texture_data);
588#endif 589#endif
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index 1e7eedecb..60380257a 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -17,10 +17,10 @@
17#include "common/vector_math.h" 17#include "common/vector_math.h"
18#include "core/frontend/emu_window.h" 18#include "core/frontend/emu_window.h"
19#include "core/memory.h" 19#include "core/memory.h"
20#include "video_core/debug_utils/debug_utils.h"
21#include "video_core/pica_state.h" 20#include "video_core/pica_state.h"
22#include "video_core/renderer_opengl/gl_rasterizer_cache.h" 21#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
23#include "video_core/renderer_opengl/gl_state.h" 22#include "video_core/renderer_opengl/gl_state.h"
23#include "video_core/texture/texture_decode.h"
24#include "video_core/utils.h" 24#include "video_core/utils.h"
25#include "video_core/video_core.h" 25#include "video_core/video_core.h"
26 26
@@ -339,17 +339,16 @@ CachedSurface* RasterizerCacheOpenGL::GetSurface(const CachedSurface& params, bo
339 339
340 std::vector<Math::Vec4<u8>> tex_buffer(params.width * params.height); 340 std::vector<Math::Vec4<u8>> tex_buffer(params.width * params.height);
341 341
342 Pica::DebugUtils::TextureInfo tex_info; 342 Pica::Texture::TextureInfo tex_info;
343 tex_info.width = params.width; 343 tex_info.width = params.width;
344 tex_info.height = params.height; 344 tex_info.height = params.height;
345 tex_info.stride =
346 params.width * CachedSurface::GetFormatBpp(params.pixel_format) / 8;
347 tex_info.format = (Pica::Regs::TextureFormat)params.pixel_format; 345 tex_info.format = (Pica::Regs::TextureFormat)params.pixel_format;
346 tex_info.SetDefaultStride();
348 tex_info.physical_address = params.addr; 347 tex_info.physical_address = params.addr;
349 348
350 for (unsigned y = 0; y < params.height; ++y) { 349 for (unsigned y = 0; y < params.height; ++y) {
351 for (unsigned x = 0; x < params.width; ++x) { 350 for (unsigned x = 0; x < params.width; ++x) {
352 tex_buffer[x + params.width * y] = Pica::DebugUtils::LookupTexture( 351 tex_buffer[x + params.width * y] = Pica::Texture::LookupTexture(
353 texture_src_data, x, params.height - 1 - y, tex_info); 352 texture_src_data, x, params.height - 1 - y, tex_info);
354 } 353 }
355 } 354 }
@@ -512,8 +511,9 @@ CachedSurface* RasterizerCacheOpenGL::GetSurfaceRect(const CachedSurface& params
512 511
513CachedSurface* RasterizerCacheOpenGL::GetTextureSurface( 512CachedSurface* RasterizerCacheOpenGL::GetTextureSurface(
514 const Pica::Regs::FullTextureConfig& config) { 513 const Pica::Regs::FullTextureConfig& config) {
515 Pica::DebugUtils::TextureInfo info = 514
516 Pica::DebugUtils::TextureInfo::FromPicaRegister(config.config, config.format); 515 Pica::Texture::TextureInfo info =
516 Pica::Texture::TextureInfo::FromPicaRegister(config.config, config.format);
517 517
518 CachedSurface params; 518 CachedSurface params;
519 params.addr = info.physical_address; 519 params.addr = info.physical_address;
diff --git a/src/video_core/texture/etc1.cpp b/src/video_core/texture/etc1.cpp
new file mode 100644
index 000000000..af60cde1e
--- /dev/null
+++ b/src/video_core/texture/etc1.cpp
@@ -0,0 +1,124 @@
1// Copyright 2017 Citra 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 "common/bit_field.h"
9#include "common/color.h"
10#include "common/common_types.h"
11#include "common/math_util.h"
12#include "common/vector_math.h"
13#include "video_core/texture/etc1.h"
14
15namespace Pica {
16namespace Texture {
17
18namespace {
19
20constexpr std::array<u8[2], 8> etc1_modifier_table = {{
21 {2, 8}, {5, 17}, {9, 29}, {13, 42}, {18, 60}, {24, 80}, {33, 106}, {47, 183},
22}};
23
24union ETC1Tile {
25 u64 raw;
26
27 // Each of these two is a collection of 16 bits (one per lookup value)
28 BitField<0, 16, u64> table_subindexes;
29 BitField<16, 16, u64> negation_flags;
30
31 unsigned GetTableSubIndex(unsigned index) const {
32 return (table_subindexes >> index) & 1;
33 }
34
35 bool GetNegationFlag(unsigned index) const {
36 return ((negation_flags >> index) & 1) == 1;
37 }
38
39 BitField<32, 1, u64> flip;
40 BitField<33, 1, u64> differential_mode;
41
42 BitField<34, 3, u64> table_index_2;
43 BitField<37, 3, u64> table_index_1;
44
45 union {
46 // delta value + base value
47 BitField<40, 3, s64> db;
48 BitField<43, 5, u64> b;
49
50 BitField<48, 3, s64> dg;
51 BitField<51, 5, u64> g;
52
53 BitField<56, 3, s64> dr;
54 BitField<59, 5, u64> r;
55 } differential;
56
57 union {
58 BitField<40, 4, u64> b2;
59 BitField<44, 4, u64> b1;
60
61 BitField<48, 4, u64> g2;
62 BitField<52, 4, u64> g1;
63
64 BitField<56, 4, u64> r2;
65 BitField<60, 4, u64> r1;
66 } separate;
67
68 const Math::Vec3<u8> GetRGB(unsigned int x, unsigned int y) const {
69 int texel = 4 * x + y;
70
71 if (flip)
72 std::swap(x, y);
73
74 // Lookup base value
75 Math::Vec3<int> ret;
76 if (differential_mode) {
77 ret.r() = static_cast<int>(differential.r);
78 ret.g() = static_cast<int>(differential.g);
79 ret.b() = static_cast<int>(differential.b);
80 if (x >= 2) {
81 ret.r() += static_cast<int>(differential.dr);
82 ret.g() += static_cast<int>(differential.dg);
83 ret.b() += static_cast<int>(differential.db);
84 }
85 ret.r() = Color::Convert5To8(ret.r());
86 ret.g() = Color::Convert5To8(ret.g());
87 ret.b() = Color::Convert5To8(ret.b());
88 } else {
89 if (x < 2) {
90 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1));
91 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1));
92 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1));
93 } else {
94 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2));
95 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2));
96 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2));
97 }
98 }
99
100 // Add modifier
101 unsigned table_index =
102 static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value());
103
104 int modifier = etc1_modifier_table[table_index][GetTableSubIndex(texel)];
105 if (GetNegationFlag(texel))
106 modifier *= -1;
107
108 ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255);
109 ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255);
110 ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255);
111
112 return ret.Cast<u8>();
113 }
114};
115
116} // anonymous namespace
117
118Math::Vec3<u8> SampleETC1Subtile(u64 value, unsigned int x, unsigned int y) {
119 ETC1Tile tile{value};
120 return tile.GetRGB(x, y);
121}
122
123} // namespace Texture
124} // namespace Pica
diff --git a/src/video_core/texture/etc1.h b/src/video_core/texture/etc1.h
new file mode 100644
index 000000000..e188b19df
--- /dev/null
+++ b/src/video_core/texture/etc1.h
@@ -0,0 +1,16 @@
1// Copyright 2017 Citra 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 "common/common_types.h"
8#include "common/vector_math.h"
9
10namespace Pica {
11namespace Texture {
12
13Math::Vec3<u8> SampleETC1Subtile(u64 value, unsigned int x, unsigned int y);
14
15} // namespace Texture
16} // namespace Pica
diff --git a/src/video_core/texture/texture_decode.cpp b/src/video_core/texture/texture_decode.cpp
new file mode 100644
index 000000000..f611a1aa9
--- /dev/null
+++ b/src/video_core/texture/texture_decode.cpp
@@ -0,0 +1,229 @@
1// Copyright 2017 Citra 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 "common/assert.h"
8#include "common/color.h"
9#include "common/logging/log.h"
10#include "common/math_util.h"
11#include "common/swap.h"
12#include "common/vector_math.h"
13#include "video_core/pica.h"
14#include "video_core/texture/etc1.h"
15#include "video_core/texture/texture_decode.h"
16#include "video_core/utils.h"
17
18using TextureFormat = Pica::Regs::TextureFormat;
19
20namespace Pica {
21namespace Texture {
22
23constexpr size_t TILE_SIZE = 8 * 8;
24constexpr size_t ETC1_SUBTILES = 2 * 2;
25
26size_t CalculateTileSize(TextureFormat format) {
27 switch (format) {
28 case TextureFormat::RGBA8:
29 return 4 * TILE_SIZE;
30
31 case TextureFormat::RGB8:
32 return 3 * TILE_SIZE;
33
34 case TextureFormat::RGB5A1:
35 case TextureFormat::RGB565:
36 case TextureFormat::RGBA4:
37 case TextureFormat::IA8:
38 case TextureFormat::RG8:
39 return 2 * TILE_SIZE;
40
41 case TextureFormat::I8:
42 case TextureFormat::A8:
43 case TextureFormat::IA4:
44 return 1 * TILE_SIZE;
45
46 case TextureFormat::I4:
47 case TextureFormat::A4:
48 return TILE_SIZE / 2;
49
50 case TextureFormat::ETC1:
51 return ETC1_SUBTILES * 8;
52
53 case TextureFormat::ETC1A4:
54 return ETC1_SUBTILES * 16;
55
56 default: // placeholder for yet unknown formats
57 UNIMPLEMENTED();
58 return 0;
59 }
60}
61
62Math::Vec4<u8> LookupTexture(const u8* source, unsigned int x, unsigned int y,
63 const TextureInfo& info, bool disable_alpha) {
64 // Coordinate in tiles
65 const unsigned int coarse_x = x / 8;
66 const unsigned int coarse_y = y / 8;
67
68 // Coordinate inside the tile
69 const unsigned int fine_x = x % 8;
70 const unsigned int fine_y = y % 8;
71
72 const u8* line = source + coarse_y * info.stride;
73 const u8* tile = line + coarse_x * CalculateTileSize(info.format);
74 return LookupTexelInTile(tile, fine_x, fine_y, info, disable_alpha);
75}
76
77Math::Vec4<u8> LookupTexelInTile(const u8* source, unsigned int x, unsigned int y,
78 const TextureInfo& info, bool disable_alpha) {
79 DEBUG_ASSERT(x < 8);
80 DEBUG_ASSERT(y < 8);
81
82 using VideoCore::MortonInterleave;
83
84 switch (info.format) {
85 case Regs::TextureFormat::RGBA8: {
86 auto res = Color::DecodeRGBA8(source + MortonInterleave(x, y) * 4);
87 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
88 }
89
90 case Regs::TextureFormat::RGB8: {
91 auto res = Color::DecodeRGB8(source + MortonInterleave(x, y) * 3);
92 return {res.r(), res.g(), res.b(), 255};
93 }
94
95 case Regs::TextureFormat::RGB5A1: {
96 auto res = Color::DecodeRGB5A1(source + MortonInterleave(x, y) * 2);
97 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
98 }
99
100 case Regs::TextureFormat::RGB565: {
101 auto res = Color::DecodeRGB565(source + MortonInterleave(x, y) * 2);
102 return {res.r(), res.g(), res.b(), 255};
103 }
104
105 case Regs::TextureFormat::RGBA4: {
106 auto res = Color::DecodeRGBA4(source + MortonInterleave(x, y) * 2);
107 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
108 }
109
110 case Regs::TextureFormat::IA8: {
111 const u8* source_ptr = source + MortonInterleave(x, y) * 2;
112
113 if (disable_alpha) {
114 // Show intensity as red, alpha as green
115 return {source_ptr[1], source_ptr[0], 0, 255};
116 } else {
117 return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
118 }
119 }
120
121 case Regs::TextureFormat::RG8: {
122 auto res = Color::DecodeRG8(source + MortonInterleave(x, y) * 2);
123 return {res.r(), res.g(), 0, 255};
124 }
125
126 case Regs::TextureFormat::I8: {
127 const u8* source_ptr = source + MortonInterleave(x, y);
128 return {*source_ptr, *source_ptr, *source_ptr, 255};
129 }
130
131 case Regs::TextureFormat::A8: {
132 const u8* source_ptr = source + MortonInterleave(x, y);
133
134 if (disable_alpha) {
135 return {*source_ptr, *source_ptr, *source_ptr, 255};
136 } else {
137 return {0, 0, 0, *source_ptr};
138 }
139 }
140
141 case Regs::TextureFormat::IA4: {
142 const u8* source_ptr = source + MortonInterleave(x, y);
143
144 u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
145 u8 a = Color::Convert4To8((*source_ptr) & 0xF);
146
147 if (disable_alpha) {
148 // Show intensity as red, alpha as green
149 return {i, a, 0, 255};
150 } else {
151 return {i, i, i, a};
152 }
153 }
154
155 case Regs::TextureFormat::I4: {
156 u32 morton_offset = MortonInterleave(x, y);
157 const u8* source_ptr = source + morton_offset / 2;
158
159 u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
160 i = Color::Convert4To8(i);
161
162 return {i, i, i, 255};
163 }
164
165 case Regs::TextureFormat::A4: {
166 u32 morton_offset = MortonInterleave(x, y);
167 const u8* source_ptr = source + morton_offset / 2;
168
169 u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
170 a = Color::Convert4To8(a);
171
172 if (disable_alpha) {
173 return {a, a, a, 255};
174 } else {
175 return {0, 0, 0, a};
176 }
177 }
178
179 case Regs::TextureFormat::ETC1:
180 case Regs::TextureFormat::ETC1A4: {
181 bool has_alpha = (info.format == Regs::TextureFormat::ETC1A4);
182 size_t subtile_size = has_alpha ? 16 : 8;
183
184 // ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
185 constexpr unsigned int subtile_width = 4;
186 constexpr unsigned int subtile_height = 4;
187
188 unsigned int subtile_index = (x / subtile_width) + 2 * (y / subtile_height);
189 x %= subtile_width;
190 y %= subtile_height;
191
192 const u8* subtile_ptr = source + subtile_index * subtile_size;
193
194 u8 alpha = 255;
195 if (has_alpha) {
196 u64_le packed_alpha;
197 memcpy(&packed_alpha, subtile_ptr, sizeof(u64));
198 subtile_ptr += sizeof(u64);
199
200 alpha = Color::Convert4To8((packed_alpha >> (4 * (x * subtile_width + y))) & 0xF);
201 }
202
203 u64_le subtile_data;
204 memcpy(&subtile_data, subtile_ptr, sizeof(u64));
205
206 return Math::MakeVec(SampleETC1Subtile(subtile_data, x, y),
207 disable_alpha ? (u8)255 : alpha);
208 }
209
210 default:
211 LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
212 DEBUG_ASSERT(false);
213 return {};
214 }
215}
216
217TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
218 const Regs::TextureFormat& format) {
219 TextureInfo info;
220 info.physical_address = config.GetPhysicalAddress();
221 info.width = config.width;
222 info.height = config.height;
223 info.format = format;
224 info.SetDefaultStride();
225 return info;
226}
227
228} // namespace Texture
229} // namespace Pica
diff --git a/src/video_core/texture/texture_decode.h b/src/video_core/texture/texture_decode.h
new file mode 100644
index 000000000..5c636939a
--- /dev/null
+++ b/src/video_core/texture/texture_decode.h
@@ -0,0 +1,60 @@
1// Copyright 2017 Citra 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 "common/common_types.h"
8#include "common/vector_math.h"
9#include "video_core/pica.h"
10
11namespace Pica {
12namespace Texture {
13
14/// Returns the byte size of a 8*8 tile of the specified texture format.
15size_t CalculateTileSize(Pica::Regs::TextureFormat format);
16
17struct TextureInfo {
18 PAddr physical_address;
19 unsigned int width;
20 unsigned int height;
21 ptrdiff_t stride;
22 Pica::Regs::TextureFormat format;
23
24 static TextureInfo FromPicaRegister(const Pica::Regs::TextureConfig& config,
25 const Pica::Regs::TextureFormat& format);
26
27 /// Calculates stride from format and width, assuming that the entire texture is contiguous.
28 void SetDefaultStride() {
29 stride = Pica::Texture::CalculateTileSize(format) * (width / 8);
30 }
31};
32
33/**
34 * Lookup texel located at the given coordinates and return an RGBA vector of its color.
35 * @param source Source pointer to read data from
36 * @param x,y Texture coordinates to read from
37 * @param info TextureInfo object describing the texture setup
38 * @param disable_alpha This is used for debug widgets which use this method to display textures
39 * without providing a good way to visualize alpha by themselves. If true, this will return 255 for
40 * the alpha component, and either drop the information entirely or store it in an "unused" color
41 * channel.
42 * @todo Eventually we should get rid of the disable_alpha parameter.
43 */
44Math::Vec4<u8> LookupTexture(const u8* source, unsigned int x, unsigned int y,
45 const TextureInfo& info, bool disable_alpha = false);
46
47/**
48 * Looks up a texel from a single 8x8 texture tile.
49 *
50 * @param source Pointer to the beginning of the tile.
51 * @param x, y In-tile coordinates to read from. Must be < 8.
52 * @param info TextureInfo describing the texture format.
53 * @param disable_alpha Used for debugging. Sets the result alpha to 255 and either discards the
54 * real alpha or inserts it in an otherwise unused channel.
55 */
56Math::Vec4<u8> LookupTexelInTile(const u8* source, unsigned int x, unsigned int y,
57 const TextureInfo& info, bool disable_alpha);
58
59} // namespace Texture
60} // namespace Pica