From 4d38213f2351a88c1720840ef36b99d61e6d176d Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 18 Sep 2019 00:58:30 +1000 Subject: [PATCH] GPU: Implement VRAM-to-VRAM copies --- src/pse/gpu.cpp | 42 +++++++++++++++++++++++++++++++++++++-- src/pse/gpu.h | 2 ++ src/pse/gpu_hw_opengl.cpp | 13 ++++++++++++ src/pse/gpu_hw_opengl.h | 1 + 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/pse/gpu.cpp b/src/pse/gpu.cpp index 0958cec23..c66dd62da 100644 --- a/src/pse/gpu.cpp +++ b/src/pse/gpu.cpp @@ -343,7 +343,10 @@ void GPU::WriteGP0(u32 value) case 0x00: // NOP break; - case 0x02: // Fill Rectnagle + case 0x01: // Clear cache + break; + + case 0x02: // Fill Rectangle { if (!HandleFillRectangleCommand()) return; @@ -357,13 +360,20 @@ void GPU::WriteGP0(u32 value) } break; - case 0xC0: // Copy Rectnagle VRAM->CPU + case 0xC0: // Copy Rectangle VRAM->CPU { if (!HandleCopyRectangleVRAMToCPUCommand()) return; } break; + case 0x80: // Copy Rectangle VRAM->VRAM + { + if (!HandleCopyRectangleVRAMToVRAMCommand()) + return; + } + break; + case 0xE1: // Set draw mode { // 0..10 bits match GPUSTAT @@ -669,6 +679,32 @@ bool GPU::HandleCopyRectangleVRAMToCPUCommand() return true; } +bool GPU::HandleCopyRectangleVRAMToVRAMCommand() +{ + if (m_GP0_command.size() < 4) + return false; + + const u32 src_x = m_GP0_command[1] & UINT32_C(0xFFFF); + const u32 src_y = m_GP0_command[1] >> 16; + const u32 dst_x = m_GP0_command[2] & UINT32_C(0xFFFF); + const u32 dst_y = m_GP0_command[2] >> 16; + const u32 width = m_GP0_command[3] & UINT32_C(0xFFFF); + const u32 height = m_GP0_command[3] >> 16; + + Log_DebugPrintf("Copy rectangle from VRAM to VRAM src=(%u,%u), dst=(%u,%u), size=(%u,%u)", src_x, src_y, dst_x, dst_y, + width, height); + + if ((src_x + width) > VRAM_WIDTH || (src_y + height) > VRAM_HEIGHT || (dst_x + width) > VRAM_WIDTH || + (dst_y + height) > VRAM_HEIGHT) + { + Panic("Out of bounds VRAM copy"); + return true; + } + + CopyVRAM(src_x, src_y, dst_x, dst_y, width, height); + return true; +} + void GPU::UpdateDisplay() { m_texture_config.page_changed = true; @@ -681,6 +717,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) {} +void GPU::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height) {} + void GPU::DispatchRenderCommand(RenderCommand rc, u32 num_vertices) {} void GPU::FlushRender() {} diff --git a/src/pse/gpu.h b/src/pse/gpu.h index b8022f5dc..1d63cbbd0 100644 --- a/src/pse/gpu.h +++ b/src/pse/gpu.h @@ -131,12 +131,14 @@ protected: bool HandleFillRectangleCommand(); bool HandleCopyRectangleCPUToVRAMCommand(); bool HandleCopyRectangleVRAMToCPUCommand(); + bool HandleCopyRectangleVRAMToVRAMCommand(); // Rendering in the backend virtual void UpdateDisplay(); virtual void ReadVRAM(u32 x, u32 y, u32 width, u32 height, void* buffer); virtual void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color); virtual void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data); + virtual void CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height); virtual void DispatchRenderCommand(RenderCommand rc, u32 num_vertices); virtual void FlushRender(); diff --git a/src/pse/gpu_hw_opengl.cpp b/src/pse/gpu_hw_opengl.cpp index bc752d80a..7c5df30bb 100644 --- a/src/pse/gpu_hw_opengl.cpp +++ b/src/pse/gpu_hw_opengl.cpp @@ -300,6 +300,19 @@ void GPU_HW_OpenGL::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* rgba_data.data()); } +void GPU_HW_OpenGL::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height) +{ + glDisable(GL_SCISSOR_TEST); + + // lower-left origin flip + src_y = VRAM_HEIGHT - src_y - height; + dst_y = VRAM_HEIGHT - dst_y - height; + + glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer_fbo_id); + glBlitFramebuffer(src_x, src_y, src_x + width, src_y + height, dst_x, dst_y, dst_x + width, dst_y + height, + GL_COLOR_BUFFER_BIT, GL_NEAREST); +} + void GPU_HW_OpenGL::UpdateTexturePageTexture() { glBindFramebuffer(GL_FRAMEBUFFER, m_texture_page_fbo_id); diff --git a/src/pse/gpu_hw_opengl.h b/src/pse/gpu_hw_opengl.h index 98f5ea7a3..3c5cb6df3 100644 --- a/src/pse/gpu_hw_opengl.h +++ b/src/pse/gpu_hw_opengl.h @@ -21,6 +21,7 @@ protected: void ReadVRAM(u32 x, u32 y, u32 width, u32 height, void* buffer) override; void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color) override; void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data) override; + void CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height) override; void UpdateTexturePageTexture() override; void FlushRender() override;