| 
									
										
										
										
											2013-04-17 23:29:41 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Core/Core.h"
 | 
					
						
							|  |  |  | #include "Core/HW/Memmap.h"
 | 
					
						
							| 
									
										
										
										
											2014-08-01 23:21:03 -07:00
										 |  |  | #include "VideoBackends/OGL/GLInterfaceBase.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "VideoBackends/Software/BPMemLoader.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/DebugUtil.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/EfbCopy.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/EfbInterface.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/HwRasterizer.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/SWCommandProcessor.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/SWRenderer.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/SWStatistics.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/SWVideoConfig.h"
 | 
					
						
							|  |  |  | #include "VideoBackends/Software/TextureEncoder.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-08 16:49:33 +02:00
										 |  |  | #include "VideoCommon/Fifo.h"
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-20 23:51:39 +12:00
										 |  |  | static const float s_gammaLUT[] = | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	1.0f, | 
					
						
							|  |  |  | 	1.7f, | 
					
						
							|  |  |  | 	2.2f, | 
					
						
							|  |  |  | 	1.0f | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | namespace EfbCopy | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-08 14:29:26 +02:00
										 |  |  | 	static void CopyToXfb(u32 xfbAddr, u32 fbWidth, u32 fbHeight, const EFBRectangle& sourceRc, float Gamma) | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-03-01 18:52:19 +01:00
										 |  |  | 		GLInterface->Update(); // update the render window position and the backbuffer size
 | 
					
						
							| 
									
										
										
										
											2013-11-23 20:04:37 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-29 04:52:19 +00:00
										 |  |  | 		if (!g_SWVideoConfig.bHwRasterizer) | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-08-20 23:51:39 +12:00
										 |  |  | 			INFO_LOG(VIDEO, "xfbaddr: %x, fbwidth: %i, fbheight: %i, source: (%i, %i, %i, %i), Gamma %f", | 
					
						
							|  |  |  | 					 xfbAddr, fbWidth, fbHeight, sourceRc.top, sourceRc.left, sourceRc.bottom, sourceRc.right, Gamma); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 			if (!g_SWVideoConfig.bBypassXFB) | 
					
						
							| 
									
										
										
										
											2014-03-01 18:52:19 +01:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2013-11-23 20:04:37 +13:00
										 |  |  | 				EfbInterface::yuv422_packed* xfb_in_ram = (EfbInterface::yuv422_packed *) Memory::GetPointer(xfbAddr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				EfbInterface::CopyToXFB(xfb_in_ram, fbWidth, fbHeight, sourceRc, Gamma); | 
					
						
							| 
									
										
										
										
											2014-03-01 18:52:19 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				// Ask SWRenderer for the next color texture
 | 
					
						
							| 
									
										
										
										
											2014-08-10 22:01:42 -04:00
										 |  |  | 				u8 *colorTexture = SWRenderer::GetNextColorTexture(); | 
					
						
							| 
									
										
										
										
											2013-11-23 20:04:37 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 20:16:30 +13:00
										 |  |  | 				EfbInterface::BypassXFB(colorTexture, fbWidth, fbHeight, sourceRc, Gamma); | 
					
						
							| 
									
										
										
										
											2013-11-23 20:04:37 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 18:52:19 +01:00
										 |  |  | 				// Tell SWRenderer we are now finished with it.
 | 
					
						
							| 
									
										
										
										
											2014-08-10 22:01:42 -04:00
										 |  |  | 				SWRenderer::SwapColorTexture(); | 
					
						
							| 
									
										
										
										
											2014-02-02 01:00:19 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 18:52:19 +01:00
										 |  |  | 				// FifoPlayer is broken and never calls BeginFrame/EndFrame.
 | 
					
						
							|  |  |  | 				// Hence, we manually force a swap now. This emulates the behavior
 | 
					
						
							|  |  |  | 				// of hardware backends with XFB emulation disabled.
 | 
					
						
							|  |  |  | 				// TODO: Fix FifoPlayer by making proper use of VideoInterface!
 | 
					
						
							|  |  |  | 				//       This requires careful synchronization since GPU commands
 | 
					
						
							|  |  |  | 				//       are processed on a different thread than VI commands.
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:00:19 +13:00
										 |  |  | 				SWRenderer::Swap(fbWidth, fbHeight); | 
					
						
							| 
									
										
										
										
											2014-07-17 13:14:11 +02:00
										 |  |  | 				DebugUtil::OnFrameEnd(fbWidth, fbHeight); | 
					
						
							| 
									
										
										
										
											2013-11-23 20:04:37 +13:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-20 23:51:39 +12:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-08 14:29:26 +02:00
										 |  |  | 	static void CopyToRam() | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (!g_SWVideoConfig.bHwRasterizer) | 
					
						
							| 
									
										
										
										
											2010-12-02 05:38:48 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-01-31 01:28:32 +00:00
										 |  |  | 			u8 *dest_ptr = Memory::GetPointer(bpmem.copyTexDest << 5); | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-02 05:38:48 +00:00
										 |  |  | 			TextureEncoder::Encode(dest_ptr); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-08 14:29:26 +02:00
										 |  |  | 	static void ClearEfb() | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		u32 clearColor = (bpmem.clearcolorAR & 0xff) << 24 | bpmem.clearcolorGB << 8 | (bpmem.clearcolorAR & 0xff00) >> 8; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int left   = bpmem.copyTexSrcXY.x; | 
					
						
							|  |  |  | 		int top    = bpmem.copyTexSrcXY.y; | 
					
						
							| 
									
										
										
										
											2013-08-23 00:07:21 +12:00
										 |  |  | 		int right  = left + bpmem.copyTexSrcWH.x; | 
					
						
							|  |  |  | 		int bottom = top + bpmem.copyTexSrcWH.y; | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for (u16 y = top; y <= bottom; y++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			for (u16 x = left; x <= right; x++) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				EfbInterface::SetColor(x, y, (u8*)(&clearColor)); | 
					
						
							|  |  |  | 				EfbInterface::SetDepth(x, y, bpmem.clearZValue); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void CopyEfb() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-08-20 23:51:39 +12:00
										 |  |  | 		EFBRectangle rc; | 
					
						
							|  |  |  | 		rc.left = (int)bpmem.copyTexSrcXY.x; | 
					
						
							|  |  |  | 		rc.top = (int)bpmem.copyTexSrcXY.y; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-23 00:07:21 +12:00
										 |  |  | 		// flipper represents the widths internally as last pixel minus starting pixel, so
 | 
					
						
							|  |  |  | 		// these are zero indexed.
 | 
					
						
							|  |  |  | 		rc.right = rc.left + (int)bpmem.copyTexSrcWH.x + 1; | 
					
						
							|  |  |  | 		rc.bottom = rc.top + (int)bpmem.copyTexSrcWH.y + 1; | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!g_bSkipCurrentFrame) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (bpmem.triggerEFBCopy.copy_to_xfb) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2013-08-20 23:51:39 +12:00
										 |  |  | 				float yScale; | 
					
						
							|  |  |  | 				if (bpmem.triggerEFBCopy.scale_invert) | 
					
						
							|  |  |  | 					yScale = 256.0f / (float)bpmem.dispcopyyscale; | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					yScale = (float)bpmem.dispcopyyscale / 256.0f; | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-20 23:51:39 +12:00
										 |  |  | 				float xfbLines = ((bpmem.copyTexSrcWH.y + 1.0f) * yScale); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (yScale != 1.0) | 
					
						
							|  |  |  | 					WARN_LOG(VIDEO, "yScale of %f is currently unsupported", yScale); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if ((u32)xfbLines > MAX_XFB_HEIGHT) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					INFO_LOG(VIDEO, "Tried to scale EFB to too many XFB lines (%f)", xfbLines); | 
					
						
							|  |  |  | 					xfbLines = MAX_XFB_HEIGHT; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				CopyToXfb(bpmem.copyTexDest << 5, | 
					
						
							|  |  |  | 						  bpmem.copyMipMapStrideChannels << 4, | 
					
						
							|  |  |  | 						  (u32)xfbLines, | 
					
						
							|  |  |  | 						  rc, | 
					
						
							|  |  |  | 						  s_gammaLUT[bpmem.triggerEFBCopy.gamma]); | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2013-08-20 23:51:39 +12:00
										 |  |  | 				CopyToRam(); // FIXME: should use the rectangle we have already created above
 | 
					
						
							| 
									
										
										
										
											2013-04-13 23:54:02 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (bpmem.triggerEFBCopy.clear) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (g_SWVideoConfig.bHwRasterizer) | 
					
						
							|  |  |  | 					HwRasterizer::Clear(); | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					ClearEfb(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-09 01:37:08 +00:00
										 |  |  | } |