diff --git a/src/core/mdec.cpp b/src/core/mdec.cpp index d40d5f15..44ffc425 100644 --- a/src/core/mdec.cpp +++ b/src/core/mdec.cpp @@ -2,6 +2,7 @@ #include "common/state_wrapper.h" #include "cpu_core.h" #include "dma.h" +#include "gpu_types.h" #include "interrupt_controller.h" #include "system.h" @@ -323,14 +324,6 @@ bool MDEC::HandleDecodeMacroblockCommand() return DecodeColoredMacroblock(); } -// Parasite Eve needs slightly higher timing in 16bpp mode, Disney's Treasure Planet needs lower timing in 24bpp mode. -static constexpr std::array s_ticks_per_block = {{ - 448, // 4bpp - 448, // 8bpp - 448, // 24bpp - 550, // 16bpp -}}; - bool MDEC::DecodeMonoMacroblock() { // TODO: This should guard the output not the input @@ -347,7 +340,7 @@ bool MDEC::DecodeMonoMacroblock() y_to_mono(m_blocks[0]); - ScheduleBlockCopyOut(s_ticks_per_block[static_cast(m_status.data_output_depth)] * 6); + ScheduleBlockCopyOut(TICKS_PER_BLOCK * 6); m_total_blocks_decoded++; return true; @@ -376,7 +369,7 @@ bool MDEC::DecodeColoredMacroblock() yuv_to_rgb(8, 8, m_blocks[0], m_blocks[1], m_blocks[5]); m_total_blocks_decoded += 4; - ScheduleBlockCopyOut(s_ticks_per_block[static_cast(m_status.data_output_depth)] * 6); + ScheduleBlockCopyOut(TICKS_PER_BLOCK * 6); return true; } @@ -464,22 +457,47 @@ void MDEC::CopyOutBlock() case DataOutputDepth_15Bit: { - const u16 a = ZeroExtend16(m_status.data_output_bit15.GetValue()); - for (u32 i = 0; i < static_cast(m_block_rgb.size());) + if (!g_settings.use_old_mdec_routines) { - u32 color = m_block_rgb[i++]; - u16 r = Truncate16((color >> 3) & 0x1Fu); - u16 g = Truncate16((color >> 11) & 0x1Fu); - u16 b = Truncate16((color >> 19) & 0x1Fu); - const u16 color15a = r | (g << 5) | (b << 10) | (a << 15); + const u32 a = ZeroExtend32(m_status.data_output_bit15.GetValue()) << 15; + for (u32 i = 0; i < static_cast(m_block_rgb.size());) + { +#define E8TO5(color) (std::min((((color) + 4) >> 3), 0x1F)) + u32 color = m_block_rgb[i++]; + u32 r = E8TO5(color & 0xFFu); + u32 g = E8TO5((color >> 8) & 0xFFu); + u32 b = E8TO5((color >> 16) & 0xFFu); + const u32 color15a = r | (g << 5) | (b << 10) | a; - color = m_block_rgb[i++]; - r = Truncate16((color >> 3) & 0x1Fu); - g = Truncate16((color >> 11) & 0x1Fu); - b = Truncate16((color >> 19) & 0x1Fu); - const u16 color15b = r | (g << 5) | (b << 10) | (a << 15); + color = m_block_rgb[i++]; + r = E8TO5(color & 0xFFu); + g = E8TO5((color >> 8) & 0xFFu); + b = E8TO5((color >> 16) & 0xFFu); + const u32 color15b = r | (g << 5) | (b << 10) | a; +#undef E8TO5 - m_data_out_fifo.Push(ZeroExtend32(color15a) | (ZeroExtend32(color15b) << 16)); + m_data_out_fifo.Push(color15a | (color15b << 16)); + } + } + else + { + const u16 a = ZeroExtend16(m_status.data_output_bit15.GetValue()) << 15; + for (u32 i = 0; i < static_cast(m_block_rgb.size());) + { + u32 color = m_block_rgb[i++]; + u16 r = Truncate16((color >> 3) & 0x1Fu); + u16 g = Truncate16((color >> 11) & 0x1Fu); + u16 b = Truncate16((color >> 19) & 0x1Fu); + const u16 color15a = r | (g << 5) | (b << 10) | (a << 15); + + color = m_block_rgb[i++]; + r = Truncate16((color >> 3) & 0x1Fu); + g = Truncate16((color >> 11) & 0x1Fu); + b = Truncate16((color >> 19) & 0x1Fu); + const u16 color15b = r | (g << 5) | (b << 10) | (a << 15); + + m_data_out_fifo.Push(ZeroExtend32(color15a) | (ZeroExtend32(color15b) << 16)); + } } } break; @@ -531,7 +549,7 @@ bool MDEC::rl_decode_block(s16* blk, const u8* qt) val = std::clamp(val, -0x400, 0x3FF); if (m_current_q_scale > 0) blk[zagzig[m_current_coefficient]] = static_cast(val); - else if (m_current_q_scale == 0) + else blk[m_current_coefficient] = static_cast(val); } @@ -554,7 +572,7 @@ bool MDEC::rl_decode_block(s16* blk, const u8* qt) val = std::clamp(val, -0x400, 0x3FF); if (m_current_q_scale > 0) blk[zagzig[m_current_coefficient]] = static_cast(val); - else if (m_current_q_scale == 0) + else blk[m_current_coefficient] = static_cast(val); } @@ -569,6 +587,42 @@ bool MDEC::rl_decode_block(s16* blk, const u8* qt) } void MDEC::IDCT(s16* blk) +{ + // people have made texture packs using the old conversion routines.. best to just leave them be. + if (!g_settings.use_old_mdec_routines) + IDCT_New(blk); + else + IDCT_Old(blk); +} + +void MDEC::IDCT_New(s16* blk) +{ + std::array temp; + for (u32 x = 0; x < 8; x++) + { + for (u32 y = 0; y < 8; y++) + { + // TODO: We could alter zigzag and invert scale_table to get these in row-major order, + // in which case we could do optimize this to a vector multiply. + s32 sum = 0; + for (u32 z = 0; z < 8; z++) + sum += s32(blk[y + z * 8]) * s32(m_scale_table[x + z * 8] / 8); + temp[x + y * 8] = static_cast((sum + 0xfff) / 0x2000); + } + } + for (u32 x = 0; x < 8; x++) + { + for (u32 y = 0; y < 8; y++) + { + s32 sum = 0; + for (u32 z = 0; z < 8; z++) + sum += temp[y + z * 8] * s32(m_scale_table[x + z * 8] / 8); + blk[x + y * 8] = static_cast(std::clamp((sum + 0xfff) / 0x2000, -128, 127)); + } + } +} + +void MDEC::IDCT_Old(s16* blk) { std::array temp_buffer; for (u32 x = 0; x < 8; x++) @@ -598,6 +652,7 @@ void MDEC::IDCT(s16* blk) void MDEC::yuv_to_rgb(u32 xx, u32 yy, const std::array& Crblk, const std::array& Cbblk, const std::array& Yblk) { + const s16 addval = m_status.data_output_signed ? 0 : 0x80; for (u32 y = 0; y < 8; y++) { for (u32 x = 0; x < 8; x++) @@ -610,14 +665,9 @@ void MDEC::yuv_to_rgb(u32 xx, u32 yy, const std::array& Crblk, const st B = static_cast(1.772f * static_cast(B)); s16 Y = Yblk[x + y * 8]; - R = static_cast(std::clamp(static_cast(Y) + R, -128, 127)); - G = static_cast(std::clamp(static_cast(Y) + G, -128, 127)); - B = static_cast(std::clamp(static_cast(Y) + B, -128, 127)); - - // TODO: Signed output - R += 128; - G += 128; - B += 128; + R = static_cast(std::clamp(static_cast(Y) + R, -128, 127)) + addval; + G = static_cast(std::clamp(static_cast(Y) + G, -128, 127)) + addval; + B = static_cast(std::clamp(static_cast(Y) + B, -128, 127)) + addval; m_block_rgb[(x + xx) + ((y + yy) * 16)] = ZeroExtend32(static_cast(R)) | (ZeroExtend32(static_cast(G)) << 8) | diff --git a/src/core/mdec.h b/src/core/mdec.h index 21d376c2..da00db4b 100644 --- a/src/core/mdec.h +++ b/src/core/mdec.h @@ -31,6 +31,7 @@ private: static constexpr u32 DATA_IN_FIFO_SIZE = 1024; static constexpr u32 DATA_OUT_FIFO_SIZE = 768; static constexpr u32 NUM_BLOCKS = 6; + static constexpr TickCount TICKS_PER_BLOCK = 448; enum DataOutputDepth : u8 { @@ -115,6 +116,8 @@ private: // from nocash spec bool rl_decode_block(s16* blk, const u8* qt); void IDCT(s16* blk); + void IDCT_New(s16* blk); + void IDCT_Old(s16* blk); void yuv_to_rgb(u32 xx, u32 yy, const std::array& Crblk, const std::array& Cbblk, const std::array& Yblk); void y_to_mono(const std::array& Yblk); diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 32d75df9..68eb1d27 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -229,6 +229,8 @@ void Settings::Load(SettingsInterface& si) audio_buffer_size = si.GetIntValue("Audio", "BufferSize", HostInterface::DEFAULT_AUDIO_BUFFER_SIZE); + use_old_mdec_routines = si.GetBoolValue("Hacks", "UseOldMDECRoutines", false); + dma_max_slice_ticks = si.GetIntValue("Hacks", "DMAMaxSliceTicks", DEFAULT_DMA_MAX_SLICE_TICKS); dma_halt_ticks = si.GetIntValue("Hacks", "DMAHaltTicks", DEFAULT_DMA_HALT_TICKS); gpu_fifo_size = static_cast(si.GetIntValue("Hacks", "GPUFIFOSize", DEFAULT_GPU_FIFO_SIZE)); diff --git a/src/core/settings.h b/src/core/settings.h index 822f946a..2e0d5369 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -127,6 +127,8 @@ struct Settings u32 audio_buffer_size = 2048; + bool use_old_mdec_routines = false; + // timing hacks section TickCount dma_max_slice_ticks = 1000; TickCount dma_halt_ticks = 100; diff --git a/src/libretro/libretro_core_options.h b/src/libretro/libretro_core_options.h index baeb0aad..678a601f 100644 --- a/src/libretro/libretro_core_options.h +++ b/src/libretro/libretro_core_options.h @@ -4552,6 +4552,20 @@ struct retro_core_option_v2_definition option_defs_us[] = { }, "false" }, + { + "duckstation_Hacks.UseOldMDECRoutines", + "Use Old MDEC Routines", + NULL, + "Use old routines for MDEC content.", + NULL, + "advanced", + { + { "true", "Enabled" }, + { "false", "Disabled" }, + { NULL, NULL }, + }, + "false" + }, { NULL, NULL, NULL, NULL, NULL, NULL, {{0}}, NULL }, };