From 6566e665df6edf1465076d32e187fa4d4e311d2c Mon Sep 17 00:00:00 2001 From: Jake Stine Date: Wed, 19 May 2021 20:40:49 -0700 Subject: [PATCH] 1X and 4X Software Up-Rendering Feature Added (#43) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 2X and 4X Software Up-Rendering Feature Added What this change includes: - 2X software uprender - 4X software uprender (mostly untested, but yolo) - significant perf improvements to all SW rendering, including native res - config setting '[GPU].SoftwareUprender' What this change is missing: - UI integration. It can only be enabled from the settings.ini file at the moment - Testing. I only checked four games so far * SW Uprender 2x Fixed. Mainly two typos, one in SetPixel, and one in a template parameter, caused the problems. I did a bunch of var renaming and cleanup along the way. * Add preliminary support for on-the-fly resolution change, rename and refactor some vars * SW uprender: simplified so HW/SW share same setting. * Fixed crash caused by bad malloc from on-the-fly res change refactor * Fixed another crash. Bad math typo. * Fix mode bad math in ReadVRAM * Fix 4x rendering (mostly) - gradients are off, but could be SW integer precision issue. hooks up proper software display buffer texture allocation. * Fix FMVs rendering on host 16bpp display buffers This bug varies by user's PC hardware and OS. Hard to verify and reproduce. >_< * Fix hardcoded upscale factor * Reduce diff size a bit Co-authored-by: Jean-André Santoni --- src/core/gpu.cpp | 14 +- src/core/gpu.h | 5 +- src/core/gpu_backend.cpp | 14 + src/core/gpu_backend.h | 8 +- src/core/gpu_commands.cpp | 1 + src/core/gpu_hw.cpp | 6 +- src/core/gpu_hw.h | 2 + src/core/gpu_hw_d3d11.cpp | 2 +- src/core/gpu_hw_d3d12.cpp | 2 +- src/core/gpu_hw_opengl.cpp | 2 +- src/core/gpu_hw_vulkan.cpp | 2 +- src/core/gpu_sw.cpp | 242 +++++--- src/core/gpu_sw.h | 7 +- src/core/gpu_sw_backend.cpp | 1122 ++++++++++++++++++++++++----------- src/core/gpu_sw_backend.h | 174 +++++- src/core/gpu_types.h | 9 + 16 files changed, 1149 insertions(+), 463 deletions(-) diff --git a/src/core/gpu.cpp b/src/core/gpu.cpp index a5a1b90f..0b65c8b7 100644 --- a/src/core/gpu.cpp +++ b/src/core/gpu.cpp @@ -235,7 +235,7 @@ bool GPU::DoState(StateWrapper& sw, HostDisplayTexture** host_texture, bool upda else { ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT); - sw.DoBytes(m_vram_ptr, VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); + sw.DoBytes(GetVRAMshadowPtr(), VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); } } @@ -973,6 +973,8 @@ u32 GPU::ReadGPUREAD() if (m_blitter_state != BlitterState::ReadingVRAM) return m_GPUREAD_latch; + auto m_vram_ptr = GetVRAMshadowPtr(); + // Read two pixels out of VRAM and combine them. Zero fill odd pixel counts. u32 value = 0; for (u32 i = 0; i < 2; i++) @@ -1251,6 +1253,8 @@ void GPU::ReadVRAM(u32 x, u32 y, u32 width, u32 height) {} void GPU::FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color) { + auto m_vram_ptr = GetVRAMshadowPtr(); + const u16 color16 = VRAMRGBA8888ToRGBA5551(color); if ((x + width) <= VRAM_WIDTH && !IsInterlacedRenderingEnabled()) { @@ -1298,6 +1302,8 @@ void GPU::FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color) void GPU::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, bool set_mask, bool check_mask) { + auto m_vram_ptr = GetVRAMshadowPtr(); + // Fast path when the copy is not oversized. if ((x + width) <= VRAM_WIDTH && (y + height) <= VRAM_HEIGHT && !set_mask && !check_mask) { @@ -1334,6 +1340,8 @@ void GPU::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, bool void GPU::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height) { + auto m_vram_ptr = GetVRAMshadowPtr(); + // Break up oversized copies. This behavior has not been verified on console. if ((src_x + width) > VRAM_WIDTH || (dst_x + width) > VRAM_WIDTH) { @@ -1478,11 +1486,11 @@ bool GPU::DumpVRAMToFile(const char* filename) const char* extension = std::strrchr(filename, '.'); if (extension && StringUtil::Strcasecmp(extension, ".png") == 0) { - return DumpVRAMToFile(filename, VRAM_WIDTH, VRAM_HEIGHT, sizeof(u16) * VRAM_WIDTH, m_vram_ptr, true); + return DumpVRAMToFile(filename, VRAM_WIDTH, VRAM_HEIGHT, sizeof(u16) * VRAM_WIDTH, GetVRAMshadowPtr(), true); } else if (extension && StringUtil::Strcasecmp(extension, ".bin") == 0) { - return FileSystem::WriteBinaryFile(filename, m_vram_ptr, VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); + return FileSystem::WriteBinaryFile(filename, GetVRAMshadowPtr(), VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); } else { diff --git a/src/core/gpu.h b/src/core/gpu.h index 7a1272dc..4a3cafd4 100644 --- a/src/core/gpu.h +++ b/src/core/gpu.h @@ -75,6 +75,9 @@ public: virtual GPURenderer GetRendererType() const = 0; + virtual u16* GetVRAMshadowPtr() = 0; + + virtual bool Initialize(HostDisplay* host_display); virtual void Reset(bool clear_vram); virtual bool DoState(StateWrapper& sw, HostDisplayTexture** save_to_texture, bool update_display); @@ -321,7 +324,7 @@ protected: std::unique_ptr m_command_tick_event; // Pointer to VRAM, used for reads/writes. In the hardware backends, this is the shadow buffer. - u16* m_vram_ptr = nullptr; + //u16* m_vram_ptr = nullptr; union GPUSTAT { diff --git a/src/core/gpu_backend.cpp b/src/core/gpu_backend.cpp index 42673e31..a7769afd 100644 --- a/src/core/gpu_backend.cpp +++ b/src/core/gpu_backend.cpp @@ -44,6 +44,12 @@ void GPUBackend::Shutdown() StopGPUThread(); } +GPUBackendReadVRAMCommand* GPUBackend::NewReadVRAMCommand() +{ + return static_cast( + AllocateCommand(GPUBackendCommandType::ReadVRAM, sizeof(GPUBackendReadVRAMCommand))); +} + GPUBackendFillVRAMCommand* GPUBackend::NewFillVRAMCommand() { return static_cast( @@ -272,6 +278,14 @@ void GPUBackend::HandleCommand(const GPUBackendCommand* cmd) { switch (cmd->type) { + case GPUBackendCommandType::ReadVRAM: + { + FlushRender(); + const auto* ccmd = static_cast(cmd); + ReadVRAM(ZeroExtend32(ccmd->x), ZeroExtend32(ccmd->y), ZeroExtend32(ccmd->width), ZeroExtend32(ccmd->height)); + } + break; + case GPUBackendCommandType::FillVRAM: { FlushRender(); diff --git a/src/core/gpu_backend.h b/src/core/gpu_backend.h index e8b9d0d8..b96d05f4 100644 --- a/src/core/gpu_backend.h +++ b/src/core/gpu_backend.h @@ -19,13 +19,14 @@ public: GPUBackend(); virtual ~GPUBackend(); - ALWAYS_INLINE u16* GetVRAM() const { return m_vram_ptr; } + virtual u16* GetVRAMshadowPtr() = 0; virtual bool Initialize(bool force_thread); virtual void UpdateSettings(); virtual void Reset(bool clear_vram); virtual void Shutdown(); + GPUBackendReadVRAMCommand* NewReadVRAMCommand(); GPUBackendFillVRAMCommand* NewFillVRAMCommand(); GPUBackendUpdateVRAMCommand* NewUpdateVRAMCommand(u32 num_words); GPUBackendCopyVRAMCommand* NewCopyVRAMCommand(); @@ -35,7 +36,7 @@ public: GPUBackendDrawLineCommand* NewDrawLineCommand(u32 num_vertices); void PushCommand(GPUBackendCommand* cmd); - void Sync(bool allow_sleep); + virtual void Sync(bool allow_sleep); /// Processes all pending GPU commands. void RunGPULoop(); @@ -47,6 +48,7 @@ protected: void StartGPUThread(); void StopGPUThread(); + virtual void ReadVRAM(u32 x, u32 y, u32 width, u32 height) {} virtual void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color, GPUBackendCommandParameters params) = 0; virtual void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, GPUBackendCommandParameters params) = 0; @@ -60,8 +62,6 @@ protected: void HandleCommand(const GPUBackendCommand* cmd); - u16* m_vram_ptr = nullptr; - Common::Rectangle m_drawing_area{}; Common::Event m_sync_event; diff --git a/src/core/gpu_commands.cpp b/src/core/gpu_commands.cpp index a2586ab7..e1d8b6aa 100644 --- a/src/core/gpu_commands.cpp +++ b/src/core/gpu_commands.cpp @@ -577,6 +577,7 @@ bool GPU::HandleCopyRectangleVRAMToCPUCommand() if (g_settings.debugging.dump_vram_to_cpu_copies) { + auto m_vram_ptr = GetVRAMshadowPtr(); DumpVRAMToFile(StringUtil::StdStringFromFormat("vram_to_cpu_copy_%u.png", s_vram_to_cpu_dump_id++).c_str(), m_vram_transfer.width, m_vram_transfer.height, sizeof(u16) * VRAM_WIDTH, &m_vram_ptr[m_vram_transfer.y * VRAM_WIDTH + m_vram_transfer.x], true); diff --git a/src/core/gpu_hw.cpp b/src/core/gpu_hw.cpp index e3b4efa3..8bf85562 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -33,7 +33,6 @@ ALWAYS_INLINE static bool ShouldUseUVLimits() GPU_HW::GPU_HW() : GPU() { - m_vram_ptr = m_vram_shadow.data(); } GPU_HW::~GPU_HW() @@ -1119,7 +1118,7 @@ void GPU_HW::UpdateSoftwareRenderer(bool copy_vram_from_hw) if (current_enabled == new_enabled) return; - m_vram_ptr = m_vram_shadow.data(); + auto m_vram_ptr = GetVRAMshadowPtr(); if (!new_enabled) { @@ -1138,7 +1137,7 @@ void GPU_HW::UpdateSoftwareRenderer(bool copy_vram_from_hw) { FlushRender(); ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT); - std::memcpy(sw_renderer->GetVRAM(), m_vram_ptr, sizeof(u16) * VRAM_WIDTH * VRAM_HEIGHT); + std::memcpy(sw_renderer->GetVRAMshadowPtr(), m_vram_ptr, sizeof(u16) * VRAM_WIDTH * VRAM_HEIGHT); // Sync the drawing area. GPUBackendSetDrawingAreaCommand* cmd = sw_renderer->NewSetDrawingAreaCommand(); @@ -1147,7 +1146,6 @@ void GPU_HW::UpdateSoftwareRenderer(bool copy_vram_from_hw) } m_sw_renderer = std::move(sw_renderer); - m_vram_ptr = m_sw_renderer->GetVRAM(); } void GPU_HW::FillBackendCommandParameters(GPUBackendCommand* cmd) const diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index 68917653..decc905d 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -40,6 +40,8 @@ public: void UpdateResolutionScale() override final; std::tuple GetEffectiveDisplayResolution() override final; + u16* GetVRAMshadowPtr() override { return m_vram_shadow.data(); } + protected: enum : u32 { diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 716f611f..5e696efc 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -193,7 +193,7 @@ void GPU_HW_D3D11::UpdateSettings() if (framebuffer_changed) { RestoreGraphicsAPIState(); - UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, GetVRAMshadowPtr(), false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index ca7262c6..ff967d5c 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -161,7 +161,7 @@ void GPU_HW_D3D12::UpdateSettings() if (framebuffer_changed) { RestoreGraphicsAPIState(); - UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, GetVRAMshadowPtr(), false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_hw_opengl.cpp b/src/core/gpu_hw_opengl.cpp index 88c6f769..9f1d9746 100644 --- a/src/core/gpu_hw_opengl.cpp +++ b/src/core/gpu_hw_opengl.cpp @@ -261,7 +261,7 @@ void GPU_HW_OpenGL::UpdateSettings() if (framebuffer_changed) { RestoreGraphicsAPIState(); - UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, GetVRAMshadowPtr(), false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index 258aeba3..b5168de1 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -233,7 +233,7 @@ void GPU_HW_Vulkan::UpdateSettings() if (framebuffer_changed) { RestoreGraphicsAPIState(); - UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, GetVRAMshadowPtr(), false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_sw.cpp b/src/core/gpu_sw.cpp index 980735ff..f04b1970 100644 --- a/src/core/gpu_sw.cpp +++ b/src/core/gpu_sw.cpp @@ -28,9 +28,10 @@ ALWAYS_INLINE static constexpr std::tuple MinMax(T v1, T v2) return std::tie(v1, v2); } +#define GetVramDisplayMemPtr() m_backend.GetUPRAM() + GPU_SW::GPU_SW() { - m_vram_ptr = m_backend.GetVRAM(); } GPU_SW::~GPU_SW() @@ -38,6 +39,9 @@ GPU_SW::~GPU_SW() m_backend.Shutdown(); if (m_host_display) m_host_display->ClearDisplayTexture(); + + if (m_display_texture_buffer) + free(m_display_texture_buffer); } GPURenderer GPU_SW::GetRendererType() const @@ -45,11 +49,34 @@ GPURenderer GPU_SW::GetRendererType() const return GPURenderer::Software; } +static ptrdiff_t getDisplayTextureSizeInBytes(int scale) +{ + return GPU_MAX_DISPLAY_WIDTH * GPU_MAX_DISPLAY_HEIGHT * scale * scale * sizeof(u32); +} + + +void GPU_SW::AllocDisplayBuffer() +{ + auto backend_uprender = m_backend.uprender_scale(); + + // backend will make some choices about what uprender it supports, so make sure it gets init'd first! + DebugAssert(backend_uprender >= 1); + + if (m_display_texture_buffer) + free(m_display_texture_buffer); + m_display_texture_buffer = nullptr; + + m_display_texture_scale = backend_uprender; + m_display_texture_buffer = (u8*)malloc(getDisplayTextureSizeInBytes(m_display_texture_scale)); +} + bool GPU_SW::Initialize(HostDisplay* host_display) { if (!GPU::Initialize(host_display) || !m_backend.Initialize(false)) return false; + AllocDisplayBuffer(); + static constexpr auto formats_for_16bit = make_array(HostDisplayPixelFormat::RGB565, HostDisplayPixelFormat::RGBA5551, HostDisplayPixelFormat::RGBA8, HostDisplayPixelFormat::BGRA8); static constexpr auto formats_for_24bit = @@ -92,6 +119,7 @@ void GPU_SW::UpdateSettings() { GPU::UpdateSettings(); m_backend.UpdateSettings(); + AllocDisplayBuffer(); } template @@ -223,17 +251,28 @@ ALWAYS_INLINE void CopyOutRow16(const u16* s } template -void GPU_SW::CopyOut15Bit(u32 src_x, u32 src_y, u32 width, u32 height, u32 field, bool interlaced, bool interleaved) +void GPU_SW::CopyOut15Bit(u32 src_x_native, u32 src_y_native, u32 width_native, u32 height_native, u32 field, bool interlaced, bool interleaved) { u8* dst_ptr; u32 dst_stride; + // FIXME - Uprender - architecturally all this stuff should be implemented in GPU_SW_Backend or -- better yet -- should be generalized + // and not part of any monolithic class. But hat would cause huge diff-changes across this module so for now I'm just replicating a + // bunch of GPU_SW_BACH paramaters into local var space so we can do what we need to do. + + auto uprender_scale = m_backend.uprender_scale(); + auto vram_upsize_x = VRAM_WIDTH * uprender_scale; + auto vram_upsize_y = VRAM_HEIGHT * uprender_scale; + + auto width_up = width_native * uprender_scale; + auto height_up = height_native * uprender_scale; + using OutputPixelType = std::conditional_t< display_format == HostDisplayPixelFormat::RGBA8 || display_format == HostDisplayPixelFormat::BGRA8, u32, u16>; if (!interlaced) { - if (!m_host_display->BeginSetDisplayPixels(display_format, width, height, reinterpret_cast(&dst_ptr), + if (!m_host_display->BeginSetDisplayPixels(display_format, width_up, height_up, reinterpret_cast(&dst_ptr), &dst_stride)) { return; @@ -241,44 +280,49 @@ void GPU_SW::CopyOut15Bit(u32 src_x, u32 src_y, u32 width, u32 height, u32 field } else { - dst_stride = GPU_MAX_DISPLAY_WIDTH * sizeof(OutputPixelType); - dst_ptr = m_display_texture_buffer.data() + (field != 0 ? dst_stride : 0); + dst_stride = GPU_MAX_DISPLAY_WIDTH * m_display_texture_scale * sizeof(OutputPixelType); + dst_ptr = m_display_texture_buffer + (field != 0 ? dst_stride : 0); } const u32 output_stride = dst_stride; const u8 interlaced_shift = BoolToUInt8(interlaced); const u8 interleaved_shift = BoolToUInt8(interleaved); + auto src_x_up = src_x_native * uprender_scale; + auto src_y_up = src_y_native * uprender_scale; + + auto m_vram_ptr = GetVramDisplayMemPtr(); + // Fast path when not wrapping around. - if ((src_x + width) <= VRAM_WIDTH && (src_y + height) <= VRAM_HEIGHT) + if ((src_x_native + width_native) <= VRAM_WIDTH && (src_y_native + height_native) <= VRAM_HEIGHT) { - const u32 rows = height >> interlaced_shift; + const u32 rows = height_up >> interlaced_shift; dst_stride <<= interlaced_shift; - const u16* src_ptr = &m_vram_ptr[src_y * VRAM_WIDTH + src_x]; - const u32 src_step = VRAM_WIDTH << interleaved_shift; + const u16* src_ptr = &m_vram_ptr[src_y_up * vram_upsize_x + src_x_up]; + const u32 src_step = vram_upsize_x << interleaved_shift; for (u32 row = 0; row < rows; row++) { - CopyOutRow16(src_ptr, reinterpret_cast(dst_ptr), width); + CopyOutRow16(src_ptr, reinterpret_cast(dst_ptr), width_up); src_ptr += src_step; dst_ptr += dst_stride; } } else { - const u32 rows = height >> interlaced_shift; + const u32 rows = height_up >> interlaced_shift; dst_stride <<= interlaced_shift; - const u32 end_x = src_x + width; + const u32 end_x_up = src_x_up + width_up; for (u32 row = 0; row < rows; row++) { - const u16* src_row_ptr = &m_vram_ptr[(src_y % VRAM_HEIGHT) * VRAM_WIDTH]; + const u16* src_row_ptr = &m_vram_ptr[(src_y_up % vram_upsize_y) * vram_upsize_x]; OutputPixelType* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = src_x; col < end_x; col++) - *(dst_row_ptr++) = VRAM16ToOutput(src_row_ptr[col % VRAM_WIDTH]); + for (u32 col = src_x_up; col < end_x_up; col++) + *(dst_row_ptr++) = VRAM16ToOutput(src_row_ptr[col % vram_upsize_x]); - src_y += (1 << interleaved_shift); + src_y_up += (1 << interleaved_shift); dst_ptr += dst_stride; } } @@ -289,7 +333,7 @@ void GPU_SW::CopyOut15Bit(u32 src_x, u32 src_y, u32 width, u32 height, u32 field } else { - m_host_display->SetDisplayPixels(display_format, width, height, m_display_texture_buffer.data(), output_stride); + m_host_display->SetDisplayPixels(display_format, width_up, height_up, m_display_texture_buffer, output_stride); } } @@ -316,18 +360,36 @@ void GPU_SW::CopyOut15Bit(HostDisplayPixelFormat display_format, u32 src_x, u32 } template -void GPU_SW::CopyOut24Bit(u32 src_x, u32 src_y, u32 skip_x, u32 width, u32 height, u32 field, bool interlaced, +void GPU_SW::CopyOut24Bit(u32 src_x_native, u32 src_y_native, u32 skip_x_native, u32 width_native, u32 height_native, u32 field, bool interlaced, bool interleaved) { u8* dst_ptr; u32 dst_stride; + auto uprender_scale = m_backend.uprender_scale(); + + auto width_up = width_native * uprender_scale; + auto height_up = height_native * uprender_scale; + auto src_x_up = src_x_native * uprender_scale; + auto src_y_up = src_y_native * uprender_scale; + auto skip_x_up = skip_x_native * uprender_scale; + + // 24 bit modes must be sampled either from a native res 'shadow copy', or by adapting + // logic to handle the upres swizzle (due to PSX VMEM being 16-bits natively): + // + // 1 2 3 4 5 6 7 8 9 10 + // RG RG BR BR GB GB RG RG BR BR + // RG RG BR BR GB GB RG RG BR BR + // + // Other portions of the GPU depend on the shadow copy being up-to-date anyway, so let's + // just read from that for now. + using OutputPixelType = std::conditional_t< display_format == HostDisplayPixelFormat::RGBA8 || display_format == HostDisplayPixelFormat::BGRA8, u32, u16>; if (!interlaced) { - if (!m_host_display->BeginSetDisplayPixels(display_format, width, height, reinterpret_cast(&dst_ptr), + if (!m_host_display->BeginSetDisplayPixels(display_format, width_up, height_up, reinterpret_cast(&dst_ptr), &dst_stride)) { return; @@ -335,85 +397,76 @@ void GPU_SW::CopyOut24Bit(u32 src_x, u32 src_y, u32 skip_x, u32 width, u32 heigh } else { - dst_stride = Common::AlignUpPow2(width * sizeof(OutputPixelType), 4); - dst_ptr = m_display_texture_buffer.data() + (field != 0 ? dst_stride : 0); + dst_stride = GPU_MAX_DISPLAY_WIDTH * m_display_texture_scale * sizeof(OutputPixelType); + dst_ptr = m_display_texture_buffer + (field != 0 ? dst_stride : 0); } const u32 output_stride = dst_stride; const u8 interlaced_shift = BoolToUInt8(interlaced); const u8 interleaved_shift = BoolToUInt8(interleaved); - const u32 rows = height >> interlaced_shift; + const u32 rows_native = height_native >> interlaced_shift; + dst_stride <<= interlaced_shift; - if ((src_x + width) <= VRAM_WIDTH && (src_y + (rows << interleaved_shift)) <= VRAM_HEIGHT) - { - const u8* src_ptr = reinterpret_cast(&m_vram_ptr[src_y * VRAM_WIDTH + src_x]) + (skip_x * 3); - const u32 src_stride = (VRAM_WIDTH << interleaved_shift) * sizeof(u16); - for (u32 row = 0; row < rows; row++) - { - if constexpr (display_format == HostDisplayPixelFormat::RGBA8) - { - const u8* src_row_ptr = src_ptr; - u8* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = 0; col < width; col++) - { - *(dst_row_ptr++) = *(src_row_ptr++); - *(dst_row_ptr++) = *(src_row_ptr++); - *(dst_row_ptr++) = *(src_row_ptr++); - *(dst_row_ptr++) = 0xFF; - } - } - else if constexpr (display_format == HostDisplayPixelFormat::BGRA8) - { - const u8* src_row_ptr = src_ptr; - u8* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = 0; col < width; col++) - { - *(dst_row_ptr++) = src_row_ptr[2]; - *(dst_row_ptr++) = src_row_ptr[1]; - *(dst_row_ptr++) = src_row_ptr[0]; - *(dst_row_ptr++) = 0xFF; - src_row_ptr += 3; - } - } - else if constexpr (display_format == HostDisplayPixelFormat::RGB565) - { - const u8* src_row_ptr = src_ptr; - u16* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = 0; col < width; col++) - { - *(dst_row_ptr++) = ((static_cast(src_row_ptr[0]) >> 3) << 11) | - ((static_cast(src_row_ptr[1]) >> 2) << 5) | (static_cast(src_row_ptr[2]) >> 3); - src_row_ptr += 3; - } - } - else if constexpr (display_format == HostDisplayPixelFormat::RGBA5551) - { - const u8* src_row_ptr = src_ptr; - u16* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = 0; col < width; col++) - { - *(dst_row_ptr++) = ((static_cast(src_row_ptr[0]) >> 3) << 10) | - ((static_cast(src_row_ptr[1]) >> 3) << 5) | (static_cast(src_row_ptr[2]) >> 3); - src_row_ptr += 3; - } - } + auto shadow_ptr = m_backend.GetVRAMshadowPtr(); - src_ptr += src_stride; - dst_ptr += dst_stride; + if ((src_x_native + width_native) <= VRAM_WIDTH && (src_y_native + (rows_native << interleaved_shift)) <= VRAM_HEIGHT) + { + const u8* src_ptr = reinterpret_cast(&shadow_ptr[(src_y_native * VRAM_WIDTH) + src_x_native]) + (skip_x_native * 3); + const u32 src_stride = (VRAM_WIDTH << interleaved_shift) * sizeof(u16); + for (u32 row = 0; row < rows_native; row++, src_ptr += src_stride) + { + for (int uy = 0; uy < uprender_scale; ++uy, dst_ptr += dst_stride) + { + u8 const *src_row_ptr = src_ptr; + u8 *dst_row_p8 = reinterpret_cast(dst_ptr); + u16 *dst_row_p16 = reinterpret_cast(dst_ptr); + + for (u32 col = 0; col < width_native; col++, src_row_ptr+=3) + { + for (int ux = 0; ux < uprender_scale; ++ux) + { + if constexpr (display_format == HostDisplayPixelFormat::RGBA8) + { + *(dst_row_p8++) = src_row_ptr[0]; + *(dst_row_p8++) = src_row_ptr[1]; + *(dst_row_p8++) = src_row_ptr[2]; + *(dst_row_p8++) = 0xFF; + } + else if constexpr (display_format == HostDisplayPixelFormat::BGRA8) + { + *(dst_row_p8++) = src_row_ptr[2]; + *(dst_row_p8++) = src_row_ptr[1]; + *(dst_row_p8++) = src_row_ptr[0]; + *(dst_row_p8++) = 0xFF; + } + else if constexpr (display_format == HostDisplayPixelFormat::RGB565) + { + *(dst_row_p16++) = ((static_cast(src_row_ptr[0]) >> 3) << 11) | + ((static_cast(src_row_ptr[1]) >> 2) << 5 ) | (static_cast(src_row_ptr[2]) >> 3); + } + else if constexpr (display_format == HostDisplayPixelFormat::RGBA5551) + { + *(dst_row_p16++) = ((static_cast(src_row_ptr[0]) >> 3) << 10) | + ((static_cast(src_row_ptr[1]) >> 3) << 5 ) | (static_cast(src_row_ptr[2]) >> 3); + } + } + } + } } } else { - for (u32 row = 0; row < rows; row++) + // FIXME : UPRENDER : This has not been implemented yet. + for (u32 row = 0; row < rows_native; row++) { - const u16* src_row_ptr = &m_vram_ptr[(src_y % VRAM_HEIGHT) * VRAM_WIDTH]; + const u16* src_row_ptr = &shadow_ptr[(src_y_native % VRAM_HEIGHT) * VRAM_WIDTH]; OutputPixelType* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = 0; col < width; col++) + for (u32 col = 0; col < width_up; col++) { - const u32 offset = (src_x + (((skip_x + col) * 3) / 2)); - const u16 s0 = src_row_ptr[offset % VRAM_WIDTH]; + const u32 offset = (src_x_up + (((skip_x_up + col) * 3) / 2)); + const u16 s0 = src_row_ptr[(offset + 0) % VRAM_WIDTH]; const u16 s1 = src_row_ptr[(offset + 1) % VRAM_WIDTH]; const u8 shift = static_cast(col & 1u) * 8; const u32 rgb = (((ZeroExtend32(s1) << 16) | ZeroExtend32(s0)) >> shift); @@ -436,7 +489,7 @@ void GPU_SW::CopyOut24Bit(u32 src_x, u32 src_y, u32 skip_x, u32 width, u32 heigh } } - src_y += (1 << interleaved_shift); + src_y_up += (1 << interleaved_shift); dst_ptr += dst_stride; } } @@ -447,7 +500,7 @@ void GPU_SW::CopyOut24Bit(u32 src_x, u32 src_y, u32 skip_x, u32 width, u32 heigh } else { - m_host_display->SetDisplayPixels(display_format, width, height, m_display_texture_buffer.data(), output_stride); + m_host_display->SetDisplayPixels(display_format, width_up, height_up, m_display_texture_buffer, output_stride); } } @@ -476,14 +529,22 @@ void GPU_SW::CopyOut24Bit(HostDisplayPixelFormat display_format, u32 src_x, u32 void GPU_SW::ClearDisplay() { - std::memset(m_display_texture_buffer.data(), 0, m_display_texture_buffer.size()); + if (m_display_texture_buffer) + std::memset(m_display_texture_buffer, 0, getDisplayTextureSizeInBytes(m_display_texture_scale)); } void GPU_SW::UpdateDisplay() { + if (!m_display_texture_buffer) + return; + // fill display texture m_backend.Sync(true); + auto uprender_scale = m_backend.uprender_scale(); + auto vram_upsize_x = VRAM_WIDTH * uprender_scale; + auto vram_upsize_y = VRAM_HEIGHT * uprender_scale; + if (!g_settings.debugging.show_vram) { if (IsDisplayDisabled()) @@ -534,8 +595,8 @@ void GPU_SW::UpdateDisplay() else { CopyOut15Bit(m_16bit_display_format, 0, 0, VRAM_WIDTH, VRAM_HEIGHT, 0, false, false); - m_host_display->SetDisplayParameters(VRAM_WIDTH, VRAM_HEIGHT, 0, 0, VRAM_WIDTH, VRAM_HEIGHT, - static_cast(VRAM_WIDTH) / static_cast(VRAM_HEIGHT)); + m_host_display->SetDisplayParameters(vram_upsize_x, vram_upsize_y, 0, 0, vram_upsize_x, vram_upsize_y, + static_cast(vram_upsize_x) / static_cast(vram_upsize_y)); } } @@ -824,6 +885,13 @@ void GPU_SW::DispatchRenderCommand() void GPU_SW::ReadVRAM(u32 x, u32 y, u32 width, u32 height) { + auto* cmd = m_backend.NewReadVRAMCommand(); + FillBackendCommandParameters(cmd); + cmd->x = static_cast(x); + cmd->y = static_cast(y); + cmd->width = static_cast(width); + cmd->height = static_cast(height); + m_backend.PushCommand(cmd); m_backend.Sync(false); } diff --git a/src/core/gpu_sw.h b/src/core/gpu_sw.h index 6ba407e3..7e3a17d5 100644 --- a/src/core/gpu_sw.h +++ b/src/core/gpu_sw.h @@ -17,11 +17,15 @@ public: GPURenderer GetRendererType() const override; + u16* GetVRAMshadowPtr() override { return m_backend.GetVRAMshadowPtr(); } + bool Initialize(HostDisplay* host_display) override; bool DoState(StateWrapper& sw, HostDisplayTexture** host_texture, bool update_display) override; void Reset(bool clear_vram) override; void UpdateSettings() override; + void AllocDisplayBuffer(); + protected: void ReadVRAM(u32 x, u32 y, u32 width, u32 height) override; void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color) override; @@ -47,7 +51,8 @@ protected: void FillBackendCommandParameters(GPUBackendCommand* cmd) const; void FillDrawCommand(GPUBackendDrawCommand* cmd, GPURenderCommand rc) const; - HeapArray m_display_texture_buffer; + int m_display_texture_scale = 0; // mirrors backbuffer uprender scale + u8* m_display_texture_buffer = nullptr; HostDisplayPixelFormat m_16bit_display_format = HostDisplayPixelFormat::RGB565; HostDisplayPixelFormat m_24bit_display_format = HostDisplayPixelFormat::RGBA8; diff --git a/src/core/gpu_sw_backend.cpp b/src/core/gpu_sw_backend.cpp index c32c23b6..765a1747 100644 --- a/src/core/gpu_sw_backend.cpp +++ b/src/core/gpu_sw_backend.cpp @@ -5,27 +5,107 @@ #include "host_display.h" #include "system.h" #include +#include + Log_SetChannel(GPU_SW_Backend); +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4244) // warning C4324: structure was padded due to alignment specifier +#pragma warning(disable : 4189) // warning C4189: local variable is initialized but not referenced +#endif + GPU_SW_Backend::GPU_SW_Backend() : GPUBackend() { - m_vram.fill(0); - m_vram_ptr = m_vram.data(); } -GPU_SW_Backend::~GPU_SW_Backend() = default; +static ptrdiff_t getVRAMSizeInBytes(int scale) +{ + return VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16) * (scale*scale); +} + +void GPU_SW_Backend::SetUprenderScale(int scale) +{ + switch (scale) + { + case 1: + case 2: + case 4: + // OK! + break; + + case 3: + scale = 4; + break; + + default: + // if the input scale is invalid then default to either the current setting (if valid) + // or native res if vram memory is not initialized yet. + if (scale > 4) + scale = 4; + else if (!m_upram) + scale = 1; + else + return; + } + + // TODO: capture current VRAM and re-upload after resolution change. + + + auto new_upshift = (int)log2(scale); + if (m_upram_ptr && (m_uprender_shift == new_upshift)) + return; + + if (m_upram) + { + free(m_upram); + m_upram = nullptr; + } + + m_uprender_shift = new_upshift; + + if (scale == 1) + { + m_upram_ptr = m_vram.data(); + } + else + { + m_upram = (u16*)malloc(getVRAMSizeInBytes(scale)); + m_upram_ptr = m_upram; + + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, GetVRAMshadowPtr(), {}); + } +} + +GPU_SW_Backend::~GPU_SW_Backend() +{ + if (m_upram) + { + free(m_upram); + m_upram = nullptr; + } +} bool GPU_SW_Backend::Initialize(bool force_thread) { + SetUprenderScale(g_settings.gpu_resolution_scale); return GPUBackend::Initialize(force_thread); } +void GPU_SW_Backend::UpdateSettings() +{ + GPUBackend::UpdateSettings(); // invokes Sync() [inheritance style coding caveat] + + // function internally shortcuts out if setting is up-to-date. + SetUprenderScale(g_settings.gpu_resolution_scale); +} + void GPU_SW_Backend::Reset(bool clear_vram) { GPUBackend::Reset(clear_vram); if (clear_vram) - m_vram.fill(0); + memset(m_upram_ptr, 0, getVRAMSizeInBytes(uprender_scale())); } void GPU_SW_Backend::DrawPolygon(const GPUBackendDrawPolygonCommand* cmd) @@ -33,8 +113,17 @@ void GPU_SW_Backend::DrawPolygon(const GPUBackendDrawPolygonCommand* cmd) const GPURenderCommand rc{cmd->rc.bits}; const bool dithering_enable = rc.IsDitheringEnabled() && cmd->draw_mode.dither_enable; - const DrawTriangleFunction DrawFunction = GetDrawTriangleFunction( - rc.shading_enable, rc.texture_enable, rc.raw_texture_enable, rc.transparency_enable, dithering_enable); + auto DrawFunction = GetDrawTriangleFunction(0 + | (!!rc.shading_enable ) * TShaderParam_ShadingEnable + | (!!rc.texture_enable ) * TShaderParam_TextureEnable + | (!!rc.raw_texture_enable ) * TShaderParam_RawTextureEnable + | (!!rc.transparency_enable ) * TShaderParam_TransparencyEnable + | (dithering_enable ) * TShaderParam_DitheringEnable + | (!!cmd->params.GetMaskAND()) * TShaderParam_MaskAndEnable + | (!!cmd->params.GetMaskOR ()) * TShaderParam_MaskOrEnable, + + m_uprender_shift + ); (this->*DrawFunction)(cmd, &cmd->vertices[0], &cmd->vertices[1], &cmd->vertices[2]); if (rc.quad_polygon) @@ -45,16 +134,30 @@ void GPU_SW_Backend::DrawRectangle(const GPUBackendDrawRectangleCommand* cmd) { const GPURenderCommand rc{cmd->rc.bits}; - const DrawRectangleFunction DrawFunction = - GetDrawRectangleFunction(rc.texture_enable, rc.raw_texture_enable, rc.transparency_enable); + auto DrawFunction = GetDrawRectangleFunction(0 + | (!!rc.texture_enable ) * TRectShader_TextureEnable + | (!!rc.raw_texture_enable ) * TRectShader_RawTextureEnable + | (!!rc.transparency_enable ) * TRectShader_TransparencyEnable + | (!!cmd->params.GetMaskAND()) * TRectShader_MaskAndEnable + | (!!cmd->params.GetMaskOR ()) * TRectShader_MaskOrEnable, + + m_uprender_shift + ); (this->*DrawFunction)(cmd); } void GPU_SW_Backend::DrawLine(const GPUBackendDrawLineCommand* cmd) { - const DrawLineFunction DrawFunction = - GetDrawLineFunction(cmd->rc.shading_enable, cmd->rc.transparency_enable, cmd->IsDitheringEnabled()); + auto DrawFunction = GetDrawLineFunction(0 + | (!!cmd->rc.shading_enable ) * TLineShader_ShadingEnable + | (!!cmd->rc.transparency_enable ) * TLineShader_TransparencyEnable + | (cmd->IsDitheringEnabled() ) * TLineShader_DitheringEnable + | (!!cmd->params.GetMaskAND() ) * TLineShader_MaskAndEnable + | (!!cmd->params.GetMaskOR () ) * TLineShader_MaskOrEnable, + + m_uprender_shift + ); for (u16 i = 1; i < cmd->num_vertices; i++) (this->*DrawFunction)(cmd, &cmd->vertices[i - 1], &cmd->vertices[i]); @@ -79,16 +182,25 @@ constexpr GPU_SW_Backend::DitherLUT GPU_SW_Backend::ComputeDitherLUT() static constexpr GPU_SW_Backend::DitherLUT s_dither_lut = GPU_SW_Backend::ComputeDitherLUT(); -template -void ALWAYS_INLINE_RELEASE GPU_SW_Backend::ShadePixel(const GPUBackendDrawCommand* cmd, u32 x, u32 y, u8 color_r, - u8 color_g, u8 color_b, u8 texcoord_x, u8 texcoord_y) +template +void ALWAYS_INLINE_RELEASE GPU_SW_Backend::ShadePixel(const GPUBackendDrawCommand* cmd, s32 x, s32 y, u8 color_r, + u8 color_g, u8 color_b, u8 texcoord_x_u8, u8 texcoord_y_u8) { +// shading parameter is ignored for ShadePixel (should always be 0) +//constexpr bool shading_enable = (TShaderParams & TShaderParam_ShadingEnable ) == TShaderParam_ShadingEnable ; + constexpr bool texture_enable = (TShaderParams & TShaderParam_TextureEnable ) == TShaderParam_TextureEnable ; + constexpr bool raw_texture_enable = (TShaderParams & TShaderParam_RawTextureEnable ) == TShaderParam_RawTextureEnable ; + constexpr bool transparency_enable = (TShaderParams & TShaderParam_TransparencyEnable) == TShaderParam_TransparencyEnable; + constexpr bool dithering_enable = (TShaderParams & TShaderParam_DitheringEnable ) == TShaderParam_DitheringEnable ; + constexpr bool mask_and_enable = (TShaderParams & TShaderParam_MaskAndEnable ) == TShaderParam_MaskAndEnable ; + constexpr bool mask_or_enable = (TShaderParams & TShaderParam_MaskOrEnable ) == TShaderParam_MaskOrEnable ; + VRAMPixel color; if constexpr (texture_enable) { // Apply texture window - texcoord_x = (texcoord_x & cmd->window.and_x) | cmd->window.or_x; - texcoord_y = (texcoord_y & cmd->window.and_y) | cmd->window.or_y; + int texcoord_x = (texcoord_x_u8 & cmd->window.and_x) | cmd->window.or_x; + int texcoord_y = (texcoord_y_u8 & cmd->window.and_y) | cmd->window.or_y; VRAMPixel texture_color; switch (cmd->draw_mode.texture_mode) @@ -96,30 +208,30 @@ void ALWAYS_INLINE_RELEASE GPU_SW_Backend::ShadePixel(const GPUBackendDrawComman case GPUTextureMode::Palette4Bit: { const u16 palette_value = - GetPixel((cmd->draw_mode.GetTexturePageBaseX() + ZeroExtend32(texcoord_x / 4)) % VRAM_WIDTH, - (cmd->draw_mode.GetTexturePageBaseY() + ZeroExtend32(texcoord_y)) % VRAM_HEIGHT); + GetPixel((cmd->draw_mode.GetTexturePageBaseX() + (texcoord_x / 4)) % VRAM_WIDTH, + (cmd->draw_mode.GetTexturePageBaseY() + (texcoord_y / 1)) % VRAM_HEIGHT); const u16 palette_index = (palette_value >> ((texcoord_x % 4) * 4)) & 0x0Fu; texture_color.bits = - GetPixel((cmd->palette.GetXBase() + ZeroExtend32(palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); + GetPixel((cmd->palette.GetXBase() + (palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); } break; case GPUTextureMode::Palette8Bit: { const u16 palette_value = - GetPixel((cmd->draw_mode.GetTexturePageBaseX() + ZeroExtend32(texcoord_x / 2)) % VRAM_WIDTH, - (cmd->draw_mode.GetTexturePageBaseY() + ZeroExtend32(texcoord_y)) % VRAM_HEIGHT); + GetPixel((cmd->draw_mode.GetTexturePageBaseX() + (texcoord_x / 2)) % VRAM_WIDTH, + (cmd->draw_mode.GetTexturePageBaseY() + (texcoord_y / 1)) % VRAM_HEIGHT); const u16 palette_index = (palette_value >> ((texcoord_x % 2) * 8)) & 0xFFu; texture_color.bits = - GetPixel((cmd->palette.GetXBase() + ZeroExtend32(palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); + GetPixel((cmd->palette.GetXBase() + (palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); } break; default: { - texture_color.bits = GetPixel((cmd->draw_mode.GetTexturePageBaseX() + ZeroExtend32(texcoord_x)) % VRAM_WIDTH, - (cmd->draw_mode.GetTexturePageBaseY() + ZeroExtend32(texcoord_y)) % VRAM_HEIGHT); + texture_color.bits = GetPixel((cmd->draw_mode.GetTexturePageBaseX() + (texcoord_x)) % VRAM_WIDTH, + (cmd->draw_mode.GetTexturePageBaseY() + (texcoord_y)) % VRAM_HEIGHT); } break; } @@ -153,10 +265,20 @@ void ALWAYS_INLINE_RELEASE GPU_SW_Backend::ShadePixel(const GPUBackendDrawComman (ZeroExtend16(s_dither_lut[dither_y][dither_x][color_b]) << 10) | (transparency_enable ? 0x8000u : 0); } - const VRAMPixel bg_color{GetPixel(static_cast(x), static_cast(y))}; - if constexpr (transparency_enable) + auto constexpr vram_upsize_x = VRAM_WIDTH << TUprenderShift; + + if constexpr (transparency_enable || mask_and_enable) { - if (color.bits & 0x8000u || !texture_enable) + const VRAMPixel bg_color { UPRAM_ACCESSOR[vram_upsize_x * y + x] }; + + if constexpr (mask_and_enable) + { + const u16 mask_and = cmd->params.GetMaskAND(); + if ((bg_color.bits & mask_and) != 0) + return; + } + + if (transparency_enable && (color.bits & 0x8000u || !texture_enable)) { // Based on blargg's efficient 15bpp pixel math. u32 bg_bits = ZeroExtend32(bg_color.bits); @@ -212,42 +334,69 @@ void ALWAYS_INLINE_RELEASE GPU_SW_Backend::ShadePixel(const GPUBackendDrawComman } } - const u16 mask_and = cmd->params.GetMaskAND(); - if ((bg_color.bits & mask_and) != 0) - return; + u16 result = color.bits; - SetPixel(static_cast(x), static_cast(y), color.bits | cmd->params.GetMaskOR()); + if constexpr (mask_or_enable) + result |= cmd->params.GetMaskOR(); + + UPRAM_ACCESSOR[vram_upsize_x * y + x] = result; } -template +template void GPU_SW_Backend::DrawRectangle(const GPUBackendDrawRectangleCommand* cmd) { + constexpr bool texture_enable = (TRectParams & TRectShader_TextureEnable ) == TRectShader_TextureEnable ; + constexpr bool raw_texture_enable = (TRectParams & TRectShader_RawTextureEnable ) == TRectShader_RawTextureEnable ; + constexpr bool transparency_enable = (TRectParams & TRectShader_TransparencyEnable) == TRectShader_TransparencyEnable; + constexpr bool mask_and_enable = (TRectParams & TRectShader_MaskAndEnable ) == TRectShader_MaskAndEnable ; + constexpr bool mask_or_enable = (TRectParams & TRectShader_MaskOrEnable ) == TRectShader_MaskOrEnable ; + const s32 origin_x = cmd->x; const s32 origin_y = cmd->y; const auto [r, g, b] = UnpackColorRGB24(cmd->color); const auto [origin_texcoord_x, origin_texcoord_y] = UnpackTexcoord(cmd->texcoord); - for (u32 offset_y = 0; offset_y < cmd->height; offset_y++) + auto upscale = uprender_scale(); + + auto origin_x_up = origin_x * upscale; + auto origin_y_up = origin_y * upscale; + + auto size_x_up = cmd->width * upscale; + auto size_y_up = cmd->height * upscale; + + for (int offset_y = 0; offset_y < size_y_up; offset_y++) { - const s32 y = origin_y + static_cast(offset_y); - if (y < static_cast(m_drawing_area.top) || y > static_cast(m_drawing_area.bottom) || - (cmd->params.interlaced_rendering && cmd->params.active_line_lsb == (Truncate8(static_cast(y)) & 1u))) + const s32 y_up = origin_y_up + static_cast(offset_y); + if (y_up < static_cast(m_drawing_area.top * upscale) || y_up > static_cast(m_drawing_area.bottom * upscale) || + (cmd->params.interlaced_rendering && cmd->params.active_line_lsb == (Truncate8(static_cast(y_up)) & 1u))) { continue; } - const u8 texcoord_y = Truncate8(ZeroExtend32(origin_texcoord_y) + offset_y); + const u8 texcoord_y = Truncate8(ZeroExtend32(origin_texcoord_y) + (offset_y / upscale)); - for (u32 offset_x = 0; offset_x < cmd->width; offset_x++) + for (int offset_x = 0; offset_x < size_x_up; offset_x++) { - const s32 x = origin_x + static_cast(offset_x); - if (x < static_cast(m_drawing_area.left) || x > static_cast(m_drawing_area.right)) + const s32 x_up = origin_x_up + static_cast(offset_x); + if (x_up < static_cast(m_drawing_area.left * upscale) || x_up > static_cast(m_drawing_area.right * upscale)) continue; - const u8 texcoord_x = Truncate8(ZeroExtend32(origin_texcoord_x) + offset_x); + // TODO: sub-pixel texture sampling? + // Not sure it matters unless we're using tex replacements, as PSX was very likely using 1:1 scale textures + // for rectangle draws. + const u8 texcoord_x = Truncate8(ZeroExtend32(origin_texcoord_x) + (offset_x / upscale)); - ShadePixel( - cmd, static_cast(x), static_cast(y), r, g, b, texcoord_x, texcoord_y); + constexpr u32 ShaderParams = + TShaderParam_ShadingEnable * 0 | + TShaderParam_TextureEnable * texture_enable | + TShaderParam_RawTextureEnable * raw_texture_enable | + TShaderParam_TransparencyEnable * transparency_enable | + TShaderParam_DitheringEnable * 0 | + TShaderParam_MaskAndEnable * 1 | + TShaderParam_MaskOrEnable * 1 ; + + ShadePixel( + cmd, static_cast(x_up), static_cast(y_up), r, g, b, texcoord_x, texcoord_y); } } } @@ -258,7 +407,7 @@ void GPU_SW_Backend::DrawRectangle(const GPUBackendDrawRectangleCommand* cmd) #define COORD_FBS 12 #define COORD_MF_INT(n) ((n) << COORD_FBS) -#define COORD_POST_PADDING 12 +#define COORD_POST_PADDING 10 static ALWAYS_INLINE_RELEASE s64 MakePolyXFP(s32 x) { @@ -324,182 +473,198 @@ bool ALWAYS_INLINE_RELEASE GPU_SW_Backend::CalcIDeltas(i_deltas& idl, const GPUB #undef CALCIS } -template -void ALWAYS_INLINE_RELEASE GPU_SW_Backend::AddIDeltas_DX(i_group& ig, const i_deltas& idl, u32 count /*= 1*/) +template +void ALWAYS_INLINE_RELEASE GPU_SW_Backend::AddIDeltas_DX(I_GROUP& ig, const i_deltas& idl, AddDeltasScalar_t count /*= 1*/) { if constexpr (shading_enable) { - ig.r += idl.dr_dx * count; - ig.g += idl.dg_dx * count; - ig.b += idl.db_dx * count; + ig.r += (idl.dr_dx * count); + ig.g += (idl.dg_dx * count); + ig.b += (idl.db_dx * count); } if constexpr (texture_enable) { - ig.u += idl.du_dx * count; - ig.v += idl.dv_dx * count; + ig.u += (idl.du_dx * count); + ig.v += (idl.dv_dx * count); } } -template -void ALWAYS_INLINE_RELEASE GPU_SW_Backend::AddIDeltas_DY(i_group& ig, const i_deltas& idl, u32 count /*= 1*/) +template +void ALWAYS_INLINE_RELEASE GPU_SW_Backend::AddIDeltas_DY(I_GROUP& ig, const i_deltas& idl, AddDeltasScalar_t count /*= 1*/) { if constexpr (shading_enable) { - ig.r += idl.dr_dy * count; - ig.g += idl.dg_dy * count; - ig.b += idl.db_dy * count; + ig.r += (idl.dr_dy * count); + ig.g += (idl.dg_dy * count); + ig.b += (idl.db_dy * count); } if constexpr (texture_enable) { - ig.u += idl.du_dy * count; - ig.v += idl.dv_dy * count; + ig.u += (idl.du_dy * count); + ig.v += (idl.dv_dy * count); } } -template -void GPU_SW_Backend::DrawSpan(const GPUBackendDrawPolygonCommand* cmd, s32 y, s32 x_start, s32 x_bound, i_group ig, +struct i_group_float +{ + float u, v; + float r, g, b; +}; + +template +void GPU_SW_Backend::DrawSpan(const GPUBackendDrawPolygonCommand* cmd, s32 y_up, s32 x_start_up, s32 x_bound_up, i_group igi, const i_deltas& idl) { - if (cmd->params.interlaced_rendering && cmd->params.active_line_lsb == (Truncate8(static_cast(y)) & 1u)) + constexpr int upscale = (1 << TUprenderShift); + + constexpr bool shading_enable = (TShaderParams & TShaderParam_ShadingEnable ) == TShaderParam_ShadingEnable ; + constexpr bool texture_enable = (TShaderParams & TShaderParam_TextureEnable ) == TShaderParam_TextureEnable ; + constexpr bool raw_texture_enable = (TShaderParams & TShaderParam_RawTextureEnable ) == TShaderParam_RawTextureEnable ; + constexpr bool transparency_enable = (TShaderParams & TShaderParam_TransparencyEnable) == TShaderParam_TransparencyEnable; + constexpr bool dithering_enable = (TShaderParams & TShaderParam_DitheringEnable ) == TShaderParam_DitheringEnable ; + constexpr bool mask_and_enable = (TShaderParams & TShaderParam_MaskAndEnable ) == TShaderParam_MaskAndEnable ; + constexpr bool mask_or_enable = (TShaderParams & TShaderParam_MaskOrEnable ) == TShaderParam_MaskOrEnable ; + + auto y_native = y_up / upscale; + auto x_start_native = x_start_up / upscale; + auto x_bound_native = x_bound_up / upscale; + + if (cmd->params.interlaced_rendering && cmd->params.active_line_lsb == (y_up & 1u)) return; - s32 x_ig_adjust = x_start; - s32 w = x_bound - x_start; - s32 x = TruncateGPUVertexPosition(x_start); + s32 x_ig_adjust = x_start_up; + s32 w_native = x_bound_native - x_start_native; + s32 w_up = x_bound_up - x_start_up; + s32 x_native = SignExtendN<11, s32>(x_start_native); + s32 x_up = SignExtendN<11 + TUprenderShift, s32>(x_start_up); - if (x < static_cast(m_drawing_area.left)) + if (x_up < static_cast(m_drawing_area.left * upscale)) { - s32 delta = static_cast(m_drawing_area.left) - x; - x_ig_adjust += delta; - x += delta; - w -= delta; + s32 delta_up = static_cast(m_drawing_area.left * upscale) - x_up; + x_ig_adjust += delta_up; + x_up += delta_up; + w_up -= delta_up; } - if ((x + w) > (static_cast(m_drawing_area.right) + 1)) - w = static_cast(m_drawing_area.right) + 1 - x; + if ((x_up + w_up) > (static_cast(m_drawing_area.right * upscale) + upscale)) + { + w_up = static_cast(m_drawing_area.right * upscale) + upscale - x_up; + } - if (w <= 0) + if (w_up <= 0) return; - AddIDeltas_DX(ig, idl, x_ig_adjust); - AddIDeltas_DY(ig, idl, y); +#if 0 + if (x_native < static_cast(m_drawing_area.left)) + { + s32 delta_native = static_cast(m_drawing_area.left) - x_native; + x_native += delta_native; + w_native -= delta_native; + } + + if ((x_native + w_native) > (static_cast(m_drawing_area.right) + 1)) + { + w_native = (static_cast(m_drawing_area.right + 1)) - x_native; + } + + if (w_native <= 0) + return; +#endif + +#if USE_FLOAT_STEP + i_group_float igf = {}; + + igf.r = (float)igi.r / (1ll << (COORD_FBS + COORD_POST_PADDING)); + igf.g = (float)igi.g / (1ll << (COORD_FBS + COORD_POST_PADDING)); + igf.b = (float)igi.b / (1ll << (COORD_FBS + COORD_POST_PADDING)); + igf.u = (float)igi.u / (1ll << (COORD_FBS + COORD_POST_PADDING)); + igf.v = (float)igi.v / (1ll << (COORD_FBS + COORD_POST_PADDING)); + + float postpad_scalar = 1ll << (COORD_FBS + COORD_POST_PADDING; + + AddIDeltas_DX(igf, idl, (float)x_ig_adjust / postpad_scalar); + AddIDeltas_DY(igf, idl, (float)y_up / postpad_scalar); +#endif + +#if USE_INT_STEP + AddIDeltas_DX(igi, idl, x_ig_adjust); + AddIDeltas_DY(igi, idl, y_up ); +#endif do { - const u32 r = ig.r >> (COORD_FBS + COORD_POST_PADDING); - const u32 g = ig.g >> (COORD_FBS + COORD_POST_PADDING); - const u32 b = ig.b >> (COORD_FBS + COORD_POST_PADDING); - const u32 u = ig.u >> (COORD_FBS + COORD_POST_PADDING); - const u32 v = ig.v >> (COORD_FBS + COORD_POST_PADDING); + DebugAssert(x_up <= static_cast(m_drawing_area.right+1) * upscale); - ShadePixel( - cmd, static_cast(x), static_cast(y), Truncate8(r), Truncate8(g), Truncate8(b), Truncate8(u), - Truncate8(v)); +#if USE_FLOAT_STEP + auto r = Truncate8((int)floorf(igf.r)); + auto g = Truncate8((int)floorf(igf.g)); + auto b = Truncate8((int)floorf(igf.b)); + auto u = Truncate8((int)floorf(igf.u)); + auto v = Truncate8((int)floorf(igf.v)); +#endif - x++; - AddIDeltas_DX(ig, idl); - } while (--w > 0); +#if USE_INT_STEP + auto ri = Truncate8(igi.r >> (COORD_FBS + COORD_POST_PADDING)); + auto gi = Truncate8(igi.g >> (COORD_FBS + COORD_POST_PADDING)); + auto bi = Truncate8(igi.b >> (COORD_FBS + COORD_POST_PADDING)); + auto ui = Truncate8(igi.u >> (COORD_FBS + COORD_POST_PADDING)); + auto vi = Truncate8(igi.v >> (COORD_FBS + COORD_POST_PADDING)); +#endif + + //DebugAssert(r == ri && u == ui); + //DebugAssert(g == gi && v == vi); + //DebugAssert(b == bi); + + constexpr u32 ShaderParams = + TShaderParam_ShadingEnable * 0 | + TShaderParam_TextureEnable * texture_enable | + TShaderParam_RawTextureEnable * raw_texture_enable | + TShaderParam_TransparencyEnable * transparency_enable | + TShaderParam_DitheringEnable * dithering_enable | + TShaderParam_MaskAndEnable * mask_and_enable | + TShaderParam_MaskOrEnable * mask_or_enable ; + + // FIXME: also need to clip uprender right edge. + if (x_up >= 0) { + ShadePixel( + cmd, x_up, y_up, +#if USE_INT_STEP + ri, gi, bi, ui, vi +#else + r, g, b, u, v +#endif + ); + } + x_up++; + +#if USE_FLOAT_STEP + AddIDeltas_DX(igf, idl, 1.0f / postpad_scalar); +#endif + +#if USE_INT_STEP + AddIDeltas_DX(igi, idl); +#endif + + } while (--w_up > 0); } -template +template void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, - const GPUBackendDrawPolygonCommand::Vertex* v0, - const GPUBackendDrawPolygonCommand::Vertex* v1, - const GPUBackendDrawPolygonCommand::Vertex* v2) + const GPUBackendDrawPolygonCommand::Vertex* v0n, + const GPUBackendDrawPolygonCommand::Vertex* v1n, + const GPUBackendDrawPolygonCommand::Vertex* v2n) { - u32 core_vertex; - { - u32 cvtemp = 0; + int constexpr upscale = (1 << TUprenderShift); - if (v1->x <= v0->x) - { - if (v2->x <= v1->x) - cvtemp = (1 << 2); - else - cvtemp = (1 << 1); - } - else if (v2->x < v0->x) - cvtemp = (1 << 2); - else - cvtemp = (1 << 0); - - if (v2->y < v1->y) - { - std::swap(v2, v1); - cvtemp = ((cvtemp >> 1) & 0x2) | ((cvtemp << 1) & 0x4) | (cvtemp & 0x1); - } - - if (v1->y < v0->y) - { - std::swap(v1, v0); - cvtemp = ((cvtemp >> 1) & 0x1) | ((cvtemp << 1) & 0x2) | (cvtemp & 0x4); - } - - if (v2->y < v1->y) - { - std::swap(v2, v1); - cvtemp = ((cvtemp >> 1) & 0x2) | ((cvtemp << 1) & 0x4) | (cvtemp & 0x1); - } - - core_vertex = cvtemp >> 1; - } - - if (v0->y == v2->y) - return; - - if (static_cast(std::abs(v2->x - v0->x)) >= MAX_PRIMITIVE_WIDTH || - static_cast(std::abs(v2->x - v1->x)) >= MAX_PRIMITIVE_WIDTH || - static_cast(std::abs(v1->x - v0->x)) >= MAX_PRIMITIVE_WIDTH || - static_cast(v2->y - v0->y) >= MAX_PRIMITIVE_HEIGHT) - { - return; - } - - s64 base_coord = MakePolyXFP(v0->x); - s64 base_step = MakePolyXFPStep((v2->x - v0->x), (v2->y - v0->y)); - s64 bound_coord_us; - s64 bound_coord_ls; - bool right_facing; - - if (v1->y == v0->y) - { - bound_coord_us = 0; - right_facing = (bool)(v1->x > v0->x); - } - else - { - bound_coord_us = MakePolyXFPStep((v1->x - v0->x), (v1->y - v0->y)); - right_facing = (bool)(bound_coord_us > base_step); - } - - if (v2->y == v1->y) - bound_coord_ls = 0; - else - bound_coord_ls = MakePolyXFPStep((v2->x - v1->x), (v2->y - v1->y)); - - i_deltas idl; - if (!CalcIDeltas(idl, v0, v1, v2)) - return; - - const GPUBackendDrawPolygonCommand::Vertex* vertices[3] = {v0, v1, v2}; - - i_group ig; - if constexpr (texture_enable) - { - ig.u = (COORD_MF_INT(vertices[core_vertex]->u) + (1 << (COORD_FBS - 1))) << COORD_POST_PADDING; - ig.v = (COORD_MF_INT(vertices[core_vertex]->v) + (1 << (COORD_FBS - 1))) << COORD_POST_PADDING; - } - - ig.r = (COORD_MF_INT(vertices[core_vertex]->r) + (1 << (COORD_FBS - 1))) << COORD_POST_PADDING; - ig.g = (COORD_MF_INT(vertices[core_vertex]->g) + (1 << (COORD_FBS - 1))) << COORD_POST_PADDING; - ig.b = (COORD_MF_INT(vertices[core_vertex]->b) + (1 << (COORD_FBS - 1))) << COORD_POST_PADDING; - - AddIDeltas_DX(ig, idl, -vertices[core_vertex]->x); - AddIDeltas_DY(ig, idl, -vertices[core_vertex]->y); + constexpr bool shading_enable = (TShaderParams & TShaderParam_ShadingEnable ) == TShaderParam_ShadingEnable ; + constexpr bool texture_enable = (TShaderParams & TShaderParam_TextureEnable ) == TShaderParam_TextureEnable ; + constexpr bool raw_texture_enable = (TShaderParams & TShaderParam_RawTextureEnable ) == TShaderParam_RawTextureEnable ; + constexpr bool transparency_enable = (TShaderParams & TShaderParam_TransparencyEnable) == TShaderParam_TransparencyEnable; + constexpr bool dithering_enable = (TShaderParams & TShaderParam_DitheringEnable ) == TShaderParam_DitheringEnable ; + constexpr bool mask_and_enable = (TShaderParams & TShaderParam_MaskAndEnable ) == TShaderParam_MaskAndEnable ; + constexpr bool mask_or_enable = (TShaderParams & TShaderParam_MaskOrEnable ) == TShaderParam_MaskOrEnable ; struct TriangleHalf { @@ -512,36 +677,150 @@ void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, bool dec_mode; } tripart[2]; - u32 vo = 0; - u32 vp = 0; - if (core_vertex != 0) - vo = 1; - if (core_vertex == 2) - vp = 3; + i_group ig; + i_deltas idl; + if (1) { - TriangleHalf* tp = &tripart[vo]; - tp->y_coord = vertices[0 ^ vo]->y; - tp->y_bound = vertices[1 ^ vo]->y; - tp->x_coord[right_facing] = MakePolyXFP(vertices[0 ^ vo]->x); - tp->x_step[right_facing] = bound_coord_us; - tp->x_coord[!right_facing] = base_coord + ((vertices[vo]->y - vertices[0]->y) * base_step); - tp->x_step[!right_facing] = base_step; - tp->dec_mode = vo; + u32 core_vertex; + { + u32 cvtemp = 0; + + if (v1n->x <= v0n->x) + { + if (v2n->x <= v1n->x) + cvtemp = (1 << 2); + else + cvtemp = (1 << 1); + } + else if (v2n->x < v0n->x) + cvtemp = (1 << 2); + else + cvtemp = (1 << 0); + + if (v2n->y < v1n->y) + { + std::swap(v2n, v1n); + cvtemp = ((cvtemp >> 1) & 0x2) | ((cvtemp << 1) & 0x4) | (cvtemp & 0x1); + } + + if (v1n->y < v0n->y) + { + std::swap(v1n, v0n); + cvtemp = ((cvtemp >> 1) & 0x1) | ((cvtemp << 1) & 0x2) | (cvtemp & 0x4); + } + + if (v2n->y < v1n->y) + { + std::swap(v2n, v1n); + cvtemp = ((cvtemp >> 1) & 0x2) | ((cvtemp << 1) & 0x4) | (cvtemp & 0x1); + } + + core_vertex = cvtemp >> 1; + } + + if (v0n->y == v2n->y) + return; + + if (static_cast(std::abs(v2n->x - v0n->x)) >= MAX_PRIMITIVE_WIDTH || + static_cast(std::abs(v2n->x - v1n->x)) >= MAX_PRIMITIVE_WIDTH || + static_cast(std::abs(v1n->x - v0n->x)) >= MAX_PRIMITIVE_WIDTH || + static_cast(v2n->y - v0n->y) >= MAX_PRIMITIVE_HEIGHT) + { + return; + } + + s64 bound_coord_us; + s64 bound_coord_ls; + bool right_facing; + + GPUBackendDrawPolygonCommand::Vertex up_v0 = *v0n; + GPUBackendDrawPolygonCommand::Vertex up_v1 = *v1n; + GPUBackendDrawPolygonCommand::Vertex up_v2 = *v2n; + + GPUBackendDrawPolygonCommand::Vertex* v0 = &up_v0; + GPUBackendDrawPolygonCommand::Vertex* v1 = &up_v1; + GPUBackendDrawPolygonCommand::Vertex* v2 = &up_v2; + + v0->x *= upscale; + v0->y *= upscale; + v1->x *= upscale; + v1->y *= upscale; + v2->x *= upscale; + v2->y *= upscale; + + s64 base_coord = MakePolyXFP(v0->x); + s64 base_step = MakePolyXFPStep((v2->x - v0->x), (v2->y - v0->y)); + + if (v1->y == v0->y) + { + bound_coord_us = 0; + right_facing = (bool)(v1->x > v0->x); + } + else + { + bound_coord_us = MakePolyXFPStep((v1->x - v0->x), (v1->y - v0->y)); + right_facing = (bool)(bound_coord_us > base_step); + } + + if (v2->y == v1->y) + bound_coord_ls = 0; + else + bound_coord_ls = MakePolyXFPStep((v2->x - v1->x), (v2->y - v1->y)); + + if (!CalcIDeltas(idl, v0, v1, v2)) + return; + + const GPUBackendDrawPolygonCommand::Vertex* vertices[3] = {v0, v1, v2}; + int upshift = TUprenderShift; + + if constexpr (texture_enable) + { + ig.u = (COORD_MF_INT(vertices[core_vertex]->u) + (1 << (COORD_FBS - 1 - upshift))) << COORD_POST_PADDING; + ig.v = (COORD_MF_INT(vertices[core_vertex]->v) + (1 << (COORD_FBS - 1 - upshift))) << COORD_POST_PADDING; + } + + ig.r = (COORD_MF_INT(vertices[core_vertex]->r) + (1 << (COORD_FBS - 1 - upshift))) << COORD_POST_PADDING; + ig.g = (COORD_MF_INT(vertices[core_vertex]->g) + (1 << (COORD_FBS - 1 - upshift))) << COORD_POST_PADDING; + ig.b = (COORD_MF_INT(vertices[core_vertex]->b) + (1 << (COORD_FBS - 1 - upshift))) << COORD_POST_PADDING; + + AddIDeltas_DX(ig, idl, -vertices[core_vertex]->x); + AddIDeltas_DY(ig, idl, -vertices[core_vertex]->y); + + u32 vo = 0; + u32 vp = 0; + if (core_vertex != 0) + vo = 1; + if (core_vertex == 2) + vp = 3; + + { + TriangleHalf* tp = &tripart[vo]; + tp->y_coord = vertices[0 ^ vo]->y; + tp->y_bound = vertices[1 ^ vo]->y; + tp->x_coord[right_facing] = MakePolyXFP(vertices[0 ^ vo]->x); + tp->x_step[right_facing] = bound_coord_us; + tp->x_coord[!right_facing] = base_coord + ((vertices[vo]->y - vertices[0]->y) * base_step); + tp->x_step[!right_facing] = base_step; + tp->dec_mode = vo; + } + + { + TriangleHalf* tp = &tripart[vo ^ 1]; + tp->y_coord = vertices[1 ^ vp]->y; + tp->y_bound = vertices[2 ^ vp]->y; + tp->x_coord[right_facing] = MakePolyXFP(vertices[1 ^ vp]->x); + tp->x_step[right_facing] = bound_coord_ls; + tp->x_coord[!right_facing] = + base_coord + ((vertices[1 ^ vp]->y - vertices[0]->y) * + base_step); // base_coord + ((vertices[1].y - vertices[0].y) * base_step); + tp->x_step[!right_facing] = base_step; + tp->dec_mode = vp; + } } - { - TriangleHalf* tp = &tripart[vo ^ 1]; - tp->y_coord = vertices[1 ^ vp]->y; - tp->y_bound = vertices[2 ^ vp]->y; - tp->x_coord[right_facing] = MakePolyXFP(vertices[1 ^ vp]->x); - tp->x_step[right_facing] = bound_coord_ls; - tp->x_coord[!right_facing] = - base_coord + ((vertices[1 ^ vp]->y - vertices[0]->y) * - base_step); // base_coord + ((vertices[1].y - vertices[0].y) * base_step); - tp->x_step[!right_facing] = base_step; - tp->dec_mode = vp; - } + // TODO: none of the code above this line depends on template parameters. + // We could generalize that code and then for (u32 i = 0; i < 2; i++) { @@ -562,15 +841,15 @@ void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, lc -= ls; rc -= rs; - s32 y = TruncateGPUVertexPosition(yi); + s32 y = SignExtendN<11 + TUprenderShift, s32>(yi); - if (y < static_cast(m_drawing_area.top)) + if (y < static_cast(m_drawing_area.top) * upscale) break; - if (y > static_cast(m_drawing_area.bottom)) + if (y > (static_cast(m_drawing_area.bottom) * upscale) + (upscale/2)) continue; - DrawSpan( + DrawSpan( cmd, yi, GetPolyXFP_Int(lc), GetPolyXFP_Int(rc), ig, idl); } } @@ -578,15 +857,14 @@ void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, { while (yi < yb) { - s32 y = TruncateGPUVertexPosition(yi); + s32 y = SignExtendN<11 + TUprenderShift, s32>(yi); - if (y > static_cast(m_drawing_area.bottom)) + if (y > static_cast(m_drawing_area.bottom) * upscale) break; - if (y >= static_cast(m_drawing_area.top)) + if (y >= static_cast(m_drawing_area.top) * upscale) { - - DrawSpan( + DrawSpan( cmd, yi, GetPolyXFP_Int(lc), GetPolyXFP_Int(rc), ig, idl); } @@ -598,6 +876,76 @@ void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, } } +GPU_SW_Backend::DrawTriangleFunction GPU_SW_Backend::GetDrawTriangleFunction(u32 shaderParams, int uprender_shift) +{ +#define FN(bmask) &GPU_SW_Backend::DrawTriangle +#define F2(bmask) &GPU_SW_Backend::DrawTriangle +#define F4(bmask) &GPU_SW_Backend::DrawTriangle + + static constexpr DrawTriangleFunction funcs[3][TShaderParams_MAX] = { + { + FN(0x000),FN(0x001),FN(0x002),FN(0x003),FN(0x004),FN(0x005),FN(0x006),FN(0x007), + FN(0x008),FN(0x009),FN(0x00a),FN(0x00b),FN(0x00c),FN(0x00d),FN(0x00e),FN(0x00f), + FN(0x010),FN(0x011),FN(0x012),FN(0x013),FN(0x014),FN(0x015),FN(0x016),FN(0x017), + FN(0x018),FN(0x019),FN(0x01a),FN(0x01b),FN(0x01c),FN(0x01d),FN(0x01e),FN(0x01f), + FN(0x020),FN(0x021),FN(0x022),FN(0x023),FN(0x024),FN(0x025),FN(0x026),FN(0x027), + FN(0x028),FN(0x029),FN(0x02a),FN(0x02b),FN(0x02c),FN(0x02d),FN(0x02e),FN(0x02f), + FN(0x030),FN(0x031),FN(0x032),FN(0x033),FN(0x034),FN(0x035),FN(0x036),FN(0x037), + FN(0x038),FN(0x039),FN(0x03a),FN(0x03b),FN(0x03c),FN(0x03d),FN(0x03e),FN(0x03f), + FN(0x040),FN(0x041),FN(0x042),FN(0x043),FN(0x044),FN(0x045),FN(0x046),FN(0x047), + FN(0x048),FN(0x049),FN(0x04a),FN(0x04b),FN(0x04c),FN(0x04d),FN(0x04e),FN(0x04f), + FN(0x050),FN(0x051),FN(0x052),FN(0x053),FN(0x054),FN(0x055),FN(0x056),FN(0x057), + FN(0x058),FN(0x059),FN(0x05a),FN(0x05b),FN(0x05c),FN(0x05d),FN(0x05e),FN(0x05f), + FN(0x060),FN(0x061),FN(0x062),FN(0x063),FN(0x064),FN(0x065),FN(0x066),FN(0x067), + FN(0x068),FN(0x069),FN(0x06a),FN(0x06b),FN(0x06c),FN(0x06d),FN(0x06e),FN(0x06f), + FN(0x070),FN(0x071),FN(0x072),FN(0x073),FN(0x074),FN(0x075),FN(0x076),FN(0x077), + FN(0x078),FN(0x079),FN(0x07a),FN(0x07b),FN(0x07c),FN(0x07d),FN(0x07e),FN(0x07f), + }, + { + F2(0x000),F2(0x001),F2(0x002),F2(0x003),F2(0x004),F2(0x005),F2(0x006),F2(0x007), + F2(0x008),F2(0x009),F2(0x00a),F2(0x00b),F2(0x00c),F2(0x00d),F2(0x00e),F2(0x00f), + F2(0x010),F2(0x011),F2(0x012),F2(0x013),F2(0x014),F2(0x015),F2(0x016),F2(0x017), + F2(0x018),F2(0x019),F2(0x01a),F2(0x01b),F2(0x01c),F2(0x01d),F2(0x01e),F2(0x01f), + F2(0x020),F2(0x021),F2(0x022),F2(0x023),F2(0x024),F2(0x025),F2(0x026),F2(0x027), + F2(0x028),F2(0x029),F2(0x02a),F2(0x02b),F2(0x02c),F2(0x02d),F2(0x02e),F2(0x02f), + F2(0x030),F2(0x031),F2(0x032),F2(0x033),F2(0x034),F2(0x035),F2(0x036),F2(0x037), + F2(0x038),F2(0x039),F2(0x03a),F2(0x03b),F2(0x03c),F2(0x03d),F2(0x03e),F2(0x03f), + F2(0x040),F2(0x041),F2(0x042),F2(0x043),F2(0x044),F2(0x045),F2(0x046),F2(0x047), + F2(0x048),F2(0x049),F2(0x04a),F2(0x04b),F2(0x04c),F2(0x04d),F2(0x04e),F2(0x04f), + F2(0x050),F2(0x051),F2(0x052),F2(0x053),F2(0x054),F2(0x055),F2(0x056),F2(0x057), + F2(0x058),F2(0x059),F2(0x05a),F2(0x05b),F2(0x05c),F2(0x05d),F2(0x05e),F2(0x05f), + F2(0x060),F2(0x061),F2(0x062),F2(0x063),F2(0x064),F2(0x065),F2(0x066),F2(0x067), + F2(0x068),F2(0x069),F2(0x06a),F2(0x06b),F2(0x06c),F2(0x06d),F2(0x06e),F2(0x06f), + F2(0x070),F2(0x071),F2(0x072),F2(0x073),F2(0x074),F2(0x075),F2(0x076),F2(0x077), + F2(0x078),F2(0x079),F2(0x07a),F2(0x07b),F2(0x07c),F2(0x07d),F2(0x07e),F2(0x07f), + }, + { + F4(0x000),F4(0x001),F4(0x002),F4(0x003),F4(0x004),F4(0x005),F4(0x006),F4(0x007), + F4(0x008),F4(0x009),F4(0x00a),F4(0x00b),F4(0x00c),F4(0x00d),F4(0x00e),F4(0x00f), + F4(0x010),F4(0x011),F4(0x012),F4(0x013),F4(0x014),F4(0x015),F4(0x016),F4(0x017), + F4(0x018),F4(0x019),F4(0x01a),F4(0x01b),F4(0x01c),F4(0x01d),F4(0x01e),F4(0x01f), + F4(0x020),F4(0x021),F4(0x022),F4(0x023),F4(0x024),F4(0x025),F4(0x026),F4(0x027), + F4(0x028),F4(0x029),F4(0x02a),F4(0x02b),F4(0x02c),F4(0x02d),F4(0x02e),F4(0x02f), + F4(0x030),F4(0x031),F4(0x032),F4(0x033),F4(0x034),F4(0x035),F4(0x036),F4(0x037), + F4(0x038),F4(0x039),F4(0x03a),F4(0x03b),F4(0x03c),F4(0x03d),F4(0x03e),F4(0x03f), + F4(0x040),F4(0x041),F4(0x042),F4(0x043),F4(0x044),F4(0x045),F4(0x046),F4(0x047), + F4(0x048),F4(0x049),F4(0x04a),F4(0x04b),F4(0x04c),F4(0x04d),F4(0x04e),F4(0x04f), + F4(0x050),F4(0x051),F4(0x052),F4(0x053),F4(0x054),F4(0x055),F4(0x056),F4(0x057), + F4(0x058),F4(0x059),F4(0x05a),F4(0x05b),F4(0x05c),F4(0x05d),F4(0x05e),F4(0x05f), + F4(0x060),F4(0x061),F4(0x062),F4(0x063),F4(0x064),F4(0x065),F4(0x066),F4(0x067), + F4(0x068),F4(0x069),F4(0x06a),F4(0x06b),F4(0x06c),F4(0x06d),F4(0x06e),F4(0x06f), + F4(0x070),F4(0x071),F4(0x072),F4(0x073),F4(0x074),F4(0x075),F4(0x076),F4(0x077), + F4(0x078),F4(0x079),F4(0x07a),F4(0x07b),F4(0x07c),F4(0x07d),F4(0x07e),F4(0x07f), + } + }; + +#undef FN +#undef F2 +#undef F4 + + return funcs[uprender_shift][shaderParams]; +} + enum { Line_XY_FractBits = 32 @@ -631,10 +979,17 @@ static ALWAYS_INLINE_RELEASE s64 LineDivide(s64 delta, s32 dk) return (delta / dk); } -template +template void GPU_SW_Backend::DrawLine(const GPUBackendDrawLineCommand* cmd, const GPUBackendDrawLineCommand::Vertex* p0, const GPUBackendDrawLineCommand::Vertex* p1) { + constexpr auto upscale = (1<x - p0->x); const s32 i_dy = std::abs(p1->y - p0->y); const s32 k = (i_dx > i_dy) ? i_dx : i_dy; @@ -694,16 +1049,37 @@ void GPU_SW_Backend::DrawLine(const GPUBackendDrawLineCommand* cmd, const GPUBac if ((!cmd->params.interlaced_rendering || cmd->params.active_line_lsb != (Truncate8(static_cast(y)) & 1u)) && x >= static_cast(m_drawing_area.left) && x <= static_cast(m_drawing_area.right) && - y >= static_cast(m_drawing_area.top) && y <= static_cast(m_drawing_area.bottom)) + y >= static_cast(m_drawing_area.top) && y <= static_cast(m_drawing_area.bottom)) { const u8 r = shading_enable ? static_cast(cur_point.r >> Line_RGB_FractBits) : p0->r; const u8 g = shading_enable ? static_cast(cur_point.g >> Line_RGB_FractBits) : p0->g; const u8 b = shading_enable ? static_cast(cur_point.b >> Line_RGB_FractBits) : p0->b; - ShadePixel(cmd, static_cast(x), static_cast(y), r, - g, b, 0, 0); - } + // FIXME: this is sufficient for horizontal or vertical lines, but diaglonals will likely + // need more advanced heuristics or anti-aliasing to avoid looking ugly and/or causing unsightly + // coverage artifacts. + for (int yu=0; yu(cmd, + static_cast((x * upscale) + xu), + static_cast((y * upscale) + yu), + r, g, b, 0, 0 + ); + } + } + } cur_point.x += step.dx_dk; cur_point.y += step.dy_dk; @@ -716,50 +1092,141 @@ void GPU_SW_Backend::DrawLine(const GPUBackendDrawLineCommand* cmd, const GPUBac } } +GPU_SW_Backend::DrawLineFunction GPU_SW_Backend::GetDrawLineFunction(u32 lineShaderParams, int upshift) +{ +#define FN(bmask) &GPU_SW_Backend::DrawLine +#define F2(bmask) &GPU_SW_Backend::DrawLine +#define F4(bmask) &GPU_SW_Backend::DrawLine + + static constexpr DrawLineFunction funcs[3][TLineShader_MAX] = { + { + FN(0x000),FN(0x001),FN(0x002),FN(0x003),FN(0x004),FN(0x005),FN(0x006),FN(0x007), + FN(0x008),FN(0x009),FN(0x00a),FN(0x00b),FN(0x00c),FN(0x00d),FN(0x00e),FN(0x00f), + FN(0x010),FN(0x011),FN(0x012),FN(0x013),FN(0x014),FN(0x015),FN(0x016),FN(0x017), + FN(0x018),FN(0x019),FN(0x01a),FN(0x01b),FN(0x01c),FN(0x01d),FN(0x01e),FN(0x01f), + }, + { + F2(0x000),F2(0x001),F2(0x002),F2(0x003),F2(0x004),F2(0x005),F2(0x006),F2(0x007), + F2(0x008),F2(0x009),F2(0x00a),F2(0x00b),F2(0x00c),F2(0x00d),F2(0x00e),F2(0x00f), + F2(0x010),F2(0x011),F2(0x012),F2(0x013),F2(0x014),F2(0x015),F2(0x016),F2(0x017), + F2(0x018),F2(0x019),F2(0x01a),F2(0x01b),F2(0x01c),F2(0x01d),F2(0x01e),F2(0x01f), + }, + { + F4(0x000),F4(0x001),F4(0x002),F4(0x003),F4(0x004),F4(0x005),F4(0x006),F4(0x007), + F4(0x008),F4(0x009),F4(0x00a),F4(0x00b),F4(0x00c),F4(0x00d),F4(0x00e),F4(0x00f), + F4(0x010),F4(0x011),F4(0x012),F4(0x013),F4(0x014),F4(0x015),F4(0x016),F4(0x017), + F4(0x018),F4(0x019),F4(0x01a),F4(0x01b),F4(0x01c),F4(0x01d),F4(0x01e),F4(0x01f), + } + }; + +#undef F + +#undef FN +#undef F2 +#undef F4 + + return funcs[upshift][lineShaderParams]; +} + +GPU_SW_Backend::DrawRectangleFunction +GPU_SW_Backend::GetDrawRectangleFunction(u32 rectShaderParams, int upshift) +{ +#define FN(bmask) &GPU_SW_Backend::DrawRectangle +#define F2(bmask) &GPU_SW_Backend::DrawRectangle +#define F4(bmask) &GPU_SW_Backend::DrawRectangle + + static constexpr DrawRectangleFunction funcs[3][TRectShader_MAX] = { + { + FN(0x000),FN(0x001),FN(0x002),FN(0x003),FN(0x004),FN(0x005),FN(0x006),FN(0x007), + FN(0x008),FN(0x009),FN(0x00a),FN(0x00b),FN(0x00c),FN(0x00d),FN(0x00e),FN(0x00f), + FN(0x010),FN(0x011),FN(0x012),FN(0x013),FN(0x014),FN(0x015),FN(0x016),FN(0x017), + FN(0x018),FN(0x019),FN(0x01a),FN(0x01b),FN(0x01c),FN(0x01d),FN(0x01e),FN(0x01f), + }, + { + F2(0x000),F2(0x001),F2(0x002),F2(0x003),F2(0x004),F2(0x005),F2(0x006),F2(0x007), + F2(0x008),F2(0x009),F2(0x00a),F2(0x00b),F2(0x00c),F2(0x00d),F2(0x00e),F2(0x00f), + F2(0x010),F2(0x011),F2(0x012),F2(0x013),F2(0x014),F2(0x015),F2(0x016),F2(0x017), + F2(0x018),F2(0x019),F2(0x01a),F2(0x01b),F2(0x01c),F2(0x01d),F2(0x01e),F2(0x01f), + }, + { + F4(0x000),F4(0x001),F4(0x002),F4(0x003),F4(0x004),F4(0x005),F4(0x006),F4(0x007), + F4(0x008),F4(0x009),F4(0x00a),F4(0x00b),F4(0x00c),F4(0x00d),F4(0x00e),F4(0x00f), + F4(0x010),F4(0x011),F4(0x012),F4(0x013),F4(0x014),F4(0x015),F4(0x016),F4(0x017), + F4(0x018),F4(0x019),F4(0x01a),F4(0x01b),F4(0x01c),F4(0x01d),F4(0x01e),F4(0x01f), + } + }; + +#undef FN +#undef F2 +#undef F4 + + return funcs[upshift][rectShaderParams]; +} + void GPU_SW_Backend::FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color, GPUBackendCommandParameters params) { const u16 color16 = VRAMRGBA8888ToRGBA5551(color); - if ((x + width) <= VRAM_WIDTH && !params.interlaced_rendering) - { - for (u32 yoffs = 0; yoffs < height; yoffs++) - { - const u32 row = (y + yoffs) % VRAM_HEIGHT; - std::fill_n(&m_vram_ptr[row * VRAM_WIDTH + x], width, color16); - } - } - else if (params.interlaced_rendering) - { - // Hardware tests show that fills seem to break on the first two lines when the offset matches the displayed field. - const u32 active_field = params.active_line_lsb; - for (u32 yoffs = 0; yoffs < height; yoffs++) - { - const u32 row = (y + yoffs) % VRAM_HEIGHT; - if ((row & u32(1)) == active_field) - continue; - u16* row_ptr = &m_vram_ptr[row * VRAM_WIDTH]; - for (u32 xoffs = 0; xoffs < width; xoffs++) + if (m_uprender_shift == 0) + { + if ((x + width) <= VRAM_WIDTH && !params.interlaced_rendering) + { + for (u32 yoffs = 0; yoffs < height; yoffs++) { - const u32 col = (x + xoffs) % VRAM_WIDTH; - row_ptr[col] = color16; + const u32 row = (y + yoffs) % VRAM_HEIGHT; + std::fill_n(&UPRAM_ACCESSOR[row * VRAM_WIDTH + x], width, color16); + } + } + else if (params.interlaced_rendering) + { + // Hardware tests show that fills seem to break on the first two lines when the offset matches the displayed field. + const u32 active_field = params.active_line_lsb; + for (u32 yoffs = 0; yoffs < height; yoffs++) + { + const u32 row = (y + yoffs) % VRAM_HEIGHT; + if ((row & u32(1)) == active_field) + continue; + + u16* row_ptr = &UPRAM_ACCESSOR[row * VRAM_WIDTH]; + for (u32 xoffs = 0; xoffs < width; xoffs++) + { + const u32 col = (x + xoffs) % VRAM_WIDTH; + row_ptr[col] = color16; + } + } + } + else + { + for (u32 yoffs = 0; yoffs < height; yoffs++) + { + const u32 row = (y + yoffs) % VRAM_HEIGHT; + u16* row_ptr = &UPRAM_ACCESSOR[row * VRAM_WIDTH]; + for (u32 xoffs = 0; xoffs < width; xoffs++) + { + const u32 col = (x + xoffs) % VRAM_WIDTH; + row_ptr[col] = color16; + } } } } else { + const u32 active_field = params.active_line_lsb; for (u32 yoffs = 0; yoffs < height; yoffs++) { - const u32 row = (y + yoffs) % VRAM_HEIGHT; - u16* row_ptr = &m_vram_ptr[row * VRAM_WIDTH]; - for (u32 xoffs = 0; xoffs < width; xoffs++) + for(u32 xoffs = 0; xoffs < width; xoffs++) { - const u32 col = (x + xoffs) % VRAM_WIDTH; - row_ptr[col] = color16; + const u32 row = (y + yoffs) % VRAM_HEIGHT; + if (params.interlaced_rendering && (row & u32(1)) == active_field) + continue; + + SetPixelSlow(x+xoffs, y+yoffs, color16); } } } } + void GPU_SW_Backend::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, GPUBackendCommandParameters params) { @@ -767,12 +1234,25 @@ void GPU_SW_Backend::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* if ((x + width) <= VRAM_WIDTH && (y + height) <= VRAM_HEIGHT && !params.IsMaskingEnabled()) { const u16* src_ptr = static_cast(data); - u16* dst_ptr = &m_vram_ptr[y * VRAM_WIDTH + x]; - for (u32 yoffs = 0; yoffs < height; yoffs++) + if (m_uprender_shift == 0) { - std::copy_n(src_ptr, width, dst_ptr); - src_ptr += width; - dst_ptr += VRAM_WIDTH; + u16* dst_ptr = &UPRAM_ACCESSOR[y * VRAM_WIDTH + x]; + for (u32 yoffs = 0; yoffs < height; yoffs++) + { + std::copy_n(src_ptr, width, dst_ptr); + src_ptr += width; + dst_ptr += VRAM_WIDTH; + } + } + else + { + for (u32 yoffs = 0; yoffs < height; yoffs++) + { + for(u32 xoffs = 0; xoffs < width; xoffs++, src_ptr++) + { + SetPixelSlow(x+xoffs, y+yoffs, src_ptr[0]); + } + } } } else @@ -784,18 +1264,57 @@ void GPU_SW_Backend::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* for (u32 row = 0; row < height;) { - u16* dst_row_ptr = &m_vram_ptr[((y + row++) % VRAM_HEIGHT) * VRAM_WIDTH]; + auto dsty = (y + row++) % VRAM_HEIGHT; for (u32 col = 0; col < width;) { // TODO: Handle unaligned reads... - u16* pixel_ptr = &dst_row_ptr[(x + col++) % VRAM_WIDTH]; - if (((*pixel_ptr) & mask_and) == 0) - *pixel_ptr = *(src_ptr++) | mask_or; + auto dstx = (x + col++) % VRAM_WIDTH; + + auto pixel = GetPixelSlow(dstx, dsty); + if ((pixel & mask_and) == 0) + SetPixelSlow(dstx, dsty, *(src_ptr++) | mask_or); } } } } +void GPU_SW_Backend::ReadVRAM(u32 x, u32 y, u32 width, u32 height) +{ + // copy from m_upram_ptr to m_vram_ptr using simple sparse read logic. + + auto* shadow_ptr = GetVRAMshadowPtr(); + auto upscale = uprender_scale(); + + if (upscale == 1) + { + if (shadow_ptr == GetUPRAM()) + return; + + // performance failure - vram copy is not needed when running native res. + DebugAssert(shadow_ptr == GetUPRAM()); + } + + for (u32 yoffs = 0; yoffs < height; yoffs++) + { + const u32 row = (y + yoffs) % VRAM_HEIGHT; + const u16* src = UPRAM_ACCESSOR + (row * VRAM_WIDTH * upscale * upscale); // skip 2 rows, hence 2nd upscale mult + u16* dst = shadow_ptr + (row * VRAM_WIDTH); + for (u32 xoffs = 0; xoffs < width; xoffs++) + { + const u32 col = (x + xoffs) % VRAM_WIDTH; + dst[col] = src[col * upscale]; + } + } + +} + +void GPU_SW_Backend::Sync(bool allow_sleep) +{ + GPUBackend::Sync(allow_sleep); + ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT); +} + + void GPU_SW_Backend::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height, GPUBackendCommandParameters params) { @@ -840,15 +1359,17 @@ void GPU_SW_Backend::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 wi { for (u32 row = 0; row < height; row++) { - const u16* src_row_ptr = &m_vram_ptr[((src_y + row) % VRAM_HEIGHT) * VRAM_WIDTH]; - u16* dst_row_ptr = &m_vram_ptr[((dst_y + row) % VRAM_HEIGHT) * VRAM_WIDTH]; + auto sy = (src_y + row) % VRAM_HEIGHT; + auto dy = (dst_y + row) % VRAM_HEIGHT; for (s32 col = static_cast(width - 1); col >= 0; col--) { - const u16 src_pixel = src_row_ptr[(src_x + static_cast(col)) % VRAM_WIDTH]; - u16* dst_pixel_ptr = &dst_row_ptr[(dst_x + static_cast(col)) % VRAM_WIDTH]; - if ((*dst_pixel_ptr & mask_and) == 0) - *dst_pixel_ptr = src_pixel | mask_or; + auto sx = (src_x + static_cast(col)) % VRAM_WIDTH; + auto dx = (dst_x + static_cast(col)) % VRAM_WIDTH; + auto src_pixel = GetPixelSlow(sx, sy); + auto dst_pixel = GetPixelSlow(dx, dy); + if ((dst_pixel & mask_and) == 0) + SetPixelSlow(dx, dy, src_pixel | mask_or); } } } @@ -856,15 +1377,17 @@ void GPU_SW_Backend::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 wi { for (u32 row = 0; row < height; row++) { - const u16* src_row_ptr = &m_vram_ptr[((src_y + row) % VRAM_HEIGHT) * VRAM_WIDTH]; - u16* dst_row_ptr = &m_vram_ptr[((dst_y + row) % VRAM_HEIGHT) * VRAM_WIDTH]; + auto sy = (src_y + row) % VRAM_HEIGHT; + auto dy = (dst_y + row) % VRAM_HEIGHT; for (u32 col = 0; col < width; col++) { - const u16 src_pixel = src_row_ptr[(src_x + col) % VRAM_WIDTH]; - u16* dst_pixel_ptr = &dst_row_ptr[(dst_x + col) % VRAM_WIDTH]; - if ((*dst_pixel_ptr & mask_and) == 0) - *dst_pixel_ptr = src_pixel | mask_or; + auto sx = (src_x + static_cast(col)) % VRAM_WIDTH; + auto dx = (dst_x + static_cast(col)) % VRAM_WIDTH; + auto src_pixel = GetPixelSlow(sx, sy); + auto dst_pixel = GetPixelSlow(dx, dy); + if ((dst_pixel & mask_and) == 0) + SetPixelSlow(dx, dy, src_pixel | mask_or); } } } @@ -874,69 +1397,6 @@ void GPU_SW_Backend::FlushRender() {} void GPU_SW_Backend::DrawingAreaChanged() {} -GPU_SW_Backend::DrawLineFunction GPU_SW_Backend::GetDrawLineFunction(bool shading_enable, bool transparency_enable, - bool dithering_enable) -{ - static constexpr DrawLineFunction funcs[2][2][2] = { - {{&GPU_SW_Backend::DrawLine, &GPU_SW_Backend::DrawLine}, - {&GPU_SW_Backend::DrawLine, &GPU_SW_Backend::DrawLine}}, - {{&GPU_SW_Backend::DrawLine, &GPU_SW_Backend::DrawLine}, - {&GPU_SW_Backend::DrawLine, &GPU_SW_Backend::DrawLine}}}; - - return funcs[u8(shading_enable)][u8(transparency_enable)][u8(dithering_enable)]; -} - -GPU_SW_Backend::DrawRectangleFunction -GPU_SW_Backend::GetDrawRectangleFunction(bool texture_enable, bool raw_texture_enable, bool transparency_enable) -{ - static constexpr DrawRectangleFunction funcs[2][2][2] = { - {{&GPU_SW_Backend::DrawRectangle, &GPU_SW_Backend::DrawRectangle}, - {&GPU_SW_Backend::DrawRectangle, &GPU_SW_Backend::DrawRectangle}}, - {{&GPU_SW_Backend::DrawRectangle, &GPU_SW_Backend::DrawRectangle}, - {&GPU_SW_Backend::DrawRectangle, &GPU_SW_Backend::DrawRectangle}}}; - - return funcs[u8(texture_enable)][u8(raw_texture_enable)][u8(transparency_enable)]; -} - -GPU_SW_Backend::DrawTriangleFunction GPU_SW_Backend::GetDrawTriangleFunction(bool shading_enable, bool texture_enable, - bool raw_texture_enable, - bool transparency_enable, - bool dithering_enable) -{ - static constexpr DrawTriangleFunction funcs[2][2][2][2][2] = { - {{{{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}, - {{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}}, - {{{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}, - {{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}}}, - {{{{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}, - {{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}}, - {{{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}, - {{&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}, - {&GPU_SW_Backend::DrawTriangle, - &GPU_SW_Backend::DrawTriangle}}}}}; - - return funcs[u8(shading_enable)][u8(texture_enable)][u8(raw_texture_enable)][u8(transparency_enable)] - [u8(dithering_enable)]; -} \ No newline at end of file +#ifdef _MSC_VER +#pragma warning(pop) +#endif diff --git a/src/core/gpu_sw_backend.h b/src/core/gpu_sw_backend.h index 5a7be6ff..276edaae 100644 --- a/src/core/gpu_sw_backend.h +++ b/src/core/gpu_sw_backend.h @@ -4,25 +4,125 @@ #include #include +#define SW_GPU_VRAM_BOUNDS_CHECK 0 +#define USE_FLOAT_STEP 0 +#define USE_INT_STEP 1 + +#if USE_FLOAT_STEP +using AddDeltasScalar_t = float; +#endif + +#if USE_INT_STEP +using AddDeltasScalar_t = int; +#endif + +#define UPRAM_ACCESSOR m_upram_ptr + class GPU_SW_Backend final : public GPUBackend { +protected: + // TODO : rename this to TTrigShader_* + static constexpr u32 TShaderParam_ShadingEnable = (1<<0); + static constexpr u32 TShaderParam_TextureEnable = (1<<1); + static constexpr u32 TShaderParam_RawTextureEnable = (1<<2); + static constexpr u32 TShaderParam_TransparencyEnable = (1<<3); + static constexpr u32 TShaderParam_DitheringEnable = (1<<4); + static constexpr u32 TShaderParam_MaskAndEnable = (1<<5); + static constexpr u32 TShaderParam_MaskOrEnable = (1<<6); + static constexpr u32 TShaderParams_MAX = (1<<7); + +// ideal layout for rectangles ... + static constexpr u32 TRectShader_TransparencyEnable = (1<<0); + static constexpr u32 TRectShader_TextureEnable = (1<<1); + static constexpr u32 TRectShader_RawTextureEnable = (1<<2); + static constexpr u32 TRectShader_MaskAndEnable = (1<<3); + static constexpr u32 TRectShader_MaskOrEnable = (1<<4); + static constexpr u32 TRectShader_MAX = (1<<5); + +// ideal layout for lines ... + static constexpr u32 TLineShader_TransparencyEnable = (1<<0); + static constexpr u32 TLineShader_ShadingEnable = (1<<1); + static constexpr u32 TLineShader_DitheringEnable = (1<<2); + static constexpr u32 TLineShader_MaskAndEnable = (1<<3); + static constexpr u32 TLineShader_MaskOrEnable = (1<<4); + static constexpr u32 TLineShader_MAX = (1<<5); + public: GPU_SW_Backend(); ~GPU_SW_Backend() override; bool Initialize(bool force_thread) override; + void SetUprenderScale(int scale); void Reset(bool clear_vram) override; + void UpdateSettings() override; - ALWAYS_INLINE_RELEASE u16 GetPixel(const u32 x, const u32 y) const { return m_vram[VRAM_WIDTH * y + x]; } - ALWAYS_INLINE_RELEASE const u16* GetPixelPtr(const u32 x, const u32 y) const { return &m_vram[VRAM_WIDTH * y + x]; } - ALWAYS_INLINE_RELEASE u16* GetPixelPtr(const u32 x, const u32 y) { return &m_vram[VRAM_WIDTH * y + x]; } - ALWAYS_INLINE_RELEASE void SetPixel(const u32 x, const u32 y, const u16 value) { m_vram[VRAM_WIDTH * y + x] = value; } + ALWAYS_INLINE u16* GetUPRAM() const { return m_upram_ptr; } + ALWAYS_INLINE u16* GetVRAMshadowPtr() override { return m_vram.data(); } + + ALWAYS_INLINE int uprender_scale() { + return (1 << m_uprender_shift); + } + + + // jstine - uprender TODO - rename this to texel, since it's no longer pixel logic. + // and double check all referenced uses. + + + template + ALWAYS_INLINE u16 GetPixel(const int x, const int y) const + { + auto upshift = RESOLUTION_SHIFT; + auto vram_uprender_size_x = VRAM_WIDTH << upshift; + return UPRAM_ACCESSOR[((y * vram_uprender_size_x) << upshift) + (x << upshift)]; + } + + ALWAYS_INLINE u16 GetPixelSlow(const int x, const int y) const + { + auto upshift = m_uprender_shift; + auto vram_uprender_size_x = VRAM_WIDTH << upshift; + return UPRAM_ACCESSOR[((y * vram_uprender_size_x) << upshift) + (x << upshift)]; + } + + ALWAYS_INLINE void _impl_SetPixel(const int x, const int y, const u16 value, int upshift) + { + auto upscale = (1< + ALWAYS_INLINE void SetPixel(const int x, const int y, const u16 value) + { + _impl_SetPixel(x, y, value, RESOLUTION_SHIFT); + } + + ALWAYS_INLINE void SetPixelSlow(const int x, const int y, const u16 value) + { + _impl_SetPixel(x, y, value, m_uprender_shift); + } // this is actually (31 * 255) >> 4) == 494, but to simplify addressing we use the next power of two (512) static constexpr u32 DITHER_LUT_SIZE = 512; using DitherLUT = std::array, DITHER_MATRIX_SIZE>, DITHER_MATRIX_SIZE>; static constexpr DitherLUT ComputeDitherLUT(); + void Sync(bool allow_sleep) override; + protected: union VRAMPixel { @@ -38,6 +138,13 @@ protected: bits = (ZeroExtend16(r_)) | (ZeroExtend16(g_) << 5) | (ZeroExtend16(b_) << 10) | (static_cast(c_) << 15); } + static VRAMPixel Create(u8 r_, u8 g_, u8 b_, bool c_ = false) + { + VRAMPixel result; + result.Set(r_, g_, b_, c_); + return result; + } + void ClampAndSet(u8 r_, u8 g_, u8 b_, bool c_ = false) { Set(std::min(r_, 0x1F), std::min(g_, 0x1F), std::min(b_, 0x1F), c_); @@ -84,11 +191,14 @@ protected: return std::make_tuple(static_cast(rgb24), static_cast(rgb24 >> 8), static_cast(rgb24 >> 16)); } +public: + void ReadVRAM(u32 x, u32 y, u32 width, u32 height) override; void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color, GPUBackendCommandParameters params) override; void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, GPUBackendCommandParameters params) override; void CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height, GPUBackendCommandParameters params) override; +protected: void DrawPolygon(const GPUBackendDrawPolygonCommand* cmd) override; void DrawLine(const GPUBackendDrawLineCommand* cmd) override; void DrawRectangle(const GPUBackendDrawRectangleCommand* cmd) override; @@ -98,16 +208,12 @@ protected: ////////////////////////////////////////////////////////////////////////// // Rasterization ////////////////////////////////////////////////////////////////////////// - template - void ShadePixel(const GPUBackendDrawCommand* cmd, u32 x, u32 y, u8 color_r, u8 color_g, u8 color_b, u8 texcoord_x, + template + void ShadePixel(const GPUBackendDrawCommand* cmd, s32 x, s32 y, u8 color_r, u8 color_g, u8 color_b, u8 texcoord_x, u8 texcoord_y); - template - void DrawRectangle(const GPUBackendDrawRectangleCommand* cmd); - using DrawRectangleFunction = void (GPU_SW_Backend::*)(const GPUBackendDrawRectangleCommand* cmd); - DrawRectangleFunction GetDrawRectangleFunction(bool texture_enable, bool raw_texture_enable, - bool transparency_enable); + VRAMPixel PlotPixelBlend(GPUTransparencyMode blendMode, VRAMPixel bg_pix, VRAMPixel fore_pix); ////////////////////////////////////////////////////////////////////////// // Polygon and line rasterization ported from Mednafen @@ -131,37 +237,49 @@ protected: bool CalcIDeltas(i_deltas& idl, const GPUBackendDrawPolygonCommand::Vertex* A, const GPUBackendDrawPolygonCommand::Vertex* B, const GPUBackendDrawPolygonCommand::Vertex* C); - template - void AddIDeltas_DX(i_group& ig, const i_deltas& idl, u32 count = 1); + template + void AddIDeltas_DX(I_GROUP& ig, const i_deltas& idl, AddDeltasScalar_t count = 1); - template - void AddIDeltas_DY(i_group& ig, const i_deltas& idl, u32 count = 1); + template + void AddIDeltas_DY(I_GROUP& ig, const i_deltas& idl, AddDeltasScalar_t count = 1); - template + template void DrawSpan(const GPUBackendDrawPolygonCommand* cmd, s32 y, s32 x_start, s32 x_bound, i_group ig, const i_deltas& idl); - template - void DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, const GPUBackendDrawPolygonCommand::Vertex* v0, - const GPUBackendDrawPolygonCommand::Vertex* v1, const GPUBackendDrawPolygonCommand::Vertex* v2); using DrawTriangleFunction = void (GPU_SW_Backend::*)(const GPUBackendDrawPolygonCommand* cmd, const GPUBackendDrawPolygonCommand::Vertex* v0, const GPUBackendDrawPolygonCommand::Vertex* v1, const GPUBackendDrawPolygonCommand::Vertex* v2); - DrawTriangleFunction GetDrawTriangleFunction(bool shading_enable, bool texture_enable, bool raw_texture_enable, - bool transparency_enable, bool dithering_enable); - - template - void DrawLine(const GPUBackendDrawLineCommand* cmd, const GPUBackendDrawLineCommand::Vertex* p0, - const GPUBackendDrawLineCommand::Vertex* p1); using DrawLineFunction = void (GPU_SW_Backend::*)(const GPUBackendDrawLineCommand* cmd, const GPUBackendDrawLineCommand::Vertex* p0, const GPUBackendDrawLineCommand::Vertex* p1); - DrawLineFunction GetDrawLineFunction(bool shading_enable, bool transparency_enable, bool dithering_enable); + using DrawRectangleFunction = void (GPU_SW_Backend::*)(const GPUBackendDrawRectangleCommand* cmd); + + template + void DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, + const GPUBackendDrawPolygonCommand::Vertex* v0, + const GPUBackendDrawPolygonCommand::Vertex* v1, + const GPUBackendDrawPolygonCommand::Vertex* v2); + + template + void DrawLine(const GPUBackendDrawLineCommand* cmd, + const GPUBackendDrawLineCommand::Vertex* p0, + const GPUBackendDrawLineCommand::Vertex* p1); + + template + void DrawRectangle(const GPUBackendDrawRectangleCommand* cmd); + + + DrawTriangleFunction GetDrawTriangleFunction (u32 trigShaderParams, int upshift); + DrawRectangleFunction GetDrawRectangleFunction (u32 rectShaderParams, int upshift); + DrawLineFunction GetDrawLineFunction (u32 lineShaderParams, int upshift); + + int m_uprender_shift = 0; // 0 = native, 1 = 2x, 2 = 4x, etc. std::array m_vram; + u16* m_upram = nullptr; + u16* m_upram_ptr = nullptr; }; diff --git a/src/core/gpu_types.h b/src/core/gpu_types.h index 56353817..7366190e 100644 --- a/src/core/gpu_types.h +++ b/src/core/gpu_types.h @@ -232,6 +232,7 @@ enum class GPUBackendCommandType : u8 { Wraparound, Sync, + ReadVRAM, FillVRAM, UpdateVRAM, CopyVRAM, @@ -280,6 +281,14 @@ struct GPUBackendSyncCommand : public GPUBackendCommand bool allow_sleep; }; +struct GPUBackendReadVRAMCommand : public GPUBackendCommand +{ + u16 x; + u16 y; + u16 width; + u16 height; +}; + struct GPUBackendFillVRAMCommand : public GPUBackendCommand { u16 x;