DX9/DX11: Workaround the viewpoint/EFB creation issues in e.g. SMG2 on NVIDIA hardwar...
[dolphin.git] / Source / Plugins / Plugin_VideoDX9 / Src / FramebufferManager.cpp
blobcebcd00211a5bdaef904b19dc9427671b2b773e8
1 // Copyright (C) 2003 Dolphin Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
18 #include "D3DBase.h"
19 #include "Render.h"
20 #include "FramebufferManager.h"
21 #include "VideoConfig.h"
22 #include "PixelShaderCache.h"
23 #include "VertexShaderCache.h"
24 #include "TextureConverter.h"
26 #undef CHECK
27 #define CHECK(hr, Message, ...) if (FAILED(hr)) { PanicAlert(__FUNCTION__ "Failed in %s at line %d: " Message, __FILE__, __LINE__, __VA_ARGS__); }
29 FramebufferManager FBManager;
31 LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorRTSurface()
33 return s_efb_color_surface;
36 LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthRTSurface()
38 return s_efb_depth_surface;
41 LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorOffScreenRTSurface()
43 return s_efb_color_OffScreenReadBuffer;
46 LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthOffScreenRTSurface()
48 return s_efb_depth_OffScreenReadBuffer;
51 LPDIRECT3DSURFACE9 FramebufferManager::GetEFBColorReadSurface()
53 return s_efb_color_ReadBuffer;
56 LPDIRECT3DSURFACE9 FramebufferManager::GetEFBDepthReadSurface()
58 return s_efb_depth_ReadBuffer;
61 D3DFORMAT FramebufferManager::GetEFBDepthRTSurfaceFormat()
63 return s_efb_depth_surface_Format;
66 D3DFORMAT FramebufferManager::GetEFBDepthReadSurfaceFormat()
68 return s_efb_depth_ReadBuffer_Format;
71 D3DFORMAT FramebufferManager::GetEFBColorRTSurfaceFormat()
73 return s_efb_color_surface_Format;
76 LPDIRECT3DTEXTURE9 FramebufferManager::GetEFBColorTexture(const EFBRectangle& sourceRc)
78 return s_efb_color_texture;
81 LPDIRECT3DTEXTURE9 FramebufferManager::GetEFBDepthTexture(const EFBRectangle &sourceRc)
83 return s_efb_depth_texture;
86 void FramebufferManager::Create()
88 // Simplest possible setup to start with.
89 int target_width = Renderer::GetFullTargetWidth();
90 int target_height = Renderer::GetFullTargetHeight();
92 s_efb_color_surface_Format = D3DFMT_A8R8G8B8;
93 // Get the framebuffer texture
94 HRESULT hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format,
95 D3DPOOL_DEFAULT, &s_efb_color_texture, NULL);
96 if(s_efb_color_texture)
98 hr = s_efb_color_texture->GetSurfaceLevel(0, &s_efb_color_surface);
100 CHECK(hr, "Create Color Texture (size: %dx%d; hr=%#x)", target_width, target_height, hr);
101 hr = D3D::dev->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format,
102 D3DPOOL_DEFAULT, &s_efb_colorRead_texture, NULL);
103 CHECK(hr, "Create Color Read Texture (hr=%#x)", hr);
104 if(s_efb_colorRead_texture)
106 s_efb_colorRead_texture->GetSurfaceLevel(0, &s_efb_color_ReadBuffer);
108 // Create an offscreen surface that we can lock to retrieve the data
109 hr = D3D::dev->CreateOffscreenPlainSurface(1, 1, s_efb_color_surface_Format, D3DPOOL_SYSTEMMEM, &s_efb_color_OffScreenReadBuffer, NULL);
110 CHECK(hr, "Create Color offScreen Surface (hr=%#x)", hr);
112 // Select Zbuffer format supported by hardware.
113 if (g_ActiveConfig.bEFBAccessEnable)
115 D3DFORMAT *DepthTexFormats = new D3DFORMAT[5];
116 DepthTexFormats[0] = FOURCC_INTZ;
117 DepthTexFormats[1] = FOURCC_DF24;
118 DepthTexFormats[2] = FOURCC_RAWZ;
119 DepthTexFormats[3] = FOURCC_DF16;
120 DepthTexFormats[4] = D3DFMT_D24X8;
122 for(int i = 0; i < 5; i++)
124 s_efb_depth_surface_Format = DepthTexFormats[i];
125 // Get the framebuffer Depth texture
126 hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_DEPTHSTENCIL, s_efb_depth_surface_Format,
127 D3DPOOL_DEFAULT, &s_efb_depth_texture, NULL);
128 if (!FAILED(hr))
129 break;
131 CHECK(hr, "Depth Color Texture (size: %dx%d; hr=%#x)", target_width, target_height, hr);
132 // Get the Surface
133 if(s_efb_depth_texture)
135 s_efb_depth_texture->GetSurfaceLevel(0, &s_efb_depth_surface);
137 // Create a 4x4 pixel texture to work as a buffer for peeking
138 if(s_efb_depth_surface_Format == FOURCC_RAWZ || s_efb_depth_surface_Format == D3DFMT_D24X8)
140 DepthTexFormats[0] = D3DFMT_A8R8G8B8;
142 else
144 DepthTexFormats[0] = D3DFMT_R32F;
146 DepthTexFormats[1] = D3DFMT_A8R8G8B8;
148 for(int i = 0; i < 2; i++)
150 s_efb_depth_ReadBuffer_Format = DepthTexFormats[i];
151 // Get the framebuffer Depth texture
152 hr = D3D::dev->CreateTexture(4, 4, 1, D3DUSAGE_RENDERTARGET, s_efb_depth_ReadBuffer_Format,
153 D3DPOOL_DEFAULT, &s_efb_depthRead_texture, NULL);
154 if (!FAILED(hr))
155 break;
158 CHECK(hr, "Create Depth Read texture (hr=%#x)", hr);
159 if(s_efb_depthRead_texture)
161 s_efb_depthRead_texture->GetSurfaceLevel(0, &s_efb_depth_ReadBuffer);
163 // Create an offscreen surface that we can lock to retrieve the data
164 hr = D3D::dev->CreateOffscreenPlainSurface(4, 4, s_efb_depth_ReadBuffer_Format, D3DPOOL_SYSTEMMEM, &s_efb_depth_OffScreenReadBuffer, NULL);
165 CHECK(hr, "Create Depth offScreen Surface (hr=%#x)", hr);
166 delete [] DepthTexFormats;
168 else
170 s_efb_depth_surface_Format = D3DFMT_D24X8;
171 hr = D3D::dev->CreateDepthStencilSurface(target_width, target_height, s_efb_depth_surface_Format,
172 D3DMULTISAMPLE_NONE, 0, FALSE, &s_efb_depth_surface, NULL);
173 CHECK(hr, "CreateDepthStencilSurface (hr=%#x)", hr);
177 void FramebufferManager::Destroy()
179 if (s_efb_depth_surface)
180 s_efb_depth_surface->Release();
181 s_efb_depth_surface = NULL;
183 if (s_efb_color_surface)
184 s_efb_color_surface->Release();
185 s_efb_color_surface = NULL;
187 if (s_efb_color_ReadBuffer)
188 s_efb_color_ReadBuffer->Release();
189 s_efb_color_ReadBuffer = NULL;
191 if (s_efb_depth_ReadBuffer)
192 s_efb_depth_ReadBuffer->Release();
193 s_efb_depth_ReadBuffer = NULL;
195 if (s_efb_color_OffScreenReadBuffer)
196 s_efb_color_OffScreenReadBuffer->Release();
197 s_efb_color_OffScreenReadBuffer = NULL;
199 if (s_efb_depth_OffScreenReadBuffer)
200 s_efb_depth_OffScreenReadBuffer->Release();
201 s_efb_depth_OffScreenReadBuffer = NULL;
203 if (s_efb_color_texture)
204 s_efb_color_texture->Release();
205 s_efb_color_texture = NULL;
207 if (s_efb_colorRead_texture)
208 s_efb_colorRead_texture->Release();
209 s_efb_colorRead_texture = NULL;
211 if (s_efb_depth_texture)
212 s_efb_depth_texture->Release();
213 s_efb_depth_texture = NULL;
215 if (s_efb_depthRead_texture)
216 s_efb_depthRead_texture->Release();
217 s_efb_depthRead_texture = NULL;
219 for (VirtualXFBListType::iterator it = m_virtualXFBList.begin(); it != m_virtualXFBList.end(); ++it)
221 if(it->xfbSource.texture)
222 it->xfbSource.texture->Release();
224 m_virtualXFBList.clear();
225 if(m_realXFBSource.texture)
226 m_realXFBSource.texture->Release();
227 m_realXFBSource.texture = NULL;
231 void FramebufferManager::CopyToXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc)
233 if (g_ActiveConfig.bUseRealXFB)
234 copyToRealXFB(xfbAddr, fbWidth, fbHeight, sourceRc);
235 else
236 copyToVirtualXFB(xfbAddr, fbWidth, fbHeight, sourceRc);
239 const XFBSource** FramebufferManager::GetXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount)
241 if (g_ActiveConfig.bUseRealXFB)
242 return getRealXFBSource(xfbAddr, fbWidth, fbHeight, xfbCount);
243 else
244 return getVirtualXFBSource(xfbAddr, fbWidth, fbHeight, xfbCount);
247 FramebufferManager::VirtualXFBListType::iterator FramebufferManager::findVirtualXFB(u32 xfbAddr, u32 width, u32 height)
249 u32 srcLower = xfbAddr;
250 u32 srcUpper = xfbAddr + 2 * width * height;
252 VirtualXFBListType::iterator it;
253 for (it = m_virtualXFBList.begin(); it != m_virtualXFBList.end(); ++it)
255 u32 dstLower = it->xfbAddr;
256 u32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight;
258 if (dstLower >= srcLower && dstUpper <= srcUpper)
259 return it;
262 // That address is not in the Virtual XFB list.
263 return m_virtualXFBList.end();
266 void FramebufferManager::replaceVirtualXFB()
268 VirtualXFBListType::iterator it = m_virtualXFBList.begin();
270 s32 srcLower = it->xfbAddr;
271 s32 srcUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight;
272 s32 lineSize = 2 * it->xfbWidth;
274 ++it;
276 while (it != m_virtualXFBList.end())
278 s32 dstLower = it->xfbAddr;
279 s32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight;
281 if (dstLower >= srcLower && dstUpper <= srcUpper)
283 // Invalidate the data
284 it->xfbAddr = 0;
285 it->xfbHeight = 0;
286 it->xfbWidth = 0;
288 else if (addrRangesOverlap(srcLower, srcUpper, dstLower, dstUpper))
290 s32 upperOverlap = (srcUpper - dstLower) / lineSize;
291 s32 lowerOverlap = (dstUpper - srcLower) / lineSize;
293 if (upperOverlap > 0 && lowerOverlap < 0)
295 it->xfbAddr += lineSize * upperOverlap;
296 it->xfbHeight -= upperOverlap;
298 else if (lowerOverlap > 0)
300 it->xfbHeight -= lowerOverlap;
304 ++it;
308 void FramebufferManager::copyToRealXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc)
310 u8* xfb_in_ram = Memory_GetPtr(xfbAddr);
311 if (!xfb_in_ram)
313 WARN_LOG(VIDEO, "Tried to copy to invalid XFB address");
314 return;
317 TargetRectangle targetRc = Renderer::ConvertEFBRectangle(sourceRc);
318 TextureConverter::EncodeToRamYUYV(GetEFBColorTexture(sourceRc), targetRc, xfb_in_ram, fbWidth, fbHeight);
321 void FramebufferManager::copyToVirtualXFB(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc)
323 LPDIRECT3DTEXTURE9 xfbTexture;
324 HRESULT hr = 0;
327 VirtualXFBListType::iterator it = findVirtualXFB(xfbAddr, fbWidth, fbHeight);
329 if (it == m_virtualXFBList.end() && (int)m_virtualXFBList.size() >= MAX_VIRTUAL_XFB)
331 // Replace the last virtual XFB
332 --it;
335 float scaleX = Renderer::GetXFBScaleX();
336 float scaleY = Renderer::GetXFBScaleY();
337 TargetRectangle targetSource,efbSource;
338 efbSource = Renderer::ConvertEFBRectangle(sourceRc);
339 targetSource.top = (int)(sourceRc.top *scaleY);
340 targetSource.bottom = (int)(sourceRc.bottom *scaleY);
341 targetSource.left = (int)(sourceRc.left *scaleX);
342 targetSource.right = (int)(sourceRc.right * scaleX);
343 int target_width = targetSource.right - targetSource.left;
344 int target_height = targetSource.bottom - targetSource.top;
345 if (it != m_virtualXFBList.end())
347 // Overwrite an existing Virtual XFB.
349 it->xfbAddr = xfbAddr;
350 it->xfbWidth = fbWidth;
351 it->xfbHeight = fbHeight;
353 it->xfbSource.srcAddr = xfbAddr;
354 it->xfbSource.srcWidth = fbWidth;
355 it->xfbSource.srcHeight = fbHeight;
357 if(it->xfbSource.texWidth != target_width || it->xfbSource.texHeight != target_height || !(it->xfbSource.texture))
359 if(it->xfbSource.texture)
360 it->xfbSource.texture->Release();
361 it->xfbSource.texture = NULL;
362 hr = D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format,
363 D3DPOOL_DEFAULT, &(it->xfbSource.texture), NULL);
367 xfbTexture = it->xfbSource.texture;
369 it->xfbSource.texWidth = target_width;
370 it->xfbSource.texHeight = target_height;
372 // Move this Virtual XFB to the front of the list.
373 m_virtualXFBList.splice(m_virtualXFBList.begin(), m_virtualXFBList, it);
375 // Keep stale XFB data from being used
376 replaceVirtualXFB();
378 else
380 // Create a new Virtual XFB and place it at the front of the list.
382 D3D::dev->CreateTexture(target_width, target_height, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format,
383 D3DPOOL_DEFAULT, &xfbTexture, NULL);
384 VirtualXFB newVirt;
386 newVirt.xfbAddr = xfbAddr;
387 newVirt.xfbWidth = fbWidth;
388 newVirt.xfbHeight = fbHeight;
390 newVirt.xfbSource.texture = xfbTexture;
391 newVirt.xfbSource.texWidth = target_width;
392 newVirt.xfbSource.texHeight = target_height;
394 // Add the new Virtual XFB to the list
396 if ((int)m_virtualXFBList.size() >= MAX_VIRTUAL_XFB)
398 // List overflowed; delete the oldest.
399 m_virtualXFBList.back().xfbSource.texture->Release();
400 m_virtualXFBList.pop_back();
403 m_virtualXFBList.push_front(newVirt);
406 // Copy EFB to XFB texture
408 if(!xfbTexture)
409 return;
410 LPDIRECT3DTEXTURE9 read_texture = GetEFBColorTexture(sourceRc);
412 Renderer::ResetAPIState(); // Reset any game specific settings
413 LPDIRECT3DSURFACE9 Rendersurf = NULL;
415 xfbTexture->GetSurfaceLevel(0, &Rendersurf);
416 D3D::dev->SetDepthStencilSurface(NULL);
417 D3D::dev->SetRenderTarget(0, Rendersurf);
419 D3DVIEWPORT9 vp;
421 vp.X = 0;
422 vp.Y = 0;
423 vp.Width = target_width;
424 vp.Height = target_height;
425 vp.MinZ = 0.0f;
426 vp.MaxZ = 1.0f;
427 D3D::dev->SetViewport(&vp);
428 RECT sourcerect;
429 sourcerect.bottom = efbSource.bottom;
430 sourcerect.left = efbSource.left;
431 sourcerect.right = efbSource.right;
432 sourcerect.top = efbSource.top;
434 D3D::ChangeSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
435 D3D::ChangeSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
437 D3D::drawShadedTexQuad(
438 read_texture,
439 &sourcerect,
440 Renderer::GetFullTargetWidth(),
441 Renderer::GetFullTargetHeight(),
442 target_width,
443 target_height,
444 PixelShaderCache::GetColorCopyProgram( g_ActiveConfig.iMultisampleMode),
445 VertexShaderCache::GetSimpleVertexShader( g_ActiveConfig.iMultisampleMode));
448 D3D::RefreshSamplerState(0, D3DSAMP_MINFILTER);
449 D3D::RefreshSamplerState(0, D3DSAMP_MAGFILTER);
450 D3D::SetTexture(0, NULL);
451 D3D::dev->SetRenderTarget(0, GetEFBColorRTSurface());
452 D3D::dev->SetDepthStencilSurface(GetEFBDepthRTSurface());
453 Renderer::RestoreAPIState();
454 Rendersurf->Release();
457 const XFBSource** FramebufferManager::getRealXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount)
459 xfbCount = 1;
461 m_realXFBSource.texWidth = fbWidth;
462 m_realXFBSource.texHeight = fbHeight;
464 m_realXFBSource.srcAddr = xfbAddr;
465 m_realXFBSource.srcWidth = fbWidth;
466 m_realXFBSource.srcHeight = fbHeight;
468 if (!m_realXFBSource.texture)
470 D3D::dev->CreateTexture(fbWidth, fbHeight, 1, D3DUSAGE_RENDERTARGET, s_efb_color_surface_Format,
471 D3DPOOL_DEFAULT, &m_realXFBSource.texture, NULL);
474 // Decode YUYV data from GameCube RAM
475 TextureConverter::DecodeToTexture(xfbAddr, fbWidth, fbHeight, m_realXFBSource.texture);
477 m_overlappingXFBArray[0] = &m_realXFBSource;
479 return &m_overlappingXFBArray[0];
482 const XFBSource** FramebufferManager::getVirtualXFBSource(u32 xfbAddr, u32 fbWidth, u32 fbHeight, u32 &xfbCount)
484 xfbCount = 0;
486 if (m_virtualXFBList.size() == 0)
488 // No Virtual XFBs available.
489 return NULL;
492 u32 srcLower = xfbAddr;
493 u32 srcUpper = xfbAddr + 2 * fbWidth * fbHeight;
495 VirtualXFBListType::reverse_iterator it;
496 for (it = m_virtualXFBList.rbegin(); it != m_virtualXFBList.rend(); ++it)
498 u32 dstLower = it->xfbAddr;
499 u32 dstUpper = it->xfbAddr + 2 * it->xfbWidth * it->xfbHeight;
501 if (addrRangesOverlap(srcLower, srcUpper, dstLower, dstUpper))
503 m_overlappingXFBArray[xfbCount] = &(it->xfbSource);
504 xfbCount++;
508 return &m_overlappingXFBArray[0];