diff --git a/GPU/GLES/Framebuffer.cpp b/GPU/GLES/Framebuffer.cpp index 9cabd91c87..9ee84bc33a 100644 --- a/GPU/GLES/Framebuffer.cpp +++ b/GPU/GLES/Framebuffer.cpp @@ -406,6 +406,12 @@ void FramebufferManager::SetRenderFrameBuffer() { float renderWidthFactor = (float)PSP_CoreParameter().renderWidth / 480.0f; float renderHeightFactor = (float)PSP_CoreParameter().renderHeight / 272.0f; + // Save current render framebuffer to memory + if(currentRenderVfb_) { + // TODO: Add a way to disable the call since it is rather expensive. + ReadFramebufferToMemory(currentRenderVfb_); + } + // None found? Create one. if (!vfb) { gstate_c.textureChanged = true; @@ -481,7 +487,7 @@ void FramebufferManager::SetRenderFrameBuffer() { // We already have it! } else if (vfb != currentRenderVfb_) { // Use it as a render target. - DEBUG_LOG(HLE, "Switching render target to FBO for %08x", vfb->fb_address); + DEBUG_LOG(HLE, "Switching render target to FBO for %08x: %i x %i x %i ", vfb->fb_address, vfb->width, vfb->height, vfb->format); vfb->usageFlags |= FB_USAGE_RENDERTARGET; gstate_c.textureChanged = true; vfb->last_frame_used = gpuStats.numFrames; @@ -598,6 +604,9 @@ void FramebufferManager::CopyDisplayToOutput() { glBindTexture(GL_TEXTURE_2D, 0); } + // TODO: Add a way to disable the call since it is rather expensive. + ReadFramebufferToMemory(vfb); + if (resized_) { glstate.depthWrite.set(GL_TRUE); glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); @@ -606,6 +615,238 @@ void FramebufferManager::CopyDisplayToOutput() { } } +void FramebufferManager::ReadFramebufferToMemory(VirtualFramebuffer *vfb) { + // This only works with buffered rendering + if (!useBufferedRendering_) { + return; + } + + fbo_unbind(); + if(gl_extensions.FBO_ARB) { // TODO: fbo_unbind should use GL_FRAMEBUFFER to do this? Don't want to change native + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } + if(vfb) { + float renderWidthFactor = (float)PSP_CoreParameter().renderWidth / 480.0f; + float renderHeightFactor = (float)PSP_CoreParameter().renderHeight / 272.0f; + + // If render resolution different we blit a new framebuffer and copy that one to memory + // (assuming rendering a smaller resolution than the PSP isn't done on any device, but not sure on that) + // A more accurate (and probably costly) solution would be to draw a framebuffer at native resolution + // parallel to the rendering one? + if(renderWidthFactor > 1.0f || renderHeightFactor > 1.0f) { + // For now we'll also keep these framebuffer objects on the same struct as the ones that can get displayed + // (and blatantly copy work already done above while at it) + VirtualFramebuffer *nvfb = 0; + + // We maintain a separate vector of framebuffer objects for blitting (guessing the point is saving on FBOs?) + for (size_t i = 0; i < bvfbs_.size(); ++i) { + VirtualFramebuffer *v = bvfbs_[i]; + if (MaskedEqual(v->fb_address, vfb->fb_address) && v->format == vfb->format) { + if (v->bufferWidth == vfb->bufferWidth && v->bufferHeight == vfb->bufferHeight) { + nvfb = v; + v->fb_stride = vfb->fb_stride; + v->width = vfb->width; + v->height = vfb->height; + break; + } + } + } + + // Create a new fbo if none was found for the size + if(!nvfb) { + nvfb = new VirtualFramebuffer(); + nvfb->fbo = 0; + nvfb->fb_address = vfb->fb_address; + nvfb->fb_stride = vfb->fb_stride; + nvfb->z_address = vfb->z_address; + nvfb->z_stride = vfb->z_stride; + nvfb->width = vfb->width; + nvfb->height = vfb->height; + nvfb->renderWidth = vfb->width; + nvfb->renderHeight = vfb->height; + nvfb->bufferWidth = vfb->bufferWidth; + nvfb->bufferHeight = vfb->bufferHeight; + nvfb->format = vfb->format; + nvfb->usageFlags = FB_USAGE_RENDERTARGET; + nvfb->dirtyAfterDisplay = true; + + if (g_Config.bTrueColor) { + nvfb->colorDepth = FBO_8888; + } else { + switch (vfb->format) { + case GE_FORMAT_4444: + nvfb->colorDepth = FBO_4444; + break; + case GE_FORMAT_5551: + nvfb->colorDepth = FBO_5551; + break; + case GE_FORMAT_565: + nvfb->colorDepth = FBO_565; + break; + case GE_FORMAT_8888: + nvfb->colorDepth = FBO_8888; + break; + default: + nvfb->colorDepth = FBO_8888; + break; + } + } + + //#ifdef ANDROID + // nvfb->colorDepth = FBO_8888; + //#endif + + nvfb->fbo = fbo_create(nvfb->width, nvfb->height, 1, true, nvfb->colorDepth); + if (useBufferedRendering_) { + if (nvfb->fbo) { + fbo_bind_as_render_target(nvfb->fbo); + } else { + ERROR_LOG(HLE, "Error creating FBO! %i x %i", vfb->renderWidth, vfb->renderHeight); + } + } + + nvfb->last_frame_used = gpuStats.numFrames; + bvfbs_.push_back(nvfb); + } else { + // We already have one, so we set it as a render target. + //DEBUG_LOG(HLE, "Switching render target to FBO for %08x: %i x %i x %i ", nvfb->fb_address, nvfb->width, nvfb->height, nvfb->format); + nvfb->usageFlags |= FB_USAGE_RENDERTARGET; + nvfb->last_frame_used = gpuStats.numFrames; + nvfb->dirtyAfterDisplay = true; + + if (useBufferedRendering_) { + if (nvfb->fbo) { + fbo_bind_as_render_target(nvfb->fbo); + } else { + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } + } + } +#ifdef USING_GLES2 + // Some tiled mobile GPUs benefit IMMENSELY from clearing an FBO before rendering + // to it. This broke stuff before, so now it only clears on the first use of an + // FBO in a frame. This means that some games won't be able to avoid the on-some-GPUs + // performance-crushing framebuffer reloads from RAM, but we'll have to live with that. + if (nvfb->last_frame_used != gpuStats.numFrames) { + glstate.depthWrite.set(GL_TRUE); + glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glClearColor(0,0,0,1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + } +#endif + } + + // We bind the resized fbo for reading + if (useBufferedRendering_) { + if (vfb->fbo) { + fbo_bind_for_read(vfb->fbo); + } else { + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } + } + } + + // And we check both framebuffers for completeness + if(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE + || glCheckFramebufferStatus(GL_READ_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + DEBUG_LOG(HLE, "Incomplete FBOs pre-blitting"); + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } + return; + } + + // TODO: glReadBuffer and glDrawBuffer should maybe be specifically set here? + + // Then we blit the color buffer using linear filtering + DEBUG_LOG(HLE, "Blitting FBOs for %08x: %i x %i to %i x %i ", nvfb->fb_address, vfb->renderWidth, vfb->renderHeight, nvfb->renderWidth, nvfb->renderHeight); + glBlitFramebuffer(0, 0, vfb->fb_stride * renderWidthFactor, vfb->renderHeight, 0, 0, nvfb->fb_stride, nvfb->height, GL_COLOR_BUFFER_BIT, GL_LINEAR); + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } + vfb = nvfb; + } + + int pixelType, pixelSize, pixelFormat, align; + switch (vfb->format) { + case GE_FORMAT_4444: // 16 bit ABGR + pixelType = GL_UNSIGNED_SHORT_4_4_4_4_REV; + pixelFormat = GL_RGBA; + pixelSize = 2; + align = 8; + break; + case GE_FORMAT_5551: // 16 bit ABGR + pixelType = GL_UNSIGNED_SHORT_1_5_5_5_REV; + pixelFormat = GL_RGBA; + pixelSize = 2; + align = 8; + break; + case GE_FORMAT_565: // 16 bit BGR + pixelType = GL_UNSIGNED_SHORT_5_6_5_REV; + pixelFormat = GL_RGB; + pixelSize = 2; + align = 8; + break; + case GE_FORMAT_8888: // 32 bit ABGR + default: // And same as above + pixelType = GL_UNSIGNED_INT_8_8_8_8_REV; + pixelFormat = GL_RGBA; + pixelSize = 4; + align = 4; + break; + } + + if (useBufferedRendering_) { + if (vfb->fbo) { + fbo_bind_for_read(vfb->fbo); + } else { + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } + return; + } + } + + // Prepare buffers to read the pixel data into + // Ideally we'd apply the image flip inplace and only need one, + // but right now this is simpler + // (maybe it's more efficient to have the GPU flip it in the framebuffer and then flip it back?) + int bufHeight = vfb->height; + size_t bufSize = vfb->fb_stride * bufHeight; + GLubyte *buf1 = (GLubyte *) malloc(bufSize * pixelSize); + GLubyte *buf2 = (GLubyte *) malloc(bufSize * pixelSize); + + u32 fb_address = (0x44000000) | vfb->fb_address; + + DEBUG_LOG(HLE, "Reading pixels to mem, bufSize = %u, buf = %08x, fb_address = %08x", bufSize, buf1, fb_address); + glPixelStorei(GL_PACK_ALIGNMENT, align); + glReadPixels(0, 0, vfb->fb_stride, vfb->height, pixelFormat, pixelType, buf1); + + // We have to flip glReadPixels data upside down + int i, j, t; + for(i = 0; i < bufHeight; i++) { + for(j = 0; j < vfb->fb_stride * pixelSize; j++) { + buf2[(bufHeight - 1 - i) * vfb->fb_stride * pixelSize + j] = buf1[i * vfb->fb_stride * pixelSize + j]; + } + } + + Memory::Memcpy(fb_address, buf2, bufSize * pixelSize); + free(buf1); + free(buf2); + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } + } +} + void FramebufferManager::EndFrame() { if (resized_) { DestroyAllFBOs(); diff --git a/GPU/GLES/Framebuffer.h b/GPU/GLES/Framebuffer.h index 8142e1949d..40dbba2f76 100644 --- a/GPU/GLES/Framebuffer.h +++ b/GPU/GLES/Framebuffer.h @@ -105,6 +105,8 @@ public: void SetRenderFrameBuffer(); // Uses parameters computed from gstate void UpdateFromMemory(u32 addr, int size); + void ReadFramebufferToMemory(VirtualFramebuffer *vfb); + // TODO: Break out into some form of FBO manager VirtualFramebuffer *GetDisplayFBO(); void SetDisplayFramebuffer(u32 framebuf, u32 stride, int format); @@ -141,6 +143,9 @@ private: VirtualFramebuffer *currentRenderVfb_; + // Used by ReadFramebufferToMemory + std::vector bvfbs_; // blitting FBOs + // Used by DrawPixels unsigned int drawPixelsTex_; int drawPixelsTexFormat_;