From 4fa30ce6ec97fed33a3a1b5f968d3cb0cbecbd3b Mon Sep 17 00:00:00 2001 From: arnastia Date: Tue, 25 Jun 2013 13:50:35 +0100 Subject: [PATCH 1/6] Add support for reading framebuffers to memory. Signed-off-by: arnastia --- GPU/GLES/Framebuffer.cpp | 243 ++++++++++++++++++++++++++++++++++++++- GPU/GLES/Framebuffer.h | 5 + 2 files changed, 247 insertions(+), 1 deletion(-) 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_; From 2e48c897800098fb028ff5cc42d62fa231db2e41 Mon Sep 17 00:00:00 2001 From: arnastia Date: Tue, 25 Jun 2013 13:53:00 +0100 Subject: [PATCH 2/6] Small fix for debugger (GWL_WNDPROC isn't defined for 64bits, wouldn't compile). --- Windows/Debugger/Debugger_MemoryDlg.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Windows/Debugger/Debugger_MemoryDlg.cpp b/Windows/Debugger/Debugger_MemoryDlg.cpp index 63dbc1470e..564cc9f519 100644 --- a/Windows/Debugger/Debugger_MemoryDlg.cpp +++ b/Windows/Debugger/Debugger_MemoryDlg.cpp @@ -51,8 +51,14 @@ CMemoryDlg::CMemoryDlg(HINSTANCE _hInstance, HWND _hParent, DebugInterface *_cpu // subclass the edit box HWND editWnd = GetDlgItem(m_hDlg,IDC_ADDRESS); + // TODO: GWL_WNDPROC isn't defined for 64 bits windows, GWLP_WNDPROC is instead +#if defined( _WIN64 ) + DefAddressEditProc = (WNDPROC)GetWindowLong(editWnd,GWLP_WNDPROC); + SetWindowLong(editWnd,GWLP_WNDPROC,(long)AddressEditProc); +#else DefAddressEditProc = (WNDPROC)GetWindowLong(editWnd,GWL_WNDPROC); SetWindowLong(editWnd,GWL_WNDPROC,(long)AddressEditProc); +#endif AddressEditParentHwnd = m_hDlg; Size(); From b823bb7543700e071e5c817bf3881877a0e6c6e2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 25 Jun 2013 09:51:39 -0400 Subject: [PATCH 3/6] Change PSPPointer function name Valid to IsValid. Improves readability and shows intent better. --- Core/Dialog/PSPOskDialog.cpp | 6 ++--- Core/Dialog/SavedataParam.cpp | 44 +++++++++++++++++------------------ Core/HLE/sceAtrac.cpp | 12 +++++----- Core/HLE/sceIo.cpp | 4 ++-- Core/HLE/sceMpeg.cpp | 2 +- Core/MemMap.h | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Core/Dialog/PSPOskDialog.cpp b/Core/Dialog/PSPOskDialog.cpp index ea1c178ec7..d4fff6045b 100755 --- a/Core/Dialog/PSPOskDialog.cpp +++ b/Core/Dialog/PSPOskDialog.cpp @@ -128,7 +128,7 @@ PSPOskDialog::~PSPOskDialog() { void PSPOskDialog::ConvertUCS2ToUTF8(std::string& _string, const PSPPointer em_address) { - if (!em_address.Valid()) + if (!em_address.IsValid()) { _string = ""; return; @@ -198,7 +198,7 @@ int PSPOskDialog::Init(u32 oskPtr) return SCE_ERROR_UTILITY_INVALID_PARAM_SIZE; } // Also seems to crash. - if (!oskParams->fields.Valid()) + if (!oskParams->fields.IsValid()) { ERROR_LOG_REPORT(HLE, "sceUtilityOskInitStart: invalid field data (%08x)", oskParams->fields.ptr); return -1; @@ -221,7 +221,7 @@ int PSPOskDialog::Init(u32 oskPtr) inputChars = L""; - if (oskParams->fields[0].intext.Valid()) { + if (oskParams->fields[0].intext.IsValid()) { auto src = oskParams->fields[0].intext; int c; while ((c = *src++) != 0) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 0696439b01..a2e8d8dfa0 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -263,7 +263,7 @@ bool SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &save memset(cryptedHash,0,0x10); // Encrypt save. // TODO: Is this the correct difference between MAKEDATA and MAKEDATASECURE? - if (param->dataBuf.Valid() && g_Config.bEncryptSave && secureMode) + if (param->dataBuf.IsValid() && g_Config.bEncryptSave && secureMode) { cryptedSize = param->dataSize; if(cryptedSize == 0 || (SceSize)cryptedSize > param->dataBufSize) @@ -329,7 +329,7 @@ bool SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &save else memset(tmpData, 0, FILE_LIST_TOTAL_SIZE); - if (param->dataBuf.Valid()) + if (param->dataBuf.IsValid()) { char *fName = (char*)tmpData; for(int i = 0; i < FILE_LIST_COUNT_MAX; i++) @@ -370,7 +370,7 @@ bool SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &save WritePSPFile(sfopath, sfoData, (SceSize)sfoSize); delete[] sfoData; - if(param->dataBuf.Valid()) // Can launch save without save data in mode 13 + if(param->dataBuf.IsValid()) // Can launch save without save data in mode 13 { std::string filePath = dirPath+"/"+GetFileName(param); u8 *data_ = 0; @@ -408,26 +408,26 @@ bool SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &save // SAVE ICON0 - if (param->icon0FileData.buf.Valid()) + if (param->icon0FileData.buf.IsValid()) { std::string icon0path = dirPath + "/" + ICON0_FILENAME; WritePSPFile(icon0path, param->icon0FileData.buf, param->icon0FileData.bufSize); } // SAVE ICON1 - if (param->icon1FileData.buf.Valid()) + if (param->icon1FileData.buf.IsValid()) { std::string icon1path = dirPath + "/" + ICON1_FILENAME; WritePSPFile(icon1path, param->icon1FileData.buf, param->icon1FileData.bufSize); } // SAVE PIC1 - if (param->pic1FileData.buf.Valid()) + if (param->pic1FileData.buf.IsValid()) { std::string pic1path = dirPath + "/" + PIC1_FILENAME; WritePSPFile(pic1path, param->pic1FileData.buf, param->pic1FileData.bufSize); } // Save SND - if (param->snd0FileData.buf.Valid()) + if (param->snd0FileData.buf.IsValid()) { std::string snd0path = dirPath + "/" + SND0_FILENAME; WritePSPFile(snd0path, param->snd0FileData.buf, param->snd0FileData.bufSize); @@ -733,7 +733,7 @@ int SavedataParam::GetSizes(SceUtilitySavedataParam *param) int ret = 0; - if (param->msFree.Valid()) + if (param->msFree.IsValid()) { param->msFree->clusterSize = (u32)MemoryStick_SectorSize(); param->msFree->freeClusters = (u32)(MemoryStick_FreeSpace() / MemoryStick_SectorSize()); @@ -742,7 +742,7 @@ int SavedataParam::GetSizes(SceUtilitySavedataParam *param) memset(param->msFree->freeSpaceStr, 0, sizeof(param->msFree->freeSpaceStr)); strncpy(param->msFree->freeSpaceStr, spaceTxt.c_str(), sizeof(param->msFree->freeSpaceStr)); } - if (param->msData.Valid()) + if (param->msData.IsValid()) { const std::string gameName(param->msData->gameName, strnlen(param->msData->gameName, sizeof(param->msData->gameName))); const std::string saveName(param->msData->saveName, strnlen(param->msData->saveName, sizeof(param->msData->saveName))); @@ -767,7 +767,7 @@ int SavedataParam::GetSizes(SceUtilitySavedataParam *param) ret = SCE_UTILITY_SAVEDATA_ERROR_SIZES_NO_DATA; } } - if (param->utilityData.Valid()) + if (param->utilityData.IsValid()) { int total_size = 0; total_size += getSizeNormalized(1); // SFO; @@ -799,14 +799,14 @@ bool SavedataParam::GetList(SceUtilitySavedataParam *param) return false; } - if (param->idList.Valid()) + if (param->idList.IsValid()) { u32 maxFile = param->idList->maxCount; std::vector validDir; std::vector allDir = pspFileSystem.GetDirListing(savePath); - if (param->idList.Valid()) + if (param->idList.IsValid()) { std::string searchString = GetGameName(param)+GetSaveName(param); for (size_t i = 0; i < allDir.size() && validDir.size() < maxFile; i++) @@ -844,23 +844,23 @@ int SavedataParam::GetFilesList(SceUtilitySavedataParam *param) return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_STATUS; } - if (!param->fileList.Valid()) { + if (!param->fileList.IsValid()) { ERROR_LOG_REPORT(HLE, "SavedataParam::GetFilesList(): bad fileList address %08x", param->fileList.ptr); // Should crash. return -1; } auto &fileList = param->fileList; - if (fileList->secureEntries.Valid() && fileList->maxSecureEntries > 99) { + if (fileList->secureEntries.IsValid() && fileList->maxSecureEntries > 99) { ERROR_LOG_REPORT(HLE, "SavedataParam::GetFilesList(): too many secure entries, %d", fileList->maxSecureEntries); return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS; } - if (fileList->normalEntries.Valid() && fileList->maxNormalEntries > 8192) { + if (fileList->normalEntries.IsValid() && fileList->maxNormalEntries > 8192) { ERROR_LOG_REPORT(HLE, "SavedataParam::GetFilesList(): too many normal entries, %d", fileList->maxNormalEntries); return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS; } // TODO: This may depend on sdk version or something? Not returned by default. - if (false && fileList->systemEntries.Valid() && fileList->maxSystemEntries > 5) { + if (false && fileList->systemEntries.IsValid() && fileList->maxSystemEntries > 5) { ERROR_LOG_REPORT(HLE, "SavedataParam::GetFilesList(): too many system entries, %d", fileList->maxSystemEntries); return SCE_UTILITY_SAVEDATA_ERROR_RW_BAD_PARAMS; } @@ -927,11 +927,11 @@ int SavedataParam::GetFilesList(SceUtilitySavedataParam *param) SceUtilitySavedataFileListEntry *entry = NULL; int sizeOffset = 0; if (isSystemFile) { - if (fileList->systemEntries.Valid() && fileList->resultNumSystemEntries < fileList->maxSystemEntries) { + if (fileList->systemEntries.IsValid() && fileList->resultNumSystemEntries < fileList->maxSystemEntries) { entry = &fileList->systemEntries[fileList->resultNumSystemEntries++]; } } else if (secureFilenames.find(file->name) != secureFilenames.end()) { - if (fileList->secureEntries.Valid() && fileList->resultNumSecureEntries < fileList->maxSecureEntries) { + if (fileList->secureEntries.IsValid() && fileList->resultNumSecureEntries < fileList->maxSecureEntries) { entry = &fileList->secureEntries[fileList->resultNumSecureEntries++]; } // Secure files are slightly bigger. @@ -940,7 +940,7 @@ int SavedataParam::GetFilesList(SceUtilitySavedataParam *param) sizeOffset = -0x10; } } else { - if (fileList->normalEntries.Valid() && fileList->resultNumNormalEntries < fileList->maxNormalEntries) { + if (fileList->normalEntries.IsValid() && fileList->resultNumNormalEntries < fileList->maxNormalEntries) { entry = &fileList->normalEntries[fileList->resultNumNormalEntries++]; } } @@ -976,7 +976,7 @@ bool SavedataParam::GetSize(SceUtilitySavedataParam *param) PSPFileInfo info = pspFileSystem.GetFileInfo(saveDir); bool exists = info.exists; - if (param->sizeInfo.Valid()) + if (param->sizeInfo.IsValid()) { // TODO: Read the entries and count up the size vs. existing size? @@ -1042,7 +1042,7 @@ int SavedataParam::SetPspParam(SceUtilitySavedataParam *param) SceUtilitySavedataSaveName *saveNameListData; bool hasMultipleFileName = false; - if (param->saveNameList.Valid()) + if (param->saveNameList.IsValid()) { Clear(); @@ -1209,7 +1209,7 @@ void SavedataParam::ClearFileInfo(SaveFileInfo &saveInfo, std::string saveName) saveInfo.idx = 0; saveInfo.textureData = 0; - if (GetPspParam()->newData.Valid() && GetPspParam()->newData->buf.Valid()) + if (GetPspParam()->newData.IsValid() && GetPspParam()->newData->buf.IsValid()) { // We have a png to show if (!noSaveIcon) diff --git a/Core/HLE/sceAtrac.cpp b/Core/HLE/sceAtrac.cpp index 2795b80c25..e14da86e33 100644 --- a/Core/HLE/sceAtrac.cpp +++ b/Core/HLE/sceAtrac.cpp @@ -137,7 +137,7 @@ struct Atrac { Atrac3plus_Decoder::CloseContext(&decoder_context); sampleQueue.clear(); - if (atracContext.Valid()) + if (atracContext.IsValid()) kernelMemory.Free(atracContext.ptr); } @@ -513,7 +513,7 @@ u32 _AtracAddStreamData(int atracID, u8 *buf, u32 bytesToAdd) { atrac->first.size = atrac->first.filesize; atrac->first.fileoffset = atrac->first.size; atrac->first.writableBytes = 0; - if (atrac->atracContext.Valid()) { + if (atrac->atracContext.IsValid()) { // refresh atracContext _AtracGenarateContext(atrac, atrac->atracContext); } @@ -670,7 +670,7 @@ u32 _AtracDecodeData(int atracID, u8* outbuf, u32 *SamplesNum, u32* finish, int *finish = finishFlag; *remains = atrac->getRemainFrames(); } - if (atrac->atracContext.Valid()) { + if (atrac->atracContext.IsValid()) { // refresh atracContext _AtracGenarateContext(atrac, atrac->atracContext); } @@ -1456,18 +1456,18 @@ int _sceAtracGetContextAddress(int atracID) ERROR_LOG(HLE, "_sceAtracGetContextAddress(%i): bad atrac id", atracID); return 0; } - if (!atrac->atracContext.Valid()) { + if (!atrac->atracContext.IsValid()) { // allocate a new atracContext u32 contextsize = 256; atrac->atracContext = kernelMemory.Alloc(contextsize, false, "Atrac Context"); - if (atrac->atracContext.Valid()) + if (atrac->atracContext.IsValid()) Memory::Memset(atrac->atracContext.ptr, 0, 256); WARN_LOG(HLE, "%08x=_sceAtracGetContextAddress(%i): allocated new context", atrac->atracContext.ptr, atracID); } else WARN_LOG(HLE, "%08x=_sceAtracGetContextAddress(%i)", atrac->atracContext.ptr, atracID); - if (atrac->atracContext.Valid()) + if (atrac->atracContext.IsValid()) _AtracGenarateContext(atrac, atrac->atracContext); if (atrac->currentSample >= atrac->endSample && atrac->loopNum == 0) { // This is a hack method to release those already finished atrac3 voice. diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 908aeb9198..5fca1d36f1 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -1676,7 +1676,7 @@ u32 sceIoGetFdList(u32 outAddr, int outSize, u32 fdNumAddr) { // Always have the first three. for (int i = 0; i < PSP_MIN_FD; ++i) { // TODO: Technically it seems like these are fixed ids > PSP_COUNT_FDS. - if (count < outSize && out.Valid()) { + if (count < outSize && out.IsValid()) { out[count] = i; } ++count; @@ -1686,7 +1686,7 @@ u32 sceIoGetFdList(u32 outAddr, int outSize, u32 fdNumAddr) { if (fds[i] == 0) { continue; } - if (count < outSize && out.Valid()) { + if (count < outSize && out.IsValid()) { out[count] = i; } ++count; diff --git a/Core/HLE/sceMpeg.cpp b/Core/HLE/sceMpeg.cpp index 0f9d1d7c7e..54c78353c0 100644 --- a/Core/HLE/sceMpeg.cpp +++ b/Core/HLE/sceMpeg.cpp @@ -871,7 +871,7 @@ int sceMpegRingbufferAvailableSize(u32 ringbufferAddr) PSPPointer ringbuffer; ringbuffer = ringbufferAddr; - if (!ringbuffer.Valid()) { + if (!ringbuffer.IsValid()) { ERROR_LOG(HLE, "sceMpegRingbufferAvailableSize(%08x) - bad address", ringbufferAddr); return -1; } diff --git a/Core/MemMap.h b/Core/MemMap.h index 9d06555653..016c95e51b 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -349,7 +349,7 @@ struct PSPPointer return (T *)(Memory::base + ptr); } - bool Valid() const + bool IsValid() const { return Memory::IsValidAddress(ptr); } From b52ee53c98affd65f304eb3637f70ba83256b911 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 25 Jun 2013 09:54:42 -0400 Subject: [PATCH 4/6] Fix some signed/unsigned mismatch warnings in __sceAudio.cpp. --- Core/HLE/__sceAudio.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Core/HLE/__sceAudio.cpp b/Core/HLE/__sceAudio.cpp index 778e80df17..84e7667062 100644 --- a/Core/HLE/__sceAudio.cpp +++ b/Core/HLE/__sceAudio.cpp @@ -290,18 +290,18 @@ void __AudioUpdate() { chans[i].sampleQueue.popPointers(hwBlockSize * 2, &buf1, &sz1, &buf2, &sz2); if (firstChannel) { - for (int s = 0; s < sz1; s++) + for (size_t s = 0; s < sz1; s++) mixBuffer[s] = buf1[s]; if (buf2) { - for (int s = 0; s < sz2; s++) + for (size_t s = 0; s < sz2; s++) mixBuffer[s + sz1] = buf2[s]; } firstChannel = false; } else { - for (int s = 0; s < sz1; s++) + for (size_t s = 0; s < sz1; s++) mixBuffer[s] += buf1[s]; if (buf2) { - for (int s = 0; s < sz2; s++) + for (size_t s = 0; s < sz2; s++) mixBuffer[s + sz1] += buf2[s]; } } @@ -317,10 +317,10 @@ void __AudioUpdate() { s16 *buf1 = 0, *buf2 = 0; size_t sz1, sz2; outAudioQueue.pushPointers(hwBlockSize * 2, &buf1, &sz1, &buf2, &sz2); - for (int s = 0; s < sz1; s++) + for (size_t s = 0; s < sz1; s++) buf1[s] = clamp_s16(mixBuffer[s]); if (buf2) { - for (int s = 0; s < sz2; s++) + for (size_t s = 0; s < sz2; s++) buf2[s] = clamp_s16(mixBuffer[s + sz1]); } } else { From 98b9acf0d3879d9a49f82ff4ff22886033ddcdce Mon Sep 17 00:00:00 2001 From: arnastia Date: Tue, 25 Jun 2013 19:14:10 +0100 Subject: [PATCH 5/6] Add FramebuffersToMem option to allow disabling of reading framebuffer contents to memory (defaults to false). --- Core/Config.cpp | 2 ++ Core/Config.h | 1 + GPU/GLES/Framebuffer.cpp | 60 +++++++++++++++++++++------------------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index 146f20e29f..f004f5b604 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -114,6 +114,7 @@ void Config::Load(const char *iniFileName) #endif graphics->Get("StretchToDisplay", &bStretchToDisplay, false); graphics->Get("TrueColor", &bTrueColor, true); + graphics->Get("FramebuffersToMem", &bFramebuffersToMem, false); graphics->Get("MipMap", &bMipMap, true); graphics->Get("TexScalingLevel", &iTexScalingLevel, 1); graphics->Get("TexScalingType", &iTexScalingType, 0); @@ -216,6 +217,7 @@ void Config::Save() #endif graphics->Set("StretchToDisplay", bStretchToDisplay); graphics->Set("TrueColor", bTrueColor); + graphics->Set("FramebuffersToMem", bFramebuffersToMem); graphics->Set("MipMap", bMipMap); graphics->Set("TexScalingLevel", iTexScalingLevel); graphics->Set("TexScalingType", iTexScalingType); diff --git a/Core/Config.h b/Core/Config.h index 9881c582bc..251d4229a3 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -79,6 +79,7 @@ public: bool bFullScreen; int iAnisotropyLevel; bool bTrueColor; + bool bFramebuffersToMem; bool bMipMap; int iTexScalingLevel; // 1 = off, 2 = 2x, ..., 5 = 5x int iTexScalingType; // 0 = xBRZ, 1 = Hybrid diff --git a/GPU/GLES/Framebuffer.cpp b/GPU/GLES/Framebuffer.cpp index 9ee84bc33a..d936f20204 100644 --- a/GPU/GLES/Framebuffer.cpp +++ b/GPU/GLES/Framebuffer.cpp @@ -408,8 +408,9 @@ void FramebufferManager::SetRenderFrameBuffer() { // Save current render framebuffer to memory if(currentRenderVfb_) { - // TODO: Add a way to disable the call since it is rather expensive. - ReadFramebufferToMemory(currentRenderVfb_); + if(g_Config.bFramebuffersToMem) { + ReadFramebufferToMemory(currentRenderVfb_); + } } // None found? Create one. @@ -604,8 +605,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(g_Config.bFramebuffersToMem) { + ReadFramebufferToMemory(vfb); + } if (resized_) { glstate.depthWrite.set(GL_TRUE); @@ -775,31 +777,31 @@ void FramebufferManager::ReadFramebufferToMemory(VirtualFramebuffer *vfb) { 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; + 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_) { From ad5df976bd184e3a15dcd420df474dfdd5dae61a Mon Sep 17 00:00:00 2001 From: arnastia Date: Fri, 28 Jun 2013 14:48:36 +0100 Subject: [PATCH 6/6] Blit framebuffers using screen aligned quads (buggy); Implement asynchronous framebuffer readback for OpenGL and optional CPU-side pixel format conversion (needs optimization); Add OpenGL ES support (untested). --- Core/Config.cpp | 4 + Core/Config.h | 2 + GPU/GLES/Framebuffer.cpp | 652 +++++++++++++++++++++++++++------------ GPU/GLES/Framebuffer.h | 22 +- 4 files changed, 490 insertions(+), 190 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index f004f5b604..3778e9761c 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -115,6 +115,8 @@ void Config::Load(const char *iniFileName) graphics->Get("StretchToDisplay", &bStretchToDisplay, false); graphics->Get("TrueColor", &bTrueColor, true); graphics->Get("FramebuffersToMem", &bFramebuffersToMem, false); + graphics->Get("AsyncReadback", &bAsyncReadback, true); + graphics->Get("CPUConvert", &bCPUConvert, false); graphics->Get("MipMap", &bMipMap, true); graphics->Get("TexScalingLevel", &iTexScalingLevel, 1); graphics->Get("TexScalingType", &iTexScalingType, 0); @@ -218,6 +220,8 @@ void Config::Save() graphics->Set("StretchToDisplay", bStretchToDisplay); graphics->Set("TrueColor", bTrueColor); graphics->Set("FramebuffersToMem", bFramebuffersToMem); + graphics->Set("AsyncReadback", bAsyncReadback); + graphics->Set("CPUConvert", bCPUConvert); graphics->Set("MipMap", bMipMap); graphics->Set("TexScalingLevel", iTexScalingLevel); graphics->Set("TexScalingType", iTexScalingType); diff --git a/Core/Config.h b/Core/Config.h index 251d4229a3..ab2c402e56 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -80,6 +80,8 @@ public: int iAnisotropyLevel; bool bTrueColor; bool bFramebuffersToMem; + bool bAsyncReadback; // TEMP + bool bCPUConvert; // TEMP bool bMipMap; int iTexScalingLevel; // 1 = off, 2 = 2x, ..., 5 = 5x int iTexScalingType; // 0 = xBRZ, 1 = Hybrid diff --git a/GPU/GLES/Framebuffer.cpp b/GPU/GLES/Framebuffer.cpp index d936f20204..093c5c4545 100644 --- a/GPU/GLES/Framebuffer.cpp +++ b/GPU/GLES/Framebuffer.cpp @@ -57,6 +57,18 @@ static const char basic_vs[] = " gl_Position = u_viewproj * a_position;\n" "}\n"; +static const char blit_fs[] = + "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "uniform sampler2D sampler0;\n" + "varying vec4 v_color;\n" + "varying vec2 v_texcoord0;\n" + "void main() {\n" + " v_color = texture2D(sampler0, v_texcoord0).bgra;\n" + " gl_FragColor = v_color;\n" + "}\n"; + // Aggressively delete unused FBO:s to save gpu memory. enum { FBO_OLD_AGE = 5, @@ -66,6 +78,32 @@ static bool MaskedEqual(u32 addr1, u32 addr2) { return (addr1 & 0x3FFFFFF) == (addr2 & 0x3FFFFFF); } +static u16 RGBA8888toRGB565(u32 px) { + return ((px >> 3) & 0x001F) | ((px >> 5) & 0x07E0) | ((px >> 8) & 0xF800); +} + +static u16 BGRA8888toRGB565(u32 px) { + return ((px >> 19) & 0x001F) | ((px >> 5) & 0x07E0) | ((px << 8) & 0xF800); +} + +static u16 RGBA8888toRGBA4444(u32 px) { + return ((px >> 4) & 0x000F) | ((px >> 8) & 0x00F0) | ((px >> 12) & 0x0F00) | ((px >> 16) & 0xF000); +} + +static u16 BGRA8888toRGBA4444(u32 px) { + return ((px >> 20) & 0x000F) | ((px >> 8) & 0x00F0) | ((px << 4) & 0x0F00) | ((px >> 16) & 0xF000); +} + +static u16 RGBA8888toRGBA1555(u32 px) { + return ((px >> 3) & 0x001F) | ((px >> 6) & 0x03E0) | ((px >> 9) & 0x7C00) | ((px >> 16) & 0x8000); +} + +static u16 BGRA8888toRGBA1555(u32 px) { + return ((px >> 19) & 0x001F) | ((px >> 6) & 0x03E0) | ((px << 7) & 0x7C00) | ((px >> 16) & 0x8000); +} + +void ConvertFromRGBA8888(u8 *dst, u8 *src, u32 stride, u32 height, int format, bool bgra = false); + void CenterRect(float *x, float *y, float *w, float *h, float origW, float origH, float frameW, float frameH) { @@ -116,7 +154,9 @@ FramebufferManager::FramebufferManager() : currentRenderVfb_(0), drawPixelsTex_(0), drawPixelsTexFormat_(-1), - convBuf(0) + convBuf(0), + pixelBufObj_(0), + currentPBO_(0) { draw2dprogram = glsl_create_source(basic_vs, tex_fs); @@ -124,6 +164,12 @@ FramebufferManager::FramebufferManager() : glUniform1i(draw2dprogram->sampler0, 0); glsl_unbind(); + blitprogram = glsl_create_source(basic_vs, blit_fs); + + glsl_bind(blitprogram); + glUniform1i(draw2dprogram->sampler0, 0); + glsl_unbind(); + // And an initial clear. We don't clear per frame as the games are supposed to handle that // by themselves. glstate.depthWrite.set(GL_TRUE); @@ -138,6 +184,9 @@ FramebufferManager::~FramebufferManager() { if (drawPixelsTex_) glDeleteTextures(1, &drawPixelsTex_); glsl_destroy(draw2dprogram); + glsl_destroy(blitprogram); + + delete [] pixelBufObj_; delete [] convBuf; } @@ -243,27 +292,33 @@ void FramebufferManager::DrawPixels(const u8 *framebuf, int pixelFormat, int lin DrawActiveTexture(x, y, w, h, false, 480.0f / 512.0f); } -void FramebufferManager::DrawActiveTexture(float x, float y, float w, float h, bool flip, float uscale) { +void FramebufferManager::DrawActiveTexture(float x, float y, float w, float h, bool flip, float uscale, GLSLProgram *program) { float u2 = uscale; float v1 = flip ? 1.0f : 0.0f; float v2 = flip ? 0.0f : 1.0f; const float pos[12] = {x,y,0, x+w,y,0, x+w,y+h,0, x,y+h,0}; const float texCoords[8] = {0, v1, u2, v1, u2, v2, 0, v2}; + const GLubyte indices[4] = {0,1,3,2}; - glsl_bind(draw2dprogram); + if(!program) { + program = draw2dprogram; + } + + glsl_bind(program); Matrix4x4 ortho; ortho.setOrtho(0, (float)PSP_CoreParameter().pixelWidth, (float)PSP_CoreParameter().pixelHeight, 0, -1, 1); - glUniformMatrix4fv(draw2dprogram->u_viewproj, 1, GL_FALSE, ortho.getReadPtr()); + glUniformMatrix4fv(program->u_viewproj, 1, GL_FALSE, ortho.getReadPtr()); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - glEnableVertexAttribArray(draw2dprogram->a_position); - glEnableVertexAttribArray(draw2dprogram->a_texcoord0); - glVertexAttribPointer(draw2dprogram->a_position, 3, GL_FLOAT, GL_FALSE, 12, pos); - glVertexAttribPointer(draw2dprogram->a_texcoord0, 2, GL_FLOAT, GL_FALSE, 8, texCoords); - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // TODO: TRIANGLE_STRIP is more likely to be optimized. - glDisableVertexAttribArray(draw2dprogram->a_position); - glDisableVertexAttribArray(draw2dprogram->a_texcoord0); + glEnableVertexAttribArray(program->a_position); + glEnableVertexAttribArray(program->a_texcoord0); + glVertexAttribPointer(program->a_position, 3, GL_FLOAT, GL_FALSE, 12, pos); + glVertexAttribPointer(program->a_texcoord0, 2, GL_FLOAT, GL_FALSE, 8, texCoords); + //glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // TODO: TRIANGLE_STRIP is more likely to be optimized. + glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices); // Trying glDrawElements with GL_TRIANGLE_STRIP + glDisableVertexAttribArray(program->a_position); + glDisableVertexAttribArray(program->a_texcoord0); glsl_unbind(); } @@ -587,6 +642,10 @@ void FramebufferManager::CopyDisplayToOutput() { } displayFramebuf_ = vfb; + if(g_Config.bFramebuffersToMem) { + ReadFramebufferToMemory(vfb); + } + if (vfb->fbo) { glstate.viewport.set(0, 0, PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight); DEBUG_LOG(HLE, "Displaying FBO %08x", vfb->fb_address); @@ -605,10 +664,6 @@ void FramebufferManager::CopyDisplayToOutput() { glBindTexture(GL_TEXTURE_2D, 0); } - if(g_Config.bFramebuffersToMem) { - ReadFramebufferToMemory(vfb); - } - if (resized_) { glstate.depthWrite.set(GL_TRUE); glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); @@ -628,182 +683,254 @@ void FramebufferManager::ReadFramebufferToMemory(VirtualFramebuffer *vfb) { glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); } if(vfb) { - float renderWidthFactor = (float)PSP_CoreParameter().renderWidth / 480.0f; - float renderHeightFactor = (float)PSP_CoreParameter().renderHeight / 272.0f; + // We'll pseudo-blit framebuffers here to get a resized and flipped version of vfb. + // For now we'll keep these on the same struct as the ones that can get displayed + // (and blatantly copy work already done above while at it). + VirtualFramebuffer *nvfb = 0; - // 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; - } + // We maintain a separate vector of framebuffer objects for blitting. + 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; + // 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: + 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 (!(nvfb->fbo)) { + ERROR_LOG(HLE, "Error creating FBO! %i x %i", nvfb->renderWidth, nvfb->renderHeight); + } + + if (useBufferedRendering_) { + if (nvfb->fbo) { + fbo_bind_as_render_target(nvfb->fbo); + } else { + fbo_unbind(); + return; + } + } + + nvfb->last_frame_used = gpuStats.numFrames; + bvfbs_.push_back(nvfb); + + glstate.depthWrite.set(GL_TRUE); + glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glClearColor(0.0f,0.0f,0.0f,1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + //glEnable(GL_DITHER); + } else { + 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); +#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 + } else { + fbo_unbind(); + return; + } + } + } + + BlitFramebuffer_(vfb, nvfb, true); + + PackFramebuffer_(nvfb); + } +} + +void FramebufferManager::BlitFramebuffer_(VirtualFramebuffer *src, VirtualFramebuffer *dst, bool flip, float upscale) { + // This only works with buffered rendering + if (!useBufferedRendering_) { + return; + } + + fbo_bind_as_render_target(dst->fbo); + + if(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + ERROR_LOG(HLE, "Incomplete target framebuffer, aborting blit"); + fbo_unbind(); + return; + } + + if(src->format == GE_FORMAT_565) { + // Not sure this should be done + glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + } else { + glstate.colorMask.set(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + } + + glstate.viewport.set(0, 0, dst->width, dst->height); + glstate.depthTest.disable(); + glstate.blend.disable(); + glstate.cullFace.disable(); + glstate.depthTest.disable(); + glstate.scissorTest.disable(); + glstate.stencilTest.disable(); + + fbo_bind_color_as_texture(src->fbo, 0); + + float x, y, w, h; + CenterRect(&x, &y, &w, &h, 480.0f, 272.0f, (float)PSP_CoreParameter().pixelWidth, (float)PSP_CoreParameter().pixelHeight); + +#ifdef USING_GLES2 + DrawActiveTexture(x, y, w, h, !flip, upscale, draw2dprogram); +#else + if(!g_Config.bAsyncReadback || (g_Config.bCPUConvert && src->format != GE_FORMAT_8888)) { + DrawActiveTexture(x, y, w, h, !flip, upscale, draw2dprogram); + } else { + DrawActiveTexture(x, y, w, h, !flip, upscale, blitprogram); + } +#endif + + glBindTexture(GL_TEXTURE_2D, 0); + fbo_unbind(); +} + +void FramebufferManager::PackFramebuffer_(VirtualFramebuffer *vfb) { +#ifdef USING_GLES2 + PackFramebufferGLES_(vfb); // synchronous glReadPixels +#else + if(g_Config.bAsyncReadback) { + PackFramebufferGL_(vfb); // asynchronous glReadPixels using PBOs + } else { + PackFramebufferGLES_(vfb); // synchronous glReadPixels + } +#endif +} + +void ConvertFromRGBA8888(u8 *dst, u8 *src, u32 stride, u32 height, int format, bool bgra) { + if(format == GE_FORMAT_8888) { + return; + } else { + for (int j = 0; j < height; j++) { + const u32 *src32 = (u32 *)(src + stride * j*4); + u16 *dst16 = (u16 *)(dst + stride * j*2); + switch (format) { + case GE_FORMAT_565: // BGR 565 + for(int i = 0; i < stride; i++) { + u32 px = *(src32 + i); + *(dst16+i) = (bgra ? BGRA8888toRGB565(px) : RGBA8888toRGB565(px)); + } + break; + case GE_FORMAT_5551: // ABGR 1555 + for(int i = 0; i < stride; i++) { + u32 px = *(src32 + i); + *(dst16+i) = (bgra ? BGRA8888toRGBA1555(px) : RGBA8888toRGBA1555(px)); + } + + break; + case GE_FORMAT_4444: // ABGR 4444 + for(int i = 0; i < stride; i++) { + u32 px = *(src32 + i); + *(dst16+i) = (bgra ? BGRA8888toRGBA4444(px) : RGBA8888toRGBA4444(px)); + } + break; + default: + break; + } + } + } +} + +void FramebufferManager::PackFramebufferGL_(VirtualFramebuffer *vfb) { + GLubyte *packed = 0; + + // Order packing/readback of the framebuffer + if(vfb) { + int pixelType, pixelFormat, pixelSize, align; + switch (vfb->format) { case GE_FORMAT_4444: // 16 bit ABGR pixelType = GL_UNSIGNED_SHORT_4_4_4_4_REV; - pixelFormat = GL_RGBA; + pixelFormat = GL_BGRA; pixelSize = 2; align = 8; break; case GE_FORMAT_5551: // 16 bit ABGR pixelType = GL_UNSIGNED_SHORT_1_5_5_5_REV; - pixelFormat = GL_RGBA; + pixelFormat = GL_BGRA; pixelSize = 2; align = 8; break; case GE_FORMAT_565: // 16 bit BGR - pixelType = GL_UNSIGNED_SHORT_5_6_5_REV; - pixelFormat = GL_RGB; + pixelType = GL_UNSIGNED_SHORT_5_6_5; + pixelFormat = GL_BGR; 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; + default: + pixelType = GL_UNSIGNED_BYTE; + pixelFormat = GL_BGRA; pixelSize = 4; align = 4; break; } + size_t bufSize = vfb->fb_stride * vfb->height * pixelSize; + u32 fb_address = (0x44000000) | vfb->fb_address; + if (useBufferedRendering_) { if (vfb->fbo) { fbo_bind_for_read(vfb->fbo); @@ -816,37 +943,170 @@ void FramebufferManager::ReadFramebufferToMemory(VirtualFramebuffer *vfb) { } } - // 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); + if(!pixelBufObj_) { + GLuint pbos[2]; - u32 fb_address = (0x44000000) | vfb->fb_address; + glGenBuffers(2, pbos); - 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]; + pixelBufObj_ = new AsyncPBO[2]; + + pixelBufObj_[0].handle = pbos[0]; + pixelBufObj_[0].maxSize = 0; + pixelBufObj_[0].reading = false; + + pixelBufObj_[1].handle = pbos[1]; + pixelBufObj_[1].maxSize = 0; + pixelBufObj_[1].reading = false; + + currentPBO_ = 0; + } + + glBindBuffer(GL_PIXEL_PACK_BUFFER, pixelBufObj_[currentPBO_].handle); + + if(pixelBufObj_[currentPBO_].maxSize < bufSize) { + if(g_Config.bCPUConvert && pixelType != GL_UNSIGNED_BYTE) { + glBufferData(GL_PIXEL_PACK_BUFFER, bufSize*2, NULL, GL_DYNAMIC_READ); + } else { + glBufferData(GL_PIXEL_PACK_BUFFER, bufSize, NULL, GL_DYNAMIC_READ); } + pixelBufObj_[currentPBO_].maxSize = bufSize; } - Memory::Memcpy(fb_address, buf2, bufSize * pixelSize); - free(buf1); - free(buf2); - fbo_unbind(); - if(gl_extensions.FBO_ARB) { - glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + pixelBufObj_[currentPBO_].fb_address = fb_address; + pixelBufObj_[currentPBO_].size = bufSize; + pixelBufObj_[currentPBO_].stride = vfb->fb_stride; + pixelBufObj_[currentPBO_].height = vfb->height; + pixelBufObj_[currentPBO_].format = vfb->format; + pixelBufObj_[currentPBO_].reading = true; + + if(g_Config.bCPUConvert) { + glPixelStorei(GL_PACK_ALIGNMENT, 4); + glReadPixels(0, 0, vfb->fb_stride, vfb->height, GL_BGRA, GL_UNSIGNED_BYTE, 0); + } else { + glPixelStorei(GL_PACK_ALIGNMENT, align); + glReadPixels(0, 0, vfb->fb_stride, vfb->height, pixelFormat, pixelType, 0); } } + + // Receive data from previous framebuffer + u8 nextPBO = (currentPBO_ + 1) % 2; + if(pixelBufObj_[nextPBO].reading) { + glBindBuffer(GL_PIXEL_PACK_BUFFER, pixelBufObj_[nextPBO].handle); + packed = (GLubyte *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY); + + if(packed) { + DEBUG_LOG(HLE, "Reading pbo to mem, bufSize = %u, packed = %08x, fb_address = %08x, pbo = %u", + pixelBufObj_[nextPBO].size, packed, pixelBufObj_[nextPBO].fb_address, nextPBO); + + if(g_Config.bCPUConvert) { + switch(pixelBufObj_[nextPBO].format) { + case GE_FORMAT_565: + case GE_FORMAT_5551: + case GE_FORMAT_4444: { + u8 *processed = (u8 *)malloc(pixelBufObj_[nextPBO].size); + + if(processed) { + ConvertFromRGBA8888(processed, packed, + pixelBufObj_[nextPBO].stride, pixelBufObj_[nextPBO].height, + pixelBufObj_[nextPBO].format, true); + + Memory::Memcpy(pixelBufObj_[nextPBO].fb_address, processed, pixelBufObj_[nextPBO].size); + + free(processed); + } + } + break; + default: + Memory::Memcpy(pixelBufObj_[nextPBO].fb_address, packed, pixelBufObj_[nextPBO].size); + } + } else { // we don't need to convert + Memory::Memcpy(pixelBufObj_[nextPBO].fb_address, packed, pixelBufObj_[nextPBO].size); + } + glUnmapBuffer(GL_PIXEL_PACK_BUFFER); + + pixelBufObj_[nextPBO].reading = false; + } + } + + currentPBO_ = nextPBO; + + glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } +} + +void FramebufferManager::PackFramebufferGLES_(VirtualFramebuffer *vfb) { + // Pixel size is always 4 here because data will come in RGBA8888 + size_t bufSize = vfb->fb_stride * vfb->height * 4; // pixel size always 4 here + + u8 align = (vfb->format != GE_FORMAT_8888) ? 8 : 4; + + u32 fb_address = (0x44000000) | vfb->fb_address; + + 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; + } + } + + GLubyte *packed = (GLubyte *)malloc(bufSize * sizeof(GLubyte)); + + if(packed) { + DEBUG_LOG(HLE, "Reading framebuffer to mem, bufSize = %u, packed = %08x, fb_address = %08x", + bufSize, packed, fb_address); + + glPixelStorei(GL_PACK_ALIGNMENT, 4); + glReadPixels(0, 0, vfb->fb_stride, vfb->height, GL_RGBA, GL_UNSIGNED_BYTE, packed); + GLenum error = glGetError(); + switch(error) { + case 0: + break; + case GL_INVALID_ENUM: + ERROR_LOG(HLE, "glReadPixels: GL_INVALID_ENUM"); + break; + case GL_INVALID_VALUE: + ERROR_LOG(HLE, "glReadPixels: GL_INVALID_VALUE"); + break; + case GL_INVALID_OPERATION: + ERROR_LOG(HLE, "glReadPixels: GL_INVALID_OPERATION"); + break; + case GL_INVALID_FRAMEBUFFER_OPERATION: + ERROR_LOG(HLE, "glReadPixels: GL_INVALID_FRAMEBUFFER_OPERATION"); + break; + default: + ERROR_LOG(HLE, "glReadPixels: UNKNOWN OPENGL ERROR %u", error); + break; + } + + if(vfb->format != GE_FORMAT_8888) { // if not RGBA 8888 we need to convert + u8 *processed = (u8 *)malloc(vfb->fb_stride * vfb->height * 2); + + if(processed) { + ConvertFromRGBA8888(processed, packed, vfb->fb_stride, vfb->height, vfb->format); + + Memory::Memcpy(fb_address, processed, vfb->fb_stride * vfb->height * 2); + + free(processed); + } + } else { + Memory::Memcpy(fb_address, packed, bufSize); + } + + free(packed); + } + + fbo_unbind(); + if(gl_extensions.FBO_ARB) { + glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); + } } void FramebufferManager::EndFrame() { @@ -929,6 +1189,20 @@ void FramebufferManager::DecimateFBOs() { vfbs_.erase(vfbs_.begin() + i--); } } + + // Do the same for ReadFramebuffersToMemory's VFBs + for (size_t i = 0; i < bvfbs_.size(); ++i) { + VirtualFramebuffer *vfb = bvfbs_[i]; + if (vfb == displayFramebuf_ || vfb == prevDisplayFramebuf_ || vfb == prevPrevDisplayFramebuf_) { + continue; + } + int age = frameLastFramebufUsed - vfb->last_frame_used; + if (age > FBO_OLD_AGE) { + INFO_LOG(HLE, "Decimating FBO for %08x (%i x %i x %i), age %i", vfb->fb_address, vfb->width, vfb->height, vfb->format, age) + DestroyFramebuf(vfb); + bvfbs_.erase(bvfbs_.begin() + i--); + } + } } void FramebufferManager::DestroyAllFBOs() { diff --git a/GPU/GLES/Framebuffer.h b/GPU/GLES/Framebuffer.h index 40dbba2f76..58dedc38f9 100644 --- a/GPU/GLES/Framebuffer.h +++ b/GPU/GLES/Framebuffer.h @@ -77,6 +77,19 @@ struct VirtualFramebuffer { void CenterRect(float *x, float *y, float *w, float *h, float origW, float origH, float frameW, float frameH); +// Simple struct for asynchronous PBO readbacks +struct AsyncPBO { + GLuint handle; + size_t maxSize; + + u32 fb_address; + u32 stride; + u32 height; + size_t size; + int format; + bool reading; +}; + class ShaderManager; class FramebufferManager { @@ -92,7 +105,7 @@ public: } void DrawPixels(const u8 *framebuf, int pixelFormat, int linesize); - void DrawActiveTexture(float x, float y, float w, float h, bool flip = false, float uscale = 1.0f); + void DrawActiveTexture(float x, float y, float w, float h, bool flip = false, float uscale = 1.0f, GLSLProgram *program = 0); void DestroyAllFBOs(); void DecimateFBOs(); @@ -144,7 +157,14 @@ private: VirtualFramebuffer *currentRenderVfb_; // Used by ReadFramebufferToMemory + void BlitFramebuffer_(VirtualFramebuffer *src, VirtualFramebuffer *dst, bool flip = false, float upscale = 1.0f); + void PackFramebuffer_(VirtualFramebuffer *vfb); + void PackFramebufferGL_(VirtualFramebuffer *vfb); + void PackFramebufferGLES_(VirtualFramebuffer *vfb); std::vector bvfbs_; // blitting FBOs + GLSLProgram *blitprogram; + AsyncPBO *pixelBufObj_; //this isn't that large + u8 currentPBO_; // Used by DrawPixels unsigned int drawPixelsTex_;