diff --git a/src/pse/gpu.cpp b/src/pse/gpu.cpp index 61613d73f..179d7fe16 100644 --- a/src/pse/gpu.cpp +++ b/src/pse/gpu.cpp @@ -103,7 +103,7 @@ u32 GPU::ReadGPUREAD() void GPU::WriteGP0(u32 value) { m_GP0_command.push_back(value); - Assert(m_GP0_command.size() <= 128); + Assert(m_GP0_command.size() <= 1048576); const u8 command = Truncate8(m_GP0_command[0] >> 24); const u32 param = m_GP0_command[0] & UINT32_C(0x00FFFFFF); @@ -121,6 +121,13 @@ void GPU::WriteGP0(u32 value) case 0x00: // NOP break; + case 0xA0: // Copy Rectangle CPU->VRAM + { + if (!HandleCopyRectangleCPUToVRAMCommand()) + return; + } + break; + case 0xE1: // Set draw mode { // 0..10 bits match GPUSTAT @@ -288,6 +295,36 @@ bool GPU::HandleRenderCommand() return true; } +bool GPU::HandleCopyRectangleCPUToVRAMCommand() +{ + if (m_GP0_command.size() < 3) + return false; + + const u32 copy_width = m_GP0_command[2] & UINT32_C(0xFFFF); + const u32 copy_height = m_GP0_command[2] >> 16; + const u32 num_pixels = copy_width * copy_height; + const u32 num_words = 3 + ((num_pixels + 1) / 2); + if (m_GP0_command.size() < num_words) + return false; + + const u32 dst_x = m_GP0_command[1] & UINT32_C(0xFFFF); + const u32 dst_y = m_GP0_command[1] >> 16; + + Log_DebugPrintf("Copy rectangle from CPU to VRAM offset=(%u,%u), size=(%u,%u)", dst_x, dst_y, copy_width, + copy_height); + + if ((dst_x + copy_width) > VRAM_WIDTH || (dst_y + copy_height) > VRAM_HEIGHT) + { + Panic("Out of bounds VRAM copy"); + return true; + } + + UpdateVRAM(dst_x, dst_y, copy_width, copy_height, &m_GP0_command[3]); + return true; +} + +void GPU::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data) {} + void GPU::DispatchRenderCommand(RenderCommand rc, u32 num_vertices) {} void GPU::FlushRender() {} \ No newline at end of file diff --git a/src/pse/gpu.h b/src/pse/gpu.h index 252d91278..385d4994b 100644 --- a/src/pse/gpu.h +++ b/src/pse/gpu.h @@ -97,8 +97,10 @@ protected: // Rendering commands, returns false if not enough data is provided bool HandleRenderCommand(); + bool HandleCopyRectangleCPUToVRAMCommand(); // Rendering in the backend + virtual void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data); virtual void DispatchRenderCommand(RenderCommand rc, u32 num_vertices); virtual void FlushRender(); diff --git a/src/pse/gpu_hw.cpp b/src/pse/gpu_hw.cpp index efb2c7b2c..132aeba21 100644 --- a/src/pse/gpu_hw.cpp +++ b/src/pse/gpu_hw.cpp @@ -85,7 +85,7 @@ void main() { // 0..+1023 -> -1..1 float pos_x = (float(a_position.x) / 511.5) - 1.0; - float pos_y = (float(a_position.y) / 255.5) + 1.0; + float pos_y = (float(a_position.y) / -255.5) + 1.0; gl_Position = vec4(pos_x, pos_y, 0.0, 1.0); v_color = a_color; diff --git a/src/pse/gpu_hw_opengl.cpp b/src/pse/gpu_hw_opengl.cpp index d6f5b20f3..fe7994efd 100644 --- a/src/pse/gpu_hw_opengl.cpp +++ b/src/pse/gpu_hw_opengl.cpp @@ -51,7 +51,7 @@ void GPU_HW_OpenGL::ClearFramebuffer() glClear(GL_COLOR_BUFFER_BIT); glBindFramebuffer(GL_FRAMEBUFFER, 0); - m_system->GetHostInterface()->SetDisplayTexture(m_framebuffer_texture.get(), 0, 0, VRAM_WIDTH, VRAM_HEIGHT); + //m_system->GetHostInterface()->SetDisplayTexture(m_framebuffer_texture.get(), 0, 0, VRAM_WIDTH, VRAM_HEIGHT); } void GPU_HW_OpenGL::DestroyFramebuffer() @@ -128,6 +128,45 @@ void GPU_HW_OpenGL::SetViewport() void GPU_HW_OpenGL::SetScissor() {} +inline u32 ConvertRGBA5551ToRGBA8888(u16 color) +{ + u8 r = Truncate8(color & 31); + u8 g = Truncate8((color >> 5) & 31); + u8 b = Truncate8((color >> 10) & 31); + u8 a = Truncate8((color >> 15) & 1); + + // 00012345 -> 1234545 + b = (b << 3) | (b >> 3); + g = (g << 3) | (g >> 3); + r = (r << 3) | (r >> 3); + a = a ? 255 : 0; + + return ZeroExtend32(r) | (ZeroExtend32(g) << 8) | (ZeroExtend32(b) << 16) | (ZeroExtend32(a) << 24); +} + +void GPU_HW_OpenGL::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data) +{ + const u32 pixel_count = width * height; + std::vector rgba_data; + rgba_data.reserve(pixel_count); + + const u8* source_ptr = static_cast(data); + for (u32 i = 0; i < pixel_count; i++) + { + u16 src_col; + std::memcpy(&src_col, source_ptr, sizeof(src_col)); + source_ptr += sizeof(src_col); + + const u32 dst_col = ConvertRGBA5551ToRGBA8888(src_col); + rgba_data.push_back(dst_col); + } + + m_framebuffer_texture->Bind(); + glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, + rgba_data.data()); + m_system->GetHostInterface()->SetDisplayTexture(m_framebuffer_texture.get(), 0, 0, VRAM_WIDTH, VRAM_HEIGHT); +} + void GPU_HW_OpenGL::DispatchRenderCommand(RenderCommand rc, u32 num_vertices) { LoadVertices(rc, num_vertices); diff --git a/src/pse/gpu_hw_opengl.h b/src/pse/gpu_hw_opengl.h index cdb89403b..090f634c5 100644 --- a/src/pse/gpu_hw_opengl.h +++ b/src/pse/gpu_hw_opengl.h @@ -16,8 +16,9 @@ public: void Reset() override; protected: - virtual void DispatchRenderCommand(RenderCommand rc, u32 num_vertices) override; - virtual void FlushRender() override; + void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data) override; + void DispatchRenderCommand(RenderCommand rc, u32 num_vertices) override; + void FlushRender() override; private: void CreateFramebuffer();