2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-09-01 20:54:40 +00:00
|
|
|
#include <algorithm>
|
2015-12-21 19:11:01 +00:00
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/FramebufferManagerBase.h"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-11-14 23:56:26 +00:00
|
|
|
|
2015-12-22 23:47:20 +00:00
|
|
|
std::unique_ptr<FramebufferManagerBase> g_framebuffer_manager;
|
2010-11-14 23:56:26 +00:00
|
|
|
|
2015-12-21 19:11:01 +00:00
|
|
|
std::unique_ptr<XFBSourceBase> FramebufferManagerBase::m_realXFBSource; // Only used in Real XFB mode
|
2010-11-14 23:56:26 +00:00
|
|
|
FramebufferManagerBase::VirtualXFBListType FramebufferManagerBase::m_virtualXFBList; // Only used in Virtual XFB mode
|
2015-12-21 19:11:01 +00:00
|
|
|
std::array<const XFBSourceBase*, FramebufferManagerBase::MAX_VIRTUAL_XFB> FramebufferManagerBase::m_overlappingXFBArray;
|
2010-11-14 23:56:26 +00:00
|
|
|
|
2012-10-02 20:11:15 +00:00
|
|
|
unsigned int FramebufferManagerBase::s_last_xfb_width = 1;
|
|
|
|
unsigned int FramebufferManagerBase::s_last_xfb_height = 1;
|
2012-09-28 21:48:18 +00:00
|
|
|
|
2014-11-06 10:41:39 +00:00
|
|
|
unsigned int FramebufferManagerBase::m_EFBLayers = 1;
|
|
|
|
|
2010-11-14 23:56:26 +00:00
|
|
|
FramebufferManagerBase::FramebufferManagerBase()
|
|
|
|
{
|
2015-12-21 19:11:01 +00:00
|
|
|
// Can't hurt
|
|
|
|
m_overlappingXFBArray.fill(nullptr);
|
2010-11-14 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FramebufferManagerBase::~FramebufferManagerBase()
|
|
|
|
{
|
2015-12-21 19:11:01 +00:00
|
|
|
// Necessary, as these are static members
|
|
|
|
// (they really shouldn't be and should be refactored at some point).
|
2010-11-14 23:56:26 +00:00
|
|
|
m_virtualXFBList.clear();
|
2015-12-21 19:11:01 +00:00
|
|
|
m_realXFBSource.reset();
|
2010-11-14 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 06:20:46 +00:00
|
|
|
const XFBSourceBase* const* FramebufferManagerBase::GetXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32* xfbCountP)
|
2010-11-14 23:56:26 +00:00
|
|
|
{
|
2013-04-24 13:21:54 +00:00
|
|
|
if (!g_ActiveConfig.bUseXFB)
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2011-03-19 00:21:59 +00:00
|
|
|
|
2010-11-14 23:56:26 +00:00
|
|
|
if (g_ActiveConfig.bUseRealXFB)
|
2014-10-02 06:20:46 +00:00
|
|
|
return GetRealXFBSource(xfbAddr, fbWidth, fbHeight, xfbCountP);
|
2010-11-14 23:56:26 +00:00
|
|
|
else
|
2014-10-02 06:20:46 +00:00
|
|
|
return GetVirtualXFBSource(xfbAddr, fbWidth, fbHeight, xfbCountP);
|
2010-11-14 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 06:20:46 +00:00
|
|
|
const XFBSourceBase* const* FramebufferManagerBase::GetRealXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32* xfbCountP)
|
2010-11-14 23:56:26 +00:00
|
|
|
{
|
2014-10-02 06:20:46 +00:00
|
|
|
*xfbCountP = 1;
|
2010-11-14 23:56:26 +00:00
|
|
|
|
2014-05-03 06:07:55 +00:00
|
|
|
// recreate if needed
|
|
|
|
if (m_realXFBSource && (m_realXFBSource->texWidth != fbWidth || m_realXFBSource->texHeight != fbHeight))
|
2015-12-21 19:11:01 +00:00
|
|
|
m_realXFBSource.reset();
|
2014-05-03 06:07:55 +00:00
|
|
|
|
2015-11-21 15:11:36 +00:00
|
|
|
if (!m_realXFBSource && g_framebuffer_manager)
|
2014-12-20 16:24:35 +00:00
|
|
|
m_realXFBSource = g_framebuffer_manager->CreateXFBSource(fbWidth, fbHeight, 1);
|
2010-11-14 23:56:26 +00:00
|
|
|
|
2015-11-21 09:45:26 +00:00
|
|
|
if (!m_realXFBSource)
|
|
|
|
return nullptr;
|
|
|
|
|
2010-11-14 23:56:26 +00:00
|
|
|
m_realXFBSource->srcAddr = xfbAddr;
|
|
|
|
|
|
|
|
m_realXFBSource->srcWidth = MAX_XFB_WIDTH;
|
|
|
|
m_realXFBSource->srcHeight = MAX_XFB_HEIGHT;
|
|
|
|
|
|
|
|
m_realXFBSource->texWidth = fbWidth;
|
|
|
|
m_realXFBSource->texHeight = fbHeight;
|
|
|
|
|
2013-03-20 01:51:12 +00:00
|
|
|
m_realXFBSource->sourceRc.left = 0;
|
2016-03-06 08:48:35 +00:00
|
|
|
m_realXFBSource->sourceRc.top = 0;
|
2013-03-20 01:51:12 +00:00
|
|
|
m_realXFBSource->sourceRc.right = fbWidth;
|
2016-03-06 08:48:35 +00:00
|
|
|
m_realXFBSource->sourceRc.bottom = fbHeight;
|
2010-11-14 23:56:26 +00:00
|
|
|
|
|
|
|
// Decode YUYV data from GameCube RAM
|
|
|
|
m_realXFBSource->DecodeToTexture(xfbAddr, fbWidth, fbHeight);
|
|
|
|
|
2015-12-21 19:11:01 +00:00
|
|
|
m_overlappingXFBArray[0] = m_realXFBSource.get();
|
2010-11-14 23:56:26 +00:00
|
|
|
return &m_overlappingXFBArray[0];
|
|
|
|
}
|
|
|
|
|
2014-10-02 06:20:46 +00:00
|
|
|
const XFBSourceBase* const* FramebufferManagerBase::GetVirtualXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32* xfbCountP)
|
2010-11-14 23:56:26 +00:00
|
|
|
{
|
2014-10-02 06:20:46 +00:00
|
|
|
u32 xfbCount = 0;
|
2010-11-14 23:56:26 +00:00
|
|
|
|
|
|
|
if (m_virtualXFBList.empty()) // no Virtual XFBs available
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2010-11-14 23:56:26 +00:00
|
|
|
|
|
|
|
u32 srcLower = xfbAddr;
|
|
|
|
u32 srcUpper = xfbAddr + 2 * fbWidth * fbHeight;
|
|
|
|
|
|
|
|
VirtualXFBListType::reverse_iterator
|
|
|
|
it = m_virtualXFBList.rbegin(),
|
|
|
|
vlend = m_virtualXFBList.rend();
|
|
|
|
for (; it != vlend; ++it)
|
|
|
|
{
|
|
|
|
VirtualXFB* vxfb = &*it;
|
|
|
|
|
|
|
|
u32 dstLower = vxfb->xfbAddr;
|
|
|
|
u32 dstUpper = vxfb->xfbAddr + 2 * vxfb->xfbWidth * vxfb->xfbHeight;
|
|
|
|
|
2014-09-20 18:54:59 +00:00
|
|
|
if (AddressRangesOverlap(srcLower, srcUpper, dstLower, dstUpper))
|
2010-11-14 23:56:26 +00:00
|
|
|
{
|
2015-12-21 19:11:01 +00:00
|
|
|
m_overlappingXFBArray[xfbCount] = vxfb->xfbSource.get();
|
2010-11-14 23:56:26 +00:00
|
|
|
++xfbCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 06:20:46 +00:00
|
|
|
*xfbCountP = xfbCount;
|
2010-11-14 23:56:26 +00:00
|
|
|
return &m_overlappingXFBArray[0];
|
|
|
|
}
|
|
|
|
|
2015-07-07 13:09:25 +00:00
|
|
|
void FramebufferManagerBase::CopyToXFB(u32 xfbAddr, u32 fbStride, u32 fbHeight, const EFBRectangle& sourceRc, float Gamma)
|
2010-11-14 23:56:26 +00:00
|
|
|
{
|
|
|
|
if (g_ActiveConfig.bUseRealXFB)
|
2015-11-21 15:11:36 +00:00
|
|
|
{
|
|
|
|
if (g_framebuffer_manager)
|
|
|
|
g_framebuffer_manager->CopyToRealXFB(xfbAddr, fbStride, fbHeight, sourceRc, Gamma);
|
|
|
|
}
|
2010-11-14 23:56:26 +00:00
|
|
|
else
|
2015-11-21 15:11:36 +00:00
|
|
|
{
|
2015-07-07 13:09:25 +00:00
|
|
|
CopyToVirtualXFB(xfbAddr, fbStride, fbHeight, sourceRc, Gamma);
|
2015-11-21 15:11:36 +00:00
|
|
|
}
|
2010-11-14 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:48:56 +00:00
|
|
|
void FramebufferManagerBase::CopyToVirtualXFB(u32 xfbAddr, u32 fbStride, u32 fbHeight, const EFBRectangle& sourceRc, float Gamma)
|
2010-11-14 23:56:26 +00:00
|
|
|
{
|
2015-11-21 15:11:36 +00:00
|
|
|
if (!g_framebuffer_manager)
|
|
|
|
return;
|
|
|
|
|
2015-07-24 18:48:56 +00:00
|
|
|
VirtualXFBListType::iterator vxfb = FindVirtualXFB(xfbAddr, sourceRc.GetWidth(), fbHeight);
|
2010-11-14 23:56:26 +00:00
|
|
|
|
|
|
|
if (m_virtualXFBList.end() == vxfb)
|
|
|
|
{
|
|
|
|
if (m_virtualXFBList.size() < MAX_VIRTUAL_XFB)
|
|
|
|
{
|
|
|
|
// create a new Virtual XFB and place it at the front of the list
|
2015-02-04 15:36:42 +00:00
|
|
|
m_virtualXFBList.emplace_front();
|
2010-11-14 23:56:26 +00:00
|
|
|
vxfb = m_virtualXFBList.begin();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Replace the last virtual XFB
|
|
|
|
--vxfb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//else // replace existing virtual XFB
|
|
|
|
|
|
|
|
// move this Virtual XFB to the front of the list.
|
|
|
|
if (m_virtualXFBList.begin() != vxfb)
|
|
|
|
m_virtualXFBList.splice(m_virtualXFBList.begin(), m_virtualXFBList, vxfb);
|
|
|
|
|
|
|
|
unsigned int target_width, target_height;
|
2014-12-24 23:58:16 +00:00
|
|
|
g_framebuffer_manager->GetTargetSize(&target_width, &target_height);
|
2010-11-14 23:56:26 +00:00
|
|
|
|
|
|
|
// recreate if needed
|
|
|
|
if (vxfb->xfbSource && (vxfb->xfbSource->texWidth != target_width || vxfb->xfbSource->texHeight != target_height))
|
2015-12-21 19:11:01 +00:00
|
|
|
vxfb->xfbSource.reset();
|
2010-11-14 23:56:26 +00:00
|
|
|
|
|
|
|
if (!vxfb->xfbSource)
|
|
|
|
{
|
2014-12-20 16:24:35 +00:00
|
|
|
vxfb->xfbSource = g_framebuffer_manager->CreateXFBSource(target_width, target_height, m_EFBLayers);
|
2015-11-21 09:45:26 +00:00
|
|
|
if (!vxfb->xfbSource)
|
|
|
|
return;
|
|
|
|
|
2010-11-14 23:56:26 +00:00
|
|
|
vxfb->xfbSource->texWidth = target_width;
|
|
|
|
vxfb->xfbSource->texHeight = target_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
vxfb->xfbSource->srcAddr = vxfb->xfbAddr = xfbAddr;
|
2015-07-24 18:48:56 +00:00
|
|
|
vxfb->xfbSource->srcWidth = vxfb->xfbWidth = sourceRc.GetWidth();
|
2010-11-14 23:56:26 +00:00
|
|
|
vxfb->xfbSource->srcHeight = vxfb->xfbHeight = fbHeight;
|
|
|
|
|
2010-11-18 02:21:26 +00:00
|
|
|
vxfb->xfbSource->sourceRc = g_renderer->ConvertEFBRectangle(sourceRc);
|
2010-11-14 23:56:26 +00:00
|
|
|
|
|
|
|
// keep stale XFB data from being used
|
|
|
|
ReplaceVirtualXFB();
|
|
|
|
|
|
|
|
// Copy EFB data to XFB and restore render target again
|
2010-12-27 03:18:01 +00:00
|
|
|
vxfb->xfbSource->CopyEFB(Gamma);
|
2010-11-14 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FramebufferManagerBase::VirtualXFBListType::iterator FramebufferManagerBase::FindVirtualXFB(u32 xfbAddr, u32 width, u32 height)
|
|
|
|
{
|
|
|
|
const u32 srcLower = xfbAddr;
|
|
|
|
const u32 srcUpper = xfbAddr + 2 * width * height;
|
|
|
|
|
2015-09-01 20:54:40 +00:00
|
|
|
return std::find_if(m_virtualXFBList.begin(), m_virtualXFBList.end(), [srcLower, srcUpper](const VirtualXFB& xfb) {
|
|
|
|
const u32 dstLower = xfb.xfbAddr;
|
|
|
|
const u32 dstUpper = xfb.xfbAddr + 2 * xfb.xfbWidth * xfb.xfbHeight;
|
2010-11-14 23:56:26 +00:00
|
|
|
|
2015-09-01 20:54:40 +00:00
|
|
|
return dstLower >= srcLower && dstUpper <= srcUpper;
|
|
|
|
});
|
2010-11-14 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FramebufferManagerBase::ReplaceVirtualXFB()
|
|
|
|
{
|
|
|
|
VirtualXFBListType::iterator it = m_virtualXFBList.begin();
|
|
|
|
|
|
|
|
const s32 srcLower = it->xfbAddr;
|
|
|
|
const s32 srcUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight;
|
|
|
|
const s32 lineSize = 2 * it->xfbWidth;
|
|
|
|
|
|
|
|
++it;
|
|
|
|
|
|
|
|
for (; it != m_virtualXFBList.end(); ++it)
|
|
|
|
{
|
|
|
|
s32 dstLower = it->xfbAddr;
|
|
|
|
s32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight;
|
|
|
|
|
|
|
|
if (dstLower >= srcLower && dstUpper <= srcUpper)
|
|
|
|
{
|
|
|
|
// Invalidate the data
|
|
|
|
it->xfbAddr = 0;
|
|
|
|
it->xfbHeight = 0;
|
|
|
|
it->xfbWidth = 0;
|
|
|
|
}
|
2014-09-20 18:54:59 +00:00
|
|
|
else if (AddressRangesOverlap(srcLower, srcUpper, dstLower, dstUpper))
|
2010-11-14 23:56:26 +00:00
|
|
|
{
|
|
|
|
s32 upperOverlap = (srcUpper - dstLower) / lineSize;
|
|
|
|
s32 lowerOverlap = (dstUpper - srcLower) / lineSize;
|
|
|
|
|
|
|
|
if (upperOverlap > 0 && lowerOverlap < 0)
|
|
|
|
{
|
|
|
|
it->xfbAddr += lineSize * upperOverlap;
|
|
|
|
it->xfbHeight -= upperOverlap;
|
|
|
|
}
|
|
|
|
else if (lowerOverlap > 0)
|
|
|
|
{
|
|
|
|
it->xfbHeight -= lowerOverlap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-28 22:19:28 +00:00
|
|
|
|
2014-12-24 23:58:16 +00:00
|
|
|
int FramebufferManagerBase::ScaleToVirtualXfbWidth(int x)
|
2012-09-28 22:19:28 +00:00
|
|
|
{
|
|
|
|
if (g_ActiveConfig.RealXFBEnabled())
|
2012-10-02 20:11:15 +00:00
|
|
|
return x;
|
2012-09-28 22:19:28 +00:00
|
|
|
|
2014-07-26 10:45:10 +00:00
|
|
|
return x * (int)Renderer::GetTargetRectangle().GetWidth() / (int)FramebufferManagerBase::LastXfbWidth();
|
2012-09-28 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
2014-12-24 23:58:16 +00:00
|
|
|
int FramebufferManagerBase::ScaleToVirtualXfbHeight(int y)
|
2012-09-28 22:19:28 +00:00
|
|
|
{
|
|
|
|
if (g_ActiveConfig.RealXFBEnabled())
|
2012-10-02 20:11:15 +00:00
|
|
|
return y;
|
2012-09-28 22:19:28 +00:00
|
|
|
|
2014-07-26 10:45:10 +00:00
|
|
|
return y * (int)Renderer::GetTargetRectangle().GetHeight() / (int)FramebufferManagerBase::LastXfbHeight();
|
2012-09-28 22:19:28 +00:00
|
|
|
}
|