summaryrefslogtreecommitdiff
path: root/src/video_core
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core')
-rw-r--r--src/video_core/CMakeLists.txt34
-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/rasterizer.cpp5
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.cpp11
-rw-r--r--src/video_core/texture/texture_decode.cpp270
-rw-r--r--src/video_core/texture/texture_decode.h40
7 files changed, 340 insertions, 301 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index d55b84ce0..ecb424cfd 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -1,36 +1,29 @@
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/texture_decode.cpp
18 vertex_loader.cpp 19 vertex_loader.cpp
19 video_core.cpp 20 video_core.cpp
20 ) 21 )
21 22
22set(HEADERS 23set(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 24 clipper.h
33 command_processor.h 25 command_processor.h
26 debug_utils/debug_utils.h
34 gpu_debugger.h 27 gpu_debugger.h
35 pica.h 28 pica.h
36 pica_state.h 29 pica_state.h
@@ -39,10 +32,19 @@ set(HEADERS
39 rasterizer.h 32 rasterizer.h
40 rasterizer_interface.h 33 rasterizer_interface.h
41 renderer_base.h 34 renderer_base.h
35 renderer_opengl/gl_rasterizer.h
36 renderer_opengl/gl_rasterizer_cache.h
37 renderer_opengl/gl_resource_manager.h
38 renderer_opengl/gl_shader_gen.h
39 renderer_opengl/gl_shader_util.h
40 renderer_opengl/gl_state.h
41 renderer_opengl/pica_to_gl.h
42 renderer_opengl/renderer_opengl.h
42 shader/debug_data.h 43 shader/debug_data.h
43 shader/shader.h 44 shader/shader.h
44 shader/shader_interpreter.h 45 shader/shader_interpreter.h
45 swrasterizer.h 46 swrasterizer.h
47 texture/texture_decode.h
46 utils.h 48 utils.h
47 vertex_loader.h 49 vertex_loader.h
48 video_core.h 50 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/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..4167d3161 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,7 +339,7 @@ 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 = 345 tex_info.stride =
@@ -349,7 +349,7 @@ CachedSurface* RasterizerCacheOpenGL::GetSurface(const CachedSurface& params, bo
349 349
350 for (unsigned y = 0; y < params.height; ++y) { 350 for (unsigned y = 0; y < params.height; ++y) {
351 for (unsigned x = 0; x < params.width; ++x) { 351 for (unsigned x = 0; x < params.width; ++x) {
352 tex_buffer[x + params.width * y] = Pica::DebugUtils::LookupTexture( 352 tex_buffer[x + params.width * y] = Pica::Texture::LookupTexture(
353 texture_src_data, x, params.height - 1 - y, tex_info); 353 texture_src_data, x, params.height - 1 - y, tex_info);
354 } 354 }
355 } 355 }
@@ -512,8 +512,9 @@ CachedSurface* RasterizerCacheOpenGL::GetSurfaceRect(const CachedSurface& params
512 512
513CachedSurface* RasterizerCacheOpenGL::GetTextureSurface( 513CachedSurface* RasterizerCacheOpenGL::GetTextureSurface(
514 const Pica::Regs::FullTextureConfig& config) { 514 const Pica::Regs::FullTextureConfig& config) {
515 Pica::DebugUtils::TextureInfo info = 515
516 Pica::DebugUtils::TextureInfo::FromPicaRegister(config.config, config.format); 516 Pica::Texture::TextureInfo info =
517 Pica::Texture::TextureInfo::FromPicaRegister(config.config, config.format);
517 518
518 CachedSurface params; 519 CachedSurface params;
519 params.addr = info.physical_address; 520 params.addr = info.physical_address;
diff --git a/src/video_core/texture/texture_decode.cpp b/src/video_core/texture/texture_decode.cpp
new file mode 100644
index 000000000..a3b05fe81
--- /dev/null
+++ b/src/video_core/texture/texture_decode.cpp
@@ -0,0 +1,270 @@
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/vector_math.h"
12#include "video_core/texture/texture_decode.h"
13#include "video_core/utils.h"
14
15namespace Pica {
16namespace Texture {
17
18Math::Vec4<u8> LookupTexture(const u8* source, int x, int y, const TextureInfo& info,
19 bool disable_alpha) {
20 const unsigned int coarse_x = x & ~7;
21 const unsigned int coarse_y = y & ~7;
22
23 if (info.format != Regs::TextureFormat::ETC1 && info.format != Regs::TextureFormat::ETC1A4) {
24 // TODO(neobrain): Fix code design to unify vertical block offsets!
25 source += coarse_y * info.stride;
26 }
27
28 // TODO: Assert that width/height are multiples of block dimensions
29
30 switch (info.format) {
31 case Regs::TextureFormat::RGBA8: {
32 auto res = Color::DecodeRGBA8(source + VideoCore::GetMortonOffset(x, y, 4));
33 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
34 }
35
36 case Regs::TextureFormat::RGB8: {
37 auto res = Color::DecodeRGB8(source + VideoCore::GetMortonOffset(x, y, 3));
38 return {res.r(), res.g(), res.b(), 255};
39 }
40
41 case Regs::TextureFormat::RGB5A1: {
42 auto res = Color::DecodeRGB5A1(source + VideoCore::GetMortonOffset(x, y, 2));
43 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
44 }
45
46 case Regs::TextureFormat::RGB565: {
47 auto res = Color::DecodeRGB565(source + VideoCore::GetMortonOffset(x, y, 2));
48 return {res.r(), res.g(), res.b(), 255};
49 }
50
51 case Regs::TextureFormat::RGBA4: {
52 auto res = Color::DecodeRGBA4(source + VideoCore::GetMortonOffset(x, y, 2));
53 return {res.r(), res.g(), res.b(), static_cast<u8>(disable_alpha ? 255 : res.a())};
54 }
55
56 case Regs::TextureFormat::IA8: {
57 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 2);
58
59 if (disable_alpha) {
60 // Show intensity as red, alpha as green
61 return {source_ptr[1], source_ptr[0], 0, 255};
62 } else {
63 return {source_ptr[1], source_ptr[1], source_ptr[1], source_ptr[0]};
64 }
65 }
66
67 case Regs::TextureFormat::RG8: {
68 auto res = Color::DecodeRG8(source + VideoCore::GetMortonOffset(x, y, 2));
69 return {res.r(), res.g(), 0, 255};
70 }
71
72 case Regs::TextureFormat::I8: {
73 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
74 return {*source_ptr, *source_ptr, *source_ptr, 255};
75 }
76
77 case Regs::TextureFormat::A8: {
78 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
79
80 if (disable_alpha) {
81 return {*source_ptr, *source_ptr, *source_ptr, 255};
82 } else {
83 return {0, 0, 0, *source_ptr};
84 }
85 }
86
87 case Regs::TextureFormat::IA4: {
88 const u8* source_ptr = source + VideoCore::GetMortonOffset(x, y, 1);
89
90 u8 i = Color::Convert4To8(((*source_ptr) & 0xF0) >> 4);
91 u8 a = Color::Convert4To8((*source_ptr) & 0xF);
92
93 if (disable_alpha) {
94 // Show intensity as red, alpha as green
95 return {i, a, 0, 255};
96 } else {
97 return {i, i, i, a};
98 }
99 }
100
101 case Regs::TextureFormat::I4: {
102 u32 morton_offset = VideoCore::GetMortonOffset(x, y, 1);
103 const u8* source_ptr = source + morton_offset / 2;
104
105 u8 i = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
106 i = Color::Convert4To8(i);
107
108 return {i, i, i, 255};
109 }
110
111 case Regs::TextureFormat::A4: {
112 u32 morton_offset = VideoCore::GetMortonOffset(x, y, 1);
113 const u8* source_ptr = source + morton_offset / 2;
114
115 u8 a = (morton_offset % 2) ? ((*source_ptr & 0xF0) >> 4) : (*source_ptr & 0xF);
116 a = Color::Convert4To8(a);
117
118 if (disable_alpha) {
119 return {a, a, a, 255};
120 } else {
121 return {0, 0, 0, a};
122 }
123 }
124
125 case Regs::TextureFormat::ETC1:
126 case Regs::TextureFormat::ETC1A4: {
127 bool has_alpha = (info.format == Regs::TextureFormat::ETC1A4);
128
129 // ETC1 further subdivides each 8x8 tile into four 4x4 subtiles
130 const int subtile_width = 4;
131 const int subtile_height = 4;
132
133 int subtile_index = ((x / subtile_width) & 1) + 2 * ((y / subtile_height) & 1);
134 unsigned subtile_bytes = has_alpha ? 2 : 1; // TODO: Name...
135
136 const u64* source_ptr = (const u64*)(source + coarse_x * subtile_bytes * 4 +
137 coarse_y * subtile_bytes * 4 * (info.width / 8) +
138 subtile_index * subtile_bytes * 8);
139 u64 alpha = 0xFFFFFFFFFFFFFFFF;
140 if (has_alpha) {
141 alpha = *source_ptr;
142 source_ptr++;
143 }
144
145 union ETC1Tile {
146 // Each of these two is a collection of 16 bits (one per lookup value)
147 BitField<0, 16, u64> table_subindexes;
148 BitField<16, 16, u64> negation_flags;
149
150 unsigned GetTableSubIndex(unsigned index) const {
151 return (table_subindexes >> index) & 1;
152 }
153
154 bool GetNegationFlag(unsigned index) const {
155 return ((negation_flags >> index) & 1) == 1;
156 }
157
158 BitField<32, 1, u64> flip;
159 BitField<33, 1, u64> differential_mode;
160
161 BitField<34, 3, u64> table_index_2;
162 BitField<37, 3, u64> table_index_1;
163
164 union {
165 // delta value + base value
166 BitField<40, 3, s64> db;
167 BitField<43, 5, u64> b;
168
169 BitField<48, 3, s64> dg;
170 BitField<51, 5, u64> g;
171
172 BitField<56, 3, s64> dr;
173 BitField<59, 5, u64> r;
174 } differential;
175
176 union {
177 BitField<40, 4, u64> b2;
178 BitField<44, 4, u64> b1;
179
180 BitField<48, 4, u64> g2;
181 BitField<52, 4, u64> g1;
182
183 BitField<56, 4, u64> r2;
184 BitField<60, 4, u64> r1;
185 } separate;
186
187 const Math::Vec3<u8> GetRGB(int x, int y) const {
188 int texel = 4 * x + y;
189
190 if (flip)
191 std::swap(x, y);
192
193 // Lookup base value
194 Math::Vec3<int> ret;
195 if (differential_mode) {
196 ret.r() = static_cast<int>(differential.r);
197 ret.g() = static_cast<int>(differential.g);
198 ret.b() = static_cast<int>(differential.b);
199 if (x >= 2) {
200 ret.r() += static_cast<int>(differential.dr);
201 ret.g() += static_cast<int>(differential.dg);
202 ret.b() += static_cast<int>(differential.db);
203 }
204 ret.r() = Color::Convert5To8(ret.r());
205 ret.g() = Color::Convert5To8(ret.g());
206 ret.b() = Color::Convert5To8(ret.b());
207 } else {
208 if (x < 2) {
209 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r1));
210 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g1));
211 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b1));
212 } else {
213 ret.r() = Color::Convert4To8(static_cast<u8>(separate.r2));
214 ret.g() = Color::Convert4To8(static_cast<u8>(separate.g2));
215 ret.b() = Color::Convert4To8(static_cast<u8>(separate.b2));
216 }
217 }
218
219 // Add modifier
220 unsigned table_index =
221 static_cast<int>((x < 2) ? table_index_1.Value() : table_index_2.Value());
222
223 static const std::array<std::array<u8, 2>, 8> etc1_modifier_table = {{
224 {{2, 8}},
225 {{5, 17}},
226 {{9, 29}},
227 {{13, 42}},
228 {{18, 60}},
229 {{24, 80}},
230 {{33, 106}},
231 {{47, 183}},
232 }};
233
234 int modifier = etc1_modifier_table.at(table_index).at(GetTableSubIndex(texel));
235 if (GetNegationFlag(texel))
236 modifier *= -1;
237
238 ret.r() = MathUtil::Clamp(ret.r() + modifier, 0, 255);
239 ret.g() = MathUtil::Clamp(ret.g() + modifier, 0, 255);
240 ret.b() = MathUtil::Clamp(ret.b() + modifier, 0, 255);
241
242 return ret.Cast<u8>();
243 }
244 } const* etc1_tile = reinterpret_cast<const ETC1Tile*>(source_ptr);
245
246 alpha >>= 4 * ((x & 3) * 4 + (y & 3));
247 return Math::MakeVec(etc1_tile->GetRGB(x & 3, y & 3),
248 disable_alpha ? (u8)255 : Color::Convert4To8(alpha & 0xF));
249 }
250
251 default:
252 LOG_ERROR(HW_GPU, "Unknown texture format: %x", (u32)info.format);
253 DEBUG_ASSERT(false);
254 return {};
255 }
256}
257
258TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config,
259 const Regs::TextureFormat& format) {
260 TextureInfo info;
261 info.physical_address = config.GetPhysicalAddress();
262 info.width = config.width;
263 info.height = config.height;
264 info.format = format;
265 info.stride = Pica::Regs::NibblesPerPixel(info.format) * info.width / 2;
266 return info;
267}
268
269} // namespace Texture
270} // 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..0c1438b0f
--- /dev/null
+++ b/src/video_core/texture/texture_decode.h
@@ -0,0 +1,40 @@
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
14struct TextureInfo {
15 PAddr physical_address;
16 int width;
17 int height;
18 int stride;
19 Pica::Regs::TextureFormat format;
20
21 static TextureInfo FromPicaRegister(const Pica::Regs::TextureConfig& config,
22 const Pica::Regs::TextureFormat& format);
23};
24
25/**
26 * Lookup texel located at the given coordinates and return an RGBA vector of its color.
27 * @param source Source pointer to read data from
28 * @param s,t Texture coordinates to read from
29 * @param info TextureInfo object describing the texture setup
30 * @param disable_alpha This is used for debug widgets which use this method to display textures
31 * without providing a good way to visualize alpha by themselves. If true, this will return 255 for
32 * the alpha component, and either drop the information entirely or store it in an "unused" color
33 * channel.
34 * @todo Eventually we should get rid of the disable_alpha parameter.
35 */
36Math::Vec4<u8> LookupTexture(const u8* source, int s, int t, const TextureInfo& info,
37 bool disable_alpha = false);
38
39} // namespace Texture
40} // namespace Pica