diff --git a/README.md b/README.md index a4d7d865..df90c44d 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,7 @@ SwanStation features include: - CPU Recompiler/JIT (x86-64, armv7/AArch32 and AArch64) - Hardware (D3D11, OpenGL, Vulkan) and software rendering - - Upscaling in both hardware and software renderers - - Texture filtering, and true colour (24-bit) in hardware renderers + - Upscaling, texture filtering, and true colour (24-bit) in hardware renderers - PGXP for geometry precision, texture correction, and depth buffer emulation - Adaptive downsampling filter - Post processing shader chains diff --git a/README.pt-br.md b/README.pt-br.md index 6e6a0f8a..bc0644ac 100644 --- a/README.pt-br.md +++ b/README.pt-br.md @@ -13,8 +13,7 @@ As características da SwanStation incluem: - CPU Recompilador/JIT (x86-64, armv7/AArch32 e também para AArch64) - Hardware (D3D11, OpenGL, Vulkan) e renderizador por software - - Ampliação tanto em renderizadores de hardware como de software - - Filtragem de textura, "função true color" (24-bit) nos renderizadores baseados por hardware (placa de vídeo) + - Aumento de resolução, filtragem de textura, "função true color" (24-bit) nos renderizadores baseados por hardware (placa de vídeo) - PGXP para precisão de geometria, correção de textura, e profundidade de campo - Filtro adaptativo de resolução - Opções de pós-processamento diff --git a/src/core/gpu.cpp b/src/core/gpu.cpp index 4e827d52..caccc162 100644 --- a/src/core/gpu.cpp +++ b/src/core/gpu.cpp @@ -261,7 +261,7 @@ bool GPU::DoState(StateWrapper& sw, HostDisplayTexture** host_texture, bool upda else { ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT); - sw.DoBytes(GetVRAMshadowPtr(), VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); + sw.DoBytes(m_vram_ptr, VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); } } @@ -999,8 +999,6 @@ 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++) @@ -1279,8 +1277,6 @@ 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()) { @@ -1328,8 +1324,6 @@ 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) { @@ -1366,8 +1360,6 @@ 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) { @@ -1512,11 +1504,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, GetVRAMshadowPtr(), true); + return DumpVRAMToFile(filename, VRAM_WIDTH, VRAM_HEIGHT, sizeof(u16) * VRAM_WIDTH, m_vram_ptr, true); } else if (extension && StringUtil::Strcasecmp(extension, ".bin") == 0) { - return FileSystem::WriteBinaryFile(filename, GetVRAMshadowPtr(), VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); + return FileSystem::WriteBinaryFile(filename, m_vram_ptr, VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16)); } else { diff --git a/src/core/gpu.h b/src/core/gpu.h index 1903ca7a..017cfa1d 100644 --- a/src/core/gpu.h +++ b/src/core/gpu.h @@ -75,9 +75,6 @@ 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); @@ -328,7 +325,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 a7769afd..42673e31 100644 --- a/src/core/gpu_backend.cpp +++ b/src/core/gpu_backend.cpp @@ -44,12 +44,6 @@ void GPUBackend::Shutdown() StopGPUThread(); } -GPUBackendReadVRAMCommand* GPUBackend::NewReadVRAMCommand() -{ - return static_cast( - AllocateCommand(GPUBackendCommandType::ReadVRAM, sizeof(GPUBackendReadVRAMCommand))); -} - GPUBackendFillVRAMCommand* GPUBackend::NewFillVRAMCommand() { return static_cast( @@ -278,14 +272,6 @@ 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 b96d05f4..e8b9d0d8 100644 --- a/src/core/gpu_backend.h +++ b/src/core/gpu_backend.h @@ -19,14 +19,13 @@ public: GPUBackend(); virtual ~GPUBackend(); - virtual u16* GetVRAMshadowPtr() = 0; + ALWAYS_INLINE u16* GetVRAM() const { return m_vram_ptr; } 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(); @@ -36,7 +35,7 @@ public: GPUBackendDrawLineCommand* NewDrawLineCommand(u32 num_vertices); void PushCommand(GPUBackendCommand* cmd); - virtual void Sync(bool allow_sleep); + void Sync(bool allow_sleep); /// Processes all pending GPU commands. void RunGPULoop(); @@ -48,7 +47,6 @@ 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; @@ -62,6 +60,8 @@ 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 d24a05c2..b8cd1bc2 100644 --- a/src/core/gpu_commands.cpp +++ b/src/core/gpu_commands.cpp @@ -577,7 +577,6 @@ 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 70760719..ddb8fa89 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -33,6 +33,7 @@ ALWAYS_INLINE static bool ShouldUseUVLimits() GPU_HW::GPU_HW() : GPU() { + m_vram_ptr = m_vram_shadow.data(); } GPU_HW::~GPU_HW() @@ -1124,7 +1125,7 @@ void GPU_HW::UpdateSoftwareRenderer(bool copy_vram_from_hw) if (current_enabled == new_enabled) return; - auto m_vram_ptr = GetVRAMshadowPtr(); + m_vram_ptr = m_vram_shadow.data(); if (!new_enabled) { @@ -1143,7 +1144,7 @@ void GPU_HW::UpdateSoftwareRenderer(bool copy_vram_from_hw) { FlushRender(); ReadVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT); - std::memcpy(sw_renderer->GetVRAMshadowPtr(), m_vram_ptr, sizeof(u16) * VRAM_WIDTH * VRAM_HEIGHT); + std::memcpy(sw_renderer->GetVRAM(), m_vram_ptr, sizeof(u16) * VRAM_WIDTH * VRAM_HEIGHT); // Sync the drawing area. GPUBackendSetDrawingAreaCommand* cmd = sw_renderer->NewSetDrawingAreaCommand(); @@ -1152,6 +1153,7 @@ 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 8bf51134..d609e426 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -41,8 +41,6 @@ public: std::tuple GetEffectiveDisplayResolution(bool scaled = true) override final; std::tuple GetFullDisplayResolution(bool scaled = true) 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 add1bc01..584ea253 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, GetVRAMshadowPtr(), false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 62877178..27a44022 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, GetVRAMshadowPtr(), false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_hw_opengl.cpp b/src/core/gpu_hw_opengl.cpp index 96bb72a6..64daba8c 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, GetVRAMshadowPtr(), false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index c9e9075a..5dd48a4b 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -235,7 +235,7 @@ void GPU_HW_Vulkan::UpdateSettings() if (framebuffer_changed) { RestoreGraphicsAPIState(); - UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, GetVRAMshadowPtr(), false, false); + UpdateVRAM(0, 0, VRAM_WIDTH, VRAM_HEIGHT, m_vram_ptr, false, false); UpdateDepthBufferFromMaskBit(); UpdateDisplay(); ResetGraphicsAPIState(); diff --git a/src/core/gpu_sw.cpp b/src/core/gpu_sw.cpp index 6aa109b3..5d8f14e1 100644 --- a/src/core/gpu_sw.cpp +++ b/src/core/gpu_sw.cpp @@ -28,10 +28,9 @@ 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() @@ -39,9 +38,6 @@ 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 @@ -49,34 +45,11 @@ 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 = @@ -119,7 +92,6 @@ void GPU_SW::UpdateSettings() { GPU::UpdateSettings(); m_backend.UpdateSettings(); - AllocDisplayBuffer(); } template @@ -251,28 +223,17 @@ ALWAYS_INLINE void CopyOutRow16(const u16* s } template -void GPU_SW::CopyOut15Bit(u32 src_x_native, u32 src_y_native, u32 width_native, u32 height_native, u32 field, bool interlaced, bool interleaved) +void GPU_SW::CopyOut15Bit(u32 src_x, u32 src_y, u32 width, u32 height, 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_up, height_up, reinterpret_cast(&dst_ptr), + if (!m_host_display->BeginSetDisplayPixels(display_format, width, height, reinterpret_cast(&dst_ptr), &dst_stride)) { return; @@ -280,49 +241,44 @@ void GPU_SW::CopyOut15Bit(u32 src_x_native, u32 src_y_native, u32 width_native, } else { - dst_stride = GPU_MAX_DISPLAY_WIDTH * m_display_texture_scale * sizeof(OutputPixelType); - dst_ptr = m_display_texture_buffer + (field != 0 ? dst_stride : 0); + dst_stride = GPU_MAX_DISPLAY_WIDTH * sizeof(OutputPixelType); + dst_ptr = m_display_texture_buffer.data() + (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_native + width_native) <= VRAM_WIDTH && (src_y_native + height_native) <= VRAM_HEIGHT) + if ((src_x + width) <= VRAM_WIDTH && (src_y + height) <= VRAM_HEIGHT) { - const u32 rows = height_up >> interlaced_shift; + const u32 rows = height >> interlaced_shift; dst_stride <<= interlaced_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; + const u16* src_ptr = &m_vram_ptr[src_y * VRAM_WIDTH + src_x]; + const u32 src_step = VRAM_WIDTH << interleaved_shift; for (u32 row = 0; row < rows; row++) { - CopyOutRow16(src_ptr, reinterpret_cast(dst_ptr), width_up); + CopyOutRow16(src_ptr, reinterpret_cast(dst_ptr), width); src_ptr += src_step; dst_ptr += dst_stride; } } else { - const u32 rows = height_up >> interlaced_shift; + const u32 rows = height >> interlaced_shift; dst_stride <<= interlaced_shift; - const u32 end_x_up = src_x_up + width_up; + const u32 end_x = src_x + width; for (u32 row = 0; row < rows; row++) { - const u16* src_row_ptr = &m_vram_ptr[(src_y_up % vram_upsize_y) * vram_upsize_x]; + const u16* src_row_ptr = &m_vram_ptr[(src_y % VRAM_HEIGHT) * VRAM_WIDTH]; OutputPixelType* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = src_x_up; col < end_x_up; col++) - *(dst_row_ptr++) = VRAM16ToOutput(src_row_ptr[col % vram_upsize_x]); + for (u32 col = src_x; col < end_x; col++) + *(dst_row_ptr++) = VRAM16ToOutput(src_row_ptr[col % VRAM_WIDTH]); - src_y_up += (1 << interleaved_shift); + src_y += (1 << interleaved_shift); dst_ptr += dst_stride; } } @@ -333,7 +289,7 @@ void GPU_SW::CopyOut15Bit(u32 src_x_native, u32 src_y_native, u32 width_native, } else { - m_host_display->SetDisplayPixels(display_format, width_up, height_up, m_display_texture_buffer, output_stride); + m_host_display->SetDisplayPixels(display_format, width, height, m_display_texture_buffer.data(), output_stride); } } @@ -360,36 +316,18 @@ void GPU_SW::CopyOut15Bit(HostDisplayPixelFormat display_format, u32 src_x, u32 } template -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, +void GPU_SW::CopyOut24Bit(u32 src_x, u32 src_y, u32 skip_x, u32 width, u32 height, 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_up, height_up, reinterpret_cast(&dst_ptr), + if (!m_host_display->BeginSetDisplayPixels(display_format, width, height, reinterpret_cast(&dst_ptr), &dst_stride)) { return; @@ -397,76 +335,85 @@ void GPU_SW::CopyOut24Bit(u32 src_x_native, u32 src_y_native, u32 skip_x_native, } else { - dst_stride = GPU_MAX_DISPLAY_WIDTH * m_display_texture_scale * sizeof(OutputPixelType); - dst_ptr = m_display_texture_buffer + (field != 0 ? dst_stride : 0); + dst_stride = Common::AlignUpPow2(width * sizeof(OutputPixelType), 4); + dst_ptr = m_display_texture_buffer.data() + (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_native = height_native >> interlaced_shift; - + const u32 rows = height >> interlaced_shift; dst_stride <<= interlaced_shift; - auto shadow_ptr = m_backend.GetVRAMshadowPtr(); - - if ((src_x_native + width_native) <= VRAM_WIDTH && (src_y_native + (rows_native << interleaved_shift)) <= VRAM_HEIGHT) + if ((src_x + width) <= VRAM_WIDTH && (src_y + (rows << 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 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_native; row++, src_ptr += src_stride) + for (u32 row = 0; row < rows; row++) { - for (int uy = 0; uy < uprender_scale; ++uy, dst_ptr += dst_stride) + if constexpr (display_format == HostDisplayPixelFormat::RGBA8) { - 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) + const u8* src_row_ptr = src_ptr; + u8* dst_row_ptr = reinterpret_cast(dst_ptr); + for (u32 col = 0; col < width; col++) { - 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); - } - } + *(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; + } + } + + src_ptr += src_stride; + dst_ptr += dst_stride; } } else { - // FIXME : UPRENDER : This has not been implemented yet. - for (u32 row = 0; row < rows_native; row++) + for (u32 row = 0; row < rows; row++) { - const u16* src_row_ptr = &shadow_ptr[(src_y_native % VRAM_HEIGHT) * VRAM_WIDTH]; + const u16* src_row_ptr = &m_vram_ptr[(src_y % VRAM_HEIGHT) * VRAM_WIDTH]; OutputPixelType* dst_row_ptr = reinterpret_cast(dst_ptr); - for (u32 col = 0; col < width_up; col++) + for (u32 col = 0; col < width; col++) { - const u32 offset = (src_x_up + (((skip_x_up + col) * 3) / 2)); - const u16 s0 = src_row_ptr[(offset + 0) % VRAM_WIDTH]; + const u32 offset = (src_x + (((skip_x + col) * 3) / 2)); + const u16 s0 = src_row_ptr[offset % 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); @@ -489,7 +436,7 @@ void GPU_SW::CopyOut24Bit(u32 src_x_native, u32 src_y_native, u32 skip_x_native, } } - src_y_up += (1 << interleaved_shift); + src_y += (1 << interleaved_shift); dst_ptr += dst_stride; } } @@ -500,7 +447,7 @@ void GPU_SW::CopyOut24Bit(u32 src_x_native, u32 src_y_native, u32 skip_x_native, } else { - m_host_display->SetDisplayPixels(display_format, width_up, height_up, m_display_texture_buffer, output_stride); + m_host_display->SetDisplayPixels(display_format, width, height, m_display_texture_buffer.data(), output_stride); } } @@ -529,22 +476,14 @@ void GPU_SW::CopyOut24Bit(HostDisplayPixelFormat display_format, u32 src_x, u32 void GPU_SW::ClearDisplay() { - if (m_display_texture_buffer) - std::memset(m_display_texture_buffer, 0, getDisplayTextureSizeInBytes(m_display_texture_scale)); + std::memset(m_display_texture_buffer.data(), 0, m_display_texture_buffer.size()); } 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) { m_host_display->SetDisplayParameters(m_crtc_state.display_width, m_crtc_state.display_height, @@ -595,8 +534,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_upsize_x, vram_upsize_y, 0, 0, vram_upsize_x, vram_upsize_y, - static_cast(vram_upsize_x) / static_cast(vram_upsize_y)); + m_host_display->SetDisplayParameters(VRAM_WIDTH, VRAM_HEIGHT, 0, 0, VRAM_WIDTH, VRAM_HEIGHT, + static_cast(VRAM_WIDTH) / static_cast(VRAM_HEIGHT)); } } @@ -885,13 +824,6 @@ 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 7e3a17d5..6ba407e3 100644 --- a/src/core/gpu_sw.h +++ b/src/core/gpu_sw.h @@ -17,15 +17,11 @@ 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; @@ -51,8 +47,7 @@ protected: void FillBackendCommandParameters(GPUBackendCommand* cmd) const; void FillDrawCommand(GPUBackendDrawCommand* cmd, GPURenderCommand rc) const; - int m_display_texture_scale = 0; // mirrors backbuffer uprender scale - u8* m_display_texture_buffer = nullptr; + HeapArray m_display_texture_buffer; 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 1dcd6b19..c32c23b6 100644 --- a/src/core/gpu_sw_backend.cpp +++ b/src/core/gpu_sw_backend.cpp @@ -5,109 +5,27 @@ #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(); } -static ptrdiff_t getVRAMSizeInBytes(int scale) -{ - return VRAM_WIDTH * VRAM_HEIGHT * sizeof(u16) * (scale*scale); -} - -void GPU_SW_Backend::SetUprenderScale(int scale) -{ - //const GPURenderer renderer = GetRendererType(); - if (g_settings.gpu_renderer != GPURenderer::Software) - { - scale = 1; - } - else - { - switch (scale) - { - case 1: - case 2: - // OK! - 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 > 2) - scale = 2; - 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; - } -} +GPU_SW_Backend::~GPU_SW_Backend() = default; bool GPU_SW_Backend::Initialize(bool force_thread) { - SetUprenderScale(g_settings.gpu_resolution_soft_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_soft_scale); -} - void GPU_SW_Backend::Reset(bool clear_vram) { GPUBackend::Reset(clear_vram); if (clear_vram) - memset(m_upram_ptr, 0, getVRAMSizeInBytes(uprender_scale())); + m_vram.fill(0); } void GPU_SW_Backend::DrawPolygon(const GPUBackendDrawPolygonCommand* cmd) @@ -115,17 +33,8 @@ 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; - 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 - ); + const DrawTriangleFunction DrawFunction = GetDrawTriangleFunction( + rc.shading_enable, rc.texture_enable, rc.raw_texture_enable, rc.transparency_enable, dithering_enable); (this->*DrawFunction)(cmd, &cmd->vertices[0], &cmd->vertices[1], &cmd->vertices[2]); if (rc.quad_polygon) @@ -136,30 +45,16 @@ void GPU_SW_Backend::DrawRectangle(const GPUBackendDrawRectangleCommand* cmd) { const GPURenderCommand rc{cmd->rc.bits}; - 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 - ); + const DrawRectangleFunction DrawFunction = + GetDrawRectangleFunction(rc.texture_enable, rc.raw_texture_enable, rc.transparency_enable); (this->*DrawFunction)(cmd); } void GPU_SW_Backend::DrawLine(const GPUBackendDrawLineCommand* cmd) { - 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 - ); + const DrawLineFunction DrawFunction = + GetDrawLineFunction(cmd->rc.shading_enable, cmd->rc.transparency_enable, cmd->IsDitheringEnabled()); for (u16 i = 1; i < cmd->num_vertices; i++) (this->*DrawFunction)(cmd, &cmd->vertices[i - 1], &cmd->vertices[i]); @@ -184,25 +79,16 @@ 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, s32 x, s32 y, u8 color_r, - u8 color_g, u8 color_b, u8 texcoord_x_u8, u8 texcoord_y_u8) +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) { -// 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 - 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; + texcoord_x = (texcoord_x & cmd->window.and_x) | cmd->window.or_x; + texcoord_y = (texcoord_y & cmd->window.and_y) | cmd->window.or_y; VRAMPixel texture_color; switch (cmd->draw_mode.texture_mode) @@ -210,30 +96,30 @@ void ALWAYS_INLINE_RELEASE GPU_SW_Backend::ShadePixel(const GPUBackendDrawComman case GPUTextureMode::Palette4Bit: { const u16 palette_value = - GetPixel((cmd->draw_mode.GetTexturePageBaseX() + (texcoord_x / 4)) % VRAM_WIDTH, - (cmd->draw_mode.GetTexturePageBaseY() + (texcoord_y / 1)) % VRAM_HEIGHT); + GetPixel((cmd->draw_mode.GetTexturePageBaseX() + ZeroExtend32(texcoord_x / 4)) % VRAM_WIDTH, + (cmd->draw_mode.GetTexturePageBaseY() + ZeroExtend32(texcoord_y)) % VRAM_HEIGHT); const u16 palette_index = (palette_value >> ((texcoord_x % 4) * 4)) & 0x0Fu; texture_color.bits = - GetPixel((cmd->palette.GetXBase() + (palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); + GetPixel((cmd->palette.GetXBase() + ZeroExtend32(palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); } break; case GPUTextureMode::Palette8Bit: { const u16 palette_value = - GetPixel((cmd->draw_mode.GetTexturePageBaseX() + (texcoord_x / 2)) % VRAM_WIDTH, - (cmd->draw_mode.GetTexturePageBaseY() + (texcoord_y / 1)) % VRAM_HEIGHT); + GetPixel((cmd->draw_mode.GetTexturePageBaseX() + ZeroExtend32(texcoord_x / 2)) % VRAM_WIDTH, + (cmd->draw_mode.GetTexturePageBaseY() + ZeroExtend32(texcoord_y)) % VRAM_HEIGHT); const u16 palette_index = (palette_value >> ((texcoord_x % 2) * 8)) & 0xFFu; texture_color.bits = - GetPixel((cmd->palette.GetXBase() + (palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); + GetPixel((cmd->palette.GetXBase() + ZeroExtend32(palette_index)) % VRAM_WIDTH, cmd->palette.GetYBase()); } break; default: { - texture_color.bits = GetPixel((cmd->draw_mode.GetTexturePageBaseX() + (texcoord_x)) % VRAM_WIDTH, - (cmd->draw_mode.GetTexturePageBaseY() + (texcoord_y)) % VRAM_HEIGHT); + texture_color.bits = GetPixel((cmd->draw_mode.GetTexturePageBaseX() + ZeroExtend32(texcoord_x)) % VRAM_WIDTH, + (cmd->draw_mode.GetTexturePageBaseY() + ZeroExtend32(texcoord_y)) % VRAM_HEIGHT); } break; } @@ -267,20 +153,10 @@ 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); } - auto constexpr vram_upsize_x = VRAM_WIDTH << TUprenderShift; - - if constexpr (transparency_enable || mask_and_enable) + const VRAMPixel bg_color{GetPixel(static_cast(x), static_cast(y))}; + if constexpr (transparency_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)) + if (color.bits & 0x8000u || !texture_enable) { // Based on blargg's efficient 15bpp pixel math. u32 bg_bits = ZeroExtend32(bg_color.bits); @@ -336,69 +212,42 @@ void ALWAYS_INLINE_RELEASE GPU_SW_Backend::ShadePixel(const GPUBackendDrawComman } } - u16 result = color.bits; + const u16 mask_and = cmd->params.GetMaskAND(); + if ((bg_color.bits & mask_and) != 0) + return; - if constexpr (mask_or_enable) - result |= cmd->params.GetMaskOR(); - - UPRAM_ACCESSOR[vram_upsize_x * y + x] = result; + SetPixel(static_cast(x), static_cast(y), color.bits | cmd->params.GetMaskOR()); } -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); - 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++) + for (u32 offset_y = 0; offset_y < cmd->height; offset_y++) { - 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))) + 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))) { continue; } - const u8 texcoord_y = Truncate8(ZeroExtend32(origin_texcoord_y) + (offset_y / upscale)); + const u8 texcoord_y = Truncate8(ZeroExtend32(origin_texcoord_y) + offset_y); - for (int offset_x = 0; offset_x < size_x_up; offset_x++) + for (u32 offset_x = 0; offset_x < cmd->width; offset_x++) { - 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)) + const s32 x = origin_x + static_cast(offset_x); + if (x < static_cast(m_drawing_area.left) || x > static_cast(m_drawing_area.right)) continue; - // 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)); + const u8 texcoord_x = Truncate8(ZeroExtend32(origin_texcoord_x) + offset_x); - 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); + ShadePixel( + cmd, static_cast(x), static_cast(y), r, g, b, texcoord_x, texcoord_y); } } } @@ -409,7 +258,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 10 +#define COORD_POST_PADDING 12 static ALWAYS_INLINE_RELEASE s64 MakePolyXFP(s32 x) { @@ -475,198 +324,182 @@ 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, AddDeltasScalar_t count /*= 1*/) +template +void ALWAYS_INLINE_RELEASE GPU_SW_Backend::AddIDeltas_DX(i_group& ig, const i_deltas& idl, u32 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, AddDeltasScalar_t count /*= 1*/) +template +void ALWAYS_INLINE_RELEASE GPU_SW_Backend::AddIDeltas_DY(i_group& ig, const i_deltas& idl, u32 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; } } -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, +template +void GPU_SW_Backend::DrawSpan(const GPUBackendDrawPolygonCommand* cmd, s32 y, s32 x_start, s32 x_bound, i_group ig, const i_deltas& idl) { - 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)) + if (cmd->params.interlaced_rendering && cmd->params.active_line_lsb == (Truncate8(static_cast(y)) & 1u)) return; - 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); + s32 x_ig_adjust = x_start; + s32 w = x_bound - x_start; + s32 x = TruncateGPUVertexPosition(x_start); - if (x_up < static_cast(m_drawing_area.left * upscale)) + if (x < static_cast(m_drawing_area.left)) { - 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; + s32 delta = static_cast(m_drawing_area.left) - x; + x_ig_adjust += delta; + x += delta; + w -= delta; } - 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 ((x + w) > (static_cast(m_drawing_area.right) + 1)) + w = static_cast(m_drawing_area.right) + 1 - x; - if (w_up <= 0) + if (w <= 0) return; -#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 + AddIDeltas_DX(ig, idl, x_ig_adjust); + AddIDeltas_DY(ig, idl, y); do { - DebugAssert(x_up <= static_cast(m_drawing_area.right+1) * upscale); + 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); -#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 + ShadePixel( + cmd, static_cast(x), static_cast(y), Truncate8(r), Truncate8(g), Truncate8(b), Truncate8(u), + Truncate8(v)); -#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); + x++; + AddIDeltas_DX(ig, idl); + } while (--w > 0); } -template +template void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, - const GPUBackendDrawPolygonCommand::Vertex* v0n, - const GPUBackendDrawPolygonCommand::Vertex* v1n, - const GPUBackendDrawPolygonCommand::Vertex* v2n) + const GPUBackendDrawPolygonCommand::Vertex* v0, + const GPUBackendDrawPolygonCommand::Vertex* v1, + const GPUBackendDrawPolygonCommand::Vertex* v2) { - int constexpr upscale = (1 << TUprenderShift); + u32 core_vertex; + { + u32 cvtemp = 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 ; + 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); struct TriangleHalf { @@ -679,150 +512,36 @@ void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, bool dec_mode; } tripart[2]; - i_group ig; - i_deltas idl; + u32 vo = 0; + u32 vp = 0; + if (core_vertex != 0) + vo = 1; + if (core_vertex == 2) + vp = 3; - if (1) { - 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]; + 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; } - // TODO: none of the code above this line depends on template parameters. - // We could generalize that code and then + { + 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; + } for (u32 i = 0; i < 2; i++) { @@ -843,31 +562,32 @@ void GPU_SW_Backend::DrawTriangle(const GPUBackendDrawPolygonCommand* cmd, lc -= ls; rc -= rs; - s32 y = SignExtendN<11 + TUprenderShift, s32>(yi); + s32 y = TruncateGPUVertexPosition(yi); - if (y < static_cast(m_drawing_area.top) * upscale) + if (y < static_cast(m_drawing_area.top)) break; - if (y > (static_cast(m_drawing_area.bottom) * upscale) + (upscale/2)) + if (y > static_cast(m_drawing_area.bottom)) continue; - DrawSpan( - cmd, y, GetPolyXFP_Int(lc), GetPolyXFP_Int(rc), ig, idl); + DrawSpan( + cmd, yi, GetPolyXFP_Int(lc), GetPolyXFP_Int(rc), ig, idl); } } else { while (yi < yb) { - s32 y = SignExtendN<11 + TUprenderShift, s32>(yi); + s32 y = TruncateGPUVertexPosition(yi); - if (y > static_cast(m_drawing_area.bottom) * upscale) + if (y > static_cast(m_drawing_area.bottom)) break; - if (y >= static_cast(m_drawing_area.top) * upscale) + if (y >= static_cast(m_drawing_area.top)) { - DrawSpan( - cmd, y, GetPolyXFP_Int(lc), GetPolyXFP_Int(rc), ig, idl); + + DrawSpan( + cmd, yi, GetPolyXFP_Int(lc), GetPolyXFP_Int(rc), ig, idl); } yi++; @@ -878,76 +598,6 @@ 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 @@ -981,17 +631,10 @@ 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; @@ -1051,37 +694,16 @@ 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; - // 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 - ); - } - } + ShadePixel(cmd, static_cast(x), static_cast(y), r, + g, b, 0, 0); } + cur_point.x += step.dx_dk; cur_point.y += step.dy_dk; @@ -1094,141 +716,50 @@ 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 (m_uprender_shift == 0) + if ((x + width) <= VRAM_WIDTH && !params.interlaced_rendering) { - if ((x + width) <= VRAM_WIDTH && !params.interlaced_rendering) + for (u32 yoffs = 0; yoffs < height; yoffs++) { - for (u32 yoffs = 0; yoffs < height; yoffs++) - { - const u32 row = (y + yoffs) % VRAM_HEIGHT; - std::fill_n(&UPRAM_ACCESSOR[row * VRAM_WIDTH + x], width, color16); - } + const u32 row = (y + yoffs) % VRAM_HEIGHT; + std::fill_n(&m_vram_ptr[row * VRAM_WIDTH + x], width, color16); } - else if (params.interlaced_rendering) + } + 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++) { - // 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++) { - 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; - } + 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++) { - for(u32 xoffs = 0; xoffs < width; xoffs++) + const u32 row = (y + yoffs) % VRAM_HEIGHT; + u16* row_ptr = &m_vram_ptr[row * VRAM_WIDTH]; + for (u32 xoffs = 0; xoffs < width; xoffs++) { - const u32 row = (y + yoffs) % VRAM_HEIGHT; - if (params.interlaced_rendering && (row & u32(1)) == active_field) - continue; - - SetPixelSlow(x+xoffs, y+yoffs, color16); + const u32 col = (x + xoffs) % VRAM_WIDTH; + row_ptr[col] = color16; } } } } - void GPU_SW_Backend::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, GPUBackendCommandParameters params) { @@ -1236,25 +767,12 @@ 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); - if (m_uprender_shift == 0) + u16* dst_ptr = &m_vram_ptr[y * VRAM_WIDTH + x]; + for (u32 yoffs = 0; yoffs < height; yoffs++) { - 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]); - } - } + std::copy_n(src_ptr, width, dst_ptr); + src_ptr += width; + dst_ptr += VRAM_WIDTH; } } else @@ -1266,57 +784,18 @@ void GPU_SW_Backend::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* for (u32 row = 0; row < height;) { - auto dsty = (y + row++) % VRAM_HEIGHT; + u16* dst_row_ptr = &m_vram_ptr[((y + row++) % VRAM_HEIGHT) * VRAM_WIDTH]; for (u32 col = 0; col < width;) { // TODO: Handle unaligned reads... - auto dstx = (x + col++) % VRAM_WIDTH; - - auto pixel = GetPixelSlow(dstx, dsty); - if ((pixel & mask_and) == 0) - SetPixelSlow(dstx, dsty, *(src_ptr++) | mask_or); + u16* pixel_ptr = &dst_row_ptr[(x + col++) % VRAM_WIDTH]; + if (((*pixel_ptr) & mask_and) == 0) + *pixel_ptr = *(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) { @@ -1361,17 +840,15 @@ 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++) { - auto sy = (src_y + row) % VRAM_HEIGHT; - auto dy = (dst_y + row) % VRAM_HEIGHT; + 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]; for (s32 col = static_cast(width - 1); col >= 0; col--) { - 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); + 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; } } } @@ -1379,17 +856,15 @@ 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++) { - auto sy = (src_y + row) % VRAM_HEIGHT; - auto dy = (dst_y + row) % VRAM_HEIGHT; + 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]; for (u32 col = 0; col < width; col++) { - 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); + 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; } } } @@ -1399,6 +874,69 @@ void GPU_SW_Backend::FlushRender() {} void GPU_SW_Backend::DrawingAreaChanged() {} -#ifdef _MSC_VER -#pragma warning(pop) -#endif +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 diff --git a/src/core/gpu_sw_backend.h b/src/core/gpu_sw_backend.h index 276edaae..5a7be6ff 100644 --- a/src/core/gpu_sw_backend.h +++ b/src/core/gpu_sw_backend.h @@ -4,125 +4,25 @@ #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 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); - } + 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; } // 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 { @@ -138,13 +38,6 @@ 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_); @@ -191,14 +84,11 @@ 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; @@ -208,12 +98,16 @@ protected: ////////////////////////////////////////////////////////////////////////// // Rasterization ////////////////////////////////////////////////////////////////////////// - template - void ShadePixel(const GPUBackendDrawCommand* cmd, s32 x, s32 y, u8 color_r, u8 color_g, u8 color_b, u8 texcoord_x, + template + void 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 DrawRectangle(const GPUBackendDrawRectangleCommand* cmd); - VRAMPixel PlotPixelBlend(GPUTransparencyMode blendMode, VRAMPixel bg_pix, VRAMPixel fore_pix); + using DrawRectangleFunction = void (GPU_SW_Backend::*)(const GPUBackendDrawRectangleCommand* cmd); + DrawRectangleFunction GetDrawRectangleFunction(bool texture_enable, bool raw_texture_enable, + bool transparency_enable); ////////////////////////////////////////////////////////////////////////// // Polygon and line rasterization ported from Mednafen @@ -237,49 +131,37 @@ 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, AddDeltasScalar_t count = 1); + template + void AddIDeltas_DX(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 + void AddIDeltas_DY(i_group& ig, const i_deltas& idl, u32 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); - 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. + DrawLineFunction GetDrawLineFunction(bool shading_enable, bool transparency_enable, bool dithering_enable); 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 7366190e..56353817 100644 --- a/src/core/gpu_types.h +++ b/src/core/gpu_types.h @@ -232,7 +232,6 @@ enum class GPUBackendCommandType : u8 { Wraparound, Sync, - ReadVRAM, FillVRAM, UpdateVRAM, CopyVRAM, @@ -281,14 +280,6 @@ 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; diff --git a/src/duckstation-libretro/libretro_core_options.h b/src/duckstation-libretro/libretro_core_options.h index f12c10dc..42dca9d4 100644 --- a/src/duckstation-libretro/libretro_core_options.h +++ b/src/duckstation-libretro/libretro_core_options.h @@ -420,22 +420,6 @@ struct retro_core_option_v2_definition option_defs_us[] = { }, "1" }, - { - "duckstation_GPU.ResolutionSoftScale", - "Enhanced Resolution (Software)", - NULL, - "Renders in double resolution at the cost of lower performance.", - NULL, - "enhancement", - { - { "1", "Disabled" }, -#ifndef ANDROID - { "2", "Enabled" }, -#endif - { NULL, NULL }, - }, - "1" - }, { "duckstation_GPU.UseThread", "Threaded Rendering (Software)", @@ -452,7 +436,7 @@ struct retro_core_option_v2_definition option_defs_us[] = { }, { "duckstation_GPU.UseSoftwareRendererForReadbacks", - "Use Software Renderer For Readbacks", + "Use Software Renderer For Readbacks (Restart)", NULL, "Runs the software renderer in parallel for VRAM readbacks. On some systems, this may " "result in greater performance when using graphical enhancements with the hardware renderer.", diff --git a/src/duckstation-libretro/libretro_host_display.cpp b/src/duckstation-libretro/libretro_host_display.cpp index 11050a95..aeb66ae0 100644 --- a/src/duckstation-libretro/libretro_host_display.cpp +++ b/src/duckstation-libretro/libretro_host_display.cpp @@ -211,7 +211,7 @@ bool LibretroHostDisplay::BeginSetDisplayPixels(HostDisplayPixelFormat format, u m_software_fb.format = RETRO_PIXEL_FORMAT_UNKNOWN; m_software_fb.access_flags = RETRO_MEMORY_ACCESS_WRITE; m_software_fb.memory_flags = 0; - /*if (g_retro_environment_callback(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &m_software_fb) && + /* if (g_retro_environment_callback(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &m_software_fb) && m_software_fb.format == retro_pf) { SetDisplayTexture(m_software_fb.data, format, m_software_fb.width, m_software_fb.height, 0, 0, m_software_fb.width, @@ -219,7 +219,7 @@ bool LibretroHostDisplay::BeginSetDisplayPixels(HostDisplayPixelFormat format, u *out_buffer = m_software_fb.data; *out_pitch = static_cast(m_software_fb.pitch); return true; - }*/ + } */ const u32 pitch = Common::AlignUpPow2(width * GetDisplayPixelFormatSize(format), 4); const u32 required_size = height * pitch; @@ -254,6 +254,10 @@ bool LibretroHostDisplay::Render() if (m_display_texture_handle == m_software_fb.data) ClearDisplayTexture(); } + else + { + g_retro_video_refresh_callback(nullptr, 0, 0, 0); + } return true; } @@ -261,5 +265,5 @@ bool LibretroHostDisplay::Render() bool LibretroHostDisplay::RenderScreenshot(u32 width, u32 height, std::vector* out_pixels, u32* out_stride, HostDisplayPixelFormat* out_format) { - return true; + return false; } diff --git a/src/duckstation-libretro/libretro_host_interface.cpp b/src/duckstation-libretro/libretro_host_interface.cpp index 163d3151..192214be 100644 --- a/src/duckstation-libretro/libretro_host_interface.cpp +++ b/src/duckstation-libretro/libretro_host_interface.cpp @@ -566,17 +566,11 @@ bool LibretroHostInterface::retro_load_game(const struct retro_game_info* game) g_retro_environment_callback(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display); option_display.key = "duckstation_GPU.ResolutionScale"; g_retro_environment_callback(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display); -#ifdef ANDROID - option_display.key = "duckstation_GPU.ResolutionSoftScale"; - g_retro_environment_callback(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display); -#endif } else { option_display.key = "duckstation_GPU.UseThread"; g_retro_environment_callback(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display); - option_display.key = "duckstation_GPU.ResolutionSoftScale"; - g_retro_environment_callback(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display); } return true; @@ -945,6 +939,12 @@ void LibretroHostInterface::UpdateSettings() ReportFormattedMessage("Setting memory card 1 to Save RAM mode will apply on core reload, to prevent save loss."); g_settings.memory_card_types[0] = old_settings.memory_card_types[0]; } + + if (g_settings.gpu_use_software_renderer_for_readbacks != old_settings.gpu_use_software_renderer_for_readbacks) + { + ReportFormattedMessage("Please restart the core to apply this setting."); + g_settings.gpu_use_software_renderer_for_readbacks = old_settings.gpu_use_software_renderer_for_readbacks; + } } CheckForSettingsChanges(old_settings);