2010-05-28 23:14:16 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "D3DBase.h"
|
|
|
|
#include "Render.h"
|
|
|
|
#include "FramebufferManager.h"
|
|
|
|
#include "VideoConfig.h"
|
|
|
|
#include "PixelShaderCache.h"
|
|
|
|
#include "VertexShaderCache.h"
|
|
|
|
#include "TextureConverter.h"
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
// TODO: this is probably somewhere else
|
|
|
|
#define SAFE_RELEASE(p) if (p) { (p)->Release(); (p) = NULL; }
|
|
|
|
|
2010-05-28 23:14:16 +00:00
|
|
|
#undef CHECK
|
2010-06-18 14:14:13 +00:00
|
|
|
#define CHECK(hr, Message, ...) if (FAILED(hr)) { PanicAlert(__FUNCTION__ "Failed in %s at line %d: " Message, __FILE__, __LINE__, __VA_ARGS__); }
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::Efb FramebufferManager::s_efb;
|
2010-06-05 01:38:22 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::FramebufferManager()
|
2010-06-05 01:38:22 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.color_texture = NULL;
|
|
|
|
s_efb.colorRead_texture = NULL;
|
|
|
|
s_efb.depth_texture = NULL;
|
|
|
|
s_efb.depthRead_texture = NULL;
|
2010-06-05 01:38:22 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.depth_surface = NULL;
|
|
|
|
s_efb.color_surface = NULL;
|
|
|
|
s_efb.color_ReadBuffer = NULL;
|
|
|
|
s_efb.depth_ReadBuffer = NULL;
|
|
|
|
s_efb.color_OffScreenReadBuffer = NULL;
|
|
|
|
s_efb.depth_OffScreenReadBuffer = NULL;
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.color_surface_Format = D3DFMT_FORCE_DWORD;
|
|
|
|
s_efb.depth_surface_Format = D3DFMT_FORCE_DWORD;
|
|
|
|
s_efb.depth_ReadBuffer_Format = D3DFMT_FORCE_DWORD;
|
2010-05-28 23:14:16 +00:00
|
|
|
|
|
|
|
// Simplest possible setup to start with.
|
|
|
|
int target_width = Renderer::GetFullTargetWidth();
|
|
|
|
int target_height = Renderer::GetFullTargetHeight();
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.color_surface_Format = D3DFMT_A8R8G8B8;
|
2010-06-05 21:48:38 +00:00
|
|
|
// Get the framebuffer texture
|
2010-11-14 23:31:53 +00:00
|
|
|
HRESULT hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb.color_surface_Format,
|
|
|
|
D3DPOOL_DEFAULT, &s_efb.color_texture, NULL);
|
|
|
|
if (s_efb.color_texture)
|
2010-05-28 23:14:16 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
hr = s_efb.color_texture->GetSurfaceLevel(0, &s_efb.color_surface);
|
2010-06-05 01:38:22 +00:00
|
|
|
}
|
2010-08-01 14:20:50 +00:00
|
|
|
CHECK(hr, "Create color texture (size: %dx%d; hr=%#x)", target_width, target_height, hr);
|
2010-11-14 23:31:53 +00:00
|
|
|
hr = D3D::dev->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, s_efb.color_surface_Format,
|
|
|
|
D3DPOOL_DEFAULT, &s_efb.colorRead_texture, NULL);
|
2010-06-18 14:18:09 +00:00
|
|
|
CHECK(hr, "Create Color Read Texture (hr=%#x)", hr);
|
2010-11-14 23:31:53 +00:00
|
|
|
if (s_efb.colorRead_texture)
|
2010-05-28 23:14:16 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.colorRead_texture->GetSurfaceLevel(0, &s_efb.color_ReadBuffer);
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
2010-06-05 21:48:38 +00:00
|
|
|
// Create an offscreen surface that we can lock to retrieve the data
|
2010-11-14 23:31:53 +00:00
|
|
|
hr = D3D::dev->CreateOffscreenPlainSurface(1, 1, s_efb.color_surface_Format, D3DPOOL_SYSTEMMEM, &s_efb.color_OffScreenReadBuffer, NULL);
|
2010-08-01 14:20:50 +00:00
|
|
|
CHECK(hr, "Create offscreen color surface (hr=%#x)", hr);
|
|
|
|
|
|
|
|
// Select a Z-buffer format with hardware support
|
2010-11-14 23:31:53 +00:00
|
|
|
D3DFORMAT DepthTexFormats[5] = {
|
|
|
|
FOURCC_INTZ,
|
|
|
|
FOURCC_DF24,
|
|
|
|
FOURCC_RAWZ,
|
|
|
|
FOURCC_DF16,
|
|
|
|
D3DFMT_D24X8
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; ++i)
|
2010-06-05 01:38:22 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.depth_surface_Format = DepthTexFormats[i];
|
2010-08-01 14:20:50 +00:00
|
|
|
// Create the framebuffer depth texture
|
2010-11-14 23:31:53 +00:00
|
|
|
hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_DEPTHSTENCIL, s_efb.depth_surface_Format,
|
|
|
|
D3DPOOL_DEFAULT, &s_efb.depth_texture, NULL);
|
2010-08-01 14:20:50 +00:00
|
|
|
if (!FAILED(hr))
|
|
|
|
break;
|
|
|
|
}
|
2010-11-14 23:31:53 +00:00
|
|
|
|
2010-08-01 14:20:50 +00:00
|
|
|
CHECK(hr, "Framebuffer depth texture (size: %dx%d; hr=%#x)", target_width, target_height, hr);
|
|
|
|
// Get the Surface
|
2010-11-14 23:31:53 +00:00
|
|
|
if (s_efb.depth_texture)
|
2010-08-01 14:20:50 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.depth_texture->GetSurfaceLevel(0, &s_efb.depth_surface);
|
2010-08-01 14:20:50 +00:00
|
|
|
}
|
2010-11-14 23:31:53 +00:00
|
|
|
|
2010-08-01 14:20:50 +00:00
|
|
|
// Create a 4x4 pixel texture to work as a buffer for peeking
|
2010-11-14 23:31:53 +00:00
|
|
|
if (s_efb.depth_surface_Format == FOURCC_RAWZ || s_efb.depth_surface_Format == D3DFMT_D24X8)
|
2010-08-01 14:20:50 +00:00
|
|
|
{
|
|
|
|
DepthTexFormats[0] = D3DFMT_A8R8G8B8;
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-01 14:20:50 +00:00
|
|
|
DepthTexFormats[0] = D3DFMT_R32F;
|
2010-06-05 01:38:22 +00:00
|
|
|
}
|
2010-11-14 23:31:53 +00:00
|
|
|
|
2010-08-01 14:20:50 +00:00
|
|
|
DepthTexFormats[1] = D3DFMT_A8R8G8B8;
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
for (int i = 0; i < 2; ++i)
|
2010-08-01 14:20:50 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.depth_ReadBuffer_Format = DepthTexFormats[i];
|
2010-08-01 14:20:50 +00:00
|
|
|
// Get the framebuffer Depth texture
|
2010-11-14 23:31:53 +00:00
|
|
|
hr = D3D::dev->CreateTexture(4, 4, 1, D3DUSAGE_RENDERTARGET, s_efb.depth_ReadBuffer_Format,
|
|
|
|
D3DPOOL_DEFAULT, &s_efb.depthRead_texture, NULL);
|
2010-08-01 14:20:50 +00:00
|
|
|
if (!FAILED(hr))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(hr, "Create depth read texture (hr=%#x)", hr);
|
2010-11-14 23:31:53 +00:00
|
|
|
if (s_efb.depthRead_texture)
|
2010-08-01 14:20:50 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
s_efb.depthRead_texture->GetSurfaceLevel(0, &s_efb.depth_ReadBuffer);
|
2010-08-01 14:20:50 +00:00
|
|
|
}
|
2010-11-14 23:31:53 +00:00
|
|
|
|
2010-08-01 14:20:50 +00:00
|
|
|
// Create an offscreen surface that we can lock to retrieve the data
|
2010-11-14 23:31:53 +00:00
|
|
|
hr = D3D::dev->CreateOffscreenPlainSurface(4, 4, s_efb.depth_ReadBuffer_Format, D3DPOOL_SYSTEMMEM, &s_efb.depth_OffScreenReadBuffer, NULL);
|
2010-08-01 14:20:50 +00:00
|
|
|
CHECK(hr, "Create depth offscreen surface (hr=%#x)", hr);
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::~FramebufferManager()
|
2010-06-05 01:38:22 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
SAFE_RELEASE(s_efb.depth_surface);
|
|
|
|
SAFE_RELEASE(s_efb.color_surface);
|
|
|
|
SAFE_RELEASE(s_efb.color_ReadBuffer);
|
|
|
|
SAFE_RELEASE(s_efb.depth_ReadBuffer);
|
|
|
|
SAFE_RELEASE(s_efb.color_OffScreenReadBuffer);
|
|
|
|
SAFE_RELEASE(s_efb.depth_OffScreenReadBuffer);
|
|
|
|
SAFE_RELEASE(s_efb.color_texture);
|
|
|
|
SAFE_RELEASE(s_efb.colorRead_texture);
|
|
|
|
SAFE_RELEASE(s_efb.depth_texture);
|
|
|
|
SAFE_RELEASE(s_efb.depthRead_texture);
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
XFBSourceBase* FramebufferManager::CreateXFBSource(unsigned int target_width, unsigned int target_height)
|
2010-05-28 23:14:16 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
LPDIRECT3DTEXTURE9 tex;
|
|
|
|
D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET,
|
|
|
|
s_efb.color_surface_Format, D3DPOOL_DEFAULT, &tex, NULL);
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
return new XFBSource(tex);
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void FramebufferManager::GetTargetSize(unsigned int *width, unsigned int *height, const EFBRectangle& sourceRc)
|
2010-05-28 23:14:16 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
const float scaleX = Renderer::GetXFBScaleX();
|
|
|
|
const float scaleY = Renderer::GetXFBScaleY();
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
TargetRectangle targetSource;
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
targetSource.top = (int)(sourceRc.top *scaleY);
|
|
|
|
targetSource.bottom = (int)(sourceRc.bottom *scaleY);
|
|
|
|
targetSource.left = (int)(sourceRc.left *scaleX);
|
|
|
|
targetSource.right = (int)(sourceRc.right * scaleX);
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
*width = targetSource.right - targetSource.left;
|
|
|
|
*height = targetSource.bottom - targetSource.top;
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void XFBSource::Draw(const MathUtil::Rectangle<float> &sourcerc,
|
|
|
|
const MathUtil::Rectangle<float> &drawrc, int width, int height) const
|
2010-05-28 23:14:16 +00:00
|
|
|
{
|
2010-11-14 23:31:53 +00:00
|
|
|
D3D::drawShadedTexSubQuad(texture, &sourcerc, texWidth, texHeight, &drawrc, width , height,
|
|
|
|
PixelShaderCache::GetColorCopyProgram(0), VertexShaderCache::GetSimpleVertexShader(0));
|
|
|
|
}
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void XFBSource::DecodeToTexture(u32 xfbAddr, u32 fbWidth, u32 fbHeight)
|
|
|
|
{
|
|
|
|
TextureConverter::DecodeToTexture(xfbAddr, fbWidth, fbHeight, texture);
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void FramebufferManager::CopyToRealXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc)
|
2010-05-28 23:14:16 +00:00
|
|
|
{
|
|
|
|
u8* xfb_in_ram = Memory_GetPtr(xfbAddr);
|
|
|
|
if (!xfb_in_ram)
|
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Tried to copy to invalid XFB address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-18 02:21:26 +00:00
|
|
|
TargetRectangle targetRc = g_renderer->ConvertEFBRectangle(sourceRc);
|
2010-10-22 19:40:05 +00:00
|
|
|
TextureConverter::EncodeToRamYUYV(GetEFBColorTexture(), targetRc, xfb_in_ram, fbWidth, fbHeight);
|
2010-05-28 23:14:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
void XFBSource::CopyEFB()
|
2010-05-28 23:14:16 +00:00
|
|
|
{
|
2010-09-28 02:15:02 +00:00
|
|
|
// Copy EFB data to XFB and restore render target again
|
2010-05-28 23:14:16 +00:00
|
|
|
LPDIRECT3DSURFACE9 Rendersurf = NULL;
|
2010-11-14 23:31:53 +00:00
|
|
|
texture->GetSurfaceLevel(0, &Rendersurf);
|
2010-05-28 23:14:16 +00:00
|
|
|
D3D::dev->SetDepthStencilSurface(NULL);
|
2010-06-05 01:38:22 +00:00
|
|
|
D3D::dev->SetRenderTarget(0, Rendersurf);
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
D3DVIEWPORT9 vp;
|
2010-05-28 23:14:16 +00:00
|
|
|
vp.X = 0;
|
|
|
|
vp.Y = 0;
|
2010-11-14 23:31:53 +00:00
|
|
|
vp.Width = texWidth;
|
|
|
|
vp.Height = texHeight;
|
2010-05-28 23:14:16 +00:00
|
|
|
vp.MinZ = 0.0f;
|
|
|
|
vp.MaxZ = 1.0f;
|
|
|
|
D3D::dev->SetViewport(&vp);
|
2010-11-14 23:31:53 +00:00
|
|
|
|
2010-05-28 23:14:16 +00:00
|
|
|
RECT sourcerect;
|
2010-11-14 23:31:53 +00:00
|
|
|
sourcerect.bottom = sourceRc.bottom;
|
|
|
|
sourcerect.left = sourceRc.left;
|
|
|
|
sourcerect.right = sourceRc.right;
|
|
|
|
sourcerect.top = sourceRc.top;
|
2010-05-28 23:14:16 +00:00
|
|
|
|
|
|
|
D3D::ChangeSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
D3D::ChangeSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
|
|
|
|
D3D::drawShadedTexQuad(
|
2010-11-14 23:31:53 +00:00
|
|
|
FramebufferManager::GetEFBColorTexture(),
|
2010-05-28 23:14:16 +00:00
|
|
|
&sourcerect,
|
2010-06-05 01:38:22 +00:00
|
|
|
Renderer::GetFullTargetWidth(),
|
|
|
|
Renderer::GetFullTargetHeight(),
|
2010-11-14 23:31:53 +00:00
|
|
|
texWidth,
|
|
|
|
texHeight,
|
2010-06-05 01:38:22 +00:00
|
|
|
PixelShaderCache::GetColorCopyProgram( g_ActiveConfig.iMultisampleMode),
|
|
|
|
VertexShaderCache::GetSimpleVertexShader( g_ActiveConfig.iMultisampleMode));
|
2010-11-14 23:31:53 +00:00
|
|
|
|
2010-05-28 23:14:16 +00:00
|
|
|
D3D::RefreshSamplerState(0, D3DSAMP_MINFILTER);
|
|
|
|
D3D::RefreshSamplerState(0, D3DSAMP_MAGFILTER);
|
2010-06-05 01:38:22 +00:00
|
|
|
D3D::SetTexture(0, NULL);
|
2010-11-14 23:31:53 +00:00
|
|
|
D3D::dev->SetRenderTarget(0, FramebufferManager::GetEFBColorRTSurface());
|
|
|
|
D3D::dev->SetDepthStencilSurface(FramebufferManager::GetEFBDepthRTSurface());
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2010-11-14 23:31:53 +00:00
|
|
|
Rendersurf->Release();
|
2010-06-02 20:35:12 +00:00
|
|
|
}
|