diff --git a/GPU/Common/FramebufferCommon.cpp b/GPU/Common/FramebufferCommon.cpp index 12065460a1..73a02abce4 100644 --- a/GPU/Common/FramebufferCommon.cpp +++ b/GPU/Common/FramebufferCommon.cpp @@ -915,29 +915,19 @@ void FramebufferManagerCommon::CopyDisplayToOutput(bool reallyDirty) { if (!usePostShader_) { shaderManager_->DirtyLastShader(); - draw_->BindFramebufferAsRenderTarget(nullptr, { Draw::RPAction::CLEAR, Draw::RPAction::CLEAR, Draw::RPAction::CLEAR }); - draw_->BindFramebufferAsTexture(vfb->fbo, 0, Draw::FB_COLOR_BIT, 0); - draw_->SetScissorRect(0, 0, pixelWidth_, pixelHeight_); - Bind2DShader(); - DrawTextureFlags flags = g_Config.iBufFilter == SCALE_LINEAR ? DRAWTEX_LINEAR : DRAWTEX_NEAREST; - flags = flags | DRAWTEX_TO_BACKBUFFER; - // We are doing the DrawActiveTexture call directly to the backbuffer here. Hence, we must - // flip V. - if (needBackBufferYSwap_) - std::swap(v0, v1); - if (cardboardSettings.enabled) { - // Left Eye Image - SetViewport2D(cardboardSettings.leftEyeXPosition, cardboardSettings.screenYPosition, cardboardSettings.screenWidth, cardboardSettings.screenHeight); - DrawActiveTexture(x, y, w, h, (float)pixelWidth_, (float)pixelHeight_, u0, v0, u1, v1, ROTATION_LOCKED_HORIZONTAL, flags | DRAWTEX_KEEP_TEX); - // Right Eye Image - SetViewport2D(cardboardSettings.rightEyeXPosition, cardboardSettings.screenYPosition, cardboardSettings.screenWidth, cardboardSettings.screenHeight); - DrawActiveTexture(x, y, w, h, (float)pixelWidth_, (float)pixelHeight_, u0, v0, u1, v1, ROTATION_LOCKED_HORIZONTAL, flags); - } else { - // Fullscreen Image - SetViewport2D(0, 0, pixelWidth_, pixelHeight_); - DrawActiveTexture(x, y, w, h, (float)pixelWidth_, (float)pixelHeight_, u0, v0, u1, v1, uvRotation, flags); + OutputFlags flags = g_Config.iBufFilter == SCALE_LINEAR ? OutputFlags::LINEAR : OutputFlags::NEAREST; + if (needBackBufferYSwap_) { + flags |= OutputFlags::BACKBUFFER_FLIPPED; } + + // TODO: DrawActiveTexture reverses these, but I'm not sure why? Investigate. + if (GetGPUBackend() == GPUBackend::DIRECT3D9 || GetGPUBackend() == GPUBackend::DIRECT3D11) { + std::swap(v0, v1); + } + + presentation_->SourceFramebuffer(vfb->fbo); + presentation_->CopyToOutput(flags, uvRotation, u0, v0, u1, v1); } else if (usePostShader_ && extraFBOs_.size() == 1 && !postShaderAtOutputResolution_) { // An additional pass, post-processing shader to the extra FBO. shaderManager_->DirtyLastShader(); // dirty lastShader_ @@ -1017,8 +1007,7 @@ void FramebufferManagerCommon::CopyDisplayToOutput(bool reallyDirty) { DrawActiveTexture(x, y, w, h, (float)pixelWidth_, (float)pixelHeight_, u0, v0, u1, v1, uvRotation, flags); } } - } - else if (useBufferedRendering_) { + } else if (useBufferedRendering_) { WARN_LOG(FRAMEBUF, "Current VFB lacks an FBO: %08x", vfb->fb_address); } diff --git a/GPU/Common/PresentationCommon.cpp b/GPU/Common/PresentationCommon.cpp index 54d3dfe1ac..c78bd73d94 100644 --- a/GPU/Common/PresentationCommon.cpp +++ b/GPU/Common/PresentationCommon.cpp @@ -230,9 +230,30 @@ void PresentationCommon::DestroyDeviceObjects() { DoRelease(samplerLinear_); DoRelease(vdata_); DoRelease(idata_); + DoRelease(srcTexture_); + DoRelease(srcFramebuffer_); } -void PresentationCommon::CopyToOutput(OutputFlags flags, float x, float y, float x2, float y2, float u0, float v0, float u1, float v1) { +void PresentationCommon::SourceTexture(Draw::Texture *texture) { + DoRelease(srcTexture_); + DoRelease(srcFramebuffer_); + + texture->AddRef(); + srcTexture_ = texture; +} + +void PresentationCommon::SourceFramebuffer(Draw::Framebuffer *fb) { + DoRelease(srcTexture_); + DoRelease(srcFramebuffer_); + + fb->AddRef(); + srcFramebuffer_ = fb; +} + +void PresentationCommon::CopyToOutput(OutputFlags flags, int uvRotation, float u0, float v0, float u1, float v1) { + CardboardSettings cardboardSettings; + GetCardboardSettings(&cardboardSettings); + Draw::Pipeline *pipeline = flags & OutputFlags::RB_SWIZZLE ? texColorRBSwizzle_ : texColor_; Draw::SamplerState *sampler; @@ -241,25 +262,109 @@ void PresentationCommon::CopyToOutput(OutputFlags flags, float x, float y, float } else { sampler = samplerLinear_; } - draw_->BindSamplerStates(0, 1, &sampler); - const Vertex verts[4] = { - { x, y, 0, u0, v0, 0xFFFFFFFF }, // TL - { x, y2, 0, u0, v1, 0xFFFFFFFF }, // BL - { x2, y2, 0, u1, v1, 0xFFFFFFFF }, // BR - { x2, y, 0, u1, v0, 0xFFFFFFFF }, // TR + // These are the output coordinates. + float x, y, w, h; + CenterDisplayOutputRect(&x, &y, &w, &h, 480.0f, 272.0f, (float)pixelWidth_, (float)pixelHeight_, uvRotation); + + if (GetGPUBackend() == GPUBackend::DIRECT3D9) { + x -= 0.5f; + // TODO: This is plus because of some flipping going on, I think? Investigate... + y += 0.5f; + } + + if (flags & OutputFlags::BACKBUFFER_FLIPPED) { + std::swap(v0, v1); + } + + Vertex verts[4] = { + { x, y, 0, u0, v0, 0xFFFFFFFF }, // TL + { x, y + h, 0, u0, v1, 0xFFFFFFFF }, // BL + { x + w, y + h, 0, u1, v1, 0xFFFFFFFF }, // BR + { x + w, y, 0, u1, v0, 0xFFFFFFFF }, // TR }; + + float invDestW = 1.0f / (pixelWidth_ * 0.5f); + float invDestH = 1.0f / (pixelHeight_ * 0.5f); + for (int i = 0; i < 4; i++) { + verts[i].x = verts[i].x * invDestW - 1.0f; + verts[i].y = verts[i].y * invDestH - 1.0f; + } + + if (uvRotation != ROTATION_LOCKED_HORIZONTAL) { + struct { + float u; + float v; + } temp[4]; + int rotation = 0; + // Vertical and Vertical180 needed swapping after we changed the coordinate system. + switch (uvRotation) { + case ROTATION_LOCKED_HORIZONTAL180: rotation = 2; break; + case ROTATION_LOCKED_VERTICAL: rotation = 1; break; + case ROTATION_LOCKED_VERTICAL180: rotation = 3; break; + } + + // TODO: This doesn't make sense... but Vulkan is flipped only in portrait? Investigate. + if (GetGPUBackend() == GPUBackend::VULKAN && (rotation & 1) != 0) { + rotation ^= 2; + } + + for (int i = 0; i < 4; i++) { + temp[i].u = verts[(i + rotation) & 3].u; + temp[i].v = verts[(i + rotation) & 3].v; + } + for (int i = 0; i < 4; i++) { + verts[i].u = temp[i].u; + verts[i].v = temp[i].v; + } + } + draw_->UpdateBuffer(vdata_, (const uint8_t *)verts, 0, sizeof(verts), Draw::UPDATE_DISCARD); int indexes[] = { 0, 1, 2, 0, 2, 3 }; draw_->UpdateBuffer(idata_, (const uint8_t *)indexes, 0, sizeof(indexes), Draw::UPDATE_DISCARD); + draw_->BindFramebufferAsRenderTarget(nullptr, { Draw::RPAction::CLEAR, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }); + draw_->SetScissorRect(0, 0, pixelWidth_, pixelHeight_); + + if (srcTexture_) { + draw_->BindTexture(0, srcTexture_); + } else if (srcFramebuffer_) { + draw_->BindFramebufferAsTexture(srcFramebuffer_, 0, Draw::FB_COLOR_BIT, 0); + } else { + assert(false); + } + draw_->BindSamplerStates(0, 1, &sampler); + Draw::VsTexColUB ub{}; memcpy(ub.WorldViewProj, g_display_rot_matrix.m, sizeof(float) * 16); + // Make sure Direct3D 11 clears state, since we set shaders outside Draw. + draw_->BindPipeline(nullptr); draw_->BindPipeline(pipeline); draw_->UpdateDynamicUniformBuffer(&ub, sizeof(ub)); draw_->BindVertexBuffers(0, 1, &vdata_, nullptr); draw_->BindIndexBuffer(idata_, 0); - draw_->DrawIndexed(6, 0); + + auto setViewport = [&](float x, float y, float w, float h) { + Draw::Viewport viewport{ x, y, w, h, 0.0f, 1.0f }; + draw_->SetViewports(1, &viewport); + }; + + if (cardboardSettings.enabled) { + // This is what the left eye sees. + setViewport(cardboardSettings.leftEyeXPosition, cardboardSettings.screenYPosition, cardboardSettings.screenWidth, cardboardSettings.screenHeight); + draw_->DrawIndexed(6, 0); + + // And this is the right eye, unless they're a pirate. + setViewport(cardboardSettings.rightEyeXPosition, cardboardSettings.screenYPosition, cardboardSettings.screenWidth, cardboardSettings.screenHeight); + draw_->DrawIndexed(6, 0); + } else { + setViewport(0.0f, 0.0f, (float)pixelWidth_, (float)pixelHeight_); + draw_->DrawIndexed(6, 0); + } + draw_->BindIndexBuffer(nullptr, 0); + + DoRelease(srcFramebuffer_); + DoRelease(srcTexture_); } diff --git a/GPU/Common/PresentationCommon.h b/GPU/Common/PresentationCommon.h index 06463ea72c..4584954d61 100644 --- a/GPU/Common/PresentationCommon.h +++ b/GPU/Common/PresentationCommon.h @@ -37,8 +37,10 @@ void CenterDisplayOutputRect(float *x, float *y, float *w, float *h, float origW namespace Draw { class Buffer; class DrawContext; +class Framebuffer; class Pipeline; class SamplerState; +class Texture; } struct ShaderInfo; @@ -48,6 +50,7 @@ enum class OutputFlags { LINEAR = 0x0000, NEAREST = 0x0001, RB_SWIZZLE = 0x0002, + BACKBUFFER_FLIPPED = 0x0004, }; inline OutputFlags operator | (const OutputFlags &lhs, const OutputFlags &rhs) { @@ -79,7 +82,9 @@ public: void CalculatePostShaderUniforms(int bufferWidth, int bufferHeight, int renderWidth, int renderHeight, bool hasVideo, PostShaderUniforms *uniforms); // TODO: Cleanup - void CopyToOutput(OutputFlags flags, float x, float y, float x2, float y2, float u0, float v0, float u1, float v1); + void SourceTexture(Draw::Texture *texture); + void SourceFramebuffer(Draw::Framebuffer *fb); + void CopyToOutput(OutputFlags flags, int uvRotation, float u0, float v0, float u1, float v1); protected: void CreateDeviceObjects(); @@ -93,6 +98,9 @@ protected: Draw::Buffer *vdata_ = nullptr; Draw::Buffer *idata_ = nullptr; + Draw::Texture *srcTexture_ = nullptr; + Draw::Framebuffer *srcFramebuffer_ = nullptr; + int pixelWidth_ = 0; int pixelHeight_ = 0; bool postShaderAtOutputResolution_ = false; diff --git a/GPU/Software/SoftGpu.cpp b/GPU/Software/SoftGpu.cpp index 6bca7792e5..83b74524e1 100644 --- a/GPU/Software/SoftGpu.cpp +++ b/GPU/Software/SoftGpu.cpp @@ -232,39 +232,13 @@ void SoftGPU::CopyToCurrentFboFromDisplayRam(int srcwidth, int srcheight) { fbTex = draw_->CreateTexture(desc); - float dstwidth = (float)PSP_CoreParameter().pixelWidth; - float dstheight = (float)PSP_CoreParameter().pixelHeight; - - float x, y, w, h; - CenterDisplayOutputRect(&x, &y, &w, &h, 480.0f, 272.0f, dstwidth, dstheight, ROTATION_LOCKED_HORIZONTAL); - - if (GetGPUBackend() == GPUBackend::DIRECT3D9) { - x += 0.5f; - y += 0.5f; - } - - x /= 0.5f * dstwidth; - y /= 0.5f * dstheight; - w /= 0.5f * dstwidth; - h /= 0.5f * dstheight; - float x2 = x + w; - float y2 = y + h; - x -= 1.0f; - y -= 1.0f; - x2 -= 1.0f; - y2 -= 1.0f; - if (GetGPUBackend() == GPUBackend::VULKAN) { std::swap(v0, v1); } - draw_->BindFramebufferAsRenderTarget(nullptr, { Draw::RPAction::CLEAR, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }); - Draw::Viewport viewport = { 0.0f, 0.0f, dstwidth, dstheight, 0.0f, 1.0f }; - draw_->SetViewports(1, &viewport); - draw_->SetScissorRect(0, 0, dstwidth, dstheight); - - draw_->BindTexture(0, fbTex); - presentation_->CopyToOutput(outputFlags, x, y, x2, y2, u0, v0, u1, v1); + presentation_->UpdateSize(PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight); + presentation_->SourceTexture(fbTex); + presentation_->CopyToOutput(outputFlags, ROTATION_LOCKED_HORIZONTAL, u0, v0, u1, v1); } void SoftGPU::CopyDisplayToOutput(bool reallyDirty) { diff --git a/ext/native/thin3d/thin3d_gl.cpp b/ext/native/thin3d/thin3d_gl.cpp index ee06060b98..08a6133448 100644 --- a/ext/native/thin3d/thin3d_gl.cpp +++ b/ext/native/thin3d/thin3d_gl.cpp @@ -1037,10 +1037,12 @@ bool OpenGLPipeline::LinkShaders() { void OpenGLContext::BindPipeline(Pipeline *pipeline) { curPipeline_ = (OpenGLPipeline *)pipeline; - curPipeline_->blend->Apply(&renderManager_); - curPipeline_->depthStencil->Apply(&renderManager_, stencilRef_); - curPipeline_->raster->Apply(&renderManager_); - renderManager_.BindProgram(curPipeline_->program_); + if (curPipeline_) { + curPipeline_->blend->Apply(&renderManager_); + curPipeline_->depthStencil->Apply(&renderManager_, stencilRef_); + curPipeline_->raster->Apply(&renderManager_); + renderManager_.BindProgram(curPipeline_->program_); + } } void OpenGLContext::UpdateDynamicUniformBuffer(const void *ub, size_t size) {