From cacf7a7cc9268aac6da434e64c92af2ea0a96c6f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 20 Sep 2020 21:28:45 +1000 Subject: [PATCH] GPU: Precompute texture window expression [SAVEVERSION+] --- src/core/gpu.cpp | 23 +++++++++++++++-------- src/core/gpu.h | 10 +++++----- src/core/gpu_commands.cpp | 4 ---- src/core/gpu_hw.cpp | 8 ++++---- src/core/gpu_hw.h | 4 ++-- src/core/gpu_hw_shadergen.cpp | 12 ++++++------ src/core/gpu_sw.cpp | 6 ++---- src/core/save_state_version.h | 2 +- 8 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/core/gpu.cpp b/src/core/gpu.cpp index 719481e4a..850ff57d4 100644 --- a/src/core/gpu.cpp +++ b/src/core/gpu.cpp @@ -95,6 +95,7 @@ void GPU::SoftReset() m_fifo.Clear(); m_blit_buffer.clear(); m_blit_remaining_words = 0; + m_draw_mode.texture_window_value = 0xFFFFFFFFu; SetDrawMode(0); SetTexturePalette(0); SetTextureWindow(0); @@ -121,10 +122,10 @@ bool GPU::DoState(StateWrapper& sw) sw.Do(&m_draw_mode.texture_page_y); sw.Do(&m_draw_mode.texture_palette_x); sw.Do(&m_draw_mode.texture_palette_y); - sw.Do(&m_draw_mode.texture_window_mask_x); - sw.Do(&m_draw_mode.texture_window_mask_y); - sw.Do(&m_draw_mode.texture_window_offset_x); - sw.Do(&m_draw_mode.texture_window_offset_y); + sw.Do(&m_draw_mode.texture_window_and_x); + sw.Do(&m_draw_mode.texture_window_and_y); + sw.Do(&m_draw_mode.texture_window_or_x); + sw.Do(&m_draw_mode.texture_window_or_y); sw.Do(&m_draw_mode.texture_x_flip); sw.Do(&m_draw_mode.texture_y_flip); @@ -1316,10 +1317,16 @@ void GPU::SetTextureWindow(u32 value) FlushRender(); - m_draw_mode.texture_window_mask_x = value & UINT32_C(0x1F); - m_draw_mode.texture_window_mask_y = (value >> 5) & UINT32_C(0x1F); - m_draw_mode.texture_window_offset_x = (value >> 10) & UINT32_C(0x1F); - m_draw_mode.texture_window_offset_y = (value >> 15) & UINT32_C(0x1F); + const u8 mask_x = Truncate8(value & UINT32_C(0x1F)); + const u8 mask_y = Truncate8((value >> 5) & UINT32_C(0x1F)); + const u8 offset_x = Truncate8((value >> 10) & UINT32_C(0x1F)); + const u8 offset_y = Truncate8((value >> 15) & UINT32_C(0x1F)); + Log_DebugPrintf("Set texture window %02X %02X %02X %02X", mask_x, mask_y, offset_x, offset_y); + + m_draw_mode.texture_window_and_x = ~(mask_x * 8); + m_draw_mode.texture_window_and_y = ~(mask_y * 8); + m_draw_mode.texture_window_or_x = (offset_x & mask_x) * 8u; + m_draw_mode.texture_window_or_y = (offset_y & mask_y) * 8u; m_draw_mode.texture_window_value = value; m_draw_mode.texture_window_changed = true; } diff --git a/src/core/gpu.h b/src/core/gpu.h index eb7700319..61721c911 100644 --- a/src/core/gpu.h +++ b/src/core/gpu.h @@ -306,7 +306,7 @@ protected: }; // Sprites/rectangles should be clipped to 12 bits before drawing. - static constexpr s32 TruncateVertexPosition(s32 x) { return SignExtendN<11, s32>(x); } + static constexpr s32 TruncateVertexPosition(s32 x) { return SignExtendN<12, s32>(x); } struct NativeVertex { @@ -581,10 +581,10 @@ protected: u32 texture_page_y; u32 texture_palette_x; u32 texture_palette_y; - u8 texture_window_mask_x; // in 8 pixel steps - u8 texture_window_mask_y; // in 8 pixel steps - u8 texture_window_offset_x; // in 8 pixel steps - u8 texture_window_offset_y; // in 8 pixel steps + u8 texture_window_and_x; + u8 texture_window_and_y; + u8 texture_window_or_x; + u8 texture_window_or_y; bool texture_x_flip; bool texture_y_flip; bool texture_page_changed; diff --git a/src/core/gpu_commands.cpp b/src/core/gpu_commands.cpp index 1f379af3f..4c5716fa4 100644 --- a/src/core/gpu_commands.cpp +++ b/src/core/gpu_commands.cpp @@ -228,10 +228,6 @@ bool GPU::HandleSetTextureWindowCommand() { const u32 param = FifoPop() & 0x00FFFFFFu; SetTextureWindow(param); - Log_DebugPrintf("Set texture window %02X %02X %02X %02X", m_draw_mode.texture_window_mask_x, - m_draw_mode.texture_window_mask_y, m_draw_mode.texture_window_offset_x, - m_draw_mode.texture_window_offset_y); - AddCommandTicks(1); EndCommand(); return true; diff --git a/src/core/gpu_hw.cpp b/src/core/gpu_hw.cpp index b4b937bda..a75445eb6 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -948,10 +948,10 @@ void GPU_HW::DispatchRenderCommand() { m_draw_mode.ClearTextureWindowChangedFlag(); - m_batch_ubo_data.u_texture_window_mask[0] = ZeroExtend32(m_draw_mode.texture_window_mask_x); - m_batch_ubo_data.u_texture_window_mask[1] = ZeroExtend32(m_draw_mode.texture_window_mask_y); - m_batch_ubo_data.u_texture_window_offset[0] = ZeroExtend32(m_draw_mode.texture_window_offset_x); - m_batch_ubo_data.u_texture_window_offset[1] = ZeroExtend32(m_draw_mode.texture_window_offset_y); + m_batch_ubo_data.u_texture_window_and[0] = ZeroExtend32(m_draw_mode.texture_window_and_x); + m_batch_ubo_data.u_texture_window_and[1] = ZeroExtend32(m_draw_mode.texture_window_and_y); + m_batch_ubo_data.u_texture_window_or[0] = ZeroExtend32(m_draw_mode.texture_window_or_x); + m_batch_ubo_data.u_texture_window_or[1] = ZeroExtend32(m_draw_mode.texture_window_or_y); m_batch_ubo_dirty = true; } diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index 1d64de71d..9f9064849 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -119,8 +119,8 @@ protected: struct BatchUBOData { - u32 u_texture_window_mask[2]; - u32 u_texture_window_offset[2]; + u32 u_texture_window_and[2]; + u32 u_texture_window_or[2]; float u_src_alpha_factor; float u_dst_alpha_factor; u32 u_interlaced_displayed_field; diff --git a/src/core/gpu_hw_shadergen.cpp b/src/core/gpu_hw_shadergen.cpp index d0050c8d7..20d50cb4f 100644 --- a/src/core/gpu_hw_shadergen.cpp +++ b/src/core/gpu_hw_shadergen.cpp @@ -68,7 +68,7 @@ float4 RGBA5551ToRGBA8(uint v) void GPU_HW_ShaderGen::WriteBatchUniformBuffer(std::stringstream& ss) { DeclareUniformBuffer(ss, - {"uint2 u_texture_window_mask", "uint2 u_texture_window_offset", "float u_src_alpha_factor", + {"uint2 u_texture_window_and", "uint2 u_texture_window_or", "float u_src_alpha_factor", "float u_dst_alpha_factor", "uint u_interlaced_displayed_field", "bool u_set_mask_while_drawing"}, false); @@ -700,16 +700,16 @@ CONSTANT float4 TRANSPARENT_PIXEL_COLOR = float4(0.0, 0.0, 0.0, 0.0); uint2 ApplyTextureWindow(uint2 coords) { - uint x = (uint(coords.x) & ~(u_texture_window_mask.x * 8u)) | ((u_texture_window_offset.x & u_texture_window_mask.x) * 8u); - uint y = (uint(coords.y) & ~(u_texture_window_mask.y * 8u)) | ((u_texture_window_offset.y & u_texture_window_mask.y) * 8u); + uint x = (uint(coords.x) & u_texture_window_and.x) | u_texture_window_or.x; + uint y = (uint(coords.y) & u_texture_window_and.y) | u_texture_window_or.y; return uint2(x, y); } uint2 ApplyUpscaledTextureWindow(uint2 coords) { - uint x = (uint(coords.x) & ~(u_texture_window_mask.x * 8u * RESOLUTION_SCALE)) | ((u_texture_window_offset.x & u_texture_window_mask.x) * 8u * RESOLUTION_SCALE); - uint y = (uint(coords.y) & ~(u_texture_window_mask.y * 8u * RESOLUTION_SCALE)) | ((u_texture_window_offset.y & u_texture_window_mask.y) * 8u * RESOLUTION_SCALE); - return uint2(x, y); + uint2 native_coords = coords / uint2(RESOLUTION_SCALE, RESOLUTION_SCALE); + uint2 coords_offset = coords % uint2(RESOLUTION_SCALE, RESOLUTION_SCALE); + return (ApplyTextureWindow(native_coords) * uint2(RESOLUTION_SCALE, RESOLUTION_SCALE)) + coords_offset; } uint2 FloatToIntegerCoords(float2 coords) diff --git a/src/core/gpu_sw.cpp b/src/core/gpu_sw.cpp index c0edc3601..1a4503743 100644 --- a/src/core/gpu_sw.cpp +++ b/src/core/gpu_sw.cpp @@ -602,10 +602,8 @@ void GPU_SW::ShadePixel(u32 x, u32 y, u8 color_r, u8 color_g, u8 color_b, u8 tex { // Apply texture window // TODO: Precompute the second half - texcoord_x = (texcoord_x & ~(m_draw_mode.texture_window_mask_x * 8u)) | - ((m_draw_mode.texture_window_offset_x & m_draw_mode.texture_window_mask_x) * 8u); - texcoord_y = (texcoord_y & ~(m_draw_mode.texture_window_mask_y * 8u)) | - ((m_draw_mode.texture_window_offset_y & m_draw_mode.texture_window_mask_y) * 8u); + texcoord_x = (texcoord_x & m_draw_mode.texture_window_and_x) | m_draw_mode.texture_window_or_x; + texcoord_y = (texcoord_y & m_draw_mode.texture_window_and_y) | m_draw_mode.texture_window_or_y; VRAMPixel texture_color; switch (m_draw_mode.GetTextureMode()) diff --git a/src/core/save_state_version.h b/src/core/save_state_version.h index b182af9b1..9e67dffab 100644 --- a/src/core/save_state_version.h +++ b/src/core/save_state_version.h @@ -2,7 +2,7 @@ #include "types.h" static constexpr u32 SAVE_STATE_MAGIC = 0x43435544; -static constexpr u32 SAVE_STATE_VERSION = 40; +static constexpr u32 SAVE_STATE_VERSION = 41; #pragma pack(push, 4) struct SAVE_STATE_HEADER